aufs5.15.41-20230828.patch 983 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193361943619536196361973619836199362003620136202362033620436205362063620736208362093621036211362123621336214362153621636217362183621936220362213622236223362243622536226362273622836229362303623136232362333623436235362363623736238362393624036241362423624336244362453624636247362483624936250362513625236253362543625536256362573625836259362603626136262362633626436265362663626736268362693627036271362723627336274362753627636277362783627936280362813628236283362843628536286362873628836289362903629136292362933629436295362963629736298362993630036301363023630336304363053630636307363083630936310363113631236313363143631536316363173631836319363203632136322363233632436325363263632736328363293633036331363323633336334363353633636337363383633936340363413634236343363443634536346363473634836349363503635136352363533635436355363563635736358363593636036361363623636336364363653636636367363683636936370363713637236373363743637536376363773637836379363803638136382363833638436385363863638736388363893639036391363923639336394363953639636397363983639936400364013640236403364043640536406364073640836409364103641136412364133641436415364163641736418364193642036421364223642336424364253642636427364283642936430364313643236433364343643536436364373643836439364403644136442364433644436445364463644736448364493645036451364523645336454364553645636457364583645936460364613646236463364643646536466364673646836469364703647136472364733647436475364763647736478364793648036481364823648336484364853648636487364883648936490364913649236493364943649536496364973649836499365003650136502365033650436505365063650736508365093651036511365123651336514365153651636517365183651936520365213652236523365243652536526365273652836529365303653136532365333653436535365363653736538365393654036541365423654336544365453654636547365483654936550365513655236553365543655536556365573655836559365603656136562365633656436565365663656736568365693657036571365723657336574365753657636577365783657936580365813658236583365843658536586365873658836589365903659136592365933659436595365963659736598365993660036601366023660336604366053660636607366083660936610366113661236613366143661536616366173661836619366203662136622366233662436625366263662736628366293663036631366323663336634366353663636637366383663936640366413664236643366443664536646366473664836649366503665136652366533665436655366563665736658366593666036661366623666336664366653666636667366683666936670366713667236673366743667536676366773667836679366803668136682366833668436685366863668736688366893669036691366923669336694366953669636697366983669936700367013670236703367043670536706367073670836709367103671136712367133671436715367163671736718367193672036721367223672336724367253672636727367283672936730367313673236733367343673536736367373673836739367403674136742367433674436745367463674736748367493675036751367523675336754367553675636757367583675936760367613676236763367643676536766367673676836769367703677136772367733677436775367763677736778367793678036781367823678336784367853678636787367883678936790367913679236793367943679536796367973679836799368003680136802368033680436805368063680736808368093681036811368123681336814368153681636817368183681936820368213682236823368243682536826368273682836829368303683136832368333683436835368363683736838368393684036841368423684336844368453684636847368483684936850368513685236853368543685536856368573685836859368603686136862368633686436865368663686736868368693687036871368723687336874368753687636877368783687936880368813688236883368843688536886368873688836889368903689136892368933689436895368963689736898368993690036901369023690336904369053690636907369083690936910369113691236913369143691536916369173691836919369203692136922369233692436925369263692736928369293693036931369323693336934369353693636937369383693936940369413694236943369443694536946369473694836949369503695136952369533695436955369563695736958369593696036961369623696336964369653696636967369683696936970369713697236973369743697536976369773697836979369803698136982369833698436985369863698736988369893699036991369923699336994369953699636997369983699937000370013700237003370043700537006370073700837009370103701137012370133701437015370163701737018370193702037021370223702337024370253702637027370283702937030370313703237033370343703537036370373703837039370403704137042370433704437045370463704737048370493705037051370523705337054370553705637057370583705937060370613706237063370643706537066370673706837069370703707137072370733707437075370763707737078370793708037081370823708337084370853708637087370883708937090370913709237093370943709537096370973709837099371003710137102371033710437105371063710737108371093711037111371123711337114371153711637117371183711937120371213712237123371243712537126371273712837129371303713137132371333713437135371363713737138371393714037141371423714337144371453714637147371483714937150371513715237153371543715537156371573715837159371603716137162371633716437165371663716737168371693717037171371723717337174371753717637177371783717937180371813718237183371843718537186371873718837189371903719137192371933719437195371963719737198371993720037201372023720337204372053720637207372083720937210372113721237213372143721537216372173721837219372203722137222372233722437225372263722737228372293723037231372323723337234372353723637237372383723937240372413724237243372443724537246372473724837249372503725137252372533725437255372563725737258372593726037261372623726337264372653726637267372683726937270372713727237273372743727537276372773727837279372803728137282372833728437285372863728737288372893729037291372923729337294372953729637297372983729937300373013730237303373043730537306373073730837309373103731137312373133731437315373163731737318373193732037321373223732337324373253732637327373283732937330373313733237333373343733537336373373733837339373403734137342373433734437345373463734737348373493735037351373523735337354373553735637357373583735937360373613736237363373643736537366373673736837369373703737137372373733737437375373763737737378373793738037381373823738337384373853738637387373883738937390373913739237393373943739537396373973739837399374003740137402374033740437405374063740737408374093741037411374123741337414374153741637417374183741937420374213742237423374243742537426374273742837429374303743137432374333743437435374363743737438374393744037441374423744337444374453744637447374483744937450374513745237453374543745537456374573745837459374603746137462374633746437465374663746737468374693747037471374723747337474374753747637477374783747937480374813748237483374843748537486374873748837489374903749137492374933749437495374963749737498374993750037501375023750337504375053750637507375083750937510375113751237513375143751537516375173751837519375203752137522375233752437525375263752737528375293753037531375323753337534375353753637537375383753937540375413754237543375443754537546375473754837549375503755137552375533755437555375563755737558375593756037561375623756337564375653756637567375683756937570375713757237573375743757537576375773757837579375803758137582375833758437585375863758737588375893759037591375923759337594375953759637597375983759937600376013760237603376043760537606376073760837609376103761137612376133761437615376163761737618376193762037621376223762337624376253762637627376283762937630376313763237633376343763537636376373763837639376403764137642376433764437645376463764737648376493765037651376523765337654376553765637657376583765937660376613766237663376643766537666376673766837669376703767137672376733767437675376763767737678376793768037681376823768337684376853768637687376883768937690376913769237693376943769537696376973769837699377003770137702377033770437705377063770737708377093771037711377123771337714377153771637717377183771937720377213772237723377243772537726377273772837729377303773137732377333773437735377363773737738377393774037741377423774337744377453774637747377483774937750377513775237753377543775537756377573775837759377603776137762377633776437765377663776737768377693777037771377723777337774377753777637777377783777937780377813778237783377843778537786377873778837789377903779137792377933779437795377963779737798377993780037801378023780337804378053780637807378083780937810378113781237813378143781537816378173781837819378203782137822378233782437825378263782737828378293783037831378323783337834378353783637837378383783937840378413784237843378443784537846378473784837849378503785137852378533785437855378563785737858378593786037861378623786337864378653786637867378683786937870378713787237873378743787537876378773787837879378803788137882378833788437885378863788737888378893789037891378923789337894378953789637897378983789937900379013790237903379043790537906379073790837909379103791137912379133791437915379163791737918379193792037921379223792337924379253792637927379283792937930379313793237933379343793537936379373793837939379403794137942379433794437945379463794737948379493795037951379523795337954379553795637957379583795937960379613796237963379643796537966379673796837969379703797137972379733797437975379763797737978379793798037981379823798337984379853798637987379883798937990379913799237993379943799537996379973799837999380003800138002380033800438005380063800738008380093801038011380123801338014380153801638017380183801938020380213802238023380243802538026380273802838029380303803138032380333803438035380363803738038380393804038041380423804338044380453804638047380483804938050380513805238053380543805538056380573805838059380603806138062380633806438065380663806738068380693807038071380723807338074380753807638077380783807938080380813808238083380843808538086380873808838089380903809138092380933809438095380963809738098380993810038101381023810338104381053810638107381083810938110381113811238113381143811538116381173811838119381203812138122381233812438125381263812738128381293813038131381323813338134381353813638137381383813938140381413814238143381443814538146381473814838149381503815138152381533815438155381563815738158381593816038161381623816338164381653816638167381683816938170381713817238173381743817538176381773817838179381803818138182381833818438185381863818738188381893819038191381923819338194381953819638197381983819938200382013820238203382043820538206382073820838209382103821138212382133821438215382163821738218382193822038221382223822338224382253822638227382283822938230382313823238233382343823538236382373823838239382403824138242382433824438245382463824738248382493825038251382523825338254382553825638257382583825938260382613826238263382643826538266382673826838269382703827138272382733827438275382763827738278382793828038281382823828338284382853828638287382883828938290382913829238293382943829538296382973829838299383003830138302383033830438305383063830738308383093831038311383123831338314383153831638317383183831938320383213832238323383243832538326383273832838329383303833138332383333833438335383363833738338383393834038341383423834338344383453834638347383483834938350383513835238353383543835538356383573835838359383603836138362383633836438365383663836738368383693837038371383723837338374383753837638377383783837938380383813838238383383843838538386383873838838389383903839138392383933839438395383963839738398383993840038401384023840338404384053840638407384083840938410384113841238413384143841538416384173841838419384203842138422384233842438425384263842738428384293843038431384323843338434384353843638437384383843938440384413844238443384443844538446384473844838449384503845138452384533845438455384563845738458384593846038461384623846338464384653846638467384683846938470384713847238473384743847538476384773847838479384803848138482384833848438485384863848738488384893849038491
  1. diff -Naur null/Documentation/ABI/testing/debugfs-aufs linux-5.15.131/Documentation/ABI/testing/debugfs-aufs
  2. --- /dev/null
  3. +++ linux-5.15.131/Documentation/ABI/testing/debugfs-aufs 2023-09-11 14:32:48.142722627 +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-5.15.131/Documentation/ABI/testing/sysfs-aufs
  61. --- /dev/null
  62. +++ linux-5.15.131/Documentation/ABI/testing/sysfs-aufs 2023-09-11 14:32:48.142722627 +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-5.15.131/Documentation/filesystems/aufs/design/01intro.txt
  96. --- /dev/null
  97. +++ linux-5.15.131/Documentation/filesystems/aufs/design/01intro.txt 2023-09-11 14:32:48.142722627 +0300
  98. @@ -0,0 +1,171 @@
  99. +
  100. +# Copyright (C) 2005-2021 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-5.15.131/Documentation/filesystems/aufs/design/02struct.txt
  271. --- /dev/null
  272. +++ linux-5.15.131/Documentation/filesystems/aufs/design/02struct.txt 2023-09-11 14:32:48.143722614 +0300
  273. @@ -0,0 +1,258 @@
  274. +
  275. +# Copyright (C) 2005-2021 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-5.15.131/Documentation/filesystems/aufs/design/03atomic_open.txt
  533. --- /dev/null
  534. +++ linux-5.15.131/Documentation/filesystems/aufs/design/03atomic_open.txt 2023-09-11 14:32:48.143722614 +0300
  535. @@ -0,0 +1,85 @@
  536. +
  537. +# Copyright (C) 2015-2021 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-5.15.131/Documentation/filesystems/aufs/design/03lookup.txt
  622. --- /dev/null
  623. +++ linux-5.15.131/Documentation/filesystems/aufs/design/03lookup.txt 2023-09-11 14:32:48.143722614 +0300
  624. @@ -0,0 +1,113 @@
  625. +
  626. +# Copyright (C) 2005-2021 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-5.15.131/Documentation/filesystems/aufs/design/04branch.txt
  739. --- /dev/null
  740. +++ linux-5.15.131/Documentation/filesystems/aufs/design/04branch.txt 2023-09-11 14:32:48.143722614 +0300
  741. @@ -0,0 +1,74 @@
  742. +
  743. +# Copyright (C) 2005-2021 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-5.15.131/Documentation/filesystems/aufs/design/05wbr_policy.txt
  817. --- /dev/null
  818. +++ linux-5.15.131/Documentation/filesystems/aufs/design/05wbr_policy.txt 2023-09-11 14:32:48.143722614 +0300
  819. @@ -0,0 +1,64 @@
  820. +
  821. +# Copyright (C) 2005-2021 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-5.15.131/Documentation/filesystems/aufs/design/06dirren.dot
  885. --- /dev/null
  886. +++ linux-5.15.131/Documentation/filesystems/aufs/design/06dirren.dot 2023-09-11 14:32:48.143722614 +0300
  887. @@ -0,0 +1,31 @@
  888. +
  889. +// to view this graph, run dot(1) command in GRAPHVIZ.
  890. +
  891. +digraph G {
  892. +node [shape=box];
  893. +whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
  894. +
  895. +node [shape=oval];
  896. +
  897. +aufs_rename -> whinfo [label="store/remove"];
  898. +
  899. +node [shape=oval];
  900. +inode_list [label="h_inum list in branch\ncache"];
  901. +
  902. +node [shape=box];
  903. +whinode [label="h_inum list file"];
  904. +
  905. +node [shape=oval];
  906. +brmgmt [label="br_add/del/mod/umount"];
  907. +
  908. +brmgmt -> inode_list [label="create/remove"];
  909. +brmgmt -> whinode [label="load/store"];
  910. +
  911. +inode_list -> whinode [style=dashed,dir=both];
  912. +
  913. +aufs_rename -> inode_list [label="add/del"];
  914. +
  915. +aufs_lookup -> inode_list [label="search"];
  916. +
  917. +aufs_lookup -> whinfo [label="load/remove"];
  918. +}
  919. diff -Naur null/Documentation/filesystems/aufs/design/06dirren.txt linux-5.15.131/Documentation/filesystems/aufs/design/06dirren.txt
  920. --- /dev/null
  921. +++ linux-5.15.131/Documentation/filesystems/aufs/design/06dirren.txt 2023-09-11 14:32:48.143722614 +0300
  922. @@ -0,0 +1,102 @@
  923. +
  924. +# Copyright (C) 2017-2021 Junjiro R. Okajima
  925. +#
  926. +# This program is free software; you can redistribute it and/or modify
  927. +# it under the terms of the GNU General Public License as published by
  928. +# the Free Software Foundation; either version 2 of the License, or
  929. +# (at your option) any later version.
  930. +#
  931. +# This program is distributed in the hope that it will be useful,
  932. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  933. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  934. +# GNU General Public License for more details.
  935. +#
  936. +# You should have received a copy of the GNU General Public License
  937. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  938. +
  939. +Special handling for renaming a directory (DIRREN)
  940. +----------------------------------------------------------------------
  941. +First, let's assume we have a simple usecase.
  942. +
  943. +- /u = /rw + /ro
  944. +- /rw/dirA exists
  945. +- /ro/dirA and /ro/dirA/file exist too
  946. +- there is no dirB on both branches
  947. +- a user issues rename("dirA", "dirB")
  948. +
  949. +Now, what should aufs behave against this rename(2)?
  950. +There are a few possible cases.
  951. +
  952. +A. returns EROFS.
  953. + since dirA exists on a readonly branch which cannot be renamed.
  954. +B. returns EXDEV.
  955. + it is possible to copy-up dirA (only the dir itself), but the child
  956. + entries ("file" in this case) should not be. it must be a bad
  957. + approach to copy-up recursively.
  958. +C. returns a success.
  959. + even the branch /ro is readonly, aufs tries renaming it. Obviously it
  960. + is a violation of aufs' policy.
  961. +D. construct an extra information which indicates that /ro/dirA should
  962. + be handled as the name of dirB.
  963. + overlayfs has a similar feature called REDIRECT.
  964. +
  965. +Until now, aufs implements the case B only which returns EXDEV, and
  966. +expects the userspace application behaves like mv(1) which tries
  967. +issueing rename(2) recursively.
  968. +
  969. +A new aufs feature called DIRREN is introduced which implements the case
  970. +D. There are several "extra information" added.
  971. +
  972. +1. detailed info per renamed directory
  973. + path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
  974. +2. the inode-number list of directories on a branch
  975. + path: /rw/dirB/$AUFS_WH_DR_BRHINO
  976. +
  977. +The filename of "detailed info per directory" represents the lower
  978. +branch, and its format is
  979. +- a type of the branch id
  980. + one of these.
  981. + + uuid (not implemented yet)
  982. + + fsid
  983. + + dev
  984. +- the inode-number of the branch root dir
  985. +
  986. +And it contains these info in a single regular file.
  987. +- magic number
  988. +- branch's inode-number of the logically renamed dir
  989. +- the name of the before-renamed dir
  990. +
  991. +The "detailed info per directory" file is created in aufs rename(2), and
  992. +loaded in any lookup.
  993. +The info is considered in lookup for the matching case only. Here
  994. +"matching" means that the root of branch (in the info filename) is same
  995. +to the current looking-up branch. After looking-up the before-renamed
  996. +name, the inode-number is compared. And the matched dentry is used.
  997. +
  998. +The "inode-number list of directories" is a regular file which contains
  999. +simply the inode-numbers on the branch. The file is created or updated
  1000. +in removing the branch, and loaded in adding the branch. Its lifetime is
  1001. +equal to the branch.
  1002. +The list is referred in lookup, and when the current target inode is
  1003. +found in the list, the aufs tries loading the "detailed info per
  1004. +directory" and get the changed and valid name of the dir.
  1005. +
  1006. +Theoretically these "extra informaiton" may be able to be put into XATTR
  1007. +in the dir inode. But aufs doesn't choose this way because
  1008. +1. XATTR may not be supported by the branch (or its configuration)
  1009. +2. XATTR may have its size limit.
  1010. +3. XATTR may be less easy to convert than a regular file, when the
  1011. + format of the info is changed in the future.
  1012. +At the same time, I agree that the regular file approach is much slower
  1013. +than XATTR approach. So, in the future, aufs may take the XATTR or other
  1014. +better approach.
  1015. +
  1016. +This DIRREN feature is enabled by aufs configuration, and is activated
  1017. +by a new mount option.
  1018. +
  1019. +For the more complicated case, there is a work with UDBA option, which
  1020. +is to dected the direct access to the branches (by-passing aufs) and to
  1021. +maintain the cashes in aufs. Since a single cached aufs dentry may
  1022. +contains two names, before- and after-rename, the name comparision in
  1023. +UDBA handler may not work correctly. In this case, the behaviour will be
  1024. +equivalen to udba=reval case.
  1025. diff -Naur null/Documentation/filesystems/aufs/design/06fhsm.txt linux-5.15.131/Documentation/filesystems/aufs/design/06fhsm.txt
  1026. --- /dev/null
  1027. +++ linux-5.15.131/Documentation/filesystems/aufs/design/06fhsm.txt 2023-09-11 14:32:48.143722614 +0300
  1028. @@ -0,0 +1,120 @@
  1029. +
  1030. +# Copyright (C) 2011-2021 Junjiro R. Okajima
  1031. +#
  1032. +# This program is free software; you can redistribute it and/or modify
  1033. +# it under the terms of the GNU General Public License as published by
  1034. +# the Free Software Foundation; either version 2 of the License, or
  1035. +# (at your option) any later version.
  1036. +#
  1037. +# This program is distributed in the hope that it will be useful,
  1038. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1039. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1040. +# GNU General Public License for more details.
  1041. +#
  1042. +# You should have received a copy of the GNU General Public License
  1043. +# along with this program; if not, write to the Free Software
  1044. +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  1045. +
  1046. +
  1047. +File-based Hierarchical Storage Management (FHSM)
  1048. +----------------------------------------------------------------------
  1049. +Hierarchical Storage Management (or HSM) is a well-known feature in the
  1050. +storage world. Aufs provides this feature as file-based with multiple
  1051. +writable branches, based upon the principle of "Colder, the Lower".
  1052. +Here the word "colder" means that the less used files, and "lower" means
  1053. +that the position in the order of the stacked branches vertically.
  1054. +These multiple writable branches are prioritized, ie. the topmost one
  1055. +should be the fastest drive and be used heavily.
  1056. +
  1057. +o Characters in aufs FHSM story
  1058. +- aufs itself and a new branch attribute.
  1059. +- a new ioctl interface to move-down and to establish a connection with
  1060. + the daemon ("move-down" is a converse of "copy-up").
  1061. +- userspace tool and daemon.
  1062. +
  1063. +The userspace daemon establishes a connection with aufs and waits for
  1064. +the notification. The notified information is very similar to struct
  1065. +statfs containing the number of consumed blocks and inodes.
  1066. +When the consumed blocks/inodes of a branch exceeds the user-specified
  1067. +upper watermark, the daemon activates its move-down process until the
  1068. +consumed blocks/inodes reaches the user-specified lower watermark.
  1069. +
  1070. +The actual move-down is done by aufs based upon the request from
  1071. +user-space since we need to maintain the inode number and the internal
  1072. +pointer arrays in aufs.
  1073. +
  1074. +Currently aufs FHSM handles the regular files only. Additionally they
  1075. +must not be hard-linked nor pseudo-linked.
  1076. +
  1077. +
  1078. +o Cowork of aufs and the user-space daemon
  1079. + During the userspace daemon established the connection, aufs sends a
  1080. + small notification to it whenever aufs writes something into the
  1081. + writable branch. But it may cost high since aufs issues statfs(2)
  1082. + internally. So user can specify a new option to cache the
  1083. + info. Actually the notification is controlled by these factors.
  1084. + + the specified cache time.
  1085. + + classified as "force" by aufs internally.
  1086. + Until the specified time expires, aufs doesn't send the info
  1087. + except the forced cases. When aufs decide forcing, the info is always
  1088. + notified to userspace.
  1089. + For example, the number of free inodes is generally large enough and
  1090. + the shortage of it happens rarely. So aufs doesn't force the
  1091. + notification when creating a new file, directory and others. This is
  1092. + the typical case which aufs doesn't force.
  1093. + When aufs writes the actual filedata and the files consumes any of new
  1094. + blocks, the aufs forces notifying.
  1095. +
  1096. +
  1097. +o Interfaces in aufs
  1098. +- New branch attribute.
  1099. + + fhsm
  1100. + Specifies that the branch is managed by FHSM feature. In other word,
  1101. + participant in the FHSM.
  1102. + When nofhsm is set to the branch, it will not be the source/target
  1103. + branch of the move-down operation. This attribute is set
  1104. + independently from coo and moo attributes, and if you want full
  1105. + FHSM, you should specify them as well.
  1106. +- New mount option.
  1107. + + fhsm_sec
  1108. + Specifies a second to suppress many less important info to be
  1109. + notified.
  1110. +- New ioctl.
  1111. + + AUFS_CTL_FHSM_FD
  1112. + create a new file descriptor which userspace can read the notification
  1113. + (a subset of struct statfs) from aufs.
  1114. +- Module parameter 'brs'
  1115. + It has to be set to 1. Otherwise the new mount option 'fhsm' will not
  1116. + be set.
  1117. +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
  1118. + When there are two or more branches with fhsm attributes,
  1119. + /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
  1120. + terminates it. As a result of remounting and branch-manipulation, the
  1121. + number of branches with fhsm attribute can be one. In this case,
  1122. + /sbin/mount.aufs will terminate the user-space daemon.
  1123. +
  1124. +
  1125. +Finally the operation is done as these steps in kernel-space.
  1126. +- make sure that,
  1127. + + no one else is using the file.
  1128. + + the file is not hard-linked.
  1129. + + the file is not pseudo-linked.
  1130. + + the file is a regular file.
  1131. + + the parent dir is not opaqued.
  1132. +- find the target writable branch.
  1133. +- make sure the file is not whiteout-ed by the upper (than the target)
  1134. + branch.
  1135. +- make the parent dir on the target branch.
  1136. +- mutex lock the inode on the branch.
  1137. +- unlink the whiteout on the target branch (if exists).
  1138. +- lookup and create the whiteout-ed temporary name on the target branch.
  1139. +- copy the file as the whiteout-ed temporary name on the target branch.
  1140. +- rename the whiteout-ed temporary name to the original name.
  1141. +- unlink the file on the source branch.
  1142. +- maintain the internal pointer array and the external inode number
  1143. + table (XINO).
  1144. +- maintain the timestamps and other attributes of the parent dir and the
  1145. + file.
  1146. +
  1147. +And of course, in every step, an error may happen. So the operation
  1148. +should restore the original file state after an error happens.
  1149. diff -Naur null/Documentation/filesystems/aufs/design/06mmap.txt linux-5.15.131/Documentation/filesystems/aufs/design/06mmap.txt
  1150. --- /dev/null
  1151. +++ linux-5.15.131/Documentation/filesystems/aufs/design/06mmap.txt 2023-09-11 14:32:48.143722614 +0300
  1152. @@ -0,0 +1,72 @@
  1153. +
  1154. +# Copyright (C) 2005-2021 Junjiro R. Okajima
  1155. +#
  1156. +# This program is free software; you can redistribute it and/or modify
  1157. +# it under the terms of the GNU General Public License as published by
  1158. +# the Free Software Foundation; either version 2 of the License, or
  1159. +# (at your option) any later version.
  1160. +#
  1161. +# This program is distributed in the hope that it will be useful,
  1162. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1163. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1164. +# GNU General Public License for more details.
  1165. +#
  1166. +# You should have received a copy of the GNU General Public License
  1167. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  1168. +
  1169. +mmap(2) -- File Memory Mapping
  1170. +----------------------------------------------------------------------
  1171. +In aufs, the file-mapped pages are handled by a branch fs directly, no
  1172. +interaction with aufs. It means aufs_mmap() calls the branch fs's
  1173. +->mmap().
  1174. +This approach is simple and good, but there is one problem.
  1175. +Under /proc, several entries show the mmapped files by its path (with
  1176. +device and inode number), and the printed path will be the path on the
  1177. +branch fs's instead of virtual aufs's.
  1178. +This is not a problem in most cases, but some utilities lsof(1) (and its
  1179. +user) may expect the path on aufs.
  1180. +
  1181. +To address this issue, aufs adds a new member called vm_prfile in struct
  1182. +vm_area_struct (and struct vm_region). The original vm_file points to
  1183. +the file on the branch fs in order to handle everything correctly as
  1184. +usual. The new vm_prfile points to a virtual file in aufs, and the
  1185. +show-functions in procfs refers to vm_prfile if it is set.
  1186. +Also we need to maintain several other places where touching vm_file
  1187. +such like
  1188. +- fork()/clone() copies vma and the reference count of vm_file is
  1189. + incremented.
  1190. +- merging vma maintains the ref count too.
  1191. +
  1192. +This is not a good approach. It just fakes the printed path. But it
  1193. +leaves all behaviour around f_mapping unchanged. This is surely an
  1194. +advantage.
  1195. +Actually aufs had adopted another complicated approach which calls
  1196. +generic_file_mmap() and handles struct vm_operations_struct. In this
  1197. +approach, aufs met a hard problem and I could not solve it without
  1198. +switching the approach.
  1199. +
  1200. +There may be one more another approach which is
  1201. +- bind-mount the branch-root onto the aufs-root internally
  1202. +- grab the new vfsmount (ie. struct mount)
  1203. +- lazy-umount the branch-root internally
  1204. +- in open(2) the aufs-file, open the branch-file with the hidden
  1205. + vfsmount (instead of the original branch's vfsmount)
  1206. +- ideally this "bind-mount and lazy-umount" should be done atomically,
  1207. + but it may be possible from userspace by the mount helper.
  1208. +
  1209. +Adding the internal hidden vfsmount and using it in opening a file, the
  1210. +file path under /proc will be printed correctly. This approach looks
  1211. +smarter, but is not possible I am afraid.
  1212. +- aufs-root may be bind-mount later. when it happens, another hidden
  1213. + vfsmount will be required.
  1214. +- it is hard to get the chance to bind-mount and lazy-umount
  1215. + + in kernel-space, FS can have vfsmount in open(2) via
  1216. + file->f_path, and aufs can know its vfsmount. But several locks are
  1217. + already acquired, and if aufs tries to bind-mount and lazy-umount
  1218. + here, then it may cause a deadlock.
  1219. + + in user-space, bind-mount doesn't invoke the mount helper.
  1220. +- since /proc shows dev and ino, aufs has to give vma these info. it
  1221. + means a new member vm_prinode will be necessary. this is essentially
  1222. + equivalent to vm_prfile described above.
  1223. +
  1224. +I have to give up this "looks-smater" approach.
  1225. diff -Naur null/Documentation/filesystems/aufs/design/06xattr.txt linux-5.15.131/Documentation/filesystems/aufs/design/06xattr.txt
  1226. --- /dev/null
  1227. +++ linux-5.15.131/Documentation/filesystems/aufs/design/06xattr.txt 2023-09-11 14:32:48.143722614 +0300
  1228. @@ -0,0 +1,96 @@
  1229. +
  1230. +# Copyright (C) 2014-2021 Junjiro R. Okajima
  1231. +#
  1232. +# This program is free software; you can redistribute it and/or modify
  1233. +# it under the terms of the GNU General Public License as published by
  1234. +# the Free Software Foundation; either version 2 of the License, or
  1235. +# (at your option) any later version.
  1236. +#
  1237. +# This program is distributed in the hope that it will be useful,
  1238. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1239. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1240. +# GNU General Public License for more details.
  1241. +#
  1242. +# You should have received a copy of the GNU General Public License
  1243. +# along with this program; if not, write to the Free Software
  1244. +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  1245. +
  1246. +
  1247. +Listing XATTR/EA and getting the value
  1248. +----------------------------------------------------------------------
  1249. +For the inode standard attributes (owner, group, timestamps, etc.), aufs
  1250. +shows the values from the topmost existing file. This behaviour is good
  1251. +for the non-dir entries since the bahaviour exactly matches the shown
  1252. +information. But for the directories, aufs considers all the same named
  1253. +entries on the lower branches. Which means, if one of the lower entry
  1254. +rejects readdir call, then aufs returns an error even if the topmost
  1255. +entry allows it. This behaviour is necessary to respect the branch fs's
  1256. +security, but can make users confused since the user-visible standard
  1257. +attributes don't match the behaviour.
  1258. +To address this issue, aufs has a mount option called dirperm1 which
  1259. +checks the permission for the topmost entry only, and ignores the lower
  1260. +entry's permission.
  1261. +
  1262. +A similar issue can happen around XATTR.
  1263. +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
  1264. +always set. Otherwise these very unpleasant situation would happen.
  1265. +- listxattr(2) may return the duplicated entries.
  1266. +- users may not be able to remove or reset the XATTR forever,
  1267. +
  1268. +
  1269. +XATTR/EA support in the internal (copy,move)-(up,down)
  1270. +----------------------------------------------------------------------
  1271. +Generally the extended attributes of inode are categorized as these.
  1272. +- "security" for LSM and capability.
  1273. +- "system" for posix ACL, 'acl' mount option is required for the branch
  1274. + fs generally.
  1275. +- "trusted" for userspace, CAP_SYS_ADMIN is required.
  1276. +- "user" for userspace, 'user_xattr' mount option is required for the
  1277. + branch fs generally.
  1278. +
  1279. +Moreover there are some other categories. Aufs handles these rather
  1280. +unpopular categories as the ordinary ones, ie. there is no special
  1281. +condition nor exception.
  1282. +
  1283. +In copy-up, the support for XATTR on the dst branch may differ from the
  1284. +src branch. In this case, the copy-up operation will get an error and
  1285. +the original user operation which triggered the copy-up will fail. It
  1286. +can happen that even all copy-up will fail.
  1287. +When both of src and dst branches support XATTR and if an error occurs
  1288. +during copying XATTR, then the copy-up should fail obviously. That is a
  1289. +good reason and aufs should return an error to userspace. But when only
  1290. +the src branch support that XATTR, aufs should not return an error.
  1291. +For example, the src branch supports ACL but the dst branch doesn't
  1292. +because the dst branch may natively un-support it or temporary
  1293. +un-support it due to "noacl" mount option. Of course, the dst branch fs
  1294. +may NOT return an error even if the XATTR is not supported. It is
  1295. +totally up to the branch fs.
  1296. +
  1297. +Anyway when the aufs internal copy-up gets an error from the dst branch
  1298. +fs, then aufs tries removing the just copied entry and returns the error
  1299. +to the userspace. The worst case of this situation will be all copy-up
  1300. +will fail.
  1301. +
  1302. +For the copy-up operation, there two basic approaches.
  1303. +- copy the specified XATTR only (by category above), and return the
  1304. + error unconditionally if it happens.
  1305. +- copy all XATTR, and ignore the error on the specified category only.
  1306. +
  1307. +In order to support XATTR and to implement the correct behaviour, aufs
  1308. +chooses the latter approach and introduces some new branch attributes,
  1309. +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
  1310. +They correspond to the XATTR namespaces (see above). Additionally, to be
  1311. +convenient, "icex" is also provided which means all "icex*" attributes
  1312. +are set (here the word "icex" stands for "ignore copy-error on XATTR").
  1313. +
  1314. +The meaning of these attributes is to ignore the error from setting
  1315. +XATTR on that branch.
  1316. +Note that aufs tries copying all XATTR unconditionally, and ignores the
  1317. +error from the dst branch according to the specified attributes.
  1318. +
  1319. +Some XATTR may have its default value. The default value may come from
  1320. +the parent dir or the environment. If the default value is set at the
  1321. +file creating-time, it will be overwritten by copy-up.
  1322. +Some contradiction may happen I am afraid.
  1323. +Do we need another attribute to stop copying XATTR? I am unsure. For
  1324. +now, aufs implements the branch attributes to ignore the error.
  1325. diff -Naur null/Documentation/filesystems/aufs/design/07export.txt linux-5.15.131/Documentation/filesystems/aufs/design/07export.txt
  1326. --- /dev/null
  1327. +++ linux-5.15.131/Documentation/filesystems/aufs/design/07export.txt 2023-09-11 14:32:48.143722614 +0300
  1328. @@ -0,0 +1,58 @@
  1329. +
  1330. +# Copyright (C) 2005-2021 Junjiro R. Okajima
  1331. +#
  1332. +# This program is free software; you can redistribute it and/or modify
  1333. +# it under the terms of the GNU General Public License as published by
  1334. +# the Free Software Foundation; either version 2 of the License, or
  1335. +# (at your option) any later version.
  1336. +#
  1337. +# This program is distributed in the hope that it will be useful,
  1338. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1339. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1340. +# GNU General Public License for more details.
  1341. +#
  1342. +# You should have received a copy of the GNU General Public License
  1343. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  1344. +
  1345. +Export Aufs via NFS
  1346. +----------------------------------------------------------------------
  1347. +Here is an approach.
  1348. +- like xino/xib, add a new file 'xigen' which stores aufs inode
  1349. + generation.
  1350. +- iget_locked(): initialize aufs inode generation for a new inode, and
  1351. + store it in xigen file.
  1352. +- destroy_inode(): increment aufs inode generation and store it in xigen
  1353. + file. it is necessary even if it is not unlinked, because any data of
  1354. + inode may be changed by UDBA.
  1355. +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
  1356. + build file handle by
  1357. + + branch id (4 bytes)
  1358. + + superblock generation (4 bytes)
  1359. + + inode number (4 or 8 bytes)
  1360. + + parent dir inode number (4 or 8 bytes)
  1361. + + inode generation (4 bytes))
  1362. + + return value of exportfs_encode_fh() for the parent on a branch (4
  1363. + bytes)
  1364. + + file handle for a branch (by exportfs_encode_fh())
  1365. +- fh_to_dentry():
  1366. + + find the index of a branch from its id in handle, and check it is
  1367. + still exist in aufs.
  1368. + + 1st level: get the inode number from handle and search it in cache.
  1369. + + 2nd level: if not found in cache, get the parent inode number from
  1370. + the handle and search it in cache. and then open the found parent
  1371. + dir, find the matching inode number by vfs_readdir() and get its
  1372. + name, and call lookup_one_len() for the target dentry.
  1373. + + 3rd level: if the parent dir is not cached, call
  1374. + exportfs_decode_fh() for a branch and get the parent on a branch,
  1375. + build a pathname of it, convert it a pathname in aufs, call
  1376. + path_lookup(). now aufs gets a parent dir dentry, then handle it as
  1377. + the 2nd level.
  1378. + + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
  1379. + for every branch, but not itself. to get this, (currently) aufs
  1380. + searches in current->nsproxy->mnt_ns list. it may not be a good
  1381. + idea, but I didn't get other approach.
  1382. + + test the generation of the gotten inode.
  1383. +- every inode operation: they may get EBUSY due to UDBA. in this case,
  1384. + convert it into ESTALE for NFSD.
  1385. +- readdir(): call lockdep_on/off() because filldir in NFSD calls
  1386. + lookup_one_len(), vfs_getattr(), encode_fh() and others.
  1387. diff -Naur null/Documentation/filesystems/aufs/design/08shwh.txt linux-5.15.131/Documentation/filesystems/aufs/design/08shwh.txt
  1388. --- /dev/null
  1389. +++ linux-5.15.131/Documentation/filesystems/aufs/design/08shwh.txt 2023-09-11 14:32:48.143722614 +0300
  1390. @@ -0,0 +1,52 @@
  1391. +
  1392. +# Copyright (C) 2005-2021 Junjiro R. Okajima
  1393. +#
  1394. +# This program is free software; you can redistribute it and/or modify
  1395. +# it under the terms of the GNU General Public License as published by
  1396. +# the Free Software Foundation; either version 2 of the License, or
  1397. +# (at your option) any later version.
  1398. +#
  1399. +# This program is distributed in the hope that it will be useful,
  1400. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1401. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1402. +# GNU General Public License for more details.
  1403. +#
  1404. +# You should have received a copy of the GNU General Public License
  1405. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  1406. +
  1407. +Show Whiteout Mode (shwh)
  1408. +----------------------------------------------------------------------
  1409. +Generally aufs hides the name of whiteouts. But in some cases, to show
  1410. +them is very useful for users. For instance, creating a new middle layer
  1411. +(branch) by merging existing layers.
  1412. +
  1413. +(borrowing aufs1 HOW-TO from a user, Michael Towers)
  1414. +When you have three branches,
  1415. +- Bottom: 'system', squashfs (underlying base system), read-only
  1416. +- Middle: 'mods', squashfs, read-only
  1417. +- Top: 'overlay', ram (tmpfs), read-write
  1418. +
  1419. +The top layer is loaded at boot time and saved at shutdown, to preserve
  1420. +the changes made to the system during the session.
  1421. +When larger changes have been made, or smaller changes have accumulated,
  1422. +the size of the saved top layer data grows. At this point, it would be
  1423. +nice to be able to merge the two overlay branches ('mods' and 'overlay')
  1424. +and rewrite the 'mods' squashfs, clearing the top layer and thus
  1425. +restoring save and load speed.
  1426. +
  1427. +This merging is simplified by the use of another aufs mount, of just the
  1428. +two overlay branches using the 'shwh' option.
  1429. +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
  1430. + aufs /livesys/merge_union
  1431. +
  1432. +A merged view of these two branches is then available at
  1433. +/livesys/merge_union, and the new feature is that the whiteouts are
  1434. +visible!
  1435. +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
  1436. +writing to all branches. Also the default mode for all branches is 'ro'.
  1437. +It is now possible to save the combined contents of the two overlay
  1438. +branches to a new squashfs, e.g.:
  1439. +# mksquashfs /livesys/merge_union /path/to/newmods.squash
  1440. +
  1441. +This new squashfs archive can be stored on the boot device and the
  1442. +initramfs will use it to replace the old one at the next boot.
  1443. diff -Naur null/Documentation/filesystems/aufs/design/10dynop.txt linux-5.15.131/Documentation/filesystems/aufs/design/10dynop.txt
  1444. --- /dev/null
  1445. +++ linux-5.15.131/Documentation/filesystems/aufs/design/10dynop.txt 2023-09-11 14:32:48.143722614 +0300
  1446. @@ -0,0 +1,47 @@
  1447. +
  1448. +# Copyright (C) 2010-2021 Junjiro R. Okajima
  1449. +#
  1450. +# This program is free software; you can redistribute it and/or modify
  1451. +# it under the terms of the GNU General Public License as published by
  1452. +# the Free Software Foundation; either version 2 of the License, or
  1453. +# (at your option) any later version.
  1454. +#
  1455. +# This program is distributed in the hope that it will be useful,
  1456. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1457. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1458. +# GNU General Public License for more details.
  1459. +#
  1460. +# You should have received a copy of the GNU General Public License
  1461. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  1462. +
  1463. +Dynamically customizable FS operations
  1464. +----------------------------------------------------------------------
  1465. +Generally FS operations (struct inode_operations, struct
  1466. +address_space_operations, struct file_operations, etc.) are defined as
  1467. +"static const", but it never means that FS have only one set of
  1468. +operation. Some FS have multiple sets of them. For instance, ext2 has
  1469. +three sets, one for XIP, for NOBH, and for normal.
  1470. +Since aufs overrides and redirects these operations, sometimes aufs has
  1471. +to change its behaviour according to the branch FS type. More importantly
  1472. +VFS acts differently if a function (member in the struct) is set or
  1473. +not. It means aufs should have several sets of operations and select one
  1474. +among them according to the branch FS definition.
  1475. +
  1476. +In order to solve this problem and not to affect the behaviour of VFS,
  1477. +aufs defines these operations dynamically. For instance, aufs defines
  1478. +dummy direct_IO function for struct address_space_operations, but it may
  1479. +not be set to the address_space_operations actually. When the branch FS
  1480. +doesn't have it, aufs doesn't set it to its address_space_operations
  1481. +while the function definition itself is still alive. So the behaviour
  1482. +itself will not change, and it will return an error when direct_IO is
  1483. +not set.
  1484. +
  1485. +The lifetime of these dynamically generated operation object is
  1486. +maintained by aufs branch object. When the branch is removed from aufs,
  1487. +the reference counter of the object is decremented. When it reaches
  1488. +zero, the dynamically generated operation object will be freed.
  1489. +
  1490. +This approach is designed to support AIO (io_submit), Direct I/O and
  1491. +XIP (DAX) mainly.
  1492. +Currently this approach is applied to address_space_operations for
  1493. +regular files only.
  1494. diff -Naur null/Documentation/filesystems/aufs/README linux-5.15.131/Documentation/filesystems/aufs/README
  1495. --- /dev/null
  1496. +++ linux-5.15.131/Documentation/filesystems/aufs/README 2023-09-11 14:32:48.142722627 +0300
  1497. @@ -0,0 +1,407 @@
  1498. +
  1499. +Aufs5 -- advanced multi layered unification filesystem version 5.x
  1500. +http://aufs.sf.net
  1501. +Junjiro R. Okajima
  1502. +
  1503. +
  1504. +0. Introduction
  1505. +----------------------------------------
  1506. +In the early days, aufs was entirely re-designed and re-implemented
  1507. +Unionfs Version 1.x series. Adding many original ideas, approaches,
  1508. +improvements and implementations, it became totally different from
  1509. +Unionfs while keeping the basic features.
  1510. +Later, Unionfs Version 2.x series began taking some of the same
  1511. +approaches to aufs1's.
  1512. +Unionfs was being developed by Professor Erez Zadok at Stony Brook
  1513. +University and his team.
  1514. +
  1515. +Aufs5 supports linux-v5.0 and later, If you want older kernel version
  1516. +support,
  1517. +- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
  1518. +- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
  1519. +- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
  1520. + or aufs1 from CVS on SourceForge.
  1521. +
  1522. +Note: the name of aufs5-linux.git and aufs5-standalone.git on github
  1523. + were changed. Now they are aufs-linux.git and
  1524. + aufs-standalone.git and they contain aufs5 and later branches.
  1525. +
  1526. +Note: it becomes clear that "Aufs was rejected. Let's give it up."
  1527. + According to Christoph Hellwig, linux rejects all union-type
  1528. + filesystems but UnionMount.
  1529. +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
  1530. +
  1531. +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
  1532. + UnionMount, and he pointed out an issue around a directory mutex
  1533. + lock and aufs addressed it. But it is still unsure whether aufs will
  1534. + be merged (or any other union solution).
  1535. +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
  1536. +
  1537. +
  1538. +1. Features
  1539. +----------------------------------------
  1540. +- unite several directories into a single virtual filesystem. The member
  1541. + directory is called as a branch.
  1542. +- you can specify the permission flags to the branch, which are 'readonly',
  1543. + 'readwrite' and 'whiteout-able.'
  1544. +- by upper writable branch, internal copyup and whiteout, files/dirs on
  1545. + readonly branch are modifiable logically.
  1546. +- dynamic branch manipulation, add, del.
  1547. +- etc...
  1548. +
  1549. +Also there are many enhancements in aufs, such as:
  1550. +- test only the highest one for the directory permission (dirperm1)
  1551. +- copyup on open (coo=)
  1552. +- 'move' policy for copy-up between two writable branches, after
  1553. + checking free space.
  1554. +- xattr, acl
  1555. +- readdir(3) in userspace.
  1556. +- keep inode number by external inode number table
  1557. +- keep the timestamps of file/dir in internal copyup operation
  1558. +- seekable directory, supporting NFS readdir.
  1559. +- whiteout is hardlinked in order to reduce the consumption of inodes
  1560. + on branch
  1561. +- do not copyup, nor create a whiteout when it is unnecessary
  1562. +- revert a single systemcall when an error occurs in aufs
  1563. +- remount interface instead of ioctl
  1564. +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
  1565. +- loopback mounted filesystem as a branch
  1566. +- kernel thread for removing the dir who has a plenty of whiteouts
  1567. +- support copyup sparse file (a file which has a 'hole' in it)
  1568. +- default permission flags for branches
  1569. +- selectable permission flags for ro branch, whether whiteout can
  1570. + exist or not
  1571. +- export via NFS.
  1572. +- support <sysfs>/fs/aufs and <debugfs>/aufs.
  1573. +- support multiple writable branches, some policies to select one
  1574. + among multiple writable branches.
  1575. +- a new semantics for link(2) and rename(2) to support multiple
  1576. + writable branches.
  1577. +- no glibc changes are required.
  1578. +- pseudo hardlink (hardlink over branches)
  1579. +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
  1580. + including NFS or remote filesystem branch.
  1581. +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
  1582. +- and more...
  1583. +
  1584. +Currently these features are dropped temporary from aufs5.
  1585. +See design/08plan.txt in detail.
  1586. +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
  1587. + (robr)
  1588. +- statistics of aufs thread (/sys/fs/aufs/stat)
  1589. +
  1590. +Features or just an idea in the future (see also design/*.txt),
  1591. +- reorder the branch index without del/re-add.
  1592. +- permanent xino files for NFSD
  1593. +- an option for refreshing the opened files after add/del branches
  1594. +- light version, without branch manipulation. (unnecessary?)
  1595. +- copyup in userspace
  1596. +- inotify in userspace
  1597. +- readv/writev
  1598. +
  1599. +
  1600. +2. Download
  1601. +----------------------------------------
  1602. +There are three GIT trees for aufs5, aufs-linux.git,
  1603. +aufs-standalone.git, and aufs-util.git.
  1604. +While the aufs-util is always necessary, you need either of aufs-linux
  1605. +or aufs-standalone.
  1606. +
  1607. +The aufs-linux tree includes the whole linux mainline GIT tree,
  1608. +git://git.kernel.org/.../torvalds/linux.git.
  1609. +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
  1610. +build aufs5 as an external kernel module.
  1611. +Several extra patches are not included in this tree. Only
  1612. +aufs-standalone tree contains them. They are described in the later
  1613. +section "Configuration and Compilation."
  1614. +
  1615. +On the other hand, the aufs-standalone tree has only aufs source files
  1616. +and necessary patches, and you can select CONFIG_AUFS_FS=m.
  1617. +But you need to apply all aufs patches manually.
  1618. +
  1619. +You will find GIT branches whose name is in form of "aufs5.x" where "x"
  1620. +represents the linux kernel version, "linux-5.x". For instance,
  1621. +"aufs5.0" is for linux-5.0. For latest "linux-5.x-rcN", use
  1622. +"aufs5.x-rcN" branch.
  1623. +
  1624. +o aufs-linux tree
  1625. +$ git clone --reference /your/linux/git/tree \
  1626. + git://github.com/sfjro/aufs-linux.git aufs-linux.git
  1627. +- if you don't have linux GIT tree, then remove "--reference ..."
  1628. +$ cd aufs-linux.git
  1629. +$ git checkout origin/aufs5.0
  1630. +
  1631. +Or You may want to directly git-pull aufs into your linux GIT tree, and
  1632. +leave the patch-work to GIT.
  1633. +$ cd /your/linux/git/tree
  1634. +$ git remote add aufs git://github.com/sfjro/aufs-linux.git
  1635. +$ git fetch aufs
  1636. +$ git checkout -b my5.0 v5.0
  1637. +$ (add your local change...)
  1638. +$ git pull aufs aufs5.0
  1639. +- now you have v5.0 + your_changes + aufs5.0 in you my5.0 branch.
  1640. +- you may need to solve some conflicts between your_changes and
  1641. + aufs5.0. in this case, git-rerere is recommended so that you can
  1642. + solve the similar conflicts automatically when you upgrade to 5.1 or
  1643. + later in the future.
  1644. +
  1645. +o aufs-standalone tree
  1646. +$ git clone git://github.com/sfjro/aufs-standalone.git aufs-standalone.git
  1647. +$ cd aufs-standalone.git
  1648. +$ git checkout origin/aufs5.0
  1649. +
  1650. +o aufs-util tree
  1651. +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
  1652. +- note that the public aufs-util.git is on SourceForge instead of
  1653. + GitHUB.
  1654. +$ cd aufs-util.git
  1655. +$ git checkout origin/aufs5.0
  1656. +
  1657. +Note: The 5.x-rcN branch is to be used with `rc' kernel versions ONLY.
  1658. +The minor version number, 'x' in '5.x', of aufs may not always
  1659. +follow the minor version number of the kernel.
  1660. +Because changes in the kernel that cause the use of a new
  1661. +minor version number do not always require changes to aufs-util.
  1662. +
  1663. +Since aufs-util has its own minor version number, you may not be
  1664. +able to find a GIT branch in aufs-util for your kernel's
  1665. +exact minor version number.
  1666. +In this case, you should git-checkout the branch for the
  1667. +nearest lower number.
  1668. +
  1669. +For (an unreleased) example:
  1670. +If you are using "linux-5.10" and the "aufs5.10" branch
  1671. +does not exist in aufs-util repository, then "aufs5.9", "aufs5.8"
  1672. +or something numerically smaller is the branch for your kernel.
  1673. +
  1674. +Also you can view all branches by
  1675. + $ git branch -a
  1676. +
  1677. +
  1678. +3. Configuration and Compilation
  1679. +----------------------------------------
  1680. +Make sure you have git-checkout'ed the correct branch.
  1681. +
  1682. +For aufs-linux tree,
  1683. +- enable CONFIG_AUFS_FS.
  1684. +- set other aufs configurations if necessary.
  1685. +- for aufs5.13 and later
  1686. + Because aufs is not only an ordinary filesystem (callee of VFS), but
  1687. + also a caller of VFS functions for branch filesystems, subclassing of
  1688. + the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
  1689. + feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
  1690. + need to customize some LOCKDEP numbers. Here are what I use on my
  1691. + test environment.
  1692. + CONFIG_LOCKDEP_BITS=21
  1693. + CONFIG_LOCKDEP_CHAINS_BITS=21
  1694. + CONFIG_LOCKDEP_STACK_TRACE_BITS=24
  1695. + Also you will need to expand some constant values in LOCKDEP. Refer
  1696. + to lockdep-debug.patch in aufs-standalone.git.
  1697. +
  1698. +For aufs-standalone tree,
  1699. +There are several ways to build.
  1700. +
  1701. +1.
  1702. +- apply ./aufs5-kbuild.patch to your kernel source files.
  1703. +- apply ./aufs5-base.patch too.
  1704. +- apply ./aufs5-mmap.patch too.
  1705. +- apply ./aufs5-standalone.patch too, if you have a plan to set
  1706. + CONFIG_AUFS_FS=m. otherwise you don't need ./aufs5-standalone.patch.
  1707. +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
  1708. + kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
  1709. +- enable CONFIG_AUFS_FS, you can select either
  1710. + =m or =y.
  1711. +- and build your kernel as usual.
  1712. +- install the built kernel.
  1713. +- install the header files too by "make headers_install" to the
  1714. + directory where you specify. By default, it is $PWD/usr.
  1715. + "make help" shows a brief note for headers_install.
  1716. +- and reboot your system.
  1717. +
  1718. +2.
  1719. +- module only (CONFIG_AUFS_FS=m).
  1720. +- apply ./aufs5-base.patch to your kernel source files.
  1721. +- apply ./aufs5-mmap.patch too.
  1722. +- apply ./aufs5-standalone.patch too.
  1723. +- build your kernel, don't forget "make headers_install", and reboot.
  1724. +- edit ./config.mk and set other aufs configurations if necessary.
  1725. + Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
  1726. + every aufs configurations.
  1727. +- build the module by simple "make".
  1728. +- you can specify ${KDIR} make variable which points to your kernel
  1729. + source tree.
  1730. +- install the files
  1731. + + run "make install" to install the aufs module, or copy the built
  1732. + $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
  1733. + + run "make install_headers" (instead of headers_install) to install
  1734. + the modified aufs header file (you can specify DESTDIR which is
  1735. + available in aufs standalone version's Makefile only), or copy
  1736. + $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
  1737. + you like manually. By default, the target directory is $PWD/usr.
  1738. +- no need to apply aufs5-kbuild.patch, nor copying source files to your
  1739. + kernel source tree.
  1740. +
  1741. +Note: The header file aufs_type.h is necessary to build aufs-util
  1742. + as well as "make headers_install" in the kernel source tree.
  1743. + headers_install is subject to be forgotten, but it is essentially
  1744. + necessary, not only for building aufs-util.
  1745. + You may not meet problems without headers_install in some older
  1746. + version though.
  1747. +
  1748. +And then,
  1749. +- read README in aufs-util, build and install it
  1750. +- note that your distribution may contain an obsoleted version of
  1751. + aufs_type.h in /usr/include/linux or something. When you build aufs
  1752. + utilities, make sure that your compiler refers the correct aufs header
  1753. + file which is built by "make headers_install."
  1754. +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
  1755. + then run "make install_ulib" too. And refer to the aufs manual in
  1756. + detail.
  1757. +
  1758. +There several other patches in aufs-standalone.git. They are all
  1759. +optional. When you meet some problems, they will help you.
  1760. +- aufs5-loopback.patch
  1761. + Supports a nested loopback mount in a branch-fs. This patch is
  1762. + unnecessary until aufs produces a message like "you may want to try
  1763. + another patch for loopback file".
  1764. +- vfs-ino.patch
  1765. + Modifies a system global kernel internal function get_next_ino() in
  1766. + order to stop assigning 0 for an inode-number. Not directly related to
  1767. + aufs, but recommended generally.
  1768. +- tmpfs-idr.patch
  1769. + Keeps the tmpfs inode number as the lowest value. Effective to reduce
  1770. + the size of aufs XINO files for tmpfs branch. Also it prevents the
  1771. + duplication of inode number, which is important for backup tools and
  1772. + other utilities. When you find aufs XINO files for tmpfs branch
  1773. + growing too much, try this patch.
  1774. +- lockdep-debug.patch
  1775. + Similar to some kernel configurations for LOCKDEP (see the top of
  1776. + this section), you will need expand some constants in LOCKDEP for
  1777. + aufs if you enable CONFIG_LOCKDEP.
  1778. +
  1779. +
  1780. +4. Usage
  1781. +----------------------------------------
  1782. +At first, make sure aufs-util are installed, and please read the aufs
  1783. +manual, aufs.5 in aufs-util.git tree.
  1784. +$ man -l aufs.5
  1785. +
  1786. +And then,
  1787. +$ mkdir /tmp/rw /tmp/aufs
  1788. +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
  1789. +
  1790. +Here is another example. The result is equivalent.
  1791. +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
  1792. + Or
  1793. +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
  1794. +# mount -o remount,append:${HOME} /tmp/aufs
  1795. +
  1796. +Then, you can see whole tree of your home dir through /tmp/aufs. If
  1797. +you modify a file under /tmp/aufs, the one on your home directory is
  1798. +not affected, instead the same named file will be newly created under
  1799. +/tmp/rw. And all of your modification to a file will be applied to
  1800. +the one under /tmp/rw. This is called the file based Copy on Write
  1801. +(COW) method.
  1802. +Aufs mount options are described in aufs.5.
  1803. +If you run chroot or something and make your aufs as a root directory,
  1804. +then you need to customize the shutdown script. See the aufs manual in
  1805. +detail.
  1806. +
  1807. +Additionally, there are some sample usages of aufs which are a
  1808. +diskless system with network booting, and LiveCD over NFS.
  1809. +See sample dir in CVS tree on SourceForge.
  1810. +
  1811. +
  1812. +5. Contact
  1813. +----------------------------------------
  1814. +When you have any problems or strange behaviour in aufs, please let me
  1815. +know with:
  1816. +- /proc/mounts (instead of the output of mount(8))
  1817. +- /sys/module/aufs/*
  1818. +- /sys/fs/aufs/* (if you have them)
  1819. +- /debug/aufs/* (if you have them)
  1820. +- linux kernel version
  1821. + if your kernel is not plain, for example modified by distributor,
  1822. + the url where i can download its source is necessary too.
  1823. +- aufs version which was printed at loading the module or booting the
  1824. + system, instead of the date you downloaded.
  1825. +- configuration (define/undefine CONFIG_AUFS_xxx)
  1826. +- kernel configuration or /proc/config.gz (if you have it)
  1827. +- LSM (linux security module, if you are using)
  1828. +- behaviour which you think to be incorrect
  1829. +- actual operation, reproducible one is better
  1830. +- mailto: aufs-users at lists.sourceforge.net
  1831. +
  1832. +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
  1833. +and Feature Requests) on SourceForge. Please join and write to
  1834. +aufs-users ML.
  1835. +
  1836. +
  1837. +6. Acknowledgements
  1838. +----------------------------------------
  1839. +Thanks to everyone who have tried and are using aufs, whoever
  1840. +have reported a bug or any feedback.
  1841. +
  1842. +Especially donators:
  1843. +Tomas Matejicek(slax.org) made a donation (much more than once).
  1844. + Since Apr 2010, Tomas M (the author of Slax and Linux Live
  1845. + scripts) is making "doubling" donations.
  1846. + Unfortunately I cannot list all of the donators, but I really
  1847. + appreciate.
  1848. + It ends Aug 2010, but the ordinary donation URL is still available.
  1849. + <http://sourceforge.net/donate/index.php?group_id=167503>
  1850. +Dai Itasaka made a donation (2007/8).
  1851. +Chuck Smith made a donation (2008/4, 10 and 12).
  1852. +Henk Schoneveld made a donation (2008/9).
  1853. +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
  1854. +Francois Dupoux made a donation (2008/11).
  1855. +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
  1856. + aufs2 GIT tree (2009/2).
  1857. +William Grant made a donation (2009/3).
  1858. +Patrick Lane made a donation (2009/4).
  1859. +The Mail Archive (mail-archive.com) made donations (2009/5).
  1860. +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
  1861. +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
  1862. +Pavel Pronskiy made a donation (2011/2).
  1863. +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
  1864. + Networks (Ed Wildgoose) made a donation for hardware (2011/3).
  1865. +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
  1866. +11).
  1867. +Sam Liddicott made a donation (2011/9).
  1868. +Era Scarecrow made a donation (2013/4).
  1869. +Bor Ratajc made a donation (2013/4).
  1870. +Alessandro Gorreta made a donation (2013/4).
  1871. +POIRETTE Marc made a donation (2013/4).
  1872. +Alessandro Gorreta made a donation (2013/4).
  1873. +lauri kasvandik made a donation (2013/5).
  1874. +"pemasu from Finland" made a donation (2013/7).
  1875. +The Parted Magic Project made a donation (2013/9 and 11).
  1876. +Pavel Barta made a donation (2013/10).
  1877. +Nikolay Pertsev made a donation (2014/5).
  1878. +James B made a donation (2014/7, 2015/7, and 2021/12).
  1879. +Stefano Di Biase made a donation (2014/8).
  1880. +Daniel Epellei made a donation (2015/1).
  1881. +OmegaPhil made a donation (2016/1, 2018/4).
  1882. +Tomasz Szewczyk made a donation (2016/4).
  1883. +James Burry made a donation (2016/12).
  1884. +Carsten Rose made a donation (2018/9).
  1885. +Porteus Kiosk made a donation (2018/10).
  1886. +huronOS team: Enya Quetzalli made donations (2022/5, 2023/5 and 8).
  1887. +Vasily Mikhaylichenko made a donation (2023/5).
  1888. +
  1889. +Thank you very much.
  1890. +Donations are always, including future donations, very important and
  1891. +helpful for me to keep on developing aufs.
  1892. +
  1893. +
  1894. +7.
  1895. +----------------------------------------
  1896. +If you are an experienced user, no explanation is needed. Aufs is
  1897. +just a linux filesystem.
  1898. +
  1899. +
  1900. +Enjoy!
  1901. +
  1902. +# Local variables: ;
  1903. +# mode: text;
  1904. +# End: ;
  1905. diff -Naur null/fs/aufs/aufs.h linux-5.15.131/fs/aufs/aufs.h
  1906. --- /dev/null
  1907. +++ linux-5.15.131/fs/aufs/aufs.h 2023-09-11 14:32:48.145722589 +0300
  1908. @@ -0,0 +1,62 @@
  1909. +/* SPDX-License-Identifier: GPL-2.0 */
  1910. +/*
  1911. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  1912. + *
  1913. + * This program, aufs is free software; you can redistribute it and/or modify
  1914. + * it under the terms of the GNU General Public License as published by
  1915. + * the Free Software Foundation; either version 2 of the License, or
  1916. + * (at your option) any later version.
  1917. + *
  1918. + * This program is distributed in the hope that it will be useful,
  1919. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1920. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1921. + * GNU General Public License for more details.
  1922. + *
  1923. + * You should have received a copy of the GNU General Public License
  1924. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  1925. + */
  1926. +
  1927. +/*
  1928. + * all header files
  1929. + */
  1930. +
  1931. +#ifndef __AUFS_H__
  1932. +#define __AUFS_H__
  1933. +
  1934. +#ifdef __KERNEL__
  1935. +
  1936. +#define AuStub(type, name, body, ...) \
  1937. + static inline type name(__VA_ARGS__) { body; }
  1938. +
  1939. +#define AuStubVoid(name, ...) \
  1940. + AuStub(void, name, , __VA_ARGS__)
  1941. +#define AuStubInt0(name, ...) \
  1942. + AuStub(int, name, return 0, __VA_ARGS__)
  1943. +
  1944. +#include "debug.h"
  1945. +
  1946. +#include "branch.h"
  1947. +#include "cpup.h"
  1948. +#include "dcsub.h"
  1949. +#include "dbgaufs.h"
  1950. +#include "dentry.h"
  1951. +#include "dir.h"
  1952. +#include "dirren.h"
  1953. +#include "dynop.h"
  1954. +#include "file.h"
  1955. +#include "fstype.h"
  1956. +#include "hbl.h"
  1957. +#include "inode.h"
  1958. +#include "lcnt.h"
  1959. +#include "loop.h"
  1960. +#include "module.h"
  1961. +#include "opts.h"
  1962. +#include "rwsem.h"
  1963. +#include "super.h"
  1964. +#include "sysaufs.h"
  1965. +#include "vfsub.h"
  1966. +#include "whout.h"
  1967. +#include "wkq.h"
  1968. +
  1969. +#endif /* __KERNEL__ */
  1970. +#endif /* __AUFS_H__ */
  1971. diff -Naur null/fs/aufs/branch.c linux-5.15.131/fs/aufs/branch.c
  1972. --- /dev/null
  1973. +++ linux-5.15.131/fs/aufs/branch.c 2023-09-11 14:32:48.145722589 +0300
  1974. @@ -0,0 +1,1427 @@
  1975. +// SPDX-License-Identifier: GPL-2.0
  1976. +/*
  1977. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  1978. + *
  1979. + * This program, aufs is free software; you can redistribute it and/or modify
  1980. + * it under the terms of the GNU General Public License as published by
  1981. + * the Free Software Foundation; either version 2 of the License, or
  1982. + * (at your option) any later version.
  1983. + *
  1984. + * This program is distributed in the hope that it will be useful,
  1985. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1986. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1987. + * GNU General Public License for more details.
  1988. + *
  1989. + * You should have received a copy of the GNU General Public License
  1990. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  1991. + */
  1992. +
  1993. +/*
  1994. + * branch management
  1995. + */
  1996. +
  1997. +#include <linux/compat.h>
  1998. +#include <linux/statfs.h>
  1999. +#include "aufs.h"
  2000. +
  2001. +/*
  2002. + * free a single branch
  2003. + */
  2004. +static void au_br_do_free(struct au_branch *br)
  2005. +{
  2006. + int i;
  2007. + struct au_wbr *wbr;
  2008. + struct au_dykey **key;
  2009. +
  2010. + au_hnotify_fin_br(br);
  2011. + /* always, regardless the mount option */
  2012. + au_dr_hino_free(&br->br_dirren);
  2013. + au_xino_put(br);
  2014. +
  2015. + AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
  2016. + au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
  2017. + AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
  2018. + au_lcnt_fin(&br->br_count, /*do_sync*/0);
  2019. +
  2020. + wbr = br->br_wbr;
  2021. + if (wbr) {
  2022. + for (i = 0; i < AuBrWh_Last; i++)
  2023. + dput(wbr->wbr_wh[i]);
  2024. + AuDebugOn(atomic_read(&wbr->wbr_wh_running));
  2025. + AuRwDestroy(&wbr->wbr_wh_rwsem);
  2026. + }
  2027. +
  2028. + if (br->br_fhsm) {
  2029. + au_br_fhsm_fin(br->br_fhsm);
  2030. + au_kfree_try_rcu(br->br_fhsm);
  2031. + }
  2032. +
  2033. + key = br->br_dykey;
  2034. + for (i = 0; i < AuBrDynOp; i++, key++)
  2035. + if (*key)
  2036. + au_dy_put(*key);
  2037. + else
  2038. + break;
  2039. +
  2040. + /* recursive lock, s_umount of branch's */
  2041. + /* synchronize_rcu(); */ /* why? */
  2042. + lockdep_off();
  2043. + path_put(&br->br_path);
  2044. + lockdep_on();
  2045. + au_kfree_rcu(wbr);
  2046. + au_lcnt_wait_for_fin(&br->br_nfiles);
  2047. + au_lcnt_wait_for_fin(&br->br_count);
  2048. + /* I don't know why, but percpu_refcount requires this */
  2049. + /* synchronize_rcu(); */
  2050. + au_kfree_rcu(br);
  2051. +}
  2052. +
  2053. +/*
  2054. + * frees all branches
  2055. + */
  2056. +void au_br_free(struct au_sbinfo *sbinfo)
  2057. +{
  2058. + aufs_bindex_t bmax;
  2059. + struct au_branch **br;
  2060. +
  2061. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  2062. +
  2063. + bmax = sbinfo->si_bbot + 1;
  2064. + br = sbinfo->si_branch;
  2065. + while (bmax--)
  2066. + au_br_do_free(*br++);
  2067. +}
  2068. +
  2069. +/*
  2070. + * find the index of a branch which is specified by @br_id.
  2071. + */
  2072. +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
  2073. +{
  2074. + aufs_bindex_t bindex, bbot;
  2075. +
  2076. + bbot = au_sbbot(sb);
  2077. + for (bindex = 0; bindex <= bbot; bindex++)
  2078. + if (au_sbr_id(sb, bindex) == br_id)
  2079. + return bindex;
  2080. + return -1;
  2081. +}
  2082. +
  2083. +/* ---------------------------------------------------------------------- */
  2084. +
  2085. +/*
  2086. + * add a branch
  2087. + */
  2088. +
  2089. +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
  2090. + struct dentry *h_root)
  2091. +{
  2092. + if (unlikely(h_adding == h_root
  2093. + || au_test_loopback_overlap(sb, h_adding)))
  2094. + return 1;
  2095. + if (h_adding->d_sb != h_root->d_sb)
  2096. + return 0;
  2097. + return au_test_subdir(h_adding, h_root)
  2098. + || au_test_subdir(h_root, h_adding);
  2099. +}
  2100. +
  2101. +/*
  2102. + * returns a newly allocated branch. @new_nbranch is a number of branches
  2103. + * after adding a branch.
  2104. + */
  2105. +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
  2106. + int perm)
  2107. +{
  2108. + struct au_branch *add_branch;
  2109. + struct dentry *root;
  2110. + struct inode *inode;
  2111. + int err;
  2112. +
  2113. + err = -ENOMEM;
  2114. + add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
  2115. + if (unlikely(!add_branch))
  2116. + goto out;
  2117. + add_branch->br_xino = au_xino_alloc(/*nfile*/1);
  2118. + if (unlikely(!add_branch->br_xino))
  2119. + goto out_br;
  2120. + err = au_hnotify_init_br(add_branch, perm);
  2121. + if (unlikely(err))
  2122. + goto out_xino;
  2123. +
  2124. + if (au_br_writable(perm)) {
  2125. + /* may be freed separately at changing the branch permission */
  2126. + add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
  2127. + GFP_NOFS);
  2128. + if (unlikely(!add_branch->br_wbr))
  2129. + goto out_hnotify;
  2130. + }
  2131. +
  2132. + if (au_br_fhsm(perm)) {
  2133. + err = au_fhsm_br_alloc(add_branch);
  2134. + if (unlikely(err))
  2135. + goto out_wbr;
  2136. + }
  2137. +
  2138. + root = sb->s_root;
  2139. + err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
  2140. + if (!err)
  2141. + err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
  2142. + if (!err) {
  2143. + inode = d_inode(root);
  2144. + err = au_hinode_realloc(au_ii(inode), new_nbranch,
  2145. + /*may_shrink*/0);
  2146. + }
  2147. + if (!err)
  2148. + return add_branch; /* success */
  2149. +
  2150. +out_wbr:
  2151. + au_kfree_rcu(add_branch->br_wbr);
  2152. +out_hnotify:
  2153. + au_hnotify_fin_br(add_branch);
  2154. +out_xino:
  2155. + au_xino_put(add_branch);
  2156. +out_br:
  2157. + au_kfree_rcu(add_branch);
  2158. +out:
  2159. + return ERR_PTR(err);
  2160. +}
  2161. +
  2162. +/*
  2163. + * test if the branch permission is legal or not.
  2164. + */
  2165. +static int test_br(struct inode *inode, int brperm, char *path)
  2166. +{
  2167. + int err;
  2168. +
  2169. + err = (au_br_writable(brperm) && IS_RDONLY(inode));
  2170. + if (!err)
  2171. + goto out;
  2172. +
  2173. + err = -EINVAL;
  2174. + pr_err("write permission for readonly mount or inode, %s\n", path);
  2175. +
  2176. +out:
  2177. + return err;
  2178. +}
  2179. +
  2180. +/*
  2181. + * returns:
  2182. + * 0: success, the caller will add it
  2183. + * plus: success, it is already unified, the caller should ignore it
  2184. + * minus: error
  2185. + */
  2186. +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
  2187. +{
  2188. + int err;
  2189. + aufs_bindex_t bbot, bindex;
  2190. + struct dentry *root, *h_dentry;
  2191. + struct inode *inode, *h_inode;
  2192. +
  2193. + root = sb->s_root;
  2194. + bbot = au_sbbot(sb);
  2195. + if (unlikely(bbot >= 0
  2196. + && au_find_dbindex(root, add->path.dentry) >= 0)) {
  2197. + err = 1;
  2198. + if (!remount) {
  2199. + err = -EINVAL;
  2200. + pr_err("%s duplicated\n", add->pathname);
  2201. + }
  2202. + goto out;
  2203. + }
  2204. +
  2205. + err = -ENOSPC; /* -E2BIG; */
  2206. + if (unlikely(AUFS_BRANCH_MAX <= add->bindex
  2207. + || AUFS_BRANCH_MAX - 1 <= bbot)) {
  2208. + pr_err("number of branches exceeded %s\n", add->pathname);
  2209. + goto out;
  2210. + }
  2211. +
  2212. + err = -EDOM;
  2213. + if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
  2214. + pr_err("bad index %d\n", add->bindex);
  2215. + goto out;
  2216. + }
  2217. +
  2218. + inode = d_inode(add->path.dentry);
  2219. + err = -ENOENT;
  2220. + if (unlikely(!inode->i_nlink)) {
  2221. + pr_err("no existence %s\n", add->pathname);
  2222. + goto out;
  2223. + }
  2224. +
  2225. + err = -EINVAL;
  2226. + if (unlikely(inode->i_sb == sb)) {
  2227. + pr_err("%s must be outside\n", add->pathname);
  2228. + goto out;
  2229. + }
  2230. +
  2231. + if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
  2232. + pr_err("unsupported filesystem, %s (%s)\n",
  2233. + add->pathname, au_sbtype(inode->i_sb));
  2234. + goto out;
  2235. + }
  2236. +
  2237. + if (unlikely(inode->i_sb->s_stack_depth)) {
  2238. + pr_err("already stacked, %s (%s)\n",
  2239. + add->pathname, au_sbtype(inode->i_sb));
  2240. + goto out;
  2241. + }
  2242. +
  2243. + err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
  2244. + if (unlikely(err))
  2245. + goto out;
  2246. +
  2247. + if (bbot < 0)
  2248. + return 0; /* success */
  2249. +
  2250. + err = -EINVAL;
  2251. + for (bindex = 0; bindex <= bbot; bindex++)
  2252. + if (unlikely(test_overlap(sb, add->path.dentry,
  2253. + au_h_dptr(root, bindex)))) {
  2254. + pr_err("%s is overlapped\n", add->pathname);
  2255. + goto out;
  2256. + }
  2257. +
  2258. + err = 0;
  2259. + if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
  2260. + h_dentry = au_h_dptr(root, 0);
  2261. + h_inode = d_inode(h_dentry);
  2262. + if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
  2263. + || !uid_eq(h_inode->i_uid, inode->i_uid)
  2264. + || !gid_eq(h_inode->i_gid, inode->i_gid))
  2265. + pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
  2266. + add->pathname,
  2267. + i_uid_read(inode), i_gid_read(inode),
  2268. + (inode->i_mode & S_IALLUGO),
  2269. + i_uid_read(h_inode), i_gid_read(h_inode),
  2270. + (h_inode->i_mode & S_IALLUGO));
  2271. + }
  2272. +
  2273. +out:
  2274. + return err;
  2275. +}
  2276. +
  2277. +/*
  2278. + * initialize or clean the whiteouts for an adding branch
  2279. + */
  2280. +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
  2281. + int new_perm)
  2282. +{
  2283. + int err, old_perm;
  2284. + aufs_bindex_t bindex;
  2285. + struct inode *h_inode;
  2286. + struct au_wbr *wbr;
  2287. + struct au_hinode *hdir;
  2288. + struct dentry *h_dentry;
  2289. +
  2290. + err = vfsub_mnt_want_write(au_br_mnt(br));
  2291. + if (unlikely(err))
  2292. + goto out;
  2293. +
  2294. + wbr = br->br_wbr;
  2295. + old_perm = br->br_perm;
  2296. + br->br_perm = new_perm;
  2297. + hdir = NULL;
  2298. + h_inode = NULL;
  2299. + bindex = au_br_index(sb, br->br_id);
  2300. + if (0 <= bindex) {
  2301. + hdir = au_hi(d_inode(sb->s_root), bindex);
  2302. + au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
  2303. + } else {
  2304. + h_dentry = au_br_dentry(br);
  2305. + h_inode = d_inode(h_dentry);
  2306. + inode_lock_nested(h_inode, AuLsc_I_PARENT);
  2307. + }
  2308. + if (!wbr)
  2309. + err = au_wh_init(br, sb);
  2310. + else {
  2311. + wbr_wh_write_lock(wbr);
  2312. + err = au_wh_init(br, sb);
  2313. + wbr_wh_write_unlock(wbr);
  2314. + }
  2315. + if (hdir)
  2316. + au_hn_inode_unlock(hdir);
  2317. + else
  2318. + inode_unlock(h_inode);
  2319. + vfsub_mnt_drop_write(au_br_mnt(br));
  2320. + br->br_perm = old_perm;
  2321. +
  2322. + if (!err && wbr && !au_br_writable(new_perm)) {
  2323. + au_kfree_rcu(wbr);
  2324. + br->br_wbr = NULL;
  2325. + }
  2326. +
  2327. +out:
  2328. + return err;
  2329. +}
  2330. +
  2331. +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
  2332. + int perm)
  2333. +{
  2334. + int err;
  2335. + struct kstatfs kst;
  2336. + struct au_wbr *wbr;
  2337. +
  2338. + wbr = br->br_wbr;
  2339. + au_rw_init(&wbr->wbr_wh_rwsem);
  2340. + atomic_set(&wbr->wbr_wh_running, 0);
  2341. +
  2342. + /*
  2343. + * a limit for rmdir/rename a dir
  2344. + * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
  2345. + */
  2346. + err = vfs_statfs(&br->br_path, &kst);
  2347. + if (unlikely(err))
  2348. + goto out;
  2349. + err = -EINVAL;
  2350. + if (kst.f_namelen >= NAME_MAX)
  2351. + err = au_br_init_wh(sb, br, perm);
  2352. + else
  2353. + pr_err("%pd(%s), unsupported namelen %ld\n",
  2354. + au_br_dentry(br),
  2355. + au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
  2356. +
  2357. +out:
  2358. + return err;
  2359. +}
  2360. +
  2361. +/* initialize a new branch */
  2362. +static int au_br_init(struct au_branch *br, struct super_block *sb,
  2363. + struct au_opt_add *add)
  2364. +{
  2365. + int err;
  2366. + struct au_branch *brbase;
  2367. + struct file *xf;
  2368. + struct inode *h_inode;
  2369. +
  2370. + err = 0;
  2371. + br->br_perm = add->perm;
  2372. + br->br_path = add->path; /* set first, path_get() later */
  2373. + spin_lock_init(&br->br_dykey_lock);
  2374. + au_lcnt_init(&br->br_nfiles, /*release*/NULL);
  2375. + au_lcnt_init(&br->br_count, /*release*/NULL);
  2376. + br->br_id = au_new_br_id(sb);
  2377. + AuDebugOn(br->br_id < 0);
  2378. +
  2379. + /* always, regardless the given option */
  2380. + err = au_dr_br_init(sb, br, &add->path);
  2381. + if (unlikely(err))
  2382. + goto out_err;
  2383. +
  2384. + if (au_br_writable(add->perm)) {
  2385. + err = au_wbr_init(br, sb, add->perm);
  2386. + if (unlikely(err))
  2387. + goto out_err;
  2388. + }
  2389. +
  2390. + if (au_opt_test(au_mntflags(sb), XINO)) {
  2391. + brbase = au_sbr(sb, 0);
  2392. + xf = au_xino_file(brbase->br_xino, /*idx*/-1);
  2393. + AuDebugOn(!xf);
  2394. + h_inode = d_inode(add->path.dentry);
  2395. + err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
  2396. + if (unlikely(err)) {
  2397. + AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
  2398. + goto out_err;
  2399. + }
  2400. + }
  2401. +
  2402. + sysaufs_br_init(br);
  2403. + path_get(&br->br_path);
  2404. + goto out; /* success */
  2405. +
  2406. +out_err:
  2407. + memset(&br->br_path, 0, sizeof(br->br_path));
  2408. +out:
  2409. + return err;
  2410. +}
  2411. +
  2412. +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
  2413. + struct au_branch *br, aufs_bindex_t bbot,
  2414. + aufs_bindex_t amount)
  2415. +{
  2416. + struct au_branch **brp;
  2417. +
  2418. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  2419. +
  2420. + brp = sbinfo->si_branch + bindex;
  2421. + memmove(brp + 1, brp, sizeof(*brp) * amount);
  2422. + *brp = br;
  2423. + sbinfo->si_bbot++;
  2424. + if (unlikely(bbot < 0))
  2425. + sbinfo->si_bbot = 0;
  2426. +}
  2427. +
  2428. +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
  2429. + aufs_bindex_t bbot, aufs_bindex_t amount)
  2430. +{
  2431. + struct au_hdentry *hdp;
  2432. +
  2433. + AuRwMustWriteLock(&dinfo->di_rwsem);
  2434. +
  2435. + hdp = au_hdentry(dinfo, bindex);
  2436. + memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
  2437. + au_h_dentry_init(hdp);
  2438. + dinfo->di_bbot++;
  2439. + if (unlikely(bbot < 0))
  2440. + dinfo->di_btop = 0;
  2441. +}
  2442. +
  2443. +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
  2444. + aufs_bindex_t bbot, aufs_bindex_t amount)
  2445. +{
  2446. + struct au_hinode *hip;
  2447. +
  2448. + AuRwMustWriteLock(&iinfo->ii_rwsem);
  2449. +
  2450. + hip = au_hinode(iinfo, bindex);
  2451. + memmove(hip + 1, hip, sizeof(*hip) * amount);
  2452. + au_hinode_init(hip);
  2453. + iinfo->ii_bbot++;
  2454. + if (unlikely(bbot < 0))
  2455. + iinfo->ii_btop = 0;
  2456. +}
  2457. +
  2458. +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
  2459. + aufs_bindex_t bindex)
  2460. +{
  2461. + struct dentry *root, *h_dentry;
  2462. + struct inode *root_inode, *h_inode;
  2463. + aufs_bindex_t bbot, amount;
  2464. +
  2465. + root = sb->s_root;
  2466. + root_inode = d_inode(root);
  2467. + bbot = au_sbbot(sb);
  2468. + amount = bbot + 1 - bindex;
  2469. + h_dentry = au_br_dentry(br);
  2470. + au_sbilist_lock();
  2471. + au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
  2472. + au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
  2473. + au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
  2474. + au_set_h_dptr(root, bindex, dget(h_dentry));
  2475. + h_inode = d_inode(h_dentry);
  2476. + au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
  2477. + au_sbilist_unlock();
  2478. +}
  2479. +
  2480. +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
  2481. +{
  2482. + int err;
  2483. + aufs_bindex_t bbot, add_bindex;
  2484. + struct dentry *root, *h_dentry;
  2485. + struct inode *root_inode;
  2486. + struct au_branch *add_branch;
  2487. +
  2488. + root = sb->s_root;
  2489. + root_inode = d_inode(root);
  2490. + IMustLock(root_inode);
  2491. + IiMustWriteLock(root_inode);
  2492. + err = test_add(sb, add, remount);
  2493. + if (unlikely(err < 0))
  2494. + goto out;
  2495. + if (err) {
  2496. + err = 0;
  2497. + goto out; /* success */
  2498. + }
  2499. +
  2500. + bbot = au_sbbot(sb);
  2501. + add_branch = au_br_alloc(sb, bbot + 2, add->perm);
  2502. + err = PTR_ERR(add_branch);
  2503. + if (IS_ERR(add_branch))
  2504. + goto out;
  2505. +
  2506. + err = au_br_init(add_branch, sb, add);
  2507. + if (unlikely(err)) {
  2508. + au_br_do_free(add_branch);
  2509. + goto out;
  2510. + }
  2511. +
  2512. + add_bindex = add->bindex;
  2513. + sysaufs_brs_del(sb, add_bindex); /* remove successors */
  2514. + au_br_do_add(sb, add_branch, add_bindex);
  2515. + sysaufs_brs_add(sb, add_bindex); /* append successors */
  2516. + dbgaufs_brs_add(sb, add_bindex, /*topdown*/0); /* rename successors */
  2517. +
  2518. + h_dentry = add->path.dentry;
  2519. + if (!add_bindex) {
  2520. + au_cpup_attr_all(root_inode, /*force*/1);
  2521. + sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
  2522. + } else
  2523. + au_add_nlink(root_inode, d_inode(h_dentry));
  2524. +
  2525. +out:
  2526. + return err;
  2527. +}
  2528. +
  2529. +/* ---------------------------------------------------------------------- */
  2530. +
  2531. +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
  2532. + unsigned long long max __maybe_unused,
  2533. + void *arg)
  2534. +{
  2535. + unsigned long long n;
  2536. + struct file **p, *f;
  2537. + struct hlist_bl_head *files;
  2538. + struct hlist_bl_node *pos;
  2539. + struct au_finfo *finfo;
  2540. +
  2541. + n = 0;
  2542. + p = a;
  2543. + files = &au_sbi(sb)->si_files;
  2544. + hlist_bl_lock(files);
  2545. + hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
  2546. + f = finfo->fi_file;
  2547. + if (file_count(f)
  2548. + && !special_file(file_inode(f)->i_mode)) {
  2549. + get_file(f);
  2550. + *p++ = f;
  2551. + n++;
  2552. + AuDebugOn(n > max);
  2553. + }
  2554. + }
  2555. + hlist_bl_unlock(files);
  2556. +
  2557. + return n;
  2558. +}
  2559. +
  2560. +static struct file **au_farray_alloc(struct super_block *sb,
  2561. + unsigned long long *max)
  2562. +{
  2563. + struct au_sbinfo *sbi;
  2564. +
  2565. + sbi = au_sbi(sb);
  2566. + *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
  2567. + return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
  2568. +}
  2569. +
  2570. +static void au_farray_free(struct file **a, unsigned long long max)
  2571. +{
  2572. + unsigned long long ull;
  2573. +
  2574. + for (ull = 0; ull < max; ull++)
  2575. + if (a[ull])
  2576. + fput(a[ull]);
  2577. + kvfree(a);
  2578. +}
  2579. +
  2580. +/* ---------------------------------------------------------------------- */
  2581. +
  2582. +/*
  2583. + * delete a branch
  2584. + */
  2585. +
  2586. +/* to show the line number, do not make it inlined function */
  2587. +#define AuVerbose(do_info, fmt, ...) do { \
  2588. + if (do_info) \
  2589. + pr_info(fmt, ##__VA_ARGS__); \
  2590. +} while (0)
  2591. +
  2592. +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
  2593. + aufs_bindex_t bbot)
  2594. +{
  2595. + return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
  2596. +}
  2597. +
  2598. +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
  2599. + aufs_bindex_t bbot)
  2600. +{
  2601. + return au_test_ibusy(d_inode(dentry), btop, bbot);
  2602. +}
  2603. +
  2604. +/*
  2605. + * test if the branch is deletable or not.
  2606. + */
  2607. +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
  2608. + unsigned int sigen, const unsigned int verbose)
  2609. +{
  2610. + int err, i, j, ndentry;
  2611. + aufs_bindex_t btop, bbot;
  2612. + struct au_dcsub_pages dpages;
  2613. + struct au_dpage *dpage;
  2614. + struct dentry *d;
  2615. +
  2616. + err = au_dpages_init(&dpages, GFP_NOFS);
  2617. + if (unlikely(err))
  2618. + goto out;
  2619. + err = au_dcsub_pages(&dpages, root, NULL, NULL);
  2620. + if (unlikely(err))
  2621. + goto out_dpages;
  2622. +
  2623. + for (i = 0; !err && i < dpages.ndpage; i++) {
  2624. + dpage = dpages.dpages + i;
  2625. + ndentry = dpage->ndentry;
  2626. + for (j = 0; !err && j < ndentry; j++) {
  2627. + d = dpage->dentries[j];
  2628. + AuDebugOn(au_dcount(d) <= 0);
  2629. + if (!au_digen_test(d, sigen)) {
  2630. + di_read_lock_child(d, AuLock_IR);
  2631. + if (unlikely(au_dbrange_test(d))) {
  2632. + di_read_unlock(d, AuLock_IR);
  2633. + continue;
  2634. + }
  2635. + } else {
  2636. + di_write_lock_child(d);
  2637. + if (unlikely(au_dbrange_test(d))) {
  2638. + di_write_unlock(d);
  2639. + continue;
  2640. + }
  2641. + err = au_reval_dpath(d, sigen);
  2642. + if (!err)
  2643. + di_downgrade_lock(d, AuLock_IR);
  2644. + else {
  2645. + di_write_unlock(d);
  2646. + break;
  2647. + }
  2648. + }
  2649. +
  2650. + /* AuDbgDentry(d); */
  2651. + btop = au_dbtop(d);
  2652. + bbot = au_dbbot(d);
  2653. + if (btop <= bindex
  2654. + && bindex <= bbot
  2655. + && au_h_dptr(d, bindex)
  2656. + && au_test_dbusy(d, btop, bbot)) {
  2657. + err = -EBUSY;
  2658. + AuVerbose(verbose, "busy %pd\n", d);
  2659. + AuDbgDentry(d);
  2660. + }
  2661. + di_read_unlock(d, AuLock_IR);
  2662. + }
  2663. + }
  2664. +
  2665. +out_dpages:
  2666. + au_dpages_free(&dpages);
  2667. +out:
  2668. + return err;
  2669. +}
  2670. +
  2671. +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
  2672. + unsigned int sigen, const unsigned int verbose)
  2673. +{
  2674. + int err;
  2675. + unsigned long long max, ull;
  2676. + struct inode *i, **array;
  2677. + aufs_bindex_t btop, bbot;
  2678. +
  2679. + array = au_iarray_alloc(sb, &max);
  2680. + err = PTR_ERR(array);
  2681. + if (IS_ERR(array))
  2682. + goto out;
  2683. +
  2684. + err = 0;
  2685. + AuDbg("b%d\n", bindex);
  2686. + for (ull = 0; !err && ull < max; ull++) {
  2687. + i = array[ull];
  2688. + if (unlikely(!i))
  2689. + break;
  2690. + if (i->i_ino == AUFS_ROOT_INO)
  2691. + continue;
  2692. +
  2693. + /* AuDbgInode(i); */
  2694. + if (au_iigen(i, NULL) == sigen)
  2695. + ii_read_lock_child(i);
  2696. + else {
  2697. + ii_write_lock_child(i);
  2698. + err = au_refresh_hinode_self(i);
  2699. + au_iigen_dec(i);
  2700. + if (!err)
  2701. + ii_downgrade_lock(i);
  2702. + else {
  2703. + ii_write_unlock(i);
  2704. + break;
  2705. + }
  2706. + }
  2707. +
  2708. + btop = au_ibtop(i);
  2709. + bbot = au_ibbot(i);
  2710. + if (btop <= bindex
  2711. + && bindex <= bbot
  2712. + && au_h_iptr(i, bindex)
  2713. + && au_test_ibusy(i, btop, bbot)) {
  2714. + err = -EBUSY;
  2715. + AuVerbose(verbose, "busy i%lu\n", i->i_ino);
  2716. + AuDbgInode(i);
  2717. + }
  2718. + ii_read_unlock(i);
  2719. + }
  2720. + au_iarray_free(array, max);
  2721. +
  2722. +out:
  2723. + return err;
  2724. +}
  2725. +
  2726. +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
  2727. + const unsigned int verbose)
  2728. +{
  2729. + int err;
  2730. + unsigned int sigen;
  2731. +
  2732. + sigen = au_sigen(root->d_sb);
  2733. + DiMustNoWaiters(root);
  2734. + IiMustNoWaiters(d_inode(root));
  2735. + di_write_unlock(root);
  2736. + err = test_dentry_busy(root, bindex, sigen, verbose);
  2737. + if (!err)
  2738. + err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
  2739. + di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
  2740. +
  2741. + return err;
  2742. +}
  2743. +
  2744. +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
  2745. + struct file **to_free, int *idx)
  2746. +{
  2747. + int err;
  2748. + unsigned char matched, root;
  2749. + aufs_bindex_t bindex, bbot;
  2750. + struct au_fidir *fidir;
  2751. + struct au_hfile *hfile;
  2752. +
  2753. + err = 0;
  2754. + root = IS_ROOT(file->f_path.dentry);
  2755. + if (root) {
  2756. + get_file(file);
  2757. + to_free[*idx] = file;
  2758. + (*idx)++;
  2759. + goto out;
  2760. + }
  2761. +
  2762. + matched = 0;
  2763. + fidir = au_fi(file)->fi_hdir;
  2764. + AuDebugOn(!fidir);
  2765. + bbot = au_fbbot_dir(file);
  2766. + for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
  2767. + hfile = fidir->fd_hfile + bindex;
  2768. + if (!hfile->hf_file)
  2769. + continue;
  2770. +
  2771. + if (hfile->hf_br->br_id == br_id) {
  2772. + matched = 1;
  2773. + break;
  2774. + }
  2775. + }
  2776. + if (matched)
  2777. + err = -EBUSY;
  2778. +
  2779. +out:
  2780. + return err;
  2781. +}
  2782. +
  2783. +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
  2784. + struct file **to_free, int opened)
  2785. +{
  2786. + int err, idx;
  2787. + unsigned long long ull, max;
  2788. + aufs_bindex_t btop;
  2789. + struct file *file, **array;
  2790. + struct dentry *root;
  2791. + struct au_hfile *hfile;
  2792. +
  2793. + array = au_farray_alloc(sb, &max);
  2794. + err = PTR_ERR(array);
  2795. + if (IS_ERR(array))
  2796. + goto out;
  2797. +
  2798. + err = 0;
  2799. + idx = 0;
  2800. + root = sb->s_root;
  2801. + di_write_unlock(root);
  2802. + for (ull = 0; ull < max; ull++) {
  2803. + file = array[ull];
  2804. + if (unlikely(!file))
  2805. + break;
  2806. +
  2807. + /* AuDbg("%pD\n", file); */
  2808. + fi_read_lock(file);
  2809. + btop = au_fbtop(file);
  2810. + if (!d_is_dir(file->f_path.dentry)) {
  2811. + hfile = &au_fi(file)->fi_htop;
  2812. + if (hfile->hf_br->br_id == br_id)
  2813. + err = -EBUSY;
  2814. + } else
  2815. + err = test_dir_busy(file, br_id, to_free, &idx);
  2816. + fi_read_unlock(file);
  2817. + if (unlikely(err))
  2818. + break;
  2819. + }
  2820. + di_write_lock_child(root);
  2821. + au_farray_free(array, max);
  2822. + AuDebugOn(idx > opened);
  2823. +
  2824. +out:
  2825. + return err;
  2826. +}
  2827. +
  2828. +static void br_del_file(struct file **to_free, unsigned long long opened,
  2829. + aufs_bindex_t br_id)
  2830. +{
  2831. + unsigned long long ull;
  2832. + aufs_bindex_t bindex, btop, bbot, bfound;
  2833. + struct file *file;
  2834. + struct au_fidir *fidir;
  2835. + struct au_hfile *hfile;
  2836. +
  2837. + for (ull = 0; ull < opened; ull++) {
  2838. + file = to_free[ull];
  2839. + if (unlikely(!file))
  2840. + break;
  2841. +
  2842. + /* AuDbg("%pD\n", file); */
  2843. + AuDebugOn(!d_is_dir(file->f_path.dentry));
  2844. + bfound = -1;
  2845. + fidir = au_fi(file)->fi_hdir;
  2846. + AuDebugOn(!fidir);
  2847. + fi_write_lock(file);
  2848. + btop = au_fbtop(file);
  2849. + bbot = au_fbbot_dir(file);
  2850. + for (bindex = btop; bindex <= bbot; bindex++) {
  2851. + hfile = fidir->fd_hfile + bindex;
  2852. + if (!hfile->hf_file)
  2853. + continue;
  2854. +
  2855. + if (hfile->hf_br->br_id == br_id) {
  2856. + bfound = bindex;
  2857. + break;
  2858. + }
  2859. + }
  2860. + AuDebugOn(bfound < 0);
  2861. + au_set_h_fptr(file, bfound, NULL);
  2862. + if (bfound == btop) {
  2863. + for (btop++; btop <= bbot; btop++)
  2864. + if (au_hf_dir(file, btop)) {
  2865. + au_set_fbtop(file, btop);
  2866. + break;
  2867. + }
  2868. + }
  2869. + fi_write_unlock(file);
  2870. + }
  2871. +}
  2872. +
  2873. +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
  2874. + const aufs_bindex_t bindex,
  2875. + const aufs_bindex_t bbot)
  2876. +{
  2877. + struct au_branch **brp, **p;
  2878. +
  2879. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  2880. +
  2881. + brp = sbinfo->si_branch + bindex;
  2882. + if (bindex < bbot)
  2883. + memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
  2884. + sbinfo->si_branch[0 + bbot] = NULL;
  2885. + sbinfo->si_bbot--;
  2886. +
  2887. + p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
  2888. + /*may_shrink*/1);
  2889. + if (p)
  2890. + sbinfo->si_branch = p;
  2891. + /* harmless error */
  2892. +}
  2893. +
  2894. +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
  2895. + const aufs_bindex_t bbot)
  2896. +{
  2897. + struct au_hdentry *hdp, *p;
  2898. +
  2899. + AuRwMustWriteLock(&dinfo->di_rwsem);
  2900. +
  2901. + hdp = au_hdentry(dinfo, bindex);
  2902. + if (bindex < bbot)
  2903. + memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
  2904. + /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
  2905. + dinfo->di_bbot--;
  2906. +
  2907. + p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
  2908. + /*may_shrink*/1);
  2909. + if (p)
  2910. + dinfo->di_hdentry = p;
  2911. + /* harmless error */
  2912. +}
  2913. +
  2914. +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
  2915. + const aufs_bindex_t bbot)
  2916. +{
  2917. + struct au_hinode *hip, *p;
  2918. +
  2919. + AuRwMustWriteLock(&iinfo->ii_rwsem);
  2920. +
  2921. + hip = au_hinode(iinfo, bindex);
  2922. + if (bindex < bbot)
  2923. + memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
  2924. + /* au_hinode_init(au_hinode(iinfo, bbot)); */
  2925. + iinfo->ii_bbot--;
  2926. +
  2927. + p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
  2928. + /*may_shrink*/1);
  2929. + if (p)
  2930. + iinfo->ii_hinode = p;
  2931. + /* harmless error */
  2932. +}
  2933. +
  2934. +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
  2935. + struct au_branch *br)
  2936. +{
  2937. + aufs_bindex_t bbot;
  2938. + struct au_sbinfo *sbinfo;
  2939. + struct dentry *root, *h_root;
  2940. + struct inode *inode, *h_inode;
  2941. + struct au_hinode *hinode;
  2942. +
  2943. + SiMustWriteLock(sb);
  2944. +
  2945. + root = sb->s_root;
  2946. + inode = d_inode(root);
  2947. + sbinfo = au_sbi(sb);
  2948. + bbot = sbinfo->si_bbot;
  2949. +
  2950. + h_root = au_h_dptr(root, bindex);
  2951. + hinode = au_hi(inode, bindex);
  2952. + h_inode = au_igrab(hinode->hi_inode);
  2953. + au_hiput(hinode);
  2954. +
  2955. + au_sbilist_lock();
  2956. + au_br_do_del_brp(sbinfo, bindex, bbot);
  2957. + au_br_do_del_hdp(au_di(root), bindex, bbot);
  2958. + au_br_do_del_hip(au_ii(inode), bindex, bbot);
  2959. + au_sbilist_unlock();
  2960. +
  2961. + /* ignore an error */
  2962. + au_dr_br_fin(sb, br); /* always, regardless the mount option */
  2963. +
  2964. + dput(h_root);
  2965. + iput(h_inode);
  2966. + au_br_do_free(br);
  2967. +}
  2968. +
  2969. +static unsigned long long empty_cb(struct super_block *sb, void *array,
  2970. + unsigned long long max, void *arg)
  2971. +{
  2972. + return max;
  2973. +}
  2974. +
  2975. +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
  2976. +{
  2977. + int err, rerr, i;
  2978. + unsigned long long opened;
  2979. + unsigned int mnt_flags;
  2980. + aufs_bindex_t bindex, bbot, br_id;
  2981. + unsigned char do_wh, verbose;
  2982. + struct au_branch *br;
  2983. + struct au_wbr *wbr;
  2984. + struct dentry *root;
  2985. + struct file **to_free;
  2986. +
  2987. + err = 0;
  2988. + opened = 0;
  2989. + to_free = NULL;
  2990. + root = sb->s_root;
  2991. + bindex = au_find_dbindex(root, del->h_path.dentry);
  2992. + if (bindex < 0) {
  2993. + if (remount)
  2994. + goto out; /* success */
  2995. + err = -ENOENT;
  2996. + pr_err("%s no such branch\n", del->pathname);
  2997. + goto out;
  2998. + }
  2999. + AuDbg("bindex b%d\n", bindex);
  3000. +
  3001. + err = -EBUSY;
  3002. + mnt_flags = au_mntflags(sb);
  3003. + verbose = !!au_opt_test(mnt_flags, VERBOSE);
  3004. + bbot = au_sbbot(sb);
  3005. + if (unlikely(!bbot)) {
  3006. + AuVerbose(verbose, "no more branches left\n");
  3007. + goto out;
  3008. + }
  3009. +
  3010. + br = au_sbr(sb, bindex);
  3011. + AuDebugOn(!path_equal(&br->br_path, &del->h_path));
  3012. + if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
  3013. + AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
  3014. + goto out;
  3015. + }
  3016. +
  3017. + br_id = br->br_id;
  3018. + opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
  3019. + if (unlikely(opened)) {
  3020. + to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
  3021. + err = PTR_ERR(to_free);
  3022. + if (IS_ERR(to_free))
  3023. + goto out;
  3024. +
  3025. + err = test_file_busy(sb, br_id, to_free, opened);
  3026. + if (unlikely(err)) {
  3027. + AuVerbose(verbose, "%llu file(s) opened\n", opened);
  3028. + goto out;
  3029. + }
  3030. + }
  3031. +
  3032. + wbr = br->br_wbr;
  3033. + do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
  3034. + if (do_wh) {
  3035. + /* instead of WbrWhMustWriteLock(wbr) */
  3036. + SiMustWriteLock(sb);
  3037. + for (i = 0; i < AuBrWh_Last; i++) {
  3038. + dput(wbr->wbr_wh[i]);
  3039. + wbr->wbr_wh[i] = NULL;
  3040. + }
  3041. + }
  3042. +
  3043. + err = test_children_busy(root, bindex, verbose);
  3044. + if (unlikely(err)) {
  3045. + if (do_wh)
  3046. + goto out_wh;
  3047. + goto out;
  3048. + }
  3049. +
  3050. + err = 0;
  3051. + if (to_free) {
  3052. + /*
  3053. + * now we confirmed the branch is deletable.
  3054. + * let's free the remaining opened dirs on the branch.
  3055. + */
  3056. + di_write_unlock(root);
  3057. + br_del_file(to_free, opened, br_id);
  3058. + di_write_lock_child(root);
  3059. + }
  3060. +
  3061. + sysaufs_brs_del(sb, bindex); /* remove successors */
  3062. + dbgaufs_xino_del(br); /* remove one */
  3063. + au_br_do_del(sb, bindex, br);
  3064. + sysaufs_brs_add(sb, bindex); /* append successors */
  3065. + dbgaufs_brs_add(sb, bindex, /*topdown*/1); /* rename successors */
  3066. +
  3067. + if (!bindex) {
  3068. + au_cpup_attr_all(d_inode(root), /*force*/1);
  3069. + sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
  3070. + } else
  3071. + au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
  3072. + if (au_opt_test(mnt_flags, PLINK))
  3073. + au_plink_half_refresh(sb, br_id);
  3074. +
  3075. + goto out; /* success */
  3076. +
  3077. +out_wh:
  3078. + /* revert */
  3079. + rerr = au_br_init_wh(sb, br, br->br_perm);
  3080. + if (rerr)
  3081. + pr_warn("failed re-creating base whiteout, %s. (%d)\n",
  3082. + del->pathname, rerr);
  3083. +out:
  3084. + if (to_free)
  3085. + au_farray_free(to_free, opened);
  3086. + return err;
  3087. +}
  3088. +
  3089. +/* ---------------------------------------------------------------------- */
  3090. +
  3091. +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
  3092. +{
  3093. + int err;
  3094. + aufs_bindex_t btop, bbot;
  3095. + struct aufs_ibusy ibusy;
  3096. + struct inode *inode, *h_inode;
  3097. +
  3098. + err = -EPERM;
  3099. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  3100. + goto out;
  3101. +
  3102. + err = copy_from_user(&ibusy, arg, sizeof(ibusy));
  3103. + if (!err)
  3104. + /* VERIFY_WRITE */
  3105. + err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
  3106. + if (unlikely(err)) {
  3107. + err = -EFAULT;
  3108. + AuTraceErr(err);
  3109. + goto out;
  3110. + }
  3111. +
  3112. + err = -EINVAL;
  3113. + si_read_lock(sb, AuLock_FLUSH);
  3114. + if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
  3115. + goto out_unlock;
  3116. +
  3117. + err = 0;
  3118. + ibusy.h_ino = 0; /* invalid */
  3119. + inode = ilookup(sb, ibusy.ino);
  3120. + if (!inode
  3121. + || inode->i_ino == AUFS_ROOT_INO
  3122. + || au_is_bad_inode(inode))
  3123. + goto out_unlock;
  3124. +
  3125. + ii_read_lock_child(inode);
  3126. + btop = au_ibtop(inode);
  3127. + bbot = au_ibbot(inode);
  3128. + if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
  3129. + h_inode = au_h_iptr(inode, ibusy.bindex);
  3130. + if (h_inode && au_test_ibusy(inode, btop, bbot))
  3131. + ibusy.h_ino = h_inode->i_ino;
  3132. + }
  3133. + ii_read_unlock(inode);
  3134. + iput(inode);
  3135. +
  3136. +out_unlock:
  3137. + si_read_unlock(sb);
  3138. + if (!err) {
  3139. + err = __put_user(ibusy.h_ino, &arg->h_ino);
  3140. + if (unlikely(err)) {
  3141. + err = -EFAULT;
  3142. + AuTraceErr(err);
  3143. + }
  3144. + }
  3145. +out:
  3146. + return err;
  3147. +}
  3148. +
  3149. +long au_ibusy_ioctl(struct file *file, unsigned long arg)
  3150. +{
  3151. + return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
  3152. +}
  3153. +
  3154. +#ifdef CONFIG_COMPAT
  3155. +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
  3156. +{
  3157. + return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
  3158. +}
  3159. +#endif
  3160. +
  3161. +/* ---------------------------------------------------------------------- */
  3162. +
  3163. +/*
  3164. + * change a branch permission
  3165. + */
  3166. +
  3167. +static void au_warn_ima(void)
  3168. +{
  3169. +#ifdef CONFIG_IMA
  3170. + /* since it doesn't support mark_files_ro() */
  3171. + AuWarn1("RW -> RO makes IMA to produce wrong message\n");
  3172. +#endif
  3173. +}
  3174. +
  3175. +static int do_need_sigen_inc(int a, int b)
  3176. +{
  3177. + return au_br_whable(a) && !au_br_whable(b);
  3178. +}
  3179. +
  3180. +static int need_sigen_inc(int old, int new)
  3181. +{
  3182. + return do_need_sigen_inc(old, new)
  3183. + || do_need_sigen_inc(new, old);
  3184. +}
  3185. +
  3186. +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
  3187. +{
  3188. + int err, do_warn;
  3189. + unsigned int mnt_flags;
  3190. + unsigned long long ull, max;
  3191. + aufs_bindex_t br_id;
  3192. + unsigned char verbose, writer;
  3193. + struct file *file, *hf, **array;
  3194. + struct au_hfile *hfile;
  3195. + struct inode *h_inode;
  3196. +
  3197. + mnt_flags = au_mntflags(sb);
  3198. + verbose = !!au_opt_test(mnt_flags, VERBOSE);
  3199. +
  3200. + array = au_farray_alloc(sb, &max);
  3201. + err = PTR_ERR(array);
  3202. + if (IS_ERR(array))
  3203. + goto out;
  3204. +
  3205. + do_warn = 0;
  3206. + br_id = au_sbr_id(sb, bindex);
  3207. + for (ull = 0; ull < max; ull++) {
  3208. + file = array[ull];
  3209. + if (unlikely(!file))
  3210. + break;
  3211. +
  3212. + /* AuDbg("%pD\n", file); */
  3213. + fi_read_lock(file);
  3214. + if (unlikely(au_test_mmapped(file))) {
  3215. + err = -EBUSY;
  3216. + AuVerbose(verbose, "mmapped %pD\n", file);
  3217. + AuDbgFile(file);
  3218. + FiMustNoWaiters(file);
  3219. + fi_read_unlock(file);
  3220. + goto out_array;
  3221. + }
  3222. +
  3223. + hfile = &au_fi(file)->fi_htop;
  3224. + hf = hfile->hf_file;
  3225. + if (!d_is_reg(file->f_path.dentry)
  3226. + || !(file->f_mode & FMODE_WRITE)
  3227. + || hfile->hf_br->br_id != br_id
  3228. + || !(hf->f_mode & FMODE_WRITE))
  3229. + array[ull] = NULL;
  3230. + else {
  3231. + do_warn = 1;
  3232. + get_file(file);
  3233. + }
  3234. +
  3235. + FiMustNoWaiters(file);
  3236. + fi_read_unlock(file);
  3237. + fput(file);
  3238. + }
  3239. +
  3240. + err = 0;
  3241. + if (do_warn)
  3242. + au_warn_ima();
  3243. +
  3244. + for (ull = 0; ull < max; ull++) {
  3245. + file = array[ull];
  3246. + if (!file)
  3247. + continue;
  3248. +
  3249. + /* todo: already flushed? */
  3250. + /*
  3251. + * fs/super.c:mark_files_ro() is gone, but aufs keeps its
  3252. + * approach which resets f_mode and calls mnt_drop_write() and
  3253. + * file_release_write() for each file, because the branch
  3254. + * attribute in aufs world is totally different from the native
  3255. + * fs rw/ro mode.
  3256. + */
  3257. + /* fi_read_lock(file); */
  3258. + hfile = &au_fi(file)->fi_htop;
  3259. + hf = hfile->hf_file;
  3260. + /* fi_read_unlock(file); */
  3261. + spin_lock(&hf->f_lock);
  3262. + writer = !!(hf->f_mode & FMODE_WRITER);
  3263. + hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
  3264. + spin_unlock(&hf->f_lock);
  3265. + if (writer) {
  3266. + h_inode = file_inode(hf);
  3267. + if (hf->f_mode & FMODE_READ)
  3268. + i_readcount_inc(h_inode);
  3269. + put_write_access(h_inode);
  3270. + __mnt_drop_write(hf->f_path.mnt);
  3271. + }
  3272. + }
  3273. +
  3274. +out_array:
  3275. + au_farray_free(array, max);
  3276. +out:
  3277. + AuTraceErr(err);
  3278. + return err;
  3279. +}
  3280. +
  3281. +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
  3282. + int *do_refresh)
  3283. +{
  3284. + int err, rerr;
  3285. + aufs_bindex_t bindex;
  3286. + struct dentry *root;
  3287. + struct au_branch *br;
  3288. + struct au_br_fhsm *bf;
  3289. +
  3290. + root = sb->s_root;
  3291. + bindex = au_find_dbindex(root, mod->h_root);
  3292. + if (bindex < 0) {
  3293. + if (remount)
  3294. + return 0; /* success */
  3295. + err = -ENOENT;
  3296. + pr_err("%s no such branch\n", mod->path);
  3297. + goto out;
  3298. + }
  3299. + AuDbg("bindex b%d\n", bindex);
  3300. +
  3301. + err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
  3302. + if (unlikely(err))
  3303. + goto out;
  3304. +
  3305. + br = au_sbr(sb, bindex);
  3306. + AuDebugOn(mod->h_root != au_br_dentry(br));
  3307. + if (br->br_perm == mod->perm)
  3308. + return 0; /* success */
  3309. +
  3310. + /* pre-allocate for non-fhsm --> fhsm */
  3311. + bf = NULL;
  3312. + if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
  3313. + err = au_fhsm_br_alloc(br);
  3314. + if (unlikely(err))
  3315. + goto out;
  3316. + bf = br->br_fhsm;
  3317. + br->br_fhsm = NULL;
  3318. + }
  3319. +
  3320. + if (au_br_writable(br->br_perm)) {
  3321. + /* remove whiteout base */
  3322. + err = au_br_init_wh(sb, br, mod->perm);
  3323. + if (unlikely(err))
  3324. + goto out_bf;
  3325. +
  3326. + if (!au_br_writable(mod->perm)) {
  3327. + /* rw --> ro, file might be mmapped */
  3328. + DiMustNoWaiters(root);
  3329. + IiMustNoWaiters(d_inode(root));
  3330. + di_write_unlock(root);
  3331. + err = au_br_mod_files_ro(sb, bindex);
  3332. + /* aufs_write_lock() calls ..._child() */
  3333. + di_write_lock_child(root);
  3334. +
  3335. + if (unlikely(err)) {
  3336. + rerr = -ENOMEM;
  3337. + br->br_wbr = kzalloc(sizeof(*br->br_wbr),
  3338. + GFP_NOFS);
  3339. + if (br->br_wbr)
  3340. + rerr = au_wbr_init(br, sb, br->br_perm);
  3341. + if (unlikely(rerr)) {
  3342. + AuIOErr("nested error %d (%d)\n",
  3343. + rerr, err);
  3344. + br->br_perm = mod->perm;
  3345. + }
  3346. + }
  3347. + }
  3348. + } else if (au_br_writable(mod->perm)) {
  3349. + /* ro --> rw */
  3350. + err = -ENOMEM;
  3351. + br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
  3352. + if (br->br_wbr) {
  3353. + err = au_wbr_init(br, sb, mod->perm);
  3354. + if (unlikely(err)) {
  3355. + au_kfree_rcu(br->br_wbr);
  3356. + br->br_wbr = NULL;
  3357. + }
  3358. + }
  3359. + }
  3360. + if (unlikely(err))
  3361. + goto out_bf;
  3362. +
  3363. + if (au_br_fhsm(br->br_perm)) {
  3364. + if (!au_br_fhsm(mod->perm)) {
  3365. + /* fhsm --> non-fhsm */
  3366. + au_br_fhsm_fin(br->br_fhsm);
  3367. + au_kfree_rcu(br->br_fhsm);
  3368. + br->br_fhsm = NULL;
  3369. + }
  3370. + } else if (au_br_fhsm(mod->perm))
  3371. + /* non-fhsm --> fhsm */
  3372. + br->br_fhsm = bf;
  3373. +
  3374. + *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
  3375. + br->br_perm = mod->perm;
  3376. + goto out; /* success */
  3377. +
  3378. +out_bf:
  3379. + au_kfree_try_rcu(bf);
  3380. +out:
  3381. + AuTraceErr(err);
  3382. + return err;
  3383. +}
  3384. +
  3385. +/* ---------------------------------------------------------------------- */
  3386. +
  3387. +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
  3388. +{
  3389. + int err;
  3390. + struct kstatfs kstfs;
  3391. +
  3392. + err = vfs_statfs(&br->br_path, &kstfs);
  3393. + if (!err) {
  3394. + stfs->f_blocks = kstfs.f_blocks;
  3395. + stfs->f_bavail = kstfs.f_bavail;
  3396. + stfs->f_files = kstfs.f_files;
  3397. + stfs->f_ffree = kstfs.f_ffree;
  3398. + }
  3399. +
  3400. + return err;
  3401. +}
  3402. diff -Naur null/fs/aufs/branch.h linux-5.15.131/fs/aufs/branch.h
  3403. --- /dev/null
  3404. +++ linux-5.15.131/fs/aufs/branch.h 2023-09-11 14:32:48.145722589 +0300
  3405. @@ -0,0 +1,375 @@
  3406. +/* SPDX-License-Identifier: GPL-2.0 */
  3407. +/*
  3408. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  3409. + *
  3410. + * This program, aufs is free software; you can redistribute it and/or modify
  3411. + * it under the terms of the GNU General Public License as published by
  3412. + * the Free Software Foundation; either version 2 of the License, or
  3413. + * (at your option) any later version.
  3414. + *
  3415. + * This program is distributed in the hope that it will be useful,
  3416. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3417. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3418. + * GNU General Public License for more details.
  3419. + *
  3420. + * You should have received a copy of the GNU General Public License
  3421. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  3422. + */
  3423. +
  3424. +/*
  3425. + * branch filesystems and xino for them
  3426. + */
  3427. +
  3428. +#ifndef __AUFS_BRANCH_H__
  3429. +#define __AUFS_BRANCH_H__
  3430. +
  3431. +#ifdef __KERNEL__
  3432. +
  3433. +#include <linux/mount.h>
  3434. +#include "dirren.h"
  3435. +#include "dynop.h"
  3436. +#include "lcnt.h"
  3437. +#include "rwsem.h"
  3438. +#include "super.h"
  3439. +
  3440. +/* ---------------------------------------------------------------------- */
  3441. +
  3442. +/* a xino file */
  3443. +struct au_xino {
  3444. + struct file **xi_file;
  3445. + unsigned int xi_nfile;
  3446. +
  3447. + struct {
  3448. + spinlock_t spin;
  3449. + ino_t *array;
  3450. + int total;
  3451. + /* reserved for future use */
  3452. + /* unsigned long *bitmap; */
  3453. + wait_queue_head_t wqh;
  3454. + } xi_nondir;
  3455. +
  3456. + struct mutex xi_mtx; /* protects xi_file array */
  3457. + struct hlist_bl_head xi_writing;
  3458. +
  3459. + atomic_t xi_truncating;
  3460. +
  3461. + struct kref xi_kref;
  3462. +};
  3463. +
  3464. +/* File-based Hierarchical Storage Management */
  3465. +struct au_br_fhsm {
  3466. +#ifdef CONFIG_AUFS_FHSM
  3467. + struct mutex bf_lock;
  3468. + unsigned long bf_jiffy;
  3469. + struct aufs_stfs bf_stfs;
  3470. + int bf_readable;
  3471. +#endif
  3472. +};
  3473. +
  3474. +/* members for writable branch only */
  3475. +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
  3476. +struct au_wbr {
  3477. + struct au_rwsem wbr_wh_rwsem;
  3478. + struct dentry *wbr_wh[AuBrWh_Last];
  3479. + atomic_t wbr_wh_running;
  3480. +#define wbr_whbase wbr_wh[AuBrWh_BASE] /* whiteout base */
  3481. +#define wbr_plink wbr_wh[AuBrWh_PLINK] /* pseudo-link dir */
  3482. +#define wbr_orph wbr_wh[AuBrWh_ORPH] /* dir for orphans */
  3483. +
  3484. + /* mfs mode */
  3485. + unsigned long long wbr_bytes;
  3486. +};
  3487. +
  3488. +/* ext2 has 3 types of operations at least, ext3 has 4 */
  3489. +#define AuBrDynOp (AuDyLast * 4)
  3490. +
  3491. +#ifdef CONFIG_AUFS_HFSNOTIFY
  3492. +/* support for asynchronous destruction */
  3493. +struct au_br_hfsnotify {
  3494. + struct fsnotify_group *hfsn_group;
  3495. +};
  3496. +#endif
  3497. +
  3498. +/* sysfs entries */
  3499. +struct au_brsysfs {
  3500. + char name[16];
  3501. + struct attribute attr;
  3502. +};
  3503. +
  3504. +enum {
  3505. + AuBrSysfs_BR,
  3506. + AuBrSysfs_BRID,
  3507. + AuBrSysfs_Last
  3508. +};
  3509. +
  3510. +/* protected by superblock rwsem */
  3511. +struct au_branch {
  3512. + struct au_xino *br_xino;
  3513. +
  3514. + aufs_bindex_t br_id;
  3515. +
  3516. + int br_perm;
  3517. + struct path br_path;
  3518. + spinlock_t br_dykey_lock;
  3519. + struct au_dykey *br_dykey[AuBrDynOp];
  3520. + au_lcnt_t br_nfiles; /* opened files */
  3521. + au_lcnt_t br_count; /* in-use for other */
  3522. +
  3523. + struct au_wbr *br_wbr;
  3524. + struct au_br_fhsm *br_fhsm;
  3525. +
  3526. +#ifdef CONFIG_AUFS_HFSNOTIFY
  3527. + struct au_br_hfsnotify *br_hfsn;
  3528. +#endif
  3529. +
  3530. +#ifdef CONFIG_SYSFS
  3531. + /* entries under sysfs per mount-point */
  3532. + struct au_brsysfs br_sysfs[AuBrSysfs_Last];
  3533. +#endif
  3534. +
  3535. +#ifdef CONFIG_DEBUG_FS
  3536. + struct dentry *br_dbgaufs; /* xino */
  3537. +#endif
  3538. +
  3539. + struct au_dr_br br_dirren;
  3540. +};
  3541. +
  3542. +/* ---------------------------------------------------------------------- */
  3543. +
  3544. +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
  3545. +{
  3546. + return br->br_path.mnt;
  3547. +}
  3548. +
  3549. +static inline struct dentry *au_br_dentry(struct au_branch *br)
  3550. +{
  3551. + return br->br_path.dentry;
  3552. +}
  3553. +
  3554. +static inline struct user_namespace *au_br_userns(struct au_branch *br)
  3555. +{
  3556. + return mnt_user_ns(br->br_path.mnt);
  3557. +}
  3558. +
  3559. +static inline struct super_block *au_br_sb(struct au_branch *br)
  3560. +{
  3561. + return au_br_mnt(br)->mnt_sb;
  3562. +}
  3563. +
  3564. +static inline int au_br_rdonly(struct au_branch *br)
  3565. +{
  3566. + return (sb_rdonly(au_br_sb(br))
  3567. + || !au_br_writable(br->br_perm))
  3568. + ? -EROFS : 0;
  3569. +}
  3570. +
  3571. +static inline int au_br_hnotifyable(int brperm __maybe_unused)
  3572. +{
  3573. +#ifdef CONFIG_AUFS_HNOTIFY
  3574. + return !(brperm & AuBrPerm_RR);
  3575. +#else
  3576. + return 0;
  3577. +#endif
  3578. +}
  3579. +
  3580. +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
  3581. +{
  3582. + int err, exec_flag;
  3583. +
  3584. + err = 0;
  3585. + exec_flag = oflag & __FMODE_EXEC;
  3586. + if (unlikely(exec_flag && path_noexec(&br->br_path)))
  3587. + err = -EACCES;
  3588. +
  3589. + return err;
  3590. +}
  3591. +
  3592. +static inline void au_xino_get(struct au_branch *br)
  3593. +{
  3594. + struct au_xino *xi;
  3595. +
  3596. + xi = br->br_xino;
  3597. + if (xi)
  3598. + kref_get(&xi->xi_kref);
  3599. +}
  3600. +
  3601. +static inline int au_xino_count(struct au_branch *br)
  3602. +{
  3603. + int v;
  3604. + struct au_xino *xi;
  3605. +
  3606. + v = 0;
  3607. + xi = br->br_xino;
  3608. + if (xi)
  3609. + v = kref_read(&xi->xi_kref);
  3610. +
  3611. + return v;
  3612. +}
  3613. +
  3614. +/* ---------------------------------------------------------------------- */
  3615. +
  3616. +/* branch.c */
  3617. +struct au_sbinfo;
  3618. +void au_br_free(struct au_sbinfo *sinfo);
  3619. +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
  3620. +struct au_opt_add;
  3621. +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
  3622. +struct au_opt_del;
  3623. +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
  3624. +long au_ibusy_ioctl(struct file *file, unsigned long arg);
  3625. +#ifdef CONFIG_COMPAT
  3626. +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
  3627. +#endif
  3628. +struct au_opt_mod;
  3629. +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
  3630. + int *do_refresh);
  3631. +struct aufs_stfs;
  3632. +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
  3633. +
  3634. +/* xino.c */
  3635. +static const loff_t au_loff_max = LLONG_MAX;
  3636. +
  3637. +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
  3638. +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
  3639. + int wbrtop);
  3640. +struct file *au_xino_create2(struct super_block *sb, struct path *base,
  3641. + struct file *copy_src);
  3642. +struct au_xi_new {
  3643. + struct au_xino *xi; /* switch between xino and xigen */
  3644. + int idx;
  3645. + struct path *base;
  3646. + struct file *copy_src;
  3647. +};
  3648. +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
  3649. +
  3650. +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  3651. + ino_t *ino);
  3652. +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  3653. + ino_t ino);
  3654. +ssize_t xino_fread(struct file *file, void *buf, size_t size, loff_t *pos);
  3655. +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos);
  3656. +
  3657. +int au_xib_trunc(struct super_block *sb);
  3658. +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
  3659. +
  3660. +struct au_xino *au_xino_alloc(unsigned int nfile);
  3661. +int au_xino_put(struct au_branch *br);
  3662. +struct file *au_xino_file1(struct au_xino *xi);
  3663. +
  3664. +struct au_opt_xino;
  3665. +void au_xino_clr(struct super_block *sb);
  3666. +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
  3667. +struct file *au_xino_def(struct super_block *sb);
  3668. +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
  3669. + struct path *base);
  3670. +
  3671. +ino_t au_xino_new_ino(struct super_block *sb);
  3672. +void au_xino_delete_inode(struct inode *inode, const int unlinked);
  3673. +
  3674. +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
  3675. + ino_t h_ino, int idx);
  3676. +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  3677. + int *idx);
  3678. +
  3679. +int au_xino_path(struct seq_file *seq, struct file *file);
  3680. +
  3681. +/* ---------------------------------------------------------------------- */
  3682. +
  3683. +/* @idx is signed to accept -1 meaning the first file */
  3684. +static inline struct file *au_xino_file(struct au_xino *xi, int idx)
  3685. +{
  3686. + struct file *file;
  3687. +
  3688. + file = NULL;
  3689. + if (!xi)
  3690. + goto out;
  3691. +
  3692. + if (idx >= 0) {
  3693. + if (idx < xi->xi_nfile)
  3694. + file = xi->xi_file[idx];
  3695. + } else
  3696. + file = au_xino_file1(xi);
  3697. +
  3698. +out:
  3699. + return file;
  3700. +}
  3701. +
  3702. +/* ---------------------------------------------------------------------- */
  3703. +
  3704. +/* Superblock to branch */
  3705. +static inline
  3706. +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
  3707. +{
  3708. + return au_sbr(sb, bindex)->br_id;
  3709. +}
  3710. +
  3711. +static inline
  3712. +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
  3713. +{
  3714. + return au_br_mnt(au_sbr(sb, bindex));
  3715. +}
  3716. +
  3717. +static inline
  3718. +struct user_namespace *au_sbr_userns(struct super_block *sb, aufs_bindex_t bindex)
  3719. +{
  3720. + return au_br_userns(au_sbr(sb, bindex));
  3721. +}
  3722. +
  3723. +static inline
  3724. +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
  3725. +{
  3726. + return au_br_sb(au_sbr(sb, bindex));
  3727. +}
  3728. +
  3729. +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
  3730. +{
  3731. + return au_sbr(sb, bindex)->br_perm;
  3732. +}
  3733. +
  3734. +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
  3735. +{
  3736. + return au_br_whable(au_sbr_perm(sb, bindex));
  3737. +}
  3738. +
  3739. +/* ---------------------------------------------------------------------- */
  3740. +
  3741. +#define wbr_wh_read_lock(wbr) au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
  3742. +#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
  3743. +#define wbr_wh_read_trylock(wbr) au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
  3744. +#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
  3745. +/*
  3746. +#define wbr_wh_read_trylock_nested(wbr) \
  3747. + au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
  3748. +#define wbr_wh_write_trylock_nested(wbr) \
  3749. + au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
  3750. +*/
  3751. +
  3752. +#define wbr_wh_read_unlock(wbr) au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
  3753. +#define wbr_wh_write_unlock(wbr) au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
  3754. +#define wbr_wh_downgrade_lock(wbr) au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
  3755. +
  3756. +#define WbrWhMustNoWaiters(wbr) AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
  3757. +#define WbrWhMustAnyLock(wbr) AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
  3758. +#define WbrWhMustWriteLock(wbr) AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
  3759. +
  3760. +/* ---------------------------------------------------------------------- */
  3761. +
  3762. +#ifdef CONFIG_AUFS_FHSM
  3763. +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
  3764. +{
  3765. + mutex_init(&brfhsm->bf_lock);
  3766. + brfhsm->bf_jiffy = 0;
  3767. + brfhsm->bf_readable = 0;
  3768. +}
  3769. +
  3770. +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
  3771. +{
  3772. + mutex_destroy(&brfhsm->bf_lock);
  3773. +}
  3774. +#else
  3775. +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
  3776. +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
  3777. +#endif
  3778. +
  3779. +#endif /* __KERNEL__ */
  3780. +#endif /* __AUFS_BRANCH_H__ */
  3781. diff -Naur null/fs/aufs/conf.mk linux-5.15.131/fs/aufs/conf.mk
  3782. --- /dev/null
  3783. +++ linux-5.15.131/fs/aufs/conf.mk 2023-09-11 14:32:48.145722589 +0300
  3784. @@ -0,0 +1,40 @@
  3785. +# SPDX-License-Identifier: GPL-2.0
  3786. +
  3787. +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
  3788. +
  3789. +define AuConf
  3790. +ifdef ${1}
  3791. +AuConfStr += ${1}=${${1}}
  3792. +endif
  3793. +endef
  3794. +
  3795. +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
  3796. + SBILIST \
  3797. + HNOTIFY HFSNOTIFY \
  3798. + EXPORT INO_T_64 \
  3799. + XATTR \
  3800. + FHSM \
  3801. + RDU \
  3802. + DIRREN \
  3803. + SHWH \
  3804. + BR_RAMFS \
  3805. + BR_FUSE POLL \
  3806. + BR_HFSPLUS \
  3807. + BDEV_LOOP \
  3808. + DEBUG MAGIC_SYSRQ
  3809. +$(foreach i, ${AuConfAll}, \
  3810. + $(eval $(call AuConf,CONFIG_AUFS_${i})))
  3811. +
  3812. +AuConfName = ${obj}/conf.str
  3813. +${AuConfName}.tmp: FORCE
  3814. + @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
  3815. +${AuConfName}: ${AuConfName}.tmp
  3816. + @diff -q $< $@ > /dev/null 2>&1 || { \
  3817. + echo ' GEN ' $@; \
  3818. + cp -p $< $@; \
  3819. + }
  3820. +FORCE:
  3821. +clean-files += ${AuConfName} ${AuConfName}.tmp
  3822. +${obj}/sysfs.o: ${AuConfName}
  3823. +
  3824. +-include ${srctree}/${src}/conf_priv.mk
  3825. diff -Naur null/fs/aufs/cpup.c linux-5.15.131/fs/aufs/cpup.c
  3826. --- /dev/null
  3827. +++ linux-5.15.131/fs/aufs/cpup.c 2023-09-11 14:32:48.145722589 +0300
  3828. @@ -0,0 +1,1459 @@
  3829. +// SPDX-License-Identifier: GPL-2.0
  3830. +/*
  3831. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  3832. + *
  3833. + * This program, aufs is free software; you can redistribute it and/or modify
  3834. + * it under the terms of the GNU General Public License as published by
  3835. + * the Free Software Foundation; either version 2 of the License, or
  3836. + * (at your option) any later version.
  3837. + *
  3838. + * This program is distributed in the hope that it will be useful,
  3839. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3840. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3841. + * GNU General Public License for more details.
  3842. + *
  3843. + * You should have received a copy of the GNU General Public License
  3844. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  3845. + */
  3846. +
  3847. +/*
  3848. + * copy-up functions, see wbr_policy.c for copy-down
  3849. + */
  3850. +
  3851. +#include <linux/fs_stack.h>
  3852. +#include <linux/mm.h>
  3853. +#include <linux/task_work.h>
  3854. +#include "aufs.h"
  3855. +
  3856. +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
  3857. +{
  3858. + const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
  3859. + | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
  3860. +
  3861. + BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
  3862. +
  3863. + dst->i_flags |= iflags & ~mask;
  3864. + if (au_test_fs_notime(dst->i_sb))
  3865. + dst->i_flags |= S_NOATIME | S_NOCMTIME;
  3866. +}
  3867. +
  3868. +void au_cpup_attr_timesizes(struct inode *inode)
  3869. +{
  3870. + struct inode *h_inode;
  3871. +
  3872. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  3873. + fsstack_copy_attr_times(inode, h_inode);
  3874. + fsstack_copy_inode_size(inode, h_inode);
  3875. +}
  3876. +
  3877. +void au_cpup_attr_nlink(struct inode *inode, int force)
  3878. +{
  3879. + struct inode *h_inode;
  3880. + struct super_block *sb;
  3881. + aufs_bindex_t bindex, bbot;
  3882. +
  3883. + sb = inode->i_sb;
  3884. + bindex = au_ibtop(inode);
  3885. + h_inode = au_h_iptr(inode, bindex);
  3886. + if (!force
  3887. + && !S_ISDIR(h_inode->i_mode)
  3888. + && au_opt_test(au_mntflags(sb), PLINK)
  3889. + && au_plink_test(inode))
  3890. + return;
  3891. +
  3892. + /*
  3893. + * 0 can happen in revalidating.
  3894. + * h_inode->i_mutex may not be held here, but it is harmless since once
  3895. + * i_nlink reaches 0, it will never become positive except O_TMPFILE
  3896. + * case.
  3897. + * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
  3898. + * the incorrect link count.
  3899. + */
  3900. + set_nlink(inode, h_inode->i_nlink);
  3901. +
  3902. + /*
  3903. + * fewer nlink makes find(1) noisy, but larger nlink doesn't.
  3904. + * it may includes whplink directory.
  3905. + */
  3906. + if (S_ISDIR(h_inode->i_mode)) {
  3907. + bbot = au_ibbot(inode);
  3908. + for (bindex++; bindex <= bbot; bindex++) {
  3909. + h_inode = au_h_iptr(inode, bindex);
  3910. + if (h_inode)
  3911. + au_add_nlink(inode, h_inode);
  3912. + }
  3913. + }
  3914. +}
  3915. +
  3916. +void au_cpup_attr_changeable(struct inode *inode)
  3917. +{
  3918. + struct inode *h_inode;
  3919. +
  3920. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  3921. + inode->i_mode = h_inode->i_mode;
  3922. + inode->i_uid = h_inode->i_uid;
  3923. + inode->i_gid = h_inode->i_gid;
  3924. + au_cpup_attr_timesizes(inode);
  3925. + au_cpup_attr_flags(inode, h_inode->i_flags);
  3926. +}
  3927. +
  3928. +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
  3929. +{
  3930. + struct au_iinfo *iinfo = au_ii(inode);
  3931. +
  3932. + IiMustWriteLock(inode);
  3933. +
  3934. + iinfo->ii_higen = h_inode->i_generation;
  3935. + iinfo->ii_hsb1 = h_inode->i_sb;
  3936. +}
  3937. +
  3938. +void au_cpup_attr_all(struct inode *inode, int force)
  3939. +{
  3940. + struct inode *h_inode;
  3941. +
  3942. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  3943. + au_cpup_attr_changeable(inode);
  3944. + if (inode->i_nlink > 0)
  3945. + au_cpup_attr_nlink(inode, force);
  3946. + inode->i_rdev = h_inode->i_rdev;
  3947. + inode->i_blkbits = h_inode->i_blkbits;
  3948. + au_cpup_igen(inode, h_inode);
  3949. +}
  3950. +
  3951. +/* ---------------------------------------------------------------------- */
  3952. +
  3953. +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
  3954. +
  3955. +/* keep the timestamps of the parent dir when cpup */
  3956. +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
  3957. + struct path *h_path)
  3958. +{
  3959. + struct inode *h_inode;
  3960. +
  3961. + dt->dt_dentry = dentry;
  3962. + dt->dt_h_path = *h_path;
  3963. + h_inode = d_inode(h_path->dentry);
  3964. + dt->dt_atime = h_inode->i_atime;
  3965. + dt->dt_mtime = h_inode->i_mtime;
  3966. + /* smp_mb(); */
  3967. +}
  3968. +
  3969. +void au_dtime_revert(struct au_dtime *dt)
  3970. +{
  3971. + struct iattr attr;
  3972. + int err;
  3973. +
  3974. + attr.ia_atime = dt->dt_atime;
  3975. + attr.ia_mtime = dt->dt_mtime;
  3976. + attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
  3977. + | ATTR_ATIME | ATTR_ATIME_SET;
  3978. +
  3979. + /* no delegation since this is a directory */
  3980. + err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
  3981. + if (unlikely(err))
  3982. + pr_warn("restoring timestamps failed(%d). ignored\n", err);
  3983. +}
  3984. +
  3985. +/* ---------------------------------------------------------------------- */
  3986. +
  3987. +/* internal use only */
  3988. +struct au_cpup_reg_attr {
  3989. + int valid;
  3990. + struct kstat st;
  3991. + unsigned int iflags; /* inode->i_flags */
  3992. +};
  3993. +
  3994. +static noinline_for_stack
  3995. +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct path *h_src,
  3996. + struct au_cpup_reg_attr *h_src_attr)
  3997. +{
  3998. + int err, sbits, icex;
  3999. + unsigned int mnt_flags;
  4000. + unsigned char verbose;
  4001. + struct iattr ia;
  4002. + struct path h_path;
  4003. + struct inode *h_isrc, *h_idst;
  4004. + struct kstat *h_st;
  4005. + struct au_branch *br;
  4006. +
  4007. + br = au_sbr(dst->d_sb, bindex);
  4008. + h_path.mnt = au_br_mnt(br);
  4009. + h_path.dentry = au_h_dptr(dst, bindex);
  4010. + h_idst = d_inode(h_path.dentry);
  4011. + h_isrc = d_inode(h_src->dentry);
  4012. + ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
  4013. + | ATTR_ATIME | ATTR_MTIME
  4014. + | ATTR_ATIME_SET | ATTR_MTIME_SET;
  4015. + if (h_src_attr && h_src_attr->valid) {
  4016. + h_st = &h_src_attr->st;
  4017. + ia.ia_uid = h_st->uid;
  4018. + ia.ia_gid = h_st->gid;
  4019. + ia.ia_atime = h_st->atime;
  4020. + ia.ia_mtime = h_st->mtime;
  4021. + if (h_idst->i_mode != h_st->mode
  4022. + && !S_ISLNK(h_idst->i_mode)) {
  4023. + ia.ia_valid |= ATTR_MODE;
  4024. + ia.ia_mode = h_st->mode;
  4025. + }
  4026. + sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
  4027. + au_cpup_attr_flags(h_idst, h_src_attr->iflags);
  4028. + } else {
  4029. + ia.ia_uid = h_isrc->i_uid;
  4030. + ia.ia_gid = h_isrc->i_gid;
  4031. + ia.ia_atime = h_isrc->i_atime;
  4032. + ia.ia_mtime = h_isrc->i_mtime;
  4033. + if (h_idst->i_mode != h_isrc->i_mode
  4034. + && !S_ISLNK(h_idst->i_mode)) {
  4035. + ia.ia_valid |= ATTR_MODE;
  4036. + ia.ia_mode = h_isrc->i_mode;
  4037. + }
  4038. + sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
  4039. + au_cpup_attr_flags(h_idst, h_isrc->i_flags);
  4040. + }
  4041. + /* no delegation since it is just created */
  4042. + err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
  4043. +
  4044. + /* is this nfs only? */
  4045. + if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
  4046. + ia.ia_valid = ATTR_FORCE | ATTR_MODE;
  4047. + ia.ia_mode = h_isrc->i_mode;
  4048. + err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
  4049. + }
  4050. +
  4051. + icex = br->br_perm & AuBrAttr_ICEX;
  4052. + if (!err) {
  4053. + mnt_flags = au_mntflags(dst->d_sb);
  4054. + verbose = !!au_opt_test(mnt_flags, VERBOSE);
  4055. + err = au_cpup_xattr(&h_path, h_src, icex, verbose);
  4056. + }
  4057. +
  4058. + return err;
  4059. +}
  4060. +
  4061. +/* ---------------------------------------------------------------------- */
  4062. +
  4063. +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
  4064. + char *buf, unsigned long blksize)
  4065. +{
  4066. + int err;
  4067. + size_t sz, rbytes, wbytes;
  4068. + unsigned char all_zero;
  4069. + char *p, *zp;
  4070. + struct inode *h_inode;
  4071. + /* reduce stack usage */
  4072. + struct iattr *ia;
  4073. +
  4074. + zp = page_address(ZERO_PAGE(0));
  4075. + if (unlikely(!zp))
  4076. + return -ENOMEM; /* possible? */
  4077. +
  4078. + err = 0;
  4079. + all_zero = 0;
  4080. + while (len) {
  4081. + AuDbg("len %lld\n", len);
  4082. + sz = blksize;
  4083. + if (len < blksize)
  4084. + sz = len;
  4085. +
  4086. + rbytes = 0;
  4087. + /* todo: signal_pending? */
  4088. + while (!rbytes || err == -EAGAIN || err == -EINTR) {
  4089. + rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
  4090. + err = rbytes;
  4091. + }
  4092. + if (unlikely(err < 0))
  4093. + break;
  4094. +
  4095. + all_zero = 0;
  4096. + if (len >= rbytes && rbytes == blksize)
  4097. + all_zero = !memcmp(buf, zp, rbytes);
  4098. + if (!all_zero) {
  4099. + wbytes = rbytes;
  4100. + p = buf;
  4101. + while (wbytes) {
  4102. + size_t b;
  4103. +
  4104. + b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
  4105. + err = b;
  4106. + /* todo: signal_pending? */
  4107. + if (unlikely(err == -EAGAIN || err == -EINTR))
  4108. + continue;
  4109. + if (unlikely(err < 0))
  4110. + break;
  4111. + wbytes -= b;
  4112. + p += b;
  4113. + }
  4114. + if (unlikely(err < 0))
  4115. + break;
  4116. + } else {
  4117. + loff_t res;
  4118. +
  4119. + AuLabel(hole);
  4120. + res = vfsub_llseek(dst, rbytes, SEEK_CUR);
  4121. + err = res;
  4122. + if (unlikely(res < 0))
  4123. + break;
  4124. + }
  4125. + len -= rbytes;
  4126. + err = 0;
  4127. + }
  4128. +
  4129. + /* the last block may be a hole */
  4130. + if (!err && all_zero) {
  4131. + AuLabel(last hole);
  4132. +
  4133. + err = 1;
  4134. + if (au_test_nfs(dst->f_path.dentry->d_sb)) {
  4135. + /* nfs requires this step to make last hole */
  4136. + /* is this only nfs? */
  4137. + do {
  4138. + /* todo: signal_pending? */
  4139. + err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
  4140. + } while (err == -EAGAIN || err == -EINTR);
  4141. + if (err == 1)
  4142. + dst->f_pos--;
  4143. + }
  4144. +
  4145. + if (err == 1) {
  4146. + ia = (void *)buf;
  4147. + ia->ia_size = dst->f_pos;
  4148. + ia->ia_valid = ATTR_SIZE | ATTR_FILE;
  4149. + ia->ia_file = dst;
  4150. + h_inode = file_inode(dst);
  4151. + inode_lock_nested(h_inode, AuLsc_I_CHILD2);
  4152. + /* no delegation since it is just created */
  4153. + err = vfsub_notify_change(&dst->f_path, ia,
  4154. + /*delegated*/NULL);
  4155. + inode_unlock(h_inode);
  4156. + }
  4157. + }
  4158. +
  4159. + return err;
  4160. +}
  4161. +
  4162. +int au_copy_file(struct file *dst, struct file *src, loff_t len)
  4163. +{
  4164. + int err;
  4165. + unsigned long blksize;
  4166. + unsigned char do_kfree;
  4167. + char *buf;
  4168. + struct super_block *h_sb;
  4169. +
  4170. + err = -ENOMEM;
  4171. + h_sb = file_inode(dst)->i_sb;
  4172. + blksize = h_sb->s_blocksize;
  4173. + if (!blksize || PAGE_SIZE < blksize)
  4174. + blksize = PAGE_SIZE;
  4175. + AuDbg("blksize %lu\n", blksize);
  4176. + do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
  4177. + if (do_kfree)
  4178. + buf = kmalloc(blksize, GFP_NOFS);
  4179. + else
  4180. + buf = (void *)__get_free_page(GFP_NOFS);
  4181. + if (unlikely(!buf))
  4182. + goto out;
  4183. +
  4184. + if (len > (1 << 22))
  4185. + AuDbg("copying a large file %lld\n", (long long)len);
  4186. +
  4187. + src->f_pos = 0;
  4188. + dst->f_pos = 0;
  4189. + err = au_do_copy_file(dst, src, len, buf, blksize);
  4190. + if (do_kfree) {
  4191. + AuDebugOn(!au_kfree_do_sz_test(blksize));
  4192. + au_kfree_do_rcu(buf);
  4193. + } else
  4194. + free_page((unsigned long)buf);
  4195. +
  4196. +out:
  4197. + return err;
  4198. +}
  4199. +
  4200. +static int au_do_copy(struct file *dst, struct file *src, loff_t len)
  4201. +{
  4202. + int err;
  4203. + struct super_block *h_src_sb;
  4204. + struct inode *h_src_inode;
  4205. +
  4206. + h_src_inode = file_inode(src);
  4207. + h_src_sb = h_src_inode->i_sb;
  4208. +
  4209. + /* XFS acquires inode_lock */
  4210. + if (!au_test_xfs(h_src_sb))
  4211. + err = au_copy_file(dst, src, len);
  4212. + else {
  4213. + inode_unlock_shared(h_src_inode);
  4214. + err = au_copy_file(dst, src, len);
  4215. + inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
  4216. + }
  4217. +
  4218. + return err;
  4219. +}
  4220. +
  4221. +static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
  4222. +{
  4223. + int err;
  4224. + loff_t lo;
  4225. + struct super_block *h_src_sb;
  4226. + struct inode *h_src_inode;
  4227. +
  4228. + h_src_inode = file_inode(src);
  4229. + h_src_sb = h_src_inode->i_sb;
  4230. + if (h_src_sb != file_inode(dst)->i_sb
  4231. + || !dst->f_op->remap_file_range) {
  4232. + err = au_do_copy(dst, src, len);
  4233. + goto out;
  4234. + }
  4235. +
  4236. + if (!au_test_nfs(h_src_sb)) {
  4237. + inode_unlock_shared(h_src_inode);
  4238. + lo = vfsub_clone_file_range(src, dst, len);
  4239. + inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
  4240. + } else
  4241. + lo = vfsub_clone_file_range(src, dst, len);
  4242. + if (lo == len) {
  4243. + err = 0;
  4244. + goto out; /* success */
  4245. + } else if (lo >= 0)
  4246. + /* todo: possible? */
  4247. + /* paritially succeeded */
  4248. + AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
  4249. + else if (lo != -EOPNOTSUPP) {
  4250. + /* older XFS has a condition in cloning */
  4251. + err = lo;
  4252. + goto out;
  4253. + }
  4254. +
  4255. + /* the backend fs on NFS may not support cloning */
  4256. + err = au_do_copy(dst, src, len);
  4257. +
  4258. +out:
  4259. + AuTraceErr(err);
  4260. + return err;
  4261. +}
  4262. +
  4263. +/*
  4264. + * to support a sparse file which is opened with O_APPEND,
  4265. + * we need to close the file.
  4266. + */
  4267. +static int au_cp_regular(struct au_cp_generic *cpg)
  4268. +{
  4269. + int err, i;
  4270. + enum { SRC, DST };
  4271. + struct {
  4272. + aufs_bindex_t bindex;
  4273. + unsigned int flags;
  4274. + struct dentry *dentry;
  4275. + int force_wr;
  4276. + struct file *file;
  4277. + } *f, file[] = {
  4278. + {
  4279. + .bindex = cpg->bsrc,
  4280. + .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
  4281. + },
  4282. + {
  4283. + .bindex = cpg->bdst,
  4284. + .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
  4285. + .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
  4286. + }
  4287. + };
  4288. + struct au_branch *br;
  4289. + struct super_block *sb, *h_src_sb;
  4290. + struct inode *h_src_inode;
  4291. + struct task_struct *tsk = current;
  4292. +
  4293. + /* bsrc branch can be ro/rw. */
  4294. + sb = cpg->dentry->d_sb;
  4295. + f = file;
  4296. + for (i = 0; i < 2; i++, f++) {
  4297. + f->dentry = au_h_dptr(cpg->dentry, f->bindex);
  4298. + f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
  4299. + /*file*/NULL, f->force_wr);
  4300. + if (IS_ERR(f->file)) {
  4301. + err = PTR_ERR(f->file);
  4302. + if (i == SRC)
  4303. + goto out;
  4304. + else
  4305. + goto out_src;
  4306. + }
  4307. + }
  4308. +
  4309. + /* try stopping to update while we copyup */
  4310. + h_src_inode = d_inode(file[SRC].dentry);
  4311. + h_src_sb = h_src_inode->i_sb;
  4312. + if (!au_test_nfs(h_src_sb))
  4313. + IMustLock(h_src_inode);
  4314. + err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
  4315. +
  4316. + /* i wonder if we had O_NO_DELAY_FPUT flag */
  4317. + if (tsk->flags & PF_KTHREAD)
  4318. + __fput_sync(file[DST].file);
  4319. + else {
  4320. + /* it happened actually */
  4321. + fput(file[DST].file);
  4322. + /*
  4323. + * too bad.
  4324. + * we have to call both since we don't know which place the file
  4325. + * was added to.
  4326. + */
  4327. + task_work_run();
  4328. + flush_delayed_fput();
  4329. + }
  4330. + br = au_sbr(sb, file[DST].bindex);
  4331. + au_lcnt_dec(&br->br_nfiles);
  4332. +
  4333. +out_src:
  4334. + fput(file[SRC].file);
  4335. + br = au_sbr(sb, file[SRC].bindex);
  4336. + au_lcnt_dec(&br->br_nfiles);
  4337. +out:
  4338. + return err;
  4339. +}
  4340. +
  4341. +static int au_do_cpup_regular(struct au_cp_generic *cpg,
  4342. + struct au_cpup_reg_attr *h_src_attr)
  4343. +{
  4344. + int err, rerr;
  4345. + loff_t l;
  4346. + struct path h_path;
  4347. + struct inode *h_src_inode, *h_dst_inode;
  4348. +
  4349. + err = 0;
  4350. + h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
  4351. + l = i_size_read(h_src_inode);
  4352. + if (cpg->len == -1 || l < cpg->len)
  4353. + cpg->len = l;
  4354. + if (cpg->len) {
  4355. + /* try stopping to update while we are referencing */
  4356. + inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
  4357. + au_pin_hdir_unlock(cpg->pin);
  4358. +
  4359. + h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
  4360. + h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
  4361. + h_src_attr->iflags = h_src_inode->i_flags;
  4362. + if (!au_test_nfs(h_src_inode->i_sb))
  4363. + err = vfsub_getattr(&h_path, &h_src_attr->st);
  4364. + else {
  4365. + inode_unlock_shared(h_src_inode);
  4366. + err = vfsub_getattr(&h_path, &h_src_attr->st);
  4367. + inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
  4368. + }
  4369. + if (unlikely(err)) {
  4370. + inode_unlock_shared(h_src_inode);
  4371. + goto out;
  4372. + }
  4373. + h_src_attr->valid = 1;
  4374. + if (!au_test_nfs(h_src_inode->i_sb)) {
  4375. + err = au_cp_regular(cpg);
  4376. + inode_unlock_shared(h_src_inode);
  4377. + } else {
  4378. + inode_unlock_shared(h_src_inode);
  4379. + err = au_cp_regular(cpg);
  4380. + }
  4381. + rerr = au_pin_hdir_relock(cpg->pin);
  4382. + if (!err && rerr)
  4383. + err = rerr;
  4384. + }
  4385. + if (!err && (h_src_inode->i_state & I_LINKABLE)) {
  4386. + h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
  4387. + h_dst_inode = d_inode(h_path.dentry);
  4388. + spin_lock(&h_dst_inode->i_lock);
  4389. + h_dst_inode->i_state |= I_LINKABLE;
  4390. + spin_unlock(&h_dst_inode->i_lock);
  4391. + }
  4392. +
  4393. +out:
  4394. + return err;
  4395. +}
  4396. +
  4397. +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
  4398. + struct inode *h_dir)
  4399. +{
  4400. + int err;
  4401. + DEFINE_DELAYED_CALL(done);
  4402. + const char *sym;
  4403. +
  4404. + sym = vfs_get_link(h_src, &done);
  4405. + err = PTR_ERR(sym);
  4406. + if (IS_ERR(sym))
  4407. + goto out;
  4408. +
  4409. + err = vfsub_symlink(h_dir, h_path, sym);
  4410. +
  4411. +out:
  4412. + do_delayed_call(&done);
  4413. + return err;
  4414. +}
  4415. +
  4416. +/*
  4417. + * regardless 'acl' option, reset all ACL.
  4418. + * All ACL will be copied up later from the original entry on the lower branch.
  4419. + */
  4420. +static int au_reset_acl(struct path *h_path, umode_t mode)
  4421. +{
  4422. + int err;
  4423. + struct dentry *h_dentry;
  4424. + struct inode *h_inode;
  4425. + struct user_namespace *h_userns;
  4426. +
  4427. + h_userns = mnt_user_ns(h_path->mnt);
  4428. + h_dentry = h_path->dentry;
  4429. + h_inode = d_inode(h_dentry);
  4430. + /* forget_all_cached_acls(h_inode)); */
  4431. + err = vfsub_removexattr(h_userns, h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
  4432. + AuTraceErr(err);
  4433. + if (err == -EOPNOTSUPP)
  4434. + err = 0;
  4435. + if (!err)
  4436. + err = vfsub_acl_chmod(h_userns, h_inode, mode);
  4437. +
  4438. + AuTraceErr(err);
  4439. + return err;
  4440. +}
  4441. +
  4442. +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
  4443. + struct inode *h_dir, struct path *h_path)
  4444. +{
  4445. + int err;
  4446. + struct inode *dir, *inode;
  4447. + struct user_namespace *h_userns;
  4448. +
  4449. + h_userns = mnt_user_ns(h_path->mnt);
  4450. + err = vfsub_removexattr(h_userns, h_path->dentry,
  4451. + XATTR_NAME_POSIX_ACL_DEFAULT);
  4452. + AuTraceErr(err);
  4453. + if (err == -EOPNOTSUPP)
  4454. + err = 0;
  4455. + if (unlikely(err))
  4456. + goto out;
  4457. +
  4458. + /*
  4459. + * strange behaviour from the users view,
  4460. + * particularly setattr case
  4461. + */
  4462. + dir = d_inode(dst_parent);
  4463. + if (au_ibtop(dir) == cpg->bdst)
  4464. + au_cpup_attr_nlink(dir, /*force*/1);
  4465. + inode = d_inode(cpg->dentry);
  4466. + au_cpup_attr_nlink(inode, /*force*/1);
  4467. +
  4468. +out:
  4469. + return err;
  4470. +}
  4471. +
  4472. +static noinline_for_stack
  4473. +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
  4474. + struct au_cpup_reg_attr *h_src_attr)
  4475. +{
  4476. + int err;
  4477. + umode_t mode;
  4478. + unsigned int mnt_flags;
  4479. + unsigned char isdir, isreg, force;
  4480. + const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
  4481. + struct au_dtime dt;
  4482. + struct path h_path;
  4483. + struct dentry *h_src, *h_dst, *h_parent;
  4484. + struct inode *h_inode, *h_dir;
  4485. + struct super_block *sb;
  4486. +
  4487. + /* bsrc branch can be ro/rw. */
  4488. + h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
  4489. + h_inode = d_inode(h_src);
  4490. + AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
  4491. +
  4492. + /* try stopping to be referenced while we are creating */
  4493. + h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
  4494. + if (au_ftest_cpup(cpg->flags, RENAME))
  4495. + AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
  4496. + AUFS_WH_PFX_LEN));
  4497. + h_parent = h_dst->d_parent; /* dir inode is locked */
  4498. + h_dir = d_inode(h_parent);
  4499. + IMustLock(h_dir);
  4500. + AuDebugOn(h_parent != h_dst->d_parent);
  4501. +
  4502. + sb = cpg->dentry->d_sb;
  4503. + h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
  4504. + if (do_dt) {
  4505. + h_path.dentry = h_parent;
  4506. + au_dtime_store(&dt, dst_parent, &h_path);
  4507. + }
  4508. + h_path.dentry = h_dst;
  4509. +
  4510. + isreg = 0;
  4511. + isdir = 0;
  4512. + mode = h_inode->i_mode;
  4513. + switch (mode & S_IFMT) {
  4514. + case S_IFREG:
  4515. + isreg = 1;
  4516. + err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
  4517. + if (!err)
  4518. + err = au_do_cpup_regular(cpg, h_src_attr);
  4519. + break;
  4520. + case S_IFDIR:
  4521. + isdir = 1;
  4522. + err = vfsub_mkdir(h_dir, &h_path, mode);
  4523. + if (!err)
  4524. + err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
  4525. + break;
  4526. + case S_IFLNK:
  4527. + err = au_do_cpup_symlink(&h_path, h_src, h_dir);
  4528. + break;
  4529. + case S_IFCHR:
  4530. + case S_IFBLK:
  4531. + AuDebugOn(!capable(CAP_MKNOD));
  4532. + fallthrough;
  4533. + case S_IFIFO:
  4534. + case S_IFSOCK:
  4535. + err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
  4536. + break;
  4537. + default:
  4538. + AuIOErr("Unknown inode type 0%o\n", mode);
  4539. + err = -EIO;
  4540. + }
  4541. + if (!err)
  4542. + err = au_reset_acl(&h_path, mode);
  4543. +
  4544. + mnt_flags = au_mntflags(sb);
  4545. + if (!au_opt_test(mnt_flags, UDBA_NONE)
  4546. + && !isdir
  4547. + && au_opt_test(mnt_flags, XINO)
  4548. + && (h_inode->i_nlink == 1
  4549. + || (h_inode->i_state & I_LINKABLE))
  4550. + /* todo: unnecessary? */
  4551. + /* && d_inode(cpg->dentry)->i_nlink == 1 */
  4552. + && cpg->bdst < cpg->bsrc
  4553. + && !au_ftest_cpup(cpg->flags, KEEPLINO))
  4554. + au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
  4555. + /* ignore this error */
  4556. +
  4557. + if (!err) {
  4558. + force = 0;
  4559. + if (isreg) {
  4560. + force = !!cpg->len;
  4561. + if (cpg->len == -1)
  4562. + force = !!i_size_read(h_inode);
  4563. + }
  4564. + au_fhsm_wrote(sb, cpg->bdst, force);
  4565. + }
  4566. +
  4567. + if (do_dt)
  4568. + au_dtime_revert(&dt);
  4569. + return err;
  4570. +}
  4571. +
  4572. +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
  4573. +{
  4574. + int err;
  4575. + struct dentry *dentry, *h_dentry, *h_parent, *parent;
  4576. + struct path h_ppath;
  4577. + struct inode *h_dir;
  4578. + aufs_bindex_t bdst;
  4579. +
  4580. + dentry = cpg->dentry;
  4581. + bdst = cpg->bdst;
  4582. + h_ppath.mnt = au_sbr_mnt(dentry->d_sb, bdst);
  4583. + h_dentry = au_h_dptr(dentry, bdst);
  4584. + if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
  4585. + dget(h_dentry);
  4586. + au_set_h_dptr(dentry, bdst, NULL);
  4587. + err = au_lkup_neg(dentry, bdst, /*wh*/0);
  4588. + if (!err)
  4589. + h_path->dentry = dget(au_h_dptr(dentry, bdst));
  4590. + au_set_h_dptr(dentry, bdst, h_dentry);
  4591. + } else {
  4592. + err = 0;
  4593. + parent = dget_parent(dentry);
  4594. + h_ppath.dentry = au_h_dptr(parent, bdst);
  4595. + dput(parent);
  4596. + h_path->dentry = vfsub_lkup_one(&dentry->d_name, &h_ppath);
  4597. + if (IS_ERR(h_path->dentry))
  4598. + err = PTR_ERR(h_path->dentry);
  4599. + }
  4600. + if (unlikely(err))
  4601. + goto out;
  4602. +
  4603. + h_parent = h_dentry->d_parent; /* dir inode is locked */
  4604. + h_dir = d_inode(h_parent);
  4605. + IMustLock(h_dir);
  4606. + AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
  4607. + /* no delegation since it is just created */
  4608. + err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
  4609. + /*flags*/0);
  4610. + dput(h_path->dentry);
  4611. +
  4612. +out:
  4613. + return err;
  4614. +}
  4615. +
  4616. +/*
  4617. + * copyup the @dentry from @bsrc to @bdst.
  4618. + * the caller must set the both of lower dentries.
  4619. + * @len is for truncating when it is -1 copyup the entire file.
  4620. + * in link/rename cases, @dst_parent may be different from the real one.
  4621. + * basic->bsrc can be larger than basic->bdst.
  4622. + * aufs doesn't touch the credential so
  4623. + * security_inode_copy_up{,_xattr}() are unnecessary.
  4624. + */
  4625. +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
  4626. +{
  4627. + int err, rerr;
  4628. + aufs_bindex_t old_ibtop;
  4629. + unsigned char isdir, plink;
  4630. + struct dentry *h_src, *h_dst, *h_parent;
  4631. + struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
  4632. + struct super_block *sb;
  4633. + struct au_branch *br;
  4634. + struct path h_src_path;
  4635. + /* to reduce stack size */
  4636. + struct {
  4637. + struct au_dtime dt;
  4638. + struct path h_path;
  4639. + struct au_cpup_reg_attr h_src_attr;
  4640. + } *a;
  4641. +
  4642. + err = -ENOMEM;
  4643. + a = kmalloc(sizeof(*a), GFP_NOFS);
  4644. + if (unlikely(!a))
  4645. + goto out;
  4646. + a->h_src_attr.valid = 0;
  4647. +
  4648. + sb = cpg->dentry->d_sb;
  4649. + br = au_sbr(sb, cpg->bdst);
  4650. + a->h_path.mnt = au_br_mnt(br);
  4651. + h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
  4652. + h_parent = h_dst->d_parent; /* dir inode is locked */
  4653. + h_dir = d_inode(h_parent);
  4654. + IMustLock(h_dir);
  4655. +
  4656. + h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
  4657. + inode = d_inode(cpg->dentry);
  4658. +
  4659. + if (!dst_parent)
  4660. + dst_parent = dget_parent(cpg->dentry);
  4661. + else
  4662. + dget(dst_parent);
  4663. +
  4664. + plink = !!au_opt_test(au_mntflags(sb), PLINK);
  4665. + dst_inode = au_h_iptr(inode, cpg->bdst);
  4666. + if (dst_inode) {
  4667. + if (unlikely(!plink)) {
  4668. + err = -EIO;
  4669. + AuIOErr("hi%lu(i%lu) exists on b%d "
  4670. + "but plink is disabled\n",
  4671. + dst_inode->i_ino, inode->i_ino, cpg->bdst);
  4672. + goto out_parent;
  4673. + }
  4674. +
  4675. + if (dst_inode->i_nlink) {
  4676. + const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
  4677. +
  4678. + h_src = au_plink_lkup(inode, cpg->bdst);
  4679. + err = PTR_ERR(h_src);
  4680. + if (IS_ERR(h_src))
  4681. + goto out_parent;
  4682. + if (unlikely(d_is_negative(h_src))) {
  4683. + err = -EIO;
  4684. + AuIOErr("i%lu exists on b%d "
  4685. + "but not pseudo-linked\n",
  4686. + inode->i_ino, cpg->bdst);
  4687. + dput(h_src);
  4688. + goto out_parent;
  4689. + }
  4690. +
  4691. + if (do_dt) {
  4692. + a->h_path.dentry = h_parent;
  4693. + au_dtime_store(&a->dt, dst_parent, &a->h_path);
  4694. + }
  4695. +
  4696. + a->h_path.dentry = h_dst;
  4697. + delegated = NULL;
  4698. + err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
  4699. + if (!err && au_ftest_cpup(cpg->flags, RENAME))
  4700. + err = au_do_ren_after_cpup(cpg, &a->h_path);
  4701. + if (do_dt)
  4702. + au_dtime_revert(&a->dt);
  4703. + if (unlikely(err == -EWOULDBLOCK)) {
  4704. + pr_warn("cannot retry for NFSv4 delegation"
  4705. + " for an internal link\n");
  4706. + iput(delegated);
  4707. + }
  4708. + dput(h_src);
  4709. + goto out_parent;
  4710. + } else
  4711. + /* todo: cpup_wh_file? */
  4712. + /* udba work */
  4713. + au_update_ibrange(inode, /*do_put_zero*/1);
  4714. + }
  4715. +
  4716. + isdir = S_ISDIR(inode->i_mode);
  4717. + old_ibtop = au_ibtop(inode);
  4718. + err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
  4719. + if (unlikely(err))
  4720. + goto out_rev;
  4721. + dst_inode = d_inode(h_dst);
  4722. + inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
  4723. + /* todo: necessary? */
  4724. + /* au_pin_hdir_unlock(cpg->pin); */
  4725. +
  4726. + h_src_path.dentry = h_src;
  4727. + h_src_path.mnt = au_sbr_mnt(sb, cpg->bsrc);
  4728. + err = cpup_iattr(cpg->dentry, cpg->bdst, &h_src_path, &a->h_src_attr);
  4729. + if (unlikely(err)) {
  4730. + /* todo: necessary? */
  4731. + /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
  4732. + inode_unlock(dst_inode);
  4733. + goto out_rev;
  4734. + }
  4735. +
  4736. + if (cpg->bdst < old_ibtop) {
  4737. + if (S_ISREG(inode->i_mode)) {
  4738. + err = au_dy_iaop(inode, cpg->bdst, dst_inode);
  4739. + if (unlikely(err)) {
  4740. + /* ignore an error */
  4741. + /* au_pin_hdir_relock(cpg->pin); */
  4742. + inode_unlock(dst_inode);
  4743. + goto out_rev;
  4744. + }
  4745. + }
  4746. + au_set_ibtop(inode, cpg->bdst);
  4747. + } else
  4748. + au_set_ibbot(inode, cpg->bdst);
  4749. + au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
  4750. + au_hi_flags(inode, isdir));
  4751. +
  4752. + /* todo: necessary? */
  4753. + /* err = au_pin_hdir_relock(cpg->pin); */
  4754. + inode_unlock(dst_inode);
  4755. + if (unlikely(err))
  4756. + goto out_rev;
  4757. +
  4758. + src_inode = d_inode(h_src);
  4759. + if (!isdir
  4760. + && (src_inode->i_nlink > 1
  4761. + || src_inode->i_state & I_LINKABLE)
  4762. + && plink)
  4763. + au_plink_append(inode, cpg->bdst, h_dst);
  4764. +
  4765. + if (au_ftest_cpup(cpg->flags, RENAME)) {
  4766. + a->h_path.dentry = h_dst;
  4767. + err = au_do_ren_after_cpup(cpg, &a->h_path);
  4768. + }
  4769. + if (!err)
  4770. + goto out_parent; /* success */
  4771. +
  4772. + /* revert */
  4773. +out_rev:
  4774. + a->h_path.dentry = h_parent;
  4775. + au_dtime_store(&a->dt, dst_parent, &a->h_path);
  4776. + a->h_path.dentry = h_dst;
  4777. + rerr = 0;
  4778. + if (d_is_positive(h_dst)) {
  4779. + if (!isdir) {
  4780. + /* no delegation since it is just created */
  4781. + rerr = vfsub_unlink(h_dir, &a->h_path,
  4782. + /*delegated*/NULL, /*force*/0);
  4783. + } else
  4784. + rerr = vfsub_rmdir(h_dir, &a->h_path);
  4785. + }
  4786. + au_dtime_revert(&a->dt);
  4787. + if (rerr) {
  4788. + AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
  4789. + err = -EIO;
  4790. + }
  4791. +out_parent:
  4792. + dput(dst_parent);
  4793. + au_kfree_rcu(a);
  4794. +out:
  4795. + return err;
  4796. +}
  4797. +
  4798. +#if 0 /* reserved */
  4799. +struct au_cpup_single_args {
  4800. + int *errp;
  4801. + struct au_cp_generic *cpg;
  4802. + struct dentry *dst_parent;
  4803. +};
  4804. +
  4805. +static void au_call_cpup_single(void *args)
  4806. +{
  4807. + struct au_cpup_single_args *a = args;
  4808. +
  4809. + au_pin_hdir_acquire_nest(a->cpg->pin);
  4810. + *a->errp = au_cpup_single(a->cpg, a->dst_parent);
  4811. + au_pin_hdir_release(a->cpg->pin);
  4812. +}
  4813. +#endif
  4814. +
  4815. +/*
  4816. + * prevent SIGXFSZ in copy-up.
  4817. + * testing CAP_MKNOD is for generic fs,
  4818. + * but CAP_FSETID is for xfs only, currently.
  4819. + */
  4820. +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
  4821. +{
  4822. + int do_sio;
  4823. + struct super_block *sb;
  4824. + struct inode *h_dir;
  4825. +
  4826. + do_sio = 0;
  4827. + sb = au_pinned_parent(pin)->d_sb;
  4828. + if (!au_wkq_test()
  4829. + && (!au_sbi(sb)->si_plink_maint_pid
  4830. + || au_plink_maint(sb, AuLock_NOPLM))) {
  4831. + switch (mode & S_IFMT) {
  4832. + case S_IFREG:
  4833. + /* no condition about RLIMIT_FSIZE and the file size */
  4834. + do_sio = 1;
  4835. + break;
  4836. + case S_IFCHR:
  4837. + case S_IFBLK:
  4838. + do_sio = !capable(CAP_MKNOD);
  4839. + break;
  4840. + }
  4841. + if (!do_sio)
  4842. + do_sio = ((mode & (S_ISUID | S_ISGID))
  4843. + && !capable(CAP_FSETID));
  4844. + /* this workaround may be removed in the future */
  4845. + if (!do_sio) {
  4846. + h_dir = au_pinned_h_dir(pin);
  4847. + do_sio = h_dir->i_mode & S_ISVTX;
  4848. + }
  4849. + }
  4850. +
  4851. + return do_sio;
  4852. +}
  4853. +
  4854. +#if 0 /* reserved */
  4855. +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
  4856. +{
  4857. + int err, wkq_err;
  4858. + struct dentry *h_dentry;
  4859. +
  4860. + h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
  4861. + if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
  4862. + err = au_cpup_single(cpg, dst_parent);
  4863. + else {
  4864. + struct au_cpup_single_args args = {
  4865. + .errp = &err,
  4866. + .cpg = cpg,
  4867. + .dst_parent = dst_parent
  4868. + };
  4869. + wkq_err = au_wkq_wait(au_call_cpup_single, &args);
  4870. + if (unlikely(wkq_err))
  4871. + err = wkq_err;
  4872. + }
  4873. +
  4874. + return err;
  4875. +}
  4876. +#endif
  4877. +
  4878. +/*
  4879. + * copyup the @dentry from the first active lower branch to @bdst,
  4880. + * using au_cpup_single().
  4881. + */
  4882. +static int au_cpup_simple(struct au_cp_generic *cpg)
  4883. +{
  4884. + int err;
  4885. + unsigned int flags_orig;
  4886. + struct dentry *dentry;
  4887. +
  4888. + AuDebugOn(cpg->bsrc < 0);
  4889. +
  4890. + dentry = cpg->dentry;
  4891. + DiMustWriteLock(dentry);
  4892. +
  4893. + err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
  4894. + if (!err) {
  4895. + flags_orig = cpg->flags;
  4896. + au_fset_cpup(cpg->flags, RENAME);
  4897. + err = au_cpup_single(cpg, NULL);
  4898. + cpg->flags = flags_orig;
  4899. + if (!err)
  4900. + return 0; /* success */
  4901. +
  4902. + /* revert */
  4903. + au_set_h_dptr(dentry, cpg->bdst, NULL);
  4904. + au_set_dbtop(dentry, cpg->bsrc);
  4905. + }
  4906. +
  4907. + return err;
  4908. +}
  4909. +
  4910. +struct au_cpup_simple_args {
  4911. + int *errp;
  4912. + struct au_cp_generic *cpg;
  4913. +};
  4914. +
  4915. +static void au_call_cpup_simple(void *args)
  4916. +{
  4917. + struct au_cpup_simple_args *a = args;
  4918. +
  4919. + au_pin_hdir_acquire_nest(a->cpg->pin);
  4920. + *a->errp = au_cpup_simple(a->cpg);
  4921. + au_pin_hdir_release(a->cpg->pin);
  4922. +}
  4923. +
  4924. +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
  4925. +{
  4926. + int err, wkq_err;
  4927. + struct dentry *dentry, *parent;
  4928. + struct file *h_file;
  4929. + struct inode *h_dir;
  4930. + struct user_namespace *h_userns;
  4931. +
  4932. + dentry = cpg->dentry;
  4933. + h_file = NULL;
  4934. + if (au_ftest_cpup(cpg->flags, HOPEN)) {
  4935. + AuDebugOn(cpg->bsrc < 0);
  4936. + h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
  4937. + err = PTR_ERR(h_file);
  4938. + if (IS_ERR(h_file))
  4939. + goto out;
  4940. + }
  4941. +
  4942. + parent = dget_parent(dentry);
  4943. + h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
  4944. + h_userns = au_sbr_userns(dentry->d_sb, cpg->bdst);
  4945. + if (!au_test_h_perm_sio(h_userns, h_dir, MAY_EXEC | MAY_WRITE)
  4946. + && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
  4947. + err = au_cpup_simple(cpg);
  4948. + else {
  4949. + struct au_cpup_simple_args args = {
  4950. + .errp = &err,
  4951. + .cpg = cpg
  4952. + };
  4953. + wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
  4954. + if (unlikely(wkq_err))
  4955. + err = wkq_err;
  4956. + }
  4957. +
  4958. + dput(parent);
  4959. + if (h_file)
  4960. + au_h_open_post(dentry, cpg->bsrc, h_file);
  4961. +
  4962. +out:
  4963. + return err;
  4964. +}
  4965. +
  4966. +int au_sio_cpup_simple(struct au_cp_generic *cpg)
  4967. +{
  4968. + aufs_bindex_t bsrc, bbot;
  4969. + struct dentry *dentry, *h_dentry;
  4970. +
  4971. + if (cpg->bsrc < 0) {
  4972. + dentry = cpg->dentry;
  4973. + bbot = au_dbbot(dentry);
  4974. + for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
  4975. + h_dentry = au_h_dptr(dentry, bsrc);
  4976. + if (h_dentry) {
  4977. + AuDebugOn(d_is_negative(h_dentry));
  4978. + break;
  4979. + }
  4980. + }
  4981. + AuDebugOn(bsrc > bbot);
  4982. + cpg->bsrc = bsrc;
  4983. + }
  4984. + AuDebugOn(cpg->bsrc <= cpg->bdst);
  4985. + return au_do_sio_cpup_simple(cpg);
  4986. +}
  4987. +
  4988. +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
  4989. +{
  4990. + AuDebugOn(cpg->bdst <= cpg->bsrc);
  4991. + return au_do_sio_cpup_simple(cpg);
  4992. +}
  4993. +
  4994. +/* ---------------------------------------------------------------------- */
  4995. +
  4996. +/*
  4997. + * copyup the deleted file for writing.
  4998. + */
  4999. +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
  5000. + struct file *file)
  5001. +{
  5002. + int err;
  5003. + unsigned int flags_orig;
  5004. + aufs_bindex_t bsrc_orig;
  5005. + struct au_dinfo *dinfo;
  5006. + struct {
  5007. + struct au_hdentry *hd;
  5008. + struct dentry *h_dentry;
  5009. + } hdst, hsrc;
  5010. +
  5011. + dinfo = au_di(cpg->dentry);
  5012. + AuRwMustWriteLock(&dinfo->di_rwsem);
  5013. +
  5014. + bsrc_orig = cpg->bsrc;
  5015. + cpg->bsrc = dinfo->di_btop;
  5016. + hdst.hd = au_hdentry(dinfo, cpg->bdst);
  5017. + hdst.h_dentry = hdst.hd->hd_dentry;
  5018. + hdst.hd->hd_dentry = wh_dentry;
  5019. + dinfo->di_btop = cpg->bdst;
  5020. +
  5021. + hsrc.h_dentry = NULL;
  5022. + if (file) {
  5023. + hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
  5024. + hsrc.h_dentry = hsrc.hd->hd_dentry;
  5025. + hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
  5026. + }
  5027. + flags_orig = cpg->flags;
  5028. + cpg->flags = !AuCpup_DTIME;
  5029. + err = au_cpup_single(cpg, /*h_parent*/NULL);
  5030. + cpg->flags = flags_orig;
  5031. + if (file) {
  5032. + if (!err)
  5033. + err = au_reopen_nondir(file);
  5034. + hsrc.hd->hd_dentry = hsrc.h_dentry;
  5035. + }
  5036. + hdst.hd->hd_dentry = hdst.h_dentry;
  5037. + dinfo->di_btop = cpg->bsrc;
  5038. + cpg->bsrc = bsrc_orig;
  5039. +
  5040. + return err;
  5041. +}
  5042. +
  5043. +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
  5044. +{
  5045. + int err;
  5046. + aufs_bindex_t bdst;
  5047. + struct au_dtime dt;
  5048. + struct dentry *dentry, *parent, *h_parent, *wh_dentry;
  5049. + struct au_branch *br;
  5050. + struct path h_path;
  5051. +
  5052. + dentry = cpg->dentry;
  5053. + bdst = cpg->bdst;
  5054. + br = au_sbr(dentry->d_sb, bdst);
  5055. + parent = dget_parent(dentry);
  5056. + h_parent = au_h_dptr(parent, bdst);
  5057. + wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
  5058. + err = PTR_ERR(wh_dentry);
  5059. + if (IS_ERR(wh_dentry))
  5060. + goto out;
  5061. +
  5062. + h_path.dentry = h_parent;
  5063. + h_path.mnt = au_br_mnt(br);
  5064. + au_dtime_store(&dt, parent, &h_path);
  5065. + err = au_do_cpup_wh(cpg, wh_dentry, file);
  5066. + if (unlikely(err))
  5067. + goto out_wh;
  5068. +
  5069. + dget(wh_dentry);
  5070. + h_path.dentry = wh_dentry;
  5071. + if (!d_is_dir(wh_dentry)) {
  5072. + /* no delegation since it is just created */
  5073. + err = vfsub_unlink(d_inode(h_parent), &h_path,
  5074. + /*delegated*/NULL, /*force*/0);
  5075. + } else
  5076. + err = vfsub_rmdir(d_inode(h_parent), &h_path);
  5077. + if (unlikely(err)) {
  5078. + AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
  5079. + wh_dentry, err);
  5080. + err = -EIO;
  5081. + }
  5082. + au_dtime_revert(&dt);
  5083. + au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
  5084. +
  5085. +out_wh:
  5086. + dput(wh_dentry);
  5087. +out:
  5088. + dput(parent);
  5089. + return err;
  5090. +}
  5091. +
  5092. +struct au_cpup_wh_args {
  5093. + int *errp;
  5094. + struct au_cp_generic *cpg;
  5095. + struct file *file;
  5096. +};
  5097. +
  5098. +static void au_call_cpup_wh(void *args)
  5099. +{
  5100. + struct au_cpup_wh_args *a = args;
  5101. +
  5102. + au_pin_hdir_acquire_nest(a->cpg->pin);
  5103. + *a->errp = au_cpup_wh(a->cpg, a->file);
  5104. + au_pin_hdir_release(a->cpg->pin);
  5105. +}
  5106. +
  5107. +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
  5108. +{
  5109. + int err, wkq_err;
  5110. + aufs_bindex_t bdst;
  5111. + struct dentry *dentry, *parent, *h_orph, *h_parent;
  5112. + struct inode *dir, *h_dir, *h_tmpdir;
  5113. + struct au_wbr *wbr;
  5114. + struct au_pin wh_pin, *pin_orig;
  5115. + struct user_namespace *h_userns;
  5116. +
  5117. + dentry = cpg->dentry;
  5118. + bdst = cpg->bdst;
  5119. + parent = dget_parent(dentry);
  5120. + dir = d_inode(parent);
  5121. + h_orph = NULL;
  5122. + h_parent = NULL;
  5123. + h_dir = au_igrab(au_h_iptr(dir, bdst));
  5124. + h_tmpdir = h_dir;
  5125. + pin_orig = NULL;
  5126. + if (!h_dir->i_nlink) {
  5127. + wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
  5128. + h_orph = wbr->wbr_orph;
  5129. +
  5130. + h_parent = dget(au_h_dptr(parent, bdst));
  5131. + au_set_h_dptr(parent, bdst, dget(h_orph));
  5132. + h_tmpdir = d_inode(h_orph);
  5133. + au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
  5134. +
  5135. + inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
  5136. + /* todo: au_h_open_pre()? */
  5137. +
  5138. + pin_orig = cpg->pin;
  5139. + au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
  5140. + AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
  5141. + cpg->pin = &wh_pin;
  5142. + }
  5143. +
  5144. + h_userns = au_sbr_userns(dentry->d_sb, bdst);
  5145. + if (!au_test_h_perm_sio(h_userns, h_tmpdir, MAY_EXEC | MAY_WRITE)
  5146. + && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
  5147. + err = au_cpup_wh(cpg, file);
  5148. + else {
  5149. + struct au_cpup_wh_args args = {
  5150. + .errp = &err,
  5151. + .cpg = cpg,
  5152. + .file = file
  5153. + };
  5154. + wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
  5155. + if (unlikely(wkq_err))
  5156. + err = wkq_err;
  5157. + }
  5158. +
  5159. + if (h_orph) {
  5160. + inode_unlock(h_tmpdir);
  5161. + /* todo: au_h_open_post()? */
  5162. + au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
  5163. + au_set_h_dptr(parent, bdst, h_parent);
  5164. + AuDebugOn(!pin_orig);
  5165. + cpg->pin = pin_orig;
  5166. + }
  5167. + iput(h_dir);
  5168. + dput(parent);
  5169. +
  5170. + return err;
  5171. +}
  5172. +
  5173. +/* ---------------------------------------------------------------------- */
  5174. +
  5175. +/*
  5176. + * generic routine for both of copy-up and copy-down.
  5177. + */
  5178. +/* cf. revalidate function in file.c */
  5179. +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
  5180. + int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
  5181. + struct au_pin *pin,
  5182. + struct dentry *h_parent, void *arg),
  5183. + void *arg)
  5184. +{
  5185. + int err;
  5186. + struct au_pin pin;
  5187. + struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
  5188. +
  5189. + err = 0;
  5190. + parent = dget_parent(dentry);
  5191. + if (IS_ROOT(parent))
  5192. + goto out;
  5193. +
  5194. + au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
  5195. + au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
  5196. +
  5197. + /* do not use au_dpage */
  5198. + real_parent = parent;
  5199. + while (1) {
  5200. + dput(parent);
  5201. + parent = dget_parent(dentry);
  5202. + h_parent = au_h_dptr(parent, bdst);
  5203. + if (h_parent)
  5204. + goto out; /* success */
  5205. +
  5206. + /* find top dir which is necessary to cpup */
  5207. + do {
  5208. + d = parent;
  5209. + dput(parent);
  5210. + parent = dget_parent(d);
  5211. + di_read_lock_parent3(parent, !AuLock_IR);
  5212. + h_parent = au_h_dptr(parent, bdst);
  5213. + di_read_unlock(parent, !AuLock_IR);
  5214. + } while (!h_parent);
  5215. +
  5216. + if (d != real_parent)
  5217. + di_write_lock_child3(d);
  5218. +
  5219. + /* somebody else might create while we were sleeping */
  5220. + h_dentry = au_h_dptr(d, bdst);
  5221. + if (!h_dentry || d_is_negative(h_dentry)) {
  5222. + if (h_dentry)
  5223. + au_update_dbtop(d);
  5224. +
  5225. + au_pin_set_dentry(&pin, d);
  5226. + err = au_do_pin(&pin);
  5227. + if (!err) {
  5228. + err = cp(d, bdst, &pin, h_parent, arg);
  5229. + au_unpin(&pin);
  5230. + }
  5231. + }
  5232. +
  5233. + if (d != real_parent)
  5234. + di_write_unlock(d);
  5235. + if (unlikely(err))
  5236. + break;
  5237. + }
  5238. +
  5239. +out:
  5240. + dput(parent);
  5241. + return err;
  5242. +}
  5243. +
  5244. +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
  5245. + struct au_pin *pin,
  5246. + struct dentry *h_parent __maybe_unused,
  5247. + void *arg __maybe_unused)
  5248. +{
  5249. + struct au_cp_generic cpg = {
  5250. + .dentry = dentry,
  5251. + .bdst = bdst,
  5252. + .bsrc = -1,
  5253. + .len = 0,
  5254. + .pin = pin,
  5255. + .flags = AuCpup_DTIME
  5256. + };
  5257. + return au_sio_cpup_simple(&cpg);
  5258. +}
  5259. +
  5260. +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
  5261. +{
  5262. + return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
  5263. +}
  5264. +
  5265. +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
  5266. +{
  5267. + int err;
  5268. + struct dentry *parent;
  5269. + struct inode *dir;
  5270. +
  5271. + parent = dget_parent(dentry);
  5272. + dir = d_inode(parent);
  5273. + err = 0;
  5274. + if (au_h_iptr(dir, bdst))
  5275. + goto out;
  5276. +
  5277. + di_read_unlock(parent, AuLock_IR);
  5278. + di_write_lock_parent(parent);
  5279. + /* someone else might change our inode while we were sleeping */
  5280. + if (!au_h_iptr(dir, bdst))
  5281. + err = au_cpup_dirs(dentry, bdst);
  5282. + di_downgrade_lock(parent, AuLock_IR);
  5283. +
  5284. +out:
  5285. + dput(parent);
  5286. + return err;
  5287. +}
  5288. diff -Naur null/fs/aufs/cpup.h linux-5.15.131/fs/aufs/cpup.h
  5289. --- /dev/null
  5290. +++ linux-5.15.131/fs/aufs/cpup.h 2023-09-11 14:32:48.145722589 +0300
  5291. @@ -0,0 +1,100 @@
  5292. +/* SPDX-License-Identifier: GPL-2.0 */
  5293. +/*
  5294. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  5295. + *
  5296. + * This program, aufs is free software; you can redistribute it and/or modify
  5297. + * it under the terms of the GNU General Public License as published by
  5298. + * the Free Software Foundation; either version 2 of the License, or
  5299. + * (at your option) any later version.
  5300. + *
  5301. + * This program is distributed in the hope that it will be useful,
  5302. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5303. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5304. + * GNU General Public License for more details.
  5305. + *
  5306. + * You should have received a copy of the GNU General Public License
  5307. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5308. + */
  5309. +
  5310. +/*
  5311. + * copy-up/down functions
  5312. + */
  5313. +
  5314. +#ifndef __AUFS_CPUP_H__
  5315. +#define __AUFS_CPUP_H__
  5316. +
  5317. +#ifdef __KERNEL__
  5318. +
  5319. +#include <linux/path.h>
  5320. +
  5321. +struct inode;
  5322. +struct file;
  5323. +struct au_pin;
  5324. +
  5325. +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
  5326. +void au_cpup_attr_timesizes(struct inode *inode);
  5327. +void au_cpup_attr_nlink(struct inode *inode, int force);
  5328. +void au_cpup_attr_changeable(struct inode *inode);
  5329. +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
  5330. +void au_cpup_attr_all(struct inode *inode, int force);
  5331. +
  5332. +/* ---------------------------------------------------------------------- */
  5333. +
  5334. +struct au_cp_generic {
  5335. + struct dentry *dentry;
  5336. + aufs_bindex_t bdst, bsrc;
  5337. + loff_t len;
  5338. + struct au_pin *pin;
  5339. + unsigned int flags;
  5340. +};
  5341. +
  5342. +/* cpup flags */
  5343. +#define AuCpup_DTIME 1 /* do dtime_store/revert */
  5344. +#define AuCpup_KEEPLINO (1 << 1) /* do not clear the lower xino,
  5345. + for link(2) */
  5346. +#define AuCpup_RENAME (1 << 2) /* rename after cpup */
  5347. +#define AuCpup_HOPEN (1 << 3) /* call h_open_pre/post() in
  5348. + cpup */
  5349. +#define AuCpup_OVERWRITE (1 << 4) /* allow overwriting the
  5350. + existing entry */
  5351. +#define AuCpup_RWDST (1 << 5) /* force write target even if
  5352. + the branch is marked as RO */
  5353. +
  5354. +#ifndef CONFIG_AUFS_BR_HFSPLUS
  5355. +#undef AuCpup_HOPEN
  5356. +#define AuCpup_HOPEN 0
  5357. +#endif
  5358. +
  5359. +#define au_ftest_cpup(flags, name) ((flags) & AuCpup_##name)
  5360. +#define au_fset_cpup(flags, name) \
  5361. + do { (flags) |= AuCpup_##name; } while (0)
  5362. +#define au_fclr_cpup(flags, name) \
  5363. + do { (flags) &= ~AuCpup_##name; } while (0)
  5364. +
  5365. +int au_copy_file(struct file *dst, struct file *src, loff_t len);
  5366. +int au_sio_cpup_simple(struct au_cp_generic *cpg);
  5367. +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
  5368. +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
  5369. +
  5370. +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
  5371. + int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
  5372. + struct au_pin *pin,
  5373. + struct dentry *h_parent, void *arg),
  5374. + void *arg);
  5375. +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
  5376. +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
  5377. +
  5378. +/* ---------------------------------------------------------------------- */
  5379. +
  5380. +/* keep timestamps when copyup */
  5381. +struct au_dtime {
  5382. + struct dentry *dt_dentry;
  5383. + struct path dt_h_path;
  5384. + struct timespec64 dt_atime, dt_mtime;
  5385. +};
  5386. +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
  5387. + struct path *h_path);
  5388. +void au_dtime_revert(struct au_dtime *dt);
  5389. +
  5390. +#endif /* __KERNEL__ */
  5391. +#endif /* __AUFS_CPUP_H__ */
  5392. diff -Naur null/fs/aufs/dbgaufs.c linux-5.15.131/fs/aufs/dbgaufs.c
  5393. --- /dev/null
  5394. +++ linux-5.15.131/fs/aufs/dbgaufs.c 2023-09-11 14:32:48.145722589 +0300
  5395. @@ -0,0 +1,526 @@
  5396. +// SPDX-License-Identifier: GPL-2.0
  5397. +/*
  5398. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  5399. + *
  5400. + * This program, aufs is free software; you can redistribute it and/or modify
  5401. + * it under the terms of the GNU General Public License as published by
  5402. + * the Free Software Foundation; either version 2 of the License, or
  5403. + * (at your option) any later version.
  5404. + *
  5405. + * This program is distributed in the hope that it will be useful,
  5406. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5407. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5408. + * GNU General Public License for more details.
  5409. + *
  5410. + * You should have received a copy of the GNU General Public License
  5411. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5412. + */
  5413. +
  5414. +/*
  5415. + * debugfs interface
  5416. + */
  5417. +
  5418. +#include <linux/debugfs.h>
  5419. +#include "aufs.h"
  5420. +
  5421. +#ifndef CONFIG_SYSFS
  5422. +#error DEBUG_FS depends upon SYSFS
  5423. +#endif
  5424. +
  5425. +static struct dentry *dbgaufs;
  5426. +static const mode_t dbgaufs_mode = 0444;
  5427. +
  5428. +/* 20 is max digits length of ulong 64 */
  5429. +struct dbgaufs_arg {
  5430. + int n;
  5431. + char a[20 * 4];
  5432. +};
  5433. +
  5434. +/*
  5435. + * common function for all XINO files
  5436. + */
  5437. +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
  5438. + struct file *file)
  5439. +{
  5440. + void *p;
  5441. +
  5442. + p = file->private_data;
  5443. + if (p) {
  5444. + /* this is struct dbgaufs_arg */
  5445. + AuDebugOn(!au_kfree_sz_test(p));
  5446. + au_kfree_do_rcu(p);
  5447. + }
  5448. + return 0;
  5449. +}
  5450. +
  5451. +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
  5452. + int cnt)
  5453. +{
  5454. + int err;
  5455. + struct kstat st;
  5456. + struct dbgaufs_arg *p;
  5457. +
  5458. + err = -ENOMEM;
  5459. + p = kmalloc(sizeof(*p), GFP_NOFS);
  5460. + if (unlikely(!p))
  5461. + goto out;
  5462. +
  5463. + err = 0;
  5464. + p->n = 0;
  5465. + file->private_data = p;
  5466. + if (!xf)
  5467. + goto out;
  5468. +
  5469. + err = vfsub_getattr(&xf->f_path, &st);
  5470. + if (!err) {
  5471. + if (do_fcnt)
  5472. + p->n = snprintf
  5473. + (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
  5474. + cnt, st.blocks, st.blksize,
  5475. + (long long)st.size);
  5476. + else
  5477. + p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
  5478. + st.blocks, st.blksize,
  5479. + (long long)st.size);
  5480. + AuDebugOn(p->n >= sizeof(p->a));
  5481. + } else {
  5482. + p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
  5483. + err = 0;
  5484. + }
  5485. +
  5486. +out:
  5487. + return err;
  5488. +}
  5489. +
  5490. +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
  5491. + size_t count, loff_t *ppos)
  5492. +{
  5493. + struct dbgaufs_arg *p;
  5494. +
  5495. + p = file->private_data;
  5496. + return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
  5497. +}
  5498. +
  5499. +/* ---------------------------------------------------------------------- */
  5500. +
  5501. +struct dbgaufs_plink_arg {
  5502. + int n;
  5503. + char a[];
  5504. +};
  5505. +
  5506. +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
  5507. + struct file *file)
  5508. +{
  5509. + free_page((unsigned long)file->private_data);
  5510. + return 0;
  5511. +}
  5512. +
  5513. +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
  5514. +{
  5515. + int err, i, limit;
  5516. + unsigned long n, sum;
  5517. + struct dbgaufs_plink_arg *p;
  5518. + struct au_sbinfo *sbinfo;
  5519. + struct super_block *sb;
  5520. + struct hlist_bl_head *hbl;
  5521. +
  5522. + err = -ENOMEM;
  5523. + p = (void *)get_zeroed_page(GFP_NOFS);
  5524. + if (unlikely(!p))
  5525. + goto out;
  5526. +
  5527. + err = -EFBIG;
  5528. + sbinfo = inode->i_private;
  5529. + sb = sbinfo->si_sb;
  5530. + si_noflush_read_lock(sb);
  5531. + if (au_opt_test(au_mntflags(sb), PLINK)) {
  5532. + limit = PAGE_SIZE - sizeof(p->n);
  5533. +
  5534. + /* the number of buckets */
  5535. + n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
  5536. + p->n += n;
  5537. + limit -= n;
  5538. +
  5539. + sum = 0;
  5540. + for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
  5541. + i++, hbl++) {
  5542. + n = au_hbl_count(hbl);
  5543. + sum += n;
  5544. +
  5545. + n = snprintf(p->a + p->n, limit, "%lu ", n);
  5546. + p->n += n;
  5547. + limit -= n;
  5548. + if (unlikely(limit <= 0))
  5549. + goto out_free;
  5550. + }
  5551. + p->a[p->n - 1] = '\n';
  5552. +
  5553. + /* the sum of plinks */
  5554. + n = snprintf(p->a + p->n, limit, "%lu\n", sum);
  5555. + p->n += n;
  5556. + limit -= n;
  5557. + if (unlikely(limit <= 0))
  5558. + goto out_free;
  5559. + } else {
  5560. +#define str "1\n0\n0\n"
  5561. + p->n = sizeof(str) - 1;
  5562. + strcpy(p->a, str);
  5563. +#undef str
  5564. + }
  5565. + si_read_unlock(sb);
  5566. +
  5567. + err = 0;
  5568. + file->private_data = p;
  5569. + goto out; /* success */
  5570. +
  5571. +out_free:
  5572. + free_page((unsigned long)p);
  5573. +out:
  5574. + return err;
  5575. +}
  5576. +
  5577. +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
  5578. + size_t count, loff_t *ppos)
  5579. +{
  5580. + struct dbgaufs_plink_arg *p;
  5581. +
  5582. + p = file->private_data;
  5583. + return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
  5584. +}
  5585. +
  5586. +static const struct file_operations dbgaufs_plink_fop = {
  5587. + .owner = THIS_MODULE,
  5588. + .open = dbgaufs_plink_open,
  5589. + .release = dbgaufs_plink_release,
  5590. + .read = dbgaufs_plink_read
  5591. +};
  5592. +
  5593. +/* ---------------------------------------------------------------------- */
  5594. +
  5595. +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
  5596. +{
  5597. + int err;
  5598. + struct au_sbinfo *sbinfo;
  5599. + struct super_block *sb;
  5600. +
  5601. + sbinfo = inode->i_private;
  5602. + sb = sbinfo->si_sb;
  5603. + si_noflush_read_lock(sb);
  5604. + err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
  5605. + si_read_unlock(sb);
  5606. + return err;
  5607. +}
  5608. +
  5609. +static const struct file_operations dbgaufs_xib_fop = {
  5610. + .owner = THIS_MODULE,
  5611. + .open = dbgaufs_xib_open,
  5612. + .release = dbgaufs_xi_release,
  5613. + .read = dbgaufs_xi_read
  5614. +};
  5615. +
  5616. +/* ---------------------------------------------------------------------- */
  5617. +
  5618. +#define DbgaufsXi_PREFIX "xi"
  5619. +
  5620. +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
  5621. +{
  5622. + int err, idx;
  5623. + long l;
  5624. + aufs_bindex_t bindex;
  5625. + char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
  5626. + struct au_sbinfo *sbinfo;
  5627. + struct super_block *sb;
  5628. + struct au_xino *xi;
  5629. + struct file *xf;
  5630. + struct qstr *name;
  5631. + struct au_branch *br;
  5632. +
  5633. + err = -ENOENT;
  5634. + name = &file->f_path.dentry->d_name;
  5635. + if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
  5636. + || memcmp(name->name, DbgaufsXi_PREFIX,
  5637. + sizeof(DbgaufsXi_PREFIX) - 1)))
  5638. + goto out;
  5639. +
  5640. + AuDebugOn(name->len >= sizeof(a));
  5641. + memcpy(a, name->name, name->len);
  5642. + a[name->len] = '\0';
  5643. + p = strchr(a, '-');
  5644. + if (p)
  5645. + *p = '\0';
  5646. + err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
  5647. + if (unlikely(err))
  5648. + goto out;
  5649. + bindex = l;
  5650. + idx = 0;
  5651. + if (p) {
  5652. + err = kstrtol(p + 1, 10, &l);
  5653. + if (unlikely(err))
  5654. + goto out;
  5655. + idx = l;
  5656. + }
  5657. +
  5658. + err = -ENOENT;
  5659. + sbinfo = inode->i_private;
  5660. + sb = sbinfo->si_sb;
  5661. + si_noflush_read_lock(sb);
  5662. + if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
  5663. + goto out_si;
  5664. + br = au_sbr(sb, bindex);
  5665. + xi = br->br_xino;
  5666. + if (unlikely(idx >= xi->xi_nfile))
  5667. + goto out_si;
  5668. + xf = au_xino_file(xi, idx);
  5669. + if (xf)
  5670. + err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
  5671. + au_xino_count(br));
  5672. +
  5673. +out_si:
  5674. + si_read_unlock(sb);
  5675. +out:
  5676. + AuTraceErr(err);
  5677. + return err;
  5678. +}
  5679. +
  5680. +static const struct file_operations dbgaufs_xino_fop = {
  5681. + .owner = THIS_MODULE,
  5682. + .open = dbgaufs_xino_open,
  5683. + .release = dbgaufs_xi_release,
  5684. + .read = dbgaufs_xi_read
  5685. +};
  5686. +
  5687. +void dbgaufs_xino_del(struct au_branch *br)
  5688. +{
  5689. + struct dentry *dbgaufs;
  5690. +
  5691. + dbgaufs = br->br_dbgaufs;
  5692. + if (!dbgaufs)
  5693. + return;
  5694. +
  5695. + br->br_dbgaufs = NULL;
  5696. + /* debugfs acquires the parent i_mutex */
  5697. + lockdep_off();
  5698. + debugfs_remove(dbgaufs);
  5699. + lockdep_on();
  5700. +}
  5701. +
  5702. +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
  5703. +{
  5704. + aufs_bindex_t bbot;
  5705. + struct au_branch *br;
  5706. +
  5707. + if (!au_sbi(sb)->si_dbgaufs)
  5708. + return;
  5709. +
  5710. + bbot = au_sbbot(sb);
  5711. + for (; bindex <= bbot; bindex++) {
  5712. + br = au_sbr(sb, bindex);
  5713. + dbgaufs_xino_del(br);
  5714. + }
  5715. +}
  5716. +
  5717. +static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
  5718. + unsigned int idx, struct dentry *parent,
  5719. + struct au_sbinfo *sbinfo)
  5720. +{
  5721. + struct au_branch *br;
  5722. + struct dentry *d;
  5723. + /* "xi" bindex(5) "-" idx(2) NULL */
  5724. + char name[sizeof(DbgaufsXi_PREFIX) + 8];
  5725. +
  5726. + if (!idx)
  5727. + snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
  5728. + else
  5729. + snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
  5730. + bindex, idx);
  5731. + br = au_sbr(sb, bindex);
  5732. + if (br->br_dbgaufs) {
  5733. + struct qstr qstr = QSTR_INIT(name, strlen(name));
  5734. +
  5735. + if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
  5736. + /* debugfs acquires the parent i_mutex */
  5737. + lockdep_off();
  5738. + d = debugfs_rename(parent, br->br_dbgaufs, parent,
  5739. + name);
  5740. + lockdep_on();
  5741. + if (unlikely(!d))
  5742. + pr_warn("failed renaming %pd/%s, ignored.\n",
  5743. + parent, name);
  5744. + }
  5745. + } else {
  5746. + lockdep_off();
  5747. + br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
  5748. + sbinfo, &dbgaufs_xino_fop);
  5749. + lockdep_on();
  5750. + if (unlikely(!br->br_dbgaufs))
  5751. + pr_warn("failed creating %pd/%s, ignored.\n",
  5752. + parent, name);
  5753. + }
  5754. +}
  5755. +
  5756. +static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
  5757. + struct dentry *parent, struct au_sbinfo *sbinfo)
  5758. +{
  5759. + struct au_branch *br;
  5760. + struct au_xino *xi;
  5761. + unsigned int u;
  5762. +
  5763. + br = au_sbr(sb, bindex);
  5764. + xi = br->br_xino;
  5765. + for (u = 0; u < xi->xi_nfile; u++)
  5766. + dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
  5767. +}
  5768. +
  5769. +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
  5770. +{
  5771. + struct au_sbinfo *sbinfo;
  5772. + struct dentry *parent;
  5773. + aufs_bindex_t bbot;
  5774. +
  5775. + if (!au_opt_test(au_mntflags(sb), XINO))
  5776. + return;
  5777. +
  5778. + sbinfo = au_sbi(sb);
  5779. + parent = sbinfo->si_dbgaufs;
  5780. + if (!parent)
  5781. + return;
  5782. +
  5783. + bbot = au_sbbot(sb);
  5784. + if (topdown)
  5785. + for (; bindex <= bbot; bindex++)
  5786. + dbgaufs_br_add(sb, bindex, parent, sbinfo);
  5787. + else
  5788. + for (; bbot >= bindex; bbot--)
  5789. + dbgaufs_br_add(sb, bbot, parent, sbinfo);
  5790. +}
  5791. +
  5792. +/* ---------------------------------------------------------------------- */
  5793. +
  5794. +#ifdef CONFIG_AUFS_EXPORT
  5795. +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
  5796. +{
  5797. + int err;
  5798. + struct au_sbinfo *sbinfo;
  5799. + struct super_block *sb;
  5800. +
  5801. + sbinfo = inode->i_private;
  5802. + sb = sbinfo->si_sb;
  5803. + si_noflush_read_lock(sb);
  5804. + err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
  5805. + si_read_unlock(sb);
  5806. + return err;
  5807. +}
  5808. +
  5809. +static const struct file_operations dbgaufs_xigen_fop = {
  5810. + .owner = THIS_MODULE,
  5811. + .open = dbgaufs_xigen_open,
  5812. + .release = dbgaufs_xi_release,
  5813. + .read = dbgaufs_xi_read
  5814. +};
  5815. +
  5816. +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
  5817. +{
  5818. + int err;
  5819. +
  5820. + /*
  5821. + * This function is a dynamic '__init' function actually,
  5822. + * so the tiny check for si_rwsem is unnecessary.
  5823. + */
  5824. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  5825. +
  5826. + err = -EIO;
  5827. + sbinfo->si_dbgaufs_xigen = debugfs_create_file
  5828. + ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
  5829. + &dbgaufs_xigen_fop);
  5830. + if (sbinfo->si_dbgaufs_xigen)
  5831. + err = 0;
  5832. +
  5833. + return err;
  5834. +}
  5835. +#else
  5836. +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
  5837. +{
  5838. + return 0;
  5839. +}
  5840. +#endif /* CONFIG_AUFS_EXPORT */
  5841. +
  5842. +/* ---------------------------------------------------------------------- */
  5843. +
  5844. +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
  5845. +{
  5846. + /*
  5847. + * This function is a dynamic '__fin' function actually,
  5848. + * so the tiny check for si_rwsem is unnecessary.
  5849. + */
  5850. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  5851. +
  5852. + debugfs_remove_recursive(sbinfo->si_dbgaufs);
  5853. + sbinfo->si_dbgaufs = NULL;
  5854. +}
  5855. +
  5856. +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
  5857. +{
  5858. + int err;
  5859. + char name[SysaufsSiNameLen];
  5860. +
  5861. + /*
  5862. + * This function is a dynamic '__init' function actually,
  5863. + * so the tiny check for si_rwsem is unnecessary.
  5864. + */
  5865. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  5866. +
  5867. + err = -ENOENT;
  5868. + if (!dbgaufs) {
  5869. + AuErr1("/debug/aufs is uninitialized\n");
  5870. + goto out;
  5871. + }
  5872. +
  5873. + err = -EIO;
  5874. + sysaufs_name(sbinfo, name);
  5875. + sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
  5876. + if (unlikely(!sbinfo->si_dbgaufs))
  5877. + goto out;
  5878. +
  5879. + /* regardless plink/noplink option */
  5880. + sbinfo->si_dbgaufs_plink = debugfs_create_file
  5881. + ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
  5882. + &dbgaufs_plink_fop);
  5883. + if (unlikely(!sbinfo->si_dbgaufs_plink))
  5884. + goto out_dir;
  5885. +
  5886. + /* regardless xino/noxino option */
  5887. + sbinfo->si_dbgaufs_xib = debugfs_create_file
  5888. + ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
  5889. + &dbgaufs_xib_fop);
  5890. + if (unlikely(!sbinfo->si_dbgaufs_xib))
  5891. + goto out_dir;
  5892. +
  5893. + err = dbgaufs_xigen_init(sbinfo);
  5894. + if (!err)
  5895. + goto out; /* success */
  5896. +
  5897. +out_dir:
  5898. + dbgaufs_si_fin(sbinfo);
  5899. +out:
  5900. + if (unlikely(err))
  5901. + pr_err("debugfs/aufs failed\n");
  5902. + return err;
  5903. +}
  5904. +
  5905. +/* ---------------------------------------------------------------------- */
  5906. +
  5907. +void dbgaufs_fin(void)
  5908. +{
  5909. + debugfs_remove(dbgaufs);
  5910. +}
  5911. +
  5912. +int __init dbgaufs_init(void)
  5913. +{
  5914. + int err;
  5915. +
  5916. + err = -EIO;
  5917. + dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
  5918. + if (dbgaufs)
  5919. + err = 0;
  5920. + return err;
  5921. +}
  5922. diff -Naur null/fs/aufs/dbgaufs.h linux-5.15.131/fs/aufs/dbgaufs.h
  5923. --- /dev/null
  5924. +++ linux-5.15.131/fs/aufs/dbgaufs.h 2023-09-11 14:32:48.146722577 +0300
  5925. @@ -0,0 +1,53 @@
  5926. +/* SPDX-License-Identifier: GPL-2.0 */
  5927. +/*
  5928. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  5929. + *
  5930. + * This program, aufs is free software; you can redistribute it and/or modify
  5931. + * it under the terms of the GNU General Public License as published by
  5932. + * the Free Software Foundation; either version 2 of the License, or
  5933. + * (at your option) any later version.
  5934. + *
  5935. + * This program is distributed in the hope that it will be useful,
  5936. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5937. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5938. + * GNU General Public License for more details.
  5939. + *
  5940. + * You should have received a copy of the GNU General Public License
  5941. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5942. + */
  5943. +
  5944. +/*
  5945. + * debugfs interface
  5946. + */
  5947. +
  5948. +#ifndef __DBGAUFS_H__
  5949. +#define __DBGAUFS_H__
  5950. +
  5951. +#ifdef __KERNEL__
  5952. +
  5953. +struct super_block;
  5954. +struct au_sbinfo;
  5955. +struct au_branch;
  5956. +
  5957. +#ifdef CONFIG_DEBUG_FS
  5958. +/* dbgaufs.c */
  5959. +void dbgaufs_xino_del(struct au_branch *br);
  5960. +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
  5961. +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
  5962. +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
  5963. +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
  5964. +void dbgaufs_fin(void);
  5965. +int __init dbgaufs_init(void);
  5966. +#else
  5967. +AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
  5968. +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
  5969. +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
  5970. + int topdown)
  5971. +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
  5972. +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
  5973. +AuStubVoid(dbgaufs_fin, void)
  5974. +AuStubInt0(__init dbgaufs_init, void)
  5975. +#endif /* CONFIG_DEBUG_FS */
  5976. +
  5977. +#endif /* __KERNEL__ */
  5978. +#endif /* __DBGAUFS_H__ */
  5979. diff -Naur null/fs/aufs/dcsub.c linux-5.15.131/fs/aufs/dcsub.c
  5980. --- /dev/null
  5981. +++ linux-5.15.131/fs/aufs/dcsub.c 2023-09-11 14:32:48.146722577 +0300
  5982. @@ -0,0 +1,225 @@
  5983. +// SPDX-License-Identifier: GPL-2.0
  5984. +/*
  5985. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  5986. + *
  5987. + * This program, aufs is free software; you can redistribute it and/or modify
  5988. + * it under the terms of the GNU General Public License as published by
  5989. + * the Free Software Foundation; either version 2 of the License, or
  5990. + * (at your option) any later version.
  5991. + *
  5992. + * This program is distributed in the hope that it will be useful,
  5993. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5994. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5995. + * GNU General Public License for more details.
  5996. + *
  5997. + * You should have received a copy of the GNU General Public License
  5998. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5999. + */
  6000. +
  6001. +/*
  6002. + * sub-routines for dentry cache
  6003. + */
  6004. +
  6005. +#include "aufs.h"
  6006. +
  6007. +static void au_dpage_free(struct au_dpage *dpage)
  6008. +{
  6009. + int i;
  6010. + struct dentry **p;
  6011. +
  6012. + p = dpage->dentries;
  6013. + for (i = 0; i < dpage->ndentry; i++)
  6014. + dput(*p++);
  6015. + free_page((unsigned long)dpage->dentries);
  6016. +}
  6017. +
  6018. +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
  6019. +{
  6020. + int err;
  6021. + void *p;
  6022. +
  6023. + err = -ENOMEM;
  6024. + dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
  6025. + if (unlikely(!dpages->dpages))
  6026. + goto out;
  6027. +
  6028. + p = (void *)__get_free_page(gfp);
  6029. + if (unlikely(!p))
  6030. + goto out_dpages;
  6031. +
  6032. + dpages->dpages[0].ndentry = 0;
  6033. + dpages->dpages[0].dentries = p;
  6034. + dpages->ndpage = 1;
  6035. + return 0; /* success */
  6036. +
  6037. +out_dpages:
  6038. + au_kfree_try_rcu(dpages->dpages);
  6039. +out:
  6040. + return err;
  6041. +}
  6042. +
  6043. +void au_dpages_free(struct au_dcsub_pages *dpages)
  6044. +{
  6045. + int i;
  6046. + struct au_dpage *p;
  6047. +
  6048. + p = dpages->dpages;
  6049. + for (i = 0; i < dpages->ndpage; i++)
  6050. + au_dpage_free(p++);
  6051. + au_kfree_try_rcu(dpages->dpages);
  6052. +}
  6053. +
  6054. +static int au_dpages_append(struct au_dcsub_pages *dpages,
  6055. + struct dentry *dentry, gfp_t gfp)
  6056. +{
  6057. + int err, sz;
  6058. + struct au_dpage *dpage;
  6059. + void *p;
  6060. +
  6061. + dpage = dpages->dpages + dpages->ndpage - 1;
  6062. + sz = PAGE_SIZE / sizeof(dentry);
  6063. + if (unlikely(dpage->ndentry >= sz)) {
  6064. + AuLabel(new dpage);
  6065. + err = -ENOMEM;
  6066. + sz = dpages->ndpage * sizeof(*dpages->dpages);
  6067. + p = au_kzrealloc(dpages->dpages, sz,
  6068. + sz + sizeof(*dpages->dpages), gfp,
  6069. + /*may_shrink*/0);
  6070. + if (unlikely(!p))
  6071. + goto out;
  6072. +
  6073. + dpages->dpages = p;
  6074. + dpage = dpages->dpages + dpages->ndpage;
  6075. + p = (void *)__get_free_page(gfp);
  6076. + if (unlikely(!p))
  6077. + goto out;
  6078. +
  6079. + dpage->ndentry = 0;
  6080. + dpage->dentries = p;
  6081. + dpages->ndpage++;
  6082. + }
  6083. +
  6084. + AuDebugOn(au_dcount(dentry) <= 0);
  6085. + dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
  6086. + return 0; /* success */
  6087. +
  6088. +out:
  6089. + return err;
  6090. +}
  6091. +
  6092. +/* todo: BAD approach */
  6093. +/* copied from linux/fs/dcache.c */
  6094. +enum d_walk_ret {
  6095. + D_WALK_CONTINUE,
  6096. + D_WALK_QUIT,
  6097. + D_WALK_NORETRY,
  6098. + D_WALK_SKIP,
  6099. +};
  6100. +
  6101. +extern void d_walk(struct dentry *parent, void *data,
  6102. + enum d_walk_ret (*enter)(void *, struct dentry *));
  6103. +
  6104. +struct ac_dpages_arg {
  6105. + int err;
  6106. + struct au_dcsub_pages *dpages;
  6107. + struct super_block *sb;
  6108. + au_dpages_test test;
  6109. + void *arg;
  6110. +};
  6111. +
  6112. +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
  6113. +{
  6114. + enum d_walk_ret ret;
  6115. + struct ac_dpages_arg *arg = _arg;
  6116. +
  6117. + ret = D_WALK_CONTINUE;
  6118. + if (dentry->d_sb == arg->sb
  6119. + && !IS_ROOT(dentry)
  6120. + && au_dcount(dentry) > 0
  6121. + && au_di(dentry)
  6122. + && (!arg->test || arg->test(dentry, arg->arg))) {
  6123. + arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
  6124. + if (unlikely(arg->err))
  6125. + ret = D_WALK_QUIT;
  6126. + }
  6127. +
  6128. + return ret;
  6129. +}
  6130. +
  6131. +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
  6132. + au_dpages_test test, void *arg)
  6133. +{
  6134. + struct ac_dpages_arg args = {
  6135. + .err = 0,
  6136. + .dpages = dpages,
  6137. + .sb = root->d_sb,
  6138. + .test = test,
  6139. + .arg = arg
  6140. + };
  6141. +
  6142. + d_walk(root, &args, au_call_dpages_append);
  6143. +
  6144. + return args.err;
  6145. +}
  6146. +
  6147. +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
  6148. + int do_include, au_dpages_test test, void *arg)
  6149. +{
  6150. + int err;
  6151. +
  6152. + err = 0;
  6153. + write_seqlock(&rename_lock);
  6154. + spin_lock(&dentry->d_lock);
  6155. + if (do_include
  6156. + && au_dcount(dentry) > 0
  6157. + && (!test || test(dentry, arg)))
  6158. + err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
  6159. + spin_unlock(&dentry->d_lock);
  6160. + if (unlikely(err))
  6161. + goto out;
  6162. +
  6163. + /*
  6164. + * RCU for vfsmount is unnecessary since this is a traverse in a single
  6165. + * mount
  6166. + */
  6167. + while (!IS_ROOT(dentry)) {
  6168. + dentry = dentry->d_parent; /* rename_lock is locked */
  6169. + spin_lock(&dentry->d_lock);
  6170. + if (au_dcount(dentry) > 0
  6171. + && (!test || test(dentry, arg)))
  6172. + err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
  6173. + spin_unlock(&dentry->d_lock);
  6174. + if (unlikely(err))
  6175. + break;
  6176. + }
  6177. +
  6178. +out:
  6179. + write_sequnlock(&rename_lock);
  6180. + return err;
  6181. +}
  6182. +
  6183. +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
  6184. +{
  6185. + return au_di(dentry) && dentry->d_sb == arg;
  6186. +}
  6187. +
  6188. +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
  6189. + struct dentry *dentry, int do_include)
  6190. +{
  6191. + return au_dcsub_pages_rev(dpages, dentry, do_include,
  6192. + au_dcsub_dpages_aufs, dentry->d_sb);
  6193. +}
  6194. +
  6195. +int au_test_subdir(struct dentry *d1, struct dentry *d2)
  6196. +{
  6197. + struct path path[2] = {
  6198. + {
  6199. + .dentry = d1
  6200. + },
  6201. + {
  6202. + .dentry = d2
  6203. + }
  6204. + };
  6205. +
  6206. + return path_is_under(path + 0, path + 1);
  6207. +}
  6208. diff -Naur null/fs/aufs/dcsub.h linux-5.15.131/fs/aufs/dcsub.h
  6209. --- /dev/null
  6210. +++ linux-5.15.131/fs/aufs/dcsub.h 2023-09-11 14:32:48.146722577 +0300
  6211. @@ -0,0 +1,137 @@
  6212. +/* SPDX-License-Identifier: GPL-2.0 */
  6213. +/*
  6214. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  6215. + *
  6216. + * This program, aufs is free software; you can redistribute it and/or modify
  6217. + * it under the terms of the GNU General Public License as published by
  6218. + * the Free Software Foundation; either version 2 of the License, or
  6219. + * (at your option) any later version.
  6220. + *
  6221. + * This program is distributed in the hope that it will be useful,
  6222. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6223. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6224. + * GNU General Public License for more details.
  6225. + *
  6226. + * You should have received a copy of the GNU General Public License
  6227. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  6228. + */
  6229. +
  6230. +/*
  6231. + * sub-routines for dentry cache
  6232. + */
  6233. +
  6234. +#ifndef __AUFS_DCSUB_H__
  6235. +#define __AUFS_DCSUB_H__
  6236. +
  6237. +#ifdef __KERNEL__
  6238. +
  6239. +#include <linux/dcache.h>
  6240. +#include <linux/fs.h>
  6241. +
  6242. +struct au_dpage {
  6243. + int ndentry;
  6244. + struct dentry **dentries;
  6245. +};
  6246. +
  6247. +struct au_dcsub_pages {
  6248. + int ndpage;
  6249. + struct au_dpage *dpages;
  6250. +};
  6251. +
  6252. +/* ---------------------------------------------------------------------- */
  6253. +
  6254. +/* dcsub.c */
  6255. +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
  6256. +void au_dpages_free(struct au_dcsub_pages *dpages);
  6257. +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
  6258. +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
  6259. + au_dpages_test test, void *arg);
  6260. +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
  6261. + int do_include, au_dpages_test test, void *arg);
  6262. +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
  6263. + struct dentry *dentry, int do_include);
  6264. +int au_test_subdir(struct dentry *d1, struct dentry *d2);
  6265. +
  6266. +/* ---------------------------------------------------------------------- */
  6267. +
  6268. +/*
  6269. + * todo: in linux-3.13, several similar (but faster) helpers are added to
  6270. + * include/linux/dcache.h. Try them (in the future).
  6271. + */
  6272. +
  6273. +static inline int au_d_hashed_positive(struct dentry *d)
  6274. +{
  6275. + int err;
  6276. + struct inode *inode = d_inode(d);
  6277. +
  6278. + err = 0;
  6279. + if (unlikely(d_unhashed(d)
  6280. + || d_is_negative(d)
  6281. + || !inode->i_nlink))
  6282. + err = -ENOENT;
  6283. + return err;
  6284. +}
  6285. +
  6286. +static inline int au_d_linkable(struct dentry *d)
  6287. +{
  6288. + int err;
  6289. + struct inode *inode = d_inode(d);
  6290. +
  6291. + err = au_d_hashed_positive(d);
  6292. + if (err
  6293. + && d_is_positive(d)
  6294. + && (inode->i_state & I_LINKABLE))
  6295. + err = 0;
  6296. + return err;
  6297. +}
  6298. +
  6299. +static inline int au_d_alive(struct dentry *d)
  6300. +{
  6301. + int err;
  6302. + struct inode *inode;
  6303. +
  6304. + err = 0;
  6305. + if (!IS_ROOT(d))
  6306. + err = au_d_hashed_positive(d);
  6307. + else {
  6308. + inode = d_inode(d);
  6309. + if (unlikely(d_unlinked(d)
  6310. + || d_is_negative(d)
  6311. + || !inode->i_nlink))
  6312. + err = -ENOENT;
  6313. + }
  6314. + return err;
  6315. +}
  6316. +
  6317. +static inline int au_alive_dir(struct dentry *d)
  6318. +{
  6319. + int err;
  6320. +
  6321. + err = au_d_alive(d);
  6322. + if (unlikely(err || IS_DEADDIR(d_inode(d))))
  6323. + err = -ENOENT;
  6324. + return err;
  6325. +}
  6326. +
  6327. +static inline int au_qstreq(struct qstr *a, struct qstr *b)
  6328. +{
  6329. + return a->len == b->len
  6330. + && !memcmp(a->name, b->name, a->len);
  6331. +}
  6332. +
  6333. +/*
  6334. + * by the commit
  6335. + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
  6336. + * taking d_lock
  6337. + * the type of d_lockref.count became int, but the inlined function d_count()
  6338. + * still returns unsigned int.
  6339. + * I don't know why. Maybe it is for every d_count() users?
  6340. + * Anyway au_dcount() lives on.
  6341. + */
  6342. +static inline int au_dcount(struct dentry *d)
  6343. +{
  6344. + return (int)d_count(d);
  6345. +}
  6346. +
  6347. +#endif /* __KERNEL__ */
  6348. +#endif /* __AUFS_DCSUB_H__ */
  6349. diff -Naur null/fs/aufs/debug.c linux-5.15.131/fs/aufs/debug.c
  6350. --- /dev/null
  6351. +++ linux-5.15.131/fs/aufs/debug.c 2023-09-11 14:32:48.146722577 +0300
  6352. @@ -0,0 +1,444 @@
  6353. +// SPDX-License-Identifier: GPL-2.0
  6354. +/*
  6355. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  6356. + *
  6357. + * This program, aufs is free software; you can redistribute it and/or modify
  6358. + * it under the terms of the GNU General Public License as published by
  6359. + * the Free Software Foundation; either version 2 of the License, or
  6360. + * (at your option) any later version.
  6361. + *
  6362. + * This program is distributed in the hope that it will be useful,
  6363. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6364. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6365. + * GNU General Public License for more details.
  6366. + *
  6367. + * You should have received a copy of the GNU General Public License
  6368. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  6369. + */
  6370. +
  6371. +/*
  6372. + * debug print functions
  6373. + */
  6374. +
  6375. +#include <linux/iversion.h>
  6376. +#include "aufs.h"
  6377. +
  6378. +/* Returns 0, or -errno. arg is in kp->arg. */
  6379. +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
  6380. +{
  6381. + int err, n;
  6382. +
  6383. + err = kstrtoint(val, 0, &n);
  6384. + if (!err) {
  6385. + if (n > 0)
  6386. + au_debug_on();
  6387. + else
  6388. + au_debug_off();
  6389. + }
  6390. + return err;
  6391. +}
  6392. +
  6393. +/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
  6394. +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
  6395. +{
  6396. + atomic_t *a;
  6397. +
  6398. + a = kp->arg;
  6399. + return sprintf(buffer, "%d", atomic_read(a));
  6400. +}
  6401. +
  6402. +static const struct kernel_param_ops param_ops_atomic_t = {
  6403. + .set = param_atomic_t_set,
  6404. + .get = param_atomic_t_get
  6405. + /* void (*free)(void *arg) */
  6406. +};
  6407. +
  6408. +atomic_t aufs_debug = ATOMIC_INIT(0);
  6409. +MODULE_PARM_DESC(debug, "debug print");
  6410. +module_param_named(debug, aufs_debug, atomic_t, 0664);
  6411. +
  6412. +DEFINE_MUTEX(au_dbg_mtx); /* just to serialize the dbg msgs */
  6413. +char *au_plevel = KERN_DEBUG;
  6414. +#define dpri(fmt, ...) do { \
  6415. + if ((au_plevel \
  6416. + && strcmp(au_plevel, KERN_DEBUG)) \
  6417. + || au_debug_test()) \
  6418. + printk("%s" fmt, au_plevel, ##__VA_ARGS__); \
  6419. +} while (0)
  6420. +
  6421. +/* ---------------------------------------------------------------------- */
  6422. +
  6423. +void au_dpri_whlist(struct au_nhash *whlist)
  6424. +{
  6425. + unsigned long ul, n;
  6426. + struct hlist_head *head;
  6427. + struct au_vdir_wh *pos;
  6428. +
  6429. + n = whlist->nh_num;
  6430. + head = whlist->nh_head;
  6431. + for (ul = 0; ul < n; ul++) {
  6432. + hlist_for_each_entry(pos, head, wh_hash)
  6433. + dpri("b%d, %.*s, %d\n",
  6434. + pos->wh_bindex,
  6435. + pos->wh_str.len, pos->wh_str.name,
  6436. + pos->wh_str.len);
  6437. + head++;
  6438. + }
  6439. +}
  6440. +
  6441. +void au_dpri_vdir(struct au_vdir *vdir)
  6442. +{
  6443. + unsigned long ul;
  6444. + union au_vdir_deblk_p p;
  6445. + unsigned char *o;
  6446. +
  6447. + if (!vdir || IS_ERR(vdir)) {
  6448. + dpri("err %ld\n", PTR_ERR(vdir));
  6449. + return;
  6450. + }
  6451. +
  6452. + dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
  6453. + vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
  6454. + vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
  6455. + for (ul = 0; ul < vdir->vd_nblk; ul++) {
  6456. + p.deblk = vdir->vd_deblk[ul];
  6457. + o = p.deblk;
  6458. + dpri("[%lu]: %p\n", ul, o);
  6459. + }
  6460. +}
  6461. +
  6462. +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
  6463. + struct dentry *wh)
  6464. +{
  6465. + char *n = NULL;
  6466. + int l = 0;
  6467. +
  6468. + if (!inode || IS_ERR(inode)) {
  6469. + dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
  6470. + return -1;
  6471. + }
  6472. +
  6473. + /* the type of i_blocks depends upon CONFIG_LBDAF */
  6474. + BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
  6475. + && sizeof(inode->i_blocks) != sizeof(u64));
  6476. + if (wh) {
  6477. + n = (void *)wh->d_name.name;
  6478. + l = wh->d_name.len;
  6479. + }
  6480. +
  6481. + dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
  6482. + " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
  6483. + bindex, inode,
  6484. + inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
  6485. + atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
  6486. + i_size_read(inode), (unsigned long long)inode->i_blocks,
  6487. + hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
  6488. + inode->i_mapping ? inode->i_mapping->nrpages : 0,
  6489. + inode->i_state, inode->i_flags, inode_peek_iversion(inode),
  6490. + inode->i_generation,
  6491. + l ? ", wh " : "", l, n);
  6492. + return 0;
  6493. +}
  6494. +
  6495. +void au_dpri_inode(struct inode *inode)
  6496. +{
  6497. + struct au_iinfo *iinfo;
  6498. + struct au_hinode *hi;
  6499. + aufs_bindex_t bindex;
  6500. + int err, hn;
  6501. +
  6502. + err = do_pri_inode(-1, inode, -1, NULL);
  6503. + if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
  6504. + return;
  6505. +
  6506. + iinfo = au_ii(inode);
  6507. + dpri("i-1: btop %d, bbot %d, gen %d\n",
  6508. + iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
  6509. + if (iinfo->ii_btop < 0)
  6510. + return;
  6511. + hn = 0;
  6512. + for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
  6513. + hi = au_hinode(iinfo, bindex);
  6514. + hn = !!au_hn(hi);
  6515. + do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
  6516. + }
  6517. +}
  6518. +
  6519. +void au_dpri_dalias(struct inode *inode)
  6520. +{
  6521. + struct dentry *d;
  6522. +
  6523. + spin_lock(&inode->i_lock);
  6524. + hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
  6525. + au_dpri_dentry(d);
  6526. + spin_unlock(&inode->i_lock);
  6527. +}
  6528. +
  6529. +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
  6530. +{
  6531. + struct dentry *wh = NULL;
  6532. + int hn;
  6533. + struct inode *inode;
  6534. + struct au_iinfo *iinfo;
  6535. + struct au_hinode *hi;
  6536. +
  6537. + if (!dentry || IS_ERR(dentry)) {
  6538. + dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
  6539. + return -1;
  6540. + }
  6541. + /* do not call dget_parent() here */
  6542. + /* note: access d_xxx without d_lock */
  6543. + dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
  6544. + bindex, dentry, dentry,
  6545. + dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
  6546. + au_dcount(dentry), dentry->d_flags,
  6547. + d_unhashed(dentry) ? "un" : "");
  6548. + hn = -1;
  6549. + inode = NULL;
  6550. + if (d_is_positive(dentry))
  6551. + inode = d_inode(dentry);
  6552. + if (inode
  6553. + && au_test_aufs(dentry->d_sb)
  6554. + && bindex >= 0
  6555. + && !au_is_bad_inode(inode)) {
  6556. + iinfo = au_ii(inode);
  6557. + hi = au_hinode(iinfo, bindex);
  6558. + hn = !!au_hn(hi);
  6559. + wh = hi->hi_whdentry;
  6560. + }
  6561. + do_pri_inode(bindex, inode, hn, wh);
  6562. + return 0;
  6563. +}
  6564. +
  6565. +void au_dpri_dentry(struct dentry *dentry)
  6566. +{
  6567. + struct au_dinfo *dinfo;
  6568. + aufs_bindex_t bindex;
  6569. + int err;
  6570. +
  6571. + err = do_pri_dentry(-1, dentry);
  6572. + if (err || !au_test_aufs(dentry->d_sb))
  6573. + return;
  6574. +
  6575. + dinfo = au_di(dentry);
  6576. + if (!dinfo)
  6577. + return;
  6578. + dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
  6579. + dinfo->di_btop, dinfo->di_bbot,
  6580. + dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
  6581. + dinfo->di_tmpfile);
  6582. + if (dinfo->di_btop < 0)
  6583. + return;
  6584. + for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
  6585. + do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
  6586. +}
  6587. +
  6588. +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
  6589. +{
  6590. + char a[32];
  6591. +
  6592. + if (!file || IS_ERR(file)) {
  6593. + dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
  6594. + return -1;
  6595. + }
  6596. + a[0] = 0;
  6597. + if (bindex < 0
  6598. + && !IS_ERR_OR_NULL(file->f_path.dentry)
  6599. + && au_test_aufs(file->f_path.dentry->d_sb)
  6600. + && au_fi(file))
  6601. + snprintf(a, sizeof(a), ", gen %d, mmapped %d",
  6602. + au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
  6603. + dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
  6604. + bindex, file->f_mode, file->f_flags, (long)file_count(file),
  6605. + file->f_version, file->f_pos, a);
  6606. + if (!IS_ERR_OR_NULL(file->f_path.dentry))
  6607. + do_pri_dentry(bindex, file->f_path.dentry);
  6608. + return 0;
  6609. +}
  6610. +
  6611. +void au_dpri_file(struct file *file)
  6612. +{
  6613. + struct au_finfo *finfo;
  6614. + struct au_fidir *fidir;
  6615. + struct au_hfile *hfile;
  6616. + aufs_bindex_t bindex;
  6617. + int err;
  6618. +
  6619. + err = do_pri_file(-1, file);
  6620. + if (err
  6621. + || IS_ERR_OR_NULL(file->f_path.dentry)
  6622. + || !au_test_aufs(file->f_path.dentry->d_sb))
  6623. + return;
  6624. +
  6625. + finfo = au_fi(file);
  6626. + if (!finfo)
  6627. + return;
  6628. + if (finfo->fi_btop < 0)
  6629. + return;
  6630. + fidir = finfo->fi_hdir;
  6631. + if (!fidir)
  6632. + do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
  6633. + else
  6634. + for (bindex = finfo->fi_btop;
  6635. + bindex >= 0 && bindex <= fidir->fd_bbot;
  6636. + bindex++) {
  6637. + hfile = fidir->fd_hfile + bindex;
  6638. + do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
  6639. + }
  6640. +}
  6641. +
  6642. +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
  6643. +{
  6644. + struct vfsmount *mnt;
  6645. + struct super_block *sb;
  6646. +
  6647. + if (!br || IS_ERR(br))
  6648. + goto out;
  6649. + mnt = au_br_mnt(br);
  6650. + if (!mnt || IS_ERR(mnt))
  6651. + goto out;
  6652. + sb = mnt->mnt_sb;
  6653. + if (!sb || IS_ERR(sb))
  6654. + goto out;
  6655. +
  6656. + dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
  6657. + "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
  6658. + "xino %d\n",
  6659. + bindex, br->br_perm, br->br_id, br->br_wbr,
  6660. + au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
  6661. + sb->s_flags, sb->s_count,
  6662. + atomic_read(&sb->s_active),
  6663. + !!au_xino_file(br->br_xino, /*idx*/-1));
  6664. + return 0;
  6665. +
  6666. +out:
  6667. + dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
  6668. + return -1;
  6669. +}
  6670. +
  6671. +void au_dpri_sb(struct super_block *sb)
  6672. +{
  6673. + struct au_sbinfo *sbinfo;
  6674. + aufs_bindex_t bindex;
  6675. + int err;
  6676. + /* to reduce stack size */
  6677. + struct {
  6678. + struct vfsmount mnt;
  6679. + struct au_branch fake;
  6680. + } *a;
  6681. +
  6682. + /* this function can be called from magic sysrq */
  6683. + a = kzalloc(sizeof(*a), GFP_ATOMIC);
  6684. + if (unlikely(!a)) {
  6685. + dpri("no memory\n");
  6686. + return;
  6687. + }
  6688. +
  6689. + a->mnt.mnt_sb = sb;
  6690. + a->fake.br_path.mnt = &a->mnt;
  6691. + err = do_pri_br(-1, &a->fake);
  6692. + au_kfree_rcu(a);
  6693. + dpri("dev 0x%x\n", sb->s_dev);
  6694. + if (err || !au_test_aufs(sb))
  6695. + return;
  6696. +
  6697. + sbinfo = au_sbi(sb);
  6698. + if (!sbinfo)
  6699. + return;
  6700. + dpri("nw %d, gen %u, kobj %d\n",
  6701. + atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
  6702. + kref_read(&sbinfo->si_kobj.kref));
  6703. + for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
  6704. + do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
  6705. +}
  6706. +
  6707. +/* ---------------------------------------------------------------------- */
  6708. +
  6709. +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
  6710. +{
  6711. + struct inode *h_inode, *inode = d_inode(dentry);
  6712. + struct dentry *h_dentry;
  6713. + aufs_bindex_t bindex, bbot, bi;
  6714. +
  6715. + if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
  6716. + return;
  6717. +
  6718. + bbot = au_dbbot(dentry);
  6719. + bi = au_ibbot(inode);
  6720. + if (bi < bbot)
  6721. + bbot = bi;
  6722. + bindex = au_dbtop(dentry);
  6723. + bi = au_ibtop(inode);
  6724. + if (bi > bindex)
  6725. + bindex = bi;
  6726. +
  6727. + for (; bindex <= bbot; bindex++) {
  6728. + h_dentry = au_h_dptr(dentry, bindex);
  6729. + if (!h_dentry)
  6730. + continue;
  6731. + h_inode = au_h_iptr(inode, bindex);
  6732. + if (unlikely(h_inode != d_inode(h_dentry))) {
  6733. + au_debug_on();
  6734. + AuDbg("b%d, %s:%d\n", bindex, func, line);
  6735. + AuDbgDentry(dentry);
  6736. + AuDbgInode(inode);
  6737. + au_debug_off();
  6738. + if (au_test_fuse(h_inode->i_sb))
  6739. + WARN_ON_ONCE(1);
  6740. + else
  6741. + BUG();
  6742. + }
  6743. + }
  6744. +}
  6745. +
  6746. +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
  6747. +{
  6748. + int err, i, j;
  6749. + struct au_dcsub_pages dpages;
  6750. + struct au_dpage *dpage;
  6751. + struct dentry **dentries;
  6752. +
  6753. + err = au_dpages_init(&dpages, GFP_NOFS);
  6754. + AuDebugOn(err);
  6755. + err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
  6756. + AuDebugOn(err);
  6757. + for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
  6758. + dpage = dpages.dpages + i;
  6759. + dentries = dpage->dentries;
  6760. + for (j = dpage->ndentry - 1; !err && j >= 0; j--)
  6761. + AuDebugOn(au_digen_test(dentries[j], sigen));
  6762. + }
  6763. + au_dpages_free(&dpages);
  6764. +}
  6765. +
  6766. +void au_dbg_verify_kthread(void)
  6767. +{
  6768. + if (au_wkq_test()) {
  6769. + au_dbg_blocked();
  6770. + /*
  6771. + * It may be recursive, but udba=notify between two aufs mounts,
  6772. + * where a single ro branch is shared, is not a problem.
  6773. + */
  6774. + /* WARN_ON(1); */
  6775. + }
  6776. +}
  6777. +
  6778. +/* ---------------------------------------------------------------------- */
  6779. +
  6780. +int __init au_debug_init(void)
  6781. +{
  6782. + aufs_bindex_t bindex;
  6783. + struct au_vdir_destr destr;
  6784. +
  6785. + bindex = -1;
  6786. + AuDebugOn(bindex >= 0);
  6787. +
  6788. + destr.len = -1;
  6789. + AuDebugOn(destr.len < NAME_MAX);
  6790. +
  6791. +#ifdef CONFIG_4KSTACKS
  6792. + pr_warn("CONFIG_4KSTACKS is defined.\n");
  6793. +#endif
  6794. +
  6795. + return 0;
  6796. +}
  6797. diff -Naur null/fs/aufs/debug.h linux-5.15.131/fs/aufs/debug.h
  6798. --- /dev/null
  6799. +++ linux-5.15.131/fs/aufs/debug.h 2023-09-11 14:32:48.146722577 +0300
  6800. @@ -0,0 +1,226 @@
  6801. +/* SPDX-License-Identifier: GPL-2.0 */
  6802. +/*
  6803. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  6804. + *
  6805. + * This program, aufs is free software; you can redistribute it and/or modify
  6806. + * it under the terms of the GNU General Public License as published by
  6807. + * the Free Software Foundation; either version 2 of the License, or
  6808. + * (at your option) any later version.
  6809. + *
  6810. + * This program is distributed in the hope that it will be useful,
  6811. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6812. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6813. + * GNU General Public License for more details.
  6814. + *
  6815. + * You should have received a copy of the GNU General Public License
  6816. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  6817. + */
  6818. +
  6819. +/*
  6820. + * debug print functions
  6821. + */
  6822. +
  6823. +#ifndef __AUFS_DEBUG_H__
  6824. +#define __AUFS_DEBUG_H__
  6825. +
  6826. +#ifdef __KERNEL__
  6827. +
  6828. +#include <linux/atomic.h>
  6829. +#include <linux/module.h>
  6830. +#include <linux/kallsyms.h>
  6831. +#include <linux/sysrq.h>
  6832. +
  6833. +#ifdef CONFIG_AUFS_DEBUG
  6834. +#define AuDebugOn(a) BUG_ON(a)
  6835. +
  6836. +/* module parameter */
  6837. +extern atomic_t aufs_debug;
  6838. +static inline void au_debug_on(void)
  6839. +{
  6840. + atomic_inc(&aufs_debug);
  6841. +}
  6842. +static inline void au_debug_off(void)
  6843. +{
  6844. + atomic_dec_if_positive(&aufs_debug);
  6845. +}
  6846. +
  6847. +static inline int au_debug_test(void)
  6848. +{
  6849. + return atomic_read(&aufs_debug) > 0;
  6850. +}
  6851. +#else
  6852. +#define AuDebugOn(a) do {} while (0)
  6853. +AuStubVoid(au_debug_on, void)
  6854. +AuStubVoid(au_debug_off, void)
  6855. +AuStubInt0(au_debug_test, void)
  6856. +#endif /* CONFIG_AUFS_DEBUG */
  6857. +
  6858. +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
  6859. +
  6860. +/* ---------------------------------------------------------------------- */
  6861. +
  6862. +/* debug print */
  6863. +
  6864. +#define AuDbg(fmt, ...) do { \
  6865. + if (au_debug_test()) \
  6866. + pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
  6867. +} while (0)
  6868. +#define AuLabel(l) AuDbg(#l "\n")
  6869. +#define AuIOErr(fmt, ...) pr_err("I/O Error, " fmt, ##__VA_ARGS__)
  6870. +#define AuWarn1(fmt, ...) do { \
  6871. + static unsigned char _c; \
  6872. + if (!_c++) \
  6873. + pr_warn(fmt, ##__VA_ARGS__); \
  6874. +} while (0)
  6875. +
  6876. +#define AuErr1(fmt, ...) do { \
  6877. + static unsigned char _c; \
  6878. + if (!_c++) \
  6879. + pr_err(fmt, ##__VA_ARGS__); \
  6880. +} while (0)
  6881. +
  6882. +#define AuIOErr1(fmt, ...) do { \
  6883. + static unsigned char _c; \
  6884. + if (!_c++) \
  6885. + AuIOErr(fmt, ##__VA_ARGS__); \
  6886. +} while (0)
  6887. +
  6888. +#define AuUnsupportMsg "This operation is not supported." \
  6889. + " Please report this application to aufs-users ML."
  6890. +#define AuUnsupport(fmt, ...) do { \
  6891. + pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
  6892. + dump_stack(); \
  6893. +} while (0)
  6894. +
  6895. +#define AuTraceErr(e) do { \
  6896. + if (unlikely((e) < 0)) \
  6897. + AuDbg("err %d\n", (int)(e)); \
  6898. +} while (0)
  6899. +
  6900. +#define AuTraceErrPtr(p) do { \
  6901. + if (IS_ERR(p)) \
  6902. + AuDbg("err %ld\n", PTR_ERR(p)); \
  6903. +} while (0)
  6904. +
  6905. +/* dirty macros for debug print, use with "%.*s" and caution */
  6906. +#define AuLNPair(qstr) (qstr)->len, (qstr)->name
  6907. +
  6908. +/* ---------------------------------------------------------------------- */
  6909. +
  6910. +struct dentry;
  6911. +#ifdef CONFIG_AUFS_DEBUG
  6912. +extern struct mutex au_dbg_mtx;
  6913. +extern char *au_plevel;
  6914. +struct au_nhash;
  6915. +void au_dpri_whlist(struct au_nhash *whlist);
  6916. +struct au_vdir;
  6917. +void au_dpri_vdir(struct au_vdir *vdir);
  6918. +struct inode;
  6919. +void au_dpri_inode(struct inode *inode);
  6920. +void au_dpri_dalias(struct inode *inode);
  6921. +void au_dpri_dentry(struct dentry *dentry);
  6922. +struct file;
  6923. +void au_dpri_file(struct file *filp);
  6924. +struct super_block;
  6925. +void au_dpri_sb(struct super_block *sb);
  6926. +
  6927. +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
  6928. +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
  6929. +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
  6930. +void au_dbg_verify_kthread(void);
  6931. +
  6932. +int __init au_debug_init(void);
  6933. +
  6934. +#define AuDbgWhlist(w) do { \
  6935. + mutex_lock(&au_dbg_mtx); \
  6936. + AuDbg(#w "\n"); \
  6937. + au_dpri_whlist(w); \
  6938. + mutex_unlock(&au_dbg_mtx); \
  6939. +} while (0)
  6940. +
  6941. +#define AuDbgVdir(v) do { \
  6942. + mutex_lock(&au_dbg_mtx); \
  6943. + AuDbg(#v "\n"); \
  6944. + au_dpri_vdir(v); \
  6945. + mutex_unlock(&au_dbg_mtx); \
  6946. +} while (0)
  6947. +
  6948. +#define AuDbgInode(i) do { \
  6949. + mutex_lock(&au_dbg_mtx); \
  6950. + AuDbg(#i "\n"); \
  6951. + au_dpri_inode(i); \
  6952. + mutex_unlock(&au_dbg_mtx); \
  6953. +} while (0)
  6954. +
  6955. +#define AuDbgDAlias(i) do { \
  6956. + mutex_lock(&au_dbg_mtx); \
  6957. + AuDbg(#i "\n"); \
  6958. + au_dpri_dalias(i); \
  6959. + mutex_unlock(&au_dbg_mtx); \
  6960. +} while (0)
  6961. +
  6962. +#define AuDbgDentry(d) do { \
  6963. + mutex_lock(&au_dbg_mtx); \
  6964. + AuDbg(#d "\n"); \
  6965. + au_dpri_dentry(d); \
  6966. + mutex_unlock(&au_dbg_mtx); \
  6967. +} while (0)
  6968. +
  6969. +#define AuDbgFile(f) do { \
  6970. + mutex_lock(&au_dbg_mtx); \
  6971. + AuDbg(#f "\n"); \
  6972. + au_dpri_file(f); \
  6973. + mutex_unlock(&au_dbg_mtx); \
  6974. +} while (0)
  6975. +
  6976. +#define AuDbgSb(sb) do { \
  6977. + mutex_lock(&au_dbg_mtx); \
  6978. + AuDbg(#sb "\n"); \
  6979. + au_dpri_sb(sb); \
  6980. + mutex_unlock(&au_dbg_mtx); \
  6981. +} while (0)
  6982. +
  6983. +#define AuDbgSym(addr) do { \
  6984. + char sym[KSYM_SYMBOL_LEN]; \
  6985. + sprint_symbol(sym, (unsigned long)addr); \
  6986. + AuDbg("%s\n", sym); \
  6987. +} while (0)
  6988. +#else
  6989. +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
  6990. +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
  6991. +AuStubVoid(au_dbg_verify_kthread, void)
  6992. +AuStubInt0(__init au_debug_init, void)
  6993. +
  6994. +#define AuDbgWhlist(w) do {} while (0)
  6995. +#define AuDbgVdir(v) do {} while (0)
  6996. +#define AuDbgInode(i) do {} while (0)
  6997. +#define AuDbgDAlias(i) do {} while (0)
  6998. +#define AuDbgDentry(d) do {} while (0)
  6999. +#define AuDbgFile(f) do {} while (0)
  7000. +#define AuDbgSb(sb) do {} while (0)
  7001. +#define AuDbgSym(addr) do {} while (0)
  7002. +#endif /* CONFIG_AUFS_DEBUG */
  7003. +
  7004. +/* ---------------------------------------------------------------------- */
  7005. +
  7006. +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
  7007. +int __init au_sysrq_init(void);
  7008. +void au_sysrq_fin(void);
  7009. +
  7010. +#ifdef CONFIG_HW_CONSOLE
  7011. +#define au_dbg_blocked() do { \
  7012. + WARN_ON(1); \
  7013. + handle_sysrq('w'); \
  7014. +} while (0)
  7015. +#else
  7016. +AuStubVoid(au_dbg_blocked, void)
  7017. +#endif
  7018. +
  7019. +#else
  7020. +AuStubInt0(__init au_sysrq_init, void)
  7021. +AuStubVoid(au_sysrq_fin, void)
  7022. +AuStubVoid(au_dbg_blocked, void)
  7023. +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
  7024. +
  7025. +#endif /* __KERNEL__ */
  7026. +#endif /* __AUFS_DEBUG_H__ */
  7027. diff -Naur null/fs/aufs/dentry.c linux-5.15.131/fs/aufs/dentry.c
  7028. --- /dev/null
  7029. +++ linux-5.15.131/fs/aufs/dentry.c 2023-09-11 14:32:48.146722577 +0300
  7030. @@ -0,0 +1,1168 @@
  7031. +// SPDX-License-Identifier: GPL-2.0
  7032. +/*
  7033. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  7034. + *
  7035. + * This program, aufs is free software; you can redistribute it and/or modify
  7036. + * it under the terms of the GNU General Public License as published by
  7037. + * the Free Software Foundation; either version 2 of the License, or
  7038. + * (at your option) any later version.
  7039. + *
  7040. + * This program is distributed in the hope that it will be useful,
  7041. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7042. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7043. + * GNU General Public License for more details.
  7044. + *
  7045. + * You should have received a copy of the GNU General Public License
  7046. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  7047. + */
  7048. +
  7049. +/*
  7050. + * lookup and dentry operations
  7051. + */
  7052. +
  7053. +#include <linux/iversion.h>
  7054. +#include "aufs.h"
  7055. +
  7056. +/*
  7057. + * returns positive/negative dentry, NULL or an error.
  7058. + * NULL means whiteout-ed or not-found.
  7059. + */
  7060. +static struct dentry*
  7061. +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
  7062. + aufs_bindex_t bindex, struct au_do_lookup_args *args)
  7063. +{
  7064. + struct dentry *h_dentry;
  7065. + struct inode *h_inode;
  7066. + struct au_branch *br;
  7067. + struct user_namespace *h_userns;
  7068. + struct path h_path;
  7069. + int wh_found, opq;
  7070. + unsigned char wh_able;
  7071. + const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
  7072. + const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
  7073. + IGNORE_PERM);
  7074. +
  7075. + wh_found = 0;
  7076. + br = au_sbr(dentry->d_sb, bindex);
  7077. + h_path.dentry = h_parent;
  7078. + h_path.mnt = au_br_mnt(br);
  7079. + h_userns = au_br_userns(br);
  7080. + wh_able = !!au_br_whable(br->br_perm);
  7081. + if (wh_able)
  7082. + wh_found = au_wh_test(h_userns, &h_path, &args->whname,
  7083. + ignore_perm);
  7084. + h_dentry = ERR_PTR(wh_found);
  7085. + if (!wh_found)
  7086. + goto real_lookup;
  7087. + if (unlikely(wh_found < 0))
  7088. + goto out;
  7089. +
  7090. + /* We found a whiteout */
  7091. + /* au_set_dbbot(dentry, bindex); */
  7092. + au_set_dbwh(dentry, bindex);
  7093. + if (!allow_neg)
  7094. + return NULL; /* success */
  7095. +
  7096. +real_lookup:
  7097. + if (!ignore_perm)
  7098. + h_dentry = vfsub_lkup_one(args->name, &h_path);
  7099. + else
  7100. + h_dentry = au_sio_lkup_one(h_userns, args->name, &h_path);
  7101. + if (IS_ERR(h_dentry)) {
  7102. + if (PTR_ERR(h_dentry) == -ENAMETOOLONG
  7103. + && !allow_neg)
  7104. + h_dentry = NULL;
  7105. + goto out;
  7106. + }
  7107. +
  7108. + h_inode = d_inode(h_dentry);
  7109. + if (d_is_negative(h_dentry)) {
  7110. + if (!allow_neg)
  7111. + goto out_neg;
  7112. + } else if (wh_found
  7113. + || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
  7114. + goto out_neg;
  7115. + else if (au_ftest_lkup(args->flags, DIRREN)
  7116. + /* && h_inode */
  7117. + && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
  7118. + AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
  7119. + (unsigned long long)h_inode->i_ino);
  7120. + goto out_neg;
  7121. + }
  7122. +
  7123. + if (au_dbbot(dentry) <= bindex)
  7124. + au_set_dbbot(dentry, bindex);
  7125. + if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
  7126. + au_set_dbtop(dentry, bindex);
  7127. + au_set_h_dptr(dentry, bindex, h_dentry);
  7128. +
  7129. + if (!d_is_dir(h_dentry)
  7130. + || !wh_able
  7131. + || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
  7132. + goto out; /* success */
  7133. +
  7134. + h_path.dentry = h_dentry;
  7135. + inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
  7136. + opq = au_diropq_test(h_userns, &h_path);
  7137. + inode_unlock_shared(h_inode);
  7138. + if (opq > 0)
  7139. + au_set_dbdiropq(dentry, bindex);
  7140. + else if (unlikely(opq < 0)) {
  7141. + au_set_h_dptr(dentry, bindex, NULL);
  7142. + h_dentry = ERR_PTR(opq);
  7143. + }
  7144. + goto out;
  7145. +
  7146. +out_neg:
  7147. + dput(h_dentry);
  7148. + h_dentry = NULL;
  7149. +out:
  7150. + return h_dentry;
  7151. +}
  7152. +
  7153. +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
  7154. +{
  7155. + if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
  7156. + && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
  7157. + return -EPERM;
  7158. + return 0;
  7159. +}
  7160. +
  7161. +/*
  7162. + * returns the number of lower positive dentries,
  7163. + * otherwise an error.
  7164. + * can be called at unlinking with @type is zero.
  7165. + */
  7166. +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
  7167. + unsigned int flags)
  7168. +{
  7169. + int npositive, err;
  7170. + aufs_bindex_t bindex, btail, bdiropq;
  7171. + unsigned char isdir, dirperm1, dirren;
  7172. + struct au_do_lookup_args args = {
  7173. + .flags = flags,
  7174. + .name = &dentry->d_name
  7175. + };
  7176. + struct dentry *parent;
  7177. + struct super_block *sb;
  7178. +
  7179. + sb = dentry->d_sb;
  7180. + err = au_test_shwh(sb, args.name);
  7181. + if (unlikely(err))
  7182. + goto out;
  7183. +
  7184. + err = au_wh_name_alloc(&args.whname, args.name);
  7185. + if (unlikely(err))
  7186. + goto out;
  7187. +
  7188. + isdir = !!d_is_dir(dentry);
  7189. + dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
  7190. + dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
  7191. + if (dirren)
  7192. + au_fset_lkup(args.flags, DIRREN);
  7193. +
  7194. + npositive = 0;
  7195. + parent = dget_parent(dentry);
  7196. + btail = au_dbtaildir(parent);
  7197. + for (bindex = btop; bindex <= btail; bindex++) {
  7198. + struct dentry *h_parent, *h_dentry;
  7199. + struct inode *h_inode, *h_dir;
  7200. + struct au_branch *br;
  7201. +
  7202. + h_dentry = au_h_dptr(dentry, bindex);
  7203. + if (h_dentry) {
  7204. + if (d_is_positive(h_dentry))
  7205. + npositive++;
  7206. + break;
  7207. + }
  7208. + h_parent = au_h_dptr(parent, bindex);
  7209. + if (!h_parent || !d_is_dir(h_parent))
  7210. + continue;
  7211. +
  7212. + if (dirren) {
  7213. + /* if the inum matches, then use the prepared name */
  7214. + err = au_dr_lkup_name(&args, bindex);
  7215. + if (unlikely(err))
  7216. + goto out_parent;
  7217. + }
  7218. +
  7219. + h_dir = d_inode(h_parent);
  7220. + inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
  7221. + h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
  7222. + inode_unlock_shared(h_dir);
  7223. + err = PTR_ERR(h_dentry);
  7224. + if (IS_ERR(h_dentry))
  7225. + goto out_parent;
  7226. + if (h_dentry)
  7227. + au_fclr_lkup(args.flags, ALLOW_NEG);
  7228. + if (dirperm1)
  7229. + au_fset_lkup(args.flags, IGNORE_PERM);
  7230. +
  7231. + if (au_dbwh(dentry) == bindex)
  7232. + break;
  7233. + if (!h_dentry)
  7234. + continue;
  7235. + if (d_is_negative(h_dentry))
  7236. + continue;
  7237. + h_inode = d_inode(h_dentry);
  7238. + npositive++;
  7239. + if (!args.type)
  7240. + args.type = h_inode->i_mode & S_IFMT;
  7241. + if (args.type != S_IFDIR)
  7242. + break;
  7243. + else if (isdir) {
  7244. + /* the type of lower may be different */
  7245. + bdiropq = au_dbdiropq(dentry);
  7246. + if (bdiropq >= 0 && bdiropq <= bindex)
  7247. + break;
  7248. + }
  7249. + br = au_sbr(sb, bindex);
  7250. + if (dirren
  7251. + && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
  7252. + /*add_ent*/NULL)) {
  7253. + /* prepare next name to lookup */
  7254. + err = au_dr_lkup(&args, dentry, bindex);
  7255. + if (unlikely(err))
  7256. + goto out_parent;
  7257. + }
  7258. + }
  7259. +
  7260. + if (npositive) {
  7261. + AuLabel(positive);
  7262. + au_update_dbtop(dentry);
  7263. + }
  7264. + err = npositive;
  7265. + if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
  7266. + && au_dbtop(dentry) < 0)) {
  7267. + err = -EIO;
  7268. + AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
  7269. + dentry, err);
  7270. + }
  7271. +
  7272. +out_parent:
  7273. + dput(parent);
  7274. + au_kfree_try_rcu(args.whname.name);
  7275. + if (dirren)
  7276. + au_dr_lkup_fin(&args);
  7277. +out:
  7278. + return err;
  7279. +}
  7280. +
  7281. +struct dentry *au_sio_lkup_one(struct user_namespace *userns, struct qstr *name,
  7282. + struct path *ppath)
  7283. +{
  7284. + struct dentry *dentry;
  7285. + int wkq_err;
  7286. +
  7287. + if (!au_test_h_perm_sio(userns, d_inode(ppath->dentry), MAY_EXEC))
  7288. + dentry = vfsub_lkup_one(name, ppath);
  7289. + else {
  7290. + struct vfsub_lkup_one_args args = {
  7291. + .errp = &dentry,
  7292. + .name = name,
  7293. + .ppath = ppath
  7294. + };
  7295. +
  7296. + wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
  7297. + if (unlikely(wkq_err))
  7298. + dentry = ERR_PTR(wkq_err);
  7299. + }
  7300. +
  7301. + return dentry;
  7302. +}
  7303. +
  7304. +/*
  7305. + * lookup @dentry on @bindex which should be negative.
  7306. + */
  7307. +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
  7308. +{
  7309. + int err;
  7310. + struct dentry *parent, *h_dentry;
  7311. + struct au_branch *br;
  7312. + struct user_namespace *h_userns;
  7313. + struct path h_ppath;
  7314. +
  7315. + parent = dget_parent(dentry);
  7316. + br = au_sbr(dentry->d_sb, bindex);
  7317. + h_ppath.dentry = au_h_dptr(parent, bindex);
  7318. + h_ppath.mnt = au_br_mnt(br);
  7319. + h_userns = au_br_userns(br);
  7320. + if (wh)
  7321. + h_dentry = au_whtmp_lkup(h_ppath.dentry, br, &dentry->d_name);
  7322. + else
  7323. + h_dentry = au_sio_lkup_one(h_userns, &dentry->d_name, &h_ppath);
  7324. + err = PTR_ERR(h_dentry);
  7325. + if (IS_ERR(h_dentry))
  7326. + goto out;
  7327. + if (unlikely(d_is_positive(h_dentry))) {
  7328. + err = -EIO;
  7329. + AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
  7330. + dput(h_dentry);
  7331. + goto out;
  7332. + }
  7333. +
  7334. + err = 0;
  7335. + if (bindex < au_dbtop(dentry))
  7336. + au_set_dbtop(dentry, bindex);
  7337. + if (au_dbbot(dentry) < bindex)
  7338. + au_set_dbbot(dentry, bindex);
  7339. + au_set_h_dptr(dentry, bindex, h_dentry);
  7340. +
  7341. +out:
  7342. + dput(parent);
  7343. + return err;
  7344. +}
  7345. +
  7346. +/* ---------------------------------------------------------------------- */
  7347. +
  7348. +/* subset of struct inode */
  7349. +struct au_iattr {
  7350. + unsigned long i_ino;
  7351. + /* unsigned int i_nlink; */
  7352. + kuid_t i_uid;
  7353. + kgid_t i_gid;
  7354. + u64 i_version;
  7355. +/*
  7356. + loff_t i_size;
  7357. + blkcnt_t i_blocks;
  7358. +*/
  7359. + umode_t i_mode;
  7360. +};
  7361. +
  7362. +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
  7363. +{
  7364. + ia->i_ino = h_inode->i_ino;
  7365. + /* ia->i_nlink = h_inode->i_nlink; */
  7366. + ia->i_uid = h_inode->i_uid;
  7367. + ia->i_gid = h_inode->i_gid;
  7368. + ia->i_version = inode_query_iversion(h_inode);
  7369. +/*
  7370. + ia->i_size = h_inode->i_size;
  7371. + ia->i_blocks = h_inode->i_blocks;
  7372. +*/
  7373. + ia->i_mode = (h_inode->i_mode & S_IFMT);
  7374. +}
  7375. +
  7376. +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
  7377. +{
  7378. + return ia->i_ino != h_inode->i_ino
  7379. + /* || ia->i_nlink != h_inode->i_nlink */
  7380. + || !uid_eq(ia->i_uid, h_inode->i_uid)
  7381. + || !gid_eq(ia->i_gid, h_inode->i_gid)
  7382. + || !inode_eq_iversion(h_inode, ia->i_version)
  7383. +/*
  7384. + || ia->i_size != h_inode->i_size
  7385. + || ia->i_blocks != h_inode->i_blocks
  7386. +*/
  7387. + || ia->i_mode != (h_inode->i_mode & S_IFMT);
  7388. +}
  7389. +
  7390. +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
  7391. + struct au_branch *br)
  7392. +{
  7393. + int err;
  7394. + struct au_iattr ia;
  7395. + struct inode *h_inode;
  7396. + struct dentry *h_d;
  7397. + struct super_block *h_sb;
  7398. + struct path h_ppath;
  7399. +
  7400. + err = 0;
  7401. + memset(&ia, -1, sizeof(ia));
  7402. + h_sb = h_dentry->d_sb;
  7403. + h_inode = NULL;
  7404. + if (d_is_positive(h_dentry)) {
  7405. + h_inode = d_inode(h_dentry);
  7406. + au_iattr_save(&ia, h_inode);
  7407. + } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
  7408. + /* nfs d_revalidate may return 0 for negative dentry */
  7409. + /* fuse d_revalidate always return 0 for negative dentry */
  7410. + goto out;
  7411. +
  7412. + /* main purpose is namei.c:cached_lookup() and d_revalidate */
  7413. + h_ppath.dentry = h_parent;
  7414. + h_ppath.mnt = au_br_mnt(br);
  7415. + h_d = vfsub_lkup_one(&h_dentry->d_name, &h_ppath);
  7416. + err = PTR_ERR(h_d);
  7417. + if (IS_ERR(h_d))
  7418. + goto out;
  7419. +
  7420. + err = 0;
  7421. + if (unlikely(h_d != h_dentry
  7422. + || d_inode(h_d) != h_inode
  7423. + || (h_inode && au_iattr_test(&ia, h_inode))))
  7424. + err = au_busy_or_stale();
  7425. + dput(h_d);
  7426. +
  7427. +out:
  7428. + AuTraceErr(err);
  7429. + return err;
  7430. +}
  7431. +
  7432. +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
  7433. + struct dentry *h_parent, struct au_branch *br)
  7434. +{
  7435. + int err;
  7436. +
  7437. + err = 0;
  7438. + if (udba == AuOpt_UDBA_REVAL
  7439. + && !au_test_fs_remote(h_dentry->d_sb)) {
  7440. + IMustLock(h_dir);
  7441. + err = (d_inode(h_dentry->d_parent) != h_dir);
  7442. + } else if (udba != AuOpt_UDBA_NONE)
  7443. + err = au_h_verify_dentry(h_dentry, h_parent, br);
  7444. +
  7445. + return err;
  7446. +}
  7447. +
  7448. +/* ---------------------------------------------------------------------- */
  7449. +
  7450. +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
  7451. +{
  7452. + int err;
  7453. + aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
  7454. + struct au_hdentry tmp, *p, *q;
  7455. + struct au_dinfo *dinfo;
  7456. + struct super_block *sb;
  7457. +
  7458. + DiMustWriteLock(dentry);
  7459. +
  7460. + sb = dentry->d_sb;
  7461. + dinfo = au_di(dentry);
  7462. + bbot = dinfo->di_bbot;
  7463. + bwh = dinfo->di_bwh;
  7464. + bdiropq = dinfo->di_bdiropq;
  7465. + bindex = dinfo->di_btop;
  7466. + p = au_hdentry(dinfo, bindex);
  7467. + for (; bindex <= bbot; bindex++, p++) {
  7468. + if (!p->hd_dentry)
  7469. + continue;
  7470. +
  7471. + new_bindex = au_br_index(sb, p->hd_id);
  7472. + if (new_bindex == bindex)
  7473. + continue;
  7474. +
  7475. + if (dinfo->di_bwh == bindex)
  7476. + bwh = new_bindex;
  7477. + if (dinfo->di_bdiropq == bindex)
  7478. + bdiropq = new_bindex;
  7479. + if (new_bindex < 0) {
  7480. + au_hdput(p);
  7481. + p->hd_dentry = NULL;
  7482. + continue;
  7483. + }
  7484. +
  7485. + /* swap two lower dentries, and loop again */
  7486. + q = au_hdentry(dinfo, new_bindex);
  7487. + tmp = *q;
  7488. + *q = *p;
  7489. + *p = tmp;
  7490. + if (tmp.hd_dentry) {
  7491. + bindex--;
  7492. + p--;
  7493. + }
  7494. + }
  7495. +
  7496. + dinfo->di_bwh = -1;
  7497. + if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
  7498. + dinfo->di_bwh = bwh;
  7499. +
  7500. + dinfo->di_bdiropq = -1;
  7501. + if (bdiropq >= 0
  7502. + && bdiropq <= au_sbbot(sb)
  7503. + && au_sbr_whable(sb, bdiropq))
  7504. + dinfo->di_bdiropq = bdiropq;
  7505. +
  7506. + err = -EIO;
  7507. + dinfo->di_btop = -1;
  7508. + dinfo->di_bbot = -1;
  7509. + bbot = au_dbbot(parent);
  7510. + bindex = 0;
  7511. + p = au_hdentry(dinfo, bindex);
  7512. + for (; bindex <= bbot; bindex++, p++)
  7513. + if (p->hd_dentry) {
  7514. + dinfo->di_btop = bindex;
  7515. + break;
  7516. + }
  7517. +
  7518. + if (dinfo->di_btop >= 0) {
  7519. + bindex = bbot;
  7520. + p = au_hdentry(dinfo, bindex);
  7521. + for (; bindex >= 0; bindex--, p--)
  7522. + if (p->hd_dentry) {
  7523. + dinfo->di_bbot = bindex;
  7524. + err = 0;
  7525. + break;
  7526. + }
  7527. + }
  7528. +
  7529. + return err;
  7530. +}
  7531. +
  7532. +static void au_do_hide(struct dentry *dentry)
  7533. +{
  7534. + struct inode *inode;
  7535. +
  7536. + if (d_really_is_positive(dentry)) {
  7537. + inode = d_inode(dentry);
  7538. + if (!d_is_dir(dentry)) {
  7539. + if (inode->i_nlink && !d_unhashed(dentry))
  7540. + drop_nlink(inode);
  7541. + } else {
  7542. + clear_nlink(inode);
  7543. + /* stop next lookup */
  7544. + inode->i_flags |= S_DEAD;
  7545. + }
  7546. + smp_mb(); /* necessary? */
  7547. + }
  7548. + d_drop(dentry);
  7549. +}
  7550. +
  7551. +static int au_hide_children(struct dentry *parent)
  7552. +{
  7553. + int err, i, j, ndentry;
  7554. + struct au_dcsub_pages dpages;
  7555. + struct au_dpage *dpage;
  7556. + struct dentry *dentry;
  7557. +
  7558. + err = au_dpages_init(&dpages, GFP_NOFS);
  7559. + if (unlikely(err))
  7560. + goto out;
  7561. + err = au_dcsub_pages(&dpages, parent, NULL, NULL);
  7562. + if (unlikely(err))
  7563. + goto out_dpages;
  7564. +
  7565. + /* in reverse order */
  7566. + for (i = dpages.ndpage - 1; i >= 0; i--) {
  7567. + dpage = dpages.dpages + i;
  7568. + ndentry = dpage->ndentry;
  7569. + for (j = ndentry - 1; j >= 0; j--) {
  7570. + dentry = dpage->dentries[j];
  7571. + if (dentry != parent)
  7572. + au_do_hide(dentry);
  7573. + }
  7574. + }
  7575. +
  7576. +out_dpages:
  7577. + au_dpages_free(&dpages);
  7578. +out:
  7579. + return err;
  7580. +}
  7581. +
  7582. +static void au_hide(struct dentry *dentry)
  7583. +{
  7584. + int err;
  7585. +
  7586. + AuDbgDentry(dentry);
  7587. + if (d_is_dir(dentry)) {
  7588. + /* shrink_dcache_parent(dentry); */
  7589. + err = au_hide_children(dentry);
  7590. + if (unlikely(err))
  7591. + AuIOErr("%pd, failed hiding children, ignored %d\n",
  7592. + dentry, err);
  7593. + }
  7594. + au_do_hide(dentry);
  7595. +}
  7596. +
  7597. +/*
  7598. + * By adding a dirty branch, a cached dentry may be affected in various ways.
  7599. + *
  7600. + * a dirty branch is added
  7601. + * - on the top of layers
  7602. + * - in the middle of layers
  7603. + * - to the bottom of layers
  7604. + *
  7605. + * on the added branch there exists
  7606. + * - a whiteout
  7607. + * - a diropq
  7608. + * - a same named entry
  7609. + * + exist
  7610. + * * negative --> positive
  7611. + * * positive --> positive
  7612. + * - type is unchanged
  7613. + * - type is changed
  7614. + * + doesn't exist
  7615. + * * negative --> negative
  7616. + * * positive --> negative (rejected by au_br_del() for non-dir case)
  7617. + * - none
  7618. + */
  7619. +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
  7620. + struct au_dinfo *tmp)
  7621. +{
  7622. + int err;
  7623. + aufs_bindex_t bindex, bbot;
  7624. + struct {
  7625. + struct dentry *dentry;
  7626. + struct inode *inode;
  7627. + mode_t mode;
  7628. + } orig_h, tmp_h = {
  7629. + .dentry = NULL
  7630. + };
  7631. + struct au_hdentry *hd;
  7632. + struct inode *inode, *h_inode;
  7633. + struct dentry *h_dentry;
  7634. +
  7635. + err = 0;
  7636. + AuDebugOn(dinfo->di_btop < 0);
  7637. + orig_h.mode = 0;
  7638. + orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
  7639. + orig_h.inode = NULL;
  7640. + if (d_is_positive(orig_h.dentry)) {
  7641. + orig_h.inode = d_inode(orig_h.dentry);
  7642. + orig_h.mode = orig_h.inode->i_mode & S_IFMT;
  7643. + }
  7644. + if (tmp->di_btop >= 0) {
  7645. + tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
  7646. + if (d_is_positive(tmp_h.dentry)) {
  7647. + tmp_h.inode = d_inode(tmp_h.dentry);
  7648. + tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
  7649. + }
  7650. + }
  7651. +
  7652. + inode = NULL;
  7653. + if (d_really_is_positive(dentry))
  7654. + inode = d_inode(dentry);
  7655. + if (!orig_h.inode) {
  7656. + AuDbg("negative originally\n");
  7657. + if (inode) {
  7658. + au_hide(dentry);
  7659. + goto out;
  7660. + }
  7661. + AuDebugOn(inode);
  7662. + AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
  7663. + AuDebugOn(dinfo->di_bdiropq != -1);
  7664. +
  7665. + if (!tmp_h.inode) {
  7666. + AuDbg("negative --> negative\n");
  7667. + /* should have only one negative lower */
  7668. + if (tmp->di_btop >= 0
  7669. + && tmp->di_btop < dinfo->di_btop) {
  7670. + AuDebugOn(tmp->di_btop != tmp->di_bbot);
  7671. + AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
  7672. + au_set_h_dptr(dentry, dinfo->di_btop, NULL);
  7673. + au_di_cp(dinfo, tmp);
  7674. + hd = au_hdentry(tmp, tmp->di_btop);
  7675. + au_set_h_dptr(dentry, tmp->di_btop,
  7676. + dget(hd->hd_dentry));
  7677. + }
  7678. + au_dbg_verify_dinode(dentry);
  7679. + } else {
  7680. + AuDbg("negative --> positive\n");
  7681. + /*
  7682. + * similar to the behaviour of creating with bypassing
  7683. + * aufs.
  7684. + * unhash it in order to force an error in the
  7685. + * succeeding create operation.
  7686. + * we should not set S_DEAD here.
  7687. + */
  7688. + d_drop(dentry);
  7689. + /* au_di_swap(tmp, dinfo); */
  7690. + au_dbg_verify_dinode(dentry);
  7691. + }
  7692. + } else {
  7693. + AuDbg("positive originally\n");
  7694. + /* inode may be NULL */
  7695. + AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
  7696. + if (!tmp_h.inode) {
  7697. + AuDbg("positive --> negative\n");
  7698. + /* or bypassing aufs */
  7699. + au_hide(dentry);
  7700. + if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
  7701. + dinfo->di_bwh = tmp->di_bwh;
  7702. + if (inode)
  7703. + err = au_refresh_hinode_self(inode);
  7704. + au_dbg_verify_dinode(dentry);
  7705. + } else if (orig_h.mode == tmp_h.mode) {
  7706. + AuDbg("positive --> positive, same type\n");
  7707. + if (!S_ISDIR(orig_h.mode)
  7708. + && dinfo->di_btop > tmp->di_btop) {
  7709. + /*
  7710. + * similar to the behaviour of removing and
  7711. + * creating.
  7712. + */
  7713. + au_hide(dentry);
  7714. + if (inode)
  7715. + err = au_refresh_hinode_self(inode);
  7716. + au_dbg_verify_dinode(dentry);
  7717. + } else {
  7718. + /* fill empty slots */
  7719. + if (dinfo->di_btop > tmp->di_btop)
  7720. + dinfo->di_btop = tmp->di_btop;
  7721. + if (dinfo->di_bbot < tmp->di_bbot)
  7722. + dinfo->di_bbot = tmp->di_bbot;
  7723. + dinfo->di_bwh = tmp->di_bwh;
  7724. + dinfo->di_bdiropq = tmp->di_bdiropq;
  7725. + bbot = dinfo->di_bbot;
  7726. + bindex = tmp->di_btop;
  7727. + hd = au_hdentry(tmp, bindex);
  7728. + for (; bindex <= bbot; bindex++, hd++) {
  7729. + if (au_h_dptr(dentry, bindex))
  7730. + continue;
  7731. + h_dentry = hd->hd_dentry;
  7732. + if (!h_dentry)
  7733. + continue;
  7734. + AuDebugOn(d_is_negative(h_dentry));
  7735. + h_inode = d_inode(h_dentry);
  7736. + AuDebugOn(orig_h.mode
  7737. + != (h_inode->i_mode
  7738. + & S_IFMT));
  7739. + au_set_h_dptr(dentry, bindex,
  7740. + dget(h_dentry));
  7741. + }
  7742. + if (inode)
  7743. + err = au_refresh_hinode(inode, dentry);
  7744. + au_dbg_verify_dinode(dentry);
  7745. + }
  7746. + } else {
  7747. + AuDbg("positive --> positive, different type\n");
  7748. + /* similar to the behaviour of removing and creating */
  7749. + au_hide(dentry);
  7750. + if (inode)
  7751. + err = au_refresh_hinode_self(inode);
  7752. + au_dbg_verify_dinode(dentry);
  7753. + }
  7754. + }
  7755. +
  7756. +out:
  7757. + return err;
  7758. +}
  7759. +
  7760. +void au_refresh_dop(struct dentry *dentry, int force_reval)
  7761. +{
  7762. + const struct dentry_operations *dop
  7763. + = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
  7764. + static const unsigned int mask
  7765. + = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
  7766. +
  7767. + BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
  7768. +
  7769. + if (dentry->d_op == dop)
  7770. + return;
  7771. +
  7772. + AuDbg("%pd\n", dentry);
  7773. + spin_lock(&dentry->d_lock);
  7774. + if (dop == &aufs_dop)
  7775. + dentry->d_flags |= mask;
  7776. + else
  7777. + dentry->d_flags &= ~mask;
  7778. + dentry->d_op = dop;
  7779. + spin_unlock(&dentry->d_lock);
  7780. +}
  7781. +
  7782. +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
  7783. +{
  7784. + int err, ebrange, nbr;
  7785. + unsigned int sigen;
  7786. + struct au_dinfo *dinfo, *tmp;
  7787. + struct super_block *sb;
  7788. + struct inode *inode;
  7789. +
  7790. + DiMustWriteLock(dentry);
  7791. + AuDebugOn(IS_ROOT(dentry));
  7792. + AuDebugOn(d_really_is_negative(parent));
  7793. +
  7794. + sb = dentry->d_sb;
  7795. + sigen = au_sigen(sb);
  7796. + err = au_digen_test(parent, sigen);
  7797. + if (unlikely(err))
  7798. + goto out;
  7799. +
  7800. + nbr = au_sbbot(sb) + 1;
  7801. + dinfo = au_di(dentry);
  7802. + err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
  7803. + if (unlikely(err))
  7804. + goto out;
  7805. + ebrange = au_dbrange_test(dentry);
  7806. + if (!ebrange)
  7807. + ebrange = au_do_refresh_hdentry(dentry, parent);
  7808. +
  7809. + if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
  7810. + AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
  7811. + if (d_really_is_positive(dentry)) {
  7812. + inode = d_inode(dentry);
  7813. + err = au_refresh_hinode_self(inode);
  7814. + }
  7815. + au_dbg_verify_dinode(dentry);
  7816. + if (!err)
  7817. + goto out_dgen; /* success */
  7818. + goto out;
  7819. + }
  7820. +
  7821. + /* temporary dinfo */
  7822. + AuDbgDentry(dentry);
  7823. + err = -ENOMEM;
  7824. + tmp = au_di_alloc(sb, AuLsc_DI_TMP);
  7825. + if (unlikely(!tmp))
  7826. + goto out;
  7827. + au_di_swap(tmp, dinfo);
  7828. + /* returns the number of positive dentries */
  7829. + /*
  7830. + * if current working dir is removed, it returns an error.
  7831. + * but the dentry is legal.
  7832. + */
  7833. + err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
  7834. + AuDbgDentry(dentry);
  7835. + au_di_swap(tmp, dinfo);
  7836. + if (err == -ENOENT)
  7837. + err = 0;
  7838. + if (err >= 0) {
  7839. + /* compare/refresh by dinfo */
  7840. + AuDbgDentry(dentry);
  7841. + err = au_refresh_by_dinfo(dentry, dinfo, tmp);
  7842. + au_dbg_verify_dinode(dentry);
  7843. + AuTraceErr(err);
  7844. + }
  7845. + au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
  7846. + au_rw_write_unlock(&tmp->di_rwsem);
  7847. + au_di_free(tmp);
  7848. + if (unlikely(err))
  7849. + goto out;
  7850. +
  7851. +out_dgen:
  7852. + au_update_digen(dentry);
  7853. +out:
  7854. + if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
  7855. + AuIOErr("failed refreshing %pd, %d\n", dentry, err);
  7856. + AuDbgDentry(dentry);
  7857. + }
  7858. + AuTraceErr(err);
  7859. + return err;
  7860. +}
  7861. +
  7862. +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
  7863. + struct dentry *dentry, aufs_bindex_t bindex)
  7864. +{
  7865. + int err, valid;
  7866. +
  7867. + err = 0;
  7868. + if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
  7869. + goto out;
  7870. +
  7871. + AuDbg("b%d\n", bindex);
  7872. + /*
  7873. + * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
  7874. + * due to whiteout and branch permission.
  7875. + */
  7876. + flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
  7877. + | LOOKUP_FOLLOW | LOOKUP_EXCL);
  7878. + /* it may return tri-state */
  7879. + valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
  7880. +
  7881. + if (unlikely(valid < 0))
  7882. + err = valid;
  7883. + else if (!valid)
  7884. + err = -EINVAL;
  7885. +
  7886. +out:
  7887. + AuTraceErr(err);
  7888. + return err;
  7889. +}
  7890. +
  7891. +/* todo: remove this */
  7892. +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
  7893. + unsigned int flags, int do_udba, int dirren)
  7894. +{
  7895. + int err;
  7896. + umode_t mode, h_mode;
  7897. + aufs_bindex_t bindex, btail, btop, ibs, ibe;
  7898. + unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
  7899. + struct inode *h_inode, *h_cached_inode;
  7900. + struct dentry *h_dentry;
  7901. + struct qstr *name, *h_name;
  7902. +
  7903. + err = 0;
  7904. + plus = 0;
  7905. + mode = 0;
  7906. + ibs = -1;
  7907. + ibe = -1;
  7908. + unhashed = !!d_unhashed(dentry);
  7909. + is_root = !!IS_ROOT(dentry);
  7910. + name = &dentry->d_name;
  7911. + tmpfile = au_di(dentry)->di_tmpfile;
  7912. +
  7913. + /*
  7914. + * Theoretically, REVAL test should be unnecessary in case of
  7915. + * {FS,I}NOTIFY.
  7916. + * But {fs,i}notify doesn't fire some necessary events,
  7917. + * IN_ATTRIB for atime/nlink/pageio
  7918. + * Let's do REVAL test too.
  7919. + */
  7920. + if (do_udba && inode) {
  7921. + mode = (inode->i_mode & S_IFMT);
  7922. + plus = (inode->i_nlink > 0);
  7923. + ibs = au_ibtop(inode);
  7924. + ibe = au_ibbot(inode);
  7925. + }
  7926. +
  7927. + btop = au_dbtop(dentry);
  7928. + btail = btop;
  7929. + if (inode && S_ISDIR(inode->i_mode))
  7930. + btail = au_dbtaildir(dentry);
  7931. + for (bindex = btop; bindex <= btail; bindex++) {
  7932. + h_dentry = au_h_dptr(dentry, bindex);
  7933. + if (!h_dentry)
  7934. + continue;
  7935. +
  7936. + AuDbg("b%d, %pd\n", bindex, h_dentry);
  7937. + h_nfs = !!au_test_nfs(h_dentry->d_sb);
  7938. + spin_lock(&h_dentry->d_lock);
  7939. + h_name = &h_dentry->d_name;
  7940. + if (unlikely(do_udba
  7941. + && !is_root
  7942. + && ((!h_nfs
  7943. + && (unhashed != !!d_unhashed(h_dentry)
  7944. + || (!tmpfile && !dirren
  7945. + && !au_qstreq(name, h_name))
  7946. + ))
  7947. + || (h_nfs
  7948. + && !(flags & LOOKUP_OPEN)
  7949. + && (h_dentry->d_flags
  7950. + & DCACHE_NFSFS_RENAMED)))
  7951. + )) {
  7952. + int h_unhashed;
  7953. +
  7954. + h_unhashed = d_unhashed(h_dentry);
  7955. + spin_unlock(&h_dentry->d_lock);
  7956. + AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
  7957. + unhashed, h_unhashed, dentry, h_dentry);
  7958. + goto err;
  7959. + }
  7960. + spin_unlock(&h_dentry->d_lock);
  7961. +
  7962. + err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
  7963. + if (unlikely(err))
  7964. + /* do not goto err, to keep the errno */
  7965. + break;
  7966. +
  7967. + /* todo: plink too? */
  7968. + if (!do_udba)
  7969. + continue;
  7970. +
  7971. + /* UDBA tests */
  7972. + if (unlikely(!!inode != d_is_positive(h_dentry)))
  7973. + goto err;
  7974. +
  7975. + h_inode = NULL;
  7976. + if (d_is_positive(h_dentry))
  7977. + h_inode = d_inode(h_dentry);
  7978. + h_plus = plus;
  7979. + h_mode = mode;
  7980. + h_cached_inode = h_inode;
  7981. + if (h_inode) {
  7982. + h_mode = (h_inode->i_mode & S_IFMT);
  7983. + h_plus = (h_inode->i_nlink > 0);
  7984. + }
  7985. + if (inode && ibs <= bindex && bindex <= ibe)
  7986. + h_cached_inode = au_h_iptr(inode, bindex);
  7987. +
  7988. + if (!h_nfs) {
  7989. + if (unlikely(plus != h_plus && !tmpfile))
  7990. + goto err;
  7991. + } else {
  7992. + if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
  7993. + && !is_root
  7994. + && !IS_ROOT(h_dentry)
  7995. + && unhashed != d_unhashed(h_dentry)))
  7996. + goto err;
  7997. + }
  7998. + if (unlikely(mode != h_mode
  7999. + || h_cached_inode != h_inode))
  8000. + goto err;
  8001. + continue;
  8002. +
  8003. +err:
  8004. + err = -EINVAL;
  8005. + break;
  8006. + }
  8007. +
  8008. + AuTraceErr(err);
  8009. + return err;
  8010. +}
  8011. +
  8012. +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
  8013. +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
  8014. +{
  8015. + int err;
  8016. + struct dentry *parent;
  8017. +
  8018. + if (!au_digen_test(dentry, sigen))
  8019. + return 0;
  8020. +
  8021. + parent = dget_parent(dentry);
  8022. + di_read_lock_parent(parent, AuLock_IR);
  8023. + AuDebugOn(au_digen_test(parent, sigen));
  8024. + au_dbg_verify_gen(parent, sigen);
  8025. + err = au_refresh_dentry(dentry, parent);
  8026. + di_read_unlock(parent, AuLock_IR);
  8027. + dput(parent);
  8028. + AuTraceErr(err);
  8029. + return err;
  8030. +}
  8031. +
  8032. +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
  8033. +{
  8034. + int err;
  8035. + struct dentry *d, *parent;
  8036. +
  8037. + if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
  8038. + return simple_reval_dpath(dentry, sigen);
  8039. +
  8040. + /* slow loop, keep it simple and stupid */
  8041. + /* cf: au_cpup_dirs() */
  8042. + err = 0;
  8043. + parent = NULL;
  8044. + while (au_digen_test(dentry, sigen)) {
  8045. + d = dentry;
  8046. + while (1) {
  8047. + dput(parent);
  8048. + parent = dget_parent(d);
  8049. + if (!au_digen_test(parent, sigen))
  8050. + break;
  8051. + d = parent;
  8052. + }
  8053. +
  8054. + if (d != dentry)
  8055. + di_write_lock_child2(d);
  8056. +
  8057. + /* someone might update our dentry while we were sleeping */
  8058. + if (au_digen_test(d, sigen)) {
  8059. + /*
  8060. + * todo: consolidate with simple_reval_dpath(),
  8061. + * do_refresh() and au_reval_for_attr().
  8062. + */
  8063. + di_read_lock_parent(parent, AuLock_IR);
  8064. + err = au_refresh_dentry(d, parent);
  8065. + di_read_unlock(parent, AuLock_IR);
  8066. + }
  8067. +
  8068. + if (d != dentry)
  8069. + di_write_unlock(d);
  8070. + dput(parent);
  8071. + if (unlikely(err))
  8072. + break;
  8073. + }
  8074. +
  8075. + return err;
  8076. +}
  8077. +
  8078. +/*
  8079. + * if valid returns 1, otherwise 0.
  8080. + */
  8081. +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
  8082. +{
  8083. + int valid, err;
  8084. + unsigned int sigen;
  8085. + unsigned char do_udba, dirren;
  8086. + struct super_block *sb;
  8087. + struct inode *inode;
  8088. +
  8089. + /* todo: support rcu-walk? */
  8090. + if (flags & LOOKUP_RCU)
  8091. + return -ECHILD;
  8092. +
  8093. + valid = 0;
  8094. + if (unlikely(!au_di(dentry)))
  8095. + goto out;
  8096. +
  8097. + valid = 1;
  8098. + sb = dentry->d_sb;
  8099. + /*
  8100. + * todo: very ugly
  8101. + * i_mutex of parent dir may be held,
  8102. + * but we should not return 'invalid' due to busy.
  8103. + */
  8104. + err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
  8105. + if (unlikely(err)) {
  8106. + valid = err;
  8107. + AuTraceErr(err);
  8108. + goto out;
  8109. + }
  8110. + inode = NULL;
  8111. + if (d_really_is_positive(dentry))
  8112. + inode = d_inode(dentry);
  8113. + if (unlikely(inode && au_is_bad_inode(inode))) {
  8114. + err = -EINVAL;
  8115. + AuTraceErr(err);
  8116. + goto out_dgrade;
  8117. + }
  8118. + if (unlikely(au_dbrange_test(dentry))) {
  8119. + err = -EINVAL;
  8120. + AuTraceErr(err);
  8121. + goto out_dgrade;
  8122. + }
  8123. +
  8124. + sigen = au_sigen(sb);
  8125. + if (au_digen_test(dentry, sigen)) {
  8126. + AuDebugOn(IS_ROOT(dentry));
  8127. + err = au_reval_dpath(dentry, sigen);
  8128. + if (unlikely(err)) {
  8129. + AuTraceErr(err);
  8130. + goto out_dgrade;
  8131. + }
  8132. + }
  8133. + di_downgrade_lock(dentry, AuLock_IR);
  8134. +
  8135. + err = -EINVAL;
  8136. + if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
  8137. + && inode
  8138. + && !(inode->i_state && I_LINKABLE)
  8139. + && (IS_DEADDIR(inode) || !inode->i_nlink)) {
  8140. + AuTraceErr(err);
  8141. + goto out_inval;
  8142. + }
  8143. +
  8144. + do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
  8145. + if (do_udba && inode) {
  8146. + aufs_bindex_t btop = au_ibtop(inode);
  8147. + struct inode *h_inode;
  8148. +
  8149. + if (btop >= 0) {
  8150. + h_inode = au_h_iptr(inode, btop);
  8151. + if (h_inode && au_test_higen(inode, h_inode)) {
  8152. + AuTraceErr(err);
  8153. + goto out_inval;
  8154. + }
  8155. + }
  8156. + }
  8157. +
  8158. + dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
  8159. + err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
  8160. + if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
  8161. + err = -EIO;
  8162. + AuDbg("both of real entry and whiteout found, %p, err %d\n",
  8163. + dentry, err);
  8164. + }
  8165. + goto out_inval;
  8166. +
  8167. +out_dgrade:
  8168. + di_downgrade_lock(dentry, AuLock_IR);
  8169. +out_inval:
  8170. + aufs_read_unlock(dentry, AuLock_IR);
  8171. + AuTraceErr(err);
  8172. + valid = !err;
  8173. +out:
  8174. + if (!valid) {
  8175. + AuDbg("%pd invalid, %d\n", dentry, valid);
  8176. + d_drop(dentry);
  8177. + }
  8178. + return valid;
  8179. +}
  8180. +
  8181. +static void aufs_d_release(struct dentry *dentry)
  8182. +{
  8183. + if (au_di(dentry)) {
  8184. + au_di_fin(dentry);
  8185. + au_hn_di_reinit(dentry);
  8186. + }
  8187. +}
  8188. +
  8189. +const struct dentry_operations aufs_dop = {
  8190. + .d_revalidate = aufs_d_revalidate,
  8191. + .d_weak_revalidate = aufs_d_revalidate,
  8192. + .d_release = aufs_d_release
  8193. +};
  8194. +
  8195. +/* aufs_dop without d_revalidate */
  8196. +const struct dentry_operations aufs_dop_noreval = {
  8197. + .d_release = aufs_d_release
  8198. +};
  8199. diff -Naur null/fs/aufs/dentry.h linux-5.15.131/fs/aufs/dentry.h
  8200. --- /dev/null
  8201. +++ linux-5.15.131/fs/aufs/dentry.h 2023-09-11 14:32:48.146722577 +0300
  8202. @@ -0,0 +1,269 @@
  8203. +/* SPDX-License-Identifier: GPL-2.0 */
  8204. +/*
  8205. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  8206. + *
  8207. + * This program, aufs is free software; you can redistribute it and/or modify
  8208. + * it under the terms of the GNU General Public License as published by
  8209. + * the Free Software Foundation; either version 2 of the License, or
  8210. + * (at your option) any later version.
  8211. + *
  8212. + * This program is distributed in the hope that it will be useful,
  8213. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8214. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8215. + * GNU General Public License for more details.
  8216. + *
  8217. + * You should have received a copy of the GNU General Public License
  8218. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  8219. + */
  8220. +
  8221. +/*
  8222. + * lookup and dentry operations
  8223. + */
  8224. +
  8225. +#ifndef __AUFS_DENTRY_H__
  8226. +#define __AUFS_DENTRY_H__
  8227. +
  8228. +#ifdef __KERNEL__
  8229. +
  8230. +#include <linux/dcache.h>
  8231. +#include "dirren.h"
  8232. +#include "rwsem.h"
  8233. +
  8234. +struct au_hdentry {
  8235. + struct dentry *hd_dentry;
  8236. + aufs_bindex_t hd_id;
  8237. +};
  8238. +
  8239. +struct au_dinfo {
  8240. + atomic_t di_generation;
  8241. +
  8242. + struct au_rwsem di_rwsem;
  8243. + aufs_bindex_t di_btop, di_bbot, di_bwh, di_bdiropq;
  8244. + unsigned char di_tmpfile; /* to allow the different name */
  8245. + struct au_hdentry *di_hdentry;
  8246. + struct rcu_head rcu;
  8247. +} ____cacheline_aligned_in_smp;
  8248. +
  8249. +/* ---------------------------------------------------------------------- */
  8250. +
  8251. +/* flags for au_lkup_dentry() */
  8252. +#define AuLkup_ALLOW_NEG 1
  8253. +#define AuLkup_IGNORE_PERM (1 << 1)
  8254. +#define AuLkup_DIRREN (1 << 2)
  8255. +#define au_ftest_lkup(flags, name) ((flags) & AuLkup_##name)
  8256. +#define au_fset_lkup(flags, name) \
  8257. + do { (flags) |= AuLkup_##name; } while (0)
  8258. +#define au_fclr_lkup(flags, name) \
  8259. + do { (flags) &= ~AuLkup_##name; } while (0)
  8260. +
  8261. +#ifndef CONFIG_AUFS_DIRREN
  8262. +#undef AuLkup_DIRREN
  8263. +#define AuLkup_DIRREN 0
  8264. +#endif
  8265. +
  8266. +struct au_do_lookup_args {
  8267. + unsigned int flags;
  8268. + mode_t type;
  8269. + struct qstr whname, *name;
  8270. + struct au_dr_lookup dirren;
  8271. +};
  8272. +
  8273. +/* ---------------------------------------------------------------------- */
  8274. +
  8275. +/* dentry.c */
  8276. +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
  8277. +struct au_branch;
  8278. +struct dentry *au_sio_lkup_one(struct user_namespace *userns, struct qstr *name,
  8279. + struct path *ppath);
  8280. +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
  8281. + struct dentry *h_parent, struct au_branch *br);
  8282. +
  8283. +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
  8284. + unsigned int flags);
  8285. +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
  8286. +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
  8287. +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
  8288. +void au_refresh_dop(struct dentry *dentry, int force_reval);
  8289. +
  8290. +/* dinfo.c */
  8291. +void au_di_init_once(void *_di);
  8292. +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
  8293. +void au_di_free(struct au_dinfo *dinfo);
  8294. +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
  8295. +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
  8296. +int au_di_init(struct dentry *dentry);
  8297. +void au_di_fin(struct dentry *dentry);
  8298. +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
  8299. +
  8300. +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
  8301. +void di_read_unlock(struct dentry *d, int flags);
  8302. +void di_downgrade_lock(struct dentry *d, int flags);
  8303. +void di_write_lock(struct dentry *d, unsigned int lsc);
  8304. +void di_write_unlock(struct dentry *d);
  8305. +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
  8306. +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
  8307. +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
  8308. +
  8309. +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
  8310. +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
  8311. +aufs_bindex_t au_dbtail(struct dentry *dentry);
  8312. +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
  8313. +
  8314. +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
  8315. + struct dentry *h_dentry);
  8316. +int au_digen_test(struct dentry *dentry, unsigned int sigen);
  8317. +int au_dbrange_test(struct dentry *dentry);
  8318. +void au_update_digen(struct dentry *dentry);
  8319. +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
  8320. +void au_update_dbtop(struct dentry *dentry);
  8321. +void au_update_dbbot(struct dentry *dentry);
  8322. +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
  8323. +
  8324. +/* ---------------------------------------------------------------------- */
  8325. +
  8326. +static inline struct au_dinfo *au_di(struct dentry *dentry)
  8327. +{
  8328. + return dentry->d_fsdata;
  8329. +}
  8330. +
  8331. +/* ---------------------------------------------------------------------- */
  8332. +
  8333. +/* lock subclass for dinfo */
  8334. +enum {
  8335. + AuLsc_DI_CHILD, /* child first */
  8336. + AuLsc_DI_CHILD2, /* rename(2), link(2), and cpup at hnotify */
  8337. + AuLsc_DI_CHILD3, /* copyup dirs */
  8338. + AuLsc_DI_PARENT,
  8339. + AuLsc_DI_PARENT2,
  8340. + AuLsc_DI_PARENT3,
  8341. + AuLsc_DI_TMP /* temp for replacing dinfo */
  8342. +};
  8343. +
  8344. +/*
  8345. + * di_read_lock_child, di_write_lock_child,
  8346. + * di_read_lock_child2, di_write_lock_child2,
  8347. + * di_read_lock_child3, di_write_lock_child3,
  8348. + * di_read_lock_parent, di_write_lock_parent,
  8349. + * di_read_lock_parent2, di_write_lock_parent2,
  8350. + * di_read_lock_parent3, di_write_lock_parent3,
  8351. + */
  8352. +#define AuReadLockFunc(name, lsc) \
  8353. +static inline void di_read_lock_##name(struct dentry *d, int flags) \
  8354. +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
  8355. +
  8356. +#define AuWriteLockFunc(name, lsc) \
  8357. +static inline void di_write_lock_##name(struct dentry *d) \
  8358. +{ di_write_lock(d, AuLsc_DI_##lsc); }
  8359. +
  8360. +#define AuRWLockFuncs(name, lsc) \
  8361. + AuReadLockFunc(name, lsc) \
  8362. + AuWriteLockFunc(name, lsc)
  8363. +
  8364. +AuRWLockFuncs(child, CHILD);
  8365. +AuRWLockFuncs(child2, CHILD2);
  8366. +AuRWLockFuncs(child3, CHILD3);
  8367. +AuRWLockFuncs(parent, PARENT);
  8368. +AuRWLockFuncs(parent2, PARENT2);
  8369. +AuRWLockFuncs(parent3, PARENT3);
  8370. +
  8371. +#undef AuReadLockFunc
  8372. +#undef AuWriteLockFunc
  8373. +#undef AuRWLockFuncs
  8374. +
  8375. +#define DiMustNoWaiters(d) AuRwMustNoWaiters(&au_di(d)->di_rwsem)
  8376. +#define DiMustAnyLock(d) AuRwMustAnyLock(&au_di(d)->di_rwsem)
  8377. +#define DiMustWriteLock(d) AuRwMustWriteLock(&au_di(d)->di_rwsem)
  8378. +
  8379. +/* ---------------------------------------------------------------------- */
  8380. +
  8381. +/* todo: memory barrier? */
  8382. +static inline unsigned int au_digen(struct dentry *d)
  8383. +{
  8384. + return atomic_read(&au_di(d)->di_generation);
  8385. +}
  8386. +
  8387. +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
  8388. +{
  8389. + hdentry->hd_dentry = NULL;
  8390. +}
  8391. +
  8392. +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
  8393. + aufs_bindex_t bindex)
  8394. +{
  8395. + return di->di_hdentry + bindex;
  8396. +}
  8397. +
  8398. +static inline void au_hdput(struct au_hdentry *hd)
  8399. +{
  8400. + if (hd)
  8401. + dput(hd->hd_dentry);
  8402. +}
  8403. +
  8404. +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
  8405. +{
  8406. + DiMustAnyLock(dentry);
  8407. + return au_di(dentry)->di_btop;
  8408. +}
  8409. +
  8410. +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
  8411. +{
  8412. + DiMustAnyLock(dentry);
  8413. + return au_di(dentry)->di_bbot;
  8414. +}
  8415. +
  8416. +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
  8417. +{
  8418. + DiMustAnyLock(dentry);
  8419. + return au_di(dentry)->di_bwh;
  8420. +}
  8421. +
  8422. +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
  8423. +{
  8424. + DiMustAnyLock(dentry);
  8425. + return au_di(dentry)->di_bdiropq;
  8426. +}
  8427. +
  8428. +/* todo: hard/soft set? */
  8429. +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
  8430. +{
  8431. + DiMustWriteLock(dentry);
  8432. + au_di(dentry)->di_btop = bindex;
  8433. +}
  8434. +
  8435. +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
  8436. +{
  8437. + DiMustWriteLock(dentry);
  8438. + au_di(dentry)->di_bbot = bindex;
  8439. +}
  8440. +
  8441. +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
  8442. +{
  8443. + DiMustWriteLock(dentry);
  8444. + /* dbwh can be outside of btop - bbot range */
  8445. + au_di(dentry)->di_bwh = bindex;
  8446. +}
  8447. +
  8448. +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
  8449. +{
  8450. + DiMustWriteLock(dentry);
  8451. + au_di(dentry)->di_bdiropq = bindex;
  8452. +}
  8453. +
  8454. +/* ---------------------------------------------------------------------- */
  8455. +
  8456. +#ifdef CONFIG_AUFS_HNOTIFY
  8457. +static inline void au_digen_dec(struct dentry *d)
  8458. +{
  8459. + atomic_dec(&au_di(d)->di_generation);
  8460. +}
  8461. +
  8462. +static inline void au_hn_di_reinit(struct dentry *dentry)
  8463. +{
  8464. + dentry->d_fsdata = NULL;
  8465. +}
  8466. +#else
  8467. +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
  8468. +#endif /* CONFIG_AUFS_HNOTIFY */
  8469. +
  8470. +#endif /* __KERNEL__ */
  8471. +#endif /* __AUFS_DENTRY_H__ */
  8472. diff -Naur null/fs/aufs/dinfo.c linux-5.15.131/fs/aufs/dinfo.c
  8473. --- /dev/null
  8474. +++ linux-5.15.131/fs/aufs/dinfo.c 2023-09-11 14:32:48.146722577 +0300
  8475. @@ -0,0 +1,554 @@
  8476. +// SPDX-License-Identifier: GPL-2.0
  8477. +/*
  8478. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  8479. + *
  8480. + * This program, aufs is free software; you can redistribute it and/or modify
  8481. + * it under the terms of the GNU General Public License as published by
  8482. + * the Free Software Foundation; either version 2 of the License, or
  8483. + * (at your option) any later version.
  8484. + *
  8485. + * This program is distributed in the hope that it will be useful,
  8486. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8487. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8488. + * GNU General Public License for more details.
  8489. + *
  8490. + * You should have received a copy of the GNU General Public License
  8491. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  8492. + */
  8493. +
  8494. +/*
  8495. + * dentry private data
  8496. + */
  8497. +
  8498. +#include "aufs.h"
  8499. +
  8500. +void au_di_init_once(void *_dinfo)
  8501. +{
  8502. + struct au_dinfo *dinfo = _dinfo;
  8503. +
  8504. + au_rw_init(&dinfo->di_rwsem);
  8505. +}
  8506. +
  8507. +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
  8508. +{
  8509. + struct au_dinfo *dinfo;
  8510. + int nbr, i;
  8511. +
  8512. + dinfo = au_cache_alloc_dinfo();
  8513. + if (unlikely(!dinfo))
  8514. + goto out;
  8515. +
  8516. + nbr = au_sbbot(sb) + 1;
  8517. + if (nbr <= 0)
  8518. + nbr = 1;
  8519. + dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
  8520. + if (dinfo->di_hdentry) {
  8521. + au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
  8522. + dinfo->di_btop = -1;
  8523. + dinfo->di_bbot = -1;
  8524. + dinfo->di_bwh = -1;
  8525. + dinfo->di_bdiropq = -1;
  8526. + dinfo->di_tmpfile = 0;
  8527. + for (i = 0; i < nbr; i++)
  8528. + dinfo->di_hdentry[i].hd_id = -1;
  8529. + goto out;
  8530. + }
  8531. +
  8532. + au_cache_free_dinfo(dinfo);
  8533. + dinfo = NULL;
  8534. +
  8535. +out:
  8536. + return dinfo;
  8537. +}
  8538. +
  8539. +void au_di_free(struct au_dinfo *dinfo)
  8540. +{
  8541. + struct au_hdentry *p;
  8542. + aufs_bindex_t bbot, bindex;
  8543. +
  8544. + /* dentry may not be revalidated */
  8545. + bindex = dinfo->di_btop;
  8546. + if (bindex >= 0) {
  8547. + bbot = dinfo->di_bbot;
  8548. + p = au_hdentry(dinfo, bindex);
  8549. + while (bindex++ <= bbot)
  8550. + au_hdput(p++);
  8551. + }
  8552. + au_kfree_try_rcu(dinfo->di_hdentry);
  8553. + au_cache_free_dinfo(dinfo);
  8554. +}
  8555. +
  8556. +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
  8557. +{
  8558. + struct au_hdentry *p;
  8559. + aufs_bindex_t bi;
  8560. +
  8561. + AuRwMustWriteLock(&a->di_rwsem);
  8562. + AuRwMustWriteLock(&b->di_rwsem);
  8563. +
  8564. +#define DiSwap(v, name) \
  8565. + do { \
  8566. + v = a->di_##name; \
  8567. + a->di_##name = b->di_##name; \
  8568. + b->di_##name = v; \
  8569. + } while (0)
  8570. +
  8571. + DiSwap(p, hdentry);
  8572. + DiSwap(bi, btop);
  8573. + DiSwap(bi, bbot);
  8574. + DiSwap(bi, bwh);
  8575. + DiSwap(bi, bdiropq);
  8576. + /* smp_mb(); */
  8577. +
  8578. +#undef DiSwap
  8579. +}
  8580. +
  8581. +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
  8582. +{
  8583. + AuRwMustWriteLock(&dst->di_rwsem);
  8584. + AuRwMustWriteLock(&src->di_rwsem);
  8585. +
  8586. + dst->di_btop = src->di_btop;
  8587. + dst->di_bbot = src->di_bbot;
  8588. + dst->di_bwh = src->di_bwh;
  8589. + dst->di_bdiropq = src->di_bdiropq;
  8590. + /* smp_mb(); */
  8591. +}
  8592. +
  8593. +int au_di_init(struct dentry *dentry)
  8594. +{
  8595. + int err;
  8596. + struct super_block *sb;
  8597. + struct au_dinfo *dinfo;
  8598. +
  8599. + err = 0;
  8600. + sb = dentry->d_sb;
  8601. + dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
  8602. + if (dinfo) {
  8603. + atomic_set(&dinfo->di_generation, au_sigen(sb));
  8604. + /* smp_mb(); */ /* atomic_set */
  8605. + dentry->d_fsdata = dinfo;
  8606. + } else
  8607. + err = -ENOMEM;
  8608. +
  8609. + return err;
  8610. +}
  8611. +
  8612. +void au_di_fin(struct dentry *dentry)
  8613. +{
  8614. + struct au_dinfo *dinfo;
  8615. +
  8616. + dinfo = au_di(dentry);
  8617. + AuRwDestroy(&dinfo->di_rwsem);
  8618. + au_di_free(dinfo);
  8619. +}
  8620. +
  8621. +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
  8622. +{
  8623. + int err, sz;
  8624. + struct au_hdentry *hdp;
  8625. +
  8626. + AuRwMustWriteLock(&dinfo->di_rwsem);
  8627. +
  8628. + err = -ENOMEM;
  8629. + sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
  8630. + if (!sz)
  8631. + sz = sizeof(*hdp);
  8632. + hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
  8633. + may_shrink);
  8634. + if (hdp) {
  8635. + dinfo->di_hdentry = hdp;
  8636. + err = 0;
  8637. + }
  8638. +
  8639. + return err;
  8640. +}
  8641. +
  8642. +/* ---------------------------------------------------------------------- */
  8643. +
  8644. +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
  8645. +{
  8646. + switch (lsc) {
  8647. + case AuLsc_DI_CHILD:
  8648. + ii_write_lock_child(inode);
  8649. + break;
  8650. + case AuLsc_DI_CHILD2:
  8651. + ii_write_lock_child2(inode);
  8652. + break;
  8653. + case AuLsc_DI_CHILD3:
  8654. + ii_write_lock_child3(inode);
  8655. + break;
  8656. + case AuLsc_DI_PARENT:
  8657. + ii_write_lock_parent(inode);
  8658. + break;
  8659. + case AuLsc_DI_PARENT2:
  8660. + ii_write_lock_parent2(inode);
  8661. + break;
  8662. + case AuLsc_DI_PARENT3:
  8663. + ii_write_lock_parent3(inode);
  8664. + break;
  8665. + default:
  8666. + BUG();
  8667. + }
  8668. +}
  8669. +
  8670. +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
  8671. +{
  8672. + switch (lsc) {
  8673. + case AuLsc_DI_CHILD:
  8674. + ii_read_lock_child(inode);
  8675. + break;
  8676. + case AuLsc_DI_CHILD2:
  8677. + ii_read_lock_child2(inode);
  8678. + break;
  8679. + case AuLsc_DI_CHILD3:
  8680. + ii_read_lock_child3(inode);
  8681. + break;
  8682. + case AuLsc_DI_PARENT:
  8683. + ii_read_lock_parent(inode);
  8684. + break;
  8685. + case AuLsc_DI_PARENT2:
  8686. + ii_read_lock_parent2(inode);
  8687. + break;
  8688. + case AuLsc_DI_PARENT3:
  8689. + ii_read_lock_parent3(inode);
  8690. + break;
  8691. + default:
  8692. + BUG();
  8693. + }
  8694. +}
  8695. +
  8696. +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
  8697. +{
  8698. + struct inode *inode;
  8699. +
  8700. + au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
  8701. + if (d_really_is_positive(d)) {
  8702. + inode = d_inode(d);
  8703. + if (au_ftest_lock(flags, IW))
  8704. + do_ii_write_lock(inode, lsc);
  8705. + else if (au_ftest_lock(flags, IR))
  8706. + do_ii_read_lock(inode, lsc);
  8707. + }
  8708. +}
  8709. +
  8710. +void di_read_unlock(struct dentry *d, int flags)
  8711. +{
  8712. + struct inode *inode;
  8713. +
  8714. + if (d_really_is_positive(d)) {
  8715. + inode = d_inode(d);
  8716. + if (au_ftest_lock(flags, IW)) {
  8717. + au_dbg_verify_dinode(d);
  8718. + ii_write_unlock(inode);
  8719. + } else if (au_ftest_lock(flags, IR)) {
  8720. + au_dbg_verify_dinode(d);
  8721. + ii_read_unlock(inode);
  8722. + }
  8723. + }
  8724. + au_rw_read_unlock(&au_di(d)->di_rwsem);
  8725. +}
  8726. +
  8727. +void di_downgrade_lock(struct dentry *d, int flags)
  8728. +{
  8729. + if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
  8730. + ii_downgrade_lock(d_inode(d));
  8731. + au_rw_dgrade_lock(&au_di(d)->di_rwsem);
  8732. +}
  8733. +
  8734. +void di_write_lock(struct dentry *d, unsigned int lsc)
  8735. +{
  8736. + au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
  8737. + if (d_really_is_positive(d))
  8738. + do_ii_write_lock(d_inode(d), lsc);
  8739. +}
  8740. +
  8741. +void di_write_unlock(struct dentry *d)
  8742. +{
  8743. + au_dbg_verify_dinode(d);
  8744. + if (d_really_is_positive(d))
  8745. + ii_write_unlock(d_inode(d));
  8746. + au_rw_write_unlock(&au_di(d)->di_rwsem);
  8747. +}
  8748. +
  8749. +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
  8750. +{
  8751. + AuDebugOn(d1 == d2
  8752. + || d_inode(d1) == d_inode(d2)
  8753. + || d1->d_sb != d2->d_sb);
  8754. +
  8755. + if ((isdir && au_test_subdir(d1, d2))
  8756. + || d1 < d2) {
  8757. + di_write_lock_child(d1);
  8758. + di_write_lock_child2(d2);
  8759. + } else {
  8760. + di_write_lock_child(d2);
  8761. + di_write_lock_child2(d1);
  8762. + }
  8763. +}
  8764. +
  8765. +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
  8766. +{
  8767. + AuDebugOn(d1 == d2
  8768. + || d_inode(d1) == d_inode(d2)
  8769. + || d1->d_sb != d2->d_sb);
  8770. +
  8771. + if ((isdir && au_test_subdir(d1, d2))
  8772. + || d1 < d2) {
  8773. + di_write_lock_parent(d1);
  8774. + di_write_lock_parent2(d2);
  8775. + } else {
  8776. + di_write_lock_parent(d2);
  8777. + di_write_lock_parent2(d1);
  8778. + }
  8779. +}
  8780. +
  8781. +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
  8782. +{
  8783. + di_write_unlock(d1);
  8784. + if (d_inode(d1) == d_inode(d2))
  8785. + au_rw_write_unlock(&au_di(d2)->di_rwsem);
  8786. + else
  8787. + di_write_unlock(d2);
  8788. +}
  8789. +
  8790. +/* ---------------------------------------------------------------------- */
  8791. +
  8792. +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
  8793. +{
  8794. + struct dentry *d;
  8795. +
  8796. + DiMustAnyLock(dentry);
  8797. +
  8798. + if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
  8799. + return NULL;
  8800. + AuDebugOn(bindex < 0);
  8801. + d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
  8802. + AuDebugOn(d && au_dcount(d) <= 0);
  8803. + return d;
  8804. +}
  8805. +
  8806. +/*
  8807. + * extended version of au_h_dptr().
  8808. + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
  8809. + * error.
  8810. + */
  8811. +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
  8812. +{
  8813. + struct dentry *h_dentry;
  8814. + struct inode *inode, *h_inode;
  8815. +
  8816. + AuDebugOn(d_really_is_negative(dentry));
  8817. +
  8818. + h_dentry = NULL;
  8819. + if (au_dbtop(dentry) <= bindex
  8820. + && bindex <= au_dbbot(dentry))
  8821. + h_dentry = au_h_dptr(dentry, bindex);
  8822. + if (h_dentry && !au_d_linkable(h_dentry)) {
  8823. + dget(h_dentry);
  8824. + goto out; /* success */
  8825. + }
  8826. +
  8827. + inode = d_inode(dentry);
  8828. + AuDebugOn(bindex < au_ibtop(inode));
  8829. + AuDebugOn(au_ibbot(inode) < bindex);
  8830. + h_inode = au_h_iptr(inode, bindex);
  8831. + h_dentry = d_find_alias(h_inode);
  8832. + if (h_dentry) {
  8833. + if (!IS_ERR(h_dentry)) {
  8834. + if (!au_d_linkable(h_dentry))
  8835. + goto out; /* success */
  8836. + dput(h_dentry);
  8837. + } else
  8838. + goto out;
  8839. + }
  8840. +
  8841. + if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
  8842. + h_dentry = au_plink_lkup(inode, bindex);
  8843. + AuDebugOn(!h_dentry);
  8844. + if (!IS_ERR(h_dentry)) {
  8845. + if (!au_d_hashed_positive(h_dentry))
  8846. + goto out; /* success */
  8847. + dput(h_dentry);
  8848. + h_dentry = NULL;
  8849. + }
  8850. + }
  8851. +
  8852. +out:
  8853. + AuDbgDentry(h_dentry);
  8854. + return h_dentry;
  8855. +}
  8856. +
  8857. +aufs_bindex_t au_dbtail(struct dentry *dentry)
  8858. +{
  8859. + aufs_bindex_t bbot, bwh;
  8860. +
  8861. + bbot = au_dbbot(dentry);
  8862. + if (0 <= bbot) {
  8863. + bwh = au_dbwh(dentry);
  8864. + if (!bwh)
  8865. + return bwh;
  8866. + if (0 < bwh && bwh < bbot)
  8867. + return bwh - 1;
  8868. + }
  8869. + return bbot;
  8870. +}
  8871. +
  8872. +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
  8873. +{
  8874. + aufs_bindex_t bbot, bopq;
  8875. +
  8876. + bbot = au_dbtail(dentry);
  8877. + if (0 <= bbot) {
  8878. + bopq = au_dbdiropq(dentry);
  8879. + if (0 <= bopq && bopq < bbot)
  8880. + bbot = bopq;
  8881. + }
  8882. + return bbot;
  8883. +}
  8884. +
  8885. +/* ---------------------------------------------------------------------- */
  8886. +
  8887. +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
  8888. + struct dentry *h_dentry)
  8889. +{
  8890. + struct au_dinfo *dinfo;
  8891. + struct au_hdentry *hd;
  8892. + struct au_branch *br;
  8893. +
  8894. + DiMustWriteLock(dentry);
  8895. +
  8896. + dinfo = au_di(dentry);
  8897. + hd = au_hdentry(dinfo, bindex);
  8898. + au_hdput(hd);
  8899. + hd->hd_dentry = h_dentry;
  8900. + if (h_dentry) {
  8901. + br = au_sbr(dentry->d_sb, bindex);
  8902. + hd->hd_id = br->br_id;
  8903. + }
  8904. +}
  8905. +
  8906. +int au_dbrange_test(struct dentry *dentry)
  8907. +{
  8908. + int err;
  8909. + aufs_bindex_t btop, bbot;
  8910. +
  8911. + err = 0;
  8912. + btop = au_dbtop(dentry);
  8913. + bbot = au_dbbot(dentry);
  8914. + if (btop >= 0)
  8915. + AuDebugOn(bbot < 0 && btop > bbot);
  8916. + else {
  8917. + err = -EIO;
  8918. + AuDebugOn(bbot >= 0);
  8919. + }
  8920. +
  8921. + return err;
  8922. +}
  8923. +
  8924. +int au_digen_test(struct dentry *dentry, unsigned int sigen)
  8925. +{
  8926. + int err;
  8927. +
  8928. + err = 0;
  8929. + if (unlikely(au_digen(dentry) != sigen
  8930. + || au_iigen_test(d_inode(dentry), sigen)))
  8931. + err = -EIO;
  8932. +
  8933. + return err;
  8934. +}
  8935. +
  8936. +void au_update_digen(struct dentry *dentry)
  8937. +{
  8938. + atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
  8939. + /* smp_mb(); */ /* atomic_set */
  8940. +}
  8941. +
  8942. +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
  8943. +{
  8944. + struct au_dinfo *dinfo;
  8945. + struct dentry *h_d;
  8946. + struct au_hdentry *hdp;
  8947. + aufs_bindex_t bindex, bbot;
  8948. +
  8949. + DiMustWriteLock(dentry);
  8950. +
  8951. + dinfo = au_di(dentry);
  8952. + if (!dinfo || dinfo->di_btop < 0)
  8953. + return;
  8954. +
  8955. + if (do_put_zero) {
  8956. + bbot = dinfo->di_bbot;
  8957. + bindex = dinfo->di_btop;
  8958. + hdp = au_hdentry(dinfo, bindex);
  8959. + for (; bindex <= bbot; bindex++, hdp++) {
  8960. + h_d = hdp->hd_dentry;
  8961. + if (h_d && d_is_negative(h_d))
  8962. + au_set_h_dptr(dentry, bindex, NULL);
  8963. + }
  8964. + }
  8965. +
  8966. + dinfo->di_btop = 0;
  8967. + hdp = au_hdentry(dinfo, dinfo->di_btop);
  8968. + for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
  8969. + if (hdp->hd_dentry)
  8970. + break;
  8971. + if (dinfo->di_btop > dinfo->di_bbot) {
  8972. + dinfo->di_btop = -1;
  8973. + dinfo->di_bbot = -1;
  8974. + return;
  8975. + }
  8976. +
  8977. + hdp = au_hdentry(dinfo, dinfo->di_bbot);
  8978. + for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
  8979. + if (hdp->hd_dentry)
  8980. + break;
  8981. + AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
  8982. +}
  8983. +
  8984. +void au_update_dbtop(struct dentry *dentry)
  8985. +{
  8986. + aufs_bindex_t bindex, bbot;
  8987. + struct dentry *h_dentry;
  8988. +
  8989. + bbot = au_dbbot(dentry);
  8990. + for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
  8991. + h_dentry = au_h_dptr(dentry, bindex);
  8992. + if (!h_dentry)
  8993. + continue;
  8994. + if (d_is_positive(h_dentry)) {
  8995. + au_set_dbtop(dentry, bindex);
  8996. + return;
  8997. + }
  8998. + au_set_h_dptr(dentry, bindex, NULL);
  8999. + }
  9000. +}
  9001. +
  9002. +void au_update_dbbot(struct dentry *dentry)
  9003. +{
  9004. + aufs_bindex_t bindex, btop;
  9005. + struct dentry *h_dentry;
  9006. +
  9007. + btop = au_dbtop(dentry);
  9008. + for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
  9009. + h_dentry = au_h_dptr(dentry, bindex);
  9010. + if (!h_dentry)
  9011. + continue;
  9012. + if (d_is_positive(h_dentry)) {
  9013. + au_set_dbbot(dentry, bindex);
  9014. + return;
  9015. + }
  9016. + au_set_h_dptr(dentry, bindex, NULL);
  9017. + }
  9018. +}
  9019. +
  9020. +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
  9021. +{
  9022. + aufs_bindex_t bindex, bbot;
  9023. +
  9024. + bbot = au_dbbot(dentry);
  9025. + for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
  9026. + if (au_h_dptr(dentry, bindex) == h_dentry)
  9027. + return bindex;
  9028. + return -1;
  9029. +}
  9030. diff -Naur null/fs/aufs/dir.c linux-5.15.131/fs/aufs/dir.c
  9031. --- /dev/null
  9032. +++ linux-5.15.131/fs/aufs/dir.c 2023-09-11 14:32:48.146722577 +0300
  9033. @@ -0,0 +1,765 @@
  9034. +// SPDX-License-Identifier: GPL-2.0
  9035. +/*
  9036. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  9037. + *
  9038. + * This program, aufs is free software; you can redistribute it and/or modify
  9039. + * it under the terms of the GNU General Public License as published by
  9040. + * the Free Software Foundation; either version 2 of the License, or
  9041. + * (at your option) any later version.
  9042. + *
  9043. + * This program is distributed in the hope that it will be useful,
  9044. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9045. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9046. + * GNU General Public License for more details.
  9047. + *
  9048. + * You should have received a copy of the GNU General Public License
  9049. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  9050. + */
  9051. +
  9052. +/*
  9053. + * directory operations
  9054. + */
  9055. +
  9056. +#include <linux/fs_stack.h>
  9057. +#include <linux/iversion.h>
  9058. +#include "aufs.h"
  9059. +
  9060. +void au_add_nlink(struct inode *dir, struct inode *h_dir)
  9061. +{
  9062. + unsigned int nlink;
  9063. +
  9064. + AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
  9065. +
  9066. + nlink = dir->i_nlink;
  9067. + nlink += h_dir->i_nlink - 2;
  9068. + if (h_dir->i_nlink < 2)
  9069. + nlink += 2;
  9070. + smp_mb(); /* for i_nlink */
  9071. + /* 0 can happen in revaliding */
  9072. + set_nlink(dir, nlink);
  9073. +}
  9074. +
  9075. +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
  9076. +{
  9077. + unsigned int nlink;
  9078. +
  9079. + AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
  9080. +
  9081. + nlink = dir->i_nlink;
  9082. + nlink -= h_dir->i_nlink - 2;
  9083. + if (h_dir->i_nlink < 2)
  9084. + nlink -= 2;
  9085. + smp_mb(); /* for i_nlink */
  9086. + /* nlink == 0 means the branch-fs is broken */
  9087. + set_nlink(dir, nlink);
  9088. +}
  9089. +
  9090. +loff_t au_dir_size(struct file *file, struct dentry *dentry)
  9091. +{
  9092. + loff_t sz;
  9093. + aufs_bindex_t bindex, bbot;
  9094. + struct file *h_file;
  9095. + struct dentry *h_dentry;
  9096. +
  9097. + sz = 0;
  9098. + if (file) {
  9099. + AuDebugOn(!d_is_dir(file->f_path.dentry));
  9100. +
  9101. + bbot = au_fbbot_dir(file);
  9102. + for (bindex = au_fbtop(file);
  9103. + bindex <= bbot && sz < KMALLOC_MAX_SIZE;
  9104. + bindex++) {
  9105. + h_file = au_hf_dir(file, bindex);
  9106. + if (h_file && file_inode(h_file))
  9107. + sz += vfsub_f_size_read(h_file);
  9108. + }
  9109. + } else {
  9110. + AuDebugOn(!dentry);
  9111. + AuDebugOn(!d_is_dir(dentry));
  9112. +
  9113. + bbot = au_dbtaildir(dentry);
  9114. + for (bindex = au_dbtop(dentry);
  9115. + bindex <= bbot && sz < KMALLOC_MAX_SIZE;
  9116. + bindex++) {
  9117. + h_dentry = au_h_dptr(dentry, bindex);
  9118. + if (h_dentry && d_is_positive(h_dentry))
  9119. + sz += i_size_read(d_inode(h_dentry));
  9120. + }
  9121. + }
  9122. + if (sz < KMALLOC_MAX_SIZE)
  9123. + sz = roundup_pow_of_two(sz);
  9124. + if (sz > KMALLOC_MAX_SIZE)
  9125. + sz = KMALLOC_MAX_SIZE;
  9126. + else if (sz < NAME_MAX) {
  9127. + BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
  9128. + sz = AUFS_RDBLK_DEF;
  9129. + }
  9130. + return sz;
  9131. +}
  9132. +
  9133. +struct au_dir_ts_arg {
  9134. + struct dentry *dentry;
  9135. + aufs_bindex_t brid;
  9136. +};
  9137. +
  9138. +static void au_do_dir_ts(void *arg)
  9139. +{
  9140. + struct au_dir_ts_arg *a = arg;
  9141. + struct au_dtime dt;
  9142. + struct path h_path;
  9143. + struct inode *dir, *h_dir;
  9144. + struct super_block *sb;
  9145. + struct au_branch *br;
  9146. + struct au_hinode *hdir;
  9147. + int err;
  9148. + aufs_bindex_t btop, bindex;
  9149. +
  9150. + sb = a->dentry->d_sb;
  9151. + if (d_really_is_negative(a->dentry))
  9152. + goto out;
  9153. + /* no dir->i_mutex lock */
  9154. + aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
  9155. +
  9156. + dir = d_inode(a->dentry);
  9157. + btop = au_ibtop(dir);
  9158. + bindex = au_br_index(sb, a->brid);
  9159. + if (bindex < btop)
  9160. + goto out_unlock;
  9161. +
  9162. + br = au_sbr(sb, bindex);
  9163. + h_path.dentry = au_h_dptr(a->dentry, bindex);
  9164. + if (!h_path.dentry)
  9165. + goto out_unlock;
  9166. + h_path.mnt = au_br_mnt(br);
  9167. + au_dtime_store(&dt, a->dentry, &h_path);
  9168. +
  9169. + br = au_sbr(sb, btop);
  9170. + if (!au_br_writable(br->br_perm))
  9171. + goto out_unlock;
  9172. + h_path.dentry = au_h_dptr(a->dentry, btop);
  9173. + h_path.mnt = au_br_mnt(br);
  9174. + err = vfsub_mnt_want_write(h_path.mnt);
  9175. + if (err)
  9176. + goto out_unlock;
  9177. + hdir = au_hi(dir, btop);
  9178. + au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
  9179. + h_dir = au_h_iptr(dir, btop);
  9180. + if (h_dir->i_nlink
  9181. + && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
  9182. + dt.dt_h_path = h_path;
  9183. + au_dtime_revert(&dt);
  9184. + }
  9185. + au_hn_inode_unlock(hdir);
  9186. + vfsub_mnt_drop_write(h_path.mnt);
  9187. + au_cpup_attr_timesizes(dir);
  9188. +
  9189. +out_unlock:
  9190. + aufs_read_unlock(a->dentry, AuLock_DW);
  9191. +out:
  9192. + dput(a->dentry);
  9193. + au_nwt_done(&au_sbi(sb)->si_nowait);
  9194. + au_kfree_try_rcu(arg);
  9195. +}
  9196. +
  9197. +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
  9198. +{
  9199. + int perm, wkq_err;
  9200. + aufs_bindex_t btop;
  9201. + struct au_dir_ts_arg *arg;
  9202. + struct dentry *dentry;
  9203. + struct super_block *sb;
  9204. +
  9205. + IMustLock(dir);
  9206. +
  9207. + dentry = d_find_any_alias(dir);
  9208. + AuDebugOn(!dentry);
  9209. + sb = dentry->d_sb;
  9210. + btop = au_ibtop(dir);
  9211. + if (btop == bindex) {
  9212. + au_cpup_attr_timesizes(dir);
  9213. + goto out;
  9214. + }
  9215. +
  9216. + perm = au_sbr_perm(sb, btop);
  9217. + if (!au_br_writable(perm))
  9218. + goto out;
  9219. +
  9220. + arg = kmalloc(sizeof(*arg), GFP_NOFS);
  9221. + if (!arg)
  9222. + goto out;
  9223. +
  9224. + arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
  9225. + arg->brid = au_sbr_id(sb, bindex);
  9226. + wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
  9227. + if (unlikely(wkq_err)) {
  9228. + pr_err("wkq %d\n", wkq_err);
  9229. + dput(dentry);
  9230. + au_kfree_try_rcu(arg);
  9231. + }
  9232. +
  9233. +out:
  9234. + dput(dentry);
  9235. +}
  9236. +
  9237. +/* ---------------------------------------------------------------------- */
  9238. +
  9239. +static int reopen_dir(struct file *file)
  9240. +{
  9241. + int err;
  9242. + unsigned int flags;
  9243. + aufs_bindex_t bindex, btail, btop;
  9244. + struct dentry *dentry, *h_dentry;
  9245. + struct file *h_file;
  9246. +
  9247. + /* open all lower dirs */
  9248. + dentry = file->f_path.dentry;
  9249. + btop = au_dbtop(dentry);
  9250. + for (bindex = au_fbtop(file); bindex < btop; bindex++)
  9251. + au_set_h_fptr(file, bindex, NULL);
  9252. + au_set_fbtop(file, btop);
  9253. +
  9254. + btail = au_dbtaildir(dentry);
  9255. + for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
  9256. + au_set_h_fptr(file, bindex, NULL);
  9257. + au_set_fbbot_dir(file, btail);
  9258. +
  9259. + flags = vfsub_file_flags(file);
  9260. + for (bindex = btop; bindex <= btail; bindex++) {
  9261. + h_dentry = au_h_dptr(dentry, bindex);
  9262. + if (!h_dentry)
  9263. + continue;
  9264. + h_file = au_hf_dir(file, bindex);
  9265. + if (h_file)
  9266. + continue;
  9267. +
  9268. + h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
  9269. + err = PTR_ERR(h_file);
  9270. + if (IS_ERR(h_file))
  9271. + goto out; /* close all? */
  9272. + au_set_h_fptr(file, bindex, h_file);
  9273. + }
  9274. + au_update_figen(file);
  9275. + /* todo: necessary? */
  9276. + /* file->f_ra = h_file->f_ra; */
  9277. + err = 0;
  9278. +
  9279. +out:
  9280. + return err;
  9281. +}
  9282. +
  9283. +static int do_open_dir(struct file *file, int flags, struct file *h_file)
  9284. +{
  9285. + int err;
  9286. + aufs_bindex_t bindex, btail;
  9287. + struct dentry *dentry, *h_dentry;
  9288. + struct vfsmount *mnt;
  9289. +
  9290. + FiMustWriteLock(file);
  9291. + AuDebugOn(h_file);
  9292. +
  9293. + err = 0;
  9294. + mnt = file->f_path.mnt;
  9295. + dentry = file->f_path.dentry;
  9296. + file->f_version = inode_query_iversion(d_inode(dentry));
  9297. + bindex = au_dbtop(dentry);
  9298. + au_set_fbtop(file, bindex);
  9299. + btail = au_dbtaildir(dentry);
  9300. + au_set_fbbot_dir(file, btail);
  9301. + for (; !err && bindex <= btail; bindex++) {
  9302. + h_dentry = au_h_dptr(dentry, bindex);
  9303. + if (!h_dentry)
  9304. + continue;
  9305. +
  9306. + err = vfsub_test_mntns(mnt, h_dentry->d_sb);
  9307. + if (unlikely(err))
  9308. + break;
  9309. + h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
  9310. + if (IS_ERR(h_file)) {
  9311. + err = PTR_ERR(h_file);
  9312. + break;
  9313. + }
  9314. + au_set_h_fptr(file, bindex, h_file);
  9315. + }
  9316. + au_update_figen(file);
  9317. + /* todo: necessary? */
  9318. + /* file->f_ra = h_file->f_ra; */
  9319. + if (!err)
  9320. + return 0; /* success */
  9321. +
  9322. + /* close all */
  9323. + for (bindex = au_fbtop(file); bindex <= btail; bindex++)
  9324. + au_set_h_fptr(file, bindex, NULL);
  9325. + au_set_fbtop(file, -1);
  9326. + au_set_fbbot_dir(file, -1);
  9327. +
  9328. + return err;
  9329. +}
  9330. +
  9331. +static int aufs_open_dir(struct inode *inode __maybe_unused,
  9332. + struct file *file)
  9333. +{
  9334. + int err;
  9335. + struct super_block *sb;
  9336. + struct au_fidir *fidir;
  9337. +
  9338. + err = -ENOMEM;
  9339. + sb = file->f_path.dentry->d_sb;
  9340. + si_read_lock(sb, AuLock_FLUSH);
  9341. + fidir = au_fidir_alloc(sb);
  9342. + if (fidir) {
  9343. + struct au_do_open_args args = {
  9344. + .open = do_open_dir,
  9345. + .fidir = fidir
  9346. + };
  9347. + err = au_do_open(file, &args);
  9348. + if (unlikely(err))
  9349. + au_kfree_rcu(fidir);
  9350. + }
  9351. + si_read_unlock(sb);
  9352. + return err;
  9353. +}
  9354. +
  9355. +static int aufs_release_dir(struct inode *inode __maybe_unused,
  9356. + struct file *file)
  9357. +{
  9358. + struct au_vdir *vdir_cache;
  9359. + struct au_finfo *finfo;
  9360. + struct au_fidir *fidir;
  9361. + struct au_hfile *hf;
  9362. + aufs_bindex_t bindex, bbot;
  9363. +
  9364. + finfo = au_fi(file);
  9365. + fidir = finfo->fi_hdir;
  9366. + if (fidir) {
  9367. + au_hbl_del(&finfo->fi_hlist,
  9368. + &au_sbi(file->f_path.dentry->d_sb)->si_files);
  9369. + vdir_cache = fidir->fd_vdir_cache; /* lock-free */
  9370. + if (vdir_cache)
  9371. + au_vdir_free(vdir_cache);
  9372. +
  9373. + bindex = finfo->fi_btop;
  9374. + if (bindex >= 0) {
  9375. + hf = fidir->fd_hfile + bindex;
  9376. + /*
  9377. + * calls fput() instead of filp_close(),
  9378. + * since no dnotify or lock for the lower file.
  9379. + */
  9380. + bbot = fidir->fd_bbot;
  9381. + for (; bindex <= bbot; bindex++, hf++)
  9382. + if (hf->hf_file)
  9383. + au_hfput(hf, /*execed*/0);
  9384. + }
  9385. + au_kfree_rcu(fidir);
  9386. + finfo->fi_hdir = NULL;
  9387. + }
  9388. + au_finfo_fin(file);
  9389. + return 0;
  9390. +}
  9391. +
  9392. +/* ---------------------------------------------------------------------- */
  9393. +
  9394. +static int au_do_flush_dir(struct file *file, fl_owner_t id)
  9395. +{
  9396. + int err;
  9397. + aufs_bindex_t bindex, bbot;
  9398. + struct file *h_file;
  9399. +
  9400. + err = 0;
  9401. + bbot = au_fbbot_dir(file);
  9402. + for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
  9403. + h_file = au_hf_dir(file, bindex);
  9404. + if (h_file)
  9405. + err = vfsub_flush(h_file, id);
  9406. + }
  9407. + return err;
  9408. +}
  9409. +
  9410. +static int aufs_flush_dir(struct file *file, fl_owner_t id)
  9411. +{
  9412. + return au_do_flush(file, id, au_do_flush_dir);
  9413. +}
  9414. +
  9415. +/* ---------------------------------------------------------------------- */
  9416. +
  9417. +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
  9418. +{
  9419. + int err;
  9420. + aufs_bindex_t bbot, bindex;
  9421. + struct inode *inode;
  9422. + struct super_block *sb;
  9423. +
  9424. + err = 0;
  9425. + sb = dentry->d_sb;
  9426. + inode = d_inode(dentry);
  9427. + IMustLock(inode);
  9428. + bbot = au_dbbot(dentry);
  9429. + for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
  9430. + struct path h_path;
  9431. +
  9432. + if (au_test_ro(sb, bindex, inode))
  9433. + continue;
  9434. + h_path.dentry = au_h_dptr(dentry, bindex);
  9435. + if (!h_path.dentry)
  9436. + continue;
  9437. +
  9438. + h_path.mnt = au_sbr_mnt(sb, bindex);
  9439. + err = vfsub_fsync(NULL, &h_path, datasync);
  9440. + }
  9441. +
  9442. + return err;
  9443. +}
  9444. +
  9445. +static int au_do_fsync_dir(struct file *file, int datasync)
  9446. +{
  9447. + int err;
  9448. + aufs_bindex_t bbot, bindex;
  9449. + struct file *h_file;
  9450. + struct super_block *sb;
  9451. + struct inode *inode;
  9452. +
  9453. + err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
  9454. + if (unlikely(err))
  9455. + goto out;
  9456. +
  9457. + inode = file_inode(file);
  9458. + sb = inode->i_sb;
  9459. + bbot = au_fbbot_dir(file);
  9460. + for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
  9461. + h_file = au_hf_dir(file, bindex);
  9462. + if (!h_file || au_test_ro(sb, bindex, inode))
  9463. + continue;
  9464. +
  9465. + err = vfsub_fsync(h_file, &h_file->f_path, datasync);
  9466. + }
  9467. +
  9468. +out:
  9469. + return err;
  9470. +}
  9471. +
  9472. +/*
  9473. + * @file may be NULL
  9474. + */
  9475. +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
  9476. + int datasync)
  9477. +{
  9478. + int err;
  9479. + struct dentry *dentry;
  9480. + struct inode *inode;
  9481. + struct super_block *sb;
  9482. +
  9483. + err = 0;
  9484. + dentry = file->f_path.dentry;
  9485. + inode = d_inode(dentry);
  9486. + inode_lock(inode);
  9487. + sb = dentry->d_sb;
  9488. + si_noflush_read_lock(sb);
  9489. + if (file)
  9490. + err = au_do_fsync_dir(file, datasync);
  9491. + else {
  9492. + di_write_lock_child(dentry);
  9493. + err = au_do_fsync_dir_no_file(dentry, datasync);
  9494. + }
  9495. + au_cpup_attr_timesizes(inode);
  9496. + di_write_unlock(dentry);
  9497. + if (file)
  9498. + fi_write_unlock(file);
  9499. +
  9500. + si_read_unlock(sb);
  9501. + inode_unlock(inode);
  9502. + return err;
  9503. +}
  9504. +
  9505. +/* ---------------------------------------------------------------------- */
  9506. +
  9507. +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
  9508. +{
  9509. + int err;
  9510. + struct dentry *dentry;
  9511. + struct inode *inode, *h_inode;
  9512. + struct super_block *sb;
  9513. +
  9514. + AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
  9515. +
  9516. + dentry = file->f_path.dentry;
  9517. + inode = d_inode(dentry);
  9518. + IMustLock(inode);
  9519. +
  9520. + sb = dentry->d_sb;
  9521. + si_read_lock(sb, AuLock_FLUSH);
  9522. + err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
  9523. + if (unlikely(err))
  9524. + goto out;
  9525. + err = au_alive_dir(dentry);
  9526. + if (!err)
  9527. + err = au_vdir_init(file);
  9528. + di_downgrade_lock(dentry, AuLock_IR);
  9529. + if (unlikely(err))
  9530. + goto out_unlock;
  9531. +
  9532. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  9533. + if (!au_test_nfsd()) {
  9534. + err = au_vdir_fill_de(file, ctx);
  9535. + fsstack_copy_attr_atime(inode, h_inode);
  9536. + } else {
  9537. + /*
  9538. + * nfsd filldir may call lookup_one_len(), vfs_getattr(),
  9539. + * encode_fh() and others.
  9540. + */
  9541. + atomic_inc(&h_inode->i_count);
  9542. + di_read_unlock(dentry, AuLock_IR);
  9543. + si_read_unlock(sb);
  9544. + err = au_vdir_fill_de(file, ctx);
  9545. + fsstack_copy_attr_atime(inode, h_inode);
  9546. + fi_write_unlock(file);
  9547. + iput(h_inode);
  9548. +
  9549. + AuTraceErr(err);
  9550. + return err;
  9551. + }
  9552. +
  9553. +out_unlock:
  9554. + di_read_unlock(dentry, AuLock_IR);
  9555. + fi_write_unlock(file);
  9556. +out:
  9557. + si_read_unlock(sb);
  9558. + return err;
  9559. +}
  9560. +
  9561. +/* ---------------------------------------------------------------------- */
  9562. +
  9563. +#define AuTestEmpty_WHONLY 1
  9564. +#define AuTestEmpty_CALLED (1 << 1)
  9565. +#define AuTestEmpty_SHWH (1 << 2)
  9566. +#define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
  9567. +#define au_fset_testempty(flags, name) \
  9568. + do { (flags) |= AuTestEmpty_##name; } while (0)
  9569. +#define au_fclr_testempty(flags, name) \
  9570. + do { (flags) &= ~AuTestEmpty_##name; } while (0)
  9571. +
  9572. +#ifndef CONFIG_AUFS_SHWH
  9573. +#undef AuTestEmpty_SHWH
  9574. +#define AuTestEmpty_SHWH 0
  9575. +#endif
  9576. +
  9577. +struct test_empty_arg {
  9578. + struct dir_context ctx;
  9579. + struct au_nhash *whlist;
  9580. + unsigned int flags;
  9581. + int err;
  9582. + aufs_bindex_t bindex;
  9583. +};
  9584. +
  9585. +static int test_empty_cb(struct dir_context *ctx, const char *__name,
  9586. + int namelen, loff_t offset __maybe_unused, u64 ino,
  9587. + unsigned int d_type)
  9588. +{
  9589. + struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
  9590. + ctx);
  9591. + char *name = (void *)__name;
  9592. +
  9593. + arg->err = 0;
  9594. + au_fset_testempty(arg->flags, CALLED);
  9595. + /* smp_mb(); */
  9596. + if (name[0] == '.'
  9597. + && (namelen == 1 || (name[1] == '.' && namelen == 2)))
  9598. + goto out; /* success */
  9599. +
  9600. + if (namelen <= AUFS_WH_PFX_LEN
  9601. + || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
  9602. + if (au_ftest_testempty(arg->flags, WHONLY)
  9603. + && !au_nhash_test_known_wh(arg->whlist, name, namelen))
  9604. + arg->err = -ENOTEMPTY;
  9605. + goto out;
  9606. + }
  9607. +
  9608. + name += AUFS_WH_PFX_LEN;
  9609. + namelen -= AUFS_WH_PFX_LEN;
  9610. + if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
  9611. + arg->err = au_nhash_append_wh
  9612. + (arg->whlist, name, namelen, ino, d_type, arg->bindex,
  9613. + au_ftest_testempty(arg->flags, SHWH));
  9614. +
  9615. +out:
  9616. + /* smp_mb(); */
  9617. + AuTraceErr(arg->err);
  9618. + return arg->err;
  9619. +}
  9620. +
  9621. +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
  9622. +{
  9623. + int err;
  9624. + struct file *h_file;
  9625. + struct au_branch *br;
  9626. +
  9627. + h_file = au_h_open(dentry, arg->bindex,
  9628. + O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
  9629. + /*file*/NULL, /*force_wr*/0);
  9630. + err = PTR_ERR(h_file);
  9631. + if (IS_ERR(h_file))
  9632. + goto out;
  9633. +
  9634. + err = 0;
  9635. + if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
  9636. + && !file_inode(h_file)->i_nlink)
  9637. + goto out_put;
  9638. +
  9639. + do {
  9640. + arg->err = 0;
  9641. + au_fclr_testempty(arg->flags, CALLED);
  9642. + /* smp_mb(); */
  9643. + err = vfsub_iterate_dir(h_file, &arg->ctx);
  9644. + if (err >= 0)
  9645. + err = arg->err;
  9646. + } while (!err && au_ftest_testempty(arg->flags, CALLED));
  9647. +
  9648. +out_put:
  9649. + fput(h_file);
  9650. + br = au_sbr(dentry->d_sb, arg->bindex);
  9651. + au_lcnt_dec(&br->br_nfiles);
  9652. +out:
  9653. + return err;
  9654. +}
  9655. +
  9656. +struct do_test_empty_args {
  9657. + int *errp;
  9658. + struct dentry *dentry;
  9659. + struct test_empty_arg *arg;
  9660. +};
  9661. +
  9662. +static void call_do_test_empty(void *args)
  9663. +{
  9664. + struct do_test_empty_args *a = args;
  9665. + *a->errp = do_test_empty(a->dentry, a->arg);
  9666. +}
  9667. +
  9668. +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
  9669. +{
  9670. + int err, wkq_err;
  9671. + struct dentry *h_dentry;
  9672. + struct inode *h_inode;
  9673. + struct user_namespace *h_userns;
  9674. +
  9675. + h_userns = au_sbr_userns(dentry->d_sb, arg->bindex);
  9676. + h_dentry = au_h_dptr(dentry, arg->bindex);
  9677. + h_inode = d_inode(h_dentry);
  9678. + /* todo: i_mode changes anytime? */
  9679. + inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
  9680. + err = au_test_h_perm_sio(h_userns, h_inode, MAY_EXEC | MAY_READ);
  9681. + inode_unlock_shared(h_inode);
  9682. + if (!err)
  9683. + err = do_test_empty(dentry, arg);
  9684. + else {
  9685. + struct do_test_empty_args args = {
  9686. + .errp = &err,
  9687. + .dentry = dentry,
  9688. + .arg = arg
  9689. + };
  9690. + unsigned int flags = arg->flags;
  9691. +
  9692. + wkq_err = au_wkq_wait(call_do_test_empty, &args);
  9693. + if (unlikely(wkq_err))
  9694. + err = wkq_err;
  9695. + arg->flags = flags;
  9696. + }
  9697. +
  9698. + return err;
  9699. +}
  9700. +
  9701. +int au_test_empty_lower(struct dentry *dentry)
  9702. +{
  9703. + int err;
  9704. + unsigned int rdhash;
  9705. + aufs_bindex_t bindex, btop, btail;
  9706. + struct au_nhash whlist;
  9707. + struct test_empty_arg arg = {
  9708. + .ctx = {
  9709. + .actor = test_empty_cb
  9710. + }
  9711. + };
  9712. + int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
  9713. +
  9714. + SiMustAnyLock(dentry->d_sb);
  9715. +
  9716. + rdhash = au_sbi(dentry->d_sb)->si_rdhash;
  9717. + if (!rdhash)
  9718. + rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
  9719. + err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
  9720. + if (unlikely(err))
  9721. + goto out;
  9722. +
  9723. + arg.flags = 0;
  9724. + arg.whlist = &whlist;
  9725. + btop = au_dbtop(dentry);
  9726. + if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
  9727. + au_fset_testempty(arg.flags, SHWH);
  9728. + test_empty = do_test_empty;
  9729. + if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
  9730. + test_empty = sio_test_empty;
  9731. + arg.bindex = btop;
  9732. + err = test_empty(dentry, &arg);
  9733. + if (unlikely(err))
  9734. + goto out_whlist;
  9735. +
  9736. + au_fset_testempty(arg.flags, WHONLY);
  9737. + btail = au_dbtaildir(dentry);
  9738. + for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
  9739. + struct dentry *h_dentry;
  9740. +
  9741. + h_dentry = au_h_dptr(dentry, bindex);
  9742. + if (h_dentry && d_is_positive(h_dentry)) {
  9743. + arg.bindex = bindex;
  9744. + err = test_empty(dentry, &arg);
  9745. + }
  9746. + }
  9747. +
  9748. +out_whlist:
  9749. + au_nhash_wh_free(&whlist);
  9750. +out:
  9751. + return err;
  9752. +}
  9753. +
  9754. +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
  9755. +{
  9756. + int err;
  9757. + struct test_empty_arg arg = {
  9758. + .ctx = {
  9759. + .actor = test_empty_cb
  9760. + }
  9761. + };
  9762. + aufs_bindex_t bindex, btail;
  9763. +
  9764. + err = 0;
  9765. + arg.whlist = whlist;
  9766. + arg.flags = AuTestEmpty_WHONLY;
  9767. + if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
  9768. + au_fset_testempty(arg.flags, SHWH);
  9769. + btail = au_dbtaildir(dentry);
  9770. + for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
  9771. + struct dentry *h_dentry;
  9772. +
  9773. + h_dentry = au_h_dptr(dentry, bindex);
  9774. + if (h_dentry && d_is_positive(h_dentry)) {
  9775. + arg.bindex = bindex;
  9776. + err = sio_test_empty(dentry, &arg);
  9777. + }
  9778. + }
  9779. +
  9780. + return err;
  9781. +}
  9782. +
  9783. +/* ---------------------------------------------------------------------- */
  9784. +
  9785. +const struct file_operations aufs_dir_fop = {
  9786. + .owner = THIS_MODULE,
  9787. + .llseek = default_llseek,
  9788. + .read = generic_read_dir,
  9789. + .iterate_shared = aufs_iterate_shared,
  9790. + .unlocked_ioctl = aufs_ioctl_dir,
  9791. +#ifdef CONFIG_COMPAT
  9792. + .compat_ioctl = aufs_compat_ioctl_dir,
  9793. +#endif
  9794. + .open = aufs_open_dir,
  9795. + .release = aufs_release_dir,
  9796. + .flush = aufs_flush_dir,
  9797. + .fsync = aufs_fsync_dir
  9798. +};
  9799. diff -Naur null/fs/aufs/dir.h linux-5.15.131/fs/aufs/dir.h
  9800. --- /dev/null
  9801. +++ linux-5.15.131/fs/aufs/dir.h 2023-09-11 14:32:48.147722564 +0300
  9802. @@ -0,0 +1,134 @@
  9803. +/* SPDX-License-Identifier: GPL-2.0 */
  9804. +/*
  9805. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  9806. + *
  9807. + * This program, aufs is free software; you can redistribute it and/or modify
  9808. + * it under the terms of the GNU General Public License as published by
  9809. + * the Free Software Foundation; either version 2 of the License, or
  9810. + * (at your option) any later version.
  9811. + *
  9812. + * This program is distributed in the hope that it will be useful,
  9813. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9814. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9815. + * GNU General Public License for more details.
  9816. + *
  9817. + * You should have received a copy of the GNU General Public License
  9818. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  9819. + */
  9820. +
  9821. +/*
  9822. + * directory operations
  9823. + */
  9824. +
  9825. +#ifndef __AUFS_DIR_H__
  9826. +#define __AUFS_DIR_H__
  9827. +
  9828. +#ifdef __KERNEL__
  9829. +
  9830. +#include <linux/fs.h>
  9831. +
  9832. +/* ---------------------------------------------------------------------- */
  9833. +
  9834. +/* need to be faster and smaller */
  9835. +
  9836. +struct au_nhash {
  9837. + unsigned int nh_num;
  9838. + struct hlist_head *nh_head;
  9839. +};
  9840. +
  9841. +struct au_vdir_destr {
  9842. + unsigned char len;
  9843. + unsigned char name[];
  9844. +} __packed;
  9845. +
  9846. +struct au_vdir_dehstr {
  9847. + struct hlist_node hash;
  9848. + struct au_vdir_destr *str;
  9849. + struct rcu_head rcu;
  9850. +} ____cacheline_aligned_in_smp;
  9851. +
  9852. +struct au_vdir_de {
  9853. + ino_t de_ino;
  9854. + unsigned char de_type;
  9855. + /* caution: packed */
  9856. + struct au_vdir_destr de_str;
  9857. +} __packed;
  9858. +
  9859. +struct au_vdir_wh {
  9860. + struct hlist_node wh_hash;
  9861. +#ifdef CONFIG_AUFS_SHWH
  9862. + ino_t wh_ino;
  9863. + aufs_bindex_t wh_bindex;
  9864. + unsigned char wh_type;
  9865. +#else
  9866. + aufs_bindex_t wh_bindex;
  9867. +#endif
  9868. + /* caution: packed */
  9869. + struct au_vdir_destr wh_str;
  9870. +} __packed;
  9871. +
  9872. +union au_vdir_deblk_p {
  9873. + unsigned char *deblk;
  9874. + struct au_vdir_de *de;
  9875. +};
  9876. +
  9877. +struct au_vdir {
  9878. + unsigned char **vd_deblk;
  9879. + unsigned long vd_nblk;
  9880. + struct {
  9881. + unsigned long ul;
  9882. + union au_vdir_deblk_p p;
  9883. + } vd_last;
  9884. +
  9885. + u64 vd_version;
  9886. + unsigned int vd_deblk_sz;
  9887. + unsigned long vd_jiffy;
  9888. + struct rcu_head rcu;
  9889. +} ____cacheline_aligned_in_smp;
  9890. +
  9891. +/* ---------------------------------------------------------------------- */
  9892. +
  9893. +/* dir.c */
  9894. +extern const struct file_operations aufs_dir_fop;
  9895. +void au_add_nlink(struct inode *dir, struct inode *h_dir);
  9896. +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
  9897. +loff_t au_dir_size(struct file *file, struct dentry *dentry);
  9898. +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
  9899. +int au_test_empty_lower(struct dentry *dentry);
  9900. +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
  9901. +
  9902. +/* vdir.c */
  9903. +unsigned int au_rdhash_est(loff_t sz);
  9904. +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
  9905. +void au_nhash_wh_free(struct au_nhash *whlist);
  9906. +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
  9907. + int limit);
  9908. +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
  9909. +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
  9910. + unsigned int d_type, aufs_bindex_t bindex,
  9911. + unsigned char shwh);
  9912. +void au_vdir_free(struct au_vdir *vdir);
  9913. +int au_vdir_init(struct file *file);
  9914. +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
  9915. +
  9916. +/* ioctl.c */
  9917. +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
  9918. +
  9919. +#ifdef CONFIG_AUFS_RDU
  9920. +/* rdu.c */
  9921. +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  9922. +#ifdef CONFIG_COMPAT
  9923. +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
  9924. + unsigned long arg);
  9925. +#endif
  9926. +#else
  9927. +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
  9928. + unsigned int cmd, unsigned long arg)
  9929. +#ifdef CONFIG_COMPAT
  9930. +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
  9931. + unsigned int cmd, unsigned long arg)
  9932. +#endif
  9933. +#endif
  9934. +
  9935. +#endif /* __KERNEL__ */
  9936. +#endif /* __AUFS_DIR_H__ */
  9937. diff -Naur null/fs/aufs/dirren.c linux-5.15.131/fs/aufs/dirren.c
  9938. --- /dev/null
  9939. +++ linux-5.15.131/fs/aufs/dirren.c 2023-09-11 14:32:48.147722564 +0300
  9940. @@ -0,0 +1,1315 @@
  9941. +// SPDX-License-Identifier: GPL-2.0
  9942. +/*
  9943. + * Copyright (C) 2017-2021 Junjiro R. Okajima
  9944. + *
  9945. + * This program, aufs is free software; you can redistribute it and/or modify
  9946. + * it under the terms of the GNU General Public License as published by
  9947. + * the Free Software Foundation; either version 2 of the License, or
  9948. + * (at your option) any later version.
  9949. + *
  9950. + * This program is distributed in the hope that it will be useful,
  9951. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9952. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9953. + * GNU General Public License for more details.
  9954. + *
  9955. + * You should have received a copy of the GNU General Public License
  9956. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  9957. + */
  9958. +
  9959. +/*
  9960. + * special handling in renaming a directory
  9961. + * in order to support looking-up the before-renamed name on the lower readonly
  9962. + * branches
  9963. + */
  9964. +
  9965. +#include <linux/byteorder/generic.h>
  9966. +#include "aufs.h"
  9967. +
  9968. +static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
  9969. +{
  9970. + int idx;
  9971. +
  9972. + idx = au_dr_ihash(ent->dr_h_ino);
  9973. + au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
  9974. +}
  9975. +
  9976. +static int au_dr_hino_test_empty(struct au_dr_br *dr)
  9977. +{
  9978. + int ret, i;
  9979. + struct hlist_bl_head *hbl;
  9980. +
  9981. + ret = 1;
  9982. + for (i = 0; ret && i < AuDirren_NHASH; i++) {
  9983. + hbl = dr->dr_h_ino + i;
  9984. + hlist_bl_lock(hbl);
  9985. + ret &= hlist_bl_empty(hbl);
  9986. + hlist_bl_unlock(hbl);
  9987. + }
  9988. +
  9989. + return ret;
  9990. +}
  9991. +
  9992. +static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
  9993. +{
  9994. + struct au_dr_hino *found, *ent;
  9995. + struct hlist_bl_head *hbl;
  9996. + struct hlist_bl_node *pos;
  9997. + int idx;
  9998. +
  9999. + found = NULL;
  10000. + idx = au_dr_ihash(ino);
  10001. + hbl = dr->dr_h_ino + idx;
  10002. + hlist_bl_lock(hbl);
  10003. + hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
  10004. + if (ent->dr_h_ino == ino) {
  10005. + found = ent;
  10006. + break;
  10007. + }
  10008. + hlist_bl_unlock(hbl);
  10009. +
  10010. + return found;
  10011. +}
  10012. +
  10013. +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
  10014. + struct au_dr_hino *add_ent)
  10015. +{
  10016. + int found, idx;
  10017. + struct hlist_bl_head *hbl;
  10018. + struct hlist_bl_node *pos;
  10019. + struct au_dr_hino *ent;
  10020. +
  10021. + found = 0;
  10022. + idx = au_dr_ihash(ino);
  10023. + hbl = dr->dr_h_ino + idx;
  10024. +#if 0 /* debug print */
  10025. + {
  10026. + struct hlist_bl_node *tmp;
  10027. +
  10028. + hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
  10029. + AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
  10030. + }
  10031. +#endif
  10032. + hlist_bl_lock(hbl);
  10033. + hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
  10034. + if (ent->dr_h_ino == ino) {
  10035. + found = 1;
  10036. + break;
  10037. + }
  10038. + if (!found && add_ent)
  10039. + hlist_bl_add_head(&add_ent->dr_hnode, hbl);
  10040. + hlist_bl_unlock(hbl);
  10041. +
  10042. + if (!found && add_ent)
  10043. + AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
  10044. +
  10045. + return found;
  10046. +}
  10047. +
  10048. +void au_dr_hino_free(struct au_dr_br *dr)
  10049. +{
  10050. + int i;
  10051. + struct hlist_bl_head *hbl;
  10052. + struct hlist_bl_node *pos, *tmp;
  10053. + struct au_dr_hino *ent;
  10054. +
  10055. + /* SiMustWriteLock(sb); */
  10056. +
  10057. + for (i = 0; i < AuDirren_NHASH; i++) {
  10058. + hbl = dr->dr_h_ino + i;
  10059. + /* no spinlock since sbinfo must be write-locked */
  10060. + hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
  10061. + au_kfree_rcu(ent);
  10062. + INIT_HLIST_BL_HEAD(hbl);
  10063. + }
  10064. +}
  10065. +
  10066. +/* returns the number of inodes or an error */
  10067. +static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
  10068. + struct file *hinofile)
  10069. +{
  10070. + int err, i;
  10071. + ssize_t ssz;
  10072. + loff_t pos, oldsize;
  10073. + __be64 u64;
  10074. + struct inode *hinoinode;
  10075. + struct hlist_bl_head *hbl;
  10076. + struct hlist_bl_node *n1, *n2;
  10077. + struct au_dr_hino *ent;
  10078. +
  10079. + SiMustWriteLock(sb);
  10080. + AuDebugOn(!au_br_writable(br->br_perm));
  10081. +
  10082. + hinoinode = file_inode(hinofile);
  10083. + oldsize = i_size_read(hinoinode);
  10084. +
  10085. + err = 0;
  10086. + pos = 0;
  10087. + hbl = br->br_dirren.dr_h_ino;
  10088. + for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
  10089. + /* no bit-lock since sbinfo must be write-locked */
  10090. + hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
  10091. + AuDbg("hi%llu, %pD2\n",
  10092. + (unsigned long long)ent->dr_h_ino, hinofile);
  10093. + u64 = cpu_to_be64(ent->dr_h_ino);
  10094. + ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
  10095. + if (ssz == sizeof(u64))
  10096. + continue;
  10097. +
  10098. + /* write error */
  10099. + pr_err("ssz %zd, %pD2\n", ssz, hinofile);
  10100. + err = -ENOSPC;
  10101. + if (ssz < 0)
  10102. + err = ssz;
  10103. + break;
  10104. + }
  10105. + }
  10106. + /* regardless the error */
  10107. + if (pos < oldsize) {
  10108. + err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
  10109. + AuTraceErr(err);
  10110. + }
  10111. +
  10112. + AuTraceErr(err);
  10113. + return err;
  10114. +}
  10115. +
  10116. +static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
  10117. +{
  10118. + int err, hidx;
  10119. + ssize_t ssz;
  10120. + size_t sz, n;
  10121. + loff_t pos;
  10122. + uint64_t u64;
  10123. + struct au_dr_hino *ent;
  10124. + struct inode *hinoinode;
  10125. + struct hlist_bl_head *hbl;
  10126. +
  10127. + err = 0;
  10128. + pos = 0;
  10129. + hbl = dr->dr_h_ino;
  10130. + hinoinode = file_inode(hinofile);
  10131. + sz = i_size_read(hinoinode);
  10132. + AuDebugOn(sz % sizeof(u64));
  10133. + n = sz / sizeof(u64);
  10134. + while (n--) {
  10135. + ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
  10136. + if (unlikely(ssz != sizeof(u64))) {
  10137. + pr_err("ssz %zd, %pD2\n", ssz, hinofile);
  10138. + err = -EINVAL;
  10139. + if (ssz < 0)
  10140. + err = ssz;
  10141. + goto out_free;
  10142. + }
  10143. +
  10144. + ent = kmalloc(sizeof(*ent), GFP_NOFS);
  10145. + if (!ent) {
  10146. + err = -ENOMEM;
  10147. + AuTraceErr(err);
  10148. + goto out_free;
  10149. + }
  10150. + ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
  10151. + AuDbg("hi%llu, %pD2\n",
  10152. + (unsigned long long)ent->dr_h_ino, hinofile);
  10153. + hidx = au_dr_ihash(ent->dr_h_ino);
  10154. + au_hbl_add(&ent->dr_hnode, hbl + hidx);
  10155. + }
  10156. + goto out; /* success */
  10157. +
  10158. +out_free:
  10159. + au_dr_hino_free(dr);
  10160. +out:
  10161. + AuTraceErr(err);
  10162. + return err;
  10163. +}
  10164. +
  10165. +/*
  10166. + * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
  10167. + * @path is a switch to distinguish load and store.
  10168. + */
  10169. +static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
  10170. + struct au_branch *br, const struct path *path)
  10171. +{
  10172. + int err, flags;
  10173. + unsigned char load, suspend;
  10174. + struct file *hinofile;
  10175. + struct au_hinode *hdir;
  10176. + struct inode *dir, *delegated;
  10177. + struct path hinopath;
  10178. + struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
  10179. + sizeof(AUFS_WH_DR_BRHINO) - 1);
  10180. +
  10181. + AuDebugOn(bindex < 0 && !br);
  10182. + AuDebugOn(bindex >= 0 && br);
  10183. +
  10184. + err = -EINVAL;
  10185. + suspend = !br;
  10186. + if (suspend)
  10187. + br = au_sbr(sb, bindex);
  10188. + load = !!path;
  10189. + if (!load) {
  10190. + path = &br->br_path;
  10191. + AuDebugOn(!au_br_writable(br->br_perm));
  10192. + if (unlikely(!au_br_writable(br->br_perm)))
  10193. + goto out;
  10194. + }
  10195. +
  10196. + hdir = NULL;
  10197. + if (suspend) {
  10198. + dir = d_inode(sb->s_root);
  10199. + hdir = au_hinode(au_ii(dir), bindex);
  10200. + dir = hdir->hi_inode;
  10201. + au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
  10202. + } else {
  10203. + dir = d_inode(path->dentry);
  10204. + inode_lock_nested(dir, AuLsc_I_CHILD);
  10205. + }
  10206. + hinopath.mnt = path->mnt;
  10207. + hinopath.dentry = vfsub_lkup_one(&hinoname, (struct path *)path);
  10208. + err = PTR_ERR(hinopath.dentry);
  10209. + if (IS_ERR(hinopath.dentry))
  10210. + goto out_unlock;
  10211. +
  10212. + err = 0;
  10213. + flags = O_RDONLY;
  10214. + if (load) {
  10215. + if (d_is_negative(hinopath.dentry))
  10216. + goto out_dput; /* success */
  10217. + } else {
  10218. + if (au_dr_hino_test_empty(&br->br_dirren)) {
  10219. + if (d_is_positive(hinopath.dentry)) {
  10220. + delegated = NULL;
  10221. + err = vfsub_unlink(dir, &hinopath, &delegated,
  10222. + /*force*/0);
  10223. + AuTraceErr(err);
  10224. + if (unlikely(err))
  10225. + pr_err("ignored err %d, %pd2\n",
  10226. + err, hinopath.dentry);
  10227. + if (unlikely(err == -EWOULDBLOCK))
  10228. + iput(delegated);
  10229. + err = 0;
  10230. + }
  10231. + goto out_dput;
  10232. + } else if (!d_is_positive(hinopath.dentry)) {
  10233. + err = vfsub_create(dir, &hinopath, 0600,
  10234. + /*want_excl*/false);
  10235. + AuTraceErr(err);
  10236. + if (unlikely(err))
  10237. + goto out_dput;
  10238. + }
  10239. + flags = O_WRONLY;
  10240. + }
  10241. + hinofile = vfsub_dentry_open(&hinopath, flags);
  10242. + if (suspend)
  10243. + au_hn_inode_unlock(hdir);
  10244. + else
  10245. + inode_unlock(dir);
  10246. + dput(hinopath.dentry);
  10247. + AuTraceErrPtr(hinofile);
  10248. + if (IS_ERR(hinofile)) {
  10249. + err = PTR_ERR(hinofile);
  10250. + goto out;
  10251. + }
  10252. +
  10253. + if (load)
  10254. + err = au_dr_hino_load(&br->br_dirren, hinofile);
  10255. + else
  10256. + err = au_dr_hino_store(sb, br, hinofile);
  10257. + fput(hinofile);
  10258. + goto out;
  10259. +
  10260. +out_dput:
  10261. + dput(hinopath.dentry);
  10262. +out_unlock:
  10263. + if (suspend)
  10264. + au_hn_inode_unlock(hdir);
  10265. + else
  10266. + inode_unlock(dir);
  10267. +out:
  10268. + AuTraceErr(err);
  10269. + return err;
  10270. +}
  10271. +
  10272. +/* ---------------------------------------------------------------------- */
  10273. +
  10274. +static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
  10275. +{
  10276. + int err;
  10277. + struct kstatfs kstfs;
  10278. + dev_t dev;
  10279. + struct dentry *dentry;
  10280. + struct super_block *sb;
  10281. +
  10282. + err = vfs_statfs((void *)path, &kstfs);
  10283. + AuTraceErr(err);
  10284. + if (unlikely(err))
  10285. + goto out;
  10286. +
  10287. + /* todo: support for UUID */
  10288. +
  10289. + if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
  10290. + brid->type = AuBrid_FSID;
  10291. + brid->fsid = kstfs.f_fsid;
  10292. + } else {
  10293. + dentry = path->dentry;
  10294. + sb = dentry->d_sb;
  10295. + dev = sb->s_dev;
  10296. + if (dev) {
  10297. + brid->type = AuBrid_DEV;
  10298. + brid->dev = dev;
  10299. + }
  10300. + }
  10301. +
  10302. +out:
  10303. + return err;
  10304. +}
  10305. +
  10306. +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
  10307. + const struct path *path)
  10308. +{
  10309. + int err, i;
  10310. + struct au_dr_br *dr;
  10311. + struct hlist_bl_head *hbl;
  10312. +
  10313. + dr = &br->br_dirren;
  10314. + hbl = dr->dr_h_ino;
  10315. + for (i = 0; i < AuDirren_NHASH; i++, hbl++)
  10316. + INIT_HLIST_BL_HEAD(hbl);
  10317. +
  10318. + err = au_dr_brid_init(&dr->dr_brid, path);
  10319. + if (unlikely(err))
  10320. + goto out;
  10321. +
  10322. + if (au_opt_test(au_mntflags(sb), DIRREN))
  10323. + err = au_dr_hino(sb, /*bindex*/-1, br, path);
  10324. +
  10325. +out:
  10326. + AuTraceErr(err);
  10327. + return err;
  10328. +}
  10329. +
  10330. +int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
  10331. +{
  10332. + int err;
  10333. +
  10334. + err = 0;
  10335. + if (au_br_writable(br->br_perm))
  10336. + err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
  10337. + if (!err)
  10338. + au_dr_hino_free(&br->br_dirren);
  10339. +
  10340. + return err;
  10341. +}
  10342. +
  10343. +/* ---------------------------------------------------------------------- */
  10344. +
  10345. +static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
  10346. + char *buf, size_t sz)
  10347. +{
  10348. + int err;
  10349. + unsigned int major, minor;
  10350. + char *p;
  10351. +
  10352. + p = buf;
  10353. + err = snprintf(p, sz, "%d_", brid->type);
  10354. + AuDebugOn(err > sz);
  10355. + p += err;
  10356. + sz -= err;
  10357. + switch (brid->type) {
  10358. + case AuBrid_Unset:
  10359. + return -EINVAL;
  10360. + case AuBrid_UUID:
  10361. + err = snprintf(p, sz, "%pU", brid->uuid.b);
  10362. + break;
  10363. + case AuBrid_FSID:
  10364. + err = snprintf(p, sz, "%08x-%08x",
  10365. + brid->fsid.val[0], brid->fsid.val[1]);
  10366. + break;
  10367. + case AuBrid_DEV:
  10368. + major = MAJOR(brid->dev);
  10369. + minor = MINOR(brid->dev);
  10370. + if (major <= 0xff && minor <= 0xff)
  10371. + err = snprintf(p, sz, "%02x%02x", major, minor);
  10372. + else
  10373. + err = snprintf(p, sz, "%03x:%05x", major, minor);
  10374. + break;
  10375. + }
  10376. + AuDebugOn(err > sz);
  10377. + p += err;
  10378. + sz -= err;
  10379. + err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
  10380. + AuDebugOn(err > sz);
  10381. + p += err;
  10382. + sz -= err;
  10383. +
  10384. + return p - buf;
  10385. +}
  10386. +
  10387. +static int au_drinfo_name(struct au_branch *br, char *name, int len)
  10388. +{
  10389. + int rlen;
  10390. + struct dentry *br_dentry;
  10391. + struct inode *br_inode;
  10392. +
  10393. + br_dentry = au_br_dentry(br);
  10394. + br_inode = d_inode(br_dentry);
  10395. + rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
  10396. + AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
  10397. + AuDebugOn(rlen > len);
  10398. +
  10399. + return rlen;
  10400. +}
  10401. +
  10402. +/* ---------------------------------------------------------------------- */
  10403. +
  10404. +/*
  10405. + * from the given @h_dentry, construct drinfo at @*fdata.
  10406. + * when the size of @*fdata is not enough, reallocate and return new @fdata and
  10407. + * @allocated.
  10408. + */
  10409. +static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
  10410. + struct dentry *h_dentry,
  10411. + unsigned char *allocated)
  10412. +{
  10413. + int err, v;
  10414. + struct au_drinfo_fdata *f, *p;
  10415. + struct au_drinfo *drinfo;
  10416. + struct inode *h_inode;
  10417. + struct qstr *qname;
  10418. +
  10419. + err = 0;
  10420. + f = *fdata;
  10421. + h_inode = d_inode(h_dentry);
  10422. + qname = &h_dentry->d_name;
  10423. + drinfo = &f->drinfo;
  10424. + drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
  10425. + drinfo->oldnamelen = qname->len;
  10426. + if (*allocated < sizeof(*f) + qname->len) {
  10427. + v = roundup_pow_of_two(*allocated + qname->len);
  10428. + p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
  10429. + if (unlikely(!p)) {
  10430. + err = -ENOMEM;
  10431. + AuTraceErr(err);
  10432. + goto out;
  10433. + }
  10434. + f = p;
  10435. + *fdata = f;
  10436. + *allocated = v;
  10437. + drinfo = &f->drinfo;
  10438. + }
  10439. + memcpy(drinfo->oldname, qname->name, qname->len);
  10440. + AuDbg("i%llu, %.*s\n",
  10441. + be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
  10442. + drinfo->oldname);
  10443. +
  10444. +out:
  10445. + AuTraceErr(err);
  10446. + return err;
  10447. +}
  10448. +
  10449. +/* callers have to free the return value */
  10450. +static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
  10451. +{
  10452. + struct au_drinfo *ret, *drinfo;
  10453. + struct au_drinfo_fdata fdata;
  10454. + int len;
  10455. + loff_t pos;
  10456. + ssize_t ssz;
  10457. +
  10458. + ret = ERR_PTR(-EIO);
  10459. + pos = 0;
  10460. + ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
  10461. + if (unlikely(ssz != sizeof(fdata))) {
  10462. + AuIOErr("ssz %zd, %u, %pD2\n",
  10463. + ssz, (unsigned int)sizeof(fdata), file);
  10464. + goto out;
  10465. + }
  10466. +
  10467. + fdata.magic = ntohl((__force __be32)fdata.magic);
  10468. + switch (fdata.magic) {
  10469. + case AUFS_DRINFO_MAGIC_V1:
  10470. + break;
  10471. + default:
  10472. + AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
  10473. + fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
  10474. + goto out;
  10475. + }
  10476. +
  10477. + drinfo = &fdata.drinfo;
  10478. + len = drinfo->oldnamelen;
  10479. + if (!len) {
  10480. + AuIOErr("broken drinfo %pD2\n", file);
  10481. + goto out;
  10482. + }
  10483. +
  10484. + ret = NULL;
  10485. + drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
  10486. + if (unlikely(h_ino && drinfo->ino != h_ino)) {
  10487. + AuDbg("ignored i%llu, i%llu, %pD2\n",
  10488. + (unsigned long long)drinfo->ino,
  10489. + (unsigned long long)h_ino, file);
  10490. + goto out; /* success */
  10491. + }
  10492. +
  10493. + ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
  10494. + if (unlikely(!ret)) {
  10495. + ret = ERR_PTR(-ENOMEM);
  10496. + AuTraceErrPtr(ret);
  10497. + goto out;
  10498. + }
  10499. +
  10500. + *ret = *drinfo;
  10501. + ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
  10502. + if (unlikely(ssz != len)) {
  10503. + au_kfree_rcu(ret);
  10504. + ret = ERR_PTR(-EIO);
  10505. + AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
  10506. + goto out;
  10507. + }
  10508. +
  10509. + AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
  10510. +
  10511. +out:
  10512. + return ret;
  10513. +}
  10514. +
  10515. +/* ---------------------------------------------------------------------- */
  10516. +
  10517. +/* in order to be revertible */
  10518. +struct au_drinfo_rev_elm {
  10519. + int created;
  10520. + struct dentry *info_dentry;
  10521. + struct au_drinfo *info_last;
  10522. +};
  10523. +
  10524. +struct au_drinfo_rev {
  10525. + unsigned char already;
  10526. + aufs_bindex_t nelm;
  10527. + struct au_drinfo_rev_elm elm[];
  10528. +};
  10529. +
  10530. +/* todo: isn't it too large? */
  10531. +struct au_drinfo_store {
  10532. + struct path h_ppath;
  10533. + struct dentry *h_dentry;
  10534. + struct au_drinfo_fdata *fdata;
  10535. + char *infoname; /* inside of whname, just after PFX */
  10536. + char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
  10537. + aufs_bindex_t btgt, btail;
  10538. + unsigned char no_sio,
  10539. + allocated, /* current size of *fdata */
  10540. + infonamelen, /* room size for p */
  10541. + whnamelen, /* length of the generated name */
  10542. + renameback; /* renamed back */
  10543. +};
  10544. +
  10545. +/* on rename(2) error, the caller should revert it using @elm */
  10546. +static int au_drinfo_do_store(struct au_drinfo_store *w,
  10547. + struct au_drinfo_rev_elm *elm)
  10548. +{
  10549. + int err, len;
  10550. + ssize_t ssz;
  10551. + loff_t pos;
  10552. + struct path infopath = {
  10553. + .mnt = w->h_ppath.mnt
  10554. + };
  10555. + struct inode *h_dir, *h_inode, *delegated;
  10556. + struct file *infofile;
  10557. + struct qstr *qname;
  10558. +
  10559. + AuDebugOn(elm
  10560. + && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
  10561. +
  10562. + infopath.dentry = vfsub_lookup_one_len(w->whname, &w->h_ppath,
  10563. + w->whnamelen);
  10564. + AuTraceErrPtr(infopath.dentry);
  10565. + if (IS_ERR(infopath.dentry)) {
  10566. + err = PTR_ERR(infopath.dentry);
  10567. + goto out;
  10568. + }
  10569. +
  10570. + err = 0;
  10571. + h_dir = d_inode(w->h_ppath.dentry);
  10572. + if (elm && d_is_negative(infopath.dentry)) {
  10573. + err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
  10574. + AuTraceErr(err);
  10575. + if (unlikely(err))
  10576. + goto out_dput;
  10577. + elm->created = 1;
  10578. + elm->info_dentry = dget(infopath.dentry);
  10579. + }
  10580. +
  10581. + infofile = vfsub_dentry_open(&infopath, O_RDWR);
  10582. + AuTraceErrPtr(infofile);
  10583. + if (IS_ERR(infofile)) {
  10584. + err = PTR_ERR(infofile);
  10585. + goto out_dput;
  10586. + }
  10587. +
  10588. + h_inode = d_inode(infopath.dentry);
  10589. + if (elm && i_size_read(h_inode)) {
  10590. + h_inode = d_inode(w->h_dentry);
  10591. + elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
  10592. + AuTraceErrPtr(elm->info_last);
  10593. + if (IS_ERR(elm->info_last)) {
  10594. + err = PTR_ERR(elm->info_last);
  10595. + elm->info_last = NULL;
  10596. + AuDebugOn(elm->info_dentry);
  10597. + goto out_fput;
  10598. + }
  10599. + }
  10600. +
  10601. + if (elm && w->renameback) {
  10602. + delegated = NULL;
  10603. + err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
  10604. + AuTraceErr(err);
  10605. + if (unlikely(err == -EWOULDBLOCK))
  10606. + iput(delegated);
  10607. + goto out_fput;
  10608. + }
  10609. +
  10610. + pos = 0;
  10611. + qname = &w->h_dentry->d_name;
  10612. + len = sizeof(*w->fdata) + qname->len;
  10613. + if (!elm)
  10614. + len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
  10615. + ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
  10616. + if (ssz == len) {
  10617. + AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
  10618. + w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
  10619. + goto out_fput; /* success */
  10620. + } else {
  10621. + err = -EIO;
  10622. + if (ssz < 0)
  10623. + err = ssz;
  10624. + /* the caller should revert it using @elm */
  10625. + }
  10626. +
  10627. +out_fput:
  10628. + fput(infofile);
  10629. +out_dput:
  10630. + dput(infopath.dentry);
  10631. +out:
  10632. + AuTraceErr(err);
  10633. + return err;
  10634. +}
  10635. +
  10636. +struct au_call_drinfo_do_store_args {
  10637. + int *errp;
  10638. + struct au_drinfo_store *w;
  10639. + struct au_drinfo_rev_elm *elm;
  10640. +};
  10641. +
  10642. +static void au_call_drinfo_do_store(void *args)
  10643. +{
  10644. + struct au_call_drinfo_do_store_args *a = args;
  10645. +
  10646. + *a->errp = au_drinfo_do_store(a->w, a->elm);
  10647. +}
  10648. +
  10649. +static int au_drinfo_store_sio(struct au_drinfo_store *w,
  10650. + struct au_drinfo_rev_elm *elm)
  10651. +{
  10652. + int err, wkq_err;
  10653. +
  10654. + if (w->no_sio)
  10655. + err = au_drinfo_do_store(w, elm);
  10656. + else {
  10657. + struct au_call_drinfo_do_store_args a = {
  10658. + .errp = &err,
  10659. + .w = w,
  10660. + .elm = elm
  10661. + };
  10662. + wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
  10663. + if (unlikely(wkq_err))
  10664. + err = wkq_err;
  10665. + }
  10666. + AuTraceErr(err);
  10667. +
  10668. + return err;
  10669. +}
  10670. +
  10671. +static int au_drinfo_store_work_init(struct au_drinfo_store *w,
  10672. + aufs_bindex_t btgt)
  10673. +{
  10674. + int err;
  10675. +
  10676. + memset(w, 0, sizeof(*w));
  10677. + w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
  10678. + strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
  10679. + w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
  10680. + w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
  10681. + w->btgt = btgt;
  10682. + w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
  10683. +
  10684. + err = -ENOMEM;
  10685. + w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
  10686. + if (unlikely(!w->fdata)) {
  10687. + AuTraceErr(err);
  10688. + goto out;
  10689. + }
  10690. + w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
  10691. + err = 0;
  10692. +
  10693. +out:
  10694. + return err;
  10695. +}
  10696. +
  10697. +static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
  10698. +{
  10699. + au_kfree_rcu(w->fdata);
  10700. +}
  10701. +
  10702. +static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
  10703. + struct au_drinfo_store *w)
  10704. +{
  10705. + struct au_drinfo_rev_elm *elm;
  10706. + struct inode *h_dir, *delegated;
  10707. + int err, nelm;
  10708. + struct path infopath = {
  10709. + .mnt = w->h_ppath.mnt
  10710. + };
  10711. +
  10712. + h_dir = d_inode(w->h_ppath.dentry);
  10713. + IMustLock(h_dir);
  10714. +
  10715. + err = 0;
  10716. + elm = rev->elm;
  10717. + for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
  10718. + AuDebugOn(elm->created && elm->info_last);
  10719. + if (elm->created) {
  10720. + AuDbg("here\n");
  10721. + delegated = NULL;
  10722. + infopath.dentry = elm->info_dentry;
  10723. + err = vfsub_unlink(h_dir, &infopath, &delegated,
  10724. + !w->no_sio);
  10725. + AuTraceErr(err);
  10726. + if (unlikely(err == -EWOULDBLOCK))
  10727. + iput(delegated);
  10728. + dput(elm->info_dentry);
  10729. + } else if (elm->info_last) {
  10730. + AuDbg("here\n");
  10731. + w->fdata->drinfo = *elm->info_last;
  10732. + memcpy(w->fdata->drinfo.oldname,
  10733. + elm->info_last->oldname,
  10734. + elm->info_last->oldnamelen);
  10735. + err = au_drinfo_store_sio(w, /*elm*/NULL);
  10736. + au_kfree_rcu(elm->info_last);
  10737. + }
  10738. + if (unlikely(err))
  10739. + AuIOErr("%d, %s\n", err, w->whname);
  10740. + /* go on even if err */
  10741. + }
  10742. +}
  10743. +
  10744. +/* caller has to call au_dr_rename_fin() later */
  10745. +static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
  10746. + struct qstr *dst_name, void *_rev)
  10747. +{
  10748. + int err, sz, nelm;
  10749. + aufs_bindex_t bindex, btail;
  10750. + struct au_drinfo_store work;
  10751. + struct au_drinfo_rev *rev, **p;
  10752. + struct au_drinfo_rev_elm *elm;
  10753. + struct super_block *sb;
  10754. + struct au_branch *br;
  10755. + struct au_hinode *hdir;
  10756. +
  10757. + err = au_drinfo_store_work_init(&work, btgt);
  10758. + AuTraceErr(err);
  10759. + if (unlikely(err))
  10760. + goto out;
  10761. +
  10762. + err = -ENOMEM;
  10763. + btail = au_dbtaildir(dentry);
  10764. + nelm = btail - btgt;
  10765. + sz = sizeof(*rev) + sizeof(*elm) * nelm;
  10766. + rev = kcalloc(1, sz, GFP_NOFS);
  10767. + if (unlikely(!rev)) {
  10768. + AuTraceErr(err);
  10769. + goto out_args;
  10770. + }
  10771. + rev->nelm = nelm;
  10772. + elm = rev->elm;
  10773. + p = _rev;
  10774. + *p = rev;
  10775. +
  10776. + err = 0;
  10777. + sb = dentry->d_sb;
  10778. + work.h_ppath.dentry = au_h_dptr(dentry, btgt);
  10779. + work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
  10780. + hdir = au_hi(d_inode(dentry), btgt);
  10781. + au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
  10782. + for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
  10783. + work.h_dentry = au_h_dptr(dentry, bindex);
  10784. + if (!work.h_dentry)
  10785. + continue;
  10786. +
  10787. + err = au_drinfo_construct(&work.fdata, work.h_dentry,
  10788. + &work.allocated);
  10789. + AuTraceErr(err);
  10790. + if (unlikely(err))
  10791. + break;
  10792. +
  10793. + work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
  10794. + br = au_sbr(sb, bindex);
  10795. + work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
  10796. + work.whnamelen += au_drinfo_name(br, work.infoname,
  10797. + work.infonamelen);
  10798. + AuDbg("whname %.*s, i%llu, %.*s\n",
  10799. + work.whnamelen, work.whname,
  10800. + be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
  10801. + work.fdata->drinfo.oldnamelen,
  10802. + work.fdata->drinfo.oldname);
  10803. +
  10804. + err = au_drinfo_store_sio(&work, elm);
  10805. + AuTraceErr(err);
  10806. + if (unlikely(err))
  10807. + break;
  10808. + }
  10809. + if (unlikely(err)) {
  10810. + /* revert all drinfo */
  10811. + au_drinfo_store_rev(rev, &work);
  10812. + au_kfree_try_rcu(rev);
  10813. + *p = NULL;
  10814. + }
  10815. + au_hn_inode_unlock(hdir);
  10816. +
  10817. +out_args:
  10818. + au_drinfo_store_work_fin(&work);
  10819. +out:
  10820. + return err;
  10821. +}
  10822. +
  10823. +/* ---------------------------------------------------------------------- */
  10824. +
  10825. +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
  10826. + struct qstr *dst_name, void *_rev)
  10827. +{
  10828. + int err, already;
  10829. + ino_t ino;
  10830. + struct super_block *sb;
  10831. + struct au_branch *br;
  10832. + struct au_dr_br *dr;
  10833. + struct dentry *h_dentry;
  10834. + struct inode *h_inode;
  10835. + struct au_dr_hino *ent;
  10836. + struct au_drinfo_rev *rev, **p;
  10837. +
  10838. + AuDbg("bindex %d\n", bindex);
  10839. +
  10840. + err = -ENOMEM;
  10841. + ent = kmalloc(sizeof(*ent), GFP_NOFS);
  10842. + if (unlikely(!ent))
  10843. + goto out;
  10844. +
  10845. + sb = src->d_sb;
  10846. + br = au_sbr(sb, bindex);
  10847. + dr = &br->br_dirren;
  10848. + h_dentry = au_h_dptr(src, bindex);
  10849. + h_inode = d_inode(h_dentry);
  10850. + ino = h_inode->i_ino;
  10851. + ent->dr_h_ino = ino;
  10852. + already = au_dr_hino_test_add(dr, ino, ent);
  10853. + AuDbg("b%d, hi%llu, already %d\n",
  10854. + bindex, (unsigned long long)ino, already);
  10855. +
  10856. + err = au_drinfo_store(src, bindex, dst_name, _rev);
  10857. + AuTraceErr(err);
  10858. + if (!err) {
  10859. + p = _rev;
  10860. + rev = *p;
  10861. + rev->already = already;
  10862. + goto out; /* success */
  10863. + }
  10864. +
  10865. + /* revert */
  10866. + if (!already)
  10867. + au_dr_hino_del(dr, ent);
  10868. + au_kfree_rcu(ent);
  10869. +
  10870. +out:
  10871. + AuTraceErr(err);
  10872. + return err;
  10873. +}
  10874. +
  10875. +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
  10876. +{
  10877. + struct au_drinfo_rev *rev;
  10878. + struct au_drinfo_rev_elm *elm;
  10879. + int nelm;
  10880. +
  10881. + rev = _rev;
  10882. + elm = rev->elm;
  10883. + for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
  10884. + dput(elm->info_dentry);
  10885. + au_kfree_rcu(elm->info_last);
  10886. + }
  10887. + au_kfree_try_rcu(rev);
  10888. +}
  10889. +
  10890. +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
  10891. +{
  10892. + int err;
  10893. + struct au_drinfo_store work;
  10894. + struct au_drinfo_rev *rev = _rev;
  10895. + struct super_block *sb;
  10896. + struct au_branch *br;
  10897. + struct inode *h_inode;
  10898. + struct au_dr_br *dr;
  10899. + struct au_dr_hino *ent;
  10900. +
  10901. + err = au_drinfo_store_work_init(&work, btgt);
  10902. + if (unlikely(err))
  10903. + goto out;
  10904. +
  10905. + sb = src->d_sb;
  10906. + br = au_sbr(sb, btgt);
  10907. + work.h_ppath.dentry = au_h_dptr(src, btgt);
  10908. + work.h_ppath.mnt = au_br_mnt(br);
  10909. + au_drinfo_store_rev(rev, &work);
  10910. + au_drinfo_store_work_fin(&work);
  10911. + if (rev->already)
  10912. + goto out;
  10913. +
  10914. + dr = &br->br_dirren;
  10915. + h_inode = d_inode(work.h_ppath.dentry);
  10916. + ent = au_dr_hino_find(dr, h_inode->i_ino);
  10917. + BUG_ON(!ent);
  10918. + au_dr_hino_del(dr, ent);
  10919. + au_kfree_rcu(ent);
  10920. +
  10921. +out:
  10922. + au_kfree_try_rcu(rev);
  10923. + if (unlikely(err))
  10924. + pr_err("failed to remove dirren info\n");
  10925. +}
  10926. +
  10927. +/* ---------------------------------------------------------------------- */
  10928. +
  10929. +static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
  10930. + char *whname, int whnamelen,
  10931. + struct dentry **info_dentry)
  10932. +{
  10933. + struct au_drinfo *drinfo;
  10934. + struct file *f;
  10935. + struct inode *h_dir;
  10936. + struct path infopath;
  10937. + int unlocked;
  10938. +
  10939. + AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
  10940. +
  10941. + *info_dentry = NULL;
  10942. + drinfo = NULL;
  10943. + unlocked = 0;
  10944. + h_dir = d_inode(h_ppath->dentry);
  10945. + inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
  10946. + infopath.dentry = vfsub_lookup_one_len(whname, h_ppath, whnamelen);
  10947. + if (IS_ERR(infopath.dentry)) {
  10948. + drinfo = (void *)infopath.dentry;
  10949. + goto out;
  10950. + }
  10951. +
  10952. + if (d_is_negative(infopath.dentry))
  10953. + goto out_dput; /* success */
  10954. +
  10955. + infopath.mnt = h_ppath->mnt;
  10956. + f = vfsub_dentry_open(&infopath, O_RDONLY);
  10957. + inode_unlock_shared(h_dir);
  10958. + unlocked = 1;
  10959. + if (IS_ERR(f)) {
  10960. + drinfo = (void *)f;
  10961. + goto out_dput;
  10962. + }
  10963. +
  10964. + drinfo = au_drinfo_read_k(f, /*h_ino*/0);
  10965. + if (IS_ERR_OR_NULL(drinfo))
  10966. + goto out_fput;
  10967. +
  10968. + AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
  10969. + *info_dentry = dget(infopath.dentry); /* keep it alive */
  10970. +
  10971. +out_fput:
  10972. + fput(f);
  10973. +out_dput:
  10974. + dput(infopath.dentry);
  10975. +out:
  10976. + if (!unlocked)
  10977. + inode_unlock_shared(h_dir);
  10978. + AuTraceErrPtr(drinfo);
  10979. + return drinfo;
  10980. +}
  10981. +
  10982. +struct au_drinfo_do_load_args {
  10983. + struct au_drinfo **drinfop;
  10984. + struct path *h_ppath;
  10985. + char *whname;
  10986. + int whnamelen;
  10987. + struct dentry **info_dentry;
  10988. +};
  10989. +
  10990. +static void au_call_drinfo_do_load(void *args)
  10991. +{
  10992. + struct au_drinfo_do_load_args *a = args;
  10993. +
  10994. + *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
  10995. + a->info_dentry);
  10996. +}
  10997. +
  10998. +struct au_drinfo_load {
  10999. + struct path h_ppath;
  11000. + struct qstr *qname;
  11001. + unsigned char no_sio;
  11002. +
  11003. + aufs_bindex_t ninfo;
  11004. + struct au_drinfo **drinfo;
  11005. +};
  11006. +
  11007. +static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
  11008. + struct au_branch *br)
  11009. +{
  11010. + int err, wkq_err, whnamelen, e;
  11011. + char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
  11012. + = AUFS_WH_DR_INFO_PFX;
  11013. + struct au_drinfo *drinfo;
  11014. + struct qstr oldname;
  11015. + struct inode *h_dir, *delegated;
  11016. + struct dentry *info_dentry;
  11017. + struct path infopath;
  11018. +
  11019. + whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
  11020. + whnamelen += au_drinfo_name(br, whname + whnamelen,
  11021. + sizeof(whname) - whnamelen);
  11022. + if (w->no_sio)
  11023. + drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
  11024. + &info_dentry);
  11025. + else {
  11026. + struct au_drinfo_do_load_args args = {
  11027. + .drinfop = &drinfo,
  11028. + .h_ppath = &w->h_ppath,
  11029. + .whname = whname,
  11030. + .whnamelen = whnamelen,
  11031. + .info_dentry = &info_dentry
  11032. + };
  11033. + wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
  11034. + if (unlikely(wkq_err))
  11035. + drinfo = ERR_PTR(wkq_err);
  11036. + }
  11037. + err = PTR_ERR(drinfo);
  11038. + if (IS_ERR_OR_NULL(drinfo))
  11039. + goto out;
  11040. +
  11041. + err = 0;
  11042. + oldname.len = drinfo->oldnamelen;
  11043. + oldname.name = drinfo->oldname;
  11044. + if (au_qstreq(w->qname, &oldname)) {
  11045. + /* the name is renamed back */
  11046. + au_kfree_rcu(drinfo);
  11047. + drinfo = NULL;
  11048. +
  11049. + infopath.dentry = info_dentry;
  11050. + infopath.mnt = w->h_ppath.mnt;
  11051. + h_dir = d_inode(w->h_ppath.dentry);
  11052. + delegated = NULL;
  11053. + inode_lock_nested(h_dir, AuLsc_I_PARENT);
  11054. + e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
  11055. + inode_unlock(h_dir);
  11056. + if (unlikely(e))
  11057. + AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
  11058. + if (unlikely(e == -EWOULDBLOCK))
  11059. + iput(delegated);
  11060. + }
  11061. + au_kfree_rcu(w->drinfo[bindex]);
  11062. + w->drinfo[bindex] = drinfo;
  11063. + dput(info_dentry);
  11064. +
  11065. +out:
  11066. + AuTraceErr(err);
  11067. + return err;
  11068. +}
  11069. +
  11070. +/* ---------------------------------------------------------------------- */
  11071. +
  11072. +static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
  11073. +{
  11074. + struct au_drinfo **p = drinfo;
  11075. +
  11076. + while (n-- > 0)
  11077. + au_kfree_rcu(*drinfo++);
  11078. + au_kfree_try_rcu(p);
  11079. +}
  11080. +
  11081. +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
  11082. + aufs_bindex_t btgt)
  11083. +{
  11084. + int err, ninfo;
  11085. + struct au_drinfo_load w;
  11086. + aufs_bindex_t bindex, bbot;
  11087. + struct au_branch *br;
  11088. + struct inode *h_dir;
  11089. + struct au_dr_hino *ent;
  11090. + struct super_block *sb;
  11091. +
  11092. + AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
  11093. + AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
  11094. + AuLNPair(&lkup->whname), btgt);
  11095. +
  11096. + sb = dentry->d_sb;
  11097. + bbot = au_sbbot(sb);
  11098. + w.ninfo = bbot + 1;
  11099. + if (!lkup->dirren.drinfo) {
  11100. + lkup->dirren.drinfo = kcalloc(w.ninfo,
  11101. + sizeof(*lkup->dirren.drinfo),
  11102. + GFP_NOFS);
  11103. + if (unlikely(!lkup->dirren.drinfo)) {
  11104. + err = -ENOMEM;
  11105. + goto out;
  11106. + }
  11107. + lkup->dirren.ninfo = w.ninfo;
  11108. + }
  11109. + w.drinfo = lkup->dirren.drinfo;
  11110. + w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
  11111. + w.h_ppath.dentry = au_h_dptr(dentry, btgt);
  11112. + AuDebugOn(!w.h_ppath.dentry);
  11113. + w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
  11114. + w.qname = &dentry->d_name;
  11115. +
  11116. + ninfo = 0;
  11117. + for (bindex = btgt + 1; bindex <= bbot; bindex++) {
  11118. + br = au_sbr(sb, bindex);
  11119. + err = au_drinfo_load(&w, bindex, br);
  11120. + if (unlikely(err))
  11121. + goto out_free;
  11122. + if (w.drinfo[bindex])
  11123. + ninfo++;
  11124. + }
  11125. + if (!ninfo) {
  11126. + br = au_sbr(sb, btgt);
  11127. + h_dir = d_inode(w.h_ppath.dentry);
  11128. + ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
  11129. + AuDebugOn(!ent);
  11130. + au_dr_hino_del(&br->br_dirren, ent);
  11131. + au_kfree_rcu(ent);
  11132. + }
  11133. + goto out; /* success */
  11134. +
  11135. +out_free:
  11136. + au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
  11137. + lkup->dirren.ninfo = 0;
  11138. + lkup->dirren.drinfo = NULL;
  11139. +out:
  11140. + AuTraceErr(err);
  11141. + return err;
  11142. +}
  11143. +
  11144. +void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
  11145. +{
  11146. + au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
  11147. +}
  11148. +
  11149. +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
  11150. +{
  11151. + int err;
  11152. + struct au_drinfo *drinfo;
  11153. +
  11154. + err = 0;
  11155. + if (!lkup->dirren.drinfo)
  11156. + goto out;
  11157. + AuDebugOn(lkup->dirren.ninfo <= btgt);
  11158. + drinfo = lkup->dirren.drinfo[btgt];
  11159. + if (!drinfo)
  11160. + goto out;
  11161. +
  11162. + au_kfree_try_rcu(lkup->whname.name);
  11163. + lkup->whname.name = NULL;
  11164. + lkup->dirren.dr_name.len = drinfo->oldnamelen;
  11165. + lkup->dirren.dr_name.name = drinfo->oldname;
  11166. + lkup->name = &lkup->dirren.dr_name;
  11167. + err = au_wh_name_alloc(&lkup->whname, lkup->name);
  11168. + if (!err)
  11169. + AuDbg("name %.*s, whname %.*s, b%d\n",
  11170. + AuLNPair(lkup->name), AuLNPair(&lkup->whname),
  11171. + btgt);
  11172. +
  11173. +out:
  11174. + AuTraceErr(err);
  11175. + return err;
  11176. +}
  11177. +
  11178. +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
  11179. + ino_t h_ino)
  11180. +{
  11181. + int match;
  11182. + struct au_drinfo *drinfo;
  11183. +
  11184. + match = 1;
  11185. + if (!lkup->dirren.drinfo)
  11186. + goto out;
  11187. + AuDebugOn(lkup->dirren.ninfo <= bindex);
  11188. + drinfo = lkup->dirren.drinfo[bindex];
  11189. + if (!drinfo)
  11190. + goto out;
  11191. +
  11192. + match = (drinfo->ino == h_ino);
  11193. + AuDbg("match %d\n", match);
  11194. +
  11195. +out:
  11196. + return match;
  11197. +}
  11198. +
  11199. +/* ---------------------------------------------------------------------- */
  11200. +
  11201. +int au_dr_opt_set(struct super_block *sb)
  11202. +{
  11203. + int err;
  11204. + aufs_bindex_t bindex, bbot;
  11205. + struct au_branch *br;
  11206. +
  11207. + err = 0;
  11208. + bbot = au_sbbot(sb);
  11209. + for (bindex = 0; !err && bindex <= bbot; bindex++) {
  11210. + br = au_sbr(sb, bindex);
  11211. + err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
  11212. + }
  11213. +
  11214. + return err;
  11215. +}
  11216. +
  11217. +int au_dr_opt_flush(struct super_block *sb)
  11218. +{
  11219. + int err;
  11220. + aufs_bindex_t bindex, bbot;
  11221. + struct au_branch *br;
  11222. +
  11223. + err = 0;
  11224. + bbot = au_sbbot(sb);
  11225. + for (bindex = 0; !err && bindex <= bbot; bindex++) {
  11226. + br = au_sbr(sb, bindex);
  11227. + if (au_br_writable(br->br_perm))
  11228. + err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
  11229. + }
  11230. +
  11231. + return err;
  11232. +}
  11233. +
  11234. +int au_dr_opt_clr(struct super_block *sb, int no_flush)
  11235. +{
  11236. + int err;
  11237. + aufs_bindex_t bindex, bbot;
  11238. + struct au_branch *br;
  11239. +
  11240. + err = 0;
  11241. + if (!no_flush) {
  11242. + err = au_dr_opt_flush(sb);
  11243. + if (unlikely(err))
  11244. + goto out;
  11245. + }
  11246. +
  11247. + bbot = au_sbbot(sb);
  11248. + for (bindex = 0; bindex <= bbot; bindex++) {
  11249. + br = au_sbr(sb, bindex);
  11250. + au_dr_hino_free(&br->br_dirren);
  11251. + }
  11252. +
  11253. +out:
  11254. + return err;
  11255. +}
  11256. diff -Naur null/fs/aufs/dirren.h linux-5.15.131/fs/aufs/dirren.h
  11257. --- /dev/null
  11258. +++ linux-5.15.131/fs/aufs/dirren.h 2023-09-11 14:32:48.147722564 +0300
  11259. @@ -0,0 +1,140 @@
  11260. +/* SPDX-License-Identifier: GPL-2.0 */
  11261. +/*
  11262. + * Copyright (C) 2017-2021 Junjiro R. Okajima
  11263. + *
  11264. + * This program, aufs is free software; you can redistribute it and/or modify
  11265. + * it under the terms of the GNU General Public License as published by
  11266. + * the Free Software Foundation; either version 2 of the License, or
  11267. + * (at your option) any later version.
  11268. + *
  11269. + * This program is distributed in the hope that it will be useful,
  11270. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11271. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11272. + * GNU General Public License for more details.
  11273. + *
  11274. + * You should have received a copy of the GNU General Public License
  11275. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  11276. + */
  11277. +
  11278. +/*
  11279. + * renamed dir info
  11280. + */
  11281. +
  11282. +#ifndef __AUFS_DIRREN_H__
  11283. +#define __AUFS_DIRREN_H__
  11284. +
  11285. +#ifdef __KERNEL__
  11286. +
  11287. +#include <linux/dcache.h>
  11288. +#include <linux/statfs.h>
  11289. +#include <linux/uuid.h>
  11290. +#include "hbl.h"
  11291. +
  11292. +#define AuDirren_NHASH 100
  11293. +
  11294. +#ifdef CONFIG_AUFS_DIRREN
  11295. +enum au_brid_type {
  11296. + AuBrid_Unset,
  11297. + AuBrid_UUID,
  11298. + AuBrid_FSID,
  11299. + AuBrid_DEV
  11300. +};
  11301. +
  11302. +struct au_dr_brid {
  11303. + enum au_brid_type type;
  11304. + union {
  11305. + uuid_t uuid; /* unimplemented yet */
  11306. + fsid_t fsid;
  11307. + dev_t dev;
  11308. + };
  11309. +};
  11310. +
  11311. +/* 20 is the max digits length of ulong 64 */
  11312. +/* brid-type "_" uuid "_" inum */
  11313. +#define AUFS_DIRREN_FNAME_SZ (1 + 1 + UUID_STRING_LEN + 20)
  11314. +#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
  11315. +
  11316. +struct au_dr_hino {
  11317. + struct hlist_bl_node dr_hnode;
  11318. + ino_t dr_h_ino;
  11319. +};
  11320. +
  11321. +struct au_dr_br {
  11322. + struct hlist_bl_head dr_h_ino[AuDirren_NHASH];
  11323. + struct au_dr_brid dr_brid;
  11324. +};
  11325. +
  11326. +struct au_dr_lookup {
  11327. + /* dr_name is pointed by struct au_do_lookup_args.name */
  11328. + struct qstr dr_name; /* subset of dr_info */
  11329. + aufs_bindex_t ninfo;
  11330. + struct au_drinfo **drinfo;
  11331. +};
  11332. +#else
  11333. +struct au_dr_hino;
  11334. +/* empty */
  11335. +struct au_dr_br { };
  11336. +struct au_dr_lookup { };
  11337. +#endif
  11338. +
  11339. +/* ---------------------------------------------------------------------- */
  11340. +
  11341. +struct au_branch;
  11342. +struct au_do_lookup_args;
  11343. +struct au_hinode;
  11344. +#ifdef CONFIG_AUFS_DIRREN
  11345. +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
  11346. + struct au_dr_hino *add_ent);
  11347. +void au_dr_hino_free(struct au_dr_br *dr);
  11348. +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
  11349. + const struct path *path);
  11350. +int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
  11351. +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
  11352. + struct qstr *dst_name, void *_rev);
  11353. +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
  11354. +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
  11355. +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
  11356. + aufs_bindex_t bindex);
  11357. +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
  11358. +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
  11359. + ino_t h_ino);
  11360. +void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
  11361. +int au_dr_opt_set(struct super_block *sb);
  11362. +int au_dr_opt_flush(struct super_block *sb);
  11363. +int au_dr_opt_clr(struct super_block *sb, int no_flush);
  11364. +#else
  11365. +AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
  11366. + struct au_dr_hino *add_ent);
  11367. +AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
  11368. +AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
  11369. + const struct path *path);
  11370. +AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
  11371. +AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
  11372. + struct qstr *dst_name, void *_rev);
  11373. +AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
  11374. +AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
  11375. + void *rev);
  11376. +AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
  11377. + aufs_bindex_t bindex);
  11378. +AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
  11379. +AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
  11380. + aufs_bindex_t bindex, ino_t h_ino);
  11381. +AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
  11382. +AuStubInt0(au_dr_opt_set, struct super_block *sb);
  11383. +AuStubInt0(au_dr_opt_flush, struct super_block *sb);
  11384. +AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
  11385. +#endif
  11386. +
  11387. +/* ---------------------------------------------------------------------- */
  11388. +
  11389. +#ifdef CONFIG_AUFS_DIRREN
  11390. +static inline int au_dr_ihash(ino_t h_ino)
  11391. +{
  11392. + return h_ino % AuDirren_NHASH;
  11393. +}
  11394. +#else
  11395. +AuStubInt0(au_dr_ihash, ino_t h_ino);
  11396. +#endif
  11397. +
  11398. +#endif /* __KERNEL__ */
  11399. +#endif /* __AUFS_DIRREN_H__ */
  11400. diff -Naur null/fs/aufs/dynop.c linux-5.15.131/fs/aufs/dynop.c
  11401. --- /dev/null
  11402. +++ linux-5.15.131/fs/aufs/dynop.c 2023-09-11 14:32:48.147722564 +0300
  11403. @@ -0,0 +1,368 @@
  11404. +// SPDX-License-Identifier: GPL-2.0
  11405. +/*
  11406. + * Copyright (C) 2010-2021 Junjiro R. Okajima
  11407. + *
  11408. + * This program, aufs is free software; you can redistribute it and/or modify
  11409. + * it under the terms of the GNU General Public License as published by
  11410. + * the Free Software Foundation; either version 2 of the License, or
  11411. + * (at your option) any later version.
  11412. + *
  11413. + * This program is distributed in the hope that it will be useful,
  11414. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11415. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11416. + * GNU General Public License for more details.
  11417. + *
  11418. + * You should have received a copy of the GNU General Public License
  11419. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  11420. + */
  11421. +
  11422. +/*
  11423. + * dynamically customizable operations for regular files
  11424. + */
  11425. +
  11426. +#include "aufs.h"
  11427. +
  11428. +#define DyPrSym(key) AuDbgSym(key->dk_op.dy_hop)
  11429. +
  11430. +/*
  11431. + * How large will these lists be?
  11432. + * Usually just a few elements, 20-30 at most for each, I guess.
  11433. + */
  11434. +static struct hlist_bl_head dynop[AuDyLast];
  11435. +
  11436. +static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
  11437. + const void *h_op)
  11438. +{
  11439. + struct au_dykey *key, *tmp;
  11440. + struct hlist_bl_node *pos;
  11441. +
  11442. + key = NULL;
  11443. + hlist_bl_lock(hbl);
  11444. + hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
  11445. + if (tmp->dk_op.dy_hop == h_op) {
  11446. + if (kref_get_unless_zero(&tmp->dk_kref))
  11447. + key = tmp;
  11448. + break;
  11449. + }
  11450. + hlist_bl_unlock(hbl);
  11451. +
  11452. + return key;
  11453. +}
  11454. +
  11455. +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
  11456. +{
  11457. + struct au_dykey **k, *found;
  11458. + const void *h_op = key->dk_op.dy_hop;
  11459. + int i;
  11460. +
  11461. + found = NULL;
  11462. + k = br->br_dykey;
  11463. + for (i = 0; i < AuBrDynOp; i++)
  11464. + if (k[i]) {
  11465. + if (k[i]->dk_op.dy_hop == h_op) {
  11466. + found = k[i];
  11467. + break;
  11468. + }
  11469. + } else
  11470. + break;
  11471. + if (!found) {
  11472. + spin_lock(&br->br_dykey_lock);
  11473. + for (; i < AuBrDynOp; i++)
  11474. + if (k[i]) {
  11475. + if (k[i]->dk_op.dy_hop == h_op) {
  11476. + found = k[i];
  11477. + break;
  11478. + }
  11479. + } else {
  11480. + k[i] = key;
  11481. + break;
  11482. + }
  11483. + spin_unlock(&br->br_dykey_lock);
  11484. + BUG_ON(i == AuBrDynOp); /* expand the array */
  11485. + }
  11486. +
  11487. + return found;
  11488. +}
  11489. +
  11490. +/* kref_get() if @key is already added */
  11491. +static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
  11492. +{
  11493. + struct au_dykey *tmp, *found;
  11494. + struct hlist_bl_node *pos;
  11495. + const void *h_op = key->dk_op.dy_hop;
  11496. +
  11497. + found = NULL;
  11498. + hlist_bl_lock(hbl);
  11499. + hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
  11500. + if (tmp->dk_op.dy_hop == h_op) {
  11501. + if (kref_get_unless_zero(&tmp->dk_kref))
  11502. + found = tmp;
  11503. + break;
  11504. + }
  11505. + if (!found)
  11506. + hlist_bl_add_head(&key->dk_hnode, hbl);
  11507. + hlist_bl_unlock(hbl);
  11508. +
  11509. + if (!found)
  11510. + DyPrSym(key);
  11511. + return found;
  11512. +}
  11513. +
  11514. +static void dy_free_rcu(struct rcu_head *rcu)
  11515. +{
  11516. + struct au_dykey *key;
  11517. +
  11518. + key = container_of(rcu, struct au_dykey, dk_rcu);
  11519. + DyPrSym(key);
  11520. + kfree(key);
  11521. +}
  11522. +
  11523. +static void dy_free(struct kref *kref)
  11524. +{
  11525. + struct au_dykey *key;
  11526. + struct hlist_bl_head *hbl;
  11527. +
  11528. + key = container_of(kref, struct au_dykey, dk_kref);
  11529. + hbl = dynop + key->dk_op.dy_type;
  11530. + au_hbl_del(&key->dk_hnode, hbl);
  11531. + call_rcu(&key->dk_rcu, dy_free_rcu);
  11532. +}
  11533. +
  11534. +void au_dy_put(struct au_dykey *key)
  11535. +{
  11536. + kref_put(&key->dk_kref, dy_free);
  11537. +}
  11538. +
  11539. +/* ---------------------------------------------------------------------- */
  11540. +
  11541. +#define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
  11542. +
  11543. +#ifdef CONFIG_AUFS_DEBUG
  11544. +#define DyDbgDeclare(cnt) unsigned int cnt = 0
  11545. +#define DyDbgInc(cnt) do { cnt++; } while (0)
  11546. +#else
  11547. +#define DyDbgDeclare(cnt) do {} while (0)
  11548. +#define DyDbgInc(cnt) do {} while (0)
  11549. +#endif
  11550. +
  11551. +#define DySet(func, dst, src, h_op, h_sb) do { \
  11552. + DyDbgInc(cnt); \
  11553. + if (h_op->func) { \
  11554. + if (src.func) \
  11555. + dst.func = src.func; \
  11556. + else \
  11557. + AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
  11558. + } \
  11559. +} while (0)
  11560. +
  11561. +#define DySetForce(func, dst, src) do { \
  11562. + AuDebugOn(!src.func); \
  11563. + DyDbgInc(cnt); \
  11564. + dst.func = src.func; \
  11565. +} while (0)
  11566. +
  11567. +#define DySetAop(func) \
  11568. + DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
  11569. +#define DySetAopForce(func) \
  11570. + DySetForce(func, dyaop->da_op, aufs_aop)
  11571. +
  11572. +static void dy_aop(struct au_dykey *key, const void *h_op,
  11573. + struct super_block *h_sb __maybe_unused)
  11574. +{
  11575. + struct au_dyaop *dyaop = (void *)key;
  11576. + const struct address_space_operations *h_aop = h_op;
  11577. + DyDbgDeclare(cnt);
  11578. +
  11579. + AuDbg("%s\n", au_sbtype(h_sb));
  11580. +
  11581. + DySetAop(writepage);
  11582. + DySetAopForce(readpage); /* force */
  11583. + DySetAop(writepages);
  11584. + DySetAop(set_page_dirty);
  11585. + DySetAop(readpages);
  11586. + DySetAop(readahead);
  11587. + DySetAop(write_begin);
  11588. + DySetAop(write_end);
  11589. + DySetAop(bmap);
  11590. + DySetAop(invalidatepage);
  11591. + DySetAop(releasepage);
  11592. + DySetAop(freepage);
  11593. + /* this one will be changed according to an aufs mount option */
  11594. + DySetAop(direct_IO);
  11595. + DySetAop(migratepage);
  11596. + DySetAop(isolate_page);
  11597. + DySetAop(putback_page);
  11598. + DySetAop(launder_page);
  11599. + DySetAop(is_partially_uptodate);
  11600. + DySetAop(is_dirty_writeback);
  11601. + DySetAop(error_remove_page);
  11602. + DySetAop(swap_activate);
  11603. + DySetAop(swap_deactivate);
  11604. +
  11605. + DyDbgSize(cnt, *h_aop);
  11606. +}
  11607. +
  11608. +/* ---------------------------------------------------------------------- */
  11609. +
  11610. +static void dy_bug(struct kref *kref)
  11611. +{
  11612. + BUG();
  11613. +}
  11614. +
  11615. +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
  11616. +{
  11617. + struct au_dykey *key, *old;
  11618. + struct hlist_bl_head *hbl;
  11619. + struct op {
  11620. + unsigned int sz;
  11621. + void (*set)(struct au_dykey *key, const void *h_op,
  11622. + struct super_block *h_sb __maybe_unused);
  11623. + };
  11624. + static const struct op a[] = {
  11625. + [AuDy_AOP] = {
  11626. + .sz = sizeof(struct au_dyaop),
  11627. + .set = dy_aop
  11628. + }
  11629. + };
  11630. + const struct op *p;
  11631. +
  11632. + hbl = dynop + op->dy_type;
  11633. + key = dy_gfind_get(hbl, op->dy_hop);
  11634. + if (key)
  11635. + goto out_add; /* success */
  11636. +
  11637. + p = a + op->dy_type;
  11638. + key = kzalloc(p->sz, GFP_NOFS);
  11639. + if (unlikely(!key)) {
  11640. + key = ERR_PTR(-ENOMEM);
  11641. + goto out;
  11642. + }
  11643. +
  11644. + key->dk_op.dy_hop = op->dy_hop;
  11645. + kref_init(&key->dk_kref);
  11646. + p->set(key, op->dy_hop, au_br_sb(br));
  11647. + old = dy_gadd(hbl, key);
  11648. + if (old) {
  11649. + au_kfree_rcu(key);
  11650. + key = old;
  11651. + }
  11652. +
  11653. +out_add:
  11654. + old = dy_bradd(br, key);
  11655. + if (old)
  11656. + /* its ref-count should never be zero here */
  11657. + kref_put(&key->dk_kref, dy_bug);
  11658. +out:
  11659. + return key;
  11660. +}
  11661. +
  11662. +/* ---------------------------------------------------------------------- */
  11663. +/*
  11664. + * Aufs prohibits O_DIRECT by default even if the branch supports it.
  11665. + * This behaviour is necessary to return an error from open(O_DIRECT) instead
  11666. + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
  11667. + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
  11668. + * See the aufs manual in detail.
  11669. + */
  11670. +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
  11671. +{
  11672. + if (!do_dx)
  11673. + dyaop->da_op.direct_IO = NULL;
  11674. + else
  11675. + dyaop->da_op.direct_IO = aufs_aop.direct_IO;
  11676. +}
  11677. +
  11678. +static struct au_dyaop *dy_aget(struct au_branch *br,
  11679. + const struct address_space_operations *h_aop,
  11680. + int do_dx)
  11681. +{
  11682. + struct au_dyaop *dyaop;
  11683. + struct au_dynop op;
  11684. +
  11685. + op.dy_type = AuDy_AOP;
  11686. + op.dy_haop = h_aop;
  11687. + dyaop = (void *)dy_get(&op, br);
  11688. + if (IS_ERR(dyaop))
  11689. + goto out;
  11690. + dy_adx(dyaop, do_dx);
  11691. +
  11692. +out:
  11693. + return dyaop;
  11694. +}
  11695. +
  11696. +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
  11697. + struct inode *h_inode)
  11698. +{
  11699. + int err, do_dx;
  11700. + struct super_block *sb;
  11701. + struct au_branch *br;
  11702. + struct au_dyaop *dyaop;
  11703. +
  11704. + AuDebugOn(!S_ISREG(h_inode->i_mode));
  11705. + IiMustWriteLock(inode);
  11706. +
  11707. + sb = inode->i_sb;
  11708. + br = au_sbr(sb, bindex);
  11709. + do_dx = !!au_opt_test(au_mntflags(sb), DIO);
  11710. + dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
  11711. + err = PTR_ERR(dyaop);
  11712. + if (IS_ERR(dyaop))
  11713. + /* unnecessary to call dy_fput() */
  11714. + goto out;
  11715. +
  11716. + err = 0;
  11717. + inode->i_mapping->a_ops = &dyaop->da_op;
  11718. +
  11719. +out:
  11720. + return err;
  11721. +}
  11722. +
  11723. +/*
  11724. + * Is it safe to replace a_ops during the inode/file is in operation?
  11725. + * Yes, I hope so.
  11726. + */
  11727. +int au_dy_irefresh(struct inode *inode)
  11728. +{
  11729. + int err;
  11730. + aufs_bindex_t btop;
  11731. + struct inode *h_inode;
  11732. +
  11733. + err = 0;
  11734. + if (S_ISREG(inode->i_mode)) {
  11735. + btop = au_ibtop(inode);
  11736. + h_inode = au_h_iptr(inode, btop);
  11737. + err = au_dy_iaop(inode, btop, h_inode);
  11738. + }
  11739. + return err;
  11740. +}
  11741. +
  11742. +void au_dy_arefresh(int do_dx)
  11743. +{
  11744. + struct hlist_bl_head *hbl;
  11745. + struct hlist_bl_node *pos;
  11746. + struct au_dykey *key;
  11747. +
  11748. + hbl = dynop + AuDy_AOP;
  11749. + hlist_bl_lock(hbl);
  11750. + hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
  11751. + dy_adx((void *)key, do_dx);
  11752. + hlist_bl_unlock(hbl);
  11753. +}
  11754. +
  11755. +/* ---------------------------------------------------------------------- */
  11756. +
  11757. +void __init au_dy_init(void)
  11758. +{
  11759. + int i;
  11760. +
  11761. + for (i = 0; i < AuDyLast; i++)
  11762. + INIT_HLIST_BL_HEAD(dynop + i);
  11763. +}
  11764. +
  11765. +void au_dy_fin(void)
  11766. +{
  11767. + int i;
  11768. +
  11769. + for (i = 0; i < AuDyLast; i++)
  11770. + WARN_ON(!hlist_bl_empty(dynop + i));
  11771. +}
  11772. diff -Naur null/fs/aufs/dynop.h linux-5.15.131/fs/aufs/dynop.h
  11773. --- /dev/null
  11774. +++ linux-5.15.131/fs/aufs/dynop.h 2023-09-11 14:32:48.147722564 +0300
  11775. @@ -0,0 +1,77 @@
  11776. +/* SPDX-License-Identifier: GPL-2.0 */
  11777. +/*
  11778. + * Copyright (C) 2010-2021 Junjiro R. Okajima
  11779. + *
  11780. + * This program, aufs is free software; you can redistribute it and/or modify
  11781. + * it under the terms of the GNU General Public License as published by
  11782. + * the Free Software Foundation; either version 2 of the License, or
  11783. + * (at your option) any later version.
  11784. + *
  11785. + * This program is distributed in the hope that it will be useful,
  11786. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11787. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11788. + * GNU General Public License for more details.
  11789. + *
  11790. + * You should have received a copy of the GNU General Public License
  11791. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  11792. + */
  11793. +
  11794. +/*
  11795. + * dynamically customizable operations (for regular files only)
  11796. + */
  11797. +
  11798. +#ifndef __AUFS_DYNOP_H__
  11799. +#define __AUFS_DYNOP_H__
  11800. +
  11801. +#ifdef __KERNEL__
  11802. +
  11803. +#include <linux/fs.h>
  11804. +#include <linux/kref.h>
  11805. +
  11806. +enum {AuDy_AOP, AuDyLast};
  11807. +
  11808. +struct au_dynop {
  11809. + int dy_type;
  11810. + union {
  11811. + const void *dy_hop;
  11812. + const struct address_space_operations *dy_haop;
  11813. + };
  11814. +};
  11815. +
  11816. +struct au_dykey {
  11817. + union {
  11818. + struct hlist_bl_node dk_hnode;
  11819. + struct rcu_head dk_rcu;
  11820. + };
  11821. + struct au_dynop dk_op;
  11822. +
  11823. + /*
  11824. + * during I am in the branch local array, kref is gotten. when the
  11825. + * branch is removed, kref is put.
  11826. + */
  11827. + struct kref dk_kref;
  11828. +};
  11829. +
  11830. +/* stop unioning since their sizes are very different from each other */
  11831. +struct au_dyaop {
  11832. + struct au_dykey da_key;
  11833. + struct address_space_operations da_op; /* not const */
  11834. +};
  11835. +/* make sure that 'struct au_dykey *' can be any type */
  11836. +static_assert(!offsetof(struct au_dyaop, da_key));
  11837. +
  11838. +/* ---------------------------------------------------------------------- */
  11839. +
  11840. +/* dynop.c */
  11841. +struct au_branch;
  11842. +void au_dy_put(struct au_dykey *key);
  11843. +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
  11844. + struct inode *h_inode);
  11845. +int au_dy_irefresh(struct inode *inode);
  11846. +void au_dy_arefresh(int do_dio);
  11847. +
  11848. +void __init au_dy_init(void);
  11849. +void au_dy_fin(void);
  11850. +
  11851. +#endif /* __KERNEL__ */
  11852. +#endif /* __AUFS_DYNOP_H__ */
  11853. diff -Naur null/fs/aufs/export.c linux-5.15.131/fs/aufs/export.c
  11854. --- /dev/null
  11855. +++ linux-5.15.131/fs/aufs/export.c 2023-09-11 14:32:48.147722564 +0300
  11856. @@ -0,0 +1,830 @@
  11857. +// SPDX-License-Identifier: GPL-2.0
  11858. +/*
  11859. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  11860. + *
  11861. + * This program, aufs is free software; you can redistribute it and/or modify
  11862. + * it under the terms of the GNU General Public License as published by
  11863. + * the Free Software Foundation; either version 2 of the License, or
  11864. + * (at your option) any later version.
  11865. + *
  11866. + * This program is distributed in the hope that it will be useful,
  11867. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11868. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11869. + * GNU General Public License for more details.
  11870. + *
  11871. + * You should have received a copy of the GNU General Public License
  11872. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  11873. + */
  11874. +
  11875. +/*
  11876. + * export via nfs
  11877. + */
  11878. +
  11879. +#include <linux/exportfs.h>
  11880. +#include <linux/fs_struct.h>
  11881. +#include <linux/nsproxy.h>
  11882. +#include <linux/random.h>
  11883. +#include <linux/writeback.h>
  11884. +#include "aufs.h"
  11885. +
  11886. +union conv {
  11887. +#ifdef CONFIG_AUFS_INO_T_64
  11888. + __u32 a[2];
  11889. +#else
  11890. + __u32 a[1];
  11891. +#endif
  11892. + ino_t ino;
  11893. +};
  11894. +
  11895. +static ino_t decode_ino(__u32 *a)
  11896. +{
  11897. + union conv u;
  11898. +
  11899. + BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
  11900. + u.a[0] = a[0];
  11901. +#ifdef CONFIG_AUFS_INO_T_64
  11902. + u.a[1] = a[1];
  11903. +#endif
  11904. + return u.ino;
  11905. +}
  11906. +
  11907. +static void encode_ino(__u32 *a, ino_t ino)
  11908. +{
  11909. + union conv u;
  11910. +
  11911. + u.ino = ino;
  11912. + a[0] = u.a[0];
  11913. +#ifdef CONFIG_AUFS_INO_T_64
  11914. + a[1] = u.a[1];
  11915. +#endif
  11916. +}
  11917. +
  11918. +/* NFS file handle */
  11919. +enum {
  11920. + Fh_br_id,
  11921. + Fh_sigen,
  11922. +#ifdef CONFIG_AUFS_INO_T_64
  11923. + /* support 64bit inode number */
  11924. + Fh_ino1,
  11925. + Fh_ino2,
  11926. + Fh_dir_ino1,
  11927. + Fh_dir_ino2,
  11928. +#else
  11929. + Fh_ino1,
  11930. + Fh_dir_ino1,
  11931. +#endif
  11932. + Fh_igen,
  11933. + Fh_h_type,
  11934. + Fh_tail,
  11935. +
  11936. + Fh_ino = Fh_ino1,
  11937. + Fh_dir_ino = Fh_dir_ino1
  11938. +};
  11939. +
  11940. +static int au_test_anon(struct dentry *dentry)
  11941. +{
  11942. + /* note: read d_flags without d_lock */
  11943. + return !!(dentry->d_flags & DCACHE_DISCONNECTED);
  11944. +}
  11945. +
  11946. +int au_test_nfsd(void)
  11947. +{
  11948. + int ret;
  11949. + struct task_struct *tsk = current;
  11950. + char comm[sizeof(tsk->comm)];
  11951. +
  11952. + ret = 0;
  11953. + if (tsk->flags & PF_KTHREAD) {
  11954. + get_task_comm(comm, tsk);
  11955. + ret = !strcmp(comm, "nfsd");
  11956. + }
  11957. +
  11958. + return ret;
  11959. +}
  11960. +
  11961. +/* ---------------------------------------------------------------------- */
  11962. +/* inode generation external table */
  11963. +
  11964. +void au_xigen_inc(struct inode *inode)
  11965. +{
  11966. + loff_t pos;
  11967. + ssize_t sz;
  11968. + __u32 igen;
  11969. + struct super_block *sb;
  11970. + struct au_sbinfo *sbinfo;
  11971. +
  11972. + sb = inode->i_sb;
  11973. + AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
  11974. +
  11975. + sbinfo = au_sbi(sb);
  11976. + pos = inode->i_ino;
  11977. + pos *= sizeof(igen);
  11978. + igen = inode->i_generation + 1;
  11979. + sz = xino_fwrite(sbinfo->si_xigen, &igen, sizeof(igen), &pos);
  11980. + if (sz == sizeof(igen))
  11981. + return; /* success */
  11982. +
  11983. + if (unlikely(sz >= 0))
  11984. + AuIOErr("xigen error (%zd)\n", sz);
  11985. +}
  11986. +
  11987. +int au_xigen_new(struct inode *inode)
  11988. +{
  11989. + int err;
  11990. + loff_t pos;
  11991. + ssize_t sz;
  11992. + struct super_block *sb;
  11993. + struct au_sbinfo *sbinfo;
  11994. + struct file *file;
  11995. +
  11996. + err = 0;
  11997. + /* todo: dirty, at mount time */
  11998. + if (inode->i_ino == AUFS_ROOT_INO)
  11999. + goto out;
  12000. + sb = inode->i_sb;
  12001. + SiMustAnyLock(sb);
  12002. + if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
  12003. + goto out;
  12004. +
  12005. + err = -EFBIG;
  12006. + pos = inode->i_ino;
  12007. + if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
  12008. + AuIOErr1("too large i%lld\n", pos);
  12009. + goto out;
  12010. + }
  12011. + pos *= sizeof(inode->i_generation);
  12012. +
  12013. + err = 0;
  12014. + sbinfo = au_sbi(sb);
  12015. + file = sbinfo->si_xigen;
  12016. + BUG_ON(!file);
  12017. +
  12018. + if (vfsub_f_size_read(file)
  12019. + < pos + sizeof(inode->i_generation)) {
  12020. + inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
  12021. + sz = xino_fwrite(file, &inode->i_generation,
  12022. + sizeof(inode->i_generation), &pos);
  12023. + } else
  12024. + sz = xino_fread(file, &inode->i_generation,
  12025. + sizeof(inode->i_generation), &pos);
  12026. + if (sz == sizeof(inode->i_generation))
  12027. + goto out; /* success */
  12028. +
  12029. + err = sz;
  12030. + if (unlikely(sz >= 0)) {
  12031. + err = -EIO;
  12032. + AuIOErr("xigen error (%zd)\n", sz);
  12033. + }
  12034. +
  12035. +out:
  12036. + return err;
  12037. +}
  12038. +
  12039. +int au_xigen_set(struct super_block *sb, struct path *path)
  12040. +{
  12041. + int err;
  12042. + struct au_sbinfo *sbinfo;
  12043. + struct file *file;
  12044. +
  12045. + SiMustWriteLock(sb);
  12046. +
  12047. + sbinfo = au_sbi(sb);
  12048. + file = au_xino_create2(sb, path, sbinfo->si_xigen);
  12049. + err = PTR_ERR(file);
  12050. + if (IS_ERR(file))
  12051. + goto out;
  12052. + err = 0;
  12053. + if (sbinfo->si_xigen)
  12054. + fput(sbinfo->si_xigen);
  12055. + sbinfo->si_xigen = file;
  12056. +
  12057. +out:
  12058. + AuTraceErr(err);
  12059. + return err;
  12060. +}
  12061. +
  12062. +void au_xigen_clr(struct super_block *sb)
  12063. +{
  12064. + struct au_sbinfo *sbinfo;
  12065. +
  12066. + SiMustWriteLock(sb);
  12067. +
  12068. + sbinfo = au_sbi(sb);
  12069. + if (sbinfo->si_xigen) {
  12070. + fput(sbinfo->si_xigen);
  12071. + sbinfo->si_xigen = NULL;
  12072. + }
  12073. +}
  12074. +
  12075. +/* ---------------------------------------------------------------------- */
  12076. +
  12077. +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
  12078. + ino_t dir_ino)
  12079. +{
  12080. + struct dentry *dentry, *d;
  12081. + struct inode *inode;
  12082. + unsigned int sigen;
  12083. +
  12084. + dentry = NULL;
  12085. + inode = ilookup(sb, ino);
  12086. + if (!inode)
  12087. + goto out;
  12088. +
  12089. + dentry = ERR_PTR(-ESTALE);
  12090. + sigen = au_sigen(sb);
  12091. + if (unlikely(au_is_bad_inode(inode)
  12092. + || IS_DEADDIR(inode)
  12093. + || sigen != au_iigen(inode, NULL)))
  12094. + goto out_iput;
  12095. +
  12096. + dentry = NULL;
  12097. + if (!dir_ino || S_ISDIR(inode->i_mode))
  12098. + dentry = d_find_alias(inode);
  12099. + else {
  12100. + spin_lock(&inode->i_lock);
  12101. + hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
  12102. + spin_lock(&d->d_lock);
  12103. + if (!au_test_anon(d)
  12104. + && d_inode(d->d_parent)->i_ino == dir_ino) {
  12105. + dentry = dget_dlock(d);
  12106. + spin_unlock(&d->d_lock);
  12107. + break;
  12108. + }
  12109. + spin_unlock(&d->d_lock);
  12110. + }
  12111. + spin_unlock(&inode->i_lock);
  12112. + }
  12113. + if (unlikely(dentry && au_digen_test(dentry, sigen))) {
  12114. + /* need to refresh */
  12115. + dput(dentry);
  12116. + dentry = NULL;
  12117. + }
  12118. +
  12119. +out_iput:
  12120. + iput(inode);
  12121. +out:
  12122. + AuTraceErrPtr(dentry);
  12123. + return dentry;
  12124. +}
  12125. +
  12126. +/* ---------------------------------------------------------------------- */
  12127. +
  12128. +/* todo: dirty? */
  12129. +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
  12130. +
  12131. +struct au_compare_mnt_args {
  12132. + /* input */
  12133. + struct super_block *sb;
  12134. +
  12135. + /* output */
  12136. + struct vfsmount *mnt;
  12137. +};
  12138. +
  12139. +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
  12140. +{
  12141. + struct au_compare_mnt_args *a = arg;
  12142. +
  12143. + if (mnt->mnt_sb != a->sb)
  12144. + return 0;
  12145. + a->mnt = mntget(mnt);
  12146. + return 1;
  12147. +}
  12148. +
  12149. +static struct vfsmount *au_mnt_get(struct super_block *sb)
  12150. +{
  12151. + int err;
  12152. + struct path root;
  12153. + struct au_compare_mnt_args args = {
  12154. + .sb = sb
  12155. + };
  12156. +
  12157. + get_fs_root(current->fs, &root);
  12158. + rcu_read_lock();
  12159. + err = iterate_mounts(au_compare_mnt, &args, root.mnt);
  12160. + rcu_read_unlock();
  12161. + path_put(&root);
  12162. + AuDebugOn(!err);
  12163. + AuDebugOn(!args.mnt);
  12164. + return args.mnt;
  12165. +}
  12166. +
  12167. +struct au_nfsd_si_lock {
  12168. + unsigned int sigen;
  12169. + aufs_bindex_t bindex, br_id;
  12170. + unsigned char force_lock;
  12171. +};
  12172. +
  12173. +static int si_nfsd_read_lock(struct super_block *sb,
  12174. + struct au_nfsd_si_lock *nsi_lock)
  12175. +{
  12176. + int err;
  12177. + aufs_bindex_t bindex;
  12178. +
  12179. + si_read_lock(sb, AuLock_FLUSH);
  12180. +
  12181. + /* branch id may be wrapped around */
  12182. + err = 0;
  12183. + bindex = au_br_index(sb, nsi_lock->br_id);
  12184. + if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
  12185. + goto out; /* success */
  12186. +
  12187. + err = -ESTALE;
  12188. + bindex = -1;
  12189. + if (!nsi_lock->force_lock)
  12190. + si_read_unlock(sb);
  12191. +
  12192. +out:
  12193. + nsi_lock->bindex = bindex;
  12194. + return err;
  12195. +}
  12196. +
  12197. +struct find_name_by_ino {
  12198. + struct dir_context ctx;
  12199. + int called, found;
  12200. + ino_t ino;
  12201. + char *name;
  12202. + int namelen;
  12203. +};
  12204. +
  12205. +static int
  12206. +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
  12207. + loff_t offset, u64 ino, unsigned int d_type)
  12208. +{
  12209. + struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
  12210. + ctx);
  12211. +
  12212. + a->called++;
  12213. + if (a->ino != ino)
  12214. + return 0;
  12215. +
  12216. + memcpy(a->name, name, namelen);
  12217. + a->namelen = namelen;
  12218. + a->found = 1;
  12219. + return 1;
  12220. +}
  12221. +
  12222. +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
  12223. + struct au_nfsd_si_lock *nsi_lock)
  12224. +{
  12225. + struct dentry *dentry, *parent;
  12226. + struct file *file;
  12227. + struct inode *dir;
  12228. + struct find_name_by_ino arg = {
  12229. + .ctx = {
  12230. + .actor = find_name_by_ino
  12231. + }
  12232. + };
  12233. + int err;
  12234. +
  12235. + parent = path->dentry;
  12236. + if (nsi_lock)
  12237. + si_read_unlock(parent->d_sb);
  12238. + file = vfsub_dentry_open(path, au_dir_roflags);
  12239. + dentry = (void *)file;
  12240. + if (IS_ERR(file))
  12241. + goto out;
  12242. +
  12243. + dentry = ERR_PTR(-ENOMEM);
  12244. + arg.name = (void *)__get_free_page(GFP_NOFS);
  12245. + if (unlikely(!arg.name))
  12246. + goto out_file;
  12247. + arg.ino = ino;
  12248. + arg.found = 0;
  12249. + do {
  12250. + arg.called = 0;
  12251. + /* smp_mb(); */
  12252. + err = vfsub_iterate_dir(file, &arg.ctx);
  12253. + } while (!err && !arg.found && arg.called);
  12254. + dentry = ERR_PTR(err);
  12255. + if (unlikely(err))
  12256. + goto out_name;
  12257. + /* instead of ENOENT */
  12258. + dentry = ERR_PTR(-ESTALE);
  12259. + if (!arg.found)
  12260. + goto out_name;
  12261. +
  12262. + /* do not call vfsub_lkup_one() */
  12263. + dir = d_inode(parent);
  12264. + dentry = vfsub_lookup_one_len_unlocked(arg.name, path, arg.namelen);
  12265. + AuTraceErrPtr(dentry);
  12266. + if (IS_ERR(dentry))
  12267. + goto out_name;
  12268. + AuDebugOn(au_test_anon(dentry));
  12269. + if (unlikely(d_really_is_negative(dentry))) {
  12270. + dput(dentry);
  12271. + dentry = ERR_PTR(-ENOENT);
  12272. + }
  12273. +
  12274. +out_name:
  12275. + free_page((unsigned long)arg.name);
  12276. +out_file:
  12277. + fput(file);
  12278. +out:
  12279. + if (unlikely(nsi_lock
  12280. + && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
  12281. + if (!IS_ERR(dentry)) {
  12282. + dput(dentry);
  12283. + dentry = ERR_PTR(-ESTALE);
  12284. + }
  12285. + AuTraceErrPtr(dentry);
  12286. + return dentry;
  12287. +}
  12288. +
  12289. +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
  12290. + ino_t dir_ino,
  12291. + struct au_nfsd_si_lock *nsi_lock)
  12292. +{
  12293. + struct dentry *dentry;
  12294. + struct path path;
  12295. +
  12296. + if (dir_ino != AUFS_ROOT_INO) {
  12297. + path.dentry = decode_by_ino(sb, dir_ino, 0);
  12298. + dentry = path.dentry;
  12299. + if (!path.dentry || IS_ERR(path.dentry))
  12300. + goto out;
  12301. + AuDebugOn(au_test_anon(path.dentry));
  12302. + } else
  12303. + path.dentry = dget(sb->s_root);
  12304. +
  12305. + path.mnt = au_mnt_get(sb);
  12306. + dentry = au_lkup_by_ino(&path, ino, nsi_lock);
  12307. + path_put(&path);
  12308. +
  12309. +out:
  12310. + AuTraceErrPtr(dentry);
  12311. + return dentry;
  12312. +}
  12313. +
  12314. +/* ---------------------------------------------------------------------- */
  12315. +
  12316. +static int h_acceptable(void *expv, struct dentry *dentry)
  12317. +{
  12318. + return 1;
  12319. +}
  12320. +
  12321. +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
  12322. + char *buf, int len, struct super_block *sb)
  12323. +{
  12324. + char *p;
  12325. + int n;
  12326. + struct path path;
  12327. +
  12328. + p = d_path(h_rootpath, buf, len);
  12329. + if (IS_ERR(p))
  12330. + goto out;
  12331. + n = strlen(p);
  12332. +
  12333. + path.mnt = h_rootpath->mnt;
  12334. + path.dentry = h_parent;
  12335. + p = d_path(&path, buf, len);
  12336. + if (IS_ERR(p))
  12337. + goto out;
  12338. + if (n != 1)
  12339. + p += n;
  12340. +
  12341. + path.mnt = au_mnt_get(sb);
  12342. + path.dentry = sb->s_root;
  12343. + p = d_path(&path, buf, len - strlen(p));
  12344. + mntput(path.mnt);
  12345. + if (IS_ERR(p))
  12346. + goto out;
  12347. + if (n != 1)
  12348. + p[strlen(p)] = '/';
  12349. +
  12350. +out:
  12351. + AuTraceErrPtr(p);
  12352. + return p;
  12353. +}
  12354. +
  12355. +static
  12356. +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
  12357. + int fh_len, struct au_nfsd_si_lock *nsi_lock)
  12358. +{
  12359. + struct dentry *dentry, *h_parent, *root;
  12360. + struct super_block *h_sb;
  12361. + char *pathname, *p;
  12362. + struct vfsmount *h_mnt;
  12363. + struct au_branch *br;
  12364. + int err;
  12365. + struct path path;
  12366. +
  12367. + br = au_sbr(sb, nsi_lock->bindex);
  12368. + h_mnt = au_br_mnt(br);
  12369. + h_sb = h_mnt->mnt_sb;
  12370. + /* todo: call lower fh_to_dentry()? fh_to_parent()? */
  12371. + lockdep_off();
  12372. + h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
  12373. + fh_len - Fh_tail, fh[Fh_h_type],
  12374. + h_acceptable, /*context*/NULL);
  12375. + lockdep_on();
  12376. + dentry = h_parent;
  12377. + if (unlikely(!h_parent || IS_ERR(h_parent))) {
  12378. + AuWarn1("%s decode_fh failed, %ld\n",
  12379. + au_sbtype(h_sb), PTR_ERR(h_parent));
  12380. + goto out;
  12381. + }
  12382. + dentry = NULL;
  12383. + if (unlikely(au_test_anon(h_parent))) {
  12384. + AuWarn1("%s decode_fh returned a disconnected dentry\n",
  12385. + au_sbtype(h_sb));
  12386. + goto out_h_parent;
  12387. + }
  12388. +
  12389. + dentry = ERR_PTR(-ENOMEM);
  12390. + pathname = (void *)__get_free_page(GFP_NOFS);
  12391. + if (unlikely(!pathname))
  12392. + goto out_h_parent;
  12393. +
  12394. + root = sb->s_root;
  12395. + path.mnt = h_mnt;
  12396. + di_read_lock_parent(root, !AuLock_IR);
  12397. + path.dentry = au_h_dptr(root, nsi_lock->bindex);
  12398. + di_read_unlock(root, !AuLock_IR);
  12399. + p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
  12400. + dentry = (void *)p;
  12401. + if (IS_ERR(p))
  12402. + goto out_pathname;
  12403. +
  12404. + si_read_unlock(sb);
  12405. + err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
  12406. + dentry = ERR_PTR(err);
  12407. + if (unlikely(err))
  12408. + goto out_relock;
  12409. +
  12410. + dentry = ERR_PTR(-ENOENT);
  12411. + AuDebugOn(au_test_anon(path.dentry));
  12412. + if (unlikely(d_really_is_negative(path.dentry)))
  12413. + goto out_path;
  12414. +
  12415. + if (ino != d_inode(path.dentry)->i_ino)
  12416. + dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
  12417. + else
  12418. + dentry = dget(path.dentry);
  12419. +
  12420. +out_path:
  12421. + path_put(&path);
  12422. +out_relock:
  12423. + if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
  12424. + if (!IS_ERR(dentry)) {
  12425. + dput(dentry);
  12426. + dentry = ERR_PTR(-ESTALE);
  12427. + }
  12428. +out_pathname:
  12429. + free_page((unsigned long)pathname);
  12430. +out_h_parent:
  12431. + dput(h_parent);
  12432. +out:
  12433. + AuTraceErrPtr(dentry);
  12434. + return dentry;
  12435. +}
  12436. +
  12437. +/* ---------------------------------------------------------------------- */
  12438. +
  12439. +static struct dentry *
  12440. +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
  12441. + int fh_type)
  12442. +{
  12443. + struct dentry *dentry;
  12444. + __u32 *fh = fid->raw;
  12445. + struct au_branch *br;
  12446. + ino_t ino, dir_ino;
  12447. + struct au_nfsd_si_lock nsi_lock = {
  12448. + .force_lock = 0
  12449. + };
  12450. +
  12451. + dentry = ERR_PTR(-ESTALE);
  12452. + /* it should never happen, but the file handle is unreliable */
  12453. + if (unlikely(fh_len < Fh_tail))
  12454. + goto out;
  12455. + nsi_lock.sigen = fh[Fh_sigen];
  12456. + nsi_lock.br_id = fh[Fh_br_id];
  12457. +
  12458. + /* branch id may be wrapped around */
  12459. + br = NULL;
  12460. + if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
  12461. + goto out;
  12462. + nsi_lock.force_lock = 1;
  12463. +
  12464. + /* is this inode still cached? */
  12465. + ino = decode_ino(fh + Fh_ino);
  12466. + /* it should never happen */
  12467. + if (unlikely(ino == AUFS_ROOT_INO))
  12468. + goto out_unlock;
  12469. +
  12470. + dir_ino = decode_ino(fh + Fh_dir_ino);
  12471. + dentry = decode_by_ino(sb, ino, dir_ino);
  12472. + if (IS_ERR(dentry))
  12473. + goto out_unlock;
  12474. + if (dentry)
  12475. + goto accept;
  12476. +
  12477. + /* is the parent dir cached? */
  12478. + br = au_sbr(sb, nsi_lock.bindex);
  12479. + au_lcnt_inc(&br->br_nfiles);
  12480. + dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
  12481. + if (IS_ERR(dentry))
  12482. + goto out_unlock;
  12483. + if (dentry)
  12484. + goto accept;
  12485. +
  12486. + /* lookup path */
  12487. + dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
  12488. + if (IS_ERR(dentry))
  12489. + goto out_unlock;
  12490. + if (unlikely(!dentry))
  12491. + /* todo?: make it ESTALE */
  12492. + goto out_unlock;
  12493. +
  12494. +accept:
  12495. + if (!au_digen_test(dentry, au_sigen(sb))
  12496. + && d_inode(dentry)->i_generation == fh[Fh_igen])
  12497. + goto out_unlock; /* success */
  12498. +
  12499. + dput(dentry);
  12500. + dentry = ERR_PTR(-ESTALE);
  12501. +out_unlock:
  12502. + if (br)
  12503. + au_lcnt_dec(&br->br_nfiles);
  12504. + si_read_unlock(sb);
  12505. +out:
  12506. + AuTraceErrPtr(dentry);
  12507. + return dentry;
  12508. +}
  12509. +
  12510. +#if 0 /* reserved for future use */
  12511. +/* support subtreecheck option */
  12512. +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
  12513. + int fh_len, int fh_type)
  12514. +{
  12515. + struct dentry *parent;
  12516. + __u32 *fh = fid->raw;
  12517. + ino_t dir_ino;
  12518. +
  12519. + dir_ino = decode_ino(fh + Fh_dir_ino);
  12520. + parent = decode_by_ino(sb, dir_ino, 0);
  12521. + if (IS_ERR(parent))
  12522. + goto out;
  12523. + if (!parent)
  12524. + parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
  12525. + dir_ino, fh, fh_len);
  12526. +
  12527. +out:
  12528. + AuTraceErrPtr(parent);
  12529. + return parent;
  12530. +}
  12531. +#endif
  12532. +
  12533. +/* ---------------------------------------------------------------------- */
  12534. +
  12535. +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
  12536. + struct inode *dir)
  12537. +{
  12538. + int err;
  12539. + aufs_bindex_t bindex;
  12540. + struct super_block *sb, *h_sb;
  12541. + struct dentry *dentry, *parent, *h_parent;
  12542. + struct inode *h_dir;
  12543. + struct au_branch *br;
  12544. +
  12545. + err = -ENOSPC;
  12546. + if (unlikely(*max_len <= Fh_tail)) {
  12547. + AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
  12548. + goto out;
  12549. + }
  12550. +
  12551. + err = FILEID_ROOT;
  12552. + if (inode->i_ino == AUFS_ROOT_INO) {
  12553. + AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
  12554. + goto out;
  12555. + }
  12556. +
  12557. + h_parent = NULL;
  12558. + sb = inode->i_sb;
  12559. + err = si_read_lock(sb, AuLock_FLUSH);
  12560. + if (unlikely(err))
  12561. + goto out;
  12562. +
  12563. +#ifdef CONFIG_AUFS_DEBUG
  12564. + if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
  12565. + AuWarn1("NFS-exporting requires xino\n");
  12566. +#endif
  12567. + err = -EIO;
  12568. + parent = NULL;
  12569. + ii_read_lock_child(inode);
  12570. + bindex = au_ibtop(inode);
  12571. + if (!dir) {
  12572. + dentry = d_find_any_alias(inode);
  12573. + if (unlikely(!dentry))
  12574. + goto out_unlock;
  12575. + AuDebugOn(au_test_anon(dentry));
  12576. + parent = dget_parent(dentry);
  12577. + dput(dentry);
  12578. + if (unlikely(!parent))
  12579. + goto out_unlock;
  12580. + if (d_really_is_positive(parent))
  12581. + dir = d_inode(parent);
  12582. + }
  12583. +
  12584. + ii_read_lock_parent(dir);
  12585. + h_dir = au_h_iptr(dir, bindex);
  12586. + ii_read_unlock(dir);
  12587. + if (unlikely(!h_dir))
  12588. + goto out_parent;
  12589. + h_parent = d_find_any_alias(h_dir);
  12590. + if (unlikely(!h_parent))
  12591. + goto out_hparent;
  12592. +
  12593. + err = -EPERM;
  12594. + br = au_sbr(sb, bindex);
  12595. + h_sb = au_br_sb(br);
  12596. + if (unlikely(!h_sb->s_export_op)) {
  12597. + AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
  12598. + goto out_hparent;
  12599. + }
  12600. +
  12601. + fh[Fh_br_id] = br->br_id;
  12602. + fh[Fh_sigen] = au_sigen(sb);
  12603. + encode_ino(fh + Fh_ino, inode->i_ino);
  12604. + encode_ino(fh + Fh_dir_ino, dir->i_ino);
  12605. + fh[Fh_igen] = inode->i_generation;
  12606. +
  12607. + *max_len -= Fh_tail;
  12608. + fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
  12609. + max_len,
  12610. + /*connectable or subtreecheck*/0);
  12611. + err = fh[Fh_h_type];
  12612. + *max_len += Fh_tail;
  12613. + /* todo: macros? */
  12614. + if (err != FILEID_INVALID)
  12615. + err = 99;
  12616. + else
  12617. + AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
  12618. +
  12619. +out_hparent:
  12620. + dput(h_parent);
  12621. +out_parent:
  12622. + dput(parent);
  12623. +out_unlock:
  12624. + ii_read_unlock(inode);
  12625. + si_read_unlock(sb);
  12626. +out:
  12627. + if (unlikely(err < 0))
  12628. + err = FILEID_INVALID;
  12629. + return err;
  12630. +}
  12631. +
  12632. +/* ---------------------------------------------------------------------- */
  12633. +
  12634. +static int aufs_commit_metadata(struct inode *inode)
  12635. +{
  12636. + int err;
  12637. + aufs_bindex_t bindex;
  12638. + struct super_block *sb;
  12639. + struct inode *h_inode;
  12640. + int (*f)(struct inode *inode);
  12641. +
  12642. + sb = inode->i_sb;
  12643. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  12644. + ii_write_lock_child(inode);
  12645. + bindex = au_ibtop(inode);
  12646. + AuDebugOn(bindex < 0);
  12647. + h_inode = au_h_iptr(inode, bindex);
  12648. +
  12649. + f = h_inode->i_sb->s_export_op->commit_metadata;
  12650. + if (f)
  12651. + err = f(h_inode);
  12652. + else
  12653. + err = sync_inode_metadata(h_inode, /*wait*/1);
  12654. +
  12655. + au_cpup_attr_timesizes(inode);
  12656. + ii_write_unlock(inode);
  12657. + si_read_unlock(sb);
  12658. + return err;
  12659. +}
  12660. +
  12661. +/* ---------------------------------------------------------------------- */
  12662. +
  12663. +static struct export_operations aufs_export_op = {
  12664. + .fh_to_dentry = aufs_fh_to_dentry,
  12665. + /* .fh_to_parent = aufs_fh_to_parent, */
  12666. + .encode_fh = aufs_encode_fh,
  12667. + .commit_metadata = aufs_commit_metadata
  12668. +};
  12669. +
  12670. +void au_export_init(struct super_block *sb)
  12671. +{
  12672. + struct au_sbinfo *sbinfo;
  12673. + __u32 u;
  12674. +
  12675. + BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
  12676. + && IS_MODULE(CONFIG_EXPORTFS),
  12677. + AUFS_NAME ": unsupported configuration "
  12678. + "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
  12679. +
  12680. + sb->s_export_op = &aufs_export_op;
  12681. + sbinfo = au_sbi(sb);
  12682. + sbinfo->si_xigen = NULL;
  12683. + get_random_bytes(&u, sizeof(u));
  12684. + BUILD_BUG_ON(sizeof(u) != sizeof(int));
  12685. + atomic_set(&sbinfo->si_xigen_next, u);
  12686. +}
  12687. diff -Naur null/fs/aufs/fhsm.c linux-5.15.131/fs/aufs/fhsm.c
  12688. --- /dev/null
  12689. +++ linux-5.15.131/fs/aufs/fhsm.c 2023-09-11 14:32:48.147722564 +0300
  12690. @@ -0,0 +1,427 @@
  12691. +// SPDX-License-Identifier: GPL-2.0
  12692. +/*
  12693. + * Copyright (C) 2011-2021 Junjiro R. Okajima
  12694. + *
  12695. + * This program, aufs is free software; you can redistribute it and/or modify
  12696. + * it under the terms of the GNU General Public License as published by
  12697. + * the Free Software Foundation; either version 2 of the License, or
  12698. + * (at your option) any later version.
  12699. + *
  12700. + * This program is distributed in the hope that it will be useful,
  12701. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12702. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12703. + * GNU General Public License for more details.
  12704. + *
  12705. + * You should have received a copy of the GNU General Public License
  12706. + * along with this program; if not, write to the Free Software
  12707. + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  12708. + */
  12709. +
  12710. +/*
  12711. + * File-based Hierarchy Storage Management
  12712. + */
  12713. +
  12714. +#include <linux/anon_inodes.h>
  12715. +#include <linux/poll.h>
  12716. +#include <linux/seq_file.h>
  12717. +#include <linux/statfs.h>
  12718. +#include "aufs.h"
  12719. +
  12720. +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
  12721. +{
  12722. + struct au_sbinfo *sbinfo;
  12723. + struct au_fhsm *fhsm;
  12724. +
  12725. + SiMustAnyLock(sb);
  12726. +
  12727. + sbinfo = au_sbi(sb);
  12728. + fhsm = &sbinfo->si_fhsm;
  12729. + AuDebugOn(!fhsm);
  12730. + return fhsm->fhsm_bottom;
  12731. +}
  12732. +
  12733. +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
  12734. +{
  12735. + struct au_sbinfo *sbinfo;
  12736. + struct au_fhsm *fhsm;
  12737. +
  12738. + SiMustWriteLock(sb);
  12739. +
  12740. + sbinfo = au_sbi(sb);
  12741. + fhsm = &sbinfo->si_fhsm;
  12742. + AuDebugOn(!fhsm);
  12743. + fhsm->fhsm_bottom = bindex;
  12744. +}
  12745. +
  12746. +/* ---------------------------------------------------------------------- */
  12747. +
  12748. +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
  12749. +{
  12750. + struct au_br_fhsm *bf;
  12751. +
  12752. + bf = br->br_fhsm;
  12753. + MtxMustLock(&bf->bf_lock);
  12754. +
  12755. + return !bf->bf_readable
  12756. + || time_after(jiffies,
  12757. + bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
  12758. +}
  12759. +
  12760. +/* ---------------------------------------------------------------------- */
  12761. +
  12762. +static void au_fhsm_notify(struct super_block *sb, int val)
  12763. +{
  12764. + struct au_sbinfo *sbinfo;
  12765. + struct au_fhsm *fhsm;
  12766. +
  12767. + SiMustAnyLock(sb);
  12768. +
  12769. + sbinfo = au_sbi(sb);
  12770. + fhsm = &sbinfo->si_fhsm;
  12771. + if (au_fhsm_pid(fhsm)
  12772. + && atomic_read(&fhsm->fhsm_readable) != -1) {
  12773. + atomic_set(&fhsm->fhsm_readable, val);
  12774. + if (val)
  12775. + wake_up(&fhsm->fhsm_wqh);
  12776. + }
  12777. +}
  12778. +
  12779. +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
  12780. + struct aufs_stfs *rstfs, int do_lock, int do_notify)
  12781. +{
  12782. + int err;
  12783. + struct au_branch *br;
  12784. + struct au_br_fhsm *bf;
  12785. +
  12786. + br = au_sbr(sb, bindex);
  12787. + AuDebugOn(au_br_rdonly(br));
  12788. + bf = br->br_fhsm;
  12789. + AuDebugOn(!bf);
  12790. +
  12791. + if (do_lock)
  12792. + mutex_lock(&bf->bf_lock);
  12793. + else
  12794. + MtxMustLock(&bf->bf_lock);
  12795. +
  12796. + /* sb->s_root for NFS is unreliable */
  12797. + err = au_br_stfs(br, &bf->bf_stfs);
  12798. + if (unlikely(err)) {
  12799. + AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
  12800. + goto out;
  12801. + }
  12802. +
  12803. + bf->bf_jiffy = jiffies;
  12804. + bf->bf_readable = 1;
  12805. + if (do_notify)
  12806. + au_fhsm_notify(sb, /*val*/1);
  12807. + if (rstfs)
  12808. + *rstfs = bf->bf_stfs;
  12809. +
  12810. +out:
  12811. + if (do_lock)
  12812. + mutex_unlock(&bf->bf_lock);
  12813. + au_fhsm_notify(sb, /*val*/1);
  12814. +
  12815. + return err;
  12816. +}
  12817. +
  12818. +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
  12819. +{
  12820. + int err;
  12821. + struct au_sbinfo *sbinfo;
  12822. + struct au_fhsm *fhsm;
  12823. + struct au_branch *br;
  12824. + struct au_br_fhsm *bf;
  12825. +
  12826. + AuDbg("b%d, force %d\n", bindex, force);
  12827. + SiMustAnyLock(sb);
  12828. +
  12829. + sbinfo = au_sbi(sb);
  12830. + fhsm = &sbinfo->si_fhsm;
  12831. + if (!au_ftest_si(sbinfo, FHSM)
  12832. + || fhsm->fhsm_bottom == bindex)
  12833. + return;
  12834. +
  12835. + br = au_sbr(sb, bindex);
  12836. + bf = br->br_fhsm;
  12837. + AuDebugOn(!bf);
  12838. + mutex_lock(&bf->bf_lock);
  12839. + if (force
  12840. + || au_fhsm_pid(fhsm)
  12841. + || au_fhsm_test_jiffy(sbinfo, br))
  12842. + err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
  12843. + /*do_notify*/1);
  12844. + mutex_unlock(&bf->bf_lock);
  12845. +}
  12846. +
  12847. +void au_fhsm_wrote_all(struct super_block *sb, int force)
  12848. +{
  12849. + aufs_bindex_t bindex, bbot;
  12850. + struct au_branch *br;
  12851. +
  12852. + /* exclude the bottom */
  12853. + bbot = au_fhsm_bottom(sb);
  12854. + for (bindex = 0; bindex < bbot; bindex++) {
  12855. + br = au_sbr(sb, bindex);
  12856. + if (au_br_fhsm(br->br_perm))
  12857. + au_fhsm_wrote(sb, bindex, force);
  12858. + }
  12859. +}
  12860. +
  12861. +/* ---------------------------------------------------------------------- */
  12862. +
  12863. +static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
  12864. +{
  12865. + __poll_t mask;
  12866. + struct au_sbinfo *sbinfo;
  12867. + struct au_fhsm *fhsm;
  12868. +
  12869. + mask = 0;
  12870. + sbinfo = file->private_data;
  12871. + fhsm = &sbinfo->si_fhsm;
  12872. + poll_wait(file, &fhsm->fhsm_wqh, wait);
  12873. + if (atomic_read(&fhsm->fhsm_readable))
  12874. + mask = EPOLLIN /* | EPOLLRDNORM */;
  12875. +
  12876. + if (!mask)
  12877. + AuDbg("mask 0x%x\n", mask);
  12878. + return mask;
  12879. +}
  12880. +
  12881. +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
  12882. + struct aufs_stfs *stfs, __s16 brid)
  12883. +{
  12884. + int err;
  12885. +
  12886. + err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
  12887. + if (!err)
  12888. + err = __put_user(brid, &stbr->brid);
  12889. + if (unlikely(err))
  12890. + err = -EFAULT;
  12891. +
  12892. + return err;
  12893. +}
  12894. +
  12895. +static ssize_t au_fhsm_do_read(struct super_block *sb,
  12896. + struct aufs_stbr __user *stbr, size_t count)
  12897. +{
  12898. + ssize_t err;
  12899. + int nstbr;
  12900. + aufs_bindex_t bindex, bbot;
  12901. + struct au_branch *br;
  12902. + struct au_br_fhsm *bf;
  12903. +
  12904. + /* except the bottom branch */
  12905. + err = 0;
  12906. + nstbr = 0;
  12907. + bbot = au_fhsm_bottom(sb);
  12908. + for (bindex = 0; !err && bindex < bbot; bindex++) {
  12909. + br = au_sbr(sb, bindex);
  12910. + if (!au_br_fhsm(br->br_perm))
  12911. + continue;
  12912. +
  12913. + bf = br->br_fhsm;
  12914. + mutex_lock(&bf->bf_lock);
  12915. + if (bf->bf_readable) {
  12916. + err = -EFAULT;
  12917. + if (count >= sizeof(*stbr))
  12918. + err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
  12919. + br->br_id);
  12920. + if (!err) {
  12921. + bf->bf_readable = 0;
  12922. + count -= sizeof(*stbr);
  12923. + nstbr++;
  12924. + }
  12925. + }
  12926. + mutex_unlock(&bf->bf_lock);
  12927. + }
  12928. + if (!err)
  12929. + err = sizeof(*stbr) * nstbr;
  12930. +
  12931. + return err;
  12932. +}
  12933. +
  12934. +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
  12935. + loff_t *pos)
  12936. +{
  12937. + ssize_t err;
  12938. + int readable;
  12939. + aufs_bindex_t nfhsm, bindex, bbot;
  12940. + struct au_sbinfo *sbinfo;
  12941. + struct au_fhsm *fhsm;
  12942. + struct au_branch *br;
  12943. + struct super_block *sb;
  12944. +
  12945. + err = 0;
  12946. + sbinfo = file->private_data;
  12947. + fhsm = &sbinfo->si_fhsm;
  12948. +need_data:
  12949. + spin_lock_irq(&fhsm->fhsm_wqh.lock);
  12950. + if (!atomic_read(&fhsm->fhsm_readable)) {
  12951. + if (vfsub_file_flags(file) & O_NONBLOCK)
  12952. + err = -EAGAIN;
  12953. + else
  12954. + err = wait_event_interruptible_locked_irq
  12955. + (fhsm->fhsm_wqh,
  12956. + atomic_read(&fhsm->fhsm_readable));
  12957. + }
  12958. + spin_unlock_irq(&fhsm->fhsm_wqh.lock);
  12959. + if (unlikely(err))
  12960. + goto out;
  12961. +
  12962. + /* sb may already be dead */
  12963. + au_rw_read_lock(&sbinfo->si_rwsem);
  12964. + readable = atomic_read(&fhsm->fhsm_readable);
  12965. + if (readable > 0) {
  12966. + sb = sbinfo->si_sb;
  12967. + AuDebugOn(!sb);
  12968. + /* exclude the bottom branch */
  12969. + nfhsm = 0;
  12970. + bbot = au_fhsm_bottom(sb);
  12971. + for (bindex = 0; bindex < bbot; bindex++) {
  12972. + br = au_sbr(sb, bindex);
  12973. + if (au_br_fhsm(br->br_perm))
  12974. + nfhsm++;
  12975. + }
  12976. + err = -EMSGSIZE;
  12977. + if (nfhsm * sizeof(struct aufs_stbr) <= count) {
  12978. + atomic_set(&fhsm->fhsm_readable, 0);
  12979. + err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
  12980. + count);
  12981. + }
  12982. + }
  12983. + au_rw_read_unlock(&sbinfo->si_rwsem);
  12984. + if (!readable)
  12985. + goto need_data;
  12986. +
  12987. +out:
  12988. + return err;
  12989. +}
  12990. +
  12991. +static int au_fhsm_release(struct inode *inode, struct file *file)
  12992. +{
  12993. + struct au_sbinfo *sbinfo;
  12994. + struct au_fhsm *fhsm;
  12995. +
  12996. + /* sb may already be dead */
  12997. + sbinfo = file->private_data;
  12998. + fhsm = &sbinfo->si_fhsm;
  12999. + spin_lock(&fhsm->fhsm_spin);
  13000. + fhsm->fhsm_pid = 0;
  13001. + spin_unlock(&fhsm->fhsm_spin);
  13002. + kobject_put(&sbinfo->si_kobj);
  13003. +
  13004. + return 0;
  13005. +}
  13006. +
  13007. +static const struct file_operations au_fhsm_fops = {
  13008. + .owner = THIS_MODULE,
  13009. + .llseek = noop_llseek,
  13010. + .read = au_fhsm_read,
  13011. + .poll = au_fhsm_poll,
  13012. + .release = au_fhsm_release
  13013. +};
  13014. +
  13015. +int au_fhsm_fd(struct super_block *sb, int oflags)
  13016. +{
  13017. + int err, fd;
  13018. + struct au_sbinfo *sbinfo;
  13019. + struct au_fhsm *fhsm;
  13020. +
  13021. + err = -EPERM;
  13022. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  13023. + goto out;
  13024. +
  13025. + err = -EINVAL;
  13026. + if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
  13027. + goto out;
  13028. +
  13029. + err = 0;
  13030. + sbinfo = au_sbi(sb);
  13031. + fhsm = &sbinfo->si_fhsm;
  13032. + spin_lock(&fhsm->fhsm_spin);
  13033. + if (!fhsm->fhsm_pid)
  13034. + fhsm->fhsm_pid = current->pid;
  13035. + else
  13036. + err = -EBUSY;
  13037. + spin_unlock(&fhsm->fhsm_spin);
  13038. + if (unlikely(err))
  13039. + goto out;
  13040. +
  13041. + oflags |= O_RDONLY;
  13042. + /* oflags |= FMODE_NONOTIFY; */
  13043. + fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
  13044. + err = fd;
  13045. + if (unlikely(fd < 0))
  13046. + goto out_pid;
  13047. +
  13048. + /* succeed regardless 'fhsm' status */
  13049. + kobject_get(&sbinfo->si_kobj);
  13050. + si_noflush_read_lock(sb);
  13051. + if (au_ftest_si(sbinfo, FHSM))
  13052. + au_fhsm_wrote_all(sb, /*force*/0);
  13053. + si_read_unlock(sb);
  13054. + goto out; /* success */
  13055. +
  13056. +out_pid:
  13057. + spin_lock(&fhsm->fhsm_spin);
  13058. + fhsm->fhsm_pid = 0;
  13059. + spin_unlock(&fhsm->fhsm_spin);
  13060. +out:
  13061. + AuTraceErr(err);
  13062. + return err;
  13063. +}
  13064. +
  13065. +/* ---------------------------------------------------------------------- */
  13066. +
  13067. +int au_fhsm_br_alloc(struct au_branch *br)
  13068. +{
  13069. + int err;
  13070. +
  13071. + err = 0;
  13072. + br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
  13073. + if (br->br_fhsm)
  13074. + au_br_fhsm_init(br->br_fhsm);
  13075. + else
  13076. + err = -ENOMEM;
  13077. +
  13078. + return err;
  13079. +}
  13080. +
  13081. +/* ---------------------------------------------------------------------- */
  13082. +
  13083. +void au_fhsm_fin(struct super_block *sb)
  13084. +{
  13085. + au_fhsm_notify(sb, /*val*/-1);
  13086. +}
  13087. +
  13088. +void au_fhsm_init(struct au_sbinfo *sbinfo)
  13089. +{
  13090. + struct au_fhsm *fhsm;
  13091. +
  13092. + fhsm = &sbinfo->si_fhsm;
  13093. + spin_lock_init(&fhsm->fhsm_spin);
  13094. + init_waitqueue_head(&fhsm->fhsm_wqh);
  13095. + atomic_set(&fhsm->fhsm_readable, 0);
  13096. + fhsm->fhsm_expire
  13097. + = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
  13098. + fhsm->fhsm_bottom = -1;
  13099. +}
  13100. +
  13101. +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
  13102. +{
  13103. + sbinfo->si_fhsm.fhsm_expire
  13104. + = msecs_to_jiffies(sec * MSEC_PER_SEC);
  13105. +}
  13106. +
  13107. +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
  13108. +{
  13109. + unsigned int u;
  13110. +
  13111. + if (!au_ftest_si(sbinfo, FHSM))
  13112. + return;
  13113. +
  13114. + u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
  13115. + if (u != AUFS_FHSM_CACHE_DEF_SEC)
  13116. + seq_printf(seq, ",fhsm_sec=%u", u);
  13117. +}
  13118. diff -Naur null/fs/aufs/file.c linux-5.15.131/fs/aufs/file.c
  13119. --- /dev/null
  13120. +++ linux-5.15.131/fs/aufs/file.c 2023-09-11 14:32:48.148722552 +0300
  13121. @@ -0,0 +1,863 @@
  13122. +// SPDX-License-Identifier: GPL-2.0
  13123. +/*
  13124. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  13125. + *
  13126. + * This program, aufs is free software; you can redistribute it and/or modify
  13127. + * it under the terms of the GNU General Public License as published by
  13128. + * the Free Software Foundation; either version 2 of the License, or
  13129. + * (at your option) any later version.
  13130. + *
  13131. + * This program is distributed in the hope that it will be useful,
  13132. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13133. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13134. + * GNU General Public License for more details.
  13135. + *
  13136. + * You should have received a copy of the GNU General Public License
  13137. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  13138. + */
  13139. +
  13140. +/*
  13141. + * handling file/dir, and address_space operation
  13142. + */
  13143. +
  13144. +#ifdef CONFIG_AUFS_DEBUG
  13145. +#include <linux/migrate.h>
  13146. +#endif
  13147. +#include <linux/pagemap.h>
  13148. +#include "aufs.h"
  13149. +
  13150. +/* drop flags for writing */
  13151. +unsigned int au_file_roflags(unsigned int flags)
  13152. +{
  13153. + flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
  13154. + flags |= O_RDONLY | O_NOATIME;
  13155. + return flags;
  13156. +}
  13157. +
  13158. +/* common functions to regular file and dir */
  13159. +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
  13160. + struct file *file, int force_wr)
  13161. +{
  13162. + struct file *h_file;
  13163. + struct dentry *h_dentry;
  13164. + struct inode *h_inode;
  13165. + struct super_block *sb;
  13166. + struct au_branch *br;
  13167. + struct path h_path;
  13168. + int err;
  13169. +
  13170. + /* a race condition can happen between open and unlink/rmdir */
  13171. + h_file = ERR_PTR(-ENOENT);
  13172. + h_dentry = au_h_dptr(dentry, bindex);
  13173. + if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
  13174. + goto out;
  13175. + h_inode = d_inode(h_dentry);
  13176. + spin_lock(&h_dentry->d_lock);
  13177. + err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
  13178. + /* || !d_inode(dentry)->i_nlink */
  13179. + ;
  13180. + spin_unlock(&h_dentry->d_lock);
  13181. + if (unlikely(err))
  13182. + goto out;
  13183. +
  13184. + sb = dentry->d_sb;
  13185. + br = au_sbr(sb, bindex);
  13186. + err = au_br_test_oflag(flags, br);
  13187. + h_file = ERR_PTR(err);
  13188. + if (unlikely(err))
  13189. + goto out;
  13190. +
  13191. + /* drop flags for writing */
  13192. + if (au_test_ro(sb, bindex, d_inode(dentry))) {
  13193. + if (force_wr && !(flags & O_WRONLY))
  13194. + force_wr = 0;
  13195. + flags = au_file_roflags(flags);
  13196. + if (force_wr) {
  13197. + h_file = ERR_PTR(-EROFS);
  13198. + flags = au_file_roflags(flags);
  13199. + if (unlikely(vfsub_native_ro(h_inode)
  13200. + || IS_APPEND(h_inode)))
  13201. + goto out;
  13202. + flags &= ~O_ACCMODE;
  13203. + flags |= O_WRONLY;
  13204. + }
  13205. + }
  13206. + flags &= ~O_CREAT;
  13207. + au_lcnt_inc(&br->br_nfiles);
  13208. + h_path.dentry = h_dentry;
  13209. + h_path.mnt = au_br_mnt(br);
  13210. + h_file = vfsub_dentry_open(&h_path, flags);
  13211. + if (IS_ERR(h_file))
  13212. + goto out_br;
  13213. +
  13214. + if (flags & __FMODE_EXEC) {
  13215. + err = deny_write_access(h_file);
  13216. + if (unlikely(err)) {
  13217. + fput(h_file);
  13218. + h_file = ERR_PTR(err);
  13219. + goto out_br;
  13220. + }
  13221. + }
  13222. + fsnotify_open(h_file);
  13223. + goto out; /* success */
  13224. +
  13225. +out_br:
  13226. + au_lcnt_dec(&br->br_nfiles);
  13227. +out:
  13228. + return h_file;
  13229. +}
  13230. +
  13231. +static int au_cmoo(struct dentry *dentry)
  13232. +{
  13233. + int err, cmoo, matched;
  13234. + unsigned int udba;
  13235. + struct path h_path;
  13236. + struct au_pin pin;
  13237. + struct au_cp_generic cpg = {
  13238. + .dentry = dentry,
  13239. + .bdst = -1,
  13240. + .bsrc = -1,
  13241. + .len = -1,
  13242. + .pin = &pin,
  13243. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  13244. + };
  13245. + struct inode *delegated;
  13246. + struct super_block *sb;
  13247. + struct au_sbinfo *sbinfo;
  13248. + struct au_fhsm *fhsm;
  13249. + pid_t pid;
  13250. + struct au_branch *br;
  13251. + struct dentry *parent;
  13252. + struct au_hinode *hdir;
  13253. +
  13254. + DiMustWriteLock(dentry);
  13255. + IiMustWriteLock(d_inode(dentry));
  13256. +
  13257. + err = 0;
  13258. + if (IS_ROOT(dentry))
  13259. + goto out;
  13260. + cpg.bsrc = au_dbtop(dentry);
  13261. + if (!cpg.bsrc)
  13262. + goto out;
  13263. +
  13264. + sb = dentry->d_sb;
  13265. + sbinfo = au_sbi(sb);
  13266. + fhsm = &sbinfo->si_fhsm;
  13267. + pid = au_fhsm_pid(fhsm);
  13268. + rcu_read_lock();
  13269. + matched = (pid
  13270. + && (current->pid == pid
  13271. + || rcu_dereference(current->real_parent)->pid == pid));
  13272. + rcu_read_unlock();
  13273. + if (matched)
  13274. + goto out;
  13275. +
  13276. + br = au_sbr(sb, cpg.bsrc);
  13277. + cmoo = au_br_cmoo(br->br_perm);
  13278. + if (!cmoo)
  13279. + goto out;
  13280. + if (!d_is_reg(dentry))
  13281. + cmoo &= AuBrAttr_COO_ALL;
  13282. + if (!cmoo)
  13283. + goto out;
  13284. +
  13285. + parent = dget_parent(dentry);
  13286. + di_write_lock_parent(parent);
  13287. + err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
  13288. + cpg.bdst = err;
  13289. + if (unlikely(err < 0)) {
  13290. + err = 0; /* there is no upper writable branch */
  13291. + goto out_dgrade;
  13292. + }
  13293. + AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
  13294. +
  13295. + /* do not respect the coo attrib for the target branch */
  13296. + err = au_cpup_dirs(dentry, cpg.bdst);
  13297. + if (unlikely(err))
  13298. + goto out_dgrade;
  13299. +
  13300. + di_downgrade_lock(parent, AuLock_IR);
  13301. + udba = au_opt_udba(sb);
  13302. + err = au_pin(&pin, dentry, cpg.bdst, udba,
  13303. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  13304. + if (unlikely(err))
  13305. + goto out_parent;
  13306. +
  13307. + err = au_sio_cpup_simple(&cpg);
  13308. + au_unpin(&pin);
  13309. + if (unlikely(err))
  13310. + goto out_parent;
  13311. + if (!(cmoo & AuBrWAttr_MOO))
  13312. + goto out_parent; /* success */
  13313. +
  13314. + err = au_pin(&pin, dentry, cpg.bsrc, udba,
  13315. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  13316. + if (unlikely(err))
  13317. + goto out_parent;
  13318. +
  13319. + h_path.mnt = au_br_mnt(br);
  13320. + h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
  13321. + hdir = au_hi(d_inode(parent), cpg.bsrc);
  13322. + delegated = NULL;
  13323. + err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
  13324. + au_unpin(&pin);
  13325. + /* todo: keep h_dentry or not? */
  13326. + if (unlikely(err == -EWOULDBLOCK)) {
  13327. + pr_warn("cannot retry for NFSv4 delegation"
  13328. + " for an internal unlink\n");
  13329. + iput(delegated);
  13330. + }
  13331. + if (unlikely(err)) {
  13332. + pr_err("unlink %pd after coo failed (%d), ignored\n",
  13333. + dentry, err);
  13334. + err = 0;
  13335. + }
  13336. + goto out_parent; /* success */
  13337. +
  13338. +out_dgrade:
  13339. + di_downgrade_lock(parent, AuLock_IR);
  13340. +out_parent:
  13341. + di_read_unlock(parent, AuLock_IR);
  13342. + dput(parent);
  13343. +out:
  13344. + AuTraceErr(err);
  13345. + return err;
  13346. +}
  13347. +
  13348. +int au_do_open(struct file *file, struct au_do_open_args *args)
  13349. +{
  13350. + int err, aopen = args->aopen;
  13351. + struct dentry *dentry;
  13352. + struct au_finfo *finfo;
  13353. +
  13354. + if (!aopen)
  13355. + err = au_finfo_init(file, args->fidir);
  13356. + else {
  13357. + lockdep_off();
  13358. + err = au_finfo_init(file, args->fidir);
  13359. + lockdep_on();
  13360. + }
  13361. + if (unlikely(err))
  13362. + goto out;
  13363. +
  13364. + dentry = file->f_path.dentry;
  13365. + AuDebugOn(IS_ERR_OR_NULL(dentry));
  13366. + di_write_lock_child(dentry);
  13367. + err = au_cmoo(dentry);
  13368. + di_downgrade_lock(dentry, AuLock_IR);
  13369. + if (!err) {
  13370. + if (!aopen)
  13371. + err = args->open(file, vfsub_file_flags(file), NULL);
  13372. + else {
  13373. + lockdep_off();
  13374. + err = args->open(file, vfsub_file_flags(file),
  13375. + args->h_file);
  13376. + lockdep_on();
  13377. + }
  13378. + }
  13379. + di_read_unlock(dentry, AuLock_IR);
  13380. +
  13381. + finfo = au_fi(file);
  13382. + if (!err) {
  13383. + finfo->fi_file = file;
  13384. + au_hbl_add(&finfo->fi_hlist,
  13385. + &au_sbi(file->f_path.dentry->d_sb)->si_files);
  13386. + }
  13387. + if (!aopen)
  13388. + fi_write_unlock(file);
  13389. + else {
  13390. + lockdep_off();
  13391. + fi_write_unlock(file);
  13392. + lockdep_on();
  13393. + }
  13394. + if (unlikely(err)) {
  13395. + finfo->fi_hdir = NULL;
  13396. + au_finfo_fin(file);
  13397. + }
  13398. +
  13399. +out:
  13400. + AuTraceErr(err);
  13401. + return err;
  13402. +}
  13403. +
  13404. +int au_reopen_nondir(struct file *file)
  13405. +{
  13406. + int err;
  13407. + aufs_bindex_t btop;
  13408. + struct dentry *dentry;
  13409. + struct au_branch *br;
  13410. + struct file *h_file, *h_file_tmp;
  13411. +
  13412. + dentry = file->f_path.dentry;
  13413. + btop = au_dbtop(dentry);
  13414. + br = au_sbr(dentry->d_sb, btop);
  13415. + h_file_tmp = NULL;
  13416. + if (au_fbtop(file) == btop) {
  13417. + h_file = au_hf_top(file);
  13418. + if (file->f_mode == h_file->f_mode)
  13419. + return 0; /* success */
  13420. + h_file_tmp = h_file;
  13421. + get_file(h_file_tmp);
  13422. + au_lcnt_inc(&br->br_nfiles);
  13423. + au_set_h_fptr(file, btop, NULL);
  13424. + }
  13425. + AuDebugOn(au_fi(file)->fi_hdir);
  13426. + /*
  13427. + * it can happen
  13428. + * file exists on both of rw and ro
  13429. + * open --> dbtop and fbtop are both 0
  13430. + * prepend a branch as rw, "rw" become ro
  13431. + * remove rw/file
  13432. + * delete the top branch, "rw" becomes rw again
  13433. + * --> dbtop is 1, fbtop is still 0
  13434. + * write --> fbtop is 0 but dbtop is 1
  13435. + */
  13436. + /* AuDebugOn(au_fbtop(file) < btop); */
  13437. +
  13438. + h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
  13439. + file, /*force_wr*/0);
  13440. + err = PTR_ERR(h_file);
  13441. + if (IS_ERR(h_file)) {
  13442. + if (h_file_tmp) {
  13443. + /* revert */
  13444. + au_set_h_fptr(file, btop, h_file_tmp);
  13445. + h_file_tmp = NULL;
  13446. + }
  13447. + goto out; /* todo: close all? */
  13448. + }
  13449. +
  13450. + err = 0;
  13451. + au_set_fbtop(file, btop);
  13452. + au_set_h_fptr(file, btop, h_file);
  13453. + au_update_figen(file);
  13454. + /* todo: necessary? */
  13455. + /* file->f_ra = h_file->f_ra; */
  13456. +
  13457. +out:
  13458. + if (h_file_tmp) {
  13459. + fput(h_file_tmp);
  13460. + au_lcnt_dec(&br->br_nfiles);
  13461. + }
  13462. + return err;
  13463. +}
  13464. +
  13465. +/* ---------------------------------------------------------------------- */
  13466. +
  13467. +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
  13468. + struct dentry *hi_wh)
  13469. +{
  13470. + int err;
  13471. + aufs_bindex_t btop;
  13472. + struct au_dinfo *dinfo;
  13473. + struct dentry *h_dentry;
  13474. + struct au_hdentry *hdp;
  13475. +
  13476. + dinfo = au_di(file->f_path.dentry);
  13477. + AuRwMustWriteLock(&dinfo->di_rwsem);
  13478. +
  13479. + btop = dinfo->di_btop;
  13480. + dinfo->di_btop = btgt;
  13481. + hdp = au_hdentry(dinfo, btgt);
  13482. + h_dentry = hdp->hd_dentry;
  13483. + hdp->hd_dentry = hi_wh;
  13484. + err = au_reopen_nondir(file);
  13485. + hdp->hd_dentry = h_dentry;
  13486. + dinfo->di_btop = btop;
  13487. +
  13488. + return err;
  13489. +}
  13490. +
  13491. +static int au_ready_to_write_wh(struct file *file, loff_t len,
  13492. + aufs_bindex_t bcpup, struct au_pin *pin)
  13493. +{
  13494. + int err;
  13495. + struct inode *inode, *h_inode;
  13496. + struct dentry *h_dentry, *hi_wh;
  13497. + struct au_cp_generic cpg = {
  13498. + .dentry = file->f_path.dentry,
  13499. + .bdst = bcpup,
  13500. + .bsrc = -1,
  13501. + .len = len,
  13502. + .pin = pin
  13503. + };
  13504. +
  13505. + au_update_dbtop(cpg.dentry);
  13506. + inode = d_inode(cpg.dentry);
  13507. + h_inode = NULL;
  13508. + if (au_dbtop(cpg.dentry) <= bcpup
  13509. + && au_dbbot(cpg.dentry) >= bcpup) {
  13510. + h_dentry = au_h_dptr(cpg.dentry, bcpup);
  13511. + if (h_dentry && d_is_positive(h_dentry))
  13512. + h_inode = d_inode(h_dentry);
  13513. + }
  13514. + hi_wh = au_hi_wh(inode, bcpup);
  13515. + if (!hi_wh && !h_inode)
  13516. + err = au_sio_cpup_wh(&cpg, file);
  13517. + else
  13518. + /* already copied-up after unlink */
  13519. + err = au_reopen_wh(file, bcpup, hi_wh);
  13520. +
  13521. + if (!err
  13522. + && (inode->i_nlink > 1
  13523. + || (inode->i_state & I_LINKABLE))
  13524. + && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
  13525. + au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
  13526. +
  13527. + return err;
  13528. +}
  13529. +
  13530. +/*
  13531. + * prepare the @file for writing.
  13532. + */
  13533. +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
  13534. +{
  13535. + int err;
  13536. + aufs_bindex_t dbtop;
  13537. + struct dentry *parent;
  13538. + struct inode *inode;
  13539. + struct super_block *sb;
  13540. + struct file *h_file;
  13541. + struct au_cp_generic cpg = {
  13542. + .dentry = file->f_path.dentry,
  13543. + .bdst = -1,
  13544. + .bsrc = -1,
  13545. + .len = len,
  13546. + .pin = pin,
  13547. + .flags = AuCpup_DTIME
  13548. + };
  13549. +
  13550. + sb = cpg.dentry->d_sb;
  13551. + inode = d_inode(cpg.dentry);
  13552. + cpg.bsrc = au_fbtop(file);
  13553. + err = au_test_ro(sb, cpg.bsrc, inode);
  13554. + if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
  13555. + err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
  13556. + /*flags*/0);
  13557. + goto out;
  13558. + }
  13559. +
  13560. + /* need to cpup or reopen */
  13561. + parent = dget_parent(cpg.dentry);
  13562. + di_write_lock_parent(parent);
  13563. + err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
  13564. + cpg.bdst = err;
  13565. + if (unlikely(err < 0))
  13566. + goto out_dgrade;
  13567. + err = 0;
  13568. +
  13569. + if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
  13570. + err = au_cpup_dirs(cpg.dentry, cpg.bdst);
  13571. + if (unlikely(err))
  13572. + goto out_dgrade;
  13573. + }
  13574. +
  13575. + err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
  13576. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  13577. + if (unlikely(err))
  13578. + goto out_dgrade;
  13579. +
  13580. + dbtop = au_dbtop(cpg.dentry);
  13581. + if (dbtop <= cpg.bdst)
  13582. + cpg.bsrc = cpg.bdst;
  13583. +
  13584. + if (dbtop <= cpg.bdst /* just reopen */
  13585. + || !d_unhashed(cpg.dentry) /* copyup and reopen */
  13586. + ) {
  13587. + h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
  13588. + if (IS_ERR(h_file))
  13589. + err = PTR_ERR(h_file);
  13590. + else {
  13591. + di_downgrade_lock(parent, AuLock_IR);
  13592. + if (dbtop > cpg.bdst)
  13593. + err = au_sio_cpup_simple(&cpg);
  13594. + if (!err)
  13595. + err = au_reopen_nondir(file);
  13596. + au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
  13597. + }
  13598. + } else { /* copyup as wh and reopen */
  13599. + /*
  13600. + * since writable hfsplus branch is not supported,
  13601. + * h_open_pre/post() are unnecessary.
  13602. + */
  13603. + err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
  13604. + di_downgrade_lock(parent, AuLock_IR);
  13605. + }
  13606. +
  13607. + if (!err) {
  13608. + au_pin_set_parent_lflag(pin, /*lflag*/0);
  13609. + goto out_dput; /* success */
  13610. + }
  13611. + au_unpin(pin);
  13612. + goto out_unlock;
  13613. +
  13614. +out_dgrade:
  13615. + di_downgrade_lock(parent, AuLock_IR);
  13616. +out_unlock:
  13617. + di_read_unlock(parent, AuLock_IR);
  13618. +out_dput:
  13619. + dput(parent);
  13620. +out:
  13621. + return err;
  13622. +}
  13623. +
  13624. +/* ---------------------------------------------------------------------- */
  13625. +
  13626. +int au_do_flush(struct file *file, fl_owner_t id,
  13627. + int (*flush)(struct file *file, fl_owner_t id))
  13628. +{
  13629. + int err;
  13630. + struct super_block *sb;
  13631. + struct inode *inode;
  13632. +
  13633. + inode = file_inode(file);
  13634. + sb = inode->i_sb;
  13635. + si_noflush_read_lock(sb);
  13636. + fi_read_lock(file);
  13637. + ii_read_lock_child(inode);
  13638. +
  13639. + err = flush(file, id);
  13640. + au_cpup_attr_timesizes(inode);
  13641. +
  13642. + ii_read_unlock(inode);
  13643. + fi_read_unlock(file);
  13644. + si_read_unlock(sb);
  13645. + return err;
  13646. +}
  13647. +
  13648. +/* ---------------------------------------------------------------------- */
  13649. +
  13650. +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
  13651. +{
  13652. + int err;
  13653. + struct au_pin pin;
  13654. + struct au_finfo *finfo;
  13655. + struct dentry *parent, *hi_wh;
  13656. + struct inode *inode;
  13657. + struct super_block *sb;
  13658. + struct au_cp_generic cpg = {
  13659. + .dentry = file->f_path.dentry,
  13660. + .bdst = -1,
  13661. + .bsrc = -1,
  13662. + .len = -1,
  13663. + .pin = &pin,
  13664. + .flags = AuCpup_DTIME
  13665. + };
  13666. +
  13667. + FiMustWriteLock(file);
  13668. +
  13669. + err = 0;
  13670. + finfo = au_fi(file);
  13671. + sb = cpg.dentry->d_sb;
  13672. + inode = d_inode(cpg.dentry);
  13673. + cpg.bdst = au_ibtop(inode);
  13674. + if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
  13675. + goto out;
  13676. +
  13677. + parent = dget_parent(cpg.dentry);
  13678. + if (au_test_ro(sb, cpg.bdst, inode)) {
  13679. + di_read_lock_parent(parent, !AuLock_IR);
  13680. + err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
  13681. + cpg.bdst = err;
  13682. + di_read_unlock(parent, !AuLock_IR);
  13683. + if (unlikely(err < 0))
  13684. + goto out_parent;
  13685. + err = 0;
  13686. + }
  13687. +
  13688. + di_read_lock_parent(parent, AuLock_IR);
  13689. + hi_wh = au_hi_wh(inode, cpg.bdst);
  13690. + if (!S_ISDIR(inode->i_mode)
  13691. + && au_opt_test(au_mntflags(sb), PLINK)
  13692. + && au_plink_test(inode)
  13693. + && !d_unhashed(cpg.dentry)
  13694. + && cpg.bdst < au_dbtop(cpg.dentry)) {
  13695. + err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
  13696. + if (unlikely(err))
  13697. + goto out_unlock;
  13698. +
  13699. + /* always superio. */
  13700. + err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
  13701. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  13702. + if (!err) {
  13703. + err = au_sio_cpup_simple(&cpg);
  13704. + au_unpin(&pin);
  13705. + }
  13706. + } else if (hi_wh) {
  13707. + /* already copied-up after unlink */
  13708. + err = au_reopen_wh(file, cpg.bdst, hi_wh);
  13709. + *need_reopen = 0;
  13710. + }
  13711. +
  13712. +out_unlock:
  13713. + di_read_unlock(parent, AuLock_IR);
  13714. +out_parent:
  13715. + dput(parent);
  13716. +out:
  13717. + return err;
  13718. +}
  13719. +
  13720. +static void au_do_refresh_dir(struct file *file)
  13721. +{
  13722. + aufs_bindex_t bindex, bbot, new_bindex, brid;
  13723. + struct au_hfile *p, tmp, *q;
  13724. + struct au_finfo *finfo;
  13725. + struct super_block *sb;
  13726. + struct au_fidir *fidir;
  13727. +
  13728. + FiMustWriteLock(file);
  13729. +
  13730. + sb = file->f_path.dentry->d_sb;
  13731. + finfo = au_fi(file);
  13732. + fidir = finfo->fi_hdir;
  13733. + AuDebugOn(!fidir);
  13734. + p = fidir->fd_hfile + finfo->fi_btop;
  13735. + brid = p->hf_br->br_id;
  13736. + bbot = fidir->fd_bbot;
  13737. + for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
  13738. + if (!p->hf_file)
  13739. + continue;
  13740. +
  13741. + new_bindex = au_br_index(sb, p->hf_br->br_id);
  13742. + if (new_bindex == bindex)
  13743. + continue;
  13744. + if (new_bindex < 0) {
  13745. + au_set_h_fptr(file, bindex, NULL);
  13746. + continue;
  13747. + }
  13748. +
  13749. + /* swap two lower inode, and loop again */
  13750. + q = fidir->fd_hfile + new_bindex;
  13751. + tmp = *q;
  13752. + *q = *p;
  13753. + *p = tmp;
  13754. + if (tmp.hf_file) {
  13755. + bindex--;
  13756. + p--;
  13757. + }
  13758. + }
  13759. +
  13760. + p = fidir->fd_hfile;
  13761. + if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
  13762. + bbot = au_sbbot(sb);
  13763. + for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
  13764. + finfo->fi_btop++, p++)
  13765. + if (p->hf_file) {
  13766. + if (file_inode(p->hf_file))
  13767. + break;
  13768. + au_hfput(p, /*execed*/0);
  13769. + }
  13770. + } else {
  13771. + bbot = au_br_index(sb, brid);
  13772. + for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
  13773. + finfo->fi_btop++, p++)
  13774. + if (p->hf_file)
  13775. + au_hfput(p, /*execed*/0);
  13776. + bbot = au_sbbot(sb);
  13777. + }
  13778. +
  13779. + p = fidir->fd_hfile + bbot;
  13780. + for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
  13781. + fidir->fd_bbot--, p--)
  13782. + if (p->hf_file) {
  13783. + if (file_inode(p->hf_file))
  13784. + break;
  13785. + au_hfput(p, /*execed*/0);
  13786. + }
  13787. + AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
  13788. +}
  13789. +
  13790. +/*
  13791. + * after branch manipulating, refresh the file.
  13792. + */
  13793. +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
  13794. +{
  13795. + int err, need_reopen, nbr;
  13796. + aufs_bindex_t bbot, bindex;
  13797. + struct dentry *dentry;
  13798. + struct super_block *sb;
  13799. + struct au_finfo *finfo;
  13800. + struct au_hfile *hfile;
  13801. +
  13802. + dentry = file->f_path.dentry;
  13803. + sb = dentry->d_sb;
  13804. + nbr = au_sbbot(sb) + 1;
  13805. + finfo = au_fi(file);
  13806. + if (!finfo->fi_hdir) {
  13807. + hfile = &finfo->fi_htop;
  13808. + AuDebugOn(!hfile->hf_file);
  13809. + bindex = au_br_index(sb, hfile->hf_br->br_id);
  13810. + AuDebugOn(bindex < 0);
  13811. + if (bindex != finfo->fi_btop)
  13812. + au_set_fbtop(file, bindex);
  13813. + } else {
  13814. + err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
  13815. + if (unlikely(err))
  13816. + goto out;
  13817. + au_do_refresh_dir(file);
  13818. + }
  13819. +
  13820. + err = 0;
  13821. + need_reopen = 1;
  13822. + if (!au_test_mmapped(file))
  13823. + err = au_file_refresh_by_inode(file, &need_reopen);
  13824. + if (finfo->fi_hdir)
  13825. + /* harmless if err */
  13826. + au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
  13827. + if (!err && need_reopen && !d_unlinked(dentry))
  13828. + err = reopen(file);
  13829. + if (!err) {
  13830. + au_update_figen(file);
  13831. + goto out; /* success */
  13832. + }
  13833. +
  13834. + /* error, close all lower files */
  13835. + if (finfo->fi_hdir) {
  13836. + bbot = au_fbbot_dir(file);
  13837. + for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
  13838. + au_set_h_fptr(file, bindex, NULL);
  13839. + }
  13840. +
  13841. +out:
  13842. + return err;
  13843. +}
  13844. +
  13845. +/* common function to regular file and dir */
  13846. +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
  13847. + int wlock, unsigned int fi_lsc)
  13848. +{
  13849. + int err;
  13850. + unsigned int sigen, figen;
  13851. + aufs_bindex_t btop;
  13852. + unsigned char pseudo_link;
  13853. + struct dentry *dentry;
  13854. + struct inode *inode;
  13855. +
  13856. + err = 0;
  13857. + dentry = file->f_path.dentry;
  13858. + inode = d_inode(dentry);
  13859. + sigen = au_sigen(dentry->d_sb);
  13860. + fi_write_lock_nested(file, fi_lsc);
  13861. + figen = au_figen(file);
  13862. + if (!fi_lsc)
  13863. + di_write_lock_child(dentry);
  13864. + else
  13865. + di_write_lock_child2(dentry);
  13866. + btop = au_dbtop(dentry);
  13867. + pseudo_link = (btop != au_ibtop(inode));
  13868. + if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
  13869. + if (!wlock) {
  13870. + di_downgrade_lock(dentry, AuLock_IR);
  13871. + fi_downgrade_lock(file);
  13872. + }
  13873. + goto out; /* success */
  13874. + }
  13875. +
  13876. + AuDbg("sigen %d, figen %d\n", sigen, figen);
  13877. + if (au_digen_test(dentry, sigen)) {
  13878. + err = au_reval_dpath(dentry, sigen);
  13879. + AuDebugOn(!err && au_digen_test(dentry, sigen));
  13880. + }
  13881. +
  13882. + if (!err)
  13883. + err = refresh_file(file, reopen);
  13884. + if (!err) {
  13885. + if (!wlock) {
  13886. + di_downgrade_lock(dentry, AuLock_IR);
  13887. + fi_downgrade_lock(file);
  13888. + }
  13889. + } else {
  13890. + di_write_unlock(dentry);
  13891. + fi_write_unlock(file);
  13892. + }
  13893. +
  13894. +out:
  13895. + return err;
  13896. +}
  13897. +
  13898. +/* ---------------------------------------------------------------------- */
  13899. +
  13900. +/* cf. aufs_nopage() */
  13901. +/* for madvise(2) */
  13902. +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
  13903. +{
  13904. + unlock_page(page);
  13905. + return 0;
  13906. +}
  13907. +
  13908. +/* it will never be called, but necessary to support O_DIRECT */
  13909. +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  13910. +{ BUG(); return 0; }
  13911. +
  13912. +/* they will never be called. */
  13913. +#ifdef CONFIG_AUFS_DEBUG
  13914. +static int aufs_write_begin(struct file *file, struct address_space *mapping,
  13915. + loff_t pos, unsigned len, unsigned flags,
  13916. + struct page **pagep, void **fsdata)
  13917. +{ AuUnsupport(); return 0; }
  13918. +static int aufs_write_end(struct file *file, struct address_space *mapping,
  13919. + loff_t pos, unsigned len, unsigned copied,
  13920. + struct page *page, void *fsdata)
  13921. +{ AuUnsupport(); return 0; }
  13922. +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
  13923. +{ AuUnsupport(); return 0; }
  13924. +
  13925. +static int aufs_set_page_dirty(struct page *page)
  13926. +{ AuUnsupport(); return 0; }
  13927. +static void aufs_invalidatepage(struct page *page, unsigned int offset,
  13928. + unsigned int length)
  13929. +{ AuUnsupport(); }
  13930. +static int aufs_releasepage(struct page *page, gfp_t gfp)
  13931. +{ AuUnsupport(); return 0; }
  13932. +#if 0 /* called by memory compaction regardless file */
  13933. +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
  13934. + struct page *page, enum migrate_mode mode)
  13935. +{ AuUnsupport(); return 0; }
  13936. +#endif
  13937. +static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
  13938. +{ AuUnsupport(); return true; }
  13939. +static void aufs_putback_page(struct page *page)
  13940. +{ AuUnsupport(); }
  13941. +static int aufs_launder_page(struct page *page)
  13942. +{ AuUnsupport(); return 0; }
  13943. +static int aufs_is_partially_uptodate(struct page *page,
  13944. + unsigned long from,
  13945. + unsigned long count)
  13946. +{ AuUnsupport(); return 0; }
  13947. +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
  13948. + bool *writeback)
  13949. +{ AuUnsupport(); }
  13950. +static int aufs_error_remove_page(struct address_space *mapping,
  13951. + struct page *page)
  13952. +{ AuUnsupport(); return 0; }
  13953. +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
  13954. + sector_t *span)
  13955. +{ AuUnsupport(); return 0; }
  13956. +static void aufs_swap_deactivate(struct file *file)
  13957. +{ AuUnsupport(); }
  13958. +#endif /* CONFIG_AUFS_DEBUG */
  13959. +
  13960. +const struct address_space_operations aufs_aop = {
  13961. + .readpage = aufs_readpage,
  13962. + .direct_IO = aufs_direct_IO,
  13963. +#ifdef CONFIG_AUFS_DEBUG
  13964. + .writepage = aufs_writepage,
  13965. + /* no writepages, because of writepage */
  13966. + .set_page_dirty = aufs_set_page_dirty,
  13967. + /* no readpages, because of readpage */
  13968. + .write_begin = aufs_write_begin,
  13969. + .write_end = aufs_write_end,
  13970. + /* no bmap, no block device */
  13971. + .invalidatepage = aufs_invalidatepage,
  13972. + .releasepage = aufs_releasepage,
  13973. + /* is fallback_migrate_page ok? */
  13974. + /* .migratepage = aufs_migratepage, */
  13975. + .isolate_page = aufs_isolate_page,
  13976. + .putback_page = aufs_putback_page,
  13977. + .launder_page = aufs_launder_page,
  13978. + .is_partially_uptodate = aufs_is_partially_uptodate,
  13979. + .is_dirty_writeback = aufs_is_dirty_writeback,
  13980. + .error_remove_page = aufs_error_remove_page,
  13981. + .swap_activate = aufs_swap_activate,
  13982. + .swap_deactivate = aufs_swap_deactivate
  13983. +#endif /* CONFIG_AUFS_DEBUG */
  13984. +};
  13985. diff -Naur null/fs/aufs/file.h linux-5.15.131/fs/aufs/file.h
  13986. --- /dev/null
  13987. +++ linux-5.15.131/fs/aufs/file.h 2023-09-11 14:32:48.148722552 +0300
  13988. @@ -0,0 +1,342 @@
  13989. +/* SPDX-License-Identifier: GPL-2.0 */
  13990. +/*
  13991. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  13992. + *
  13993. + * This program, aufs is free software; you can redistribute it and/or modify
  13994. + * it under the terms of the GNU General Public License as published by
  13995. + * the Free Software Foundation; either version 2 of the License, or
  13996. + * (at your option) any later version.
  13997. + *
  13998. + * This program is distributed in the hope that it will be useful,
  13999. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14000. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14001. + * GNU General Public License for more details.
  14002. + *
  14003. + * You should have received a copy of the GNU General Public License
  14004. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14005. + */
  14006. +
  14007. +/*
  14008. + * file operations
  14009. + */
  14010. +
  14011. +#ifndef __AUFS_FILE_H__
  14012. +#define __AUFS_FILE_H__
  14013. +
  14014. +#ifdef __KERNEL__
  14015. +
  14016. +#include <linux/file.h>
  14017. +#include <linux/fs.h>
  14018. +#include <linux/mm_types.h>
  14019. +#include <linux/poll.h>
  14020. +#include "rwsem.h"
  14021. +
  14022. +struct au_branch;
  14023. +struct au_hfile {
  14024. + struct file *hf_file;
  14025. + struct au_branch *hf_br;
  14026. +};
  14027. +
  14028. +struct au_vdir;
  14029. +struct au_fidir {
  14030. + aufs_bindex_t fd_bbot;
  14031. + aufs_bindex_t fd_nent;
  14032. + struct au_vdir *fd_vdir_cache;
  14033. + struct au_hfile fd_hfile[];
  14034. +};
  14035. +
  14036. +static inline int au_fidir_sz(int nent)
  14037. +{
  14038. + AuDebugOn(nent < 0);
  14039. + return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
  14040. +}
  14041. +
  14042. +struct au_finfo {
  14043. + atomic_t fi_generation;
  14044. +
  14045. + struct au_rwsem fi_rwsem;
  14046. + aufs_bindex_t fi_btop;
  14047. +
  14048. + /* do not union them */
  14049. + struct { /* for non-dir */
  14050. + struct au_hfile fi_htop;
  14051. + atomic_t fi_mmapped;
  14052. + };
  14053. + struct au_fidir *fi_hdir; /* for dir only */
  14054. +
  14055. + struct hlist_bl_node fi_hlist;
  14056. + struct file *fi_file; /* very ugly */
  14057. + struct rcu_head rcu;
  14058. +} ____cacheline_aligned_in_smp;
  14059. +
  14060. +/* ---------------------------------------------------------------------- */
  14061. +
  14062. +/* file.c */
  14063. +extern const struct address_space_operations aufs_aop;
  14064. +unsigned int au_file_roflags(unsigned int flags);
  14065. +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
  14066. + struct file *file, int force_wr);
  14067. +struct au_do_open_args {
  14068. + int aopen;
  14069. + int (*open)(struct file *file, int flags,
  14070. + struct file *h_file);
  14071. + struct au_fidir *fidir;
  14072. + struct file *h_file;
  14073. +};
  14074. +int au_do_open(struct file *file, struct au_do_open_args *args);
  14075. +int au_reopen_nondir(struct file *file);
  14076. +struct au_pin;
  14077. +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
  14078. +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
  14079. + int wlock, unsigned int fi_lsc);
  14080. +int au_do_flush(struct file *file, fl_owner_t id,
  14081. + int (*flush)(struct file *file, fl_owner_t id));
  14082. +
  14083. +/* poll.c */
  14084. +#ifdef CONFIG_AUFS_POLL
  14085. +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
  14086. +#endif
  14087. +
  14088. +#ifdef CONFIG_AUFS_BR_HFSPLUS
  14089. +/* hfsplus.c */
  14090. +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
  14091. + int force_wr);
  14092. +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
  14093. + struct file *h_file);
  14094. +#else
  14095. +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
  14096. + aufs_bindex_t bindex, int force_wr)
  14097. +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
  14098. + struct file *h_file);
  14099. +#endif
  14100. +
  14101. +/* f_op.c */
  14102. +extern const struct file_operations aufs_file_fop;
  14103. +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
  14104. +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
  14105. +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
  14106. +
  14107. +/* finfo.c */
  14108. +void au_hfput(struct au_hfile *hf, int execed);
  14109. +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
  14110. + struct file *h_file);
  14111. +
  14112. +void au_update_figen(struct file *file);
  14113. +struct au_fidir *au_fidir_alloc(struct super_block *sb);
  14114. +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
  14115. +
  14116. +void au_fi_init_once(void *_fi);
  14117. +void au_finfo_fin(struct file *file);
  14118. +int au_finfo_init(struct file *file, struct au_fidir *fidir);
  14119. +
  14120. +/* ioctl.c */
  14121. +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
  14122. +#ifdef CONFIG_COMPAT
  14123. +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
  14124. + unsigned long arg);
  14125. +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
  14126. + unsigned long arg);
  14127. +#endif
  14128. +
  14129. +/* ---------------------------------------------------------------------- */
  14130. +
  14131. +static inline struct au_finfo *au_fi(struct file *file)
  14132. +{
  14133. + return file->private_data;
  14134. +}
  14135. +
  14136. +/* ---------------------------------------------------------------------- */
  14137. +
  14138. +#define fi_read_lock(f) au_rw_read_lock(&au_fi(f)->fi_rwsem)
  14139. +#define fi_write_lock(f) au_rw_write_lock(&au_fi(f)->fi_rwsem)
  14140. +#define fi_read_trylock(f) au_rw_read_trylock(&au_fi(f)->fi_rwsem)
  14141. +#define fi_write_trylock(f) au_rw_write_trylock(&au_fi(f)->fi_rwsem)
  14142. +/*
  14143. +#define fi_read_trylock_nested(f) \
  14144. + au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
  14145. +#define fi_write_trylock_nested(f) \
  14146. + au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
  14147. +*/
  14148. +
  14149. +#define fi_read_unlock(f) au_rw_read_unlock(&au_fi(f)->fi_rwsem)
  14150. +#define fi_write_unlock(f) au_rw_write_unlock(&au_fi(f)->fi_rwsem)
  14151. +#define fi_downgrade_lock(f) au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
  14152. +
  14153. +/* lock subclass for finfo */
  14154. +enum {
  14155. + AuLsc_FI_1,
  14156. + AuLsc_FI_2
  14157. +};
  14158. +
  14159. +static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
  14160. +{
  14161. + au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
  14162. +}
  14163. +
  14164. +static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
  14165. +{
  14166. + au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
  14167. +}
  14168. +
  14169. +/*
  14170. + * fi_read_lock_1, fi_write_lock_1,
  14171. + * fi_read_lock_2, fi_write_lock_2
  14172. + */
  14173. +#define AuReadLockFunc(name) \
  14174. +static inline void fi_read_lock_##name(struct file *f) \
  14175. +{ fi_read_lock_nested(f, AuLsc_FI_##name); }
  14176. +
  14177. +#define AuWriteLockFunc(name) \
  14178. +static inline void fi_write_lock_##name(struct file *f) \
  14179. +{ fi_write_lock_nested(f, AuLsc_FI_##name); }
  14180. +
  14181. +#define AuRWLockFuncs(name) \
  14182. + AuReadLockFunc(name) \
  14183. + AuWriteLockFunc(name)
  14184. +
  14185. +AuRWLockFuncs(1);
  14186. +AuRWLockFuncs(2);
  14187. +
  14188. +#undef AuReadLockFunc
  14189. +#undef AuWriteLockFunc
  14190. +#undef AuRWLockFuncs
  14191. +
  14192. +#define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
  14193. +#define FiMustAnyLock(f) AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
  14194. +#define FiMustWriteLock(f) AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
  14195. +
  14196. +/* ---------------------------------------------------------------------- */
  14197. +
  14198. +/* todo: hard/soft set? */
  14199. +static inline aufs_bindex_t au_fbtop(struct file *file)
  14200. +{
  14201. + FiMustAnyLock(file);
  14202. + return au_fi(file)->fi_btop;
  14203. +}
  14204. +
  14205. +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
  14206. +{
  14207. + FiMustAnyLock(file);
  14208. + AuDebugOn(!au_fi(file)->fi_hdir);
  14209. + return au_fi(file)->fi_hdir->fd_bbot;
  14210. +}
  14211. +
  14212. +static inline struct au_vdir *au_fvdir_cache(struct file *file)
  14213. +{
  14214. + FiMustAnyLock(file);
  14215. + AuDebugOn(!au_fi(file)->fi_hdir);
  14216. + return au_fi(file)->fi_hdir->fd_vdir_cache;
  14217. +}
  14218. +
  14219. +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
  14220. +{
  14221. + FiMustWriteLock(file);
  14222. + au_fi(file)->fi_btop = bindex;
  14223. +}
  14224. +
  14225. +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
  14226. +{
  14227. + FiMustWriteLock(file);
  14228. + AuDebugOn(!au_fi(file)->fi_hdir);
  14229. + au_fi(file)->fi_hdir->fd_bbot = bindex;
  14230. +}
  14231. +
  14232. +static inline void au_set_fvdir_cache(struct file *file,
  14233. + struct au_vdir *vdir_cache)
  14234. +{
  14235. + FiMustWriteLock(file);
  14236. + AuDebugOn(!au_fi(file)->fi_hdir);
  14237. + au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
  14238. +}
  14239. +
  14240. +static inline struct file *au_hf_top(struct file *file)
  14241. +{
  14242. + FiMustAnyLock(file);
  14243. + AuDebugOn(au_fi(file)->fi_hdir);
  14244. + return au_fi(file)->fi_htop.hf_file;
  14245. +}
  14246. +
  14247. +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
  14248. +{
  14249. + FiMustAnyLock(file);
  14250. + AuDebugOn(!au_fi(file)->fi_hdir);
  14251. + return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
  14252. +}
  14253. +
  14254. +/* todo: memory barrier? */
  14255. +static inline unsigned int au_figen(struct file *f)
  14256. +{
  14257. + return atomic_read(&au_fi(f)->fi_generation);
  14258. +}
  14259. +
  14260. +static inline void au_set_mmapped(struct file *f)
  14261. +{
  14262. + if (atomic_inc_return(&au_fi(f)->fi_mmapped))
  14263. + return;
  14264. + pr_warn("fi_mmapped wrapped around\n");
  14265. + while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
  14266. + ;
  14267. +}
  14268. +
  14269. +static inline void au_unset_mmapped(struct file *f)
  14270. +{
  14271. + atomic_dec(&au_fi(f)->fi_mmapped);
  14272. +}
  14273. +
  14274. +static inline int au_test_mmapped(struct file *f)
  14275. +{
  14276. + return atomic_read(&au_fi(f)->fi_mmapped);
  14277. +}
  14278. +
  14279. +/* customize vma->vm_file */
  14280. +
  14281. +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
  14282. + struct file *file)
  14283. +{
  14284. + struct file *f;
  14285. +
  14286. + f = vma->vm_file;
  14287. + get_file(file);
  14288. + vma->vm_file = file;
  14289. + fput(f);
  14290. +}
  14291. +
  14292. +#ifdef CONFIG_MMU
  14293. +#define AuDbgVmRegion(file, vma) do {} while (0)
  14294. +
  14295. +static inline void au_vm_file_reset(struct vm_area_struct *vma,
  14296. + struct file *file)
  14297. +{
  14298. + au_do_vm_file_reset(vma, file);
  14299. +}
  14300. +#else
  14301. +#define AuDbgVmRegion(file, vma) \
  14302. + AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
  14303. +
  14304. +static inline void au_vm_file_reset(struct vm_area_struct *vma,
  14305. + struct file *file)
  14306. +{
  14307. + struct file *f;
  14308. +
  14309. + au_do_vm_file_reset(vma, file);
  14310. + f = vma->vm_region->vm_file;
  14311. + get_file(file);
  14312. + vma->vm_region->vm_file = file;
  14313. + fput(f);
  14314. +}
  14315. +#endif /* CONFIG_MMU */
  14316. +
  14317. +/* handle vma->vm_prfile */
  14318. +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
  14319. + struct file *file)
  14320. +{
  14321. + get_file(file);
  14322. + vma->vm_prfile = file;
  14323. +#ifndef CONFIG_MMU
  14324. + get_file(file);
  14325. + vma->vm_region->vm_prfile = file;
  14326. +#endif
  14327. +}
  14328. +
  14329. +#endif /* __KERNEL__ */
  14330. +#endif /* __AUFS_FILE_H__ */
  14331. diff -Naur null/fs/aufs/finfo.c linux-5.15.131/fs/aufs/finfo.c
  14332. --- /dev/null
  14333. +++ linux-5.15.131/fs/aufs/finfo.c 2023-09-11 14:32:48.148722552 +0300
  14334. @@ -0,0 +1,149 @@
  14335. +// SPDX-License-Identifier: GPL-2.0
  14336. +/*
  14337. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  14338. + *
  14339. + * This program, aufs is free software; you can redistribute it and/or modify
  14340. + * it under the terms of the GNU General Public License as published by
  14341. + * the Free Software Foundation; either version 2 of the License, or
  14342. + * (at your option) any later version.
  14343. + *
  14344. + * This program is distributed in the hope that it will be useful,
  14345. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14346. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14347. + * GNU General Public License for more details.
  14348. + *
  14349. + * You should have received a copy of the GNU General Public License
  14350. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14351. + */
  14352. +
  14353. +/*
  14354. + * file private data
  14355. + */
  14356. +
  14357. +#include "aufs.h"
  14358. +
  14359. +void au_hfput(struct au_hfile *hf, int execed)
  14360. +{
  14361. + if (execed)
  14362. + allow_write_access(hf->hf_file);
  14363. + fput(hf->hf_file);
  14364. + hf->hf_file = NULL;
  14365. + au_lcnt_dec(&hf->hf_br->br_nfiles);
  14366. + hf->hf_br = NULL;
  14367. +}
  14368. +
  14369. +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
  14370. +{
  14371. + struct au_finfo *finfo = au_fi(file);
  14372. + struct au_hfile *hf;
  14373. + struct au_fidir *fidir;
  14374. +
  14375. + fidir = finfo->fi_hdir;
  14376. + if (!fidir) {
  14377. + AuDebugOn(finfo->fi_btop != bindex);
  14378. + hf = &finfo->fi_htop;
  14379. + } else
  14380. + hf = fidir->fd_hfile + bindex;
  14381. +
  14382. + if (hf && hf->hf_file)
  14383. + au_hfput(hf, vfsub_file_execed(file));
  14384. + if (val) {
  14385. + FiMustWriteLock(file);
  14386. + AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
  14387. + hf->hf_file = val;
  14388. + hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
  14389. + }
  14390. +}
  14391. +
  14392. +void au_update_figen(struct file *file)
  14393. +{
  14394. + atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
  14395. + /* smp_mb(); */ /* atomic_set */
  14396. +}
  14397. +
  14398. +/* ---------------------------------------------------------------------- */
  14399. +
  14400. +struct au_fidir *au_fidir_alloc(struct super_block *sb)
  14401. +{
  14402. + struct au_fidir *fidir;
  14403. + int nbr;
  14404. +
  14405. + nbr = au_sbbot(sb) + 1;
  14406. + if (nbr < 2)
  14407. + nbr = 2; /* initial allocate for 2 branches */
  14408. + fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
  14409. + if (fidir) {
  14410. + fidir->fd_bbot = -1;
  14411. + fidir->fd_nent = nbr;
  14412. + }
  14413. +
  14414. + return fidir;
  14415. +}
  14416. +
  14417. +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
  14418. +{
  14419. + int err;
  14420. + struct au_fidir *fidir, *p;
  14421. +
  14422. + AuRwMustWriteLock(&finfo->fi_rwsem);
  14423. + fidir = finfo->fi_hdir;
  14424. + AuDebugOn(!fidir);
  14425. +
  14426. + err = -ENOMEM;
  14427. + p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
  14428. + GFP_NOFS, may_shrink);
  14429. + if (p) {
  14430. + p->fd_nent = nbr;
  14431. + finfo->fi_hdir = p;
  14432. + err = 0;
  14433. + }
  14434. +
  14435. + return err;
  14436. +}
  14437. +
  14438. +/* ---------------------------------------------------------------------- */
  14439. +
  14440. +void au_finfo_fin(struct file *file)
  14441. +{
  14442. + struct au_finfo *finfo;
  14443. +
  14444. + au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
  14445. +
  14446. + finfo = au_fi(file);
  14447. + AuDebugOn(finfo->fi_hdir);
  14448. + AuRwDestroy(&finfo->fi_rwsem);
  14449. + au_cache_free_finfo(finfo);
  14450. +}
  14451. +
  14452. +void au_fi_init_once(void *_finfo)
  14453. +{
  14454. + struct au_finfo *finfo = _finfo;
  14455. +
  14456. + au_rw_init(&finfo->fi_rwsem);
  14457. +}
  14458. +
  14459. +int au_finfo_init(struct file *file, struct au_fidir *fidir)
  14460. +{
  14461. + int err;
  14462. + struct au_finfo *finfo;
  14463. + struct dentry *dentry;
  14464. +
  14465. + err = -ENOMEM;
  14466. + dentry = file->f_path.dentry;
  14467. + finfo = au_cache_alloc_finfo();
  14468. + if (unlikely(!finfo))
  14469. + goto out;
  14470. +
  14471. + err = 0;
  14472. + au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
  14473. + au_rw_write_lock(&finfo->fi_rwsem);
  14474. + finfo->fi_btop = -1;
  14475. + finfo->fi_hdir = fidir;
  14476. + atomic_set(&finfo->fi_generation, au_digen(dentry));
  14477. + /* smp_mb(); */ /* atomic_set */
  14478. +
  14479. + file->private_data = finfo;
  14480. +
  14481. +out:
  14482. + return err;
  14483. +}
  14484. diff -Naur null/fs/aufs/f_op.c linux-5.15.131/fs/aufs/f_op.c
  14485. --- /dev/null
  14486. +++ linux-5.15.131/fs/aufs/f_op.c 2023-09-11 14:32:48.147722564 +0300
  14487. @@ -0,0 +1,775 @@
  14488. +// SPDX-License-Identifier: GPL-2.0
  14489. +/*
  14490. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  14491. + *
  14492. + * This program, aufs is free software; you can redistribute it and/or modify
  14493. + * it under the terms of the GNU General Public License as published by
  14494. + * the Free Software Foundation; either version 2 of the License, or
  14495. + * (at your option) any later version.
  14496. + *
  14497. + * This program is distributed in the hope that it will be useful,
  14498. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14499. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14500. + * GNU General Public License for more details.
  14501. + *
  14502. + * You should have received a copy of the GNU General Public License
  14503. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14504. + */
  14505. +
  14506. +/*
  14507. + * file and vm operations
  14508. + */
  14509. +
  14510. +#include <linux/aio.h>
  14511. +#include <linux/fs_stack.h>
  14512. +#include <linux/mman.h>
  14513. +#include <linux/security.h>
  14514. +#include "aufs.h"
  14515. +
  14516. +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
  14517. +{
  14518. + int err;
  14519. + aufs_bindex_t bindex;
  14520. + struct dentry *dentry, *h_dentry;
  14521. + struct au_finfo *finfo;
  14522. + struct inode *h_inode;
  14523. +
  14524. + FiMustWriteLock(file);
  14525. +
  14526. + err = 0;
  14527. + dentry = file->f_path.dentry;
  14528. + AuDebugOn(IS_ERR_OR_NULL(dentry));
  14529. + finfo = au_fi(file);
  14530. + memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
  14531. + atomic_set(&finfo->fi_mmapped, 0);
  14532. + bindex = au_dbtop(dentry);
  14533. + if (!h_file) {
  14534. + h_dentry = au_h_dptr(dentry, bindex);
  14535. + err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
  14536. + if (unlikely(err))
  14537. + goto out;
  14538. + h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
  14539. + if (IS_ERR(h_file)) {
  14540. + err = PTR_ERR(h_file);
  14541. + goto out;
  14542. + }
  14543. + } else {
  14544. + h_dentry = h_file->f_path.dentry;
  14545. + err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
  14546. + if (unlikely(err))
  14547. + goto out;
  14548. + /* br ref is already inc-ed */
  14549. + }
  14550. +
  14551. + if ((flags & __O_TMPFILE)
  14552. + && !(flags & O_EXCL)) {
  14553. + h_inode = file_inode(h_file);
  14554. + spin_lock(&h_inode->i_lock);
  14555. + h_inode->i_state |= I_LINKABLE;
  14556. + spin_unlock(&h_inode->i_lock);
  14557. + }
  14558. + au_set_fbtop(file, bindex);
  14559. + au_set_h_fptr(file, bindex, h_file);
  14560. + au_update_figen(file);
  14561. + /* todo: necessary? */
  14562. + /* file->f_ra = h_file->f_ra; */
  14563. +
  14564. +out:
  14565. + return err;
  14566. +}
  14567. +
  14568. +static int aufs_open_nondir(struct inode *inode __maybe_unused,
  14569. + struct file *file)
  14570. +{
  14571. + int err;
  14572. + struct super_block *sb;
  14573. + struct au_do_open_args args = {
  14574. + .open = au_do_open_nondir
  14575. + };
  14576. +
  14577. + AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
  14578. + file, vfsub_file_flags(file), file->f_mode);
  14579. +
  14580. + sb = file->f_path.dentry->d_sb;
  14581. + si_read_lock(sb, AuLock_FLUSH);
  14582. + err = au_do_open(file, &args);
  14583. + si_read_unlock(sb);
  14584. + return err;
  14585. +}
  14586. +
  14587. +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
  14588. +{
  14589. + struct au_finfo *finfo;
  14590. + aufs_bindex_t bindex;
  14591. +
  14592. + finfo = au_fi(file);
  14593. + au_hbl_del(&finfo->fi_hlist,
  14594. + &au_sbi(file->f_path.dentry->d_sb)->si_files);
  14595. + bindex = finfo->fi_btop;
  14596. + if (bindex >= 0)
  14597. + au_set_h_fptr(file, bindex, NULL);
  14598. +
  14599. + au_finfo_fin(file);
  14600. + return 0;
  14601. +}
  14602. +
  14603. +/* ---------------------------------------------------------------------- */
  14604. +
  14605. +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
  14606. +{
  14607. + int err;
  14608. + struct file *h_file;
  14609. +
  14610. + err = 0;
  14611. + h_file = au_hf_top(file);
  14612. + if (h_file)
  14613. + err = vfsub_flush(h_file, id);
  14614. + return err;
  14615. +}
  14616. +
  14617. +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
  14618. +{
  14619. + return au_do_flush(file, id, au_do_flush_nondir);
  14620. +}
  14621. +
  14622. +/* ---------------------------------------------------------------------- */
  14623. +/*
  14624. + * read and write functions acquire [fdi]_rwsem once, but release before
  14625. + * mmap_sem. This is because to stop a race condition between mmap(2).
  14626. + * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
  14627. + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
  14628. + * read functions after [fdi]_rwsem are released, but it should be harmless.
  14629. + */
  14630. +
  14631. +/* Callers should call au_read_post() or fput() in the end */
  14632. +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
  14633. +{
  14634. + struct file *h_file;
  14635. + int err;
  14636. +
  14637. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
  14638. + if (!err) {
  14639. + di_read_unlock(file->f_path.dentry, AuLock_IR);
  14640. + h_file = au_hf_top(file);
  14641. + get_file(h_file);
  14642. + if (!keep_fi)
  14643. + fi_read_unlock(file);
  14644. + } else
  14645. + h_file = ERR_PTR(err);
  14646. +
  14647. + return h_file;
  14648. +}
  14649. +
  14650. +static void au_read_post(struct inode *inode, struct file *h_file)
  14651. +{
  14652. + /* update without lock, I don't think it a problem */
  14653. + fsstack_copy_attr_atime(inode, file_inode(h_file));
  14654. + fput(h_file);
  14655. +}
  14656. +
  14657. +struct au_write_pre {
  14658. + /* input */
  14659. + unsigned int lsc;
  14660. +
  14661. + /* output */
  14662. + blkcnt_t blks;
  14663. + aufs_bindex_t btop;
  14664. +};
  14665. +
  14666. +/*
  14667. + * return with iinfo is write-locked
  14668. + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
  14669. + * end
  14670. + */
  14671. +static struct file *au_write_pre(struct file *file, int do_ready,
  14672. + struct au_write_pre *wpre)
  14673. +{
  14674. + struct file *h_file;
  14675. + struct dentry *dentry;
  14676. + int err;
  14677. + unsigned int lsc;
  14678. + struct au_pin pin;
  14679. +
  14680. + lsc = 0;
  14681. + if (wpre)
  14682. + lsc = wpre->lsc;
  14683. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
  14684. + h_file = ERR_PTR(err);
  14685. + if (unlikely(err))
  14686. + goto out;
  14687. +
  14688. + dentry = file->f_path.dentry;
  14689. + if (do_ready) {
  14690. + err = au_ready_to_write(file, -1, &pin);
  14691. + if (unlikely(err)) {
  14692. + h_file = ERR_PTR(err);
  14693. + di_write_unlock(dentry);
  14694. + goto out_fi;
  14695. + }
  14696. + }
  14697. +
  14698. + di_downgrade_lock(dentry, /*flags*/0);
  14699. + if (wpre)
  14700. + wpre->btop = au_fbtop(file);
  14701. + h_file = au_hf_top(file);
  14702. + get_file(h_file);
  14703. + if (wpre)
  14704. + wpre->blks = file_inode(h_file)->i_blocks;
  14705. + if (do_ready)
  14706. + au_unpin(&pin);
  14707. + di_read_unlock(dentry, /*flags*/0);
  14708. +
  14709. +out_fi:
  14710. + fi_write_unlock(file);
  14711. +out:
  14712. + return h_file;
  14713. +}
  14714. +
  14715. +static void au_write_post(struct inode *inode, struct file *h_file,
  14716. + struct au_write_pre *wpre, ssize_t written)
  14717. +{
  14718. + struct inode *h_inode;
  14719. +
  14720. + au_cpup_attr_timesizes(inode);
  14721. + AuDebugOn(au_ibtop(inode) != wpre->btop);
  14722. + h_inode = file_inode(h_file);
  14723. + inode->i_mode = h_inode->i_mode;
  14724. + ii_write_unlock(inode);
  14725. + /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
  14726. + if (written > 0)
  14727. + au_fhsm_wrote(inode->i_sb, wpre->btop,
  14728. + /*force*/h_inode->i_blocks > wpre->blks);
  14729. + fput(h_file);
  14730. +}
  14731. +
  14732. +/*
  14733. + * todo: very ugly
  14734. + * it locks both of i_mutex and si_rwsem for read in safe.
  14735. + * if the plink maintenance mode continues forever (that is the problem),
  14736. + * may loop forever.
  14737. + */
  14738. +static void au_mtx_and_read_lock(struct inode *inode)
  14739. +{
  14740. + int err;
  14741. + struct super_block *sb = inode->i_sb;
  14742. +
  14743. + while (1) {
  14744. + inode_lock(inode);
  14745. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  14746. + if (!err)
  14747. + break;
  14748. + inode_unlock(inode);
  14749. + si_read_lock(sb, AuLock_NOPLMW);
  14750. + si_read_unlock(sb);
  14751. + }
  14752. +}
  14753. +
  14754. +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
  14755. + struct iov_iter *iov_iter)
  14756. +{
  14757. + ssize_t err;
  14758. + struct file *file;
  14759. + ssize_t (*iter)(struct kiocb *, struct iov_iter *);
  14760. +
  14761. + err = security_file_permission(h_file, rw);
  14762. + if (unlikely(err))
  14763. + goto out;
  14764. +
  14765. + err = -ENOSYS; /* the branch doesn't have its ->(read|write)_iter() */
  14766. + iter = NULL;
  14767. + if (rw == MAY_READ)
  14768. + iter = h_file->f_op->read_iter;
  14769. + else if (rw == MAY_WRITE)
  14770. + iter = h_file->f_op->write_iter;
  14771. +
  14772. + file = kio->ki_filp;
  14773. + kio->ki_filp = h_file;
  14774. + if (iter) {
  14775. + lockdep_off();
  14776. + err = iter(kio, iov_iter);
  14777. + lockdep_on();
  14778. + } else
  14779. + /* currently there is no such fs */
  14780. + WARN_ON_ONCE(1);
  14781. + kio->ki_filp = file;
  14782. +
  14783. +out:
  14784. + return err;
  14785. +}
  14786. +
  14787. +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
  14788. +{
  14789. + ssize_t err;
  14790. + struct file *file, *h_file;
  14791. + struct inode *inode;
  14792. + struct super_block *sb;
  14793. +
  14794. + file = kio->ki_filp;
  14795. + inode = file_inode(file);
  14796. + sb = inode->i_sb;
  14797. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  14798. +
  14799. + h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
  14800. + err = PTR_ERR(h_file);
  14801. + if (IS_ERR(h_file))
  14802. + goto out;
  14803. +
  14804. + if (au_test_loopback_kthread()) {
  14805. + au_warn_loopback(h_file->f_path.dentry->d_sb);
  14806. + if (file->f_mapping != h_file->f_mapping) {
  14807. + file->f_mapping = h_file->f_mapping;
  14808. + smp_mb(); /* unnecessary? */
  14809. + }
  14810. + }
  14811. + fi_read_unlock(file);
  14812. +
  14813. + err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
  14814. + /* todo: necessary? */
  14815. + /* file->f_ra = h_file->f_ra; */
  14816. + au_read_post(inode, h_file);
  14817. +
  14818. +out:
  14819. + si_read_unlock(sb);
  14820. + return err;
  14821. +}
  14822. +
  14823. +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
  14824. +{
  14825. + ssize_t err;
  14826. + struct au_write_pre wpre;
  14827. + struct inode *inode;
  14828. + struct file *file, *h_file;
  14829. +
  14830. + file = kio->ki_filp;
  14831. + inode = file_inode(file);
  14832. + au_mtx_and_read_lock(inode);
  14833. +
  14834. + wpre.lsc = 0;
  14835. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  14836. + err = PTR_ERR(h_file);
  14837. + if (IS_ERR(h_file))
  14838. + goto out;
  14839. +
  14840. + err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
  14841. + au_write_post(inode, h_file, &wpre, err);
  14842. +
  14843. +out:
  14844. + si_read_unlock(inode->i_sb);
  14845. + inode_unlock(inode);
  14846. + return err;
  14847. +}
  14848. +
  14849. +/*
  14850. + * We may be able to remove aufs_splice_{read,write}() since almost all FSes
  14851. + * don't have their own .splice_{read,write} implimentations, and they use
  14852. + * generic_file_splice_read() and iter_file_splice_write() who can act like the
  14853. + * simple converters to f_op->iter_read() and ->iter_write().
  14854. + * But we keep our own implementations because some non-mainlined FSes may have
  14855. + * their own .splice_{read,write} implimentations and aufs doesn't want to take
  14856. + * away an opportunity to co-work with aufs from them.
  14857. + */
  14858. +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
  14859. + struct pipe_inode_info *pipe, size_t len,
  14860. + unsigned int flags)
  14861. +{
  14862. + ssize_t err;
  14863. + struct file *h_file;
  14864. + struct inode *inode;
  14865. + struct super_block *sb;
  14866. +
  14867. + inode = file_inode(file);
  14868. + sb = inode->i_sb;
  14869. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  14870. +
  14871. + h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
  14872. + err = PTR_ERR(h_file);
  14873. + if (IS_ERR(h_file))
  14874. + goto out;
  14875. +
  14876. + err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
  14877. + /* todo: necessary? */
  14878. + /* file->f_ra = h_file->f_ra; */
  14879. + au_read_post(inode, h_file);
  14880. +
  14881. +out:
  14882. + si_read_unlock(sb);
  14883. + return err;
  14884. +}
  14885. +
  14886. +static ssize_t
  14887. +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
  14888. + size_t len, unsigned int flags)
  14889. +{
  14890. + ssize_t err;
  14891. + struct au_write_pre wpre;
  14892. + struct inode *inode;
  14893. + struct file *h_file;
  14894. +
  14895. + inode = file_inode(file);
  14896. + au_mtx_and_read_lock(inode);
  14897. +
  14898. + wpre.lsc = 0;
  14899. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  14900. + err = PTR_ERR(h_file);
  14901. + if (IS_ERR(h_file))
  14902. + goto out;
  14903. +
  14904. + err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
  14905. + au_write_post(inode, h_file, &wpre, err);
  14906. +
  14907. +out:
  14908. + si_read_unlock(inode->i_sb);
  14909. + inode_unlock(inode);
  14910. + return err;
  14911. +}
  14912. +
  14913. +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
  14914. + loff_t len)
  14915. +{
  14916. + long err;
  14917. + struct au_write_pre wpre;
  14918. + struct inode *inode;
  14919. + struct file *h_file;
  14920. +
  14921. + inode = file_inode(file);
  14922. + au_mtx_and_read_lock(inode);
  14923. +
  14924. + wpre.lsc = 0;
  14925. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  14926. + err = PTR_ERR(h_file);
  14927. + if (IS_ERR(h_file))
  14928. + goto out;
  14929. +
  14930. + lockdep_off();
  14931. + err = vfs_fallocate(h_file, mode, offset, len);
  14932. + lockdep_on();
  14933. + /*
  14934. + * we don't need to call file_modifed() here since au_write_post()
  14935. + * is equivalent and copies-up all timestamps and permission bits.
  14936. + */
  14937. + au_write_post(inode, h_file, &wpre, /*written*/1);
  14938. +
  14939. +out:
  14940. + si_read_unlock(inode->i_sb);
  14941. + inode_unlock(inode);
  14942. + return err;
  14943. +}
  14944. +
  14945. +static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
  14946. + struct file *dst, loff_t dst_pos,
  14947. + size_t len, unsigned int flags)
  14948. +{
  14949. + ssize_t err;
  14950. + struct au_write_pre wpre;
  14951. + enum { SRC, DST };
  14952. + struct {
  14953. + struct inode *inode;
  14954. + struct file *h_file;
  14955. + struct super_block *h_sb;
  14956. + } a[2];
  14957. +#define a_src a[SRC]
  14958. +#define a_dst a[DST]
  14959. +
  14960. + err = -EINVAL;
  14961. + a_src.inode = file_inode(src);
  14962. + if (unlikely(!S_ISREG(a_src.inode->i_mode)))
  14963. + goto out;
  14964. + a_dst.inode = file_inode(dst);
  14965. + if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
  14966. + goto out;
  14967. +
  14968. + au_mtx_and_read_lock(a_dst.inode);
  14969. + /*
  14970. + * in order to match the order in di_write_lock2_{child,parent}(),
  14971. + * use f_path.dentry for this comparison.
  14972. + */
  14973. + if (src->f_path.dentry < dst->f_path.dentry) {
  14974. + a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
  14975. + err = PTR_ERR(a_src.h_file);
  14976. + if (IS_ERR(a_src.h_file))
  14977. + goto out_si;
  14978. +
  14979. + wpre.lsc = AuLsc_FI_2;
  14980. + a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
  14981. + err = PTR_ERR(a_dst.h_file);
  14982. + if (IS_ERR(a_dst.h_file)) {
  14983. + au_read_post(a_src.inode, a_src.h_file);
  14984. + goto out_si;
  14985. + }
  14986. + } else {
  14987. + wpre.lsc = AuLsc_FI_1;
  14988. + a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
  14989. + err = PTR_ERR(a_dst.h_file);
  14990. + if (IS_ERR(a_dst.h_file))
  14991. + goto out_si;
  14992. +
  14993. + a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
  14994. + err = PTR_ERR(a_src.h_file);
  14995. + if (IS_ERR(a_src.h_file)) {
  14996. + au_write_post(a_dst.inode, a_dst.h_file, &wpre,
  14997. + /*written*/0);
  14998. + goto out_si;
  14999. + }
  15000. + }
  15001. +
  15002. + err = -EXDEV;
  15003. + a_src.h_sb = file_inode(a_src.h_file)->i_sb;
  15004. + a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
  15005. + if (unlikely(a_src.h_sb != a_dst.h_sb)) {
  15006. + AuDbgFile(src);
  15007. + AuDbgFile(dst);
  15008. + goto out_file;
  15009. + }
  15010. +
  15011. + err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
  15012. + dst_pos, len, flags);
  15013. +
  15014. +out_file:
  15015. + au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
  15016. + fi_read_unlock(src);
  15017. + au_read_post(a_src.inode, a_src.h_file);
  15018. +out_si:
  15019. + si_read_unlock(a_dst.inode->i_sb);
  15020. + inode_unlock(a_dst.inode);
  15021. +out:
  15022. + return err;
  15023. +#undef a_src
  15024. +#undef a_dst
  15025. +}
  15026. +
  15027. +/* ---------------------------------------------------------------------- */
  15028. +
  15029. +/*
  15030. + * The locking order around current->mmap_sem.
  15031. + * - in most and regular cases
  15032. + * file I/O syscall -- aufs_read() or something
  15033. + * -- si_rwsem for read -- mmap_sem
  15034. + * (Note that [fdi]i_rwsem are released before mmap_sem).
  15035. + * - in mmap case
  15036. + * mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
  15037. + * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
  15038. + * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
  15039. + * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
  15040. + * It means that when aufs acquires si_rwsem for write, the process should never
  15041. + * acquire mmap_sem.
  15042. + *
  15043. + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
  15044. + * problem either since any directory is not able to be mmap-ed.
  15045. + * The similar scenario is applied to aufs_readlink() too.
  15046. + */
  15047. +
  15048. +#if 0 /* stop calling security_file_mmap() */
  15049. +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
  15050. +#define AuConv_VM_PROT(f, b) _calc_vm_trans(f, VM_##b, PROT_##b)
  15051. +
  15052. +static unsigned long au_arch_prot_conv(unsigned long flags)
  15053. +{
  15054. + /* currently ppc64 only */
  15055. +#ifdef CONFIG_PPC64
  15056. + /* cf. linux/arch/powerpc/include/asm/mman.h */
  15057. + AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
  15058. + return AuConv_VM_PROT(flags, SAO);
  15059. +#else
  15060. + AuDebugOn(arch_calc_vm_prot_bits(-1));
  15061. + return 0;
  15062. +#endif
  15063. +}
  15064. +
  15065. +static unsigned long au_prot_conv(unsigned long flags)
  15066. +{
  15067. + return AuConv_VM_PROT(flags, READ)
  15068. + | AuConv_VM_PROT(flags, WRITE)
  15069. + | AuConv_VM_PROT(flags, EXEC)
  15070. + | au_arch_prot_conv(flags);
  15071. +}
  15072. +
  15073. +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
  15074. +#define AuConv_VM_MAP(f, b) _calc_vm_trans(f, VM_##b, MAP_##b)
  15075. +
  15076. +static unsigned long au_flag_conv(unsigned long flags)
  15077. +{
  15078. + return AuConv_VM_MAP(flags, GROWSDOWN)
  15079. + | AuConv_VM_MAP(flags, DENYWRITE)
  15080. + | AuConv_VM_MAP(flags, LOCKED);
  15081. +}
  15082. +#endif
  15083. +
  15084. +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
  15085. +{
  15086. + int err;
  15087. + const unsigned char wlock
  15088. + = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
  15089. + struct super_block *sb;
  15090. + struct file *h_file;
  15091. + struct inode *inode;
  15092. +
  15093. + AuDbgVmRegion(file, vma);
  15094. +
  15095. + inode = file_inode(file);
  15096. + sb = inode->i_sb;
  15097. + lockdep_off();
  15098. + si_read_lock(sb, AuLock_NOPLMW);
  15099. +
  15100. + h_file = au_write_pre(file, wlock, /*wpre*/NULL);
  15101. + lockdep_on();
  15102. + err = PTR_ERR(h_file);
  15103. + if (IS_ERR(h_file))
  15104. + goto out;
  15105. +
  15106. + err = 0;
  15107. + au_set_mmapped(file);
  15108. + au_vm_file_reset(vma, h_file);
  15109. + /*
  15110. + * we cannot call security_mmap_file() here since it may acquire
  15111. + * mmap_sem or i_mutex.
  15112. + *
  15113. + * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
  15114. + * au_flag_conv(vma->vm_flags));
  15115. + */
  15116. + if (!err)
  15117. + err = call_mmap(h_file, vma);
  15118. + if (!err) {
  15119. + au_vm_prfile_set(vma, file);
  15120. + fsstack_copy_attr_atime(inode, file_inode(h_file));
  15121. + goto out_fput; /* success */
  15122. + }
  15123. + au_unset_mmapped(file);
  15124. + au_vm_file_reset(vma, file);
  15125. +
  15126. +out_fput:
  15127. + lockdep_off();
  15128. + ii_write_unlock(inode);
  15129. + lockdep_on();
  15130. + fput(h_file);
  15131. +out:
  15132. + lockdep_off();
  15133. + si_read_unlock(sb);
  15134. + lockdep_on();
  15135. + AuTraceErr(err);
  15136. + return err;
  15137. +}
  15138. +
  15139. +/* ---------------------------------------------------------------------- */
  15140. +
  15141. +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
  15142. + int datasync)
  15143. +{
  15144. + int err;
  15145. + struct au_write_pre wpre;
  15146. + struct inode *inode;
  15147. + struct file *h_file;
  15148. +
  15149. + err = 0; /* -EBADF; */ /* posix? */
  15150. + if (unlikely(!(file->f_mode & FMODE_WRITE)))
  15151. + goto out;
  15152. +
  15153. + inode = file_inode(file);
  15154. + au_mtx_and_read_lock(inode);
  15155. +
  15156. + wpre.lsc = 0;
  15157. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  15158. + err = PTR_ERR(h_file);
  15159. + if (IS_ERR(h_file))
  15160. + goto out_unlock;
  15161. +
  15162. + err = vfsub_fsync(h_file, &h_file->f_path, datasync);
  15163. + au_write_post(inode, h_file, &wpre, /*written*/0);
  15164. +
  15165. +out_unlock:
  15166. + si_read_unlock(inode->i_sb);
  15167. + inode_unlock(inode);
  15168. +out:
  15169. + return err;
  15170. +}
  15171. +
  15172. +static int aufs_fasync(int fd, struct file *file, int flag)
  15173. +{
  15174. + int err;
  15175. + struct file *h_file;
  15176. + struct super_block *sb;
  15177. +
  15178. + sb = file->f_path.dentry->d_sb;
  15179. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  15180. +
  15181. + h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
  15182. + err = PTR_ERR(h_file);
  15183. + if (IS_ERR(h_file))
  15184. + goto out;
  15185. +
  15186. + if (h_file->f_op->fasync)
  15187. + err = h_file->f_op->fasync(fd, h_file, flag);
  15188. + fput(h_file); /* instead of au_read_post() */
  15189. +
  15190. +out:
  15191. + si_read_unlock(sb);
  15192. + return err;
  15193. +}
  15194. +
  15195. +static int aufs_setfl(struct file *file, unsigned long arg)
  15196. +{
  15197. + int err;
  15198. + struct file *h_file;
  15199. + struct super_block *sb;
  15200. +
  15201. + sb = file->f_path.dentry->d_sb;
  15202. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  15203. +
  15204. + h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
  15205. + err = PTR_ERR(h_file);
  15206. + if (IS_ERR(h_file))
  15207. + goto out;
  15208. +
  15209. + /* stop calling h_file->fasync */
  15210. + arg |= vfsub_file_flags(file) & FASYNC;
  15211. + err = setfl(/*unused fd*/-1, h_file, arg);
  15212. + fput(h_file); /* instead of au_read_post() */
  15213. +
  15214. +out:
  15215. + si_read_unlock(sb);
  15216. + return err;
  15217. +}
  15218. +
  15219. +/* ---------------------------------------------------------------------- */
  15220. +
  15221. +/* no one supports this operation, currently */
  15222. +#if 0 /* reserved for future use */
  15223. +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
  15224. + size_t len, loff_t *pos, int more)
  15225. +{
  15226. +}
  15227. +#endif
  15228. +
  15229. +/* ---------------------------------------------------------------------- */
  15230. +
  15231. +const struct file_operations aufs_file_fop = {
  15232. + .owner = THIS_MODULE,
  15233. +
  15234. + .llseek = default_llseek,
  15235. +
  15236. + .read_iter = aufs_read_iter,
  15237. + .write_iter = aufs_write_iter,
  15238. +
  15239. +#ifdef CONFIG_AUFS_POLL
  15240. + .poll = aufs_poll,
  15241. +#endif
  15242. + .unlocked_ioctl = aufs_ioctl_nondir,
  15243. +#ifdef CONFIG_COMPAT
  15244. + .compat_ioctl = aufs_compat_ioctl_nondir,
  15245. +#endif
  15246. + .mmap = aufs_mmap,
  15247. + .open = aufs_open_nondir,
  15248. + .flush = aufs_flush_nondir,
  15249. + .release = aufs_release_nondir,
  15250. + .fsync = aufs_fsync_nondir,
  15251. + .fasync = aufs_fasync,
  15252. + /* .sendpage = aufs_sendpage, */
  15253. + .setfl = aufs_setfl,
  15254. + .splice_write = aufs_splice_write,
  15255. + .splice_read = aufs_splice_read,
  15256. +#if 0 /* reserved for future use */
  15257. + .aio_splice_write = aufs_aio_splice_write,
  15258. + .aio_splice_read = aufs_aio_splice_read,
  15259. +#endif
  15260. + .fallocate = aufs_fallocate,
  15261. + .copy_file_range = aufs_copy_file_range
  15262. +};
  15263. diff -Naur null/fs/aufs/fsctx.c linux-5.15.131/fs/aufs/fsctx.c
  15264. --- /dev/null
  15265. +++ linux-5.15.131/fs/aufs/fsctx.c 2023-09-11 14:32:48.148722552 +0300
  15266. @@ -0,0 +1,1242 @@
  15267. +// SPDX-License-Identifier: GPL-2.0
  15268. +/*
  15269. + * Copyright (C) 2022 Junjiro R. Okajima
  15270. + *
  15271. + * This program, aufs is free software; you can redistribute it and/or modify
  15272. + * it under the terms of the GNU General Public License as published by
  15273. + * the Free Software Foundation; either version 2 of the License, or
  15274. + * (at your option) any later version.
  15275. + *
  15276. + * This program is distributed in the hope that it will be useful,
  15277. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15278. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15279. + * GNU General Public License for more details.
  15280. + *
  15281. + * You should have received a copy of the GNU General Public License
  15282. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15283. + */
  15284. +
  15285. +/*
  15286. + * fs context, aka new mount api
  15287. + */
  15288. +
  15289. +#include <linux/fs_context.h>
  15290. +#include "aufs.h"
  15291. +
  15292. +struct au_fsctx_opts {
  15293. + aufs_bindex_t bindex;
  15294. + unsigned char skipped;
  15295. + struct au_opt *opt, *opt_tail;
  15296. + struct super_block *sb;
  15297. + struct au_sbinfo *sbinfo;
  15298. + struct au_opts opts;
  15299. +};
  15300. +
  15301. +/* stop extra interpretation of errno in mount(8), and strange error messages */
  15302. +static int cvt_err(int err)
  15303. +{
  15304. + AuTraceErr(err);
  15305. +
  15306. + switch (err) {
  15307. + case -ENOENT:
  15308. + case -ENOTDIR:
  15309. + case -EEXIST:
  15310. + case -EIO:
  15311. + err = -EINVAL;
  15312. + }
  15313. + return err;
  15314. +}
  15315. +
  15316. +static int au_fsctx_reconfigure(struct fs_context *fc)
  15317. +{
  15318. + int err, do_dx;
  15319. + unsigned int mntflags;
  15320. + struct dentry *root;
  15321. + struct super_block *sb;
  15322. + struct inode *inode;
  15323. + struct au_fsctx_opts *a = fc->fs_private;
  15324. +
  15325. + AuDbg("fc %p\n", fc);
  15326. +
  15327. + root = fc->root;
  15328. + sb = root->d_sb;
  15329. + err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  15330. + if (!err) {
  15331. + di_write_lock_child(root);
  15332. + err = au_opts_verify(sb, fc->sb_flags, /*pending*/0);
  15333. + aufs_write_unlock(root);
  15334. + }
  15335. +
  15336. + inode = d_inode(root);
  15337. + inode_lock(inode);
  15338. + err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  15339. + if (unlikely(err))
  15340. + goto out;
  15341. + di_write_lock_child(root);
  15342. +
  15343. + /* au_opts_remount() may return an error */
  15344. + err = au_opts_remount(sb, &a->opts);
  15345. +
  15346. + if (au_ftest_opts(a->opts.flags, REFRESH))
  15347. + au_remount_refresh(sb, au_ftest_opts(a->opts.flags,
  15348. + REFRESH_IDOP));
  15349. +
  15350. + if (au_ftest_opts(a->opts.flags, REFRESH_DYAOP)) {
  15351. + mntflags = au_mntflags(sb);
  15352. + do_dx = !!au_opt_test(mntflags, DIO);
  15353. + au_dy_arefresh(do_dx);
  15354. + }
  15355. +
  15356. + au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
  15357. + aufs_write_unlock(root);
  15358. +
  15359. +out:
  15360. + inode_unlock(inode);
  15361. + err = cvt_err(err);
  15362. + AuTraceErr(err);
  15363. +
  15364. + return err;
  15365. +}
  15366. +
  15367. +/* ---------------------------------------------------------------------- */
  15368. +
  15369. +static int au_fsctx_fill_super(struct super_block *sb, struct fs_context *fc)
  15370. +{
  15371. + int err;
  15372. + struct au_fsctx_opts *a = fc->fs_private;
  15373. + struct au_sbinfo *sbinfo = a->sbinfo;
  15374. + struct dentry *root;
  15375. + struct inode *inode;
  15376. +
  15377. + sbinfo->si_sb = sb;
  15378. + sb->s_fs_info = sbinfo;
  15379. + kobject_get(&sbinfo->si_kobj);
  15380. +
  15381. + __si_write_lock(sb);
  15382. + si_pid_set(sb);
  15383. + au_sbilist_add(sb);
  15384. +
  15385. + /* all timestamps always follow the ones on the branch */
  15386. + sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
  15387. + sb->s_flags |= SB_I_VERSION; /* do we really need this? */
  15388. + sb->s_op = &aufs_sop;
  15389. + sb->s_d_op = &aufs_dop;
  15390. + sb->s_magic = AUFS_SUPER_MAGIC;
  15391. + sb->s_maxbytes = 0;
  15392. + sb->s_stack_depth = 1;
  15393. + au_export_init(sb);
  15394. + au_xattr_init(sb);
  15395. +
  15396. + err = au_alloc_root(sb);
  15397. + if (unlikely(err)) {
  15398. + si_write_unlock(sb);
  15399. + goto out;
  15400. + }
  15401. + root = sb->s_root;
  15402. + inode = d_inode(root);
  15403. + ii_write_lock_parent(inode);
  15404. + aufs_write_unlock(root);
  15405. +
  15406. + /* lock vfs_inode first, then aufs. */
  15407. + inode_lock(inode);
  15408. + aufs_write_lock(root);
  15409. + err = au_opts_mount(sb, &a->opts);
  15410. + AuTraceErr(err);
  15411. + if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
  15412. + sb->s_d_op = &aufs_dop_noreval;
  15413. + /* infofc(fc, "%ps", sb->s_d_op); */
  15414. + pr_info("%ps\n", sb->s_d_op);
  15415. + au_refresh_dop(root, /*force_reval*/0);
  15416. + sbinfo->si_iop_array = aufs_iop_nogetattr;
  15417. + au_refresh_iop(inode, /*force_getattr*/0);
  15418. + }
  15419. + aufs_write_unlock(root);
  15420. + inode_unlock(inode);
  15421. + if (!err)
  15422. + goto out; /* success */
  15423. +
  15424. + dput(root);
  15425. + sb->s_root = NULL;
  15426. +
  15427. +out:
  15428. + if (unlikely(err))
  15429. + kobject_put(&sbinfo->si_kobj);
  15430. + AuTraceErr(err);
  15431. + err = cvt_err(err);
  15432. + AuTraceErr(err);
  15433. + return err;
  15434. +}
  15435. +
  15436. +static int au_fsctx_get_tree(struct fs_context *fc)
  15437. +{
  15438. + int err;
  15439. +
  15440. + AuDbg("fc %p\n", fc);
  15441. + err = get_tree_nodev(fc, au_fsctx_fill_super);
  15442. +
  15443. + AuTraceErr(err);
  15444. + return err;
  15445. +}
  15446. +
  15447. +/* ---------------------------------------------------------------------- */
  15448. +
  15449. +static void au_fsctx_dump(struct au_opts *opts)
  15450. +{
  15451. +#ifdef CONFIG_AUFS_DEBUG
  15452. + /* reduce stack space */
  15453. + union {
  15454. + struct au_opt_add *add;
  15455. + struct au_opt_del *del;
  15456. + struct au_opt_mod *mod;
  15457. + struct au_opt_xino *xino;
  15458. + struct au_opt_xino_itrunc *xino_itrunc;
  15459. + struct au_opt_wbr_create *create;
  15460. + } u;
  15461. + struct au_opt *opt;
  15462. +
  15463. + opt = opts->opt;
  15464. + while (opt->type != Opt_tail) {
  15465. + switch (opt->type) {
  15466. + case Opt_add:
  15467. + u.add = &opt->add;
  15468. + AuDbg("add {b%d, %s, 0x%x, %p}\n",
  15469. + u.add->bindex, u.add->pathname, u.add->perm,
  15470. + u.add->path.dentry);
  15471. + break;
  15472. + case Opt_del:
  15473. + fallthrough;
  15474. + case Opt_idel:
  15475. + u.del = &opt->del;
  15476. + AuDbg("del {%s, %p}\n",
  15477. + u.del->pathname, u.del->h_path.dentry);
  15478. + break;
  15479. + case Opt_mod:
  15480. + fallthrough;
  15481. + case Opt_imod:
  15482. + u.mod = &opt->mod;
  15483. + AuDbg("mod {%s, 0x%x, %p}\n",
  15484. + u.mod->path, u.mod->perm, u.mod->h_root);
  15485. + break;
  15486. + case Opt_append:
  15487. + u.add = &opt->add;
  15488. + AuDbg("append {b%d, %s, 0x%x, %p}\n",
  15489. + u.add->bindex, u.add->pathname, u.add->perm,
  15490. + u.add->path.dentry);
  15491. + break;
  15492. + case Opt_prepend:
  15493. + u.add = &opt->add;
  15494. + AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
  15495. + u.add->bindex, u.add->pathname, u.add->perm,
  15496. + u.add->path.dentry);
  15497. + break;
  15498. +
  15499. + case Opt_dirwh:
  15500. + AuDbg("dirwh %d\n", opt->dirwh);
  15501. + break;
  15502. + case Opt_rdcache:
  15503. + AuDbg("rdcache %d\n", opt->rdcache);
  15504. + break;
  15505. + case Opt_rdblk:
  15506. + AuDbg("rdblk %d\n", opt->rdblk);
  15507. + break;
  15508. + case Opt_rdhash:
  15509. + AuDbg("rdhash %u\n", opt->rdhash);
  15510. + break;
  15511. +
  15512. + case Opt_xino:
  15513. + u.xino = &opt->xino;
  15514. + AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
  15515. + break;
  15516. +
  15517. +#define au_fsctx_TF(name) \
  15518. + case Opt_##name: \
  15519. + if (opt->tf) \
  15520. + AuLabel(name); \
  15521. + else \
  15522. + AuLabel(no##name); \
  15523. + break;
  15524. +
  15525. + /* simple true/false flag */
  15526. + au_fsctx_TF(trunc_xino);
  15527. + au_fsctx_TF(trunc_xib);
  15528. + au_fsctx_TF(dirperm1);
  15529. + au_fsctx_TF(plink);
  15530. + au_fsctx_TF(shwh);
  15531. + au_fsctx_TF(dio);
  15532. + au_fsctx_TF(warn_perm);
  15533. + au_fsctx_TF(verbose);
  15534. + au_fsctx_TF(sum);
  15535. + au_fsctx_TF(dirren);
  15536. + au_fsctx_TF(acl);
  15537. +#undef au_fsctx_TF
  15538. +
  15539. + case Opt_trunc_xino_path:
  15540. + fallthrough;
  15541. + case Opt_itrunc_xino:
  15542. + u.xino_itrunc = &opt->xino_itrunc;
  15543. + AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
  15544. + break;
  15545. + case Opt_noxino:
  15546. + AuLabel(noxino);
  15547. + break;
  15548. +
  15549. + case Opt_list_plink:
  15550. + AuLabel(list_plink);
  15551. + break;
  15552. + case Opt_udba:
  15553. + AuDbg("udba %d, %s\n",
  15554. + opt->udba, au_optstr_udba(opt->udba));
  15555. + break;
  15556. + case Opt_diropq_a:
  15557. + AuLabel(diropq_a);
  15558. + break;
  15559. + case Opt_diropq_w:
  15560. + AuLabel(diropq_w);
  15561. + break;
  15562. + case Opt_wsum:
  15563. + AuLabel(wsum);
  15564. + break;
  15565. + case Opt_wbr_create:
  15566. + u.create = &opt->wbr_create;
  15567. + AuDbg("create %d, %s\n", u.create->wbr_create,
  15568. + au_optstr_wbr_create(u.create->wbr_create));
  15569. + switch (u.create->wbr_create) {
  15570. + case AuWbrCreate_MFSV:
  15571. + fallthrough;
  15572. + case AuWbrCreate_PMFSV:
  15573. + AuDbg("%d sec\n", u.create->mfs_second);
  15574. + break;
  15575. + case AuWbrCreate_MFSRR:
  15576. + fallthrough;
  15577. + case AuWbrCreate_TDMFS:
  15578. + AuDbg("%llu watermark\n",
  15579. + u.create->mfsrr_watermark);
  15580. + break;
  15581. + case AuWbrCreate_MFSRRV:
  15582. + fallthrough;
  15583. + case AuWbrCreate_TDMFSV:
  15584. + fallthrough;
  15585. + case AuWbrCreate_PMFSRRV:
  15586. + AuDbg("%llu watermark, %d sec\n",
  15587. + u.create->mfsrr_watermark,
  15588. + u.create->mfs_second);
  15589. + break;
  15590. + }
  15591. + break;
  15592. + case Opt_wbr_copyup:
  15593. + AuDbg("copyup %d, %s\n", opt->wbr_copyup,
  15594. + au_optstr_wbr_copyup(opt->wbr_copyup));
  15595. + break;
  15596. + case Opt_fhsm_sec:
  15597. + AuDbg("fhsm_sec %u\n", opt->fhsm_second);
  15598. + break;
  15599. +
  15600. + default:
  15601. + AuDbg("type %d\n", opt->type);
  15602. + BUG();
  15603. + }
  15604. + opt++;
  15605. + }
  15606. +#endif
  15607. +}
  15608. +
  15609. +/* ---------------------------------------------------------------------- */
  15610. +
  15611. +/*
  15612. + * For conditionally compiled mount options.
  15613. + * Instead of fsparam_flag_no(), use this macro to distinguish ignore_silent.
  15614. + */
  15615. +#define au_ignore_flag(name, action) \
  15616. + fsparam_flag(name, action), \
  15617. + fsparam_flag("no" name, Opt_ignore_silent)
  15618. +
  15619. +const struct fs_parameter_spec aufs_fsctx_paramspec[] = {
  15620. + fsparam_string("br", Opt_br),
  15621. +
  15622. + /* "add=%d:%s" or "ins=%d:%s" */
  15623. + fsparam_string("add", Opt_add),
  15624. + fsparam_string("ins", Opt_add),
  15625. + fsparam_path("append", Opt_append),
  15626. + fsparam_path("prepend", Opt_prepend),
  15627. +
  15628. + fsparam_path("del", Opt_del),
  15629. + /* fsparam_s32("idel", Opt_idel), */
  15630. + fsparam_path("mod", Opt_mod),
  15631. + /* fsparam_string("imod", Opt_imod), */
  15632. +
  15633. + fsparam_s32("dirwh", Opt_dirwh),
  15634. +
  15635. + fsparam_path("xino", Opt_xino),
  15636. + fsparam_flag("noxino", Opt_noxino),
  15637. + fsparam_flag_no("trunc_xino", Opt_trunc_xino),
  15638. + /* "trunc_xino_v=%d:%d" */
  15639. + /* fsparam_string("trunc_xino_v", Opt_trunc_xino_v), */
  15640. + fsparam_path("trunc_xino", Opt_trunc_xino_path),
  15641. + fsparam_s32("itrunc_xino", Opt_itrunc_xino),
  15642. + /* fsparam_path("zxino", Opt_zxino), */
  15643. + fsparam_flag_no("trunc_xib", Opt_trunc_xib),
  15644. +
  15645. +#ifdef CONFIG_PROC_FS
  15646. + fsparam_flag_no("plink", Opt_plink),
  15647. +#else
  15648. + au_ignore_flag("plink", Opt_ignore),
  15649. +#endif
  15650. +
  15651. +#ifdef CONFIG_AUFS_DEBUG
  15652. + fsparam_flag("list_plink", Opt_list_plink),
  15653. +#endif
  15654. +
  15655. + fsparam_string("udba", Opt_udba),
  15656. +
  15657. + fsparam_flag_no("dio", Opt_dio),
  15658. +
  15659. +#ifdef CONFIG_AUFS_DIRREN
  15660. + fsparam_flag_no("dirren", Opt_dirren),
  15661. +#else
  15662. + au_ignore_flag("dirren", Opt_ignore),
  15663. +#endif
  15664. +
  15665. +#ifdef CONFIG_AUFS_FHSM
  15666. + fsparam_s32("fhsm_sec", Opt_fhsm_sec),
  15667. +#else
  15668. + fsparam_s32("fhsm_sec", Opt_ignore),
  15669. +#endif
  15670. +
  15671. + /* always | a | whiteouted | w */
  15672. + fsparam_string("diropq", Opt_diropq),
  15673. +
  15674. + fsparam_flag_no("warn_perm", Opt_warn_perm),
  15675. +
  15676. +#ifdef CONFIG_AUFS_SHWH
  15677. + fsparam_flag_no("shwh", Opt_shwh),
  15678. +#else
  15679. + au_ignore_flag("shwh", Opt_err),
  15680. +#endif
  15681. +
  15682. + fsparam_flag_no("dirperm1", Opt_dirperm1),
  15683. +
  15684. + fsparam_flag_no("verbose", Opt_verbose),
  15685. + fsparam_flag("v", Opt_verbose),
  15686. + fsparam_flag("quiet", Opt_noverbose),
  15687. + fsparam_flag("q", Opt_noverbose),
  15688. + /* user-space may handle this */
  15689. + fsparam_flag("silent", Opt_noverbose),
  15690. +
  15691. + fsparam_flag_no("sum", Opt_sum),
  15692. + fsparam_flag("wsum", Opt_wsum),
  15693. +
  15694. + fsparam_s32("rdcache", Opt_rdcache),
  15695. + /* "def" or s32 */
  15696. + fsparam_string("rdblk", Opt_rdblk),
  15697. + /* "def" or s32 */
  15698. + fsparam_string("rdhash", Opt_rdhash),
  15699. +
  15700. + fsparam_string("create", Opt_wbr_create),
  15701. + fsparam_string("create_policy", Opt_wbr_create),
  15702. + fsparam_string("cpup", Opt_wbr_copyup),
  15703. + fsparam_string("copyup", Opt_wbr_copyup),
  15704. + fsparam_string("copyup_policy", Opt_wbr_copyup),
  15705. +
  15706. + /* generic VFS flag */
  15707. +#ifdef CONFIG_FS_POSIX_ACL
  15708. + fsparam_flag_no("acl", Opt_acl),
  15709. +#else
  15710. + au_ignore_flag("acl", Opt_ignore),
  15711. +#endif
  15712. +
  15713. + /* internal use for the scripts */
  15714. + fsparam_string("si", Opt_ignore_silent),
  15715. +
  15716. + /* obsoleted, keep them temporary */
  15717. + fsparam_flag("nodlgt", Opt_ignore_silent),
  15718. + fsparam_flag("clean_plink", Opt_ignore),
  15719. + fsparam_string("dirs", Opt_br),
  15720. + fsparam_u32("debug", Opt_ignore),
  15721. + /* "whiteout" or "all" */
  15722. + fsparam_string("delete", Opt_ignore),
  15723. + fsparam_string("imap", Opt_ignore),
  15724. +
  15725. + /* temporary workaround, due to old mount(8)? */
  15726. + fsparam_flag("relatime", Opt_ignore_silent),
  15727. +
  15728. + {}
  15729. +};
  15730. +
  15731. +static int au_fsctx_parse_do_add(struct fs_context *fc, struct au_opt *opt,
  15732. + char *brspec, size_t speclen,
  15733. + aufs_bindex_t bindex)
  15734. +{
  15735. + int err;
  15736. + char *p;
  15737. +
  15738. + AuDbg("brspec %s\n", brspec);
  15739. +
  15740. + err = -ENOMEM;
  15741. + if (!speclen)
  15742. + speclen = strlen(brspec);
  15743. + /* will be freed by au_fsctx_free() */
  15744. + p = kmemdup_nul(brspec, speclen, GFP_NOFS);
  15745. + if (unlikely(!p)) {
  15746. + errorfc(fc, "failed in %s", brspec);
  15747. + goto out;
  15748. + }
  15749. + err = au_opt_add(opt, p, fc->sb_flags, bindex);
  15750. +
  15751. +out:
  15752. + AuTraceErr(err);
  15753. + return err;
  15754. +}
  15755. +
  15756. +static int au_fsctx_parse_br(struct fs_context *fc, char *brspec)
  15757. +{
  15758. + int err;
  15759. + char *p;
  15760. + struct au_fsctx_opts *a = fc->fs_private;
  15761. + struct au_opt *opt = a->opt;
  15762. + aufs_bindex_t bindex = a->bindex;
  15763. +
  15764. + AuDbg("brspec %s\n", brspec);
  15765. +
  15766. + err = -EINVAL;
  15767. + while ((p = strsep(&brspec, ":")) && *p) {
  15768. + err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, bindex);
  15769. + AuTraceErr(err);
  15770. + if (unlikely(err))
  15771. + break;
  15772. + bindex++;
  15773. + opt++;
  15774. + if (unlikely(opt > a->opt_tail)) {
  15775. + err = -E2BIG;
  15776. + bindex--;
  15777. + opt--;
  15778. + break;
  15779. + }
  15780. + opt->type = Opt_tail;
  15781. + a->skipped = 1;
  15782. + }
  15783. + a->bindex = bindex;
  15784. + a->opt = opt;
  15785. +
  15786. + AuTraceErr(err);
  15787. + return err;
  15788. +}
  15789. +
  15790. +static int au_fsctx_parse_add(struct fs_context *fc, char *addspec)
  15791. +{
  15792. + int err, n;
  15793. + char *p;
  15794. + struct au_fsctx_opts *a = fc->fs_private;
  15795. + struct au_opt *opt = a->opt;
  15796. +
  15797. + err = -EINVAL;
  15798. + p = strchr(addspec, ':');
  15799. + if (unlikely(!p)) {
  15800. + errorfc(fc, "bad arg in %s", addspec);
  15801. + goto out;
  15802. + }
  15803. + *p++ = '\0';
  15804. + err = kstrtoint(addspec, 0, &n);
  15805. + if (unlikely(err)) {
  15806. + errorfc(fc, "bad integer in %s", addspec);
  15807. + goto out;
  15808. + }
  15809. + AuDbg("n %d\n", n);
  15810. + err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, n);
  15811. +
  15812. +out:
  15813. + AuTraceErr(err);
  15814. + return err;
  15815. +}
  15816. +
  15817. +static int au_fsctx_parse_del(struct fs_context *fc, struct au_opt_del *del,
  15818. + struct fs_parameter *param)
  15819. +{
  15820. + int err;
  15821. +
  15822. + err = -ENOMEM;
  15823. + /* will be freed by au_fsctx_free() */
  15824. + del->pathname = kmemdup_nul(param->string, param->size, GFP_NOFS);
  15825. + if (unlikely(!del->pathname))
  15826. + goto out;
  15827. + AuDbg("del %s\n", del->pathname);
  15828. + err = vfsub_kern_path(del->pathname, AuOpt_LkupDirFlags, &del->h_path);
  15829. + if (unlikely(err))
  15830. + errorfc(fc, "lookup failed %s (%d)", del->pathname, err);
  15831. +
  15832. +out:
  15833. + AuTraceErr(err);
  15834. + return err;
  15835. +}
  15836. +
  15837. +#if 0 /* reserved for future use */
  15838. +static int au_fsctx_parse_idel(struct fs_context *fc, struct au_opt_del *del,
  15839. + aufs_bindex_t bindex)
  15840. +{
  15841. + int err;
  15842. + struct super_block *sb;
  15843. + struct dentry *root;
  15844. + struct au_fsctx_opts *a = fc->fs_private;
  15845. +
  15846. + sb = a->sb;
  15847. + AuDebugOn(!sb);
  15848. +
  15849. + err = -EINVAL;
  15850. + root = sb->s_root;
  15851. + aufs_read_lock(root, AuLock_FLUSH);
  15852. + if (bindex < 0 || au_sbbot(sb) < bindex) {
  15853. + errorfc(fc, "out of bounds, %d", bindex);
  15854. + goto out;
  15855. + }
  15856. +
  15857. + err = 0;
  15858. + del->h_path.dentry = dget(au_h_dptr(root, bindex));
  15859. + del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
  15860. +
  15861. +out:
  15862. + aufs_read_unlock(root, !AuLock_IR);
  15863. + AuTraceErr(err);
  15864. + return err;
  15865. +}
  15866. +#endif
  15867. +
  15868. +static int au_fsctx_parse_mod(struct fs_context *fc, struct au_opt_mod *mod,
  15869. + struct fs_parameter *param)
  15870. +{
  15871. + int err;
  15872. + struct path path;
  15873. + char *p;
  15874. +
  15875. + err = -ENOMEM;
  15876. + /* will be freed by au_fsctx_free() */
  15877. + mod->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
  15878. + if (unlikely(!mod->path))
  15879. + goto out;
  15880. +
  15881. + err = -EINVAL;
  15882. + p = strchr(mod->path, '=');
  15883. + if (unlikely(!p)) {
  15884. + errorfc(fc, "no permission %s", mod->path);
  15885. + goto out;
  15886. + }
  15887. +
  15888. + *p++ = 0;
  15889. + err = vfsub_kern_path(mod->path, AuOpt_LkupDirFlags, &path);
  15890. + if (unlikely(err)) {
  15891. + errorfc(fc, "lookup failed %s (%d)", mod->path, err);
  15892. + goto out;
  15893. + }
  15894. +
  15895. + mod->perm = au_br_perm_val(p);
  15896. + AuDbg("mod path %s, perm 0x%x, %s", mod->path, mod->perm, p);
  15897. + mod->h_root = dget(path.dentry);
  15898. + path_put(&path);
  15899. +
  15900. +out:
  15901. + AuTraceErr(err);
  15902. + return err;
  15903. +}
  15904. +
  15905. +#if 0 /* reserved for future use */
  15906. +static int au_fsctx_parse_imod(struct fs_context *fc, struct au_opt_mod *mod,
  15907. + char *ibrspec)
  15908. +{
  15909. + int err, n;
  15910. + char *p;
  15911. + struct super_block *sb;
  15912. + struct dentry *root;
  15913. + struct au_fsctx_opts *a = fc->fs_private;
  15914. +
  15915. + sb = a->sb;
  15916. + AuDebugOn(!sb);
  15917. +
  15918. + err = -EINVAL;
  15919. + p = strchr(ibrspec, ':');
  15920. + if (unlikely(!p)) {
  15921. + errorfc(fc, "no index, %s", ibrspec);
  15922. + goto out;
  15923. + }
  15924. + *p++ = '\0';
  15925. + err = kstrtoint(ibrspec, 0, &n);
  15926. + if (unlikely(err)) {
  15927. + errorfc(fc, "bad integer in %s", ibrspec);
  15928. + goto out;
  15929. + }
  15930. + AuDbg("n %d\n", n);
  15931. +
  15932. + root = sb->s_root;
  15933. + aufs_read_lock(root, AuLock_FLUSH);
  15934. + if (n < 0 || au_sbbot(sb) < n) {
  15935. + errorfc(fc, "out of bounds, %d", bindex);
  15936. + goto out_root;
  15937. + }
  15938. +
  15939. + err = 0;
  15940. + mod->perm = au_br_perm_val(p);
  15941. + AuDbg("mod path %s, perm 0x%x, %s\n",
  15942. + mod->path, mod->perm, p);
  15943. + mod->h_root = dget(au_h_dptr(root, bindex));
  15944. +
  15945. +out_root:
  15946. + aufs_read_unlock(root, !AuLock_IR);
  15947. +out:
  15948. + AuTraceErr(err);
  15949. + return err;
  15950. +}
  15951. +#endif
  15952. +
  15953. +static int au_fsctx_parse_xino(struct fs_context *fc,
  15954. + struct au_opt_xino *xino,
  15955. + struct fs_parameter *param)
  15956. +{
  15957. + int err;
  15958. + struct au_fsctx_opts *a = fc->fs_private;
  15959. +
  15960. + err = -ENOMEM;
  15961. + /* will be freed by au_opts_free() */
  15962. + xino->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
  15963. + if (unlikely(!xino->path))
  15964. + goto out;
  15965. + AuDbg("path %s\n", xino->path);
  15966. +
  15967. + xino->file = au_xino_create(a->sb, xino->path, /*silent*/0,
  15968. + /*wbrtop*/0);
  15969. + err = PTR_ERR(xino->file);
  15970. + if (IS_ERR(xino->file)) {
  15971. + xino->file = NULL;
  15972. + goto out;
  15973. + }
  15974. +
  15975. + err = 0;
  15976. + if (unlikely(a->sb && xino->file->f_path.dentry->d_sb == a->sb)) {
  15977. + err = -EINVAL;
  15978. + errorfc(fc, "%s must be outside", xino->path);
  15979. + }
  15980. +
  15981. +out:
  15982. + AuTraceErr(err);
  15983. + return err;
  15984. +}
  15985. +
  15986. +static
  15987. +int au_fsctx_parse_xino_itrunc_path(struct fs_context *fc,
  15988. + struct au_opt_xino_itrunc *xino_itrunc,
  15989. + char *pathname)
  15990. +{
  15991. + int err;
  15992. + aufs_bindex_t bbot, bindex;
  15993. + struct path path;
  15994. + struct dentry *root;
  15995. + struct au_fsctx_opts *a = fc->fs_private;
  15996. +
  15997. + AuDebugOn(!a->sb);
  15998. +
  15999. + err = vfsub_kern_path(pathname, AuOpt_LkupDirFlags, &path);
  16000. + if (unlikely(err)) {
  16001. + errorfc(fc, "lookup failed %s (%d)", pathname, err);
  16002. + goto out;
  16003. + }
  16004. +
  16005. + xino_itrunc->bindex = -1;
  16006. + root = a->sb->s_root;
  16007. + aufs_read_lock(root, AuLock_FLUSH);
  16008. + bbot = au_sbbot(a->sb);
  16009. + for (bindex = 0; bindex <= bbot; bindex++) {
  16010. + if (au_h_dptr(root, bindex) == path.dentry) {
  16011. + xino_itrunc->bindex = bindex;
  16012. + break;
  16013. + }
  16014. + }
  16015. + aufs_read_unlock(root, !AuLock_IR);
  16016. + path_put(&path);
  16017. +
  16018. + if (unlikely(xino_itrunc->bindex < 0)) {
  16019. + err = -EINVAL;
  16020. + errorfc(fc, "no such branch %s", pathname);
  16021. + }
  16022. +
  16023. +out:
  16024. + AuTraceErr(err);
  16025. + return err;
  16026. +}
  16027. +
  16028. +static int au_fsctx_parse_xino_itrunc(struct fs_context *fc,
  16029. + struct au_opt_xino_itrunc *xino_itrunc,
  16030. + unsigned int bindex)
  16031. +{
  16032. + int err;
  16033. + aufs_bindex_t bbot;
  16034. + struct super_block *sb;
  16035. + struct au_fsctx_opts *a = fc->fs_private;
  16036. +
  16037. + sb = a->sb;
  16038. + AuDebugOn(!sb);
  16039. +
  16040. + err = 0;
  16041. + si_noflush_read_lock(sb);
  16042. + bbot = au_sbbot(sb);
  16043. + si_read_unlock(sb);
  16044. + if (bindex <= bbot)
  16045. + xino_itrunc->bindex = bindex;
  16046. + else {
  16047. + err = -EINVAL;
  16048. + errorfc(fc, "out of bounds, %u", bindex);
  16049. + }
  16050. +
  16051. + AuTraceErr(err);
  16052. + return err;
  16053. +}
  16054. +
  16055. +static int au_fsctx_parse_param(struct fs_context *fc, struct fs_parameter *param)
  16056. +{
  16057. + int err, token;
  16058. + struct fs_parse_result result;
  16059. + struct au_fsctx_opts *a = fc->fs_private;
  16060. + struct au_opt *opt = a->opt;
  16061. +
  16062. + AuDbg("fc %p, param {key %s, string %s}\n",
  16063. + fc, param->key, param->string);
  16064. + err = fs_parse(fc, aufs_fsctx_paramspec, param, &result);
  16065. + if (unlikely(err < 0))
  16066. + goto out;
  16067. + token = err;
  16068. + AuDbg("token %d, res{negated %d, uint64 %llu}\n",
  16069. + token, result.negated, result.uint_64);
  16070. +
  16071. + err = -EINVAL;
  16072. + a->skipped = 0;
  16073. + switch (token) {
  16074. + case Opt_br:
  16075. + err = au_fsctx_parse_br(fc, param->string);
  16076. + break;
  16077. + case Opt_add:
  16078. + err = au_fsctx_parse_add(fc, param->string);
  16079. + break;
  16080. + case Opt_append:
  16081. + err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
  16082. + /*dummy bindex*/1);
  16083. + break;
  16084. + case Opt_prepend:
  16085. + err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
  16086. + /*bindex*/0);
  16087. + break;
  16088. +
  16089. + case Opt_del:
  16090. + err = au_fsctx_parse_del(fc, &opt->del, param);
  16091. + break;
  16092. +#if 0 /* reserved for future use */
  16093. + case Opt_idel:
  16094. + if (!a->sb) {
  16095. + err = 0;
  16096. + a->skipped = 1;
  16097. + break;
  16098. + }
  16099. + del->pathname = "(indexed)";
  16100. + err = au_opts_parse_idel(fc, &opt->del, result.uint_32);
  16101. + break;
  16102. +#endif
  16103. +
  16104. + case Opt_mod:
  16105. + err = au_fsctx_parse_mod(fc, &opt->mod, param);
  16106. + break;
  16107. +#ifdef IMOD /* reserved for future use */
  16108. + case Opt_imod:
  16109. + if (!a->sb) {
  16110. + err = 0;
  16111. + a->skipped = 1;
  16112. + break;
  16113. + }
  16114. + u.mod->path = "(indexed)";
  16115. + err = au_opts_parse_imod(fc, &opt->mod, param->string);
  16116. + break;
  16117. +#endif
  16118. +
  16119. + case Opt_xino:
  16120. + err = au_fsctx_parse_xino(fc, &opt->xino, param);
  16121. + break;
  16122. + case Opt_trunc_xino_path:
  16123. + if (!a->sb) {
  16124. + errorfc(fc, "no such branch %s", param->string);
  16125. + break;
  16126. + }
  16127. + err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
  16128. + param->string);
  16129. + break;
  16130. +#if 0
  16131. + case Opt_trunc_xino_v:
  16132. + if (!a->sb) {
  16133. + err = 0;
  16134. + a->skipped = 1;
  16135. + break;
  16136. + }
  16137. + err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
  16138. + param->string);
  16139. + break;
  16140. +#endif
  16141. + case Opt_itrunc_xino:
  16142. + if (!a->sb) {
  16143. + errorfc(fc, "out of bounds %s", param->string);
  16144. + break;
  16145. + }
  16146. + err = au_fsctx_parse_xino_itrunc(fc, &opt->xino_itrunc,
  16147. + result.int_32);
  16148. + break;
  16149. +
  16150. + case Opt_dirwh:
  16151. + err = 0;
  16152. + opt->dirwh = result.int_32;
  16153. + break;
  16154. +
  16155. + case Opt_rdcache:
  16156. + if (unlikely(result.int_32 > AUFS_RDCACHE_MAX)) {
  16157. + errorfc(fc, "rdcache must be smaller than %d",
  16158. + AUFS_RDCACHE_MAX);
  16159. + break;
  16160. + }
  16161. + err = 0;
  16162. + opt->rdcache = result.int_32;
  16163. + break;
  16164. +
  16165. + case Opt_rdblk:
  16166. + err = 0;
  16167. + opt->rdblk = AUFS_RDBLK_DEF;
  16168. + if (!strcmp(param->string, "def"))
  16169. + break;
  16170. +
  16171. + err = kstrtoint(param->string, 0, &result.int_32);
  16172. + if (unlikely(err)) {
  16173. + errorfc(fc, "bad value in %s", param->key);
  16174. + break;
  16175. + }
  16176. + err = -EINVAL;
  16177. + if (unlikely(result.int_32 < 0
  16178. + || result.int_32 > KMALLOC_MAX_SIZE)) {
  16179. + errorfc(fc, "bad value in %s", param->key);
  16180. + break;
  16181. + }
  16182. + if (unlikely(result.int_32 && result.int_32 < NAME_MAX)) {
  16183. + errorfc(fc, "rdblk must be larger than %d", NAME_MAX);
  16184. + break;
  16185. + }
  16186. + err = 0;
  16187. + opt->rdblk = result.int_32;
  16188. + break;
  16189. +
  16190. + case Opt_rdhash:
  16191. + err = 0;
  16192. + opt->rdhash = AUFS_RDHASH_DEF;
  16193. + if (!strcmp(param->string, "def"))
  16194. + break;
  16195. +
  16196. + err = kstrtoint(param->string, 0, &result.int_32);
  16197. + if (unlikely(err)) {
  16198. + errorfc(fc, "bad value in %s", param->key);
  16199. + break;
  16200. + }
  16201. + /* how about zero? */
  16202. + if (result.int_32 < 0
  16203. + || result.int_32 * sizeof(struct hlist_head)
  16204. + > KMALLOC_MAX_SIZE) {
  16205. + err = -EINVAL;
  16206. + errorfc(fc, "bad integer in %s", param->key);
  16207. + break;
  16208. + }
  16209. + opt->rdhash = result.int_32;
  16210. + break;
  16211. +
  16212. + case Opt_diropq:
  16213. + /*
  16214. + * As other options, fs/aufs/opts.c can handle these strings by
  16215. + * match_token(). But "diropq=" is deprecated now and will
  16216. + * never have other value. So simple strcmp() is enough here.
  16217. + */
  16218. + if (!strcmp(param->string, "a") ||
  16219. + !strcmp(param->string, "always")) {
  16220. + err = 0;
  16221. + opt->type = Opt_diropq_a;
  16222. + } else if (!strcmp(param->string, "w") ||
  16223. + !strcmp(param->string, "whiteouted")) {
  16224. + err = 0;
  16225. + opt->type = Opt_diropq_w;
  16226. + } else
  16227. + errorfc(fc, "unknown value %s", param->string);
  16228. + break;
  16229. +
  16230. + case Opt_udba:
  16231. + opt->udba = au_udba_val(param->string);
  16232. + if (opt->udba >= 0)
  16233. + err = 0;
  16234. + else
  16235. + errorf(fc, "wrong value, %s", param->string);
  16236. + break;
  16237. +
  16238. + case Opt_wbr_create:
  16239. + opt->wbr_create.wbr_create
  16240. + = au_wbr_create_val(param->string, &opt->wbr_create);
  16241. + if (opt->wbr_create.wbr_create >= 0)
  16242. + err = 0;
  16243. + else
  16244. + errorf(fc, "wrong value, %s", param->key);
  16245. + break;
  16246. +
  16247. + case Opt_wbr_copyup:
  16248. + opt->wbr_copyup = au_wbr_copyup_val(param->string);
  16249. + if (opt->wbr_copyup >= 0)
  16250. + err = 0;
  16251. + else
  16252. + errorfc(fc, "wrong value, %s", param->key);
  16253. + break;
  16254. +
  16255. + case Opt_fhsm_sec:
  16256. + if (unlikely(result.int_32 < 0)) {
  16257. + errorfc(fc, "bad integer in %s\n", param->key);
  16258. + break;
  16259. + }
  16260. + err = 0;
  16261. + if (sysaufs_brs)
  16262. + opt->fhsm_second = result.int_32;
  16263. + else
  16264. + warnfc(fc, "ignored %s %s", param->key, param->string);
  16265. + break;
  16266. +
  16267. + /* simple true/false flag */
  16268. +#define au_fsctx_TF(name) \
  16269. + case Opt_##name: \
  16270. + err = 0; \
  16271. + opt->tf = !result.negated; \
  16272. + break
  16273. + au_fsctx_TF(trunc_xino);
  16274. + au_fsctx_TF(trunc_xib);
  16275. + au_fsctx_TF(dirperm1);
  16276. + au_fsctx_TF(plink);
  16277. + au_fsctx_TF(shwh);
  16278. + au_fsctx_TF(dio);
  16279. + au_fsctx_TF(warn_perm);
  16280. + au_fsctx_TF(verbose);
  16281. + au_fsctx_TF(sum);
  16282. + au_fsctx_TF(dirren);
  16283. + au_fsctx_TF(acl);
  16284. +#undef au_fsctx_TF
  16285. +
  16286. + case Opt_noverbose:
  16287. + err = 0;
  16288. + opt->type = Opt_verbose;
  16289. + opt->tf = false;
  16290. + break;
  16291. +
  16292. + case Opt_noxino:
  16293. + fallthrough;
  16294. + case Opt_list_plink:
  16295. + fallthrough;
  16296. + case Opt_wsum:
  16297. + err = 0;
  16298. + break;
  16299. +
  16300. + case Opt_ignore:
  16301. + warnfc(fc, "ignored %s", param->key);
  16302. + fallthrough;
  16303. + case Opt_ignore_silent:
  16304. + a->skipped = 1;
  16305. + err = 0;
  16306. + break;
  16307. + default:
  16308. + a->skipped = 1;
  16309. + err = -ENOPARAM;
  16310. + break;
  16311. + }
  16312. + if (unlikely(err))
  16313. + goto out;
  16314. + if (a->skipped)
  16315. + goto out;
  16316. +
  16317. + switch (token) {
  16318. + case Opt_br:
  16319. + fallthrough;
  16320. + case Opt_noverbose:
  16321. + fallthrough;
  16322. + case Opt_diropq:
  16323. + break;
  16324. + default:
  16325. + opt->type = token;
  16326. + break;
  16327. + }
  16328. + opt++;
  16329. + if (unlikely(opt > a->opt_tail)) {
  16330. + err = -E2BIG;
  16331. + opt--;
  16332. + }
  16333. + opt->type = Opt_tail;
  16334. + a->opt = opt;
  16335. +
  16336. +out:
  16337. + return err;
  16338. +}
  16339. +
  16340. +/*
  16341. + * these options accept both 'name=val' and 'name:val' form.
  16342. + * some accept optional '=' in its value.
  16343. + * eg. br:/br1=rw:/br2=ro and br=/br1=rw:/br2=ro
  16344. + */
  16345. +static inline unsigned int is_colonopt(char *str)
  16346. +{
  16347. +#define do_test(name) \
  16348. + if (!strncmp(str, name ":", sizeof(name))) \
  16349. + return sizeof(name) - 1
  16350. + do_test("br");
  16351. + do_test("add");
  16352. + do_test("ins");
  16353. + do_test("append");
  16354. + do_test("prepend");
  16355. + do_test("del");
  16356. + /* do_test("idel"); */
  16357. + do_test("mod");
  16358. + /* do_test("imod"); */
  16359. +#undef do_test
  16360. +
  16361. + return 0;
  16362. +}
  16363. +
  16364. +static int au_fsctx_parse_monolithic(struct fs_context *fc, void *data)
  16365. +{
  16366. + int err;
  16367. + unsigned int u;
  16368. + char *str;
  16369. + struct au_fsctx_opts *a = fc->fs_private;
  16370. +
  16371. + str = data;
  16372. + AuDbg("str %s\n", str);
  16373. + while (str) {
  16374. + u = is_colonopt(str);
  16375. + if (u)
  16376. + str[u] = '=';
  16377. + str = strchr(str, ',');
  16378. + if (!str)
  16379. + break;
  16380. + str++;
  16381. + }
  16382. + str = data;
  16383. + AuDbg("str %s\n", str);
  16384. +
  16385. + err = generic_parse_monolithic(fc, str);
  16386. + AuTraceErr(err);
  16387. + au_fsctx_dump(&a->opts);
  16388. +
  16389. + return err;
  16390. +}
  16391. +
  16392. +/* ---------------------------------------------------------------------- */
  16393. +
  16394. +static void au_fsctx_opts_free(struct au_opts *opts)
  16395. +{
  16396. + struct au_opt *opt;
  16397. +
  16398. + opt = opts->opt;
  16399. + while (opt->type != Opt_tail) {
  16400. + switch (opt->type) {
  16401. + case Opt_add:
  16402. + fallthrough;
  16403. + case Opt_append:
  16404. + fallthrough;
  16405. + case Opt_prepend:
  16406. + kfree(opt->add.pathname);
  16407. + path_put(&opt->add.path);
  16408. + break;
  16409. + case Opt_del:
  16410. + kfree(opt->del.pathname);
  16411. + fallthrough;
  16412. + case Opt_idel:
  16413. + path_put(&opt->del.h_path);
  16414. + break;
  16415. + case Opt_mod:
  16416. + kfree(opt->mod.path);
  16417. + fallthrough;
  16418. + case Opt_imod:
  16419. + dput(opt->mod.h_root);
  16420. + break;
  16421. + case Opt_xino:
  16422. + kfree(opt->xino.path);
  16423. + fput(opt->xino.file);
  16424. + break;
  16425. + }
  16426. + opt++;
  16427. + }
  16428. +}
  16429. +
  16430. +static void au_fsctx_free(struct fs_context *fc)
  16431. +{
  16432. + struct au_fsctx_opts *a = fc->fs_private;
  16433. +
  16434. + /* fs_type=%p, root=%pD */
  16435. + AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
  16436. + fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
  16437. +
  16438. + kobject_put(&a->sbinfo->si_kobj);
  16439. + au_fsctx_opts_free(&a->opts);
  16440. + free_page((unsigned long)a->opts.opt);
  16441. + au_kfree_rcu(a);
  16442. +}
  16443. +
  16444. +static const struct fs_context_operations au_fsctx_ops = {
  16445. + .free = au_fsctx_free,
  16446. + .parse_param = au_fsctx_parse_param,
  16447. + .parse_monolithic = au_fsctx_parse_monolithic,
  16448. + .get_tree = au_fsctx_get_tree,
  16449. + .reconfigure = au_fsctx_reconfigure
  16450. + /*
  16451. + * nfs4 requires ->dup()? No.
  16452. + * I don't know what is this ->dup() for.
  16453. + */
  16454. +};
  16455. +
  16456. +int aufs_fsctx_init(struct fs_context *fc)
  16457. +{
  16458. + int err;
  16459. + struct au_fsctx_opts *a;
  16460. +
  16461. + /* fs_type=%p, root=%pD */
  16462. + AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
  16463. + fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
  16464. +
  16465. + /* they will be freed by au_fsctx_free() */
  16466. + err = -ENOMEM;
  16467. + a = kzalloc(sizeof(*a), GFP_NOFS);
  16468. + if (unlikely(!a))
  16469. + goto out;
  16470. + a->bindex = 0;
  16471. + a->opts.opt = (void *)__get_free_page(GFP_NOFS);
  16472. + if (unlikely(!a->opts.opt))
  16473. + goto out_a;
  16474. + a->opt = a->opts.opt;
  16475. + a->opt->type = Opt_tail;
  16476. + a->opts.max_opt = PAGE_SIZE / sizeof(*a->opts.opt);
  16477. + a->opt_tail = a->opt + a->opts.max_opt - 1;
  16478. + a->opts.sb_flags = fc->sb_flags;
  16479. +
  16480. + a->sb = NULL;
  16481. + if (fc->root) {
  16482. + AuDebugOn(fc->purpose != FS_CONTEXT_FOR_RECONFIGURE);
  16483. + a->opts.flags = AuOpts_REMOUNT;
  16484. + a->sb = fc->root->d_sb;
  16485. + a->sbinfo = au_sbi(a->sb);
  16486. + kobject_get(&a->sbinfo->si_kobj);
  16487. + } else {
  16488. + a->sbinfo = au_si_alloc(a->sb);
  16489. + AuDebugOn(!a->sbinfo);
  16490. + err = PTR_ERR(a->sbinfo);
  16491. + if (IS_ERR(a->sbinfo))
  16492. + goto out_opt;
  16493. + au_rw_write_unlock(&a->sbinfo->si_rwsem);
  16494. + }
  16495. +
  16496. + err = 0;
  16497. + fc->fs_private = a;
  16498. + fc->ops = &au_fsctx_ops;
  16499. + goto out; /* success */
  16500. +
  16501. +out_opt:
  16502. + free_page((unsigned long)a->opts.opt);
  16503. +out_a:
  16504. + au_kfree_rcu(a);
  16505. +out:
  16506. + AuTraceErr(err);
  16507. + return err;
  16508. +}
  16509. diff -Naur null/fs/aufs/fstype.h linux-5.15.131/fs/aufs/fstype.h
  16510. --- /dev/null
  16511. +++ linux-5.15.131/fs/aufs/fstype.h 2023-09-11 14:32:48.148722552 +0300
  16512. @@ -0,0 +1,401 @@
  16513. +/* SPDX-License-Identifier: GPL-2.0 */
  16514. +/*
  16515. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  16516. + *
  16517. + * This program, aufs is free software; you can redistribute it and/or modify
  16518. + * it under the terms of the GNU General Public License as published by
  16519. + * the Free Software Foundation; either version 2 of the License, or
  16520. + * (at your option) any later version.
  16521. + *
  16522. + * This program is distributed in the hope that it will be useful,
  16523. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16524. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16525. + * GNU General Public License for more details.
  16526. + *
  16527. + * You should have received a copy of the GNU General Public License
  16528. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16529. + */
  16530. +
  16531. +/*
  16532. + * judging filesystem type
  16533. + */
  16534. +
  16535. +#ifndef __AUFS_FSTYPE_H__
  16536. +#define __AUFS_FSTYPE_H__
  16537. +
  16538. +#ifdef __KERNEL__
  16539. +
  16540. +#include <linux/fs.h>
  16541. +#include <linux/magic.h>
  16542. +#include <linux/nfs_fs.h>
  16543. +#include <linux/romfs_fs.h>
  16544. +
  16545. +static inline int au_test_aufs(struct super_block *sb)
  16546. +{
  16547. + return sb->s_magic == AUFS_SUPER_MAGIC;
  16548. +}
  16549. +
  16550. +static inline const char *au_sbtype(struct super_block *sb)
  16551. +{
  16552. + return sb->s_type->name;
  16553. +}
  16554. +
  16555. +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
  16556. +{
  16557. +#if IS_ENABLED(CONFIG_ISO9660_FS)
  16558. + return sb->s_magic == ISOFS_SUPER_MAGIC;
  16559. +#else
  16560. + return 0;
  16561. +#endif
  16562. +}
  16563. +
  16564. +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
  16565. +{
  16566. +#if IS_ENABLED(CONFIG_ROMFS_FS)
  16567. + return sb->s_magic == ROMFS_MAGIC;
  16568. +#else
  16569. + return 0;
  16570. +#endif
  16571. +}
  16572. +
  16573. +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
  16574. +{
  16575. +#if IS_ENABLED(CONFIG_CRAMFS)
  16576. + return sb->s_magic == CRAMFS_MAGIC;
  16577. +#endif
  16578. + return 0;
  16579. +}
  16580. +
  16581. +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
  16582. +{
  16583. +#if IS_ENABLED(CONFIG_NFS_FS)
  16584. + return sb->s_magic == NFS_SUPER_MAGIC;
  16585. +#else
  16586. + return 0;
  16587. +#endif
  16588. +}
  16589. +
  16590. +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
  16591. +{
  16592. +#if IS_ENABLED(CONFIG_FUSE_FS)
  16593. + return sb->s_magic == FUSE_SUPER_MAGIC;
  16594. +#else
  16595. + return 0;
  16596. +#endif
  16597. +}
  16598. +
  16599. +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
  16600. +{
  16601. +#if IS_ENABLED(CONFIG_XFS_FS)
  16602. + return sb->s_magic == XFS_SB_MAGIC;
  16603. +#else
  16604. + return 0;
  16605. +#endif
  16606. +}
  16607. +
  16608. +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
  16609. +{
  16610. +#ifdef CONFIG_TMPFS
  16611. + return sb->s_magic == TMPFS_MAGIC;
  16612. +#else
  16613. + return 0;
  16614. +#endif
  16615. +}
  16616. +
  16617. +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
  16618. +{
  16619. +#if IS_ENABLED(CONFIG_ECRYPT_FS)
  16620. + return !strcmp(au_sbtype(sb), "ecryptfs");
  16621. +#else
  16622. + return 0;
  16623. +#endif
  16624. +}
  16625. +
  16626. +static inline int au_test_ramfs(struct super_block *sb)
  16627. +{
  16628. + return sb->s_magic == RAMFS_MAGIC;
  16629. +}
  16630. +
  16631. +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
  16632. +{
  16633. +#if IS_ENABLED(CONFIG_UBIFS_FS)
  16634. + return sb->s_magic == UBIFS_SUPER_MAGIC;
  16635. +#else
  16636. + return 0;
  16637. +#endif
  16638. +}
  16639. +
  16640. +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
  16641. +{
  16642. +#ifdef CONFIG_PROC_FS
  16643. + return sb->s_magic == PROC_SUPER_MAGIC;
  16644. +#else
  16645. + return 0;
  16646. +#endif
  16647. +}
  16648. +
  16649. +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
  16650. +{
  16651. +#ifdef CONFIG_SYSFS
  16652. + return sb->s_magic == SYSFS_MAGIC;
  16653. +#else
  16654. + return 0;
  16655. +#endif
  16656. +}
  16657. +
  16658. +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
  16659. +{
  16660. +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
  16661. + return sb->s_magic == CONFIGFS_MAGIC;
  16662. +#else
  16663. + return 0;
  16664. +#endif
  16665. +}
  16666. +
  16667. +static inline int au_test_minix(struct super_block *sb __maybe_unused)
  16668. +{
  16669. +#if IS_ENABLED(CONFIG_MINIX_FS)
  16670. + return sb->s_magic == MINIX3_SUPER_MAGIC
  16671. + || sb->s_magic == MINIX2_SUPER_MAGIC
  16672. + || sb->s_magic == MINIX2_SUPER_MAGIC2
  16673. + || sb->s_magic == MINIX_SUPER_MAGIC
  16674. + || sb->s_magic == MINIX_SUPER_MAGIC2;
  16675. +#else
  16676. + return 0;
  16677. +#endif
  16678. +}
  16679. +
  16680. +static inline int au_test_fat(struct super_block *sb __maybe_unused)
  16681. +{
  16682. +#if IS_ENABLED(CONFIG_FAT_FS)
  16683. + return sb->s_magic == MSDOS_SUPER_MAGIC;
  16684. +#else
  16685. + return 0;
  16686. +#endif
  16687. +}
  16688. +
  16689. +static inline int au_test_msdos(struct super_block *sb)
  16690. +{
  16691. + return au_test_fat(sb);
  16692. +}
  16693. +
  16694. +static inline int au_test_vfat(struct super_block *sb)
  16695. +{
  16696. + return au_test_fat(sb);
  16697. +}
  16698. +
  16699. +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
  16700. +{
  16701. +#ifdef CONFIG_SECURITYFS
  16702. + return sb->s_magic == SECURITYFS_MAGIC;
  16703. +#else
  16704. + return 0;
  16705. +#endif
  16706. +}
  16707. +
  16708. +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
  16709. +{
  16710. +#if IS_ENABLED(CONFIG_SQUASHFS)
  16711. + return sb->s_magic == SQUASHFS_MAGIC;
  16712. +#else
  16713. + return 0;
  16714. +#endif
  16715. +}
  16716. +
  16717. +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
  16718. +{
  16719. +#if IS_ENABLED(CONFIG_BTRFS_FS)
  16720. + return sb->s_magic == BTRFS_SUPER_MAGIC;
  16721. +#else
  16722. + return 0;
  16723. +#endif
  16724. +}
  16725. +
  16726. +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
  16727. +{
  16728. +#if IS_ENABLED(CONFIG_XENFS)
  16729. + return sb->s_magic == XENFS_SUPER_MAGIC;
  16730. +#else
  16731. + return 0;
  16732. +#endif
  16733. +}
  16734. +
  16735. +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
  16736. +{
  16737. +#ifdef CONFIG_DEBUG_FS
  16738. + return sb->s_magic == DEBUGFS_MAGIC;
  16739. +#else
  16740. + return 0;
  16741. +#endif
  16742. +}
  16743. +
  16744. +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
  16745. +{
  16746. +#if IS_ENABLED(CONFIG_NILFS)
  16747. + return sb->s_magic == NILFS_SUPER_MAGIC;
  16748. +#else
  16749. + return 0;
  16750. +#endif
  16751. +}
  16752. +
  16753. +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
  16754. +{
  16755. +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
  16756. + return sb->s_magic == HFSPLUS_SUPER_MAGIC;
  16757. +#else
  16758. + return 0;
  16759. +#endif
  16760. +}
  16761. +
  16762. +/* ---------------------------------------------------------------------- */
  16763. +/*
  16764. + * they can't be an aufs branch.
  16765. + */
  16766. +static inline int au_test_fs_unsuppoted(struct super_block *sb)
  16767. +{
  16768. + return
  16769. +#ifndef CONFIG_AUFS_BR_RAMFS
  16770. + au_test_ramfs(sb) ||
  16771. +#endif
  16772. + au_test_procfs(sb)
  16773. + || au_test_sysfs(sb)
  16774. + || au_test_configfs(sb)
  16775. + || au_test_debugfs(sb)
  16776. + || au_test_securityfs(sb)
  16777. + || au_test_xenfs(sb)
  16778. + || au_test_ecryptfs(sb)
  16779. + /* || !strcmp(au_sbtype(sb), "unionfs") */
  16780. + || au_test_aufs(sb); /* will be supported in next version */
  16781. +}
  16782. +
  16783. +static inline int au_test_fs_remote(struct super_block *sb)
  16784. +{
  16785. + return !au_test_tmpfs(sb)
  16786. +#ifdef CONFIG_AUFS_BR_RAMFS
  16787. + && !au_test_ramfs(sb)
  16788. +#endif
  16789. + && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
  16790. +}
  16791. +
  16792. +/* ---------------------------------------------------------------------- */
  16793. +
  16794. +/*
  16795. + * Note: these functions (below) are created after reading ->getattr() in all
  16796. + * filesystems under linux/fs. it means we have to do so in every update...
  16797. + */
  16798. +
  16799. +/*
  16800. + * some filesystems require getattr to refresh the inode attributes before
  16801. + * referencing.
  16802. + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
  16803. + * and leave the work for d_revalidate()
  16804. + */
  16805. +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
  16806. +{
  16807. + return au_test_nfs(sb)
  16808. + || au_test_fuse(sb)
  16809. + /* || au_test_btrfs(sb) */ /* untested */
  16810. + ;
  16811. +}
  16812. +
  16813. +/*
  16814. + * filesystems which don't maintain i_size or i_blocks.
  16815. + */
  16816. +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
  16817. +{
  16818. + return au_test_xfs(sb)
  16819. + || au_test_btrfs(sb)
  16820. + || au_test_ubifs(sb)
  16821. + || au_test_hfsplus(sb) /* maintained, but incorrect */
  16822. + /* || au_test_minix(sb) */ /* untested */
  16823. + ;
  16824. +}
  16825. +
  16826. +/*
  16827. + * filesystems which don't store the correct value in some of their inode
  16828. + * attributes.
  16829. + */
  16830. +static inline int au_test_fs_bad_iattr(struct super_block *sb)
  16831. +{
  16832. + return au_test_fs_bad_iattr_size(sb)
  16833. + || au_test_fat(sb)
  16834. + || au_test_msdos(sb)
  16835. + || au_test_vfat(sb);
  16836. +}
  16837. +
  16838. +/* they don't check i_nlink in link(2) */
  16839. +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
  16840. +{
  16841. + return au_test_tmpfs(sb)
  16842. +#ifdef CONFIG_AUFS_BR_RAMFS
  16843. + || au_test_ramfs(sb)
  16844. +#endif
  16845. + || au_test_ubifs(sb)
  16846. + || au_test_hfsplus(sb);
  16847. +}
  16848. +
  16849. +/*
  16850. + * filesystems which sets S_NOATIME and S_NOCMTIME.
  16851. + */
  16852. +static inline int au_test_fs_notime(struct super_block *sb)
  16853. +{
  16854. + return au_test_nfs(sb)
  16855. + || au_test_fuse(sb)
  16856. + || au_test_ubifs(sb)
  16857. + ;
  16858. +}
  16859. +
  16860. +/* temporary support for i#1 in cramfs */
  16861. +static inline int au_test_fs_unique_ino(struct inode *inode)
  16862. +{
  16863. + if (au_test_cramfs(inode->i_sb))
  16864. + return inode->i_ino != 1;
  16865. + return 1;
  16866. +}
  16867. +
  16868. +/* ---------------------------------------------------------------------- */
  16869. +
  16870. +/*
  16871. + * the filesystem where the xino files placed must support i/o after unlink and
  16872. + * maintain i_size and i_blocks.
  16873. + */
  16874. +static inline int au_test_fs_bad_xino(struct super_block *sb)
  16875. +{
  16876. + return au_test_fs_remote(sb)
  16877. + || au_test_fs_bad_iattr_size(sb)
  16878. + /* don't want unnecessary work for xino */
  16879. + || au_test_aufs(sb)
  16880. + || au_test_ecryptfs(sb)
  16881. + || au_test_nilfs(sb);
  16882. +}
  16883. +
  16884. +static inline int au_test_fs_trunc_xino(struct super_block *sb)
  16885. +{
  16886. + return au_test_tmpfs(sb)
  16887. + || au_test_ramfs(sb);
  16888. +}
  16889. +
  16890. +/*
  16891. + * test if the @sb is real-readonly.
  16892. + */
  16893. +static inline int au_test_fs_rr(struct super_block *sb)
  16894. +{
  16895. + return au_test_squashfs(sb)
  16896. + || au_test_iso9660(sb)
  16897. + || au_test_cramfs(sb)
  16898. + || au_test_romfs(sb);
  16899. +}
  16900. +
  16901. +/*
  16902. + * test if the @inode is nfs with 'noacl' option
  16903. + * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
  16904. + */
  16905. +static inline int au_test_nfs_noacl(struct inode *inode)
  16906. +{
  16907. + return au_test_nfs(inode->i_sb)
  16908. + /* && IS_POSIXACL(inode) */
  16909. + && !nfs_server_capable(inode, NFS_CAP_ACLS);
  16910. +}
  16911. +
  16912. +#endif /* __KERNEL__ */
  16913. +#endif /* __AUFS_FSTYPE_H__ */
  16914. diff -Naur null/fs/aufs/hbl.h linux-5.15.131/fs/aufs/hbl.h
  16915. --- /dev/null
  16916. +++ linux-5.15.131/fs/aufs/hbl.h 2023-09-11 14:32:48.148722552 +0300
  16917. @@ -0,0 +1,65 @@
  16918. +/* SPDX-License-Identifier: GPL-2.0 */
  16919. +/*
  16920. + * Copyright (C) 2017-2021 Junjiro R. Okajima
  16921. + *
  16922. + * This program, aufs is free software; you can redistribute it and/or modify
  16923. + * it under the terms of the GNU General Public License as published by
  16924. + * the Free Software Foundation; either version 2 of the License, or
  16925. + * (at your option) any later version.
  16926. + *
  16927. + * This program is distributed in the hope that it will be useful,
  16928. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16929. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16930. + * GNU General Public License for more details.
  16931. + *
  16932. + * You should have received a copy of the GNU General Public License
  16933. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16934. + */
  16935. +
  16936. +/*
  16937. + * helpers for hlist_bl.h
  16938. + */
  16939. +
  16940. +#ifndef __AUFS_HBL_H__
  16941. +#define __AUFS_HBL_H__
  16942. +
  16943. +#ifdef __KERNEL__
  16944. +
  16945. +#include <linux/list_bl.h>
  16946. +
  16947. +static inline void au_hbl_add(struct hlist_bl_node *node,
  16948. + struct hlist_bl_head *hbl)
  16949. +{
  16950. + hlist_bl_lock(hbl);
  16951. + hlist_bl_add_head(node, hbl);
  16952. + hlist_bl_unlock(hbl);
  16953. +}
  16954. +
  16955. +static inline void au_hbl_del(struct hlist_bl_node *node,
  16956. + struct hlist_bl_head *hbl)
  16957. +{
  16958. + hlist_bl_lock(hbl);
  16959. + hlist_bl_del(node);
  16960. + hlist_bl_unlock(hbl);
  16961. +}
  16962. +
  16963. +#define au_hbl_for_each(pos, head) \
  16964. + for (pos = hlist_bl_first(head); \
  16965. + pos; \
  16966. + pos = pos->next)
  16967. +
  16968. +static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
  16969. +{
  16970. + unsigned long cnt;
  16971. + struct hlist_bl_node *pos;
  16972. +
  16973. + cnt = 0;
  16974. + hlist_bl_lock(hbl);
  16975. + au_hbl_for_each(pos, hbl)
  16976. + cnt++;
  16977. + hlist_bl_unlock(hbl);
  16978. + return cnt;
  16979. +}
  16980. +
  16981. +#endif /* __KERNEL__ */
  16982. +#endif /* __AUFS_HBL_H__ */
  16983. diff -Naur null/fs/aufs/hfsnotify.c linux-5.15.131/fs/aufs/hfsnotify.c
  16984. --- /dev/null
  16985. +++ linux-5.15.131/fs/aufs/hfsnotify.c 2023-09-11 14:32:48.148722552 +0300
  16986. @@ -0,0 +1,288 @@
  16987. +// SPDX-License-Identifier: GPL-2.0
  16988. +/*
  16989. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  16990. + *
  16991. + * This program, aufs is free software; you can redistribute it and/or modify
  16992. + * it under the terms of the GNU General Public License as published by
  16993. + * the Free Software Foundation; either version 2 of the License, or
  16994. + * (at your option) any later version.
  16995. + *
  16996. + * This program is distributed in the hope that it will be useful,
  16997. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16998. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16999. + * GNU General Public License for more details.
  17000. + *
  17001. + * You should have received a copy of the GNU General Public License
  17002. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17003. + */
  17004. +
  17005. +/*
  17006. + * fsnotify for the lower directories
  17007. + */
  17008. +
  17009. +#include "aufs.h"
  17010. +
  17011. +/* FS_IN_IGNORED is unnecessary */
  17012. +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
  17013. + | FS_CREATE | FS_EVENT_ON_CHILD);
  17014. +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
  17015. +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
  17016. +
  17017. +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
  17018. +{
  17019. + struct au_hnotify *hn = container_of(mark, struct au_hnotify,
  17020. + hn_mark);
  17021. + /* AuDbg("here\n"); */
  17022. + au_cache_free_hnotify(hn);
  17023. + smp_mb__before_atomic(); /* for atomic64_dec */
  17024. + if (atomic64_dec_and_test(&au_hfsn_ifree))
  17025. + wake_up(&au_hfsn_wq);
  17026. +}
  17027. +
  17028. +static int au_hfsn_alloc(struct au_hinode *hinode)
  17029. +{
  17030. + int err;
  17031. + struct au_hnotify *hn;
  17032. + struct super_block *sb;
  17033. + struct au_branch *br;
  17034. + struct fsnotify_mark *mark;
  17035. + aufs_bindex_t bindex;
  17036. +
  17037. + hn = hinode->hi_notify;
  17038. + sb = hn->hn_aufs_inode->i_sb;
  17039. + bindex = au_br_index(sb, hinode->hi_id);
  17040. + br = au_sbr(sb, bindex);
  17041. + AuDebugOn(!br->br_hfsn);
  17042. +
  17043. + mark = &hn->hn_mark;
  17044. + fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
  17045. + mark->mask = AuHfsnMask;
  17046. + /*
  17047. + * by udba rename or rmdir, aufs assign a new inode to the known
  17048. + * h_inode, so specify 1 to allow dups.
  17049. + */
  17050. + lockdep_off();
  17051. + err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
  17052. + lockdep_on();
  17053. +
  17054. + return err;
  17055. +}
  17056. +
  17057. +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
  17058. +{
  17059. + struct fsnotify_mark *mark;
  17060. + unsigned long long ull;
  17061. + struct fsnotify_group *group;
  17062. +
  17063. + ull = atomic64_inc_return(&au_hfsn_ifree);
  17064. + BUG_ON(!ull);
  17065. +
  17066. + mark = &hn->hn_mark;
  17067. + spin_lock(&mark->lock);
  17068. + group = mark->group;
  17069. + fsnotify_get_group(group);
  17070. + spin_unlock(&mark->lock);
  17071. + lockdep_off();
  17072. + fsnotify_destroy_mark(mark, group);
  17073. + fsnotify_put_mark(mark);
  17074. + fsnotify_put_group(group);
  17075. + lockdep_on();
  17076. +
  17077. + /* free hn by myself */
  17078. + return 0;
  17079. +}
  17080. +
  17081. +/* ---------------------------------------------------------------------- */
  17082. +
  17083. +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
  17084. +{
  17085. + struct fsnotify_mark *mark;
  17086. +
  17087. + mark = &hinode->hi_notify->hn_mark;
  17088. + spin_lock(&mark->lock);
  17089. + if (do_set) {
  17090. + AuDebugOn(mark->mask & AuHfsnMask);
  17091. + mark->mask |= AuHfsnMask;
  17092. + } else {
  17093. + AuDebugOn(!(mark->mask & AuHfsnMask));
  17094. + mark->mask &= ~AuHfsnMask;
  17095. + }
  17096. + spin_unlock(&mark->lock);
  17097. + /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
  17098. +}
  17099. +
  17100. +/* ---------------------------------------------------------------------- */
  17101. +
  17102. +/* #define AuDbgHnotify */
  17103. +#ifdef AuDbgHnotify
  17104. +static char *au_hfsn_name(u32 mask)
  17105. +{
  17106. +#ifdef CONFIG_AUFS_DEBUG
  17107. +#define test_ret(flag) \
  17108. + do { \
  17109. + if (mask & flag) \
  17110. + return #flag; \
  17111. + } while (0)
  17112. + test_ret(FS_ACCESS);
  17113. + test_ret(FS_MODIFY);
  17114. + test_ret(FS_ATTRIB);
  17115. + test_ret(FS_CLOSE_WRITE);
  17116. + test_ret(FS_CLOSE_NOWRITE);
  17117. + test_ret(FS_OPEN);
  17118. + test_ret(FS_MOVED_FROM);
  17119. + test_ret(FS_MOVED_TO);
  17120. + test_ret(FS_CREATE);
  17121. + test_ret(FS_DELETE);
  17122. + test_ret(FS_DELETE_SELF);
  17123. + test_ret(FS_MOVE_SELF);
  17124. + test_ret(FS_UNMOUNT);
  17125. + test_ret(FS_Q_OVERFLOW);
  17126. + test_ret(FS_IN_IGNORED);
  17127. + test_ret(FS_ISDIR);
  17128. + test_ret(FS_IN_ONESHOT);
  17129. + test_ret(FS_EVENT_ON_CHILD);
  17130. + return "";
  17131. +#undef test_ret
  17132. +#else
  17133. + return "??";
  17134. +#endif
  17135. +}
  17136. +#endif
  17137. +
  17138. +/* ---------------------------------------------------------------------- */
  17139. +
  17140. +static void au_hfsn_free_group(struct fsnotify_group *group)
  17141. +{
  17142. + struct au_br_hfsnotify *hfsn = group->private;
  17143. +
  17144. + /* AuDbg("here\n"); */
  17145. + au_kfree_try_rcu(hfsn);
  17146. +}
  17147. +
  17148. +static int au_hfsn_handle_event(struct fsnotify_group *group,
  17149. + u32 mask, const void *data, int data_type,
  17150. + struct inode *dir,
  17151. + const struct qstr *file_name, u32 cookie,
  17152. + struct fsnotify_iter_info *iter_info)
  17153. +{
  17154. + int err;
  17155. + struct au_hnotify *hnotify;
  17156. + struct inode *h_dir, *h_inode;
  17157. + struct fsnotify_mark *inode_mark;
  17158. +
  17159. + AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
  17160. +
  17161. + err = 0;
  17162. + /* if FS_UNMOUNT happens, there must be another bug */
  17163. + AuDebugOn(mask & FS_UNMOUNT);
  17164. + if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
  17165. + goto out;
  17166. +
  17167. + h_dir = dir;
  17168. + h_inode = NULL;
  17169. +#ifdef AuDbgHnotify
  17170. + au_debug_on();
  17171. + if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
  17172. + || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
  17173. + AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
  17174. + h_dir->i_ino, mask, au_hfsn_name(mask),
  17175. + AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
  17176. + /* WARN_ON(1); */
  17177. + }
  17178. + au_debug_off();
  17179. +#endif
  17180. +
  17181. + inode_mark = fsnotify_iter_inode_mark(iter_info);
  17182. + AuDebugOn(!inode_mark);
  17183. + hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
  17184. + err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
  17185. +
  17186. +out:
  17187. + return err;
  17188. +}
  17189. +
  17190. +static struct fsnotify_ops au_hfsn_ops = {
  17191. + .handle_event = au_hfsn_handle_event,
  17192. + .free_group_priv = au_hfsn_free_group,
  17193. + .free_mark = au_hfsn_free_mark
  17194. +};
  17195. +
  17196. +/* ---------------------------------------------------------------------- */
  17197. +
  17198. +static void au_hfsn_fin_br(struct au_branch *br)
  17199. +{
  17200. + struct au_br_hfsnotify *hfsn;
  17201. +
  17202. + hfsn = br->br_hfsn;
  17203. + if (hfsn) {
  17204. + lockdep_off();
  17205. + fsnotify_put_group(hfsn->hfsn_group);
  17206. + lockdep_on();
  17207. + }
  17208. +}
  17209. +
  17210. +static int au_hfsn_init_br(struct au_branch *br, int perm)
  17211. +{
  17212. + int err;
  17213. + struct fsnotify_group *group;
  17214. + struct au_br_hfsnotify *hfsn;
  17215. +
  17216. + err = 0;
  17217. + br->br_hfsn = NULL;
  17218. + if (!au_br_hnotifyable(perm))
  17219. + goto out;
  17220. +
  17221. + err = -ENOMEM;
  17222. + hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
  17223. + if (unlikely(!hfsn))
  17224. + goto out;
  17225. +
  17226. + err = 0;
  17227. + group = fsnotify_alloc_group(&au_hfsn_ops);
  17228. + if (IS_ERR(group)) {
  17229. + err = PTR_ERR(group);
  17230. + pr_err("fsnotify_alloc_group() failed, %d\n", err);
  17231. + goto out_hfsn;
  17232. + }
  17233. +
  17234. + group->private = hfsn;
  17235. + hfsn->hfsn_group = group;
  17236. + br->br_hfsn = hfsn;
  17237. + goto out; /* success */
  17238. +
  17239. +out_hfsn:
  17240. + au_kfree_try_rcu(hfsn);
  17241. +out:
  17242. + return err;
  17243. +}
  17244. +
  17245. +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
  17246. +{
  17247. + int err;
  17248. +
  17249. + err = 0;
  17250. + if (!br->br_hfsn)
  17251. + err = au_hfsn_init_br(br, perm);
  17252. +
  17253. + return err;
  17254. +}
  17255. +
  17256. +/* ---------------------------------------------------------------------- */
  17257. +
  17258. +static void au_hfsn_fin(void)
  17259. +{
  17260. + AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
  17261. + wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
  17262. +}
  17263. +
  17264. +const struct au_hnotify_op au_hnotify_op = {
  17265. + .ctl = au_hfsn_ctl,
  17266. + .alloc = au_hfsn_alloc,
  17267. + .free = au_hfsn_free,
  17268. +
  17269. + .fin = au_hfsn_fin,
  17270. +
  17271. + .reset_br = au_hfsn_reset_br,
  17272. + .fin_br = au_hfsn_fin_br,
  17273. + .init_br = au_hfsn_init_br
  17274. +};
  17275. diff -Naur null/fs/aufs/hfsplus.c linux-5.15.131/fs/aufs/hfsplus.c
  17276. --- /dev/null
  17277. +++ linux-5.15.131/fs/aufs/hfsplus.c 2023-09-11 14:32:48.148722552 +0300
  17278. @@ -0,0 +1,60 @@
  17279. +// SPDX-License-Identifier: GPL-2.0
  17280. +/*
  17281. + * Copyright (C) 2010-2021 Junjiro R. Okajima
  17282. + *
  17283. + * This program, aufs is free software; you can redistribute it and/or modify
  17284. + * it under the terms of the GNU General Public License as published by
  17285. + * the Free Software Foundation; either version 2 of the License, or
  17286. + * (at your option) any later version.
  17287. + *
  17288. + * This program is distributed in the hope that it will be useful,
  17289. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17290. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17291. + * GNU General Public License for more details.
  17292. + *
  17293. + * You should have received a copy of the GNU General Public License
  17294. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17295. + */
  17296. +
  17297. +/*
  17298. + * special support for filesystems which acquires an inode mutex
  17299. + * at final closing a file, eg, hfsplus.
  17300. + *
  17301. + * This trick is very simple and stupid, just to open the file before really
  17302. + * necessary open to tell hfsplus that this is not the final closing.
  17303. + * The caller should call au_h_open_pre() after acquiring the inode mutex,
  17304. + * and au_h_open_post() after releasing it.
  17305. + */
  17306. +
  17307. +#include "aufs.h"
  17308. +
  17309. +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
  17310. + int force_wr)
  17311. +{
  17312. + struct file *h_file;
  17313. + struct dentry *h_dentry;
  17314. +
  17315. + h_dentry = au_h_dptr(dentry, bindex);
  17316. + AuDebugOn(!h_dentry);
  17317. + AuDebugOn(d_is_negative(h_dentry));
  17318. +
  17319. + h_file = NULL;
  17320. + if (au_test_hfsplus(h_dentry->d_sb)
  17321. + && d_is_reg(h_dentry))
  17322. + h_file = au_h_open(dentry, bindex,
  17323. + O_RDONLY | O_NOATIME | O_LARGEFILE,
  17324. + /*file*/NULL, force_wr);
  17325. + return h_file;
  17326. +}
  17327. +
  17328. +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
  17329. + struct file *h_file)
  17330. +{
  17331. + struct au_branch *br;
  17332. +
  17333. + if (h_file) {
  17334. + fput(h_file);
  17335. + br = au_sbr(dentry->d_sb, bindex);
  17336. + au_lcnt_dec(&br->br_nfiles);
  17337. + }
  17338. +}
  17339. diff -Naur null/fs/aufs/hnotify.c linux-5.15.131/fs/aufs/hnotify.c
  17340. --- /dev/null
  17341. +++ linux-5.15.131/fs/aufs/hnotify.c 2023-09-11 14:32:48.149722539 +0300
  17342. @@ -0,0 +1,715 @@
  17343. +// SPDX-License-Identifier: GPL-2.0
  17344. +/*
  17345. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  17346. + *
  17347. + * This program, aufs is free software; you can redistribute it and/or modify
  17348. + * it under the terms of the GNU General Public License as published by
  17349. + * the Free Software Foundation; either version 2 of the License, or
  17350. + * (at your option) any later version.
  17351. + *
  17352. + * This program is distributed in the hope that it will be useful,
  17353. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17354. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17355. + * GNU General Public License for more details.
  17356. + *
  17357. + * You should have received a copy of the GNU General Public License
  17358. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17359. + */
  17360. +
  17361. +/*
  17362. + * abstraction to notify the direct changes on lower directories
  17363. + */
  17364. +
  17365. +/* #include <linux/iversion.h> */
  17366. +#include "aufs.h"
  17367. +
  17368. +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
  17369. +{
  17370. + int err;
  17371. + struct au_hnotify *hn;
  17372. +
  17373. + err = -ENOMEM;
  17374. + hn = au_cache_alloc_hnotify();
  17375. + if (hn) {
  17376. + hn->hn_aufs_inode = inode;
  17377. + hinode->hi_notify = hn;
  17378. + err = au_hnotify_op.alloc(hinode);
  17379. + AuTraceErr(err);
  17380. + if (unlikely(err)) {
  17381. + hinode->hi_notify = NULL;
  17382. + au_cache_free_hnotify(hn);
  17383. + /*
  17384. + * The upper dir was removed by udba, but the same named
  17385. + * dir left. In this case, aufs assigns a new inode
  17386. + * number and set the monitor again.
  17387. + * For the lower dir, the old monitor is still left.
  17388. + */
  17389. + if (err == -EEXIST)
  17390. + err = 0;
  17391. + }
  17392. + }
  17393. +
  17394. + AuTraceErr(err);
  17395. + return err;
  17396. +}
  17397. +
  17398. +void au_hn_free(struct au_hinode *hinode)
  17399. +{
  17400. + struct au_hnotify *hn;
  17401. +
  17402. + hn = hinode->hi_notify;
  17403. + if (hn) {
  17404. + hinode->hi_notify = NULL;
  17405. + if (au_hnotify_op.free(hinode, hn))
  17406. + au_cache_free_hnotify(hn);
  17407. + }
  17408. +}
  17409. +
  17410. +/* ---------------------------------------------------------------------- */
  17411. +
  17412. +void au_hn_ctl(struct au_hinode *hinode, int do_set)
  17413. +{
  17414. + if (hinode->hi_notify)
  17415. + au_hnotify_op.ctl(hinode, do_set);
  17416. +}
  17417. +
  17418. +void au_hn_reset(struct inode *inode, unsigned int flags)
  17419. +{
  17420. + aufs_bindex_t bindex, bbot;
  17421. + struct inode *hi;
  17422. + struct dentry *iwhdentry;
  17423. +
  17424. + bbot = au_ibbot(inode);
  17425. + for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
  17426. + hi = au_h_iptr(inode, bindex);
  17427. + if (!hi)
  17428. + continue;
  17429. +
  17430. + /* inode_lock_nested(hi, AuLsc_I_CHILD); */
  17431. + iwhdentry = au_hi_wh(inode, bindex);
  17432. + if (iwhdentry)
  17433. + dget(iwhdentry);
  17434. + au_igrab(hi);
  17435. + au_set_h_iptr(inode, bindex, NULL, 0);
  17436. + au_set_h_iptr(inode, bindex, au_igrab(hi),
  17437. + flags & ~AuHi_XINO);
  17438. + iput(hi);
  17439. + dput(iwhdentry);
  17440. + /* inode_unlock(hi); */
  17441. + }
  17442. +}
  17443. +
  17444. +/* ---------------------------------------------------------------------- */
  17445. +
  17446. +static int hn_xino(struct inode *inode, struct inode *h_inode)
  17447. +{
  17448. + int err;
  17449. + aufs_bindex_t bindex, bbot, bfound, btop;
  17450. + struct inode *h_i;
  17451. +
  17452. + err = 0;
  17453. + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
  17454. + pr_warn("branch root dir was changed\n");
  17455. + goto out;
  17456. + }
  17457. +
  17458. + bfound = -1;
  17459. + bbot = au_ibbot(inode);
  17460. + btop = au_ibtop(inode);
  17461. +#if 0 /* reserved for future use */
  17462. + if (bindex == bbot) {
  17463. + /* keep this ino in rename case */
  17464. + goto out;
  17465. + }
  17466. +#endif
  17467. + for (bindex = btop; bindex <= bbot; bindex++)
  17468. + if (au_h_iptr(inode, bindex) == h_inode) {
  17469. + bfound = bindex;
  17470. + break;
  17471. + }
  17472. + if (bfound < 0)
  17473. + goto out;
  17474. +
  17475. + for (bindex = btop; bindex <= bbot; bindex++) {
  17476. + h_i = au_h_iptr(inode, bindex);
  17477. + if (!h_i)
  17478. + continue;
  17479. +
  17480. + err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
  17481. + /* ignore this error */
  17482. + /* bad action? */
  17483. + }
  17484. +
  17485. + /* children inode number will be broken */
  17486. +
  17487. +out:
  17488. + AuTraceErr(err);
  17489. + return err;
  17490. +}
  17491. +
  17492. +static int hn_gen_tree(struct dentry *dentry)
  17493. +{
  17494. + int err, i, j, ndentry;
  17495. + struct au_dcsub_pages dpages;
  17496. + struct au_dpage *dpage;
  17497. + struct dentry **dentries;
  17498. +
  17499. + err = au_dpages_init(&dpages, GFP_NOFS);
  17500. + if (unlikely(err))
  17501. + goto out;
  17502. + err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
  17503. + if (unlikely(err))
  17504. + goto out_dpages;
  17505. +
  17506. + for (i = 0; i < dpages.ndpage; i++) {
  17507. + dpage = dpages.dpages + i;
  17508. + dentries = dpage->dentries;
  17509. + ndentry = dpage->ndentry;
  17510. + for (j = 0; j < ndentry; j++) {
  17511. + struct dentry *d;
  17512. +
  17513. + d = dentries[j];
  17514. + if (IS_ROOT(d))
  17515. + continue;
  17516. +
  17517. + au_digen_dec(d);
  17518. + if (d_really_is_positive(d))
  17519. + /* todo: reset children xino?
  17520. + cached children only? */
  17521. + au_iigen_dec(d_inode(d));
  17522. + }
  17523. + }
  17524. +
  17525. +out_dpages:
  17526. + au_dpages_free(&dpages);
  17527. +out:
  17528. + return err;
  17529. +}
  17530. +
  17531. +/*
  17532. + * return 0 if processed.
  17533. + */
  17534. +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
  17535. + const unsigned int isdir)
  17536. +{
  17537. + int err;
  17538. + struct dentry *d;
  17539. + struct qstr *dname;
  17540. +
  17541. + err = 1;
  17542. + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
  17543. + pr_warn("branch root dir was changed\n");
  17544. + err = 0;
  17545. + goto out;
  17546. + }
  17547. +
  17548. + if (!isdir) {
  17549. + AuDebugOn(!name);
  17550. + au_iigen_dec(inode);
  17551. + spin_lock(&inode->i_lock);
  17552. + hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
  17553. + spin_lock(&d->d_lock);
  17554. + dname = &d->d_name;
  17555. + if (dname->len != nlen
  17556. + && memcmp(dname->name, name, nlen)) {
  17557. + spin_unlock(&d->d_lock);
  17558. + continue;
  17559. + }
  17560. + err = 0;
  17561. + au_digen_dec(d);
  17562. + spin_unlock(&d->d_lock);
  17563. + break;
  17564. + }
  17565. + spin_unlock(&inode->i_lock);
  17566. + } else {
  17567. + au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
  17568. + d = d_find_any_alias(inode);
  17569. + if (!d) {
  17570. + au_iigen_dec(inode);
  17571. + goto out;
  17572. + }
  17573. +
  17574. + spin_lock(&d->d_lock);
  17575. + dname = &d->d_name;
  17576. + if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
  17577. + spin_unlock(&d->d_lock);
  17578. + err = hn_gen_tree(d);
  17579. + spin_lock(&d->d_lock);
  17580. + }
  17581. + spin_unlock(&d->d_lock);
  17582. + dput(d);
  17583. + }
  17584. +
  17585. +out:
  17586. + AuTraceErr(err);
  17587. + return err;
  17588. +}
  17589. +
  17590. +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
  17591. +{
  17592. + int err;
  17593. +
  17594. + if (IS_ROOT(dentry)) {
  17595. + pr_warn("branch root dir was changed\n");
  17596. + return 0;
  17597. + }
  17598. +
  17599. + err = 0;
  17600. + if (!isdir) {
  17601. + au_digen_dec(dentry);
  17602. + if (d_really_is_positive(dentry))
  17603. + au_iigen_dec(d_inode(dentry));
  17604. + } else {
  17605. + au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
  17606. + if (d_really_is_positive(dentry))
  17607. + err = hn_gen_tree(dentry);
  17608. + }
  17609. +
  17610. + AuTraceErr(err);
  17611. + return err;
  17612. +}
  17613. +
  17614. +/* ---------------------------------------------------------------------- */
  17615. +
  17616. +/* hnotify job flags */
  17617. +#define AuHnJob_XINO0 1
  17618. +#define AuHnJob_GEN (1 << 1)
  17619. +#define AuHnJob_DIRENT (1 << 2)
  17620. +#define AuHnJob_ISDIR (1 << 3)
  17621. +#define AuHnJob_TRYXINO0 (1 << 4)
  17622. +#define AuHnJob_MNTPNT (1 << 5)
  17623. +#define au_ftest_hnjob(flags, name) ((flags) & AuHnJob_##name)
  17624. +#define au_fset_hnjob(flags, name) \
  17625. + do { (flags) |= AuHnJob_##name; } while (0)
  17626. +#define au_fclr_hnjob(flags, name) \
  17627. + do { (flags) &= ~AuHnJob_##name; } while (0)
  17628. +
  17629. +enum {
  17630. + AuHn_CHILD,
  17631. + AuHn_PARENT,
  17632. + AuHnLast
  17633. +};
  17634. +
  17635. +struct au_hnotify_args {
  17636. + struct inode *h_dir, *dir, *h_child_inode;
  17637. + u32 mask;
  17638. + unsigned int flags[AuHnLast];
  17639. + unsigned int h_child_nlen;
  17640. + char h_child_name[];
  17641. +};
  17642. +
  17643. +struct hn_job_args {
  17644. + unsigned int flags;
  17645. + struct inode *inode, *h_inode, *dir, *h_dir;
  17646. + struct dentry *dentry;
  17647. + char *h_name;
  17648. + int h_nlen;
  17649. +};
  17650. +
  17651. +static int hn_job(struct hn_job_args *a)
  17652. +{
  17653. + const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
  17654. + int e;
  17655. +
  17656. + /* reset xino */
  17657. + if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
  17658. + hn_xino(a->inode, a->h_inode); /* ignore this error */
  17659. +
  17660. + if (au_ftest_hnjob(a->flags, TRYXINO0)
  17661. + && a->inode
  17662. + && a->h_inode) {
  17663. + inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
  17664. + if (!a->h_inode->i_nlink
  17665. + && !(a->h_inode->i_state & I_LINKABLE))
  17666. + hn_xino(a->inode, a->h_inode); /* ignore this error */
  17667. + inode_unlock_shared(a->h_inode);
  17668. + }
  17669. +
  17670. + /* make the generation obsolete */
  17671. + if (au_ftest_hnjob(a->flags, GEN)) {
  17672. + e = -1;
  17673. + if (a->inode)
  17674. + e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
  17675. + isdir);
  17676. + if (e && a->dentry)
  17677. + hn_gen_by_name(a->dentry, isdir);
  17678. + /* ignore this error */
  17679. + }
  17680. +
  17681. + /* make dir entries obsolete */
  17682. + if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
  17683. + struct au_vdir *vdir;
  17684. +
  17685. + vdir = au_ivdir(a->inode);
  17686. + if (vdir)
  17687. + vdir->vd_jiffy = 0;
  17688. + /* IMustLock(a->inode); */
  17689. + /* inode_inc_iversion(a->inode); */
  17690. + }
  17691. +
  17692. + /* can do nothing but warn */
  17693. + if (au_ftest_hnjob(a->flags, MNTPNT)
  17694. + && a->dentry
  17695. + && d_mountpoint(a->dentry))
  17696. + pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
  17697. +
  17698. + return 0;
  17699. +}
  17700. +
  17701. +/* ---------------------------------------------------------------------- */
  17702. +
  17703. +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
  17704. + struct inode *dir)
  17705. +{
  17706. + struct dentry *dentry, *d, *parent;
  17707. + struct qstr *dname;
  17708. +
  17709. + parent = d_find_any_alias(dir);
  17710. + if (!parent)
  17711. + return NULL;
  17712. +
  17713. + dentry = NULL;
  17714. + spin_lock(&parent->d_lock);
  17715. + list_for_each_entry(d, &parent->d_subdirs, d_child) {
  17716. + /* AuDbg("%pd\n", d); */
  17717. + spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
  17718. + dname = &d->d_name;
  17719. + if (dname->len != nlen || memcmp(dname->name, name, nlen))
  17720. + goto cont_unlock;
  17721. + if (au_di(d))
  17722. + au_digen_dec(d);
  17723. + else
  17724. + goto cont_unlock;
  17725. + if (au_dcount(d) > 0) {
  17726. + dentry = dget_dlock(d);
  17727. + spin_unlock(&d->d_lock);
  17728. + break;
  17729. + }
  17730. +
  17731. +cont_unlock:
  17732. + spin_unlock(&d->d_lock);
  17733. + }
  17734. + spin_unlock(&parent->d_lock);
  17735. + dput(parent);
  17736. +
  17737. + if (dentry)
  17738. + di_write_lock_child(dentry);
  17739. +
  17740. + return dentry;
  17741. +}
  17742. +
  17743. +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
  17744. + aufs_bindex_t bindex, ino_t h_ino)
  17745. +{
  17746. + struct inode *inode;
  17747. + ino_t ino;
  17748. + int err;
  17749. +
  17750. + inode = NULL;
  17751. + err = au_xino_read(sb, bindex, h_ino, &ino);
  17752. + if (!err && ino)
  17753. + inode = ilookup(sb, ino);
  17754. + if (!inode)
  17755. + goto out;
  17756. +
  17757. + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
  17758. + pr_warn("wrong root branch\n");
  17759. + iput(inode);
  17760. + inode = NULL;
  17761. + goto out;
  17762. + }
  17763. +
  17764. + ii_write_lock_child(inode);
  17765. +
  17766. +out:
  17767. + return inode;
  17768. +}
  17769. +
  17770. +static void au_hn_bh(void *_args)
  17771. +{
  17772. + struct au_hnotify_args *a = _args;
  17773. + struct super_block *sb;
  17774. + aufs_bindex_t bindex, bbot, bfound;
  17775. + unsigned char xino, try_iput;
  17776. + int err;
  17777. + struct inode *inode;
  17778. + ino_t h_ino;
  17779. + struct hn_job_args args;
  17780. + struct dentry *dentry;
  17781. + struct au_sbinfo *sbinfo;
  17782. +
  17783. + AuDebugOn(!_args);
  17784. + AuDebugOn(!a->h_dir);
  17785. + AuDebugOn(!a->dir);
  17786. + AuDebugOn(!a->mask);
  17787. + AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
  17788. + a->mask, a->dir->i_ino, a->h_dir->i_ino,
  17789. + a->h_child_inode ? a->h_child_inode->i_ino : 0);
  17790. +
  17791. + inode = NULL;
  17792. + dentry = NULL;
  17793. + /*
  17794. + * do not lock a->dir->i_mutex here
  17795. + * because of d_revalidate() may cause a deadlock.
  17796. + */
  17797. + sb = a->dir->i_sb;
  17798. + AuDebugOn(!sb);
  17799. + sbinfo = au_sbi(sb);
  17800. + AuDebugOn(!sbinfo);
  17801. + si_write_lock(sb, AuLock_NOPLMW);
  17802. +
  17803. + if (au_opt_test(sbinfo->si_mntflags, DIRREN))
  17804. + switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
  17805. + case FS_MOVED_FROM:
  17806. + case FS_MOVED_TO:
  17807. + AuWarn1("DIRREN with UDBA may not work correctly "
  17808. + "for the direct rename(2)\n");
  17809. + }
  17810. +
  17811. + ii_read_lock_parent(a->dir);
  17812. + bfound = -1;
  17813. + bbot = au_ibbot(a->dir);
  17814. + for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
  17815. + if (au_h_iptr(a->dir, bindex) == a->h_dir) {
  17816. + bfound = bindex;
  17817. + break;
  17818. + }
  17819. + ii_read_unlock(a->dir);
  17820. + if (unlikely(bfound < 0))
  17821. + goto out;
  17822. +
  17823. + xino = !!au_opt_test(au_mntflags(sb), XINO);
  17824. + h_ino = 0;
  17825. + if (a->h_child_inode)
  17826. + h_ino = a->h_child_inode->i_ino;
  17827. +
  17828. + if (a->h_child_nlen
  17829. + && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
  17830. + || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
  17831. + dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
  17832. + a->dir);
  17833. + try_iput = 0;
  17834. + if (dentry && d_really_is_positive(dentry))
  17835. + inode = d_inode(dentry);
  17836. + if (xino && !inode && h_ino
  17837. + && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
  17838. + || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
  17839. + || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
  17840. + inode = lookup_wlock_by_ino(sb, bfound, h_ino);
  17841. + try_iput = 1;
  17842. + }
  17843. +
  17844. + args.flags = a->flags[AuHn_CHILD];
  17845. + args.dentry = dentry;
  17846. + args.inode = inode;
  17847. + args.h_inode = a->h_child_inode;
  17848. + args.dir = a->dir;
  17849. + args.h_dir = a->h_dir;
  17850. + args.h_name = a->h_child_name;
  17851. + args.h_nlen = a->h_child_nlen;
  17852. + err = hn_job(&args);
  17853. + if (dentry) {
  17854. + if (au_di(dentry))
  17855. + di_write_unlock(dentry);
  17856. + dput(dentry);
  17857. + }
  17858. + if (inode && try_iput) {
  17859. + ii_write_unlock(inode);
  17860. + iput(inode);
  17861. + }
  17862. +
  17863. + ii_write_lock_parent(a->dir);
  17864. + args.flags = a->flags[AuHn_PARENT];
  17865. + args.dentry = NULL;
  17866. + args.inode = a->dir;
  17867. + args.h_inode = a->h_dir;
  17868. + args.dir = NULL;
  17869. + args.h_dir = NULL;
  17870. + args.h_name = NULL;
  17871. + args.h_nlen = 0;
  17872. + err = hn_job(&args);
  17873. + ii_write_unlock(a->dir);
  17874. +
  17875. +out:
  17876. + iput(a->h_child_inode);
  17877. + iput(a->h_dir);
  17878. + iput(a->dir);
  17879. + si_write_unlock(sb);
  17880. + au_nwt_done(&sbinfo->si_nowait);
  17881. + au_kfree_rcu(a);
  17882. +}
  17883. +
  17884. +/* ---------------------------------------------------------------------- */
  17885. +
  17886. +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
  17887. + const struct qstr *h_child_qstr, struct inode *h_child_inode)
  17888. +{
  17889. + int err, len;
  17890. + unsigned int flags[AuHnLast], f;
  17891. + unsigned char isdir, isroot, wh;
  17892. + struct inode *dir;
  17893. + struct au_hnotify_args *args;
  17894. + char *p, *h_child_name;
  17895. +
  17896. + err = 0;
  17897. + AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
  17898. + dir = igrab(hnotify->hn_aufs_inode);
  17899. + if (!dir)
  17900. + goto out;
  17901. +
  17902. + isroot = (dir->i_ino == AUFS_ROOT_INO);
  17903. + wh = 0;
  17904. + h_child_name = (void *)h_child_qstr->name;
  17905. + len = h_child_qstr->len;
  17906. + if (h_child_name) {
  17907. + if (len > AUFS_WH_PFX_LEN
  17908. + && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
  17909. + h_child_name += AUFS_WH_PFX_LEN;
  17910. + len -= AUFS_WH_PFX_LEN;
  17911. + wh = 1;
  17912. + }
  17913. + }
  17914. +
  17915. + isdir = 0;
  17916. + if (h_child_inode)
  17917. + isdir = !!S_ISDIR(h_child_inode->i_mode);
  17918. + flags[AuHn_PARENT] = AuHnJob_ISDIR;
  17919. + flags[AuHn_CHILD] = 0;
  17920. + if (isdir)
  17921. + flags[AuHn_CHILD] = AuHnJob_ISDIR;
  17922. + au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
  17923. + au_fset_hnjob(flags[AuHn_CHILD], GEN);
  17924. + switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
  17925. + case FS_MOVED_FROM:
  17926. + case FS_MOVED_TO:
  17927. + au_fset_hnjob(flags[AuHn_CHILD], XINO0);
  17928. + au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
  17929. + fallthrough;
  17930. + case FS_CREATE:
  17931. + AuDebugOn(!h_child_name);
  17932. + break;
  17933. +
  17934. + case FS_DELETE:
  17935. + /*
  17936. + * aufs never be able to get this child inode.
  17937. + * revalidation should be in d_revalidate()
  17938. + * by checking i_nlink, i_generation or d_unhashed().
  17939. + */
  17940. + AuDebugOn(!h_child_name);
  17941. + au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
  17942. + au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
  17943. + break;
  17944. +
  17945. + default:
  17946. + AuDebugOn(1);
  17947. + }
  17948. +
  17949. + if (wh)
  17950. + h_child_inode = NULL;
  17951. +
  17952. + err = -ENOMEM;
  17953. + /* iput() and kfree() will be called in au_hnotify() */
  17954. + args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
  17955. + if (unlikely(!args)) {
  17956. + AuErr1("no memory\n");
  17957. + iput(dir);
  17958. + goto out;
  17959. + }
  17960. + args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
  17961. + args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
  17962. + args->mask = mask;
  17963. + args->dir = dir;
  17964. + args->h_dir = igrab(h_dir);
  17965. + if (h_child_inode)
  17966. + h_child_inode = igrab(h_child_inode); /* can be NULL */
  17967. + args->h_child_inode = h_child_inode;
  17968. + args->h_child_nlen = len;
  17969. + if (len) {
  17970. + p = (void *)args;
  17971. + p += sizeof(*args);
  17972. + memcpy(p, h_child_name, len);
  17973. + p[len] = 0;
  17974. + }
  17975. +
  17976. + /* NFS fires the event for silly-renamed one from kworker */
  17977. + f = 0;
  17978. + if (!dir->i_nlink
  17979. + || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
  17980. + f = AuWkq_NEST;
  17981. + err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
  17982. + if (unlikely(err)) {
  17983. + pr_err("wkq %d\n", err);
  17984. + iput(args->h_child_inode);
  17985. + iput(args->h_dir);
  17986. + iput(args->dir);
  17987. + au_kfree_rcu(args);
  17988. + }
  17989. +
  17990. +out:
  17991. + return err;
  17992. +}
  17993. +
  17994. +/* ---------------------------------------------------------------------- */
  17995. +
  17996. +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
  17997. +{
  17998. + int err;
  17999. +
  18000. + AuDebugOn(!(udba & AuOptMask_UDBA));
  18001. +
  18002. + err = 0;
  18003. + if (au_hnotify_op.reset_br)
  18004. + err = au_hnotify_op.reset_br(udba, br, perm);
  18005. +
  18006. + return err;
  18007. +}
  18008. +
  18009. +int au_hnotify_init_br(struct au_branch *br, int perm)
  18010. +{
  18011. + int err;
  18012. +
  18013. + err = 0;
  18014. + if (au_hnotify_op.init_br)
  18015. + err = au_hnotify_op.init_br(br, perm);
  18016. +
  18017. + return err;
  18018. +}
  18019. +
  18020. +void au_hnotify_fin_br(struct au_branch *br)
  18021. +{
  18022. + if (au_hnotify_op.fin_br)
  18023. + au_hnotify_op.fin_br(br);
  18024. +}
  18025. +
  18026. +static void au_hn_destroy_cache(void)
  18027. +{
  18028. + kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
  18029. + au_cache[AuCache_HNOTIFY] = NULL;
  18030. +}
  18031. +
  18032. +int __init au_hnotify_init(void)
  18033. +{
  18034. + int err;
  18035. +
  18036. + err = -ENOMEM;
  18037. + au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
  18038. + if (au_cache[AuCache_HNOTIFY]) {
  18039. + err = 0;
  18040. + if (au_hnotify_op.init)
  18041. + err = au_hnotify_op.init();
  18042. + if (unlikely(err))
  18043. + au_hn_destroy_cache();
  18044. + }
  18045. + AuTraceErr(err);
  18046. + return err;
  18047. +}
  18048. +
  18049. +void au_hnotify_fin(void)
  18050. +{
  18051. + if (au_hnotify_op.fin)
  18052. + au_hnotify_op.fin();
  18053. +
  18054. + /* cf. au_cache_fin() */
  18055. + if (au_cache[AuCache_HNOTIFY])
  18056. + au_hn_destroy_cache();
  18057. +}
  18058. diff -Naur null/fs/aufs/iinfo.c linux-5.15.131/fs/aufs/iinfo.c
  18059. --- /dev/null
  18060. +++ linux-5.15.131/fs/aufs/iinfo.c 2023-09-11 14:32:48.150722527 +0300
  18061. @@ -0,0 +1,286 @@
  18062. +// SPDX-License-Identifier: GPL-2.0
  18063. +/*
  18064. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  18065. + *
  18066. + * This program, aufs is free software; you can redistribute it and/or modify
  18067. + * it under the terms of the GNU General Public License as published by
  18068. + * the Free Software Foundation; either version 2 of the License, or
  18069. + * (at your option) any later version.
  18070. + *
  18071. + * This program is distributed in the hope that it will be useful,
  18072. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18073. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18074. + * GNU General Public License for more details.
  18075. + *
  18076. + * You should have received a copy of the GNU General Public License
  18077. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18078. + */
  18079. +
  18080. +/*
  18081. + * inode private data
  18082. + */
  18083. +
  18084. +#include "aufs.h"
  18085. +
  18086. +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
  18087. +{
  18088. + struct inode *h_inode;
  18089. + struct au_hinode *hinode;
  18090. +
  18091. + IiMustAnyLock(inode);
  18092. +
  18093. + hinode = au_hinode(au_ii(inode), bindex);
  18094. + h_inode = hinode->hi_inode;
  18095. + AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
  18096. + return h_inode;
  18097. +}
  18098. +
  18099. +/* todo: hard/soft set? */
  18100. +void au_hiput(struct au_hinode *hinode)
  18101. +{
  18102. + au_hn_free(hinode);
  18103. + dput(hinode->hi_whdentry);
  18104. + iput(hinode->hi_inode);
  18105. +}
  18106. +
  18107. +unsigned int au_hi_flags(struct inode *inode, int isdir)
  18108. +{
  18109. + unsigned int flags;
  18110. + const unsigned int mnt_flags = au_mntflags(inode->i_sb);
  18111. +
  18112. + flags = 0;
  18113. + if (au_opt_test(mnt_flags, XINO))
  18114. + au_fset_hi(flags, XINO);
  18115. + if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
  18116. + au_fset_hi(flags, HNOTIFY);
  18117. + return flags;
  18118. +}
  18119. +
  18120. +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
  18121. + struct inode *h_inode, unsigned int flags)
  18122. +{
  18123. + struct au_hinode *hinode;
  18124. + struct inode *hi;
  18125. + struct au_iinfo *iinfo = au_ii(inode);
  18126. +
  18127. + IiMustWriteLock(inode);
  18128. +
  18129. + hinode = au_hinode(iinfo, bindex);
  18130. + hi = hinode->hi_inode;
  18131. + AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
  18132. +
  18133. + if (hi)
  18134. + au_hiput(hinode);
  18135. + hinode->hi_inode = h_inode;
  18136. + if (h_inode) {
  18137. + int err;
  18138. + struct super_block *sb = inode->i_sb;
  18139. + struct au_branch *br;
  18140. +
  18141. + AuDebugOn(inode->i_mode
  18142. + && (h_inode->i_mode & S_IFMT)
  18143. + != (inode->i_mode & S_IFMT));
  18144. + if (bindex == iinfo->ii_btop)
  18145. + au_cpup_igen(inode, h_inode);
  18146. + br = au_sbr(sb, bindex);
  18147. + hinode->hi_id = br->br_id;
  18148. + if (au_ftest_hi(flags, XINO)) {
  18149. + err = au_xino_write(sb, bindex, h_inode->i_ino,
  18150. + inode->i_ino);
  18151. + if (unlikely(err))
  18152. + AuIOErr1("failed au_xino_write() %d\n", err);
  18153. + }
  18154. +
  18155. + if (au_ftest_hi(flags, HNOTIFY)
  18156. + && au_br_hnotifyable(br->br_perm)) {
  18157. + err = au_hn_alloc(hinode, inode);
  18158. + if (unlikely(err))
  18159. + AuIOErr1("au_hn_alloc() %d\n", err);
  18160. + }
  18161. + }
  18162. +}
  18163. +
  18164. +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
  18165. + struct dentry *h_wh)
  18166. +{
  18167. + struct au_hinode *hinode;
  18168. +
  18169. + IiMustWriteLock(inode);
  18170. +
  18171. + hinode = au_hinode(au_ii(inode), bindex);
  18172. + AuDebugOn(hinode->hi_whdentry);
  18173. + hinode->hi_whdentry = h_wh;
  18174. +}
  18175. +
  18176. +void au_update_iigen(struct inode *inode, int half)
  18177. +{
  18178. + struct au_iinfo *iinfo;
  18179. + struct au_iigen *iigen;
  18180. + unsigned int sigen;
  18181. +
  18182. + sigen = au_sigen(inode->i_sb);
  18183. + iinfo = au_ii(inode);
  18184. + iigen = &iinfo->ii_generation;
  18185. + spin_lock(&iigen->ig_spin);
  18186. + iigen->ig_generation = sigen;
  18187. + if (half)
  18188. + au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
  18189. + else
  18190. + au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
  18191. + spin_unlock(&iigen->ig_spin);
  18192. +}
  18193. +
  18194. +/* it may be called at remount time, too */
  18195. +void au_update_ibrange(struct inode *inode, int do_put_zero)
  18196. +{
  18197. + struct au_iinfo *iinfo;
  18198. + aufs_bindex_t bindex, bbot;
  18199. +
  18200. + AuDebugOn(au_is_bad_inode(inode));
  18201. + IiMustWriteLock(inode);
  18202. +
  18203. + iinfo = au_ii(inode);
  18204. + if (do_put_zero && iinfo->ii_btop >= 0) {
  18205. + for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
  18206. + bindex++) {
  18207. + struct inode *h_i;
  18208. +
  18209. + h_i = au_hinode(iinfo, bindex)->hi_inode;
  18210. + if (h_i
  18211. + && !h_i->i_nlink
  18212. + && !(h_i->i_state & I_LINKABLE))
  18213. + au_set_h_iptr(inode, bindex, NULL, 0);
  18214. + }
  18215. + }
  18216. +
  18217. + iinfo->ii_btop = -1;
  18218. + iinfo->ii_bbot = -1;
  18219. + bbot = au_sbbot(inode->i_sb);
  18220. + for (bindex = 0; bindex <= bbot; bindex++)
  18221. + if (au_hinode(iinfo, bindex)->hi_inode) {
  18222. + iinfo->ii_btop = bindex;
  18223. + break;
  18224. + }
  18225. + if (iinfo->ii_btop >= 0)
  18226. + for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
  18227. + if (au_hinode(iinfo, bindex)->hi_inode) {
  18228. + iinfo->ii_bbot = bindex;
  18229. + break;
  18230. + }
  18231. + AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
  18232. +}
  18233. +
  18234. +/* ---------------------------------------------------------------------- */
  18235. +
  18236. +void au_icntnr_init_once(void *_c)
  18237. +{
  18238. + struct au_icntnr *c = _c;
  18239. + struct au_iinfo *iinfo = &c->iinfo;
  18240. +
  18241. + spin_lock_init(&iinfo->ii_generation.ig_spin);
  18242. + au_rw_init(&iinfo->ii_rwsem);
  18243. + inode_init_once(&c->vfs_inode);
  18244. +}
  18245. +
  18246. +void au_hinode_init(struct au_hinode *hinode)
  18247. +{
  18248. + hinode->hi_inode = NULL;
  18249. + hinode->hi_id = -1;
  18250. + au_hn_init(hinode);
  18251. + hinode->hi_whdentry = NULL;
  18252. +}
  18253. +
  18254. +int au_iinfo_init(struct inode *inode)
  18255. +{
  18256. + struct au_iinfo *iinfo;
  18257. + struct super_block *sb;
  18258. + struct au_hinode *hi;
  18259. + int nbr, i;
  18260. +
  18261. + sb = inode->i_sb;
  18262. + iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
  18263. + nbr = au_sbbot(sb) + 1;
  18264. + if (unlikely(nbr <= 0))
  18265. + nbr = 1;
  18266. + hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
  18267. + if (hi) {
  18268. + au_lcnt_inc(&au_sbi(sb)->si_ninodes);
  18269. +
  18270. + iinfo->ii_hinode = hi;
  18271. + for (i = 0; i < nbr; i++, hi++)
  18272. + au_hinode_init(hi);
  18273. +
  18274. + iinfo->ii_generation.ig_generation = au_sigen(sb);
  18275. + iinfo->ii_btop = -1;
  18276. + iinfo->ii_bbot = -1;
  18277. + iinfo->ii_vdir = NULL;
  18278. + return 0;
  18279. + }
  18280. + return -ENOMEM;
  18281. +}
  18282. +
  18283. +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
  18284. +{
  18285. + int err, i;
  18286. + struct au_hinode *hip;
  18287. +
  18288. + AuRwMustWriteLock(&iinfo->ii_rwsem);
  18289. +
  18290. + err = -ENOMEM;
  18291. + hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
  18292. + may_shrink);
  18293. + if (hip) {
  18294. + iinfo->ii_hinode = hip;
  18295. + i = iinfo->ii_bbot + 1;
  18296. + hip += i;
  18297. + for (; i < nbr; i++, hip++)
  18298. + au_hinode_init(hip);
  18299. + err = 0;
  18300. + }
  18301. +
  18302. + return err;
  18303. +}
  18304. +
  18305. +void au_iinfo_fin(struct inode *inode)
  18306. +{
  18307. + struct au_iinfo *iinfo;
  18308. + struct au_hinode *hi;
  18309. + struct super_block *sb;
  18310. + aufs_bindex_t bindex, bbot;
  18311. + const unsigned char unlinked = !inode->i_nlink;
  18312. +
  18313. + AuDebugOn(au_is_bad_inode(inode));
  18314. +
  18315. + sb = inode->i_sb;
  18316. + au_lcnt_dec(&au_sbi(sb)->si_ninodes);
  18317. + if (si_pid_test(sb))
  18318. + au_xino_delete_inode(inode, unlinked);
  18319. + else {
  18320. + /*
  18321. + * it is safe to hide the dependency between sbinfo and
  18322. + * sb->s_umount.
  18323. + */
  18324. + lockdep_off();
  18325. + si_noflush_read_lock(sb);
  18326. + au_xino_delete_inode(inode, unlinked);
  18327. + si_read_unlock(sb);
  18328. + lockdep_on();
  18329. + }
  18330. +
  18331. + iinfo = au_ii(inode);
  18332. + if (iinfo->ii_vdir)
  18333. + au_vdir_free(iinfo->ii_vdir);
  18334. +
  18335. + bindex = iinfo->ii_btop;
  18336. + if (bindex >= 0) {
  18337. + hi = au_hinode(iinfo, bindex);
  18338. + bbot = iinfo->ii_bbot;
  18339. + while (bindex++ <= bbot) {
  18340. + if (hi->hi_inode)
  18341. + au_hiput(hi);
  18342. + hi++;
  18343. + }
  18344. + }
  18345. + au_kfree_rcu(iinfo->ii_hinode);
  18346. + AuRwDestroy(&iinfo->ii_rwsem);
  18347. +}
  18348. diff -Naur null/fs/aufs/inode.c linux-5.15.131/fs/aufs/inode.c
  18349. --- /dev/null
  18350. +++ linux-5.15.131/fs/aufs/inode.c 2023-09-11 14:32:48.150722527 +0300
  18351. @@ -0,0 +1,531 @@
  18352. +// SPDX-License-Identifier: GPL-2.0
  18353. +/*
  18354. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  18355. + *
  18356. + * This program, aufs is free software; you can redistribute it and/or modify
  18357. + * it under the terms of the GNU General Public License as published by
  18358. + * the Free Software Foundation; either version 2 of the License, or
  18359. + * (at your option) any later version.
  18360. + *
  18361. + * This program is distributed in the hope that it will be useful,
  18362. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18363. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18364. + * GNU General Public License for more details.
  18365. + *
  18366. + * You should have received a copy of the GNU General Public License
  18367. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18368. + */
  18369. +
  18370. +/*
  18371. + * inode functions
  18372. + */
  18373. +
  18374. +#include <linux/iversion.h>
  18375. +#include "aufs.h"
  18376. +
  18377. +struct inode *au_igrab(struct inode *inode)
  18378. +{
  18379. + if (inode) {
  18380. + AuDebugOn(!atomic_read(&inode->i_count));
  18381. + ihold(inode);
  18382. + }
  18383. + return inode;
  18384. +}
  18385. +
  18386. +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
  18387. +{
  18388. + au_cpup_attr_all(inode, /*force*/0);
  18389. + au_update_iigen(inode, /*half*/1);
  18390. + if (do_version)
  18391. + inode_inc_iversion(inode);
  18392. +}
  18393. +
  18394. +static int au_ii_refresh(struct inode *inode, int *update)
  18395. +{
  18396. + int err, e, nbr;
  18397. + umode_t type;
  18398. + aufs_bindex_t bindex, new_bindex;
  18399. + struct super_block *sb;
  18400. + struct au_iinfo *iinfo;
  18401. + struct au_hinode *p, *q, tmp;
  18402. +
  18403. + AuDebugOn(au_is_bad_inode(inode));
  18404. + IiMustWriteLock(inode);
  18405. +
  18406. + *update = 0;
  18407. + sb = inode->i_sb;
  18408. + nbr = au_sbbot(sb) + 1;
  18409. + type = inode->i_mode & S_IFMT;
  18410. + iinfo = au_ii(inode);
  18411. + err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
  18412. + if (unlikely(err))
  18413. + goto out;
  18414. +
  18415. + AuDebugOn(iinfo->ii_btop < 0);
  18416. + p = au_hinode(iinfo, iinfo->ii_btop);
  18417. + for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
  18418. + bindex++, p++) {
  18419. + if (!p->hi_inode)
  18420. + continue;
  18421. +
  18422. + AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
  18423. + new_bindex = au_br_index(sb, p->hi_id);
  18424. + if (new_bindex == bindex)
  18425. + continue;
  18426. +
  18427. + if (new_bindex < 0) {
  18428. + *update = 1;
  18429. + au_hiput(p);
  18430. + p->hi_inode = NULL;
  18431. + continue;
  18432. + }
  18433. +
  18434. + if (new_bindex < iinfo->ii_btop)
  18435. + iinfo->ii_btop = new_bindex;
  18436. + if (iinfo->ii_bbot < new_bindex)
  18437. + iinfo->ii_bbot = new_bindex;
  18438. + /* swap two lower inode, and loop again */
  18439. + q = au_hinode(iinfo, new_bindex);
  18440. + tmp = *q;
  18441. + *q = *p;
  18442. + *p = tmp;
  18443. + if (tmp.hi_inode) {
  18444. + bindex--;
  18445. + p--;
  18446. + }
  18447. + }
  18448. + au_update_ibrange(inode, /*do_put_zero*/0);
  18449. + au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
  18450. + e = au_dy_irefresh(inode);
  18451. + if (unlikely(e && !err))
  18452. + err = e;
  18453. +
  18454. +out:
  18455. + AuTraceErr(err);
  18456. + return err;
  18457. +}
  18458. +
  18459. +void au_refresh_iop(struct inode *inode, int force_getattr)
  18460. +{
  18461. + int type;
  18462. + struct au_sbinfo *sbi = au_sbi(inode->i_sb);
  18463. + const struct inode_operations *iop
  18464. + = force_getattr ? aufs_iop : sbi->si_iop_array;
  18465. +
  18466. + if (inode->i_op == iop)
  18467. + return;
  18468. +
  18469. + switch (inode->i_mode & S_IFMT) {
  18470. + case S_IFDIR:
  18471. + type = AuIop_DIR;
  18472. + break;
  18473. + case S_IFLNK:
  18474. + type = AuIop_SYMLINK;
  18475. + break;
  18476. + default:
  18477. + type = AuIop_OTHER;
  18478. + break;
  18479. + }
  18480. +
  18481. + inode->i_op = iop + type;
  18482. + /* unnecessary smp_wmb() */
  18483. +}
  18484. +
  18485. +int au_refresh_hinode_self(struct inode *inode)
  18486. +{
  18487. + int err, update;
  18488. +
  18489. + err = au_ii_refresh(inode, &update);
  18490. + if (!err)
  18491. + au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
  18492. +
  18493. + AuTraceErr(err);
  18494. + return err;
  18495. +}
  18496. +
  18497. +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
  18498. +{
  18499. + int err, e, update;
  18500. + unsigned int flags;
  18501. + umode_t mode;
  18502. + aufs_bindex_t bindex, bbot;
  18503. + unsigned char isdir;
  18504. + struct au_hinode *p;
  18505. + struct au_iinfo *iinfo;
  18506. +
  18507. + err = au_ii_refresh(inode, &update);
  18508. + if (unlikely(err))
  18509. + goto out;
  18510. +
  18511. + update = 0;
  18512. + iinfo = au_ii(inode);
  18513. + p = au_hinode(iinfo, iinfo->ii_btop);
  18514. + mode = (inode->i_mode & S_IFMT);
  18515. + isdir = S_ISDIR(mode);
  18516. + flags = au_hi_flags(inode, isdir);
  18517. + bbot = au_dbbot(dentry);
  18518. + for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
  18519. + struct inode *h_i, *h_inode;
  18520. + struct dentry *h_d;
  18521. +
  18522. + h_d = au_h_dptr(dentry, bindex);
  18523. + if (!h_d || d_is_negative(h_d))
  18524. + continue;
  18525. +
  18526. + h_inode = d_inode(h_d);
  18527. + AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
  18528. + if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
  18529. + h_i = au_h_iptr(inode, bindex);
  18530. + if (h_i) {
  18531. + if (h_i == h_inode)
  18532. + continue;
  18533. + err = -EIO;
  18534. + break;
  18535. + }
  18536. + }
  18537. + if (bindex < iinfo->ii_btop)
  18538. + iinfo->ii_btop = bindex;
  18539. + if (iinfo->ii_bbot < bindex)
  18540. + iinfo->ii_bbot = bindex;
  18541. + au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
  18542. + update = 1;
  18543. + }
  18544. + au_update_ibrange(inode, /*do_put_zero*/0);
  18545. + e = au_dy_irefresh(inode);
  18546. + if (unlikely(e && !err))
  18547. + err = e;
  18548. + if (!err)
  18549. + au_refresh_hinode_attr(inode, update && isdir);
  18550. +
  18551. +out:
  18552. + AuTraceErr(err);
  18553. + return err;
  18554. +}
  18555. +
  18556. +static int set_inode(struct inode *inode, struct dentry *dentry)
  18557. +{
  18558. + int err;
  18559. + unsigned int flags;
  18560. + umode_t mode;
  18561. + aufs_bindex_t bindex, btop, btail;
  18562. + unsigned char isdir;
  18563. + struct dentry *h_dentry;
  18564. + struct inode *h_inode;
  18565. + struct au_iinfo *iinfo;
  18566. + const struct inode_operations *iop;
  18567. +
  18568. + IiMustWriteLock(inode);
  18569. +
  18570. + err = 0;
  18571. + isdir = 0;
  18572. + iop = au_sbi(inode->i_sb)->si_iop_array;
  18573. + btop = au_dbtop(dentry);
  18574. + h_dentry = au_h_dptr(dentry, btop);
  18575. + h_inode = d_inode(h_dentry);
  18576. + mode = h_inode->i_mode;
  18577. + switch (mode & S_IFMT) {
  18578. + case S_IFREG:
  18579. + btail = au_dbtail(dentry);
  18580. + inode->i_op = iop + AuIop_OTHER;
  18581. + inode->i_fop = &aufs_file_fop;
  18582. + err = au_dy_iaop(inode, btop, h_inode);
  18583. + if (unlikely(err))
  18584. + goto out;
  18585. + break;
  18586. + case S_IFDIR:
  18587. + isdir = 1;
  18588. + btail = au_dbtaildir(dentry);
  18589. + inode->i_op = iop + AuIop_DIR;
  18590. + inode->i_fop = &aufs_dir_fop;
  18591. + break;
  18592. + case S_IFLNK:
  18593. + btail = au_dbtail(dentry);
  18594. + inode->i_op = iop + AuIop_SYMLINK;
  18595. + break;
  18596. + case S_IFBLK:
  18597. + case S_IFCHR:
  18598. + case S_IFIFO:
  18599. + case S_IFSOCK:
  18600. + btail = au_dbtail(dentry);
  18601. + inode->i_op = iop + AuIop_OTHER;
  18602. + init_special_inode(inode, mode, h_inode->i_rdev);
  18603. + break;
  18604. + default:
  18605. + AuIOErr("Unknown file type 0%o\n", mode);
  18606. + err = -EIO;
  18607. + goto out;
  18608. + }
  18609. +
  18610. + /* do not set hnotify for whiteouted dirs (SHWH mode) */
  18611. + flags = au_hi_flags(inode, isdir);
  18612. + if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
  18613. + && au_ftest_hi(flags, HNOTIFY)
  18614. + && dentry->d_name.len > AUFS_WH_PFX_LEN
  18615. + && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
  18616. + au_fclr_hi(flags, HNOTIFY);
  18617. + iinfo = au_ii(inode);
  18618. + iinfo->ii_btop = btop;
  18619. + iinfo->ii_bbot = btail;
  18620. + for (bindex = btop; bindex <= btail; bindex++) {
  18621. + h_dentry = au_h_dptr(dentry, bindex);
  18622. + if (h_dentry)
  18623. + au_set_h_iptr(inode, bindex,
  18624. + au_igrab(d_inode(h_dentry)), flags);
  18625. + }
  18626. + au_cpup_attr_all(inode, /*force*/1);
  18627. + /*
  18628. + * to force calling aufs_get_acl() every time,
  18629. + * do not call cache_no_acl() for aufs inode.
  18630. + */
  18631. +
  18632. +out:
  18633. + return err;
  18634. +}
  18635. +
  18636. +/*
  18637. + * successful returns with iinfo write_locked
  18638. + * minus: errno
  18639. + * zero: success, matched
  18640. + * plus: no error, but unmatched
  18641. + */
  18642. +static int reval_inode(struct inode *inode, struct dentry *dentry)
  18643. +{
  18644. + int err;
  18645. + unsigned int gen, igflags;
  18646. + aufs_bindex_t bindex, bbot;
  18647. + struct inode *h_inode, *h_dinode;
  18648. + struct dentry *h_dentry;
  18649. +
  18650. + /*
  18651. + * before this function, if aufs got any iinfo lock, it must be only
  18652. + * one, the parent dir.
  18653. + * it can happen by UDBA and the obsoleted inode number.
  18654. + */
  18655. + err = -EIO;
  18656. + if (unlikely(inode->i_ino == parent_ino(dentry)))
  18657. + goto out;
  18658. +
  18659. + err = 1;
  18660. + ii_write_lock_new_child(inode);
  18661. + h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
  18662. + h_dinode = d_inode(h_dentry);
  18663. + bbot = au_ibbot(inode);
  18664. + for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
  18665. + h_inode = au_h_iptr(inode, bindex);
  18666. + if (!h_inode || h_inode != h_dinode)
  18667. + continue;
  18668. +
  18669. + err = 0;
  18670. + gen = au_iigen(inode, &igflags);
  18671. + if (gen == au_digen(dentry)
  18672. + && !au_ig_ftest(igflags, HALF_REFRESHED))
  18673. + break;
  18674. +
  18675. + /* fully refresh inode using dentry */
  18676. + err = au_refresh_hinode(inode, dentry);
  18677. + if (!err)
  18678. + au_update_iigen(inode, /*half*/0);
  18679. + break;
  18680. + }
  18681. +
  18682. + if (unlikely(err))
  18683. + ii_write_unlock(inode);
  18684. +out:
  18685. + return err;
  18686. +}
  18687. +
  18688. +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  18689. + unsigned int d_type, ino_t *ino)
  18690. +{
  18691. + int err, idx;
  18692. + const int isnondir = d_type != DT_DIR;
  18693. +
  18694. + /* prevent hardlinked inode number from race condition */
  18695. + if (isnondir) {
  18696. + err = au_xinondir_enter(sb, bindex, h_ino, &idx);
  18697. + if (unlikely(err))
  18698. + goto out;
  18699. + }
  18700. +
  18701. + err = au_xino_read(sb, bindex, h_ino, ino);
  18702. + if (unlikely(err))
  18703. + goto out_xinondir;
  18704. +
  18705. + if (!*ino) {
  18706. + err = -EIO;
  18707. + *ino = au_xino_new_ino(sb);
  18708. + if (unlikely(!*ino))
  18709. + goto out_xinondir;
  18710. + err = au_xino_write(sb, bindex, h_ino, *ino);
  18711. + if (unlikely(err))
  18712. + goto out_xinondir;
  18713. + }
  18714. +
  18715. +out_xinondir:
  18716. + if (isnondir && idx >= 0)
  18717. + au_xinondir_leave(sb, bindex, h_ino, idx);
  18718. +out:
  18719. + return err;
  18720. +}
  18721. +
  18722. +/* successful returns with iinfo write_locked */
  18723. +/* todo: return with unlocked? */
  18724. +struct inode *au_new_inode(struct dentry *dentry, int must_new)
  18725. +{
  18726. + struct inode *inode, *h_inode;
  18727. + struct dentry *h_dentry;
  18728. + struct super_block *sb;
  18729. + ino_t h_ino, ino;
  18730. + int err, idx, hlinked;
  18731. + aufs_bindex_t btop;
  18732. +
  18733. + sb = dentry->d_sb;
  18734. + btop = au_dbtop(dentry);
  18735. + h_dentry = au_h_dptr(dentry, btop);
  18736. + h_inode = d_inode(h_dentry);
  18737. + h_ino = h_inode->i_ino;
  18738. + hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
  18739. +
  18740. +new_ino:
  18741. + /*
  18742. + * stop 'race'-ing between hardlinks under different
  18743. + * parents.
  18744. + */
  18745. + if (hlinked) {
  18746. + err = au_xinondir_enter(sb, btop, h_ino, &idx);
  18747. + inode = ERR_PTR(err);
  18748. + if (unlikely(err))
  18749. + goto out;
  18750. + }
  18751. +
  18752. + err = au_xino_read(sb, btop, h_ino, &ino);
  18753. + inode = ERR_PTR(err);
  18754. + if (unlikely(err))
  18755. + goto out_xinondir;
  18756. +
  18757. + if (!ino) {
  18758. + ino = au_xino_new_ino(sb);
  18759. + if (unlikely(!ino)) {
  18760. + inode = ERR_PTR(-EIO);
  18761. + goto out_xinondir;
  18762. + }
  18763. + }
  18764. +
  18765. + AuDbg("i%lu\n", (unsigned long)ino);
  18766. + inode = au_iget_locked(sb, ino);
  18767. + err = PTR_ERR(inode);
  18768. + if (IS_ERR(inode))
  18769. + goto out_xinondir;
  18770. +
  18771. + AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
  18772. + if (inode->i_state & I_NEW) {
  18773. + ii_write_lock_new_child(inode);
  18774. + err = set_inode(inode, dentry);
  18775. + if (!err) {
  18776. + unlock_new_inode(inode);
  18777. + goto out_xinondir; /* success */
  18778. + }
  18779. +
  18780. + /*
  18781. + * iget_failed() calls iput(), but we need to call
  18782. + * ii_write_unlock() after iget_failed(). so dirty hack for
  18783. + * i_count.
  18784. + */
  18785. + atomic_inc(&inode->i_count);
  18786. + iget_failed(inode);
  18787. + ii_write_unlock(inode);
  18788. + au_xino_write(sb, btop, h_ino, /*ino*/0);
  18789. + /* ignore this error */
  18790. + goto out_iput;
  18791. + } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
  18792. + /*
  18793. + * horrible race condition between lookup, readdir and copyup
  18794. + * (or something).
  18795. + */
  18796. + if (hlinked && idx >= 0)
  18797. + au_xinondir_leave(sb, btop, h_ino, idx);
  18798. + err = reval_inode(inode, dentry);
  18799. + if (unlikely(err < 0)) {
  18800. + hlinked = 0;
  18801. + goto out_iput;
  18802. + }
  18803. + if (!err)
  18804. + goto out; /* success */
  18805. + else if (hlinked && idx >= 0) {
  18806. + err = au_xinondir_enter(sb, btop, h_ino, &idx);
  18807. + if (unlikely(err)) {
  18808. + iput(inode);
  18809. + inode = ERR_PTR(err);
  18810. + goto out;
  18811. + }
  18812. + }
  18813. + }
  18814. +
  18815. + if (unlikely(au_test_fs_unique_ino(h_inode)))
  18816. + AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
  18817. + " b%d, %s, %pd, hi%lu, i%lu.\n",
  18818. + btop, au_sbtype(h_dentry->d_sb), dentry,
  18819. + (unsigned long)h_ino, (unsigned long)ino);
  18820. + ino = 0;
  18821. + err = au_xino_write(sb, btop, h_ino, /*ino*/0);
  18822. + if (!err) {
  18823. + iput(inode);
  18824. + if (hlinked && idx >= 0)
  18825. + au_xinondir_leave(sb, btop, h_ino, idx);
  18826. + goto new_ino;
  18827. + }
  18828. +
  18829. +out_iput:
  18830. + iput(inode);
  18831. + inode = ERR_PTR(err);
  18832. +out_xinondir:
  18833. + if (hlinked && idx >= 0)
  18834. + au_xinondir_leave(sb, btop, h_ino, idx);
  18835. +out:
  18836. + return inode;
  18837. +}
  18838. +
  18839. +/* ---------------------------------------------------------------------- */
  18840. +
  18841. +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
  18842. + struct inode *inode)
  18843. +{
  18844. + int err;
  18845. + struct inode *hi;
  18846. +
  18847. + err = au_br_rdonly(au_sbr(sb, bindex));
  18848. +
  18849. + /* pseudo-link after flushed may happen out of bounds */
  18850. + if (!err
  18851. + && inode
  18852. + && au_ibtop(inode) <= bindex
  18853. + && bindex <= au_ibbot(inode)) {
  18854. + /*
  18855. + * permission check is unnecessary since vfsub routine
  18856. + * will be called later
  18857. + */
  18858. + hi = au_h_iptr(inode, bindex);
  18859. + if (hi)
  18860. + err = IS_IMMUTABLE(hi) ? -EROFS : 0;
  18861. + }
  18862. +
  18863. + return err;
  18864. +}
  18865. +
  18866. +int au_test_h_perm(struct user_namespace *h_userns, struct inode *h_inode,
  18867. + int mask)
  18868. +{
  18869. + if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
  18870. + return 0;
  18871. + return inode_permission(h_userns, h_inode, mask);
  18872. +}
  18873. +
  18874. +int au_test_h_perm_sio(struct user_namespace *h_userns, struct inode *h_inode,
  18875. + int mask)
  18876. +{
  18877. + if (au_test_nfs(h_inode->i_sb)
  18878. + && (mask & MAY_WRITE)
  18879. + && S_ISDIR(h_inode->i_mode))
  18880. + mask |= MAY_READ; /* force permission check */
  18881. + return au_test_h_perm(h_userns, h_inode, mask);
  18882. +}
  18883. diff -Naur null/fs/aufs/inode.h linux-5.15.131/fs/aufs/inode.h
  18884. --- /dev/null
  18885. +++ linux-5.15.131/fs/aufs/inode.h 2023-09-11 14:32:48.150722527 +0300
  18886. @@ -0,0 +1,705 @@
  18887. +/* SPDX-License-Identifier: GPL-2.0 */
  18888. +/*
  18889. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  18890. + *
  18891. + * This program, aufs is free software; you can redistribute it and/or modify
  18892. + * it under the terms of the GNU General Public License as published by
  18893. + * the Free Software Foundation; either version 2 of the License, or
  18894. + * (at your option) any later version.
  18895. + *
  18896. + * This program is distributed in the hope that it will be useful,
  18897. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18898. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18899. + * GNU General Public License for more details.
  18900. + *
  18901. + * You should have received a copy of the GNU General Public License
  18902. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18903. + */
  18904. +
  18905. +/*
  18906. + * inode operations
  18907. + */
  18908. +
  18909. +#ifndef __AUFS_INODE_H__
  18910. +#define __AUFS_INODE_H__
  18911. +
  18912. +#ifdef __KERNEL__
  18913. +
  18914. +#include <linux/fsnotify.h>
  18915. +#include "rwsem.h"
  18916. +
  18917. +struct vfsmount;
  18918. +
  18919. +struct au_hnotify {
  18920. +#ifdef CONFIG_AUFS_HNOTIFY
  18921. +#ifdef CONFIG_AUFS_HFSNOTIFY
  18922. + /* never use fsnotify_add_vfsmount_mark() */
  18923. + struct fsnotify_mark hn_mark;
  18924. +#endif
  18925. + struct inode *hn_aufs_inode; /* no get/put */
  18926. + struct rcu_head rcu;
  18927. +#endif
  18928. +} ____cacheline_aligned_in_smp;
  18929. +
  18930. +struct au_hinode {
  18931. + struct inode *hi_inode;
  18932. + aufs_bindex_t hi_id;
  18933. +#ifdef CONFIG_AUFS_HNOTIFY
  18934. + struct au_hnotify *hi_notify;
  18935. +#endif
  18936. +
  18937. + /* reference to the copied-up whiteout with get/put */
  18938. + struct dentry *hi_whdentry;
  18939. +};
  18940. +
  18941. +/* ig_flags */
  18942. +#define AuIG_HALF_REFRESHED 1
  18943. +#define au_ig_ftest(flags, name) ((flags) & AuIG_##name)
  18944. +#define au_ig_fset(flags, name) \
  18945. + do { (flags) |= AuIG_##name; } while (0)
  18946. +#define au_ig_fclr(flags, name) \
  18947. + do { (flags) &= ~AuIG_##name; } while (0)
  18948. +
  18949. +struct au_iigen {
  18950. + spinlock_t ig_spin;
  18951. + __u32 ig_generation, ig_flags;
  18952. +};
  18953. +
  18954. +struct au_vdir;
  18955. +struct au_iinfo {
  18956. + struct au_iigen ii_generation;
  18957. + struct super_block *ii_hsb1; /* no get/put */
  18958. +
  18959. + struct au_rwsem ii_rwsem;
  18960. + aufs_bindex_t ii_btop, ii_bbot;
  18961. + __u32 ii_higen;
  18962. + struct au_hinode *ii_hinode;
  18963. + struct au_vdir *ii_vdir;
  18964. +};
  18965. +
  18966. +struct au_icntnr {
  18967. + struct au_iinfo iinfo;
  18968. + struct inode vfs_inode;
  18969. + struct hlist_bl_node plink;
  18970. + struct rcu_head rcu;
  18971. +} ____cacheline_aligned_in_smp;
  18972. +
  18973. +/* au_pin flags */
  18974. +#define AuPin_DI_LOCKED 1
  18975. +#define AuPin_MNT_WRITE (1 << 1)
  18976. +#define au_ftest_pin(flags, name) ((flags) & AuPin_##name)
  18977. +#define au_fset_pin(flags, name) \
  18978. + do { (flags) |= AuPin_##name; } while (0)
  18979. +#define au_fclr_pin(flags, name) \
  18980. + do { (flags) &= ~AuPin_##name; } while (0)
  18981. +
  18982. +struct au_pin {
  18983. + /* input */
  18984. + struct dentry *dentry;
  18985. + unsigned int udba;
  18986. + unsigned char lsc_di, lsc_hi, flags;
  18987. + aufs_bindex_t bindex;
  18988. +
  18989. + /* output */
  18990. + struct dentry *parent;
  18991. + struct au_hinode *hdir;
  18992. + struct vfsmount *h_mnt;
  18993. +
  18994. + /* temporary unlock/relock for copyup */
  18995. + struct dentry *h_dentry, *h_parent;
  18996. + struct au_branch *br;
  18997. + struct task_struct *task;
  18998. +};
  18999. +
  19000. +void au_pin_hdir_unlock(struct au_pin *p);
  19001. +int au_pin_hdir_lock(struct au_pin *p);
  19002. +int au_pin_hdir_relock(struct au_pin *p);
  19003. +void au_pin_hdir_acquire_nest(struct au_pin *p);
  19004. +void au_pin_hdir_release(struct au_pin *p);
  19005. +
  19006. +/* ---------------------------------------------------------------------- */
  19007. +
  19008. +static inline struct au_iinfo *au_ii(struct inode *inode)
  19009. +{
  19010. + BUG_ON(is_bad_inode(inode));
  19011. + return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
  19012. +}
  19013. +
  19014. +/* ---------------------------------------------------------------------- */
  19015. +
  19016. +/* inode.c */
  19017. +struct inode *au_igrab(struct inode *inode);
  19018. +void au_refresh_iop(struct inode *inode, int force_getattr);
  19019. +int au_refresh_hinode_self(struct inode *inode);
  19020. +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
  19021. +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  19022. + unsigned int d_type, ino_t *ino);
  19023. +struct inode *au_new_inode(struct dentry *dentry, int must_new);
  19024. +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
  19025. + struct inode *inode);
  19026. +int au_test_h_perm(struct user_namespace *h_userns, struct inode *h_inode,
  19027. + int mask);
  19028. +int au_test_h_perm_sio(struct user_namespace *h_userns, struct inode *h_inode,
  19029. + int mask);
  19030. +
  19031. +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
  19032. + ino_t h_ino, unsigned int d_type, ino_t *ino)
  19033. +{
  19034. +#ifdef CONFIG_AUFS_SHWH
  19035. + return au_ino(sb, bindex, h_ino, d_type, ino);
  19036. +#else
  19037. + return 0;
  19038. +#endif
  19039. +}
  19040. +
  19041. +/* i_op.c */
  19042. +enum {
  19043. + AuIop_SYMLINK,
  19044. + AuIop_DIR,
  19045. + AuIop_OTHER,
  19046. + AuIop_Last
  19047. +};
  19048. +extern struct inode_operations aufs_iop[AuIop_Last], /* not const */
  19049. + aufs_iop_nogetattr[AuIop_Last];
  19050. +
  19051. +/* au_wr_dir flags */
  19052. +#define AuWrDir_ADD_ENTRY 1
  19053. +#define AuWrDir_ISDIR (1 << 1)
  19054. +#define AuWrDir_TMPFILE (1 << 2)
  19055. +#define au_ftest_wrdir(flags, name) ((flags) & AuWrDir_##name)
  19056. +#define au_fset_wrdir(flags, name) \
  19057. + do { (flags) |= AuWrDir_##name; } while (0)
  19058. +#define au_fclr_wrdir(flags, name) \
  19059. + do { (flags) &= ~AuWrDir_##name; } while (0)
  19060. +
  19061. +struct au_wr_dir_args {
  19062. + aufs_bindex_t force_btgt;
  19063. + unsigned char flags;
  19064. +};
  19065. +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
  19066. + struct au_wr_dir_args *args);
  19067. +
  19068. +struct dentry *au_pinned_h_parent(struct au_pin *pin);
  19069. +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
  19070. + aufs_bindex_t bindex, int lsc_di, int lsc_hi,
  19071. + unsigned int udba, unsigned char flags);
  19072. +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
  19073. + unsigned int udba, unsigned char flags) __must_check;
  19074. +int au_do_pin(struct au_pin *pin) __must_check;
  19075. +void au_unpin(struct au_pin *pin);
  19076. +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
  19077. +
  19078. +#define AuIcpup_DID_CPUP 1
  19079. +#define au_ftest_icpup(flags, name) ((flags) & AuIcpup_##name)
  19080. +#define au_fset_icpup(flags, name) \
  19081. + do { (flags) |= AuIcpup_##name; } while (0)
  19082. +#define au_fclr_icpup(flags, name) \
  19083. + do { (flags) &= ~AuIcpup_##name; } while (0)
  19084. +
  19085. +struct au_icpup_args {
  19086. + unsigned char flags;
  19087. + unsigned char pin_flags;
  19088. + aufs_bindex_t btgt;
  19089. + unsigned int udba;
  19090. + struct au_pin pin;
  19091. + struct path h_path;
  19092. + struct inode *h_inode;
  19093. +};
  19094. +
  19095. +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
  19096. + struct au_icpup_args *a);
  19097. +
  19098. +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
  19099. + struct path *h_path, int locked);
  19100. +
  19101. +/* i_op_add.c */
  19102. +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
  19103. + struct dentry *h_parent, int isdir);
  19104. +int aufs_mknod(struct user_namespace *userns, struct inode *dir,
  19105. + struct dentry *dentry, umode_t mode, dev_t dev);
  19106. +int aufs_symlink(struct user_namespace *userns, struct inode *dir,
  19107. + struct dentry *dentry, const char *symname);
  19108. +int aufs_create(struct user_namespace *userns, struct inode *dir,
  19109. + struct dentry *dentry, umode_t mode, bool want_excl);
  19110. +struct vfsub_aopen_args;
  19111. +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
  19112. + struct vfsub_aopen_args *args);
  19113. +int aufs_tmpfile(struct user_namespace *userns, struct inode *dir,
  19114. + struct dentry *dentry, umode_t mode);
  19115. +int aufs_link(struct dentry *src_dentry, struct inode *dir,
  19116. + struct dentry *dentry);
  19117. +int aufs_mkdir(struct user_namespace *userns, struct inode *dir,
  19118. + struct dentry *dentry, umode_t mode);
  19119. +
  19120. +/* i_op_del.c */
  19121. +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
  19122. +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
  19123. + struct dentry *h_parent, int isdir);
  19124. +int aufs_unlink(struct inode *dir, struct dentry *dentry);
  19125. +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
  19126. +
  19127. +/* i_op_ren.c */
  19128. +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
  19129. +int aufs_rename(struct user_namespace *userns,
  19130. + struct inode *_src_dir, struct dentry *_src_dentry,
  19131. + struct inode *_dst_dir, struct dentry *_dst_dentry,
  19132. + unsigned int _flags);
  19133. +
  19134. +/* iinfo.c */
  19135. +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
  19136. +void au_hiput(struct au_hinode *hinode);
  19137. +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
  19138. + struct dentry *h_wh);
  19139. +unsigned int au_hi_flags(struct inode *inode, int isdir);
  19140. +
  19141. +/* hinode flags */
  19142. +#define AuHi_XINO 1
  19143. +#define AuHi_HNOTIFY (1 << 1)
  19144. +#define au_ftest_hi(flags, name) ((flags) & AuHi_##name)
  19145. +#define au_fset_hi(flags, name) \
  19146. + do { (flags) |= AuHi_##name; } while (0)
  19147. +#define au_fclr_hi(flags, name) \
  19148. + do { (flags) &= ~AuHi_##name; } while (0)
  19149. +
  19150. +#ifndef CONFIG_AUFS_HNOTIFY
  19151. +#undef AuHi_HNOTIFY
  19152. +#define AuHi_HNOTIFY 0
  19153. +#endif
  19154. +
  19155. +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
  19156. + struct inode *h_inode, unsigned int flags);
  19157. +
  19158. +void au_update_iigen(struct inode *inode, int half);
  19159. +void au_update_ibrange(struct inode *inode, int do_put_zero);
  19160. +
  19161. +void au_icntnr_init_once(void *_c);
  19162. +void au_hinode_init(struct au_hinode *hinode);
  19163. +int au_iinfo_init(struct inode *inode);
  19164. +void au_iinfo_fin(struct inode *inode);
  19165. +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
  19166. +
  19167. +#ifdef CONFIG_PROC_FS
  19168. +/* plink.c */
  19169. +int au_plink_maint(struct super_block *sb, int flags);
  19170. +struct au_sbinfo;
  19171. +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
  19172. +int au_plink_maint_enter(struct super_block *sb);
  19173. +#ifdef CONFIG_AUFS_DEBUG
  19174. +void au_plink_list(struct super_block *sb);
  19175. +#else
  19176. +AuStubVoid(au_plink_list, struct super_block *sb)
  19177. +#endif
  19178. +int au_plink_test(struct inode *inode);
  19179. +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
  19180. +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
  19181. + struct dentry *h_dentry);
  19182. +void au_plink_put(struct super_block *sb, int verbose);
  19183. +void au_plink_clean(struct super_block *sb, int verbose);
  19184. +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
  19185. +#else
  19186. +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
  19187. +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
  19188. +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
  19189. +AuStubVoid(au_plink_list, struct super_block *sb);
  19190. +AuStubInt0(au_plink_test, struct inode *inode);
  19191. +AuStub(struct dentry *, au_plink_lkup, return NULL,
  19192. + struct inode *inode, aufs_bindex_t bindex);
  19193. +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
  19194. + struct dentry *h_dentry);
  19195. +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
  19196. +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
  19197. +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
  19198. +#endif /* CONFIG_PROC_FS */
  19199. +
  19200. +#ifdef CONFIG_AUFS_XATTR
  19201. +/* xattr.c */
  19202. +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
  19203. + unsigned int verbose);
  19204. +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
  19205. +void au_xattr_init(struct super_block *sb);
  19206. +#else
  19207. +AuStubInt0(au_cpup_xattr, struct path *h_dst, struct path *h_src,
  19208. + int ignore_flags, unsigned int verbose);
  19209. +AuStubVoid(au_xattr_init, struct super_block *sb);
  19210. +#endif
  19211. +
  19212. +#ifdef CONFIG_FS_POSIX_ACL
  19213. +struct posix_acl *aufs_get_acl(struct inode *inode, int type, bool rcu);
  19214. +int aufs_set_acl(struct user_namespace *userns, struct inode *inode,
  19215. + struct posix_acl *acl, int type);
  19216. +#endif
  19217. +
  19218. +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
  19219. +enum {
  19220. + AU_XATTR_SET,
  19221. + AU_ACL_SET
  19222. +};
  19223. +
  19224. +struct au_sxattr {
  19225. + int type;
  19226. + union {
  19227. + struct {
  19228. + const char *name;
  19229. + const void *value;
  19230. + size_t size;
  19231. + int flags;
  19232. + } set;
  19233. + struct {
  19234. + struct posix_acl *acl;
  19235. + int type;
  19236. + } acl_set;
  19237. + } u;
  19238. +};
  19239. +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
  19240. + struct au_sxattr *arg);
  19241. +#endif
  19242. +
  19243. +/* ---------------------------------------------------------------------- */
  19244. +
  19245. +/* lock subclass for iinfo */
  19246. +enum {
  19247. + AuLsc_II_CHILD, /* child first */
  19248. + AuLsc_II_CHILD2, /* rename(2), link(2), and cpup at hnotify */
  19249. + AuLsc_II_CHILD3, /* copyup dirs */
  19250. + AuLsc_II_PARENT, /* see AuLsc_I_PARENT in vfsub.h */
  19251. + AuLsc_II_PARENT2,
  19252. + AuLsc_II_PARENT3, /* copyup dirs */
  19253. + AuLsc_II_NEW_CHILD
  19254. +};
  19255. +
  19256. +/*
  19257. + * ii_read_lock_child, ii_write_lock_child,
  19258. + * ii_read_lock_child2, ii_write_lock_child2,
  19259. + * ii_read_lock_child3, ii_write_lock_child3,
  19260. + * ii_read_lock_parent, ii_write_lock_parent,
  19261. + * ii_read_lock_parent2, ii_write_lock_parent2,
  19262. + * ii_read_lock_parent3, ii_write_lock_parent3,
  19263. + * ii_read_lock_new_child, ii_write_lock_new_child,
  19264. + */
  19265. +#define AuReadLockFunc(name, lsc) \
  19266. +static inline void ii_read_lock_##name(struct inode *i) \
  19267. +{ \
  19268. + au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
  19269. +}
  19270. +
  19271. +#define AuWriteLockFunc(name, lsc) \
  19272. +static inline void ii_write_lock_##name(struct inode *i) \
  19273. +{ \
  19274. + au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
  19275. +}
  19276. +
  19277. +#define AuRWLockFuncs(name, lsc) \
  19278. + AuReadLockFunc(name, lsc) \
  19279. + AuWriteLockFunc(name, lsc)
  19280. +
  19281. +AuRWLockFuncs(child, CHILD);
  19282. +AuRWLockFuncs(child2, CHILD2);
  19283. +AuRWLockFuncs(child3, CHILD3);
  19284. +AuRWLockFuncs(parent, PARENT);
  19285. +AuRWLockFuncs(parent2, PARENT2);
  19286. +AuRWLockFuncs(parent3, PARENT3);
  19287. +AuRWLockFuncs(new_child, NEW_CHILD);
  19288. +
  19289. +#undef AuReadLockFunc
  19290. +#undef AuWriteLockFunc
  19291. +#undef AuRWLockFuncs
  19292. +
  19293. +#define ii_read_unlock(i) au_rw_read_unlock(&au_ii(i)->ii_rwsem)
  19294. +#define ii_write_unlock(i) au_rw_write_unlock(&au_ii(i)->ii_rwsem)
  19295. +#define ii_downgrade_lock(i) au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
  19296. +
  19297. +#define IiMustNoWaiters(i) AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
  19298. +#define IiMustAnyLock(i) AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
  19299. +#define IiMustWriteLock(i) AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
  19300. +
  19301. +/* ---------------------------------------------------------------------- */
  19302. +
  19303. +static inline void au_icntnr_init(struct au_icntnr *c)
  19304. +{
  19305. +#ifdef CONFIG_AUFS_DEBUG
  19306. + c->vfs_inode.i_mode = 0;
  19307. +#endif
  19308. +}
  19309. +
  19310. +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
  19311. +{
  19312. + unsigned int gen;
  19313. + struct au_iinfo *iinfo;
  19314. + struct au_iigen *iigen;
  19315. +
  19316. + iinfo = au_ii(inode);
  19317. + iigen = &iinfo->ii_generation;
  19318. + spin_lock(&iigen->ig_spin);
  19319. + if (igflags)
  19320. + *igflags = iigen->ig_flags;
  19321. + gen = iigen->ig_generation;
  19322. + spin_unlock(&iigen->ig_spin);
  19323. +
  19324. + return gen;
  19325. +}
  19326. +
  19327. +/* tiny test for inode number */
  19328. +/* tmpfs generation is too rough */
  19329. +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
  19330. +{
  19331. + struct au_iinfo *iinfo;
  19332. +
  19333. + iinfo = au_ii(inode);
  19334. + AuRwMustAnyLock(&iinfo->ii_rwsem);
  19335. + return !(iinfo->ii_hsb1 == h_inode->i_sb
  19336. + && iinfo->ii_higen == h_inode->i_generation);
  19337. +}
  19338. +
  19339. +static inline void au_iigen_dec(struct inode *inode)
  19340. +{
  19341. + struct au_iinfo *iinfo;
  19342. + struct au_iigen *iigen;
  19343. +
  19344. + iinfo = au_ii(inode);
  19345. + iigen = &iinfo->ii_generation;
  19346. + spin_lock(&iigen->ig_spin);
  19347. + iigen->ig_generation--;
  19348. + spin_unlock(&iigen->ig_spin);
  19349. +}
  19350. +
  19351. +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
  19352. +{
  19353. + int err;
  19354. +
  19355. + err = 0;
  19356. + if (unlikely(inode && au_iigen(inode, NULL) != sigen))
  19357. + err = -EIO;
  19358. +
  19359. + return err;
  19360. +}
  19361. +
  19362. +/* ---------------------------------------------------------------------- */
  19363. +
  19364. +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
  19365. + aufs_bindex_t bindex)
  19366. +{
  19367. + return iinfo->ii_hinode + bindex;
  19368. +}
  19369. +
  19370. +static inline int au_is_bad_inode(struct inode *inode)
  19371. +{
  19372. + return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
  19373. +}
  19374. +
  19375. +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
  19376. + aufs_bindex_t bindex)
  19377. +{
  19378. + IiMustAnyLock(inode);
  19379. + return au_hinode(au_ii(inode), bindex)->hi_id;
  19380. +}
  19381. +
  19382. +static inline aufs_bindex_t au_ibtop(struct inode *inode)
  19383. +{
  19384. + IiMustAnyLock(inode);
  19385. + return au_ii(inode)->ii_btop;
  19386. +}
  19387. +
  19388. +static inline aufs_bindex_t au_ibbot(struct inode *inode)
  19389. +{
  19390. + IiMustAnyLock(inode);
  19391. + return au_ii(inode)->ii_bbot;
  19392. +}
  19393. +
  19394. +static inline struct au_vdir *au_ivdir(struct inode *inode)
  19395. +{
  19396. + IiMustAnyLock(inode);
  19397. + return au_ii(inode)->ii_vdir;
  19398. +}
  19399. +
  19400. +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
  19401. +{
  19402. + IiMustAnyLock(inode);
  19403. + return au_hinode(au_ii(inode), bindex)->hi_whdentry;
  19404. +}
  19405. +
  19406. +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
  19407. +{
  19408. + IiMustWriteLock(inode);
  19409. + au_ii(inode)->ii_btop = bindex;
  19410. +}
  19411. +
  19412. +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
  19413. +{
  19414. + IiMustWriteLock(inode);
  19415. + au_ii(inode)->ii_bbot = bindex;
  19416. +}
  19417. +
  19418. +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
  19419. +{
  19420. + IiMustWriteLock(inode);
  19421. + au_ii(inode)->ii_vdir = vdir;
  19422. +}
  19423. +
  19424. +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
  19425. +{
  19426. + IiMustAnyLock(inode);
  19427. + return au_hinode(au_ii(inode), bindex);
  19428. +}
  19429. +
  19430. +/* ---------------------------------------------------------------------- */
  19431. +
  19432. +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
  19433. +{
  19434. + if (pin)
  19435. + return pin->parent;
  19436. + return NULL;
  19437. +}
  19438. +
  19439. +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
  19440. +{
  19441. + if (pin && pin->hdir)
  19442. + return pin->hdir->hi_inode;
  19443. + return NULL;
  19444. +}
  19445. +
  19446. +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
  19447. +{
  19448. + if (pin)
  19449. + return pin->hdir;
  19450. + return NULL;
  19451. +}
  19452. +
  19453. +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
  19454. +{
  19455. + if (pin)
  19456. + pin->dentry = dentry;
  19457. +}
  19458. +
  19459. +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
  19460. + unsigned char lflag)
  19461. +{
  19462. + if (pin) {
  19463. + if (lflag)
  19464. + au_fset_pin(pin->flags, DI_LOCKED);
  19465. + else
  19466. + au_fclr_pin(pin->flags, DI_LOCKED);
  19467. + }
  19468. +}
  19469. +
  19470. +#if 0 /* reserved */
  19471. +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
  19472. +{
  19473. + if (pin) {
  19474. + dput(pin->parent);
  19475. + pin->parent = dget(parent);
  19476. + }
  19477. +}
  19478. +#endif
  19479. +
  19480. +/* ---------------------------------------------------------------------- */
  19481. +
  19482. +struct au_branch;
  19483. +#ifdef CONFIG_AUFS_HNOTIFY
  19484. +struct au_hnotify_op {
  19485. + void (*ctl)(struct au_hinode *hinode, int do_set);
  19486. + int (*alloc)(struct au_hinode *hinode);
  19487. +
  19488. + /*
  19489. + * if it returns true, the caller should free hinode->hi_notify,
  19490. + * otherwise ->free() frees it.
  19491. + */
  19492. + int (*free)(struct au_hinode *hinode,
  19493. + struct au_hnotify *hn) __must_check;
  19494. +
  19495. + void (*fin)(void);
  19496. + int (*init)(void);
  19497. +
  19498. + int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
  19499. + void (*fin_br)(struct au_branch *br);
  19500. + int (*init_br)(struct au_branch *br, int perm);
  19501. +};
  19502. +
  19503. +/* hnotify.c */
  19504. +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
  19505. +void au_hn_free(struct au_hinode *hinode);
  19506. +void au_hn_ctl(struct au_hinode *hinode, int do_set);
  19507. +void au_hn_reset(struct inode *inode, unsigned int flags);
  19508. +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
  19509. + const struct qstr *h_child_qstr, struct inode *h_child_inode);
  19510. +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
  19511. +int au_hnotify_init_br(struct au_branch *br, int perm);
  19512. +void au_hnotify_fin_br(struct au_branch *br);
  19513. +int __init au_hnotify_init(void);
  19514. +void au_hnotify_fin(void);
  19515. +
  19516. +/* hfsnotify.c */
  19517. +extern const struct au_hnotify_op au_hnotify_op;
  19518. +
  19519. +static inline
  19520. +void au_hn_init(struct au_hinode *hinode)
  19521. +{
  19522. + hinode->hi_notify = NULL;
  19523. +}
  19524. +
  19525. +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
  19526. +{
  19527. + return hinode->hi_notify;
  19528. +}
  19529. +
  19530. +#else
  19531. +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
  19532. + struct au_hinode *hinode __maybe_unused,
  19533. + struct inode *inode __maybe_unused)
  19534. +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
  19535. +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
  19536. +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
  19537. + int do_set __maybe_unused)
  19538. +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
  19539. + unsigned int flags __maybe_unused)
  19540. +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
  19541. + struct au_branch *br __maybe_unused,
  19542. + int perm __maybe_unused)
  19543. +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
  19544. + int perm __maybe_unused)
  19545. +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
  19546. +AuStubInt0(__init au_hnotify_init, void)
  19547. +AuStubVoid(au_hnotify_fin, void)
  19548. +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
  19549. +#endif /* CONFIG_AUFS_HNOTIFY */
  19550. +
  19551. +static inline void au_hn_suspend(struct au_hinode *hdir)
  19552. +{
  19553. + au_hn_ctl(hdir, /*do_set*/0);
  19554. +}
  19555. +
  19556. +static inline void au_hn_resume(struct au_hinode *hdir)
  19557. +{
  19558. + au_hn_ctl(hdir, /*do_set*/1);
  19559. +}
  19560. +
  19561. +static inline void au_hn_inode_lock(struct au_hinode *hdir)
  19562. +{
  19563. + inode_lock(hdir->hi_inode);
  19564. + au_hn_suspend(hdir);
  19565. +}
  19566. +
  19567. +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
  19568. + unsigned int sc __maybe_unused)
  19569. +{
  19570. + inode_lock_nested(hdir->hi_inode, sc);
  19571. + au_hn_suspend(hdir);
  19572. +}
  19573. +
  19574. +#if 0 /* unused */
  19575. +#include "vfsub.h"
  19576. +static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
  19577. + unsigned int sc)
  19578. +{
  19579. + inode_lock_shared_nested(hdir->hi_inode, sc);
  19580. + au_hn_suspend(hdir);
  19581. +}
  19582. +#endif
  19583. +
  19584. +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
  19585. +{
  19586. + au_hn_resume(hdir);
  19587. + inode_unlock(hdir->hi_inode);
  19588. +}
  19589. +
  19590. +#endif /* __KERNEL__ */
  19591. +#endif /* __AUFS_INODE_H__ */
  19592. diff -Naur null/fs/aufs/ioctl.c linux-5.15.131/fs/aufs/ioctl.c
  19593. --- /dev/null
  19594. +++ linux-5.15.131/fs/aufs/ioctl.c 2023-09-11 14:32:48.150722527 +0300
  19595. @@ -0,0 +1,220 @@
  19596. +// SPDX-License-Identifier: GPL-2.0
  19597. +/*
  19598. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  19599. + *
  19600. + * This program, aufs is free software; you can redistribute it and/or modify
  19601. + * it under the terms of the GNU General Public License as published by
  19602. + * the Free Software Foundation; either version 2 of the License, or
  19603. + * (at your option) any later version.
  19604. + *
  19605. + * This program is distributed in the hope that it will be useful,
  19606. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19607. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19608. + * GNU General Public License for more details.
  19609. + *
  19610. + * You should have received a copy of the GNU General Public License
  19611. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19612. + */
  19613. +
  19614. +/*
  19615. + * ioctl
  19616. + * plink-management and readdir in userspace.
  19617. + * assist the pathconf(3) wrapper library.
  19618. + * move-down
  19619. + * File-based Hierarchical Storage Management.
  19620. + */
  19621. +
  19622. +#include <linux/compat.h>
  19623. +#include <linux/file.h>
  19624. +#include "aufs.h"
  19625. +
  19626. +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
  19627. +{
  19628. + int err, fd;
  19629. + aufs_bindex_t wbi, bindex, bbot;
  19630. + struct file *h_file;
  19631. + struct super_block *sb;
  19632. + struct dentry *root;
  19633. + struct au_branch *br;
  19634. + struct aufs_wbr_fd wbrfd = {
  19635. + .oflags = au_dir_roflags,
  19636. + .brid = -1
  19637. + };
  19638. + const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
  19639. + | O_NOATIME | O_CLOEXEC;
  19640. +
  19641. + AuDebugOn(wbrfd.oflags & ~valid);
  19642. +
  19643. + if (arg) {
  19644. + err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
  19645. + if (unlikely(err)) {
  19646. + err = -EFAULT;
  19647. + goto out;
  19648. + }
  19649. +
  19650. + err = -EINVAL;
  19651. + AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
  19652. + wbrfd.oflags |= au_dir_roflags;
  19653. + AuDbg("0%o\n", wbrfd.oflags);
  19654. + if (unlikely(wbrfd.oflags & ~valid))
  19655. + goto out;
  19656. + }
  19657. +
  19658. + fd = get_unused_fd_flags(0);
  19659. + err = fd;
  19660. + if (unlikely(fd < 0))
  19661. + goto out;
  19662. +
  19663. + h_file = ERR_PTR(-EINVAL);
  19664. + wbi = 0;
  19665. + br = NULL;
  19666. + sb = path->dentry->d_sb;
  19667. + root = sb->s_root;
  19668. + aufs_read_lock(root, AuLock_IR);
  19669. + bbot = au_sbbot(sb);
  19670. + if (wbrfd.brid >= 0) {
  19671. + wbi = au_br_index(sb, wbrfd.brid);
  19672. + if (unlikely(wbi < 0 || wbi > bbot))
  19673. + goto out_unlock;
  19674. + }
  19675. +
  19676. + h_file = ERR_PTR(-ENOENT);
  19677. + br = au_sbr(sb, wbi);
  19678. + if (!au_br_writable(br->br_perm)) {
  19679. + if (arg)
  19680. + goto out_unlock;
  19681. +
  19682. + bindex = wbi + 1;
  19683. + wbi = -1;
  19684. + for (; bindex <= bbot; bindex++) {
  19685. + br = au_sbr(sb, bindex);
  19686. + if (au_br_writable(br->br_perm)) {
  19687. + wbi = bindex;
  19688. + br = au_sbr(sb, wbi);
  19689. + break;
  19690. + }
  19691. + }
  19692. + }
  19693. + AuDbg("wbi %d\n", wbi);
  19694. + if (wbi >= 0)
  19695. + h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
  19696. + /*force_wr*/0);
  19697. +
  19698. +out_unlock:
  19699. + aufs_read_unlock(root, AuLock_IR);
  19700. + err = PTR_ERR(h_file);
  19701. + if (IS_ERR(h_file))
  19702. + goto out_fd;
  19703. +
  19704. + au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
  19705. + fd_install(fd, h_file);
  19706. + err = fd;
  19707. + goto out; /* success */
  19708. +
  19709. +out_fd:
  19710. + put_unused_fd(fd);
  19711. +out:
  19712. + AuTraceErr(err);
  19713. + return err;
  19714. +}
  19715. +
  19716. +/* ---------------------------------------------------------------------- */
  19717. +
  19718. +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
  19719. +{
  19720. + long err;
  19721. + struct dentry *dentry;
  19722. +
  19723. + switch (cmd) {
  19724. + case AUFS_CTL_RDU:
  19725. + case AUFS_CTL_RDU_INO:
  19726. + err = au_rdu_ioctl(file, cmd, arg);
  19727. + break;
  19728. +
  19729. + case AUFS_CTL_WBR_FD:
  19730. + err = au_wbr_fd(&file->f_path, (void __user *)arg);
  19731. + break;
  19732. +
  19733. + case AUFS_CTL_IBUSY:
  19734. + err = au_ibusy_ioctl(file, arg);
  19735. + break;
  19736. +
  19737. + case AUFS_CTL_BRINFO:
  19738. + err = au_brinfo_ioctl(file, arg);
  19739. + break;
  19740. +
  19741. + case AUFS_CTL_FHSM_FD:
  19742. + dentry = file->f_path.dentry;
  19743. + if (IS_ROOT(dentry))
  19744. + err = au_fhsm_fd(dentry->d_sb, arg);
  19745. + else
  19746. + err = -ENOTTY;
  19747. + break;
  19748. +
  19749. + default:
  19750. + /* do not call the lower */
  19751. + AuDbg("0x%x\n", cmd);
  19752. + err = -ENOTTY;
  19753. + }
  19754. +
  19755. + AuTraceErr(err);
  19756. + return err;
  19757. +}
  19758. +
  19759. +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
  19760. +{
  19761. + long err;
  19762. +
  19763. + switch (cmd) {
  19764. + case AUFS_CTL_MVDOWN:
  19765. + err = au_mvdown(file->f_path.dentry, (void __user *)arg);
  19766. + break;
  19767. +
  19768. + case AUFS_CTL_WBR_FD:
  19769. + err = au_wbr_fd(&file->f_path, (void __user *)arg);
  19770. + break;
  19771. +
  19772. + default:
  19773. + /* do not call the lower */
  19774. + AuDbg("0x%x\n", cmd);
  19775. + err = -ENOTTY;
  19776. + }
  19777. +
  19778. + AuTraceErr(err);
  19779. + return err;
  19780. +}
  19781. +
  19782. +#ifdef CONFIG_COMPAT
  19783. +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
  19784. + unsigned long arg)
  19785. +{
  19786. + long err;
  19787. +
  19788. + switch (cmd) {
  19789. + case AUFS_CTL_RDU:
  19790. + case AUFS_CTL_RDU_INO:
  19791. + err = au_rdu_compat_ioctl(file, cmd, arg);
  19792. + break;
  19793. +
  19794. + case AUFS_CTL_IBUSY:
  19795. + err = au_ibusy_compat_ioctl(file, arg);
  19796. + break;
  19797. +
  19798. + case AUFS_CTL_BRINFO:
  19799. + err = au_brinfo_compat_ioctl(file, arg);
  19800. + break;
  19801. +
  19802. + default:
  19803. + err = aufs_ioctl_dir(file, cmd, arg);
  19804. + }
  19805. +
  19806. + AuTraceErr(err);
  19807. + return err;
  19808. +}
  19809. +
  19810. +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
  19811. + unsigned long arg)
  19812. +{
  19813. + return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
  19814. +}
  19815. +#endif
  19816. diff -Naur null/fs/aufs/i_op_add.c linux-5.15.131/fs/aufs/i_op_add.c
  19817. --- /dev/null
  19818. +++ linux-5.15.131/fs/aufs/i_op_add.c 2023-09-11 14:32:48.149722539 +0300
  19819. @@ -0,0 +1,941 @@
  19820. +// SPDX-License-Identifier: GPL-2.0
  19821. +/*
  19822. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  19823. + *
  19824. + * This program, aufs is free software; you can redistribute it and/or modify
  19825. + * it under the terms of the GNU General Public License as published by
  19826. + * the Free Software Foundation; either version 2 of the License, or
  19827. + * (at your option) any later version.
  19828. + *
  19829. + * This program is distributed in the hope that it will be useful,
  19830. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19831. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19832. + * GNU General Public License for more details.
  19833. + *
  19834. + * You should have received a copy of the GNU General Public License
  19835. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19836. + */
  19837. +
  19838. +/*
  19839. + * inode operations (add entry)
  19840. + */
  19841. +
  19842. +#include <linux/iversion.h>
  19843. +#include "aufs.h"
  19844. +
  19845. +/*
  19846. + * final procedure of adding a new entry, except link(2).
  19847. + * remove whiteout, instantiate, copyup the parent dir's times and size
  19848. + * and update version.
  19849. + * if it failed, re-create the removed whiteout.
  19850. + */
  19851. +static int epilog(struct inode *dir, aufs_bindex_t bindex,
  19852. + struct dentry *wh_dentry, struct dentry *dentry)
  19853. +{
  19854. + int err, rerr;
  19855. + aufs_bindex_t bwh;
  19856. + struct path h_path;
  19857. + struct super_block *sb;
  19858. + struct inode *inode, *h_dir;
  19859. + struct dentry *wh;
  19860. +
  19861. + bwh = -1;
  19862. + sb = dir->i_sb;
  19863. + if (wh_dentry) {
  19864. + h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
  19865. + IMustLock(h_dir);
  19866. + AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
  19867. + bwh = au_dbwh(dentry);
  19868. + h_path.dentry = wh_dentry;
  19869. + h_path.mnt = au_sbr_mnt(sb, bindex);
  19870. + err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
  19871. + dentry);
  19872. + if (unlikely(err))
  19873. + goto out;
  19874. + }
  19875. +
  19876. + inode = au_new_inode(dentry, /*must_new*/1);
  19877. + if (!IS_ERR(inode)) {
  19878. + d_instantiate(dentry, inode);
  19879. + dir = d_inode(dentry->d_parent); /* dir inode is locked */
  19880. + IMustLock(dir);
  19881. + au_dir_ts(dir, bindex);
  19882. + inode_inc_iversion(dir);
  19883. + au_fhsm_wrote(sb, bindex, /*force*/0);
  19884. + return 0; /* success */
  19885. + }
  19886. +
  19887. + err = PTR_ERR(inode);
  19888. + if (!wh_dentry)
  19889. + goto out;
  19890. +
  19891. + /* revert */
  19892. + /* dir inode is locked */
  19893. + wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
  19894. + rerr = PTR_ERR(wh);
  19895. + if (IS_ERR(wh)) {
  19896. + AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
  19897. + dentry, err, rerr);
  19898. + err = -EIO;
  19899. + } else
  19900. + dput(wh);
  19901. +
  19902. +out:
  19903. + return err;
  19904. +}
  19905. +
  19906. +static int au_d_may_add(struct dentry *dentry)
  19907. +{
  19908. + int err;
  19909. +
  19910. + err = 0;
  19911. + if (unlikely(d_unhashed(dentry)))
  19912. + err = -ENOENT;
  19913. + if (unlikely(d_really_is_positive(dentry)))
  19914. + err = -EEXIST;
  19915. + return err;
  19916. +}
  19917. +
  19918. +/*
  19919. + * simple tests for the adding inode operations.
  19920. + * following the checks in vfs, plus the parent-child relationship.
  19921. + */
  19922. +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
  19923. + struct dentry *h_parent, int isdir)
  19924. +{
  19925. + int err;
  19926. + umode_t h_mode;
  19927. + struct dentry *h_dentry;
  19928. + struct inode *h_inode;
  19929. +
  19930. + err = -ENAMETOOLONG;
  19931. + if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
  19932. + goto out;
  19933. +
  19934. + h_dentry = au_h_dptr(dentry, bindex);
  19935. + if (d_really_is_negative(dentry)) {
  19936. + err = -EEXIST;
  19937. + if (unlikely(d_is_positive(h_dentry)))
  19938. + goto out;
  19939. + } else {
  19940. + /* rename(2) case */
  19941. + err = -EIO;
  19942. + if (unlikely(d_is_negative(h_dentry)))
  19943. + goto out;
  19944. + h_inode = d_inode(h_dentry);
  19945. + if (unlikely(!h_inode->i_nlink))
  19946. + goto out;
  19947. +
  19948. + h_mode = h_inode->i_mode;
  19949. + if (!isdir) {
  19950. + err = -EISDIR;
  19951. + if (unlikely(S_ISDIR(h_mode)))
  19952. + goto out;
  19953. + } else if (unlikely(!S_ISDIR(h_mode))) {
  19954. + err = -ENOTDIR;
  19955. + goto out;
  19956. + }
  19957. + }
  19958. +
  19959. + err = 0;
  19960. + /* expected parent dir is locked */
  19961. + if (unlikely(h_parent != h_dentry->d_parent))
  19962. + err = -EIO;
  19963. +
  19964. +out:
  19965. + AuTraceErr(err);
  19966. + return err;
  19967. +}
  19968. +
  19969. +/*
  19970. + * initial procedure of adding a new entry.
  19971. + * prepare writable branch and the parent dir, lock it,
  19972. + * and lookup whiteout for the new entry.
  19973. + */
  19974. +static struct dentry*
  19975. +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
  19976. + struct dentry *src_dentry, struct au_pin *pin,
  19977. + struct au_wr_dir_args *wr_dir_args)
  19978. +{
  19979. + struct dentry *wh_dentry, *h_parent;
  19980. + struct super_block *sb;
  19981. + struct au_branch *br;
  19982. + int err;
  19983. + unsigned int udba;
  19984. + aufs_bindex_t bcpup;
  19985. +
  19986. + AuDbg("%pd\n", dentry);
  19987. +
  19988. + err = au_wr_dir(dentry, src_dentry, wr_dir_args);
  19989. + bcpup = err;
  19990. + wh_dentry = ERR_PTR(err);
  19991. + if (unlikely(err < 0))
  19992. + goto out;
  19993. +
  19994. + sb = dentry->d_sb;
  19995. + udba = au_opt_udba(sb);
  19996. + err = au_pin(pin, dentry, bcpup, udba,
  19997. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  19998. + wh_dentry = ERR_PTR(err);
  19999. + if (unlikely(err))
  20000. + goto out;
  20001. +
  20002. + h_parent = au_pinned_h_parent(pin);
  20003. + if (udba != AuOpt_UDBA_NONE
  20004. + && au_dbtop(dentry) == bcpup)
  20005. + err = au_may_add(dentry, bcpup, h_parent,
  20006. + au_ftest_wrdir(wr_dir_args->flags, ISDIR));
  20007. + else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
  20008. + err = -ENAMETOOLONG;
  20009. + wh_dentry = ERR_PTR(err);
  20010. + if (unlikely(err))
  20011. + goto out_unpin;
  20012. +
  20013. + br = au_sbr(sb, bcpup);
  20014. + if (dt) {
  20015. + struct path tmp = {
  20016. + .dentry = h_parent,
  20017. + .mnt = au_br_mnt(br)
  20018. + };
  20019. + au_dtime_store(dt, au_pinned_parent(pin), &tmp);
  20020. + }
  20021. +
  20022. + wh_dentry = NULL;
  20023. + if (bcpup != au_dbwh(dentry))
  20024. + goto out; /* success */
  20025. +
  20026. + /*
  20027. + * ENAMETOOLONG here means that if we allowed create such name, then it
  20028. + * would not be able to removed in the future. So we don't allow such
  20029. + * name here and we don't handle ENAMETOOLONG differently here.
  20030. + */
  20031. + wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
  20032. +
  20033. +out_unpin:
  20034. + if (IS_ERR(wh_dentry))
  20035. + au_unpin(pin);
  20036. +out:
  20037. + return wh_dentry;
  20038. +}
  20039. +
  20040. +/* ---------------------------------------------------------------------- */
  20041. +
  20042. +enum { Mknod, Symlink, Creat };
  20043. +struct simple_arg {
  20044. + int type;
  20045. + union {
  20046. + struct {
  20047. + umode_t mode;
  20048. + bool want_excl;
  20049. + bool try_aopen;
  20050. + struct vfsub_aopen_args *aopen;
  20051. + } c;
  20052. + struct {
  20053. + const char *symname;
  20054. + } s;
  20055. + struct {
  20056. + umode_t mode;
  20057. + dev_t dev;
  20058. + } m;
  20059. + } u;
  20060. +};
  20061. +
  20062. +static int add_simple(struct inode *dir, struct dentry *dentry,
  20063. + struct simple_arg *arg)
  20064. +{
  20065. + int err, rerr;
  20066. + aufs_bindex_t btop;
  20067. + unsigned char created;
  20068. + const unsigned char try_aopen
  20069. + = (arg->type == Creat && arg->u.c.try_aopen);
  20070. + struct vfsub_aopen_args *aopen = arg->u.c.aopen;
  20071. + struct dentry *wh_dentry, *parent;
  20072. + struct inode *h_dir;
  20073. + struct super_block *sb;
  20074. + struct au_branch *br;
  20075. + /* to reduce stack size */
  20076. + struct {
  20077. + struct au_dtime dt;
  20078. + struct au_pin pin;
  20079. + struct path h_path;
  20080. + struct au_wr_dir_args wr_dir_args;
  20081. + } *a;
  20082. +
  20083. + AuDbg("%pd\n", dentry);
  20084. + IMustLock(dir);
  20085. +
  20086. + err = -ENOMEM;
  20087. + a = kmalloc(sizeof(*a), GFP_NOFS);
  20088. + if (unlikely(!a))
  20089. + goto out;
  20090. + a->wr_dir_args.force_btgt = -1;
  20091. + a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
  20092. +
  20093. + parent = dentry->d_parent; /* dir inode is locked */
  20094. + if (!try_aopen) {
  20095. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
  20096. + if (unlikely(err))
  20097. + goto out_free;
  20098. + }
  20099. + err = au_d_may_add(dentry);
  20100. + if (unlikely(err))
  20101. + goto out_unlock;
  20102. + if (!try_aopen)
  20103. + di_write_lock_parent(parent);
  20104. + wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
  20105. + &a->pin, &a->wr_dir_args);
  20106. + err = PTR_ERR(wh_dentry);
  20107. + if (IS_ERR(wh_dentry))
  20108. + goto out_parent;
  20109. +
  20110. + btop = au_dbtop(dentry);
  20111. + sb = dentry->d_sb;
  20112. + br = au_sbr(sb, btop);
  20113. + a->h_path.dentry = au_h_dptr(dentry, btop);
  20114. + a->h_path.mnt = au_br_mnt(br);
  20115. + h_dir = au_pinned_h_dir(&a->pin);
  20116. + switch (arg->type) {
  20117. + case Creat:
  20118. + if (!try_aopen || !h_dir->i_op->atomic_open) {
  20119. + err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
  20120. + arg->u.c.want_excl);
  20121. + created = !err;
  20122. + if (!err && try_aopen)
  20123. + aopen->file->f_mode |= FMODE_CREATED;
  20124. + } else {
  20125. + aopen->br = br;
  20126. + err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
  20127. + AuDbg("err %d\n", err);
  20128. + AuDbgFile(aopen->file);
  20129. + created = err >= 0
  20130. + && !!(aopen->file->f_mode & FMODE_CREATED);
  20131. + }
  20132. + break;
  20133. + case Symlink:
  20134. + err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
  20135. + created = !err;
  20136. + break;
  20137. + case Mknod:
  20138. + err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
  20139. + arg->u.m.dev);
  20140. + created = !err;
  20141. + break;
  20142. + default:
  20143. + BUG();
  20144. + }
  20145. + if (unlikely(err < 0))
  20146. + goto out_unpin;
  20147. +
  20148. + err = epilog(dir, btop, wh_dentry, dentry);
  20149. + if (!err)
  20150. + goto out_unpin; /* success */
  20151. +
  20152. + /* revert */
  20153. + if (created /* && d_is_positive(a->h_path.dentry) */) {
  20154. + /* no delegation since it is just created */
  20155. + rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
  20156. + /*force*/0);
  20157. + if (rerr) {
  20158. + AuIOErr("%pd revert failure(%d, %d)\n",
  20159. + dentry, err, rerr);
  20160. + err = -EIO;
  20161. + }
  20162. + au_dtime_revert(&a->dt);
  20163. + }
  20164. + if (try_aopen && h_dir->i_op->atomic_open
  20165. + && (aopen->file->f_mode & FMODE_OPENED))
  20166. + /* aopen->file is still opened */
  20167. + au_lcnt_dec(&aopen->br->br_nfiles);
  20168. +
  20169. +out_unpin:
  20170. + au_unpin(&a->pin);
  20171. + dput(wh_dentry);
  20172. +out_parent:
  20173. + if (!try_aopen)
  20174. + di_write_unlock(parent);
  20175. +out_unlock:
  20176. + if (unlikely(err)) {
  20177. + au_update_dbtop(dentry);
  20178. + d_drop(dentry);
  20179. + }
  20180. + if (!try_aopen)
  20181. + aufs_read_unlock(dentry, AuLock_DW);
  20182. +out_free:
  20183. + au_kfree_rcu(a);
  20184. +out:
  20185. + return err;
  20186. +}
  20187. +
  20188. +int aufs_mknod(struct user_namespace *userns, struct inode *dir,
  20189. + struct dentry *dentry, umode_t mode, dev_t dev)
  20190. +{
  20191. + struct simple_arg arg = {
  20192. + .type = Mknod,
  20193. + .u.m = {
  20194. + .mode = mode,
  20195. + .dev = dev
  20196. + }
  20197. + };
  20198. + return add_simple(dir, dentry, &arg);
  20199. +}
  20200. +
  20201. +int aufs_symlink(struct user_namespace *userns, struct inode *dir,
  20202. + struct dentry *dentry, const char *symname)
  20203. +{
  20204. + struct simple_arg arg = {
  20205. + .type = Symlink,
  20206. + .u.s.symname = symname
  20207. + };
  20208. + return add_simple(dir, dentry, &arg);
  20209. +}
  20210. +
  20211. +int aufs_create(struct user_namespace *userns, struct inode *dir,
  20212. + struct dentry *dentry, umode_t mode, bool want_excl)
  20213. +{
  20214. + struct simple_arg arg = {
  20215. + .type = Creat,
  20216. + .u.c = {
  20217. + .mode = mode,
  20218. + .want_excl = want_excl
  20219. + }
  20220. + };
  20221. + return add_simple(dir, dentry, &arg);
  20222. +}
  20223. +
  20224. +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
  20225. + struct vfsub_aopen_args *aopen_args)
  20226. +{
  20227. + struct simple_arg arg = {
  20228. + .type = Creat,
  20229. + .u.c = {
  20230. + .mode = aopen_args->create_mode,
  20231. + .want_excl = aopen_args->open_flag & O_EXCL,
  20232. + .try_aopen = true,
  20233. + .aopen = aopen_args
  20234. + }
  20235. + };
  20236. + return add_simple(dir, dentry, &arg);
  20237. +}
  20238. +
  20239. +int aufs_tmpfile(struct user_namespace *userns, struct inode *dir,
  20240. + struct dentry *dentry, umode_t mode)
  20241. +{
  20242. + int err;
  20243. + aufs_bindex_t bindex;
  20244. + struct super_block *sb;
  20245. + struct dentry *parent, *h_parent, *h_dentry;
  20246. + struct inode *h_dir, *inode;
  20247. + struct vfsmount *h_mnt;
  20248. + struct user_namespace *h_userns;
  20249. + struct au_wr_dir_args wr_dir_args = {
  20250. + .force_btgt = -1,
  20251. + .flags = AuWrDir_TMPFILE
  20252. + };
  20253. +
  20254. + /* copy-up may happen */
  20255. + inode_lock(dir);
  20256. +
  20257. + sb = dir->i_sb;
  20258. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  20259. + if (unlikely(err))
  20260. + goto out;
  20261. +
  20262. + err = au_di_init(dentry);
  20263. + if (unlikely(err))
  20264. + goto out_si;
  20265. +
  20266. + err = -EBUSY;
  20267. + parent = d_find_any_alias(dir);
  20268. + AuDebugOn(!parent);
  20269. + di_write_lock_parent(parent);
  20270. + if (unlikely(d_inode(parent) != dir))
  20271. + goto out_parent;
  20272. +
  20273. + err = au_digen_test(parent, au_sigen(sb));
  20274. + if (unlikely(err))
  20275. + goto out_parent;
  20276. +
  20277. + bindex = au_dbtop(parent);
  20278. + au_set_dbtop(dentry, bindex);
  20279. + au_set_dbbot(dentry, bindex);
  20280. + err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
  20281. + bindex = err;
  20282. + if (unlikely(err < 0))
  20283. + goto out_parent;
  20284. +
  20285. + err = -EOPNOTSUPP;
  20286. + h_dir = au_h_iptr(dir, bindex);
  20287. + if (unlikely(!h_dir->i_op->tmpfile))
  20288. + goto out_parent;
  20289. +
  20290. + h_mnt = au_sbr_mnt(sb, bindex);
  20291. + err = vfsub_mnt_want_write(h_mnt);
  20292. + if (unlikely(err))
  20293. + goto out_parent;
  20294. +
  20295. + h_userns = mnt_user_ns(h_mnt);
  20296. + h_parent = au_h_dptr(parent, bindex);
  20297. + h_dentry = vfs_tmpfile(h_userns, h_parent, mode, /*open_flag*/0);
  20298. + if (IS_ERR(h_dentry)) {
  20299. + err = PTR_ERR(h_dentry);
  20300. + goto out_mnt;
  20301. + }
  20302. +
  20303. + au_set_dbtop(dentry, bindex);
  20304. + au_set_dbbot(dentry, bindex);
  20305. + au_set_h_dptr(dentry, bindex, dget(h_dentry));
  20306. + inode = au_new_inode(dentry, /*must_new*/1);
  20307. + if (IS_ERR(inode)) {
  20308. + err = PTR_ERR(inode);
  20309. + au_set_h_dptr(dentry, bindex, NULL);
  20310. + au_set_dbtop(dentry, -1);
  20311. + au_set_dbbot(dentry, -1);
  20312. + } else {
  20313. + if (!inode->i_nlink)
  20314. + set_nlink(inode, 1);
  20315. + d_tmpfile(dentry, inode);
  20316. + au_di(dentry)->di_tmpfile = 1;
  20317. +
  20318. + /* update without i_mutex */
  20319. + if (au_ibtop(dir) == au_dbtop(dentry))
  20320. + au_cpup_attr_timesizes(dir);
  20321. + }
  20322. + dput(h_dentry);
  20323. +
  20324. +out_mnt:
  20325. + vfsub_mnt_drop_write(h_mnt);
  20326. +out_parent:
  20327. + di_write_unlock(parent);
  20328. + dput(parent);
  20329. + di_write_unlock(dentry);
  20330. + if (unlikely(err)) {
  20331. + au_di_fin(dentry);
  20332. + dentry->d_fsdata = NULL;
  20333. + }
  20334. +out_si:
  20335. + si_read_unlock(sb);
  20336. +out:
  20337. + inode_unlock(dir);
  20338. + return err;
  20339. +}
  20340. +
  20341. +/* ---------------------------------------------------------------------- */
  20342. +
  20343. +struct au_link_args {
  20344. + aufs_bindex_t bdst, bsrc;
  20345. + struct au_pin pin;
  20346. + struct path h_path;
  20347. + struct dentry *src_parent, *parent;
  20348. +};
  20349. +
  20350. +static int au_cpup_before_link(struct dentry *src_dentry,
  20351. + struct au_link_args *a)
  20352. +{
  20353. + int err;
  20354. + struct dentry *h_src_dentry;
  20355. + struct au_cp_generic cpg = {
  20356. + .dentry = src_dentry,
  20357. + .bdst = a->bdst,
  20358. + .bsrc = a->bsrc,
  20359. + .len = -1,
  20360. + .pin = &a->pin,
  20361. + .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
  20362. + };
  20363. +
  20364. + di_read_lock_parent(a->src_parent, AuLock_IR);
  20365. + err = au_test_and_cpup_dirs(src_dentry, a->bdst);
  20366. + if (unlikely(err))
  20367. + goto out;
  20368. +
  20369. + h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
  20370. + err = au_pin(&a->pin, src_dentry, a->bdst,
  20371. + au_opt_udba(src_dentry->d_sb),
  20372. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  20373. + if (unlikely(err))
  20374. + goto out;
  20375. +
  20376. + err = au_sio_cpup_simple(&cpg);
  20377. + au_unpin(&a->pin);
  20378. +
  20379. +out:
  20380. + di_read_unlock(a->src_parent, AuLock_IR);
  20381. + return err;
  20382. +}
  20383. +
  20384. +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
  20385. + struct au_link_args *a)
  20386. +{
  20387. + int err;
  20388. + unsigned char plink;
  20389. + aufs_bindex_t bbot;
  20390. + struct dentry *h_src_dentry;
  20391. + struct inode *h_inode, *inode, *delegated;
  20392. + struct super_block *sb;
  20393. + struct file *h_file;
  20394. +
  20395. + plink = 0;
  20396. + h_inode = NULL;
  20397. + sb = src_dentry->d_sb;
  20398. + inode = d_inode(src_dentry);
  20399. + if (au_ibtop(inode) <= a->bdst)
  20400. + h_inode = au_h_iptr(inode, a->bdst);
  20401. + if (!h_inode || !h_inode->i_nlink) {
  20402. + /* copyup src_dentry as the name of dentry. */
  20403. + bbot = au_dbbot(dentry);
  20404. + if (bbot < a->bsrc)
  20405. + au_set_dbbot(dentry, a->bsrc);
  20406. + au_set_h_dptr(dentry, a->bsrc,
  20407. + dget(au_h_dptr(src_dentry, a->bsrc)));
  20408. + dget(a->h_path.dentry);
  20409. + au_set_h_dptr(dentry, a->bdst, NULL);
  20410. + AuDbg("temporary d_inode...\n");
  20411. + spin_lock(&dentry->d_lock);
  20412. + dentry->d_inode = d_inode(src_dentry); /* tmp */
  20413. + spin_unlock(&dentry->d_lock);
  20414. + h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
  20415. + if (IS_ERR(h_file))
  20416. + err = PTR_ERR(h_file);
  20417. + else {
  20418. + struct au_cp_generic cpg = {
  20419. + .dentry = dentry,
  20420. + .bdst = a->bdst,
  20421. + .bsrc = -1,
  20422. + .len = -1,
  20423. + .pin = &a->pin,
  20424. + .flags = AuCpup_KEEPLINO
  20425. + };
  20426. + err = au_sio_cpup_simple(&cpg);
  20427. + au_h_open_post(dentry, a->bsrc, h_file);
  20428. + if (!err) {
  20429. + dput(a->h_path.dentry);
  20430. + a->h_path.dentry = au_h_dptr(dentry, a->bdst);
  20431. + } else
  20432. + au_set_h_dptr(dentry, a->bdst,
  20433. + a->h_path.dentry);
  20434. + }
  20435. + spin_lock(&dentry->d_lock);
  20436. + dentry->d_inode = NULL; /* restore */
  20437. + spin_unlock(&dentry->d_lock);
  20438. + AuDbg("temporary d_inode...done\n");
  20439. + au_set_h_dptr(dentry, a->bsrc, NULL);
  20440. + au_set_dbbot(dentry, bbot);
  20441. + } else {
  20442. + /* the inode of src_dentry already exists on a.bdst branch */
  20443. + h_src_dentry = d_find_alias(h_inode);
  20444. + if (!h_src_dentry && au_plink_test(inode)) {
  20445. + plink = 1;
  20446. + h_src_dentry = au_plink_lkup(inode, a->bdst);
  20447. + err = PTR_ERR(h_src_dentry);
  20448. + if (IS_ERR(h_src_dentry))
  20449. + goto out;
  20450. +
  20451. + if (unlikely(d_is_negative(h_src_dentry))) {
  20452. + dput(h_src_dentry);
  20453. + h_src_dentry = NULL;
  20454. + }
  20455. +
  20456. + }
  20457. + if (h_src_dentry) {
  20458. + delegated = NULL;
  20459. + err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
  20460. + &a->h_path, &delegated);
  20461. + if (unlikely(err == -EWOULDBLOCK)) {
  20462. + pr_warn("cannot retry for NFSv4 delegation"
  20463. + " for an internal link\n");
  20464. + iput(delegated);
  20465. + }
  20466. + dput(h_src_dentry);
  20467. + } else {
  20468. + AuIOErr("no dentry found for hi%lu on b%d\n",
  20469. + h_inode->i_ino, a->bdst);
  20470. + err = -EIO;
  20471. + }
  20472. + }
  20473. +
  20474. + if (!err && !plink)
  20475. + au_plink_append(inode, a->bdst, a->h_path.dentry);
  20476. +
  20477. +out:
  20478. + AuTraceErr(err);
  20479. + return err;
  20480. +}
  20481. +
  20482. +int aufs_link(struct dentry *src_dentry, struct inode *dir,
  20483. + struct dentry *dentry)
  20484. +{
  20485. + int err, rerr;
  20486. + struct au_dtime dt;
  20487. + struct au_link_args *a;
  20488. + struct dentry *wh_dentry, *h_src_dentry;
  20489. + struct inode *inode, *delegated;
  20490. + struct super_block *sb;
  20491. + struct au_wr_dir_args wr_dir_args = {
  20492. + /* .force_btgt = -1, */
  20493. + .flags = AuWrDir_ADD_ENTRY
  20494. + };
  20495. +
  20496. + IMustLock(dir);
  20497. + inode = d_inode(src_dentry);
  20498. + IMustLock(inode);
  20499. +
  20500. + err = -ENOMEM;
  20501. + a = kzalloc(sizeof(*a), GFP_NOFS);
  20502. + if (unlikely(!a))
  20503. + goto out;
  20504. +
  20505. + a->parent = dentry->d_parent; /* dir inode is locked */
  20506. + err = aufs_read_and_write_lock2(dentry, src_dentry,
  20507. + AuLock_NOPLM | AuLock_GEN);
  20508. + if (unlikely(err))
  20509. + goto out_kfree;
  20510. + err = au_d_linkable(src_dentry);
  20511. + if (unlikely(err))
  20512. + goto out_unlock;
  20513. + err = au_d_may_add(dentry);
  20514. + if (unlikely(err))
  20515. + goto out_unlock;
  20516. +
  20517. + a->src_parent = dget_parent(src_dentry);
  20518. + wr_dir_args.force_btgt = au_ibtop(inode);
  20519. +
  20520. + di_write_lock_parent(a->parent);
  20521. + wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
  20522. + wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
  20523. + &wr_dir_args);
  20524. + err = PTR_ERR(wh_dentry);
  20525. + if (IS_ERR(wh_dentry))
  20526. + goto out_parent;
  20527. +
  20528. + err = 0;
  20529. + sb = dentry->d_sb;
  20530. + a->bdst = au_dbtop(dentry);
  20531. + a->h_path.dentry = au_h_dptr(dentry, a->bdst);
  20532. + a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
  20533. + a->bsrc = au_ibtop(inode);
  20534. + h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
  20535. + if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
  20536. + h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
  20537. + if (!h_src_dentry) {
  20538. + a->bsrc = au_dbtop(src_dentry);
  20539. + h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
  20540. + AuDebugOn(!h_src_dentry);
  20541. + } else if (IS_ERR(h_src_dentry)) {
  20542. + err = PTR_ERR(h_src_dentry);
  20543. + goto out_parent;
  20544. + }
  20545. +
  20546. + /*
  20547. + * aufs doesn't touch the credential so
  20548. + * security_dentry_create_files_as() is unnecessary.
  20549. + */
  20550. + if (au_opt_test(au_mntflags(sb), PLINK)) {
  20551. + if (a->bdst < a->bsrc
  20552. + /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
  20553. + err = au_cpup_or_link(src_dentry, dentry, a);
  20554. + else {
  20555. + delegated = NULL;
  20556. + err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
  20557. + &a->h_path, &delegated);
  20558. + if (unlikely(err == -EWOULDBLOCK)) {
  20559. + pr_warn("cannot retry for NFSv4 delegation"
  20560. + " for an internal link\n");
  20561. + iput(delegated);
  20562. + }
  20563. + }
  20564. + dput(h_src_dentry);
  20565. + } else {
  20566. + /*
  20567. + * copyup src_dentry to the branch we process,
  20568. + * and then link(2) to it.
  20569. + */
  20570. + dput(h_src_dentry);
  20571. + if (a->bdst < a->bsrc
  20572. + /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
  20573. + au_unpin(&a->pin);
  20574. + di_write_unlock(a->parent);
  20575. + err = au_cpup_before_link(src_dentry, a);
  20576. + di_write_lock_parent(a->parent);
  20577. + if (!err)
  20578. + err = au_pin(&a->pin, dentry, a->bdst,
  20579. + au_opt_udba(sb),
  20580. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  20581. + if (unlikely(err))
  20582. + goto out_wh;
  20583. + }
  20584. + if (!err) {
  20585. + h_src_dentry = au_h_dptr(src_dentry, a->bdst);
  20586. + err = -ENOENT;
  20587. + if (h_src_dentry && d_is_positive(h_src_dentry)) {
  20588. + delegated = NULL;
  20589. + err = vfsub_link(h_src_dentry,
  20590. + au_pinned_h_dir(&a->pin),
  20591. + &a->h_path, &delegated);
  20592. + if (unlikely(err == -EWOULDBLOCK)) {
  20593. + pr_warn("cannot retry"
  20594. + " for NFSv4 delegation"
  20595. + " for an internal link\n");
  20596. + iput(delegated);
  20597. + }
  20598. + }
  20599. + }
  20600. + }
  20601. + if (unlikely(err))
  20602. + goto out_unpin;
  20603. +
  20604. + if (wh_dentry) {
  20605. + a->h_path.dentry = wh_dentry;
  20606. + err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
  20607. + dentry);
  20608. + if (unlikely(err))
  20609. + goto out_revert;
  20610. + }
  20611. +
  20612. + au_dir_ts(dir, a->bdst);
  20613. + inode_inc_iversion(dir);
  20614. + inc_nlink(inode);
  20615. + inode->i_ctime = dir->i_ctime;
  20616. + d_instantiate(dentry, au_igrab(inode));
  20617. + if (d_unhashed(a->h_path.dentry))
  20618. + /* some filesystem calls d_drop() */
  20619. + d_drop(dentry);
  20620. + /* some filesystems consume an inode even hardlink */
  20621. + au_fhsm_wrote(sb, a->bdst, /*force*/0);
  20622. + goto out_unpin; /* success */
  20623. +
  20624. +out_revert:
  20625. + /* no delegation since it is just created */
  20626. + rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
  20627. + /*delegated*/NULL, /*force*/0);
  20628. + if (unlikely(rerr)) {
  20629. + AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
  20630. + err = -EIO;
  20631. + }
  20632. + au_dtime_revert(&dt);
  20633. +out_unpin:
  20634. + au_unpin(&a->pin);
  20635. +out_wh:
  20636. + dput(wh_dentry);
  20637. +out_parent:
  20638. + di_write_unlock(a->parent);
  20639. + dput(a->src_parent);
  20640. +out_unlock:
  20641. + if (unlikely(err)) {
  20642. + au_update_dbtop(dentry);
  20643. + d_drop(dentry);
  20644. + }
  20645. + aufs_read_and_write_unlock2(dentry, src_dentry);
  20646. +out_kfree:
  20647. + au_kfree_rcu(a);
  20648. +out:
  20649. + AuTraceErr(err);
  20650. + return err;
  20651. +}
  20652. +
  20653. +int aufs_mkdir(struct user_namespace *userns, struct inode *dir,
  20654. + struct dentry *dentry, umode_t mode)
  20655. +{
  20656. + int err, rerr;
  20657. + aufs_bindex_t bindex;
  20658. + unsigned char diropq;
  20659. + struct path h_path;
  20660. + struct dentry *wh_dentry, *parent, *opq_dentry;
  20661. + struct inode *h_inode;
  20662. + struct super_block *sb;
  20663. + struct {
  20664. + struct au_pin pin;
  20665. + struct au_dtime dt;
  20666. + } *a; /* reduce the stack usage */
  20667. + struct au_wr_dir_args wr_dir_args = {
  20668. + .force_btgt = -1,
  20669. + .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
  20670. + };
  20671. +
  20672. + IMustLock(dir);
  20673. +
  20674. + err = -ENOMEM;
  20675. + a = kmalloc(sizeof(*a), GFP_NOFS);
  20676. + if (unlikely(!a))
  20677. + goto out;
  20678. +
  20679. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
  20680. + if (unlikely(err))
  20681. + goto out_free;
  20682. + err = au_d_may_add(dentry);
  20683. + if (unlikely(err))
  20684. + goto out_unlock;
  20685. +
  20686. + parent = dentry->d_parent; /* dir inode is locked */
  20687. + di_write_lock_parent(parent);
  20688. + wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
  20689. + &a->pin, &wr_dir_args);
  20690. + err = PTR_ERR(wh_dentry);
  20691. + if (IS_ERR(wh_dentry))
  20692. + goto out_parent;
  20693. +
  20694. + sb = dentry->d_sb;
  20695. + bindex = au_dbtop(dentry);
  20696. + h_path.dentry = au_h_dptr(dentry, bindex);
  20697. + h_path.mnt = au_sbr_mnt(sb, bindex);
  20698. + err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
  20699. + if (unlikely(err))
  20700. + goto out_unpin;
  20701. +
  20702. + /* make the dir opaque */
  20703. + diropq = 0;
  20704. + h_inode = d_inode(h_path.dentry);
  20705. + if (wh_dentry
  20706. + || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
  20707. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  20708. + opq_dentry = au_diropq_create(dentry, bindex);
  20709. + inode_unlock(h_inode);
  20710. + err = PTR_ERR(opq_dentry);
  20711. + if (IS_ERR(opq_dentry))
  20712. + goto out_dir;
  20713. + dput(opq_dentry);
  20714. + diropq = 1;
  20715. + }
  20716. +
  20717. + err = epilog(dir, bindex, wh_dentry, dentry);
  20718. + if (!err) {
  20719. + inc_nlink(dir);
  20720. + goto out_unpin; /* success */
  20721. + }
  20722. +
  20723. + /* revert */
  20724. + if (diropq) {
  20725. + AuLabel(revert opq);
  20726. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  20727. + rerr = au_diropq_remove(dentry, bindex);
  20728. + inode_unlock(h_inode);
  20729. + if (rerr) {
  20730. + AuIOErr("%pd reverting diropq failed(%d, %d)\n",
  20731. + dentry, err, rerr);
  20732. + err = -EIO;
  20733. + }
  20734. + }
  20735. +
  20736. +out_dir:
  20737. + AuLabel(revert dir);
  20738. + rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
  20739. + if (rerr) {
  20740. + AuIOErr("%pd reverting dir failed(%d, %d)\n",
  20741. + dentry, err, rerr);
  20742. + err = -EIO;
  20743. + }
  20744. + au_dtime_revert(&a->dt);
  20745. +out_unpin:
  20746. + au_unpin(&a->pin);
  20747. + dput(wh_dentry);
  20748. +out_parent:
  20749. + di_write_unlock(parent);
  20750. +out_unlock:
  20751. + if (unlikely(err)) {
  20752. + au_update_dbtop(dentry);
  20753. + d_drop(dentry);
  20754. + }
  20755. + aufs_read_unlock(dentry, AuLock_DW);
  20756. +out_free:
  20757. + au_kfree_rcu(a);
  20758. +out:
  20759. + return err;
  20760. +}
  20761. diff -Naur null/fs/aufs/i_op.c linux-5.15.131/fs/aufs/i_op.c
  20762. --- /dev/null
  20763. +++ linux-5.15.131/fs/aufs/i_op.c 2023-09-11 14:32:48.149722539 +0300
  20764. @@ -0,0 +1,1512 @@
  20765. +// SPDX-License-Identifier: GPL-2.0
  20766. +/*
  20767. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  20768. + *
  20769. + * This program, aufs is free software; you can redistribute it and/or modify
  20770. + * it under the terms of the GNU General Public License as published by
  20771. + * the Free Software Foundation; either version 2 of the License, or
  20772. + * (at your option) any later version.
  20773. + *
  20774. + * This program is distributed in the hope that it will be useful,
  20775. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20776. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20777. + * GNU General Public License for more details.
  20778. + *
  20779. + * You should have received a copy of the GNU General Public License
  20780. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20781. + */
  20782. +
  20783. +/*
  20784. + * inode operations (except add/del/rename)
  20785. + */
  20786. +
  20787. +#include <linux/device_cgroup.h>
  20788. +#include <linux/fs_stack.h>
  20789. +#include <linux/iversion.h>
  20790. +#include <linux/security.h>
  20791. +#include "aufs.h"
  20792. +
  20793. +static int h_permission(struct inode *h_inode, int mask,
  20794. + struct path *h_path, int brperm)
  20795. +{
  20796. + int err;
  20797. + const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
  20798. + struct user_namespace *h_userns;
  20799. +
  20800. + err = -EPERM;
  20801. + if (write_mask && IS_IMMUTABLE(h_inode))
  20802. + goto out;
  20803. +
  20804. + err = -EACCES;
  20805. + if (((mask & MAY_EXEC)
  20806. + && S_ISREG(h_inode->i_mode)
  20807. + && (path_noexec(h_path)
  20808. + || !(h_inode->i_mode & 0111))))
  20809. + goto out;
  20810. +
  20811. + /*
  20812. + * - skip the lower fs test in the case of write to ro branch.
  20813. + * - nfs dir permission write check is optimized, but a policy for
  20814. + * link/rename requires a real check.
  20815. + * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
  20816. + * in this case, generic_permission() returns -EOPNOTSUPP.
  20817. + */
  20818. + h_userns = mnt_user_ns(h_path->mnt);
  20819. + if ((write_mask && !au_br_writable(brperm))
  20820. + || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
  20821. + && write_mask && !(mask & MAY_READ))
  20822. + || !h_inode->i_op->permission) {
  20823. + /* AuLabel(generic_permission); */
  20824. + /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
  20825. + err = generic_permission(h_userns, h_inode, mask);
  20826. + if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
  20827. + err = h_inode->i_op->permission(h_userns, h_inode,
  20828. + mask);
  20829. + AuTraceErr(err);
  20830. + } else {
  20831. + /* AuLabel(h_inode->permission); */
  20832. + err = h_inode->i_op->permission(h_userns, h_inode, mask);
  20833. + AuTraceErr(err);
  20834. + }
  20835. +
  20836. + if (!err)
  20837. + err = devcgroup_inode_permission(h_inode, mask);
  20838. + if (!err)
  20839. + err = security_inode_permission(h_inode, mask);
  20840. +
  20841. +out:
  20842. + return err;
  20843. +}
  20844. +
  20845. +static int aufs_permission(struct user_namespace *userns, struct inode *inode,
  20846. + int mask)
  20847. +{
  20848. + int err;
  20849. + aufs_bindex_t bindex, bbot;
  20850. + const unsigned char isdir = !!S_ISDIR(inode->i_mode),
  20851. + write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
  20852. + struct inode *h_inode;
  20853. + struct super_block *sb;
  20854. + struct au_branch *br;
  20855. +
  20856. + /* todo: support rcu-walk? */
  20857. + if (mask & MAY_NOT_BLOCK)
  20858. + return -ECHILD;
  20859. +
  20860. + sb = inode->i_sb;
  20861. + si_read_lock(sb, AuLock_FLUSH);
  20862. + ii_read_lock_child(inode);
  20863. +#if 0 /* reserved for future use */
  20864. + /*
  20865. + * This test may be rather 'too much' since the test is essentially done
  20866. + * in the aufs_lookup(). Theoretically it is possible that the inode
  20867. + * generation doesn't match to the superblock's here. But it isn't a
  20868. + * big deal I suppose.
  20869. + */
  20870. + err = au_iigen_test(inode, au_sigen(sb));
  20871. + if (unlikely(err))
  20872. + goto out;
  20873. +#endif
  20874. +
  20875. + if (!isdir
  20876. + || write_mask
  20877. + || au_opt_test(au_mntflags(sb), DIRPERM1)) {
  20878. + err = au_busy_or_stale();
  20879. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  20880. + if (unlikely(!h_inode
  20881. + || (h_inode->i_mode & S_IFMT)
  20882. + != (inode->i_mode & S_IFMT)))
  20883. + goto out;
  20884. +
  20885. + err = 0;
  20886. + bindex = au_ibtop(inode);
  20887. + br = au_sbr(sb, bindex);
  20888. + err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
  20889. + if (write_mask
  20890. + && !err
  20891. + && !special_file(h_inode->i_mode)) {
  20892. + /* test whether the upper writable branch exists */
  20893. + err = -EROFS;
  20894. + for (; bindex >= 0; bindex--)
  20895. + if (!au_br_rdonly(au_sbr(sb, bindex))) {
  20896. + err = 0;
  20897. + break;
  20898. + }
  20899. + }
  20900. + goto out;
  20901. + }
  20902. +
  20903. + /* non-write to dir */
  20904. + err = 0;
  20905. + bbot = au_ibbot(inode);
  20906. + for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
  20907. + h_inode = au_h_iptr(inode, bindex);
  20908. + if (h_inode) {
  20909. + err = au_busy_or_stale();
  20910. + if (unlikely(!S_ISDIR(h_inode->i_mode)))
  20911. + break;
  20912. +
  20913. + br = au_sbr(sb, bindex);
  20914. + err = h_permission(h_inode, mask, &br->br_path,
  20915. + br->br_perm);
  20916. + }
  20917. + }
  20918. +
  20919. +out:
  20920. + ii_read_unlock(inode);
  20921. + si_read_unlock(sb);
  20922. + return err;
  20923. +}
  20924. +
  20925. +/* ---------------------------------------------------------------------- */
  20926. +
  20927. +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
  20928. + unsigned int flags)
  20929. +{
  20930. + struct dentry *ret, *parent;
  20931. + struct inode *inode;
  20932. + struct super_block *sb;
  20933. + int err, npositive;
  20934. +
  20935. + IMustLock(dir);
  20936. +
  20937. + /* todo: support rcu-walk? */
  20938. + ret = ERR_PTR(-ECHILD);
  20939. + if (flags & LOOKUP_RCU)
  20940. + goto out;
  20941. +
  20942. + ret = ERR_PTR(-ENAMETOOLONG);
  20943. + if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
  20944. + goto out;
  20945. +
  20946. + sb = dir->i_sb;
  20947. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  20948. + ret = ERR_PTR(err);
  20949. + if (unlikely(err))
  20950. + goto out;
  20951. +
  20952. + err = au_di_init(dentry);
  20953. + ret = ERR_PTR(err);
  20954. + if (unlikely(err))
  20955. + goto out_si;
  20956. +
  20957. + inode = NULL;
  20958. + npositive = 0; /* suppress a warning */
  20959. + parent = dentry->d_parent; /* dir inode is locked */
  20960. + di_read_lock_parent(parent, AuLock_IR);
  20961. + err = au_alive_dir(parent);
  20962. + if (!err)
  20963. + err = au_digen_test(parent, au_sigen(sb));
  20964. + if (!err) {
  20965. + /* regardless LOOKUP_CREATE, always ALLOW_NEG */
  20966. + npositive = au_lkup_dentry(dentry, au_dbtop(parent),
  20967. + AuLkup_ALLOW_NEG);
  20968. + err = npositive;
  20969. + }
  20970. + di_read_unlock(parent, AuLock_IR);
  20971. + ret = ERR_PTR(err);
  20972. + if (unlikely(err < 0))
  20973. + goto out_unlock;
  20974. +
  20975. + if (npositive) {
  20976. + inode = au_new_inode(dentry, /*must_new*/0);
  20977. + if (IS_ERR(inode)) {
  20978. + ret = (void *)inode;
  20979. + inode = NULL;
  20980. + goto out_unlock;
  20981. + }
  20982. + }
  20983. +
  20984. + if (inode)
  20985. + atomic_inc(&inode->i_count);
  20986. + ret = d_splice_alias(inode, dentry);
  20987. +#if 0 /* reserved for future use */
  20988. + if (unlikely(d_need_lookup(dentry))) {
  20989. + spin_lock(&dentry->d_lock);
  20990. + dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
  20991. + spin_unlock(&dentry->d_lock);
  20992. + } else
  20993. +#endif
  20994. + if (inode) {
  20995. + if (!IS_ERR(ret)) {
  20996. + iput(inode);
  20997. + if (ret && ret != dentry)
  20998. + ii_write_unlock(inode);
  20999. + } else {
  21000. + ii_write_unlock(inode);
  21001. + iput(inode);
  21002. + inode = NULL;
  21003. + }
  21004. + }
  21005. +
  21006. +out_unlock:
  21007. + di_write_unlock(dentry);
  21008. +out_si:
  21009. + si_read_unlock(sb);
  21010. +out:
  21011. + return ret;
  21012. +}
  21013. +
  21014. +/* ---------------------------------------------------------------------- */
  21015. +
  21016. +/*
  21017. + * very dirty and complicated aufs ->atomic_open().
  21018. + * aufs_atomic_open()
  21019. + * + au_aopen_or_create()
  21020. + * + add_simple()
  21021. + * + vfsub_atomic_open()
  21022. + * + branch fs ->atomic_open()
  21023. + * may call the actual 'open' for h_file
  21024. + * + inc br_nfiles only if opened
  21025. + * + au_aopen_no_open() or au_aopen_do_open()
  21026. + *
  21027. + * au_aopen_do_open()
  21028. + * + finish_open()
  21029. + * + au_do_aopen()
  21030. + * + au_do_open() the body of all 'open'
  21031. + * + au_do_open_nondir()
  21032. + * set the passed h_file
  21033. + *
  21034. + * au_aopen_no_open()
  21035. + * + finish_no_open()
  21036. + */
  21037. +
  21038. +struct aopen_node {
  21039. + struct hlist_bl_node hblist;
  21040. + struct file *file, *h_file;
  21041. +};
  21042. +
  21043. +static int au_do_aopen(struct inode *inode, struct file *file)
  21044. +{
  21045. + struct hlist_bl_head *aopen;
  21046. + struct hlist_bl_node *pos;
  21047. + struct aopen_node *node;
  21048. + struct au_do_open_args args = {
  21049. + .aopen = 1,
  21050. + .open = au_do_open_nondir
  21051. + };
  21052. +
  21053. + aopen = &au_sbi(inode->i_sb)->si_aopen;
  21054. + hlist_bl_lock(aopen);
  21055. + hlist_bl_for_each_entry(node, pos, aopen, hblist)
  21056. + if (node->file == file) {
  21057. + args.h_file = node->h_file;
  21058. + break;
  21059. + }
  21060. + hlist_bl_unlock(aopen);
  21061. + /* AuDebugOn(!args.h_file); */
  21062. +
  21063. + return au_do_open(file, &args);
  21064. +}
  21065. +
  21066. +static int au_aopen_do_open(struct file *file, struct dentry *dentry,
  21067. + struct aopen_node *aopen_node)
  21068. +{
  21069. + int err;
  21070. + struct hlist_bl_head *aopen;
  21071. +
  21072. + AuLabel(here);
  21073. + aopen = &au_sbi(dentry->d_sb)->si_aopen;
  21074. + au_hbl_add(&aopen_node->hblist, aopen);
  21075. + err = finish_open(file, dentry, au_do_aopen);
  21076. + au_hbl_del(&aopen_node->hblist, aopen);
  21077. + /* AuDbgFile(file); */
  21078. + AuDbg("%pd%s%s\n", dentry,
  21079. + (file->f_mode & FMODE_CREATED) ? " created" : "",
  21080. + (file->f_mode & FMODE_OPENED) ? " opened" : "");
  21081. +
  21082. + AuTraceErr(err);
  21083. + return err;
  21084. +}
  21085. +
  21086. +static int au_aopen_no_open(struct file *file, struct dentry *dentry)
  21087. +{
  21088. + int err;
  21089. +
  21090. + AuLabel(here);
  21091. + dget(dentry);
  21092. + err = finish_no_open(file, dentry);
  21093. +
  21094. + AuTraceErr(err);
  21095. + return err;
  21096. +}
  21097. +
  21098. +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
  21099. + struct file *file, unsigned int open_flag,
  21100. + umode_t create_mode)
  21101. +{
  21102. + int err, did_open;
  21103. + unsigned int lkup_flags;
  21104. + aufs_bindex_t bindex;
  21105. + struct super_block *sb;
  21106. + struct dentry *parent, *d;
  21107. + struct vfsub_aopen_args args = {
  21108. + .open_flag = open_flag,
  21109. + .create_mode = create_mode
  21110. + };
  21111. + struct aopen_node aopen_node = {
  21112. + .file = file
  21113. + };
  21114. +
  21115. + IMustLock(dir);
  21116. + AuDbg("open_flag 0%o\n", open_flag);
  21117. + AuDbgDentry(dentry);
  21118. +
  21119. + err = 0;
  21120. + if (!au_di(dentry)) {
  21121. + lkup_flags = LOOKUP_OPEN;
  21122. + if (open_flag & O_CREAT)
  21123. + lkup_flags |= LOOKUP_CREATE;
  21124. + d = aufs_lookup(dir, dentry, lkup_flags);
  21125. + if (IS_ERR(d)) {
  21126. + err = PTR_ERR(d);
  21127. + AuTraceErr(err);
  21128. + goto out;
  21129. + } else if (d) {
  21130. + /*
  21131. + * obsoleted dentry found.
  21132. + * another error will be returned later.
  21133. + */
  21134. + d_drop(d);
  21135. + AuDbgDentry(d);
  21136. + dput(d);
  21137. + }
  21138. + AuDbgDentry(dentry);
  21139. + }
  21140. +
  21141. + if (d_is_positive(dentry)
  21142. + || d_unhashed(dentry)
  21143. + || d_unlinked(dentry)
  21144. + || !(open_flag & O_CREAT)) {
  21145. + err = au_aopen_no_open(file, dentry);
  21146. + goto out; /* success */
  21147. + }
  21148. +
  21149. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
  21150. + if (unlikely(err))
  21151. + goto out;
  21152. +
  21153. + sb = dentry->d_sb;
  21154. + parent = dentry->d_parent; /* dir is locked */
  21155. + di_write_lock_parent(parent);
  21156. + err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
  21157. + if (unlikely(err < 0))
  21158. + goto out_parent;
  21159. +
  21160. + AuDbgDentry(dentry);
  21161. + if (d_is_positive(dentry)) {
  21162. + err = au_aopen_no_open(file, dentry);
  21163. + goto out_parent; /* success */
  21164. + }
  21165. +
  21166. + args.file = alloc_empty_file(file->f_flags, current_cred());
  21167. + err = PTR_ERR(args.file);
  21168. + if (IS_ERR(args.file))
  21169. + goto out_parent;
  21170. +
  21171. + bindex = au_dbtop(dentry);
  21172. + err = au_aopen_or_create(dir, dentry, &args);
  21173. + AuTraceErr(err);
  21174. + AuDbgFile(args.file);
  21175. + file->f_mode = args.file->f_mode & ~FMODE_OPENED;
  21176. + did_open = !!(args.file->f_mode & FMODE_OPENED);
  21177. + if (!did_open) {
  21178. + fput(args.file);
  21179. + args.file = NULL;
  21180. + }
  21181. + di_write_unlock(parent);
  21182. + di_write_unlock(dentry);
  21183. + if (unlikely(err < 0)) {
  21184. + if (args.file)
  21185. + fput(args.file);
  21186. + goto out_sb;
  21187. + }
  21188. +
  21189. + if (!did_open)
  21190. + err = au_aopen_no_open(file, dentry);
  21191. + else {
  21192. + aopen_node.h_file = args.file;
  21193. + err = au_aopen_do_open(file, dentry, &aopen_node);
  21194. + }
  21195. + if (unlikely(err < 0)) {
  21196. + if (args.file)
  21197. + fput(args.file);
  21198. + if (did_open)
  21199. + au_lcnt_dec(&args.br->br_nfiles);
  21200. + }
  21201. + goto out_sb; /* success */
  21202. +
  21203. +out_parent:
  21204. + di_write_unlock(parent);
  21205. + di_write_unlock(dentry);
  21206. +out_sb:
  21207. + si_read_unlock(sb);
  21208. +out:
  21209. + AuTraceErr(err);
  21210. + AuDbgFile(file);
  21211. + return err;
  21212. +}
  21213. +
  21214. +
  21215. +/* ---------------------------------------------------------------------- */
  21216. +
  21217. +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
  21218. + const unsigned char add_entry, aufs_bindex_t bcpup,
  21219. + aufs_bindex_t btop)
  21220. +{
  21221. + int err;
  21222. + struct dentry *h_parent;
  21223. + struct inode *h_dir;
  21224. +
  21225. + if (add_entry)
  21226. + IMustLock(d_inode(parent));
  21227. + else
  21228. + di_write_lock_parent(parent);
  21229. +
  21230. + err = 0;
  21231. + if (!au_h_dptr(parent, bcpup)) {
  21232. + if (btop > bcpup)
  21233. + err = au_cpup_dirs(dentry, bcpup);
  21234. + else if (btop < bcpup)
  21235. + err = au_cpdown_dirs(dentry, bcpup);
  21236. + else
  21237. + BUG();
  21238. + }
  21239. + if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
  21240. + h_parent = au_h_dptr(parent, bcpup);
  21241. + h_dir = d_inode(h_parent);
  21242. + inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
  21243. + err = au_lkup_neg(dentry, bcpup, /*wh*/0);
  21244. + /* todo: no unlock here */
  21245. + inode_unlock_shared(h_dir);
  21246. +
  21247. + AuDbg("bcpup %d\n", bcpup);
  21248. + if (!err) {
  21249. + if (d_really_is_negative(dentry))
  21250. + au_set_h_dptr(dentry, btop, NULL);
  21251. + au_update_dbrange(dentry, /*do_put_zero*/0);
  21252. + }
  21253. + }
  21254. +
  21255. + if (!add_entry)
  21256. + di_write_unlock(parent);
  21257. + if (!err)
  21258. + err = bcpup; /* success */
  21259. +
  21260. + AuTraceErr(err);
  21261. + return err;
  21262. +}
  21263. +
  21264. +/*
  21265. + * decide the branch and the parent dir where we will create a new entry.
  21266. + * returns new bindex or an error.
  21267. + * copyup the parent dir if needed.
  21268. + */
  21269. +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
  21270. + struct au_wr_dir_args *args)
  21271. +{
  21272. + int err;
  21273. + unsigned int flags;
  21274. + aufs_bindex_t bcpup, btop, src_btop;
  21275. + const unsigned char add_entry
  21276. + = au_ftest_wrdir(args->flags, ADD_ENTRY)
  21277. + | au_ftest_wrdir(args->flags, TMPFILE);
  21278. + struct super_block *sb;
  21279. + struct dentry *parent;
  21280. + struct au_sbinfo *sbinfo;
  21281. +
  21282. + sb = dentry->d_sb;
  21283. + sbinfo = au_sbi(sb);
  21284. + parent = dget_parent(dentry);
  21285. + btop = au_dbtop(dentry);
  21286. + bcpup = btop;
  21287. + if (args->force_btgt < 0) {
  21288. + if (src_dentry) {
  21289. + src_btop = au_dbtop(src_dentry);
  21290. + if (src_btop < btop)
  21291. + bcpup = src_btop;
  21292. + } else if (add_entry) {
  21293. + flags = 0;
  21294. + if (au_ftest_wrdir(args->flags, ISDIR))
  21295. + au_fset_wbr(flags, DIR);
  21296. + err = AuWbrCreate(sbinfo, dentry, flags);
  21297. + bcpup = err;
  21298. + }
  21299. +
  21300. + if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
  21301. + if (add_entry)
  21302. + err = AuWbrCopyup(sbinfo, dentry);
  21303. + else {
  21304. + if (!IS_ROOT(dentry)) {
  21305. + di_read_lock_parent(parent, !AuLock_IR);
  21306. + err = AuWbrCopyup(sbinfo, dentry);
  21307. + di_read_unlock(parent, !AuLock_IR);
  21308. + } else
  21309. + err = AuWbrCopyup(sbinfo, dentry);
  21310. + }
  21311. + bcpup = err;
  21312. + if (unlikely(err < 0))
  21313. + goto out;
  21314. + }
  21315. + } else {
  21316. + bcpup = args->force_btgt;
  21317. + AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
  21318. + }
  21319. +
  21320. + AuDbg("btop %d, bcpup %d\n", btop, bcpup);
  21321. + err = bcpup;
  21322. + if (bcpup == btop)
  21323. + goto out; /* success */
  21324. +
  21325. + /* copyup the new parent into the branch we process */
  21326. + err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
  21327. + if (err >= 0) {
  21328. + if (d_really_is_negative(dentry)) {
  21329. + au_set_h_dptr(dentry, btop, NULL);
  21330. + au_set_dbtop(dentry, bcpup);
  21331. + au_set_dbbot(dentry, bcpup);
  21332. + }
  21333. + AuDebugOn(add_entry
  21334. + && !au_ftest_wrdir(args->flags, TMPFILE)
  21335. + && !au_h_dptr(dentry, bcpup));
  21336. + }
  21337. +
  21338. +out:
  21339. + dput(parent);
  21340. + return err;
  21341. +}
  21342. +
  21343. +/* ---------------------------------------------------------------------- */
  21344. +
  21345. +void au_pin_hdir_unlock(struct au_pin *p)
  21346. +{
  21347. + if (p->hdir)
  21348. + au_hn_inode_unlock(p->hdir);
  21349. +}
  21350. +
  21351. +int au_pin_hdir_lock(struct au_pin *p)
  21352. +{
  21353. + int err;
  21354. +
  21355. + err = 0;
  21356. + if (!p->hdir)
  21357. + goto out;
  21358. +
  21359. + /* even if an error happens later, keep this lock */
  21360. + au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
  21361. +
  21362. + err = -EBUSY;
  21363. + if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
  21364. + goto out;
  21365. +
  21366. + err = 0;
  21367. + if (p->h_dentry)
  21368. + err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
  21369. + p->h_parent, p->br);
  21370. +
  21371. +out:
  21372. + return err;
  21373. +}
  21374. +
  21375. +int au_pin_hdir_relock(struct au_pin *p)
  21376. +{
  21377. + int err, i;
  21378. + struct inode *h_i;
  21379. + struct dentry *h_d[] = {
  21380. + p->h_dentry,
  21381. + p->h_parent
  21382. + };
  21383. +
  21384. + err = au_pin_hdir_lock(p);
  21385. + if (unlikely(err))
  21386. + goto out;
  21387. +
  21388. + for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
  21389. + if (!h_d[i])
  21390. + continue;
  21391. + if (d_is_positive(h_d[i])) {
  21392. + h_i = d_inode(h_d[i]);
  21393. + err = !h_i->i_nlink;
  21394. + }
  21395. + }
  21396. +
  21397. +out:
  21398. + return err;
  21399. +}
  21400. +
  21401. +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
  21402. +{
  21403. + atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
  21404. +}
  21405. +
  21406. +void au_pin_hdir_acquire_nest(struct au_pin *p)
  21407. +{
  21408. + if (p->hdir) {
  21409. + rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
  21410. + p->lsc_hi, 0, NULL, _RET_IP_);
  21411. + au_pin_hdir_set_owner(p, current);
  21412. + }
  21413. +}
  21414. +
  21415. +void au_pin_hdir_release(struct au_pin *p)
  21416. +{
  21417. + if (p->hdir) {
  21418. + au_pin_hdir_set_owner(p, p->task);
  21419. + rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, _RET_IP_);
  21420. + }
  21421. +}
  21422. +
  21423. +struct dentry *au_pinned_h_parent(struct au_pin *pin)
  21424. +{
  21425. + if (pin && pin->parent)
  21426. + return au_h_dptr(pin->parent, pin->bindex);
  21427. + return NULL;
  21428. +}
  21429. +
  21430. +void au_unpin(struct au_pin *p)
  21431. +{
  21432. + if (p->hdir)
  21433. + au_pin_hdir_unlock(p);
  21434. + if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
  21435. + vfsub_mnt_drop_write(p->h_mnt);
  21436. + if (!p->hdir)
  21437. + return;
  21438. +
  21439. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  21440. + di_read_unlock(p->parent, AuLock_IR);
  21441. + iput(p->hdir->hi_inode);
  21442. + dput(p->parent);
  21443. + p->parent = NULL;
  21444. + p->hdir = NULL;
  21445. + p->h_mnt = NULL;
  21446. + /* do not clear p->task */
  21447. +}
  21448. +
  21449. +int au_do_pin(struct au_pin *p)
  21450. +{
  21451. + int err;
  21452. + struct super_block *sb;
  21453. + struct inode *h_dir;
  21454. +
  21455. + err = 0;
  21456. + sb = p->dentry->d_sb;
  21457. + p->br = au_sbr(sb, p->bindex);
  21458. + if (IS_ROOT(p->dentry)) {
  21459. + if (au_ftest_pin(p->flags, MNT_WRITE)) {
  21460. + p->h_mnt = au_br_mnt(p->br);
  21461. + err = vfsub_mnt_want_write(p->h_mnt);
  21462. + if (unlikely(err)) {
  21463. + au_fclr_pin(p->flags, MNT_WRITE);
  21464. + goto out_err;
  21465. + }
  21466. + }
  21467. + goto out;
  21468. + }
  21469. +
  21470. + p->h_dentry = NULL;
  21471. + if (p->bindex <= au_dbbot(p->dentry))
  21472. + p->h_dentry = au_h_dptr(p->dentry, p->bindex);
  21473. +
  21474. + p->parent = dget_parent(p->dentry);
  21475. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  21476. + di_read_lock(p->parent, AuLock_IR, p->lsc_di);
  21477. +
  21478. + h_dir = NULL;
  21479. + p->h_parent = au_h_dptr(p->parent, p->bindex);
  21480. + p->hdir = au_hi(d_inode(p->parent), p->bindex);
  21481. + if (p->hdir)
  21482. + h_dir = p->hdir->hi_inode;
  21483. +
  21484. + /*
  21485. + * udba case, or
  21486. + * if DI_LOCKED is not set, then p->parent may be different
  21487. + * and h_parent can be NULL.
  21488. + */
  21489. + if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
  21490. + err = -EBUSY;
  21491. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  21492. + di_read_unlock(p->parent, AuLock_IR);
  21493. + dput(p->parent);
  21494. + p->parent = NULL;
  21495. + goto out_err;
  21496. + }
  21497. +
  21498. + if (au_ftest_pin(p->flags, MNT_WRITE)) {
  21499. + p->h_mnt = au_br_mnt(p->br);
  21500. + err = vfsub_mnt_want_write(p->h_mnt);
  21501. + if (unlikely(err)) {
  21502. + au_fclr_pin(p->flags, MNT_WRITE);
  21503. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  21504. + di_read_unlock(p->parent, AuLock_IR);
  21505. + dput(p->parent);
  21506. + p->parent = NULL;
  21507. + goto out_err;
  21508. + }
  21509. + }
  21510. +
  21511. + au_igrab(h_dir);
  21512. + err = au_pin_hdir_lock(p);
  21513. + if (!err)
  21514. + goto out; /* success */
  21515. +
  21516. + au_unpin(p);
  21517. +
  21518. +out_err:
  21519. + pr_err("err %d\n", err);
  21520. + err = au_busy_or_stale();
  21521. +out:
  21522. + return err;
  21523. +}
  21524. +
  21525. +void au_pin_init(struct au_pin *p, struct dentry *dentry,
  21526. + aufs_bindex_t bindex, int lsc_di, int lsc_hi,
  21527. + unsigned int udba, unsigned char flags)
  21528. +{
  21529. + p->dentry = dentry;
  21530. + p->udba = udba;
  21531. + p->lsc_di = lsc_di;
  21532. + p->lsc_hi = lsc_hi;
  21533. + p->flags = flags;
  21534. + p->bindex = bindex;
  21535. +
  21536. + p->parent = NULL;
  21537. + p->hdir = NULL;
  21538. + p->h_mnt = NULL;
  21539. +
  21540. + p->h_dentry = NULL;
  21541. + p->h_parent = NULL;
  21542. + p->br = NULL;
  21543. + p->task = current;
  21544. +}
  21545. +
  21546. +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
  21547. + unsigned int udba, unsigned char flags)
  21548. +{
  21549. + au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
  21550. + udba, flags);
  21551. + return au_do_pin(pin);
  21552. +}
  21553. +
  21554. +/* ---------------------------------------------------------------------- */
  21555. +
  21556. +/*
  21557. + * ->setattr() and ->getattr() are called in various cases.
  21558. + * chmod, stat: dentry is revalidated.
  21559. + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
  21560. + * unhashed.
  21561. + * for ->setattr(), ia->ia_file is passed from ftruncate only.
  21562. + */
  21563. +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
  21564. +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
  21565. +{
  21566. + int err;
  21567. + struct dentry *parent;
  21568. +
  21569. + err = 0;
  21570. + if (au_digen_test(dentry, sigen)) {
  21571. + parent = dget_parent(dentry);
  21572. + di_read_lock_parent(parent, AuLock_IR);
  21573. + err = au_refresh_dentry(dentry, parent);
  21574. + di_read_unlock(parent, AuLock_IR);
  21575. + dput(parent);
  21576. + }
  21577. +
  21578. + AuTraceErr(err);
  21579. + return err;
  21580. +}
  21581. +
  21582. +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
  21583. + struct au_icpup_args *a)
  21584. +{
  21585. + int err;
  21586. + loff_t sz;
  21587. + aufs_bindex_t btop, ibtop;
  21588. + struct dentry *hi_wh, *parent;
  21589. + struct inode *inode;
  21590. + struct au_wr_dir_args wr_dir_args = {
  21591. + .force_btgt = -1,
  21592. + .flags = 0
  21593. + };
  21594. +
  21595. + if (d_is_dir(dentry))
  21596. + au_fset_wrdir(wr_dir_args.flags, ISDIR);
  21597. + /* plink or hi_wh() case */
  21598. + btop = au_dbtop(dentry);
  21599. + inode = d_inode(dentry);
  21600. + ibtop = au_ibtop(inode);
  21601. + if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
  21602. + wr_dir_args.force_btgt = ibtop;
  21603. + err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
  21604. + if (unlikely(err < 0))
  21605. + goto out;
  21606. + a->btgt = err;
  21607. + if (err != btop)
  21608. + au_fset_icpup(a->flags, DID_CPUP);
  21609. +
  21610. + err = 0;
  21611. + a->pin_flags = AuPin_MNT_WRITE;
  21612. + parent = NULL;
  21613. + if (!IS_ROOT(dentry)) {
  21614. + au_fset_pin(a->pin_flags, DI_LOCKED);
  21615. + parent = dget_parent(dentry);
  21616. + di_write_lock_parent(parent);
  21617. + }
  21618. +
  21619. + err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
  21620. + if (unlikely(err))
  21621. + goto out_parent;
  21622. +
  21623. + sz = -1;
  21624. + a->h_path.dentry = au_h_dptr(dentry, btop);
  21625. + a->h_inode = d_inode(a->h_path.dentry);
  21626. + if (ia && (ia->ia_valid & ATTR_SIZE)) {
  21627. + inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
  21628. + if (ia->ia_size < i_size_read(a->h_inode))
  21629. + sz = ia->ia_size;
  21630. + inode_unlock_shared(a->h_inode);
  21631. + }
  21632. +
  21633. + hi_wh = NULL;
  21634. + if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
  21635. + hi_wh = au_hi_wh(inode, a->btgt);
  21636. + if (!hi_wh) {
  21637. + struct au_cp_generic cpg = {
  21638. + .dentry = dentry,
  21639. + .bdst = a->btgt,
  21640. + .bsrc = -1,
  21641. + .len = sz,
  21642. + .pin = &a->pin
  21643. + };
  21644. + err = au_sio_cpup_wh(&cpg, /*file*/NULL);
  21645. + if (unlikely(err))
  21646. + goto out_unlock;
  21647. + hi_wh = au_hi_wh(inode, a->btgt);
  21648. + /* todo: revalidate hi_wh? */
  21649. + }
  21650. + }
  21651. +
  21652. + if (parent) {
  21653. + au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
  21654. + di_downgrade_lock(parent, AuLock_IR);
  21655. + dput(parent);
  21656. + parent = NULL;
  21657. + }
  21658. + if (!au_ftest_icpup(a->flags, DID_CPUP))
  21659. + goto out; /* success */
  21660. +
  21661. + if (!d_unhashed(dentry)) {
  21662. + struct au_cp_generic cpg = {
  21663. + .dentry = dentry,
  21664. + .bdst = a->btgt,
  21665. + .bsrc = btop,
  21666. + .len = sz,
  21667. + .pin = &a->pin,
  21668. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  21669. + };
  21670. + err = au_sio_cpup_simple(&cpg);
  21671. + if (!err)
  21672. + a->h_path.dentry = au_h_dptr(dentry, a->btgt);
  21673. + } else if (!hi_wh)
  21674. + a->h_path.dentry = au_h_dptr(dentry, a->btgt);
  21675. + else
  21676. + a->h_path.dentry = hi_wh; /* do not dget here */
  21677. +
  21678. +out_unlock:
  21679. + a->h_inode = d_inode(a->h_path.dentry);
  21680. + if (!err)
  21681. + goto out; /* success */
  21682. + au_unpin(&a->pin);
  21683. +out_parent:
  21684. + if (parent) {
  21685. + di_write_unlock(parent);
  21686. + dput(parent);
  21687. + }
  21688. +out:
  21689. + if (!err)
  21690. + inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
  21691. + return err;
  21692. +}
  21693. +
  21694. +static int aufs_setattr(struct user_namespace *userns, struct dentry *dentry,
  21695. + struct iattr *ia)
  21696. +{
  21697. + int err;
  21698. + struct inode *inode, *delegated;
  21699. + struct super_block *sb;
  21700. + struct file *file;
  21701. + struct au_icpup_args *a;
  21702. + struct user_namespace *h_userns;
  21703. +
  21704. + inode = d_inode(dentry);
  21705. + IMustLock(inode);
  21706. +
  21707. + err = setattr_prepare(userns, dentry, ia);
  21708. + if (unlikely(err))
  21709. + goto out;
  21710. +
  21711. + err = -ENOMEM;
  21712. + a = kzalloc(sizeof(*a), GFP_NOFS);
  21713. + if (unlikely(!a))
  21714. + goto out;
  21715. +
  21716. + if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
  21717. + ia->ia_valid &= ~ATTR_MODE;
  21718. +
  21719. + file = NULL;
  21720. + sb = dentry->d_sb;
  21721. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  21722. + if (unlikely(err))
  21723. + goto out_kfree;
  21724. +
  21725. + if (ia->ia_valid & ATTR_FILE) {
  21726. + /* currently ftruncate(2) only */
  21727. + AuDebugOn(!d_is_reg(dentry));
  21728. + file = ia->ia_file;
  21729. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
  21730. + /*fi_lsc*/0);
  21731. + if (unlikely(err))
  21732. + goto out_si;
  21733. + ia->ia_file = au_hf_top(file);
  21734. + a->udba = AuOpt_UDBA_NONE;
  21735. + } else {
  21736. + /* fchmod() doesn't pass ia_file */
  21737. + a->udba = au_opt_udba(sb);
  21738. + di_write_lock_child(dentry);
  21739. + /* no d_unlinked(), to set UDBA_NONE for root */
  21740. + if (d_unhashed(dentry))
  21741. + a->udba = AuOpt_UDBA_NONE;
  21742. + if (a->udba != AuOpt_UDBA_NONE) {
  21743. + AuDebugOn(IS_ROOT(dentry));
  21744. + err = au_reval_for_attr(dentry, au_sigen(sb));
  21745. + if (unlikely(err))
  21746. + goto out_dentry;
  21747. + }
  21748. + }
  21749. +
  21750. + err = au_pin_and_icpup(dentry, ia, a);
  21751. + if (unlikely(err < 0))
  21752. + goto out_dentry;
  21753. + if (au_ftest_icpup(a->flags, DID_CPUP)) {
  21754. + ia->ia_file = NULL;
  21755. + ia->ia_valid &= ~ATTR_FILE;
  21756. + }
  21757. +
  21758. + a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
  21759. + if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
  21760. + == (ATTR_MODE | ATTR_CTIME)) {
  21761. + err = security_path_chmod(&a->h_path, ia->ia_mode);
  21762. + if (unlikely(err))
  21763. + goto out_unlock;
  21764. + } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
  21765. + && (ia->ia_valid & ATTR_CTIME)) {
  21766. + err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
  21767. + if (unlikely(err))
  21768. + goto out_unlock;
  21769. + }
  21770. +
  21771. + if (ia->ia_valid & ATTR_SIZE) {
  21772. + struct file *f;
  21773. +
  21774. + if (ia->ia_size < i_size_read(inode))
  21775. + /* unmap only */
  21776. + truncate_setsize(inode, ia->ia_size);
  21777. +
  21778. + f = NULL;
  21779. + if (ia->ia_valid & ATTR_FILE)
  21780. + f = ia->ia_file;
  21781. + inode_unlock(a->h_inode);
  21782. + err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
  21783. + inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
  21784. + } else {
  21785. + delegated = NULL;
  21786. + while (1) {
  21787. + err = vfsub_notify_change(&a->h_path, ia, &delegated);
  21788. + if (delegated) {
  21789. + err = break_deleg_wait(&delegated);
  21790. + if (!err)
  21791. + continue;
  21792. + }
  21793. + break;
  21794. + }
  21795. + }
  21796. + /*
  21797. + * regardless aufs 'acl' option setting.
  21798. + * why don't all acl-aware fs call this func from their ->setattr()?
  21799. + */
  21800. + if (!err && (ia->ia_valid & ATTR_MODE)) {
  21801. + h_userns = mnt_user_ns(a->h_path.mnt);
  21802. + err = vfsub_acl_chmod(h_userns, a->h_inode, ia->ia_mode);
  21803. + }
  21804. + if (!err)
  21805. + au_cpup_attr_changeable(inode);
  21806. +
  21807. +out_unlock:
  21808. + inode_unlock(a->h_inode);
  21809. + au_unpin(&a->pin);
  21810. + if (unlikely(err))
  21811. + au_update_dbtop(dentry);
  21812. +out_dentry:
  21813. + di_write_unlock(dentry);
  21814. + if (file) {
  21815. + fi_write_unlock(file);
  21816. + ia->ia_file = file;
  21817. + ia->ia_valid |= ATTR_FILE;
  21818. + }
  21819. +out_si:
  21820. + si_read_unlock(sb);
  21821. +out_kfree:
  21822. + au_kfree_rcu(a);
  21823. +out:
  21824. + AuTraceErr(err);
  21825. + return err;
  21826. +}
  21827. +
  21828. +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
  21829. +static int au_h_path_to_set_attr(struct dentry *dentry,
  21830. + struct au_icpup_args *a, struct path *h_path)
  21831. +{
  21832. + int err;
  21833. + struct super_block *sb;
  21834. +
  21835. + sb = dentry->d_sb;
  21836. + a->udba = au_opt_udba(sb);
  21837. + /* no d_unlinked(), to set UDBA_NONE for root */
  21838. + if (d_unhashed(dentry))
  21839. + a->udba = AuOpt_UDBA_NONE;
  21840. + if (a->udba != AuOpt_UDBA_NONE) {
  21841. + AuDebugOn(IS_ROOT(dentry));
  21842. + err = au_reval_for_attr(dentry, au_sigen(sb));
  21843. + if (unlikely(err))
  21844. + goto out;
  21845. + }
  21846. + err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
  21847. + if (unlikely(err < 0))
  21848. + goto out;
  21849. +
  21850. + h_path->dentry = a->h_path.dentry;
  21851. + h_path->mnt = au_sbr_mnt(sb, a->btgt);
  21852. +
  21853. +out:
  21854. + return err;
  21855. +}
  21856. +
  21857. +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
  21858. + struct au_sxattr *arg)
  21859. +{
  21860. + int err;
  21861. + struct path h_path;
  21862. + struct super_block *sb;
  21863. + struct au_icpup_args *a;
  21864. + struct inode *h_inode;
  21865. + struct user_namespace *h_userns;
  21866. +
  21867. + IMustLock(inode);
  21868. +
  21869. + err = -ENOMEM;
  21870. + a = kzalloc(sizeof(*a), GFP_NOFS);
  21871. + if (unlikely(!a))
  21872. + goto out;
  21873. +
  21874. + sb = dentry->d_sb;
  21875. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  21876. + if (unlikely(err))
  21877. + goto out_kfree;
  21878. +
  21879. + h_path.dentry = NULL; /* silence gcc */
  21880. + di_write_lock_child(dentry);
  21881. + err = au_h_path_to_set_attr(dentry, a, &h_path);
  21882. + if (unlikely(err))
  21883. + goto out_di;
  21884. + h_userns = mnt_user_ns(h_path.mnt);
  21885. +
  21886. + inode_unlock(a->h_inode);
  21887. + switch (arg->type) {
  21888. + case AU_XATTR_SET:
  21889. + AuDebugOn(d_is_negative(h_path.dentry));
  21890. + err = vfsub_setxattr(h_userns, h_path.dentry,
  21891. + arg->u.set.name, arg->u.set.value,
  21892. + arg->u.set.size, arg->u.set.flags);
  21893. + break;
  21894. + case AU_ACL_SET:
  21895. + err = -EOPNOTSUPP;
  21896. + h_inode = d_inode(h_path.dentry);
  21897. + if (h_inode->i_op->set_acl) {
  21898. + /* this will call posix_acl_update_mode */
  21899. + err = h_inode->i_op->set_acl(h_userns, h_inode,
  21900. + arg->u.acl_set.acl,
  21901. + arg->u.acl_set.type);
  21902. + }
  21903. + break;
  21904. + }
  21905. + if (!err)
  21906. + au_cpup_attr_timesizes(inode);
  21907. +
  21908. + au_unpin(&a->pin);
  21909. + if (unlikely(err))
  21910. + au_update_dbtop(dentry);
  21911. +
  21912. +out_di:
  21913. + di_write_unlock(dentry);
  21914. + si_read_unlock(sb);
  21915. +out_kfree:
  21916. + au_kfree_rcu(a);
  21917. +out:
  21918. + AuTraceErr(err);
  21919. + return err;
  21920. +}
  21921. +#endif
  21922. +
  21923. +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
  21924. + unsigned int nlink)
  21925. +{
  21926. + unsigned int n;
  21927. +
  21928. + inode->i_mode = st->mode;
  21929. + /* don't i_[ug]id_write() here */
  21930. + inode->i_uid = st->uid;
  21931. + inode->i_gid = st->gid;
  21932. + inode->i_atime = st->atime;
  21933. + inode->i_mtime = st->mtime;
  21934. + inode->i_ctime = st->ctime;
  21935. +
  21936. + au_cpup_attr_nlink(inode, /*force*/0);
  21937. + if (S_ISDIR(inode->i_mode)) {
  21938. + n = inode->i_nlink;
  21939. + n -= nlink;
  21940. + n += st->nlink;
  21941. + smp_mb(); /* for i_nlink */
  21942. + /* 0 can happen */
  21943. + set_nlink(inode, n);
  21944. + }
  21945. +
  21946. + spin_lock(&inode->i_lock);
  21947. + inode->i_blocks = st->blocks;
  21948. + i_size_write(inode, st->size);
  21949. + spin_unlock(&inode->i_lock);
  21950. +}
  21951. +
  21952. +/*
  21953. + * common routine for aufs_getattr() and au_getxattr().
  21954. + * returns zero or negative (an error).
  21955. + * @dentry will be read-locked in success.
  21956. + */
  21957. +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
  21958. + struct path *h_path, int locked)
  21959. +{
  21960. + int err;
  21961. + unsigned int mnt_flags, sigen;
  21962. + unsigned char udba_none;
  21963. + aufs_bindex_t bindex;
  21964. + struct super_block *sb, *h_sb;
  21965. +
  21966. + h_path->mnt = NULL;
  21967. + h_path->dentry = NULL;
  21968. +
  21969. + err = 0;
  21970. + sb = dentry->d_sb;
  21971. + mnt_flags = au_mntflags(sb);
  21972. + udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
  21973. +
  21974. + if (unlikely(locked))
  21975. + goto body; /* skip locking dinfo */
  21976. +
  21977. + /* support fstat(2) */
  21978. + if (!d_unlinked(dentry) && !udba_none) {
  21979. + sigen = au_sigen(sb);
  21980. + err = au_digen_test(dentry, sigen);
  21981. + if (!err) {
  21982. + di_read_lock_child(dentry, AuLock_IR);
  21983. + err = au_dbrange_test(dentry);
  21984. + if (unlikely(err)) {
  21985. + di_read_unlock(dentry, AuLock_IR);
  21986. + goto out;
  21987. + }
  21988. + } else {
  21989. + AuDebugOn(IS_ROOT(dentry));
  21990. + di_write_lock_child(dentry);
  21991. + err = au_dbrange_test(dentry);
  21992. + if (!err)
  21993. + err = au_reval_for_attr(dentry, sigen);
  21994. + if (!err)
  21995. + di_downgrade_lock(dentry, AuLock_IR);
  21996. + else {
  21997. + di_write_unlock(dentry);
  21998. + goto out;
  21999. + }
  22000. + }
  22001. + } else
  22002. + di_read_lock_child(dentry, AuLock_IR);
  22003. +
  22004. +body:
  22005. + if (!inode) {
  22006. + inode = d_inode(dentry);
  22007. + if (unlikely(!inode))
  22008. + goto out;
  22009. + }
  22010. + bindex = au_ibtop(inode);
  22011. + h_path->mnt = au_sbr_mnt(sb, bindex);
  22012. + h_sb = h_path->mnt->mnt_sb;
  22013. + if (!force
  22014. + && !au_test_fs_bad_iattr(h_sb)
  22015. + && udba_none)
  22016. + goto out; /* success */
  22017. +
  22018. + if (au_dbtop(dentry) == bindex)
  22019. + h_path->dentry = au_h_dptr(dentry, bindex);
  22020. + else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
  22021. + h_path->dentry = au_plink_lkup(inode, bindex);
  22022. + if (IS_ERR(h_path->dentry))
  22023. + /* pretending success */
  22024. + h_path->dentry = NULL;
  22025. + else
  22026. + dput(h_path->dentry);
  22027. + }
  22028. +
  22029. +out:
  22030. + return err;
  22031. +}
  22032. +
  22033. +static int aufs_getattr(struct user_namespace *userns, const struct path *path,
  22034. + struct kstat *st, u32 request, unsigned int query)
  22035. +{
  22036. + int err;
  22037. + unsigned char positive;
  22038. + struct path h_path;
  22039. + struct dentry *dentry;
  22040. + struct inode *inode;
  22041. + struct super_block *sb;
  22042. +
  22043. + dentry = path->dentry;
  22044. + inode = d_inode(dentry);
  22045. + sb = dentry->d_sb;
  22046. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  22047. + if (unlikely(err))
  22048. + goto out;
  22049. + err = au_h_path_getattr(dentry, /*inode*/NULL, /*force*/0, &h_path,
  22050. + /*locked*/0);
  22051. + if (unlikely(err))
  22052. + goto out_si;
  22053. + if (unlikely(!h_path.dentry))
  22054. + /* illegally overlapped or something */
  22055. + goto out_fill; /* pretending success */
  22056. +
  22057. + positive = d_is_positive(h_path.dentry);
  22058. + if (positive)
  22059. + /* no vfsub version */
  22060. + err = vfs_getattr(&h_path, st, request, query);
  22061. + if (!err) {
  22062. + if (positive)
  22063. + au_refresh_iattr(inode, st,
  22064. + d_inode(h_path.dentry)->i_nlink);
  22065. + goto out_fill; /* success */
  22066. + }
  22067. + AuTraceErr(err);
  22068. + goto out_di;
  22069. +
  22070. +out_fill:
  22071. + generic_fillattr(userns, inode, st);
  22072. +out_di:
  22073. + di_read_unlock(dentry, AuLock_IR);
  22074. +out_si:
  22075. + si_read_unlock(sb);
  22076. +out:
  22077. + AuTraceErr(err);
  22078. + return err;
  22079. +}
  22080. +
  22081. +/* ---------------------------------------------------------------------- */
  22082. +
  22083. +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
  22084. + struct delayed_call *done)
  22085. +{
  22086. + const char *ret;
  22087. + struct dentry *h_dentry;
  22088. + struct inode *h_inode;
  22089. + int err;
  22090. + aufs_bindex_t bindex;
  22091. +
  22092. + ret = NULL; /* suppress a warning */
  22093. + err = -ECHILD;
  22094. + if (!dentry)
  22095. + goto out;
  22096. +
  22097. + err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
  22098. + if (unlikely(err))
  22099. + goto out;
  22100. +
  22101. + err = au_d_hashed_positive(dentry);
  22102. + if (unlikely(err))
  22103. + goto out_unlock;
  22104. +
  22105. + err = -EINVAL;
  22106. + inode = d_inode(dentry);
  22107. + bindex = au_ibtop(inode);
  22108. + h_inode = au_h_iptr(inode, bindex);
  22109. + if (unlikely(!h_inode->i_op->get_link))
  22110. + goto out_unlock;
  22111. +
  22112. + err = -EBUSY;
  22113. + h_dentry = NULL;
  22114. + if (au_dbtop(dentry) <= bindex) {
  22115. + h_dentry = au_h_dptr(dentry, bindex);
  22116. + if (h_dentry)
  22117. + dget(h_dentry);
  22118. + }
  22119. + if (!h_dentry) {
  22120. + h_dentry = d_find_any_alias(h_inode);
  22121. + if (IS_ERR(h_dentry)) {
  22122. + err = PTR_ERR(h_dentry);
  22123. + goto out_unlock;
  22124. + }
  22125. + }
  22126. + if (unlikely(!h_dentry))
  22127. + goto out_unlock;
  22128. +
  22129. + err = 0;
  22130. + AuDbg("%ps\n", h_inode->i_op->get_link);
  22131. + AuDbgDentry(h_dentry);
  22132. + ret = vfs_get_link(h_dentry, done);
  22133. + dput(h_dentry);
  22134. + if (IS_ERR(ret))
  22135. + err = PTR_ERR(ret);
  22136. +
  22137. +out_unlock:
  22138. + aufs_read_unlock(dentry, AuLock_IR);
  22139. +out:
  22140. + if (unlikely(err))
  22141. + ret = ERR_PTR(err);
  22142. + AuTraceErrPtr(ret);
  22143. + return ret;
  22144. +}
  22145. +
  22146. +/* ---------------------------------------------------------------------- */
  22147. +
  22148. +static int au_is_special(struct inode *inode)
  22149. +{
  22150. + return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
  22151. +}
  22152. +
  22153. +static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
  22154. + int flags)
  22155. +{
  22156. + int err;
  22157. + aufs_bindex_t bindex;
  22158. + struct super_block *sb;
  22159. + struct inode *h_inode;
  22160. + struct vfsmount *h_mnt;
  22161. +
  22162. + sb = inode->i_sb;
  22163. + WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
  22164. + "unexpected s_flags 0x%lx", sb->s_flags);
  22165. +
  22166. + /* mmap_sem might be acquired already, cf. aufs_mmap() */
  22167. + lockdep_off();
  22168. + si_read_lock(sb, AuLock_FLUSH);
  22169. + ii_write_lock_child(inode);
  22170. +
  22171. + err = 0;
  22172. + bindex = au_ibtop(inode);
  22173. + h_inode = au_h_iptr(inode, bindex);
  22174. + if (!au_test_ro(sb, bindex, inode)) {
  22175. + h_mnt = au_sbr_mnt(sb, bindex);
  22176. + err = vfsub_mnt_want_write(h_mnt);
  22177. + if (!err) {
  22178. + err = vfsub_update_time(h_inode, ts, flags);
  22179. + vfsub_mnt_drop_write(h_mnt);
  22180. + }
  22181. + } else if (au_is_special(h_inode)) {
  22182. + /*
  22183. + * Never copy-up here.
  22184. + * These special files may already be opened and used for
  22185. + * communicating. If we copied it up, then the communication
  22186. + * would be corrupted.
  22187. + */
  22188. + AuWarn1("timestamps for i%lu are ignored "
  22189. + "since it is on readonly branch (hi%lu).\n",
  22190. + inode->i_ino, h_inode->i_ino);
  22191. + } else if (flags & ~S_ATIME) {
  22192. + err = -EIO;
  22193. + AuIOErr1("unexpected flags 0x%x\n", flags);
  22194. + AuDebugOn(1);
  22195. + }
  22196. +
  22197. + if (!err)
  22198. + au_cpup_attr_timesizes(inode);
  22199. + ii_write_unlock(inode);
  22200. + si_read_unlock(sb);
  22201. + lockdep_on();
  22202. +
  22203. + if (!err && (flags & S_VERSION))
  22204. + inode_inc_iversion(inode);
  22205. +
  22206. + return err;
  22207. +}
  22208. +
  22209. +/* ---------------------------------------------------------------------- */
  22210. +
  22211. +/* no getattr version will be set by module.c:aufs_init() */
  22212. +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
  22213. + aufs_iop[] = {
  22214. + [AuIop_SYMLINK] = {
  22215. + .permission = aufs_permission,
  22216. +#ifdef CONFIG_FS_POSIX_ACL
  22217. + .get_acl = aufs_get_acl,
  22218. + .set_acl = aufs_set_acl, /* unsupport for symlink? */
  22219. +#endif
  22220. +
  22221. + .setattr = aufs_setattr,
  22222. + .getattr = aufs_getattr,
  22223. +
  22224. +#ifdef CONFIG_AUFS_XATTR
  22225. + .listxattr = aufs_listxattr,
  22226. +#endif
  22227. +
  22228. + .get_link = aufs_get_link,
  22229. +
  22230. + /* .update_time = aufs_update_time */
  22231. + },
  22232. + [AuIop_DIR] = {
  22233. + .create = aufs_create,
  22234. + .lookup = aufs_lookup,
  22235. + .link = aufs_link,
  22236. + .unlink = aufs_unlink,
  22237. + .symlink = aufs_symlink,
  22238. + .mkdir = aufs_mkdir,
  22239. + .rmdir = aufs_rmdir,
  22240. + .mknod = aufs_mknod,
  22241. + .rename = aufs_rename,
  22242. +
  22243. + .permission = aufs_permission,
  22244. +#ifdef CONFIG_FS_POSIX_ACL
  22245. + .get_acl = aufs_get_acl,
  22246. + .set_acl = aufs_set_acl,
  22247. +#endif
  22248. +
  22249. + .setattr = aufs_setattr,
  22250. + .getattr = aufs_getattr,
  22251. +
  22252. +#ifdef CONFIG_AUFS_XATTR
  22253. + .listxattr = aufs_listxattr,
  22254. +#endif
  22255. +
  22256. + .update_time = aufs_update_time,
  22257. + .atomic_open = aufs_atomic_open,
  22258. + .tmpfile = aufs_tmpfile
  22259. + },
  22260. + [AuIop_OTHER] = {
  22261. + .permission = aufs_permission,
  22262. +#ifdef CONFIG_FS_POSIX_ACL
  22263. + .get_acl = aufs_get_acl,
  22264. + .set_acl = aufs_set_acl,
  22265. +#endif
  22266. +
  22267. + .setattr = aufs_setattr,
  22268. + .getattr = aufs_getattr,
  22269. +
  22270. +#ifdef CONFIG_AUFS_XATTR
  22271. + .listxattr = aufs_listxattr,
  22272. +#endif
  22273. +
  22274. + .update_time = aufs_update_time
  22275. + }
  22276. +};
  22277. diff -Naur null/fs/aufs/i_op_del.c linux-5.15.131/fs/aufs/i_op_del.c
  22278. --- /dev/null
  22279. +++ linux-5.15.131/fs/aufs/i_op_del.c 2023-09-11 14:32:48.149722539 +0300
  22280. @@ -0,0 +1,522 @@
  22281. +// SPDX-License-Identifier: GPL-2.0
  22282. +/*
  22283. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  22284. + *
  22285. + * This program, aufs is free software; you can redistribute it and/or modify
  22286. + * it under the terms of the GNU General Public License as published by
  22287. + * the Free Software Foundation; either version 2 of the License, or
  22288. + * (at your option) any later version.
  22289. + *
  22290. + * This program is distributed in the hope that it will be useful,
  22291. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22292. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22293. + * GNU General Public License for more details.
  22294. + *
  22295. + * You should have received a copy of the GNU General Public License
  22296. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22297. + */
  22298. +
  22299. +/*
  22300. + * inode operations (del entry)
  22301. + */
  22302. +
  22303. +#include <linux/iversion.h>
  22304. +#include "aufs.h"
  22305. +
  22306. +/*
  22307. + * decide if a new whiteout for @dentry is necessary or not.
  22308. + * when it is necessary, prepare the parent dir for the upper branch whose
  22309. + * branch index is @bcpup for creation. the actual creation of the whiteout will
  22310. + * be done by caller.
  22311. + * return value:
  22312. + * 0: wh is unnecessary
  22313. + * plus: wh is necessary
  22314. + * minus: error
  22315. + */
  22316. +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
  22317. +{
  22318. + int need_wh, err;
  22319. + aufs_bindex_t btop;
  22320. + struct super_block *sb;
  22321. +
  22322. + sb = dentry->d_sb;
  22323. + btop = au_dbtop(dentry);
  22324. + if (*bcpup < 0) {
  22325. + *bcpup = btop;
  22326. + if (au_test_ro(sb, btop, d_inode(dentry))) {
  22327. + err = AuWbrCopyup(au_sbi(sb), dentry);
  22328. + *bcpup = err;
  22329. + if (unlikely(err < 0))
  22330. + goto out;
  22331. + }
  22332. + } else
  22333. + AuDebugOn(btop < *bcpup
  22334. + || au_test_ro(sb, *bcpup, d_inode(dentry)));
  22335. + AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
  22336. +
  22337. + if (*bcpup != btop) {
  22338. + err = au_cpup_dirs(dentry, *bcpup);
  22339. + if (unlikely(err))
  22340. + goto out;
  22341. + need_wh = 1;
  22342. + } else {
  22343. + struct au_dinfo *dinfo, *tmp;
  22344. +
  22345. + need_wh = -ENOMEM;
  22346. + dinfo = au_di(dentry);
  22347. + tmp = au_di_alloc(sb, AuLsc_DI_TMP);
  22348. + if (tmp) {
  22349. + au_di_cp(tmp, dinfo);
  22350. + au_di_swap(tmp, dinfo);
  22351. + /* returns the number of positive dentries */
  22352. + need_wh = au_lkup_dentry(dentry, btop + 1,
  22353. + /* AuLkup_IGNORE_PERM */ 0);
  22354. + au_di_swap(tmp, dinfo);
  22355. + au_rw_write_unlock(&tmp->di_rwsem);
  22356. + au_di_free(tmp);
  22357. + }
  22358. + }
  22359. + AuDbg("need_wh %d\n", need_wh);
  22360. + err = need_wh;
  22361. +
  22362. +out:
  22363. + return err;
  22364. +}
  22365. +
  22366. +/*
  22367. + * simple tests for the del-entry operations.
  22368. + * following the checks in vfs, plus the parent-child relationship.
  22369. + */
  22370. +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
  22371. + struct dentry *h_parent, int isdir)
  22372. +{
  22373. + int err;
  22374. + umode_t h_mode;
  22375. + struct dentry *h_dentry, *h_latest;
  22376. + struct inode *h_inode;
  22377. + struct path h_ppath;
  22378. + struct super_block *sb;
  22379. + struct au_branch *br;
  22380. + struct user_namespace *h_userns;
  22381. +
  22382. + h_dentry = au_h_dptr(dentry, bindex);
  22383. + if (d_really_is_positive(dentry)) {
  22384. + err = -ENOENT;
  22385. + if (unlikely(d_is_negative(h_dentry)))
  22386. + goto out;
  22387. + h_inode = d_inode(h_dentry);
  22388. + if (unlikely(!h_inode->i_nlink))
  22389. + goto out;
  22390. +
  22391. + h_mode = h_inode->i_mode;
  22392. + if (!isdir) {
  22393. + err = -EISDIR;
  22394. + if (unlikely(S_ISDIR(h_mode)))
  22395. + goto out;
  22396. + } else if (unlikely(!S_ISDIR(h_mode))) {
  22397. + err = -ENOTDIR;
  22398. + goto out;
  22399. + }
  22400. + } else {
  22401. + /* rename(2) case */
  22402. + err = -EIO;
  22403. + if (unlikely(d_is_positive(h_dentry)))
  22404. + goto out;
  22405. + }
  22406. +
  22407. + err = -ENOENT;
  22408. + /* expected parent dir is locked */
  22409. + if (unlikely(h_parent != h_dentry->d_parent))
  22410. + goto out;
  22411. + err = 0;
  22412. +
  22413. + /*
  22414. + * rmdir a dir may break the consistency on some filesystem.
  22415. + * let's try heavy test.
  22416. + */
  22417. + err = -EACCES;
  22418. + sb = dentry->d_sb;
  22419. + br = au_sbr(sb, bindex);
  22420. + h_userns = au_br_userns(br);
  22421. + if (unlikely(!au_opt_test(au_mntflags(sb), DIRPERM1)
  22422. + && au_test_h_perm(h_userns, d_inode(h_parent),
  22423. + MAY_EXEC | MAY_WRITE)))
  22424. + goto out;
  22425. +
  22426. + h_ppath.dentry = h_parent;
  22427. + h_ppath.mnt = au_br_mnt(br);
  22428. + h_latest = au_sio_lkup_one(h_userns, &dentry->d_name, &h_ppath);
  22429. + err = -EIO;
  22430. + if (IS_ERR(h_latest))
  22431. + goto out;
  22432. + if (h_latest == h_dentry)
  22433. + err = 0;
  22434. + dput(h_latest);
  22435. +
  22436. +out:
  22437. + return err;
  22438. +}
  22439. +
  22440. +/*
  22441. + * decide the branch where we operate for @dentry. the branch index will be set
  22442. + * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
  22443. + * dir for reverting.
  22444. + * when a new whiteout is necessary, create it.
  22445. + */
  22446. +static struct dentry*
  22447. +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
  22448. + struct au_dtime *dt, struct au_pin *pin)
  22449. +{
  22450. + struct dentry *wh_dentry;
  22451. + struct super_block *sb;
  22452. + struct path h_path;
  22453. + int err, need_wh;
  22454. + unsigned int udba;
  22455. + aufs_bindex_t bcpup;
  22456. +
  22457. + need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
  22458. + wh_dentry = ERR_PTR(need_wh);
  22459. + if (unlikely(need_wh < 0))
  22460. + goto out;
  22461. +
  22462. + sb = dentry->d_sb;
  22463. + udba = au_opt_udba(sb);
  22464. + bcpup = *rbcpup;
  22465. + err = au_pin(pin, dentry, bcpup, udba,
  22466. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  22467. + wh_dentry = ERR_PTR(err);
  22468. + if (unlikely(err))
  22469. + goto out;
  22470. +
  22471. + h_path.dentry = au_pinned_h_parent(pin);
  22472. + if (udba != AuOpt_UDBA_NONE
  22473. + && au_dbtop(dentry) == bcpup) {
  22474. + err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
  22475. + wh_dentry = ERR_PTR(err);
  22476. + if (unlikely(err))
  22477. + goto out_unpin;
  22478. + }
  22479. +
  22480. + h_path.mnt = au_sbr_mnt(sb, bcpup);
  22481. + au_dtime_store(dt, au_pinned_parent(pin), &h_path);
  22482. + wh_dentry = NULL;
  22483. + if (!need_wh)
  22484. + goto out; /* success, no need to create whiteout */
  22485. +
  22486. + wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
  22487. + if (IS_ERR(wh_dentry))
  22488. + goto out_unpin;
  22489. +
  22490. + /* returns with the parent is locked and wh_dentry is dget-ed */
  22491. + goto out; /* success */
  22492. +
  22493. +out_unpin:
  22494. + au_unpin(pin);
  22495. +out:
  22496. + return wh_dentry;
  22497. +}
  22498. +
  22499. +/*
  22500. + * when removing a dir, rename it to a unique temporary whiteout-ed name first
  22501. + * in order to be revertible and save time for removing many child whiteouts
  22502. + * under the dir.
  22503. + * returns 1 when there are too many child whiteout and caller should remove
  22504. + * them asynchronously. returns 0 when the number of children is enough small to
  22505. + * remove now or the branch fs is a remote fs.
  22506. + * otherwise return an error.
  22507. + */
  22508. +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
  22509. + struct au_nhash *whlist, struct inode *dir)
  22510. +{
  22511. + int rmdir_later, err, dirwh;
  22512. + struct dentry *h_dentry;
  22513. + struct super_block *sb;
  22514. + struct inode *inode;
  22515. +
  22516. + sb = dentry->d_sb;
  22517. + SiMustAnyLock(sb);
  22518. + h_dentry = au_h_dptr(dentry, bindex);
  22519. + err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
  22520. + if (unlikely(err))
  22521. + goto out;
  22522. +
  22523. + /* stop monitoring */
  22524. + inode = d_inode(dentry);
  22525. + au_hn_free(au_hi(inode, bindex));
  22526. +
  22527. + if (!au_test_fs_remote(h_dentry->d_sb)) {
  22528. + dirwh = au_sbi(sb)->si_dirwh;
  22529. + rmdir_later = (dirwh <= 1);
  22530. + if (!rmdir_later)
  22531. + rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
  22532. + dirwh);
  22533. + if (rmdir_later)
  22534. + return rmdir_later;
  22535. + }
  22536. +
  22537. + err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
  22538. + if (unlikely(err)) {
  22539. + AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
  22540. + h_dentry, bindex, err);
  22541. + err = 0;
  22542. + }
  22543. +
  22544. +out:
  22545. + AuTraceErr(err);
  22546. + return err;
  22547. +}
  22548. +
  22549. +/*
  22550. + * final procedure for deleting a entry.
  22551. + * maintain dentry and iattr.
  22552. + */
  22553. +static void epilog(struct inode *dir, struct dentry *dentry,
  22554. + aufs_bindex_t bindex)
  22555. +{
  22556. + struct inode *inode;
  22557. +
  22558. + inode = d_inode(dentry);
  22559. + d_drop(dentry);
  22560. + inode->i_ctime = dir->i_ctime;
  22561. +
  22562. + au_dir_ts(dir, bindex);
  22563. + inode_inc_iversion(dir);
  22564. +}
  22565. +
  22566. +/*
  22567. + * when an error happened, remove the created whiteout and revert everything.
  22568. + */
  22569. +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
  22570. + aufs_bindex_t bwh, struct dentry *wh_dentry,
  22571. + struct dentry *dentry, struct au_dtime *dt)
  22572. +{
  22573. + int rerr;
  22574. + struct path h_path = {
  22575. + .dentry = wh_dentry,
  22576. + .mnt = au_sbr_mnt(dir->i_sb, bindex)
  22577. + };
  22578. +
  22579. + rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
  22580. + if (!rerr) {
  22581. + au_set_dbwh(dentry, bwh);
  22582. + au_dtime_revert(dt);
  22583. + return 0;
  22584. + }
  22585. +
  22586. + AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
  22587. + return -EIO;
  22588. +}
  22589. +
  22590. +/* ---------------------------------------------------------------------- */
  22591. +
  22592. +int aufs_unlink(struct inode *dir, struct dentry *dentry)
  22593. +{
  22594. + int err;
  22595. + aufs_bindex_t bwh, bindex, btop;
  22596. + struct inode *inode, *h_dir, *delegated;
  22597. + struct dentry *parent, *wh_dentry;
  22598. + /* to reduce stack size */
  22599. + struct {
  22600. + struct au_dtime dt;
  22601. + struct au_pin pin;
  22602. + struct path h_path;
  22603. + } *a;
  22604. +
  22605. + IMustLock(dir);
  22606. +
  22607. + err = -ENOMEM;
  22608. + a = kmalloc(sizeof(*a), GFP_NOFS);
  22609. + if (unlikely(!a))
  22610. + goto out;
  22611. +
  22612. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
  22613. + if (unlikely(err))
  22614. + goto out_free;
  22615. + err = au_d_hashed_positive(dentry);
  22616. + if (unlikely(err))
  22617. + goto out_unlock;
  22618. + inode = d_inode(dentry);
  22619. + IMustLock(inode);
  22620. + err = -EISDIR;
  22621. + if (unlikely(d_is_dir(dentry)))
  22622. + goto out_unlock; /* possible? */
  22623. +
  22624. + btop = au_dbtop(dentry);
  22625. + bwh = au_dbwh(dentry);
  22626. + bindex = -1;
  22627. + parent = dentry->d_parent; /* dir inode is locked */
  22628. + di_write_lock_parent(parent);
  22629. + wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
  22630. + &a->pin);
  22631. + err = PTR_ERR(wh_dentry);
  22632. + if (IS_ERR(wh_dentry))
  22633. + goto out_parent;
  22634. +
  22635. + a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
  22636. + a->h_path.dentry = au_h_dptr(dentry, btop);
  22637. + dget(a->h_path.dentry);
  22638. + if (bindex == btop) {
  22639. + h_dir = au_pinned_h_dir(&a->pin);
  22640. + delegated = NULL;
  22641. + err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
  22642. + if (unlikely(err == -EWOULDBLOCK)) {
  22643. + pr_warn("cannot retry for NFSv4 delegation"
  22644. + " for an internal unlink\n");
  22645. + iput(delegated);
  22646. + }
  22647. + } else {
  22648. + /* dir inode is locked */
  22649. + h_dir = d_inode(wh_dentry->d_parent);
  22650. + IMustLock(h_dir);
  22651. + err = 0;
  22652. + }
  22653. +
  22654. + if (!err) {
  22655. + vfsub_drop_nlink(inode);
  22656. + epilog(dir, dentry, bindex);
  22657. +
  22658. + /* update target timestamps */
  22659. + if (bindex == btop) {
  22660. + vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
  22661. + /*ignore*/
  22662. + inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
  22663. + } else
  22664. + /* todo: this timestamp may be reverted later */
  22665. + inode->i_ctime = h_dir->i_ctime;
  22666. + goto out_unpin; /* success */
  22667. + }
  22668. +
  22669. + /* revert */
  22670. + if (wh_dentry) {
  22671. + int rerr;
  22672. +
  22673. + rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
  22674. + &a->dt);
  22675. + if (rerr)
  22676. + err = rerr;
  22677. + }
  22678. +
  22679. +out_unpin:
  22680. + au_unpin(&a->pin);
  22681. + dput(wh_dentry);
  22682. + dput(a->h_path.dentry);
  22683. +out_parent:
  22684. + di_write_unlock(parent);
  22685. +out_unlock:
  22686. + aufs_read_unlock(dentry, AuLock_DW);
  22687. +out_free:
  22688. + au_kfree_rcu(a);
  22689. +out:
  22690. + return err;
  22691. +}
  22692. +
  22693. +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
  22694. +{
  22695. + int err, rmdir_later;
  22696. + aufs_bindex_t bwh, bindex, btop;
  22697. + struct inode *inode;
  22698. + struct dentry *parent, *wh_dentry, *h_dentry;
  22699. + struct au_whtmp_rmdir *args;
  22700. + /* to reduce stack size */
  22701. + struct {
  22702. + struct au_dtime dt;
  22703. + struct au_pin pin;
  22704. + } *a;
  22705. +
  22706. + IMustLock(dir);
  22707. +
  22708. + err = -ENOMEM;
  22709. + a = kmalloc(sizeof(*a), GFP_NOFS);
  22710. + if (unlikely(!a))
  22711. + goto out;
  22712. +
  22713. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
  22714. + if (unlikely(err))
  22715. + goto out_free;
  22716. + err = au_alive_dir(dentry);
  22717. + if (unlikely(err))
  22718. + goto out_unlock;
  22719. + inode = d_inode(dentry);
  22720. + IMustLock(inode);
  22721. + err = -ENOTDIR;
  22722. + if (unlikely(!d_is_dir(dentry)))
  22723. + goto out_unlock; /* possible? */
  22724. +
  22725. + err = -ENOMEM;
  22726. + args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
  22727. + if (unlikely(!args))
  22728. + goto out_unlock;
  22729. +
  22730. + parent = dentry->d_parent; /* dir inode is locked */
  22731. + di_write_lock_parent(parent);
  22732. + err = au_test_empty(dentry, &args->whlist);
  22733. + if (unlikely(err))
  22734. + goto out_parent;
  22735. +
  22736. + btop = au_dbtop(dentry);
  22737. + bwh = au_dbwh(dentry);
  22738. + bindex = -1;
  22739. + wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
  22740. + &a->pin);
  22741. + err = PTR_ERR(wh_dentry);
  22742. + if (IS_ERR(wh_dentry))
  22743. + goto out_parent;
  22744. +
  22745. + h_dentry = au_h_dptr(dentry, btop);
  22746. + dget(h_dentry);
  22747. + rmdir_later = 0;
  22748. + if (bindex == btop) {
  22749. + err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
  22750. + if (err > 0) {
  22751. + rmdir_later = err;
  22752. + err = 0;
  22753. + }
  22754. + } else {
  22755. + /* stop monitoring */
  22756. + au_hn_free(au_hi(inode, btop));
  22757. +
  22758. + /* dir inode is locked */
  22759. + IMustLock(d_inode(wh_dentry->d_parent));
  22760. + err = 0;
  22761. + }
  22762. +
  22763. + if (!err) {
  22764. + vfsub_dead_dir(inode);
  22765. + au_set_dbdiropq(dentry, -1);
  22766. + epilog(dir, dentry, bindex);
  22767. +
  22768. + if (rmdir_later) {
  22769. + au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
  22770. + args = NULL;
  22771. + }
  22772. +
  22773. + goto out_unpin; /* success */
  22774. + }
  22775. +
  22776. + /* revert */
  22777. + AuLabel(revert);
  22778. + if (wh_dentry) {
  22779. + int rerr;
  22780. +
  22781. + rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
  22782. + &a->dt);
  22783. + if (rerr)
  22784. + err = rerr;
  22785. + }
  22786. +
  22787. +out_unpin:
  22788. + au_unpin(&a->pin);
  22789. + dput(wh_dentry);
  22790. + dput(h_dentry);
  22791. +out_parent:
  22792. + di_write_unlock(parent);
  22793. + if (args)
  22794. + au_whtmp_rmdir_free(args);
  22795. +out_unlock:
  22796. + aufs_read_unlock(dentry, AuLock_DW);
  22797. +out_free:
  22798. + au_kfree_rcu(a);
  22799. +out:
  22800. + AuTraceErr(err);
  22801. + return err;
  22802. +}
  22803. diff -Naur null/fs/aufs/i_op_ren.c linux-5.15.131/fs/aufs/i_op_ren.c
  22804. --- /dev/null
  22805. +++ linux-5.15.131/fs/aufs/i_op_ren.c 2023-09-11 14:32:48.149722539 +0300
  22806. @@ -0,0 +1,1257 @@
  22807. +// SPDX-License-Identifier: GPL-2.0
  22808. +/*
  22809. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  22810. + *
  22811. + * This program, aufs is free software; you can redistribute it and/or modify
  22812. + * it under the terms of the GNU General Public License as published by
  22813. + * the Free Software Foundation; either version 2 of the License, or
  22814. + * (at your option) any later version.
  22815. + *
  22816. + * This program is distributed in the hope that it will be useful,
  22817. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22818. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22819. + * GNU General Public License for more details.
  22820. + *
  22821. + * You should have received a copy of the GNU General Public License
  22822. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22823. + */
  22824. +
  22825. +/*
  22826. + * inode operation (rename entry)
  22827. + * todo: this is crazy monster
  22828. + */
  22829. +
  22830. +#include <linux/iversion.h>
  22831. +#include "aufs.h"
  22832. +
  22833. +enum { AuSRC, AuDST, AuSrcDst };
  22834. +enum { AuPARENT, AuCHILD, AuParentChild };
  22835. +
  22836. +#define AuRen_ISDIR_SRC 1
  22837. +#define AuRen_ISDIR_DST (1 << 1)
  22838. +#define AuRen_ISSAMEDIR (1 << 2)
  22839. +#define AuRen_WHSRC (1 << 3)
  22840. +#define AuRen_WHDST (1 << 4)
  22841. +#define AuRen_MNT_WRITE (1 << 5)
  22842. +#define AuRen_DT_DSTDIR (1 << 6)
  22843. +#define AuRen_DIROPQ_SRC (1 << 7)
  22844. +#define AuRen_DIROPQ_DST (1 << 8)
  22845. +#define AuRen_DIRREN (1 << 9)
  22846. +#define AuRen_DROPPED_SRC (1 << 10)
  22847. +#define AuRen_DROPPED_DST (1 << 11)
  22848. +#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
  22849. +#define au_fset_ren(flags, name) \
  22850. + do { (flags) |= AuRen_##name; } while (0)
  22851. +#define au_fclr_ren(flags, name) \
  22852. + do { (flags) &= ~AuRen_##name; } while (0)
  22853. +
  22854. +#ifndef CONFIG_AUFS_DIRREN
  22855. +#undef AuRen_DIRREN
  22856. +#define AuRen_DIRREN 0
  22857. +#endif
  22858. +
  22859. +struct au_ren_args {
  22860. + struct {
  22861. + struct dentry *dentry, *h_dentry, *parent, *h_parent,
  22862. + *wh_dentry;
  22863. + struct inode *dir, *inode;
  22864. + struct au_hinode *hdir, *hinode;
  22865. + struct au_dtime dt[AuParentChild];
  22866. + aufs_bindex_t btop, bdiropq;
  22867. + } sd[AuSrcDst];
  22868. +
  22869. +#define src_dentry sd[AuSRC].dentry
  22870. +#define src_dir sd[AuSRC].dir
  22871. +#define src_inode sd[AuSRC].inode
  22872. +#define src_h_dentry sd[AuSRC].h_dentry
  22873. +#define src_parent sd[AuSRC].parent
  22874. +#define src_h_parent sd[AuSRC].h_parent
  22875. +#define src_wh_dentry sd[AuSRC].wh_dentry
  22876. +#define src_hdir sd[AuSRC].hdir
  22877. +#define src_hinode sd[AuSRC].hinode
  22878. +#define src_h_dir sd[AuSRC].hdir->hi_inode
  22879. +#define src_dt sd[AuSRC].dt
  22880. +#define src_btop sd[AuSRC].btop
  22881. +#define src_bdiropq sd[AuSRC].bdiropq
  22882. +
  22883. +#define dst_dentry sd[AuDST].dentry
  22884. +#define dst_dir sd[AuDST].dir
  22885. +#define dst_inode sd[AuDST].inode
  22886. +#define dst_h_dentry sd[AuDST].h_dentry
  22887. +#define dst_parent sd[AuDST].parent
  22888. +#define dst_h_parent sd[AuDST].h_parent
  22889. +#define dst_wh_dentry sd[AuDST].wh_dentry
  22890. +#define dst_hdir sd[AuDST].hdir
  22891. +#define dst_hinode sd[AuDST].hinode
  22892. +#define dst_h_dir sd[AuDST].hdir->hi_inode
  22893. +#define dst_dt sd[AuDST].dt
  22894. +#define dst_btop sd[AuDST].btop
  22895. +#define dst_bdiropq sd[AuDST].bdiropq
  22896. +
  22897. + struct dentry *h_trap;
  22898. + struct au_branch *br;
  22899. + struct path h_path;
  22900. + struct au_nhash whlist;
  22901. + aufs_bindex_t btgt, src_bwh;
  22902. +
  22903. + struct {
  22904. + unsigned short auren_flags;
  22905. + unsigned char flags; /* syscall parameter */
  22906. + unsigned char exchange;
  22907. + } __packed;
  22908. +
  22909. + struct au_whtmp_rmdir *thargs;
  22910. + struct dentry *h_dst;
  22911. + struct au_hinode *h_root;
  22912. +};
  22913. +
  22914. +/* ---------------------------------------------------------------------- */
  22915. +
  22916. +/*
  22917. + * functions for reverting.
  22918. + * when an error happened in a single rename systemcall, we should revert
  22919. + * everything as if nothing happened.
  22920. + * we don't need to revert the copied-up/down the parent dir since they are
  22921. + * harmless.
  22922. + */
  22923. +
  22924. +#define RevertFailure(fmt, ...) do { \
  22925. + AuIOErr("revert failure: " fmt " (%d, %d)\n", \
  22926. + ##__VA_ARGS__, err, rerr); \
  22927. + err = -EIO; \
  22928. +} while (0)
  22929. +
  22930. +static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
  22931. +{
  22932. + int rerr;
  22933. + struct dentry *d;
  22934. +#define src_or_dst(member) a->sd[idx].member
  22935. +
  22936. + d = src_or_dst(dentry); /* {src,dst}_dentry */
  22937. + au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
  22938. + rerr = au_diropq_remove(d, a->btgt);
  22939. + au_hn_inode_unlock(src_or_dst(hinode));
  22940. + au_set_dbdiropq(d, src_or_dst(bdiropq));
  22941. + if (rerr)
  22942. + RevertFailure("remove diropq %pd", d);
  22943. +
  22944. +#undef src_or_dst_
  22945. +}
  22946. +
  22947. +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
  22948. +{
  22949. + if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
  22950. + au_ren_do_rev_diropq(err, a, AuSRC);
  22951. + if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
  22952. + au_ren_do_rev_diropq(err, a, AuDST);
  22953. +}
  22954. +
  22955. +static void au_ren_rev_rename(int err, struct au_ren_args *a)
  22956. +{
  22957. + int rerr;
  22958. + struct inode *delegated;
  22959. + struct path h_ppath = {
  22960. + .dentry = a->src_h_parent,
  22961. + .mnt = a->h_path.mnt
  22962. + };
  22963. +
  22964. + a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name, &h_ppath);
  22965. + rerr = PTR_ERR(a->h_path.dentry);
  22966. + if (IS_ERR(a->h_path.dentry)) {
  22967. + RevertFailure("lkup one %pd", a->src_dentry);
  22968. + return;
  22969. + }
  22970. +
  22971. + delegated = NULL;
  22972. + rerr = vfsub_rename(a->dst_h_dir,
  22973. + au_h_dptr(a->src_dentry, a->btgt),
  22974. + a->src_h_dir, &a->h_path, &delegated, a->flags);
  22975. + if (unlikely(rerr == -EWOULDBLOCK)) {
  22976. + pr_warn("cannot retry for NFSv4 delegation"
  22977. + " for an internal rename\n");
  22978. + iput(delegated);
  22979. + }
  22980. + d_drop(a->h_path.dentry);
  22981. + dput(a->h_path.dentry);
  22982. + /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
  22983. + if (rerr)
  22984. + RevertFailure("rename %pd", a->src_dentry);
  22985. +}
  22986. +
  22987. +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
  22988. +{
  22989. + int rerr;
  22990. + struct inode *delegated;
  22991. + struct path h_ppath = {
  22992. + .dentry = a->dst_h_parent,
  22993. + .mnt = a->h_path.mnt
  22994. + };
  22995. +
  22996. + a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name, &h_ppath);
  22997. + rerr = PTR_ERR(a->h_path.dentry);
  22998. + if (IS_ERR(a->h_path.dentry)) {
  22999. + RevertFailure("lkup one %pd", a->dst_dentry);
  23000. + return;
  23001. + }
  23002. + if (d_is_positive(a->h_path.dentry)) {
  23003. + d_drop(a->h_path.dentry);
  23004. + dput(a->h_path.dentry);
  23005. + return;
  23006. + }
  23007. +
  23008. + delegated = NULL;
  23009. + rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
  23010. + &delegated, a->flags);
  23011. + if (unlikely(rerr == -EWOULDBLOCK)) {
  23012. + pr_warn("cannot retry for NFSv4 delegation"
  23013. + " for an internal rename\n");
  23014. + iput(delegated);
  23015. + }
  23016. + d_drop(a->h_path.dentry);
  23017. + dput(a->h_path.dentry);
  23018. + if (!rerr)
  23019. + au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
  23020. + else
  23021. + RevertFailure("rename %pd", a->h_dst);
  23022. +}
  23023. +
  23024. +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
  23025. +{
  23026. + int rerr;
  23027. +
  23028. + a->h_path.dentry = a->src_wh_dentry;
  23029. + rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
  23030. + au_set_dbwh(a->src_dentry, a->src_bwh);
  23031. + if (rerr)
  23032. + RevertFailure("unlink %pd", a->src_wh_dentry);
  23033. +}
  23034. +#undef RevertFailure
  23035. +
  23036. +/* ---------------------------------------------------------------------- */
  23037. +
  23038. +/*
  23039. + * when we have to copyup the renaming entry, do it with the rename-target name
  23040. + * in order to minimize the cost (the later actual rename is unnecessary).
  23041. + * otherwise rename it on the target branch.
  23042. + */
  23043. +static int au_ren_or_cpup(struct au_ren_args *a)
  23044. +{
  23045. + int err;
  23046. + struct dentry *d;
  23047. + struct inode *delegated;
  23048. +
  23049. + d = a->src_dentry;
  23050. + if (au_dbtop(d) == a->btgt) {
  23051. + a->h_path.dentry = a->dst_h_dentry;
  23052. + AuDebugOn(au_dbtop(d) != a->btgt);
  23053. + delegated = NULL;
  23054. + err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
  23055. + a->dst_h_dir, &a->h_path, &delegated,
  23056. + a->flags);
  23057. + if (unlikely(err == -EWOULDBLOCK)) {
  23058. + pr_warn("cannot retry for NFSv4 delegation"
  23059. + " for an internal rename\n");
  23060. + iput(delegated);
  23061. + }
  23062. + } else
  23063. + BUG();
  23064. +
  23065. + if (!err && a->h_dst)
  23066. + /* it will be set to dinfo later */
  23067. + dget(a->h_dst);
  23068. +
  23069. + return err;
  23070. +}
  23071. +
  23072. +/* cf. aufs_rmdir() */
  23073. +static int au_ren_del_whtmp(struct au_ren_args *a)
  23074. +{
  23075. + int err;
  23076. + struct inode *dir;
  23077. +
  23078. + dir = a->dst_dir;
  23079. + SiMustAnyLock(dir->i_sb);
  23080. + if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
  23081. + au_sbi(dir->i_sb)->si_dirwh)
  23082. + || au_test_fs_remote(a->h_dst->d_sb)) {
  23083. + err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
  23084. + if (unlikely(err))
  23085. + pr_warn("failed removing whtmp dir %pd (%d), "
  23086. + "ignored.\n", a->h_dst, err);
  23087. + } else {
  23088. + au_nhash_wh_free(&a->thargs->whlist);
  23089. + a->thargs->whlist = a->whlist;
  23090. + a->whlist.nh_num = 0;
  23091. + au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
  23092. + dput(a->h_dst);
  23093. + a->thargs = NULL;
  23094. + }
  23095. +
  23096. + return 0;
  23097. +}
  23098. +
  23099. +/* make it 'opaque' dir. */
  23100. +static int au_ren_do_diropq(struct au_ren_args *a, int idx)
  23101. +{
  23102. + int err;
  23103. + struct dentry *d, *diropq;
  23104. +#define src_or_dst(member) a->sd[idx].member
  23105. +
  23106. + err = 0;
  23107. + d = src_or_dst(dentry); /* {src,dst}_dentry */
  23108. + src_or_dst(bdiropq) = au_dbdiropq(d);
  23109. + src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
  23110. + au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
  23111. + diropq = au_diropq_create(d, a->btgt);
  23112. + au_hn_inode_unlock(src_or_dst(hinode));
  23113. + if (IS_ERR(diropq))
  23114. + err = PTR_ERR(diropq);
  23115. + else
  23116. + dput(diropq);
  23117. +
  23118. +#undef src_or_dst_
  23119. + return err;
  23120. +}
  23121. +
  23122. +static int au_ren_diropq(struct au_ren_args *a)
  23123. +{
  23124. + int err;
  23125. + unsigned char always;
  23126. + struct dentry *d;
  23127. +
  23128. + err = 0;
  23129. + d = a->dst_dentry; /* already renamed on the branch */
  23130. + always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
  23131. + if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
  23132. + && !au_ftest_ren(a->auren_flags, DIRREN)
  23133. + && a->btgt != au_dbdiropq(a->src_dentry)
  23134. + && (a->dst_wh_dentry
  23135. + || a->btgt <= au_dbdiropq(d)
  23136. + /* hide the lower to keep xino */
  23137. + /* the lowers may not be a dir, but we hide them anyway */
  23138. + || a->btgt < au_dbbot(d)
  23139. + || always)) {
  23140. + AuDbg("here\n");
  23141. + err = au_ren_do_diropq(a, AuSRC);
  23142. + if (unlikely(err))
  23143. + goto out;
  23144. + au_fset_ren(a->auren_flags, DIROPQ_SRC);
  23145. + }
  23146. + if (!a->exchange)
  23147. + goto out; /* success */
  23148. +
  23149. + d = a->src_dentry; /* already renamed on the branch */
  23150. + if (au_ftest_ren(a->auren_flags, ISDIR_DST)
  23151. + && a->btgt != au_dbdiropq(a->dst_dentry)
  23152. + && (a->btgt < au_dbdiropq(d)
  23153. + || a->btgt < au_dbbot(d)
  23154. + || always)) {
  23155. + AuDbgDentry(a->src_dentry);
  23156. + AuDbgDentry(a->dst_dentry);
  23157. + err = au_ren_do_diropq(a, AuDST);
  23158. + if (unlikely(err))
  23159. + goto out_rev_src;
  23160. + au_fset_ren(a->auren_flags, DIROPQ_DST);
  23161. + }
  23162. + goto out; /* success */
  23163. +
  23164. +out_rev_src:
  23165. + AuDbg("err %d, reverting src\n", err);
  23166. + au_ren_rev_diropq(err, a);
  23167. +out:
  23168. + return err;
  23169. +}
  23170. +
  23171. +static int do_rename(struct au_ren_args *a)
  23172. +{
  23173. + int err;
  23174. + struct dentry *d, *h_d;
  23175. +
  23176. + if (!a->exchange) {
  23177. + /* prepare workqueue args for asynchronous rmdir */
  23178. + h_d = a->dst_h_dentry;
  23179. + if (au_ftest_ren(a->auren_flags, ISDIR_DST)
  23180. + /* && !au_ftest_ren(a->auren_flags, DIRREN) */
  23181. + && d_is_positive(h_d)) {
  23182. + err = -ENOMEM;
  23183. + a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
  23184. + GFP_NOFS);
  23185. + if (unlikely(!a->thargs))
  23186. + goto out;
  23187. + a->h_dst = dget(h_d);
  23188. + }
  23189. +
  23190. + /* create whiteout for src_dentry */
  23191. + if (au_ftest_ren(a->auren_flags, WHSRC)) {
  23192. + a->src_bwh = au_dbwh(a->src_dentry);
  23193. + AuDebugOn(a->src_bwh >= 0);
  23194. + a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
  23195. + a->src_h_parent);
  23196. + err = PTR_ERR(a->src_wh_dentry);
  23197. + if (IS_ERR(a->src_wh_dentry))
  23198. + goto out_thargs;
  23199. + }
  23200. +
  23201. + /* lookup whiteout for dentry */
  23202. + if (au_ftest_ren(a->auren_flags, WHDST)) {
  23203. + h_d = au_wh_lkup(a->dst_h_parent,
  23204. + &a->dst_dentry->d_name, a->br);
  23205. + err = PTR_ERR(h_d);
  23206. + if (IS_ERR(h_d))
  23207. + goto out_whsrc;
  23208. + if (d_is_negative(h_d))
  23209. + dput(h_d);
  23210. + else
  23211. + a->dst_wh_dentry = h_d;
  23212. + }
  23213. +
  23214. + /* rename dentry to tmpwh */
  23215. + if (a->thargs) {
  23216. + err = au_whtmp_ren(a->dst_h_dentry, a->br);
  23217. + if (unlikely(err))
  23218. + goto out_whdst;
  23219. +
  23220. + d = a->dst_dentry;
  23221. + au_set_h_dptr(d, a->btgt, NULL);
  23222. + err = au_lkup_neg(d, a->btgt, /*wh*/0);
  23223. + if (unlikely(err))
  23224. + goto out_whtmp;
  23225. + a->dst_h_dentry = au_h_dptr(d, a->btgt);
  23226. + }
  23227. + }
  23228. +
  23229. + BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
  23230. +#if 0 /* debugging */
  23231. + BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
  23232. + && d_is_positive(a->dst_h_dentry)
  23233. + && a->src_btop != a->btgt);
  23234. +#endif
  23235. +
  23236. + /* rename by vfs_rename or cpup */
  23237. + err = au_ren_or_cpup(a);
  23238. + if (unlikely(err))
  23239. + /* leave the copied-up one */
  23240. + goto out_whtmp;
  23241. +
  23242. + /* make dir opaque */
  23243. + err = au_ren_diropq(a);
  23244. + if (unlikely(err))
  23245. + goto out_rename;
  23246. +
  23247. + /* update target timestamps */
  23248. + if (a->exchange) {
  23249. + AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
  23250. + a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
  23251. + vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
  23252. + a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
  23253. + }
  23254. + AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
  23255. + a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
  23256. + vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
  23257. + a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
  23258. +
  23259. + if (!a->exchange) {
  23260. + /* remove whiteout for dentry */
  23261. + if (a->dst_wh_dentry) {
  23262. + a->h_path.dentry = a->dst_wh_dentry;
  23263. + err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
  23264. + a->dst_dentry);
  23265. + if (unlikely(err))
  23266. + goto out_diropq;
  23267. + }
  23268. +
  23269. + /* remove whtmp */
  23270. + if (a->thargs)
  23271. + au_ren_del_whtmp(a); /* ignore this error */
  23272. +
  23273. + au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
  23274. + }
  23275. + err = 0;
  23276. + goto out_success;
  23277. +
  23278. +out_diropq:
  23279. + au_ren_rev_diropq(err, a);
  23280. +out_rename:
  23281. + au_ren_rev_rename(err, a);
  23282. + dput(a->h_dst);
  23283. +out_whtmp:
  23284. + if (a->thargs)
  23285. + au_ren_rev_whtmp(err, a);
  23286. +out_whdst:
  23287. + dput(a->dst_wh_dentry);
  23288. + a->dst_wh_dentry = NULL;
  23289. +out_whsrc:
  23290. + if (a->src_wh_dentry)
  23291. + au_ren_rev_whsrc(err, a);
  23292. +out_success:
  23293. + dput(a->src_wh_dentry);
  23294. + dput(a->dst_wh_dentry);
  23295. +out_thargs:
  23296. + if (a->thargs) {
  23297. + dput(a->h_dst);
  23298. + au_whtmp_rmdir_free(a->thargs);
  23299. + a->thargs = NULL;
  23300. + }
  23301. +out:
  23302. + return err;
  23303. +}
  23304. +
  23305. +/* ---------------------------------------------------------------------- */
  23306. +
  23307. +/*
  23308. + * test if @dentry dir can be rename destination or not.
  23309. + * success means, it is a logically empty dir.
  23310. + */
  23311. +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
  23312. +{
  23313. + return au_test_empty(dentry, whlist);
  23314. +}
  23315. +
  23316. +/*
  23317. + * test if @a->src_dentry dir can be rename source or not.
  23318. + * if it can, return 0.
  23319. + * success means,
  23320. + * - it is a logically empty dir.
  23321. + * - or, it exists on writable branch and has no children including whiteouts
  23322. + * on the lower branch unless DIRREN is on.
  23323. + */
  23324. +static int may_rename_srcdir(struct au_ren_args *a)
  23325. +{
  23326. + int err;
  23327. + unsigned int rdhash;
  23328. + aufs_bindex_t btop, btgt;
  23329. + struct dentry *dentry;
  23330. + struct super_block *sb;
  23331. + struct au_sbinfo *sbinfo;
  23332. +
  23333. + dentry = a->src_dentry;
  23334. + sb = dentry->d_sb;
  23335. + sbinfo = au_sbi(sb);
  23336. + if (au_opt_test(sbinfo->si_mntflags, DIRREN))
  23337. + au_fset_ren(a->auren_flags, DIRREN);
  23338. +
  23339. + btgt = a->btgt;
  23340. + btop = au_dbtop(dentry);
  23341. + if (btop != btgt) {
  23342. + struct au_nhash whlist;
  23343. +
  23344. + SiMustAnyLock(sb);
  23345. + rdhash = sbinfo->si_rdhash;
  23346. + if (!rdhash)
  23347. + rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
  23348. + dentry));
  23349. + err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
  23350. + if (unlikely(err))
  23351. + goto out;
  23352. + err = au_test_empty(dentry, &whlist);
  23353. + au_nhash_wh_free(&whlist);
  23354. + goto out;
  23355. + }
  23356. +
  23357. + if (btop == au_dbtaildir(dentry))
  23358. + return 0; /* success */
  23359. +
  23360. + err = au_test_empty_lower(dentry);
  23361. +
  23362. +out:
  23363. + if (err == -ENOTEMPTY) {
  23364. + if (au_ftest_ren(a->auren_flags, DIRREN)) {
  23365. + err = 0;
  23366. + } else {
  23367. + AuWarn1("renaming dir who has child(ren) on multiple "
  23368. + "branches, is not supported\n");
  23369. + err = -EXDEV;
  23370. + }
  23371. + }
  23372. + return err;
  23373. +}
  23374. +
  23375. +/* side effect: sets whlist and h_dentry */
  23376. +static int au_ren_may_dir(struct au_ren_args *a)
  23377. +{
  23378. + int err;
  23379. + unsigned int rdhash;
  23380. + struct dentry *d;
  23381. +
  23382. + d = a->dst_dentry;
  23383. + SiMustAnyLock(d->d_sb);
  23384. +
  23385. + err = 0;
  23386. + if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
  23387. + rdhash = au_sbi(d->d_sb)->si_rdhash;
  23388. + if (!rdhash)
  23389. + rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
  23390. + err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
  23391. + if (unlikely(err))
  23392. + goto out;
  23393. +
  23394. + if (!a->exchange) {
  23395. + au_set_dbtop(d, a->dst_btop);
  23396. + err = may_rename_dstdir(d, &a->whlist);
  23397. + au_set_dbtop(d, a->btgt);
  23398. + } else
  23399. + err = may_rename_srcdir(a);
  23400. + }
  23401. + a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
  23402. + if (unlikely(err))
  23403. + goto out;
  23404. +
  23405. + d = a->src_dentry;
  23406. + a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
  23407. + if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
  23408. + err = may_rename_srcdir(a);
  23409. + if (unlikely(err)) {
  23410. + au_nhash_wh_free(&a->whlist);
  23411. + a->whlist.nh_num = 0;
  23412. + }
  23413. + }
  23414. +out:
  23415. + return err;
  23416. +}
  23417. +
  23418. +/* ---------------------------------------------------------------------- */
  23419. +
  23420. +/*
  23421. + * simple tests for rename.
  23422. + * following the checks in vfs, plus the parent-child relationship.
  23423. + */
  23424. +static int au_may_ren(struct au_ren_args *a)
  23425. +{
  23426. + int err, isdir;
  23427. + struct inode *h_inode;
  23428. +
  23429. + if (a->src_btop == a->btgt) {
  23430. + err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
  23431. + au_ftest_ren(a->auren_flags, ISDIR_SRC));
  23432. + if (unlikely(err))
  23433. + goto out;
  23434. + err = -EINVAL;
  23435. + if (unlikely(a->src_h_dentry == a->h_trap))
  23436. + goto out;
  23437. + }
  23438. +
  23439. + err = 0;
  23440. + if (a->dst_btop != a->btgt)
  23441. + goto out;
  23442. +
  23443. + err = -ENOTEMPTY;
  23444. + if (unlikely(a->dst_h_dentry == a->h_trap))
  23445. + goto out;
  23446. +
  23447. + err = -EIO;
  23448. + isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
  23449. + if (d_really_is_negative(a->dst_dentry)) {
  23450. + if (d_is_negative(a->dst_h_dentry))
  23451. + err = au_may_add(a->dst_dentry, a->btgt,
  23452. + a->dst_h_parent, isdir);
  23453. + } else {
  23454. + if (unlikely(d_is_negative(a->dst_h_dentry)))
  23455. + goto out;
  23456. + h_inode = d_inode(a->dst_h_dentry);
  23457. + if (h_inode->i_nlink)
  23458. + err = au_may_del(a->dst_dentry, a->btgt,
  23459. + a->dst_h_parent, isdir);
  23460. + }
  23461. +
  23462. +out:
  23463. + if (unlikely(err == -ENOENT || err == -EEXIST))
  23464. + err = -EIO;
  23465. + AuTraceErr(err);
  23466. + return err;
  23467. +}
  23468. +
  23469. +/* ---------------------------------------------------------------------- */
  23470. +
  23471. +/*
  23472. + * locking order
  23473. + * (VFS)
  23474. + * - src_dir and dir by lock_rename()
  23475. + * - inode if exists
  23476. + * (aufs)
  23477. + * - lock all
  23478. + * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
  23479. + * + si_read_lock
  23480. + * + di_write_lock2_child()
  23481. + * + di_write_lock_child()
  23482. + * + ii_write_lock_child()
  23483. + * + di_write_lock_child2()
  23484. + * + ii_write_lock_child2()
  23485. + * + src_parent and parent
  23486. + * + di_write_lock_parent()
  23487. + * + ii_write_lock_parent()
  23488. + * + di_write_lock_parent2()
  23489. + * + ii_write_lock_parent2()
  23490. + * + lower src_dir and dir by vfsub_lock_rename()
  23491. + * + verify the every relationships between child and parent. if any
  23492. + * of them failed, unlock all and return -EBUSY.
  23493. + */
  23494. +static void au_ren_unlock(struct au_ren_args *a)
  23495. +{
  23496. + vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
  23497. + a->dst_h_parent, a->dst_hdir);
  23498. + if (au_ftest_ren(a->auren_flags, DIRREN)
  23499. + && a->h_root)
  23500. + au_hn_inode_unlock(a->h_root);
  23501. + if (au_ftest_ren(a->auren_flags, MNT_WRITE))
  23502. + vfsub_mnt_drop_write(au_br_mnt(a->br));
  23503. +}
  23504. +
  23505. +static int au_ren_lock(struct au_ren_args *a)
  23506. +{
  23507. + int err;
  23508. + unsigned int udba;
  23509. +
  23510. + err = 0;
  23511. + a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
  23512. + a->src_hdir = au_hi(a->src_dir, a->btgt);
  23513. + a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
  23514. + a->dst_hdir = au_hi(a->dst_dir, a->btgt);
  23515. +
  23516. + err = vfsub_mnt_want_write(au_br_mnt(a->br));
  23517. + if (unlikely(err))
  23518. + goto out;
  23519. + au_fset_ren(a->auren_flags, MNT_WRITE);
  23520. + if (au_ftest_ren(a->auren_flags, DIRREN)) {
  23521. + struct dentry *root;
  23522. + struct inode *dir;
  23523. +
  23524. + /*
  23525. + * sbinfo is already locked, so this ii_read_lock is
  23526. + * unnecessary. but our debugging feature checks it.
  23527. + */
  23528. + root = a->src_inode->i_sb->s_root;
  23529. + if (root != a->src_parent && root != a->dst_parent) {
  23530. + dir = d_inode(root);
  23531. + ii_read_lock_parent3(dir);
  23532. + a->h_root = au_hi(dir, a->btgt);
  23533. + ii_read_unlock(dir);
  23534. + au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
  23535. + }
  23536. + }
  23537. + a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
  23538. + a->dst_h_parent, a->dst_hdir);
  23539. + udba = au_opt_udba(a->src_dentry->d_sb);
  23540. + if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
  23541. + || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
  23542. + err = au_busy_or_stale();
  23543. + if (!err && au_dbtop(a->src_dentry) == a->btgt)
  23544. + err = au_h_verify(a->src_h_dentry, udba,
  23545. + d_inode(a->src_h_parent), a->src_h_parent,
  23546. + a->br);
  23547. + if (!err && au_dbtop(a->dst_dentry) == a->btgt)
  23548. + err = au_h_verify(a->dst_h_dentry, udba,
  23549. + d_inode(a->dst_h_parent), a->dst_h_parent,
  23550. + a->br);
  23551. + if (!err)
  23552. + goto out; /* success */
  23553. +
  23554. + err = au_busy_or_stale();
  23555. + au_ren_unlock(a);
  23556. +
  23557. +out:
  23558. + return err;
  23559. +}
  23560. +
  23561. +/* ---------------------------------------------------------------------- */
  23562. +
  23563. +static void au_ren_refresh_dir(struct au_ren_args *a)
  23564. +{
  23565. + struct inode *dir;
  23566. +
  23567. + dir = a->dst_dir;
  23568. + inode_inc_iversion(dir);
  23569. + if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
  23570. + /* is this updating defined in POSIX? */
  23571. + au_cpup_attr_timesizes(a->src_inode);
  23572. + au_cpup_attr_nlink(dir, /*force*/1);
  23573. + }
  23574. + au_dir_ts(dir, a->btgt);
  23575. +
  23576. + if (a->exchange) {
  23577. + dir = a->src_dir;
  23578. + inode_inc_iversion(dir);
  23579. + if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
  23580. + /* is this updating defined in POSIX? */
  23581. + au_cpup_attr_timesizes(a->dst_inode);
  23582. + au_cpup_attr_nlink(dir, /*force*/1);
  23583. + }
  23584. + au_dir_ts(dir, a->btgt);
  23585. + }
  23586. +
  23587. + if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
  23588. + return;
  23589. +
  23590. + dir = a->src_dir;
  23591. + inode_inc_iversion(dir);
  23592. + if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
  23593. + au_cpup_attr_nlink(dir, /*force*/1);
  23594. + au_dir_ts(dir, a->btgt);
  23595. +}
  23596. +
  23597. +static void au_ren_refresh(struct au_ren_args *a)
  23598. +{
  23599. + aufs_bindex_t bbot, bindex;
  23600. + struct dentry *d, *h_d;
  23601. + struct inode *i, *h_i;
  23602. + struct super_block *sb;
  23603. +
  23604. + d = a->dst_dentry;
  23605. + d_drop(d);
  23606. + if (a->h_dst)
  23607. + /* already dget-ed by au_ren_or_cpup() */
  23608. + au_set_h_dptr(d, a->btgt, a->h_dst);
  23609. +
  23610. + i = a->dst_inode;
  23611. + if (i) {
  23612. + if (!a->exchange) {
  23613. + if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
  23614. + vfsub_drop_nlink(i);
  23615. + else {
  23616. + vfsub_dead_dir(i);
  23617. + au_cpup_attr_timesizes(i);
  23618. + }
  23619. + au_update_dbrange(d, /*do_put_zero*/1);
  23620. + } else
  23621. + au_cpup_attr_nlink(i, /*force*/1);
  23622. + } else {
  23623. + bbot = a->btgt;
  23624. + for (bindex = au_dbtop(d); bindex < bbot; bindex++)
  23625. + au_set_h_dptr(d, bindex, NULL);
  23626. + bbot = au_dbbot(d);
  23627. + for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
  23628. + au_set_h_dptr(d, bindex, NULL);
  23629. + au_update_dbrange(d, /*do_put_zero*/0);
  23630. + }
  23631. +
  23632. + if (a->exchange
  23633. + || au_ftest_ren(a->auren_flags, DIRREN)) {
  23634. + d_drop(a->src_dentry);
  23635. + if (au_ftest_ren(a->auren_flags, DIRREN))
  23636. + au_set_dbwh(a->src_dentry, -1);
  23637. + return;
  23638. + }
  23639. +
  23640. + d = a->src_dentry;
  23641. + au_set_dbwh(d, -1);
  23642. + bbot = au_dbbot(d);
  23643. + for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
  23644. + h_d = au_h_dptr(d, bindex);
  23645. + if (h_d)
  23646. + au_set_h_dptr(d, bindex, NULL);
  23647. + }
  23648. + au_set_dbbot(d, a->btgt);
  23649. +
  23650. + sb = d->d_sb;
  23651. + i = a->src_inode;
  23652. + if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
  23653. + return; /* success */
  23654. +
  23655. + bbot = au_ibbot(i);
  23656. + for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
  23657. + h_i = au_h_iptr(i, bindex);
  23658. + if (h_i) {
  23659. + au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
  23660. + /* ignore this error */
  23661. + au_set_h_iptr(i, bindex, NULL, 0);
  23662. + }
  23663. + }
  23664. + au_set_ibbot(i, a->btgt);
  23665. +}
  23666. +
  23667. +/* ---------------------------------------------------------------------- */
  23668. +
  23669. +/* mainly for link(2) and rename(2) */
  23670. +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
  23671. +{
  23672. + aufs_bindex_t bdiropq, bwh;
  23673. + struct dentry *parent;
  23674. + struct au_branch *br;
  23675. +
  23676. + parent = dentry->d_parent;
  23677. + IMustLock(d_inode(parent)); /* dir is locked */
  23678. +
  23679. + bdiropq = au_dbdiropq(parent);
  23680. + bwh = au_dbwh(dentry);
  23681. + br = au_sbr(dentry->d_sb, btgt);
  23682. + if (au_br_rdonly(br)
  23683. + || (0 <= bdiropq && bdiropq < btgt)
  23684. + || (0 <= bwh && bwh < btgt))
  23685. + btgt = -1;
  23686. +
  23687. + AuDbg("btgt %d\n", btgt);
  23688. + return btgt;
  23689. +}
  23690. +
  23691. +/* sets src_btop, dst_btop and btgt */
  23692. +static int au_ren_wbr(struct au_ren_args *a)
  23693. +{
  23694. + int err;
  23695. + struct au_wr_dir_args wr_dir_args = {
  23696. + /* .force_btgt = -1, */
  23697. + .flags = AuWrDir_ADD_ENTRY
  23698. + };
  23699. +
  23700. + a->src_btop = au_dbtop(a->src_dentry);
  23701. + a->dst_btop = au_dbtop(a->dst_dentry);
  23702. + if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
  23703. + || au_ftest_ren(a->auren_flags, ISDIR_DST))
  23704. + au_fset_wrdir(wr_dir_args.flags, ISDIR);
  23705. + wr_dir_args.force_btgt = a->src_btop;
  23706. + if (a->dst_inode && a->dst_btop < a->src_btop)
  23707. + wr_dir_args.force_btgt = a->dst_btop;
  23708. + wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
  23709. + err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
  23710. + a->btgt = err;
  23711. + if (a->exchange)
  23712. + au_update_dbtop(a->dst_dentry);
  23713. +
  23714. + return err;
  23715. +}
  23716. +
  23717. +static void au_ren_dt(struct au_ren_args *a)
  23718. +{
  23719. + a->h_path.dentry = a->src_h_parent;
  23720. + au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
  23721. + if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
  23722. + a->h_path.dentry = a->dst_h_parent;
  23723. + au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
  23724. + }
  23725. +
  23726. + au_fclr_ren(a->auren_flags, DT_DSTDIR);
  23727. + if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
  23728. + && !a->exchange)
  23729. + return;
  23730. +
  23731. + a->h_path.dentry = a->src_h_dentry;
  23732. + au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
  23733. + if (d_is_positive(a->dst_h_dentry)) {
  23734. + au_fset_ren(a->auren_flags, DT_DSTDIR);
  23735. + a->h_path.dentry = a->dst_h_dentry;
  23736. + au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
  23737. + }
  23738. +}
  23739. +
  23740. +static void au_ren_rev_dt(int err, struct au_ren_args *a)
  23741. +{
  23742. + struct dentry *h_d;
  23743. + struct inode *h_inode;
  23744. +
  23745. + au_dtime_revert(a->src_dt + AuPARENT);
  23746. + if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
  23747. + au_dtime_revert(a->dst_dt + AuPARENT);
  23748. +
  23749. + if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
  23750. + h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
  23751. + h_inode = d_inode(h_d);
  23752. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  23753. + au_dtime_revert(a->src_dt + AuCHILD);
  23754. + inode_unlock(h_inode);
  23755. +
  23756. + if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
  23757. + h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
  23758. + h_inode = d_inode(h_d);
  23759. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  23760. + au_dtime_revert(a->dst_dt + AuCHILD);
  23761. + inode_unlock(h_inode);
  23762. + }
  23763. + }
  23764. +}
  23765. +
  23766. +/* ---------------------------------------------------------------------- */
  23767. +
  23768. +int aufs_rename(struct user_namespace *userns,
  23769. + struct inode *_src_dir, struct dentry *_src_dentry,
  23770. + struct inode *_dst_dir, struct dentry *_dst_dentry,
  23771. + unsigned int _flags)
  23772. +{
  23773. + int err, lock_flags;
  23774. + void *rev;
  23775. + /* reduce stack space */
  23776. + struct au_ren_args *a;
  23777. + struct au_pin pin;
  23778. +
  23779. + AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
  23780. + IMustLock(_src_dir);
  23781. + IMustLock(_dst_dir);
  23782. +
  23783. + err = -EINVAL;
  23784. + if (unlikely(_flags & RENAME_WHITEOUT))
  23785. + goto out;
  23786. +
  23787. + err = -ENOMEM;
  23788. + BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
  23789. + a = kzalloc(sizeof(*a), GFP_NOFS);
  23790. + if (unlikely(!a))
  23791. + goto out;
  23792. +
  23793. + a->flags = _flags;
  23794. + BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
  23795. + && RENAME_EXCHANGE > U8_MAX);
  23796. + a->exchange = _flags & RENAME_EXCHANGE;
  23797. + a->src_dir = _src_dir;
  23798. + a->src_dentry = _src_dentry;
  23799. + a->src_inode = NULL;
  23800. + if (d_really_is_positive(a->src_dentry))
  23801. + a->src_inode = d_inode(a->src_dentry);
  23802. + a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
  23803. + a->dst_dir = _dst_dir;
  23804. + a->dst_dentry = _dst_dentry;
  23805. + a->dst_inode = NULL;
  23806. + if (d_really_is_positive(a->dst_dentry))
  23807. + a->dst_inode = d_inode(a->dst_dentry);
  23808. + a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
  23809. + if (a->dst_inode) {
  23810. + /*
  23811. + * if EXCHANGE && src is non-dir && dst is dir,
  23812. + * dst is not locked.
  23813. + */
  23814. + /* IMustLock(a->dst_inode); */
  23815. + au_igrab(a->dst_inode);
  23816. + }
  23817. +
  23818. + err = -ENOTDIR;
  23819. + lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
  23820. + if (d_is_dir(a->src_dentry)) {
  23821. + au_fset_ren(a->auren_flags, ISDIR_SRC);
  23822. + if (unlikely(!a->exchange
  23823. + && d_really_is_positive(a->dst_dentry)
  23824. + && !d_is_dir(a->dst_dentry)))
  23825. + goto out_free;
  23826. + lock_flags |= AuLock_DIRS;
  23827. + }
  23828. + if (a->dst_inode && d_is_dir(a->dst_dentry)) {
  23829. + au_fset_ren(a->auren_flags, ISDIR_DST);
  23830. + if (unlikely(!a->exchange
  23831. + && d_really_is_positive(a->src_dentry)
  23832. + && !d_is_dir(a->src_dentry)))
  23833. + goto out_free;
  23834. + lock_flags |= AuLock_DIRS;
  23835. + }
  23836. + err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
  23837. + lock_flags);
  23838. + if (unlikely(err))
  23839. + goto out_free;
  23840. +
  23841. + err = au_d_hashed_positive(a->src_dentry);
  23842. + if (unlikely(err))
  23843. + goto out_unlock;
  23844. + err = -ENOENT;
  23845. + if (a->dst_inode) {
  23846. + /*
  23847. + * If it is a dir, VFS unhash it before this
  23848. + * function. It means we cannot rely upon d_unhashed().
  23849. + */
  23850. + if (unlikely(!a->dst_inode->i_nlink))
  23851. + goto out_unlock;
  23852. + if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
  23853. + err = au_d_hashed_positive(a->dst_dentry);
  23854. + if (unlikely(err && !a->exchange))
  23855. + goto out_unlock;
  23856. + } else if (unlikely(IS_DEADDIR(a->dst_inode)))
  23857. + goto out_unlock;
  23858. + } else if (unlikely(d_unhashed(a->dst_dentry)))
  23859. + goto out_unlock;
  23860. +
  23861. + /*
  23862. + * is it possible?
  23863. + * yes, it happened (in linux-3.3-rcN) but I don't know why.
  23864. + * there may exist a problem somewhere else.
  23865. + */
  23866. + err = -EINVAL;
  23867. + if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
  23868. + goto out_unlock;
  23869. +
  23870. + au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
  23871. + di_write_lock_parent(a->dst_parent);
  23872. +
  23873. + /* which branch we process */
  23874. + err = au_ren_wbr(a);
  23875. + if (unlikely(err < 0))
  23876. + goto out_parent;
  23877. + a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
  23878. + a->h_path.mnt = au_br_mnt(a->br);
  23879. +
  23880. + /* are they available to be renamed */
  23881. + err = au_ren_may_dir(a);
  23882. + if (unlikely(err))
  23883. + goto out_children;
  23884. +
  23885. + /* prepare the writable parent dir on the same branch */
  23886. + if (a->dst_btop == a->btgt) {
  23887. + au_fset_ren(a->auren_flags, WHDST);
  23888. + } else {
  23889. + err = au_cpup_dirs(a->dst_dentry, a->btgt);
  23890. + if (unlikely(err))
  23891. + goto out_children;
  23892. + }
  23893. +
  23894. + err = 0;
  23895. + if (!a->exchange) {
  23896. + if (a->src_dir != a->dst_dir) {
  23897. + /*
  23898. + * this temporary unlock is safe,
  23899. + * because both dir->i_mutex are locked.
  23900. + */
  23901. + di_write_unlock(a->dst_parent);
  23902. + di_write_lock_parent(a->src_parent);
  23903. + err = au_wr_dir_need_wh(a->src_dentry,
  23904. + au_ftest_ren(a->auren_flags,
  23905. + ISDIR_SRC),
  23906. + &a->btgt);
  23907. + di_write_unlock(a->src_parent);
  23908. + di_write_lock2_parent(a->src_parent, a->dst_parent,
  23909. + /*isdir*/1);
  23910. + au_fclr_ren(a->auren_flags, ISSAMEDIR);
  23911. + } else
  23912. + err = au_wr_dir_need_wh(a->src_dentry,
  23913. + au_ftest_ren(a->auren_flags,
  23914. + ISDIR_SRC),
  23915. + &a->btgt);
  23916. + }
  23917. + if (unlikely(err < 0))
  23918. + goto out_children;
  23919. + if (err)
  23920. + au_fset_ren(a->auren_flags, WHSRC);
  23921. +
  23922. + /* cpup src */
  23923. + if (a->src_btop != a->btgt) {
  23924. + err = au_pin(&pin, a->src_dentry, a->btgt,
  23925. + au_opt_udba(a->src_dentry->d_sb),
  23926. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  23927. + if (!err) {
  23928. + struct au_cp_generic cpg = {
  23929. + .dentry = a->src_dentry,
  23930. + .bdst = a->btgt,
  23931. + .bsrc = a->src_btop,
  23932. + .len = -1,
  23933. + .pin = &pin,
  23934. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  23935. + };
  23936. + AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
  23937. + err = au_sio_cpup_simple(&cpg);
  23938. + au_unpin(&pin);
  23939. + }
  23940. + if (unlikely(err))
  23941. + goto out_children;
  23942. + a->src_btop = a->btgt;
  23943. + a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
  23944. + if (!a->exchange)
  23945. + au_fset_ren(a->auren_flags, WHSRC);
  23946. + }
  23947. +
  23948. + /* cpup dst */
  23949. + if (a->exchange && a->dst_inode
  23950. + && a->dst_btop != a->btgt) {
  23951. + err = au_pin(&pin, a->dst_dentry, a->btgt,
  23952. + au_opt_udba(a->dst_dentry->d_sb),
  23953. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  23954. + if (!err) {
  23955. + struct au_cp_generic cpg = {
  23956. + .dentry = a->dst_dentry,
  23957. + .bdst = a->btgt,
  23958. + .bsrc = a->dst_btop,
  23959. + .len = -1,
  23960. + .pin = &pin,
  23961. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  23962. + };
  23963. + err = au_sio_cpup_simple(&cpg);
  23964. + au_unpin(&pin);
  23965. + }
  23966. + if (unlikely(err))
  23967. + goto out_children;
  23968. + a->dst_btop = a->btgt;
  23969. + a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
  23970. + }
  23971. +
  23972. + /* lock them all */
  23973. + err = au_ren_lock(a);
  23974. + if (unlikely(err))
  23975. + /* leave the copied-up one */
  23976. + goto out_children;
  23977. +
  23978. + if (!a->exchange) {
  23979. + if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
  23980. + err = au_may_ren(a);
  23981. + else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
  23982. + err = -ENAMETOOLONG;
  23983. + if (unlikely(err))
  23984. + goto out_hdir;
  23985. + }
  23986. +
  23987. + /* store timestamps to be revertible */
  23988. + au_ren_dt(a);
  23989. +
  23990. + /* store dirren info */
  23991. + if (au_ftest_ren(a->auren_flags, DIRREN)) {
  23992. + err = au_dr_rename(a->src_dentry, a->btgt,
  23993. + &a->dst_dentry->d_name, &rev);
  23994. + AuTraceErr(err);
  23995. + if (unlikely(err))
  23996. + goto out_dt;
  23997. + }
  23998. +
  23999. + /* here we go */
  24000. + err = do_rename(a);
  24001. + if (unlikely(err))
  24002. + goto out_dirren;
  24003. +
  24004. + if (au_ftest_ren(a->auren_flags, DIRREN))
  24005. + au_dr_rename_fin(a->src_dentry, a->btgt, rev);
  24006. +
  24007. + /* update dir attributes */
  24008. + au_ren_refresh_dir(a);
  24009. +
  24010. + /* dput/iput all lower dentries */
  24011. + au_ren_refresh(a);
  24012. +
  24013. + goto out_hdir; /* success */
  24014. +
  24015. +out_dirren:
  24016. + if (au_ftest_ren(a->auren_flags, DIRREN))
  24017. + au_dr_rename_rev(a->src_dentry, a->btgt, rev);
  24018. +out_dt:
  24019. + au_ren_rev_dt(err, a);
  24020. +out_hdir:
  24021. + au_ren_unlock(a);
  24022. +out_children:
  24023. + au_nhash_wh_free(&a->whlist);
  24024. + if (err && a->dst_inode && a->dst_btop != a->btgt) {
  24025. + AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
  24026. + au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
  24027. + au_set_dbtop(a->dst_dentry, a->dst_btop);
  24028. + }
  24029. +out_parent:
  24030. + if (!err) {
  24031. + if (d_unhashed(a->src_dentry))
  24032. + au_fset_ren(a->auren_flags, DROPPED_SRC);
  24033. + if (d_unhashed(a->dst_dentry))
  24034. + au_fset_ren(a->auren_flags, DROPPED_DST);
  24035. + if (!a->exchange)
  24036. + d_move(a->src_dentry, a->dst_dentry);
  24037. + else {
  24038. + d_exchange(a->src_dentry, a->dst_dentry);
  24039. + if (au_ftest_ren(a->auren_flags, DROPPED_DST))
  24040. + d_drop(a->dst_dentry);
  24041. + }
  24042. + if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
  24043. + d_drop(a->src_dentry);
  24044. + } else {
  24045. + au_update_dbtop(a->dst_dentry);
  24046. + if (!a->dst_inode)
  24047. + d_drop(a->dst_dentry);
  24048. + }
  24049. + if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
  24050. + di_write_unlock(a->dst_parent);
  24051. + else
  24052. + di_write_unlock2(a->src_parent, a->dst_parent);
  24053. +out_unlock:
  24054. + aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
  24055. +out_free:
  24056. + iput(a->dst_inode);
  24057. + if (a->thargs)
  24058. + au_whtmp_rmdir_free(a->thargs);
  24059. + au_kfree_rcu(a);
  24060. +out:
  24061. + AuTraceErr(err);
  24062. + return err;
  24063. +}
  24064. diff -Naur null/fs/aufs/Kconfig linux-5.15.131/fs/aufs/Kconfig
  24065. --- /dev/null
  24066. +++ linux-5.15.131/fs/aufs/Kconfig 2023-09-11 14:32:48.145722589 +0300
  24067. @@ -0,0 +1,199 @@
  24068. +# SPDX-License-Identifier: GPL-2.0
  24069. +config AUFS_FS
  24070. + tristate "Aufs (Advanced multi layered unification filesystem) support"
  24071. + help
  24072. + Aufs is a stackable unification filesystem such as Unionfs,
  24073. + which unifies several directories and provides a merged single
  24074. + directory.
  24075. + In the early days, aufs was entirely re-designed and
  24076. + re-implemented Unionfs Version 1.x series. Introducing many
  24077. + original ideas, approaches and improvements, it becomes totally
  24078. + different from Unionfs while keeping the basic features.
  24079. +
  24080. +if AUFS_FS
  24081. +choice
  24082. + prompt "Maximum number of branches"
  24083. + default AUFS_BRANCH_MAX_127
  24084. + help
  24085. + Specifies the maximum number of branches (or member directories)
  24086. + in a single aufs. The larger value consumes more system
  24087. + resources and has a minor impact to performance.
  24088. +config AUFS_BRANCH_MAX_127
  24089. + bool "127"
  24090. + help
  24091. + Specifies the maximum number of branches (or member directories)
  24092. + in a single aufs. The larger value consumes more system
  24093. + resources and has a minor impact to performance.
  24094. +config AUFS_BRANCH_MAX_511
  24095. + bool "511"
  24096. + help
  24097. + Specifies the maximum number of branches (or member directories)
  24098. + in a single aufs. The larger value consumes more system
  24099. + resources and has a minor impact to performance.
  24100. +config AUFS_BRANCH_MAX_1023
  24101. + bool "1023"
  24102. + help
  24103. + Specifies the maximum number of branches (or member directories)
  24104. + in a single aufs. The larger value consumes more system
  24105. + resources and has a minor impact to performance.
  24106. +config AUFS_BRANCH_MAX_32767
  24107. + bool "32767"
  24108. + help
  24109. + Specifies the maximum number of branches (or member directories)
  24110. + in a single aufs. The larger value consumes more system
  24111. + resources and has a minor impact to performance.
  24112. +endchoice
  24113. +
  24114. +config AUFS_SBILIST
  24115. + bool
  24116. + depends on AUFS_MAGIC_SYSRQ || PROC_FS
  24117. + default y
  24118. + help
  24119. + Automatic configuration for internal use.
  24120. + When aufs supports Magic SysRq or /proc, enabled automatically.
  24121. +
  24122. +config AUFS_HNOTIFY
  24123. + bool "Detect direct branch access (bypassing aufs)"
  24124. + help
  24125. + If you want to modify files on branches directly, eg. bypassing aufs,
  24126. + and want aufs to detect the changes of them fully, then enable this
  24127. + option and use 'udba=notify' mount option.
  24128. + Currently there is only one available configuration, "fsnotify".
  24129. + It will have a negative impact to the performance.
  24130. + See detail in aufs.5.
  24131. +
  24132. +choice
  24133. + prompt "method" if AUFS_HNOTIFY
  24134. + default AUFS_HFSNOTIFY
  24135. +config AUFS_HFSNOTIFY
  24136. + bool "fsnotify"
  24137. + select FSNOTIFY
  24138. +endchoice
  24139. +
  24140. +config AUFS_EXPORT
  24141. + bool "NFS-exportable aufs"
  24142. + depends on EXPORTFS
  24143. + help
  24144. + If you want to export your mounted aufs via NFS, then enable this
  24145. + option. There are several requirements for this configuration.
  24146. + See detail in aufs.5.
  24147. +
  24148. +config AUFS_INO_T_64
  24149. + bool
  24150. + depends on AUFS_EXPORT
  24151. + depends on 64BIT && !(ALPHA || S390)
  24152. + default y
  24153. + help
  24154. + Automatic configuration for internal use.
  24155. + /* typedef unsigned long/int __kernel_ino_t */
  24156. + /* alpha and s390x are int */
  24157. +
  24158. +config AUFS_XATTR
  24159. + bool "support for XATTR/EA (including Security Labels)"
  24160. + help
  24161. + If your branch fs supports XATTR/EA and you want to make them
  24162. + available in aufs too, then enable this opsion and specify the
  24163. + branch attributes for EA.
  24164. + See detail in aufs.5.
  24165. +
  24166. +config AUFS_FHSM
  24167. + bool "File-based Hierarchical Storage Management"
  24168. + help
  24169. + Hierarchical Storage Management (or HSM) is a well-known feature
  24170. + in the storage world. Aufs provides this feature as file-based.
  24171. + with multiple branches.
  24172. + These multiple branches are prioritized, ie. the topmost one
  24173. + should be the fastest drive and be used heavily.
  24174. +
  24175. +config AUFS_RDU
  24176. + bool "Readdir in userspace"
  24177. + help
  24178. + Aufs has two methods to provide a merged view for a directory,
  24179. + by a user-space library and by kernel-space natively. The latter
  24180. + is always enabled but sometimes large and slow.
  24181. + If you enable this option, install the library in aufs2-util
  24182. + package, and set some environment variables for your readdir(3),
  24183. + then the work will be handled in user-space which generally
  24184. + shows better performance in most cases.
  24185. + See detail in aufs.5.
  24186. +
  24187. +config AUFS_DIRREN
  24188. + bool "Workaround for rename(2)-ing a directory"
  24189. + help
  24190. + By default, aufs returns EXDEV error in renameing a dir who has
  24191. + his child on the lower branch, since it is a bad idea to issue
  24192. + rename(2) internally for every lower branch. But user may not
  24193. + accept this behaviour. So here is a workaround to allow such
  24194. + rename(2) and store some extra information on the writable
  24195. + branch. Obviously this costs high (and I don't like it).
  24196. + To use this feature, you need to enable this configuration AND
  24197. + to specify the mount option `dirren.'
  24198. + See details in aufs.5 and the design documents.
  24199. +
  24200. +config AUFS_SHWH
  24201. + bool "Show whiteouts"
  24202. + help
  24203. + If you want to make the whiteouts in aufs visible, then enable
  24204. + this option and specify 'shwh' mount option. Although it may
  24205. + sounds like philosophy or something, but in technically it
  24206. + simply shows the name of whiteout with keeping its behaviour.
  24207. +
  24208. +config AUFS_BR_RAMFS
  24209. + bool "Ramfs (initramfs/rootfs) as an aufs branch"
  24210. + help
  24211. + If you want to use ramfs as an aufs branch fs, then enable this
  24212. + option. Generally tmpfs is recommended.
  24213. + Aufs prohibited them to be a branch fs by default, because
  24214. + initramfs becomes unusable after switch_root or something
  24215. + generally. If you sets initramfs as an aufs branch and boot your
  24216. + system by switch_root, you will meet a problem easily since the
  24217. + files in initramfs may be inaccessible.
  24218. + Unless you are going to use ramfs as an aufs branch fs without
  24219. + switch_root or something, leave it N.
  24220. +
  24221. +config AUFS_BR_FUSE
  24222. + bool "Fuse fs as an aufs branch"
  24223. + depends on FUSE_FS
  24224. + select AUFS_POLL
  24225. + help
  24226. + If you want to use fuse-based userspace filesystem as an aufs
  24227. + branch fs, then enable this option.
  24228. + It implements the internal poll(2) operation which is
  24229. + implemented by fuse only (curretnly).
  24230. +
  24231. +config AUFS_POLL
  24232. + bool
  24233. + help
  24234. + Automatic configuration for internal use.
  24235. +
  24236. +config AUFS_BR_HFSPLUS
  24237. + bool "Hfsplus as an aufs branch"
  24238. + depends on HFSPLUS_FS
  24239. + default y
  24240. + help
  24241. + If you want to use hfsplus fs as an aufs branch fs, then enable
  24242. + this option. This option introduces a small overhead at
  24243. + copying-up a file on hfsplus.
  24244. +
  24245. +config AUFS_BDEV_LOOP
  24246. + bool
  24247. + depends on BLK_DEV_LOOP
  24248. + default y
  24249. + help
  24250. + Automatic configuration for internal use.
  24251. + Convert =[ym] into =y.
  24252. +
  24253. +config AUFS_DEBUG
  24254. + bool "Debug aufs"
  24255. + help
  24256. + Enable this to compile aufs internal debug code.
  24257. + It will have a negative impact to the performance.
  24258. +
  24259. +config AUFS_MAGIC_SYSRQ
  24260. + bool
  24261. + depends on AUFS_DEBUG && MAGIC_SYSRQ
  24262. + default y
  24263. + help
  24264. + Automatic configuration for internal use.
  24265. + When aufs supports Magic SysRq, enabled automatically.
  24266. +endif
  24267. diff -Naur null/fs/aufs/lcnt.h linux-5.15.131/fs/aufs/lcnt.h
  24268. --- /dev/null
  24269. +++ linux-5.15.131/fs/aufs/lcnt.h 2023-09-11 14:32:48.150722527 +0300
  24270. @@ -0,0 +1,186 @@
  24271. +/* SPDX-License-Identifier: GPL-2.0 */
  24272. +/*
  24273. + * Copyright (C) 2018-2021 Junjiro R. Okajima
  24274. + *
  24275. + * This program, aufs is free software; you can redistribute it and/or modify
  24276. + * it under the terms of the GNU General Public License as published by
  24277. + * the Free Software Foundation; either version 2 of the License, or
  24278. + * (at your option) any later version.
  24279. + *
  24280. + * This program is distributed in the hope that it will be useful,
  24281. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24282. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24283. + * GNU General Public License for more details.
  24284. + *
  24285. + * You should have received a copy of the GNU General Public License
  24286. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24287. + */
  24288. +
  24289. +/*
  24290. + * simple long counter wrapper
  24291. + */
  24292. +
  24293. +#ifndef __AUFS_LCNT_H__
  24294. +#define __AUFS_LCNT_H__
  24295. +
  24296. +#ifdef __KERNEL__
  24297. +
  24298. +#include "debug.h"
  24299. +
  24300. +#define AuLCntATOMIC 1
  24301. +#define AuLCntPCPUCNT 2
  24302. +/*
  24303. + * why does percpu_refcount require extra synchronize_rcu()s in
  24304. + * au_br_do_free()
  24305. + */
  24306. +#define AuLCntPCPUREF 3
  24307. +
  24308. +/* #define AuLCntChosen AuLCntATOMIC */
  24309. +#define AuLCntChosen AuLCntPCPUCNT
  24310. +/* #define AuLCntChosen AuLCntPCPUREF */
  24311. +
  24312. +#if AuLCntChosen == AuLCntATOMIC
  24313. +#include <linux/atomic.h>
  24314. +
  24315. +typedef atomic_long_t au_lcnt_t;
  24316. +
  24317. +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
  24318. +{
  24319. + atomic_long_set(cnt, 0);
  24320. + return 0;
  24321. +}
  24322. +
  24323. +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
  24324. +{
  24325. + /* empty */
  24326. +}
  24327. +
  24328. +static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
  24329. + int do_sync __maybe_unused)
  24330. +{
  24331. + /* empty */
  24332. +}
  24333. +
  24334. +static inline void au_lcnt_inc(au_lcnt_t *cnt)
  24335. +{
  24336. + atomic_long_inc(cnt);
  24337. +}
  24338. +
  24339. +static inline void au_lcnt_dec(au_lcnt_t *cnt)
  24340. +{
  24341. + atomic_long_dec(cnt);
  24342. +}
  24343. +
  24344. +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
  24345. +{
  24346. + return atomic_long_read(cnt);
  24347. +}
  24348. +#endif
  24349. +
  24350. +#if AuLCntChosen == AuLCntPCPUCNT
  24351. +#include <linux/percpu_counter.h>
  24352. +
  24353. +typedef struct percpu_counter au_lcnt_t;
  24354. +
  24355. +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
  24356. +{
  24357. + return percpu_counter_init(cnt, 0, GFP_NOFS);
  24358. +}
  24359. +
  24360. +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
  24361. +{
  24362. + /* empty */
  24363. +}
  24364. +
  24365. +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
  24366. +{
  24367. + percpu_counter_destroy(cnt);
  24368. +}
  24369. +
  24370. +static inline void au_lcnt_inc(au_lcnt_t *cnt)
  24371. +{
  24372. + percpu_counter_inc(cnt);
  24373. +}
  24374. +
  24375. +static inline void au_lcnt_dec(au_lcnt_t *cnt)
  24376. +{
  24377. + percpu_counter_dec(cnt);
  24378. +}
  24379. +
  24380. +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
  24381. +{
  24382. + s64 n;
  24383. +
  24384. + n = percpu_counter_sum(cnt);
  24385. + BUG_ON(n < 0);
  24386. + if (LONG_MAX != LLONG_MAX
  24387. + && n > LONG_MAX)
  24388. + AuWarn1("%s\n", "wrap-around");
  24389. +
  24390. + return n;
  24391. +}
  24392. +#endif
  24393. +
  24394. +#if AuLCntChosen == AuLCntPCPUREF
  24395. +#include <linux/percpu-refcount.h>
  24396. +
  24397. +typedef struct percpu_ref au_lcnt_t;
  24398. +
  24399. +static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
  24400. +{
  24401. + if (!release)
  24402. + release = percpu_ref_exit;
  24403. + return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
  24404. +}
  24405. +
  24406. +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
  24407. +{
  24408. + synchronize_rcu();
  24409. +}
  24410. +
  24411. +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
  24412. +{
  24413. + percpu_ref_kill(cnt);
  24414. + if (do_sync)
  24415. + au_lcnt_wait_for_fin(cnt);
  24416. +}
  24417. +
  24418. +static inline void au_lcnt_inc(au_lcnt_t *cnt)
  24419. +{
  24420. + percpu_ref_get(cnt);
  24421. +}
  24422. +
  24423. +static inline void au_lcnt_dec(au_lcnt_t *cnt)
  24424. +{
  24425. + percpu_ref_put(cnt);
  24426. +}
  24427. +
  24428. +/*
  24429. + * avoid calling this func as possible.
  24430. + */
  24431. +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
  24432. +{
  24433. + long l;
  24434. +
  24435. + percpu_ref_switch_to_atomic_sync(cnt);
  24436. + l = atomic_long_read(&cnt->count);
  24437. + if (do_rev)
  24438. + percpu_ref_switch_to_percpu(cnt);
  24439. +
  24440. + /* percpu_ref is initialized by 1 instead of 0 */
  24441. + return l - 1;
  24442. +}
  24443. +#endif
  24444. +
  24445. +#ifdef CONFIG_AUFS_DEBUG
  24446. +#define AuLCntZero(val) do { \
  24447. + long l = val; \
  24448. + if (l) \
  24449. + AuDbg("%s = %ld\n", #val, l); \
  24450. +} while (0)
  24451. +#else
  24452. +#define AuLCntZero(val) do {} while (0)
  24453. +#endif
  24454. +
  24455. +#endif /* __KERNEL__ */
  24456. +#endif /* __AUFS_LCNT_H__ */
  24457. diff -Naur null/fs/aufs/loop.c linux-5.15.131/fs/aufs/loop.c
  24458. --- /dev/null
  24459. +++ linux-5.15.131/fs/aufs/loop.c 2023-09-11 14:32:48.150722527 +0300
  24460. @@ -0,0 +1,148 @@
  24461. +// SPDX-License-Identifier: GPL-2.0
  24462. +/*
  24463. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  24464. + *
  24465. + * This program, aufs is free software; you can redistribute it and/or modify
  24466. + * it under the terms of the GNU General Public License as published by
  24467. + * the Free Software Foundation; either version 2 of the License, or
  24468. + * (at your option) any later version.
  24469. + *
  24470. + * This program is distributed in the hope that it will be useful,
  24471. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24472. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24473. + * GNU General Public License for more details.
  24474. + *
  24475. + * You should have received a copy of the GNU General Public License
  24476. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24477. + */
  24478. +
  24479. +/*
  24480. + * support for loopback block device as a branch
  24481. + */
  24482. +
  24483. +#include "aufs.h"
  24484. +
  24485. +/* added into drivers/block/loop.c */
  24486. +static struct file *(*backing_file_func)(struct super_block *sb);
  24487. +
  24488. +/*
  24489. + * test if two lower dentries have overlapping branches.
  24490. + */
  24491. +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
  24492. +{
  24493. + struct super_block *h_sb;
  24494. + struct file *backing_file;
  24495. +
  24496. + if (unlikely(!backing_file_func)) {
  24497. + /* don't load "loop" module here */
  24498. + backing_file_func = symbol_get(loop_backing_file);
  24499. + if (unlikely(!backing_file_func))
  24500. + /* "loop" module is not loaded */
  24501. + return 0;
  24502. + }
  24503. +
  24504. + h_sb = h_adding->d_sb;
  24505. + backing_file = backing_file_func(h_sb);
  24506. + if (!backing_file)
  24507. + return 0;
  24508. +
  24509. + h_adding = backing_file->f_path.dentry;
  24510. + /*
  24511. + * h_adding can be local NFS.
  24512. + * in this case aufs cannot detect the loop.
  24513. + */
  24514. + if (unlikely(h_adding->d_sb == sb))
  24515. + return 1;
  24516. + return !!au_test_subdir(h_adding, sb->s_root);
  24517. +}
  24518. +
  24519. +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
  24520. +int au_test_loopback_kthread(void)
  24521. +{
  24522. + int ret;
  24523. + struct task_struct *tsk = current;
  24524. + char c, comm[sizeof(tsk->comm)];
  24525. +
  24526. + ret = 0;
  24527. + if (tsk->flags & PF_KTHREAD) {
  24528. + get_task_comm(comm, tsk);
  24529. + c = comm[4];
  24530. + ret = ('0' <= c && c <= '9'
  24531. + && !strncmp(comm, "loop", 4));
  24532. + }
  24533. +
  24534. + return ret;
  24535. +}
  24536. +
  24537. +/* ---------------------------------------------------------------------- */
  24538. +
  24539. +#define au_warn_loopback_step 16
  24540. +static int au_warn_loopback_nelem = au_warn_loopback_step;
  24541. +static unsigned long *au_warn_loopback_array;
  24542. +
  24543. +void au_warn_loopback(struct super_block *h_sb)
  24544. +{
  24545. + int i, new_nelem;
  24546. + unsigned long *a, magic;
  24547. + static DEFINE_SPINLOCK(spin);
  24548. +
  24549. + magic = h_sb->s_magic;
  24550. + spin_lock(&spin);
  24551. + a = au_warn_loopback_array;
  24552. + for (i = 0; i < au_warn_loopback_nelem && *a; i++)
  24553. + if (a[i] == magic) {
  24554. + spin_unlock(&spin);
  24555. + return;
  24556. + }
  24557. +
  24558. + /* h_sb is new to us, print it */
  24559. + if (i < au_warn_loopback_nelem) {
  24560. + a[i] = magic;
  24561. + goto pr;
  24562. + }
  24563. +
  24564. + /* expand the array */
  24565. + new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
  24566. + a = au_kzrealloc(au_warn_loopback_array,
  24567. + au_warn_loopback_nelem * sizeof(unsigned long),
  24568. + new_nelem * sizeof(unsigned long), GFP_ATOMIC,
  24569. + /*may_shrink*/0);
  24570. + if (a) {
  24571. + au_warn_loopback_nelem = new_nelem;
  24572. + au_warn_loopback_array = a;
  24573. + a[i] = magic;
  24574. + goto pr;
  24575. + }
  24576. +
  24577. + spin_unlock(&spin);
  24578. + AuWarn1("realloc failed, ignored\n");
  24579. + return;
  24580. +
  24581. +pr:
  24582. + spin_unlock(&spin);
  24583. + pr_warn("you may want to try another patch for loopback file "
  24584. + "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
  24585. +}
  24586. +
  24587. +int au_loopback_init(void)
  24588. +{
  24589. + int err;
  24590. + struct super_block *sb __maybe_unused;
  24591. +
  24592. + BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
  24593. +
  24594. + err = 0;
  24595. + au_warn_loopback_array = kcalloc(au_warn_loopback_step,
  24596. + sizeof(unsigned long), GFP_NOFS);
  24597. + if (unlikely(!au_warn_loopback_array))
  24598. + err = -ENOMEM;
  24599. +
  24600. + return err;
  24601. +}
  24602. +
  24603. +void au_loopback_fin(void)
  24604. +{
  24605. + if (backing_file_func)
  24606. + symbol_put(loop_backing_file);
  24607. + au_kfree_try_rcu(au_warn_loopback_array);
  24608. +}
  24609. diff -Naur null/fs/aufs/loop.h linux-5.15.131/fs/aufs/loop.h
  24610. --- /dev/null
  24611. +++ linux-5.15.131/fs/aufs/loop.h 2023-09-11 14:32:48.150722527 +0300
  24612. @@ -0,0 +1,55 @@
  24613. +/* SPDX-License-Identifier: GPL-2.0 */
  24614. +/*
  24615. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  24616. + *
  24617. + * This program, aufs is free software; you can redistribute it and/or modify
  24618. + * it under the terms of the GNU General Public License as published by
  24619. + * the Free Software Foundation; either version 2 of the License, or
  24620. + * (at your option) any later version.
  24621. + *
  24622. + * This program is distributed in the hope that it will be useful,
  24623. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24624. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24625. + * GNU General Public License for more details.
  24626. + *
  24627. + * You should have received a copy of the GNU General Public License
  24628. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24629. + */
  24630. +
  24631. +/*
  24632. + * support for loopback mount as a branch
  24633. + */
  24634. +
  24635. +#ifndef __AUFS_LOOP_H__
  24636. +#define __AUFS_LOOP_H__
  24637. +
  24638. +#ifdef __KERNEL__
  24639. +
  24640. +struct dentry;
  24641. +struct super_block;
  24642. +
  24643. +#ifdef CONFIG_AUFS_BDEV_LOOP
  24644. +/* drivers/block/loop.c */
  24645. +struct file *loop_backing_file(struct super_block *sb);
  24646. +
  24647. +/* loop.c */
  24648. +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
  24649. +int au_test_loopback_kthread(void);
  24650. +void au_warn_loopback(struct super_block *h_sb);
  24651. +
  24652. +int au_loopback_init(void);
  24653. +void au_loopback_fin(void);
  24654. +#else
  24655. +AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
  24656. +
  24657. +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
  24658. + struct dentry *h_adding)
  24659. +AuStubInt0(au_test_loopback_kthread, void)
  24660. +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
  24661. +
  24662. +AuStubInt0(au_loopback_init, void)
  24663. +AuStubVoid(au_loopback_fin, void)
  24664. +#endif /* BLK_DEV_LOOP */
  24665. +
  24666. +#endif /* __KERNEL__ */
  24667. +#endif /* __AUFS_LOOP_H__ */
  24668. diff -Naur null/fs/aufs/magic.mk linux-5.15.131/fs/aufs/magic.mk
  24669. --- /dev/null
  24670. +++ linux-5.15.131/fs/aufs/magic.mk 2023-09-11 14:32:48.150722527 +0300
  24671. @@ -0,0 +1,31 @@
  24672. +# SPDX-License-Identifier: GPL-2.0
  24673. +
  24674. +# defined in ${srctree}/fs/fuse/inode.c
  24675. +# tristate
  24676. +ifdef CONFIG_FUSE_FS
  24677. +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
  24678. +endif
  24679. +
  24680. +# defined in ${srctree}/fs/xfs/xfs_sb.h
  24681. +# tristate
  24682. +ifdef CONFIG_XFS_FS
  24683. +ccflags-y += -DXFS_SB_MAGIC=0x58465342
  24684. +endif
  24685. +
  24686. +# defined in ${srctree}/fs/configfs/mount.c
  24687. +# tristate
  24688. +ifdef CONFIG_CONFIGFS_FS
  24689. +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
  24690. +endif
  24691. +
  24692. +# defined in ${srctree}/fs/ubifs/ubifs.h
  24693. +# tristate
  24694. +ifdef CONFIG_UBIFS_FS
  24695. +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
  24696. +endif
  24697. +
  24698. +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
  24699. +# tristate
  24700. +ifdef CONFIG_HFSPLUS_FS
  24701. +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
  24702. +endif
  24703. diff -Naur null/fs/aufs/Makefile linux-5.15.131/fs/aufs/Makefile
  24704. --- /dev/null
  24705. +++ linux-5.15.131/fs/aufs/Makefile 2023-09-11 14:32:48.145722589 +0300
  24706. @@ -0,0 +1,46 @@
  24707. +# SPDX-License-Identifier: GPL-2.0
  24708. +
  24709. +include ${src}/magic.mk
  24710. +ifeq (${CONFIG_AUFS_FS},m)
  24711. +include ${src}/conf.mk
  24712. +endif
  24713. +-include ${src}/priv_def.mk
  24714. +
  24715. +# cf. include/linux/kernel.h
  24716. +# enable pr_debug
  24717. +ccflags-y += -DDEBUG
  24718. +# sparse requires the full pathname
  24719. +ifdef M
  24720. +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
  24721. +else
  24722. +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
  24723. +endif
  24724. +
  24725. +obj-$(CONFIG_AUFS_FS) += aufs.o
  24726. +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o fsctx.o \
  24727. + wkq.o vfsub.o dcsub.o \
  24728. + cpup.o whout.o wbr_policy.o \
  24729. + dinfo.o dentry.o \
  24730. + dynop.o \
  24731. + finfo.o file.o f_op.o \
  24732. + dir.o vdir.o \
  24733. + iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
  24734. + mvdown.o ioctl.o
  24735. +
  24736. +# all are boolean
  24737. +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
  24738. +aufs-$(CONFIG_SYSFS) += sysfs.o
  24739. +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
  24740. +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
  24741. +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
  24742. +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
  24743. +aufs-$(CONFIG_AUFS_EXPORT) += export.o
  24744. +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
  24745. +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
  24746. +aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
  24747. +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
  24748. +aufs-$(CONFIG_AUFS_POLL) += poll.o
  24749. +aufs-$(CONFIG_AUFS_RDU) += rdu.o
  24750. +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
  24751. +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
  24752. +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
  24753. diff -Naur null/fs/aufs/module.c linux-5.15.131/fs/aufs/module.c
  24754. --- /dev/null
  24755. +++ linux-5.15.131/fs/aufs/module.c 2023-09-11 14:32:48.150722527 +0300
  24756. @@ -0,0 +1,273 @@
  24757. +// SPDX-License-Identifier: GPL-2.0
  24758. +/*
  24759. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  24760. + *
  24761. + * This program, aufs is free software; you can redistribute it and/or modify
  24762. + * it under the terms of the GNU General Public License as published by
  24763. + * the Free Software Foundation; either version 2 of the License, or
  24764. + * (at your option) any later version.
  24765. + *
  24766. + * This program is distributed in the hope that it will be useful,
  24767. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24768. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24769. + * GNU General Public License for more details.
  24770. + *
  24771. + * You should have received a copy of the GNU General Public License
  24772. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24773. + */
  24774. +
  24775. +/*
  24776. + * module global variables and operations
  24777. + */
  24778. +
  24779. +#include <linux/module.h>
  24780. +#include <linux/seq_file.h>
  24781. +#include "aufs.h"
  24782. +
  24783. +/* shrinkable realloc */
  24784. +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
  24785. +{
  24786. + size_t sz;
  24787. + int diff;
  24788. +
  24789. + sz = 0;
  24790. + diff = -1;
  24791. + if (p) {
  24792. +#if 0 /* unused */
  24793. + if (!new_sz) {
  24794. + au_kfree_rcu(p);
  24795. + p = NULL;
  24796. + goto out;
  24797. + }
  24798. +#else
  24799. + AuDebugOn(!new_sz);
  24800. +#endif
  24801. + sz = ksize(p);
  24802. + diff = au_kmidx_sub(sz, new_sz);
  24803. + }
  24804. + if (sz && !diff)
  24805. + goto out;
  24806. +
  24807. + if (sz < new_sz)
  24808. + /* expand or SLOB */
  24809. + p = krealloc(p, new_sz, gfp);
  24810. + else if (new_sz < sz && may_shrink) {
  24811. + /* shrink */
  24812. + void *q;
  24813. +
  24814. + q = kmalloc(new_sz, gfp);
  24815. + if (q) {
  24816. + if (p) {
  24817. + memcpy(q, p, new_sz);
  24818. + au_kfree_try_rcu(p);
  24819. + }
  24820. + p = q;
  24821. + } else
  24822. + p = NULL;
  24823. + }
  24824. +
  24825. +out:
  24826. + return p;
  24827. +}
  24828. +
  24829. +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
  24830. + int may_shrink)
  24831. +{
  24832. + p = au_krealloc(p, new_sz, gfp, may_shrink);
  24833. + if (p && new_sz > nused)
  24834. + memset(p + nused, 0, new_sz - nused);
  24835. + return p;
  24836. +}
  24837. +
  24838. +/* ---------------------------------------------------------------------- */
  24839. +/*
  24840. + * aufs caches
  24841. + */
  24842. +struct kmem_cache *au_cache[AuCache_Last];
  24843. +
  24844. +static void au_cache_fin(void)
  24845. +{
  24846. + int i;
  24847. +
  24848. + /*
  24849. + * Make sure all delayed rcu free inodes are flushed before we
  24850. + * destroy cache.
  24851. + */
  24852. + rcu_barrier();
  24853. +
  24854. + /* excluding AuCache_HNOTIFY */
  24855. + BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
  24856. + for (i = 0; i < AuCache_HNOTIFY; i++) {
  24857. + kmem_cache_destroy(au_cache[i]);
  24858. + au_cache[i] = NULL;
  24859. + }
  24860. +}
  24861. +
  24862. +static int __init au_cache_init(void)
  24863. +{
  24864. + au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
  24865. + if (au_cache[AuCache_DINFO])
  24866. + /* SLAB_DESTROY_BY_RCU */
  24867. + au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
  24868. + au_icntnr_init_once);
  24869. + if (au_cache[AuCache_ICNTNR])
  24870. + au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
  24871. + au_fi_init_once);
  24872. + if (au_cache[AuCache_FINFO])
  24873. + au_cache[AuCache_VDIR] = AuCache(au_vdir);
  24874. + if (au_cache[AuCache_VDIR])
  24875. + au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
  24876. + if (au_cache[AuCache_DEHSTR])
  24877. + return 0;
  24878. +
  24879. + au_cache_fin();
  24880. + return -ENOMEM;
  24881. +}
  24882. +
  24883. +/* ---------------------------------------------------------------------- */
  24884. +
  24885. +int au_dir_roflags;
  24886. +
  24887. +#ifdef CONFIG_AUFS_SBILIST
  24888. +/*
  24889. + * iterate_supers_type() doesn't protect us from
  24890. + * remounting (branch management)
  24891. + */
  24892. +struct hlist_bl_head au_sbilist;
  24893. +#endif
  24894. +
  24895. +/*
  24896. + * functions for module interface.
  24897. + */
  24898. +MODULE_LICENSE("GPL");
  24899. +/* MODULE_LICENSE("GPL v2"); */
  24900. +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
  24901. +MODULE_DESCRIPTION(AUFS_NAME
  24902. + " -- Advanced multi layered unification filesystem");
  24903. +MODULE_VERSION(AUFS_VERSION);
  24904. +MODULE_ALIAS_FS(AUFS_NAME);
  24905. +
  24906. +/* this module parameter has no meaning when SYSFS is disabled */
  24907. +int sysaufs_brs = 1;
  24908. +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
  24909. +module_param_named(brs, sysaufs_brs, int, 0444);
  24910. +
  24911. +/* this module parameter has no meaning when USER_NS is disabled */
  24912. +bool au_userns;
  24913. +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
  24914. +module_param_named(allow_userns, au_userns, bool, 0444);
  24915. +
  24916. +/* ---------------------------------------------------------------------- */
  24917. +
  24918. +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
  24919. +
  24920. +int au_seq_path(struct seq_file *seq, struct path *path)
  24921. +{
  24922. + int err;
  24923. +
  24924. + err = seq_path(seq, path, au_esc_chars);
  24925. + if (err >= 0)
  24926. + err = 0;
  24927. + else
  24928. + err = -ENOMEM;
  24929. +
  24930. + return err;
  24931. +}
  24932. +
  24933. +/* ---------------------------------------------------------------------- */
  24934. +
  24935. +static int __init aufs_init(void)
  24936. +{
  24937. + int err, i;
  24938. + char *p;
  24939. +
  24940. + p = au_esc_chars;
  24941. + for (i = 1; i <= ' '; i++)
  24942. + *p++ = i;
  24943. + *p++ = '\\';
  24944. + *p++ = '\x7f';
  24945. + *p = 0;
  24946. +
  24947. + au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
  24948. +
  24949. + memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
  24950. + for (i = 0; i < AuIop_Last; i++)
  24951. + aufs_iop_nogetattr[i].getattr = NULL;
  24952. +
  24953. + memset(au_cache, 0, sizeof(au_cache)); /* including hnotify */
  24954. +
  24955. + au_sbilist_init();
  24956. + sysaufs_brs_init();
  24957. + au_debug_init();
  24958. + au_dy_init();
  24959. + err = sysaufs_init();
  24960. + if (unlikely(err))
  24961. + goto out;
  24962. + err = dbgaufs_init();
  24963. + if (unlikely(err))
  24964. + goto out_sysaufs;
  24965. + err = au_procfs_init();
  24966. + if (unlikely(err))
  24967. + goto out_dbgaufs;
  24968. + err = au_wkq_init();
  24969. + if (unlikely(err))
  24970. + goto out_procfs;
  24971. + err = au_loopback_init();
  24972. + if (unlikely(err))
  24973. + goto out_wkq;
  24974. + err = au_hnotify_init();
  24975. + if (unlikely(err))
  24976. + goto out_loopback;
  24977. + err = au_sysrq_init();
  24978. + if (unlikely(err))
  24979. + goto out_hin;
  24980. + err = au_cache_init();
  24981. + if (unlikely(err))
  24982. + goto out_sysrq;
  24983. +
  24984. + aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
  24985. + err = register_filesystem(&aufs_fs_type);
  24986. + if (unlikely(err))
  24987. + goto out_cache;
  24988. +
  24989. + /* since we define pr_fmt, call printk directly */
  24990. + printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
  24991. + goto out; /* success */
  24992. +
  24993. +out_cache:
  24994. + au_cache_fin();
  24995. +out_sysrq:
  24996. + au_sysrq_fin();
  24997. +out_hin:
  24998. + au_hnotify_fin();
  24999. +out_loopback:
  25000. + au_loopback_fin();
  25001. +out_wkq:
  25002. + au_wkq_fin();
  25003. +out_procfs:
  25004. + au_procfs_fin();
  25005. +out_dbgaufs:
  25006. + dbgaufs_fin();
  25007. +out_sysaufs:
  25008. + sysaufs_fin();
  25009. + au_dy_fin();
  25010. +out:
  25011. + return err;
  25012. +}
  25013. +
  25014. +static void __exit aufs_exit(void)
  25015. +{
  25016. + unregister_filesystem(&aufs_fs_type);
  25017. + au_cache_fin();
  25018. + au_sysrq_fin();
  25019. + au_hnotify_fin();
  25020. + au_loopback_fin();
  25021. + au_wkq_fin();
  25022. + au_procfs_fin();
  25023. + dbgaufs_fin();
  25024. + sysaufs_fin();
  25025. + au_dy_fin();
  25026. +}
  25027. +
  25028. +module_init(aufs_init);
  25029. +module_exit(aufs_exit);
  25030. diff -Naur null/fs/aufs/module.h linux-5.15.131/fs/aufs/module.h
  25031. --- /dev/null
  25032. +++ linux-5.15.131/fs/aufs/module.h 2023-09-11 14:32:48.150722527 +0300
  25033. @@ -0,0 +1,166 @@
  25034. +/* SPDX-License-Identifier: GPL-2.0 */
  25035. +/*
  25036. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  25037. + *
  25038. + * This program, aufs is free software; you can redistribute it and/or modify
  25039. + * it under the terms of the GNU General Public License as published by
  25040. + * the Free Software Foundation; either version 2 of the License, or
  25041. + * (at your option) any later version.
  25042. + *
  25043. + * This program is distributed in the hope that it will be useful,
  25044. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25045. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25046. + * GNU General Public License for more details.
  25047. + *
  25048. + * You should have received a copy of the GNU General Public License
  25049. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25050. + */
  25051. +
  25052. +/*
  25053. + * module initialization and module-global
  25054. + */
  25055. +
  25056. +#ifndef __AUFS_MODULE_H__
  25057. +#define __AUFS_MODULE_H__
  25058. +
  25059. +#ifdef __KERNEL__
  25060. +
  25061. +#include <linux/slab.h>
  25062. +#include "debug.h"
  25063. +#include "dentry.h"
  25064. +#include "dir.h"
  25065. +#include "file.h"
  25066. +#include "inode.h"
  25067. +
  25068. +struct path;
  25069. +struct seq_file;
  25070. +
  25071. +/* module parameters */
  25072. +extern int sysaufs_brs;
  25073. +extern bool au_userns;
  25074. +
  25075. +/* ---------------------------------------------------------------------- */
  25076. +
  25077. +extern int au_dir_roflags;
  25078. +
  25079. +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
  25080. +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
  25081. + int may_shrink);
  25082. +
  25083. +/*
  25084. + * Comparing the size of the object with sizeof(struct rcu_head)
  25085. + * case 1: object is always larger
  25086. + * --> au_kfree_rcu() or au_kfree_do_rcu()
  25087. + * case 2: object is always smaller
  25088. + * --> au_kfree_small()
  25089. + * case 3: object can be any size
  25090. + * --> au_kfree_try_rcu()
  25091. + */
  25092. +
  25093. +static inline void au_kfree_do_rcu(const void *p)
  25094. +{
  25095. + struct {
  25096. + struct rcu_head rcu;
  25097. + } *a = (void *)p;
  25098. +
  25099. + kfree_rcu(a, rcu);
  25100. +}
  25101. +
  25102. +#define au_kfree_rcu(_p) do { \
  25103. + typeof(_p) p = (_p); \
  25104. + BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head)); \
  25105. + if (p) \
  25106. + au_kfree_do_rcu(p); \
  25107. + } while (0)
  25108. +
  25109. +#define au_kfree_do_sz_test(sz) (sz >= sizeof(struct rcu_head))
  25110. +#define au_kfree_sz_test(p) (p && au_kfree_do_sz_test(ksize(p)))
  25111. +
  25112. +static inline void au_kfree_try_rcu(const void *p)
  25113. +{
  25114. + if (!p)
  25115. + return;
  25116. + if (au_kfree_sz_test(p))
  25117. + au_kfree_do_rcu(p);
  25118. + else
  25119. + kfree(p);
  25120. +}
  25121. +
  25122. +static inline void au_kfree_small(const void *p)
  25123. +{
  25124. + if (!p)
  25125. + return;
  25126. + AuDebugOn(au_kfree_sz_test(p));
  25127. + kfree(p);
  25128. +}
  25129. +
  25130. +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
  25131. +{
  25132. +#ifndef CONFIG_SLOB
  25133. + return __kmalloc_index(sz, false) - __kmalloc_index(new_sz, false);
  25134. +#else
  25135. + return -1; /* SLOB is untested */
  25136. +#endif
  25137. +}
  25138. +
  25139. +int au_seq_path(struct seq_file *seq, struct path *path);
  25140. +
  25141. +#ifdef CONFIG_PROC_FS
  25142. +/* procfs.c */
  25143. +int __init au_procfs_init(void);
  25144. +void au_procfs_fin(void);
  25145. +#else
  25146. +AuStubInt0(au_procfs_init, void);
  25147. +AuStubVoid(au_procfs_fin, void);
  25148. +#endif
  25149. +
  25150. +/* ---------------------------------------------------------------------- */
  25151. +
  25152. +/* kmem cache */
  25153. +enum {
  25154. + AuCache_DINFO,
  25155. + AuCache_ICNTNR,
  25156. + AuCache_FINFO,
  25157. + AuCache_VDIR,
  25158. + AuCache_DEHSTR,
  25159. + AuCache_HNOTIFY, /* must be last */
  25160. + AuCache_Last
  25161. +};
  25162. +
  25163. +extern struct kmem_cache *au_cache[AuCache_Last];
  25164. +
  25165. +#define AuCacheFlags (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
  25166. +#define AuCache(type) KMEM_CACHE(type, AuCacheFlags)
  25167. +#define AuCacheCtor(type, ctor) \
  25168. + kmem_cache_create(#type, sizeof(struct type), \
  25169. + __alignof__(struct type), AuCacheFlags, ctor)
  25170. +
  25171. +#define AuCacheFuncs(name, index) \
  25172. + static inline struct au_##name *au_cache_alloc_##name(void) \
  25173. + { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
  25174. + static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
  25175. + { kmem_cache_free(au_cache[AuCache_##index], p); } \
  25176. + \
  25177. + static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
  25178. + { void *p = rcu; \
  25179. + p -= offsetof(struct au_##name, rcu); \
  25180. + kmem_cache_free(au_cache[AuCache_##index], p); } \
  25181. + static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
  25182. + { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
  25183. + call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); } \
  25184. + \
  25185. + static inline void au_cache_free_##name(struct au_##name *p) \
  25186. + { /* au_cache_free_##name##_norcu(p); */ \
  25187. + au_cache_free_##name##_rcu(p); }
  25188. +
  25189. +AuCacheFuncs(dinfo, DINFO);
  25190. +AuCacheFuncs(icntnr, ICNTNR);
  25191. +AuCacheFuncs(finfo, FINFO);
  25192. +AuCacheFuncs(vdir, VDIR);
  25193. +AuCacheFuncs(vdir_dehstr, DEHSTR);
  25194. +#ifdef CONFIG_AUFS_HNOTIFY
  25195. +AuCacheFuncs(hnotify, HNOTIFY);
  25196. +#endif
  25197. +
  25198. +#endif /* __KERNEL__ */
  25199. +#endif /* __AUFS_MODULE_H__ */
  25200. diff -Naur null/fs/aufs/mvdown.c linux-5.15.131/fs/aufs/mvdown.c
  25201. --- /dev/null
  25202. +++ linux-5.15.131/fs/aufs/mvdown.c 2023-09-11 14:32:48.150722527 +0300
  25203. @@ -0,0 +1,706 @@
  25204. +// SPDX-License-Identifier: GPL-2.0
  25205. +/*
  25206. + * Copyright (C) 2011-2021 Junjiro R. Okajima
  25207. + *
  25208. + * This program, aufs is free software; you can redistribute it and/or modify
  25209. + * it under the terms of the GNU General Public License as published by
  25210. + * the Free Software Foundation; either version 2 of the License, or
  25211. + * (at your option) any later version.
  25212. + *
  25213. + * This program is distributed in the hope that it will be useful,
  25214. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25215. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25216. + * GNU General Public License for more details.
  25217. + *
  25218. + * You should have received a copy of the GNU General Public License
  25219. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25220. + */
  25221. +
  25222. +/*
  25223. + * move-down, opposite of copy-up
  25224. + */
  25225. +
  25226. +#include "aufs.h"
  25227. +
  25228. +struct au_mvd_args {
  25229. + struct {
  25230. + struct super_block *h_sb;
  25231. + struct dentry *h_parent;
  25232. + struct au_hinode *hdir;
  25233. + struct inode *h_dir, *h_inode;
  25234. + struct au_pin pin;
  25235. + } info[AUFS_MVDOWN_NARRAY];
  25236. +
  25237. + struct aufs_mvdown mvdown;
  25238. + struct dentry *dentry, *parent;
  25239. + struct inode *inode, *dir;
  25240. + struct super_block *sb;
  25241. + aufs_bindex_t bopq, bwh, bfound;
  25242. + unsigned char rename_lock;
  25243. +};
  25244. +
  25245. +#define mvd_errno mvdown.au_errno
  25246. +#define mvd_bsrc mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
  25247. +#define mvd_src_brid mvdown.stbr[AUFS_MVDOWN_UPPER].brid
  25248. +#define mvd_bdst mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
  25249. +#define mvd_dst_brid mvdown.stbr[AUFS_MVDOWN_LOWER].brid
  25250. +
  25251. +#define mvd_h_src_sb info[AUFS_MVDOWN_UPPER].h_sb
  25252. +#define mvd_h_src_parent info[AUFS_MVDOWN_UPPER].h_parent
  25253. +#define mvd_hdir_src info[AUFS_MVDOWN_UPPER].hdir
  25254. +#define mvd_h_src_dir info[AUFS_MVDOWN_UPPER].h_dir
  25255. +#define mvd_h_src_inode info[AUFS_MVDOWN_UPPER].h_inode
  25256. +#define mvd_pin_src info[AUFS_MVDOWN_UPPER].pin
  25257. +
  25258. +#define mvd_h_dst_sb info[AUFS_MVDOWN_LOWER].h_sb
  25259. +#define mvd_h_dst_parent info[AUFS_MVDOWN_LOWER].h_parent
  25260. +#define mvd_hdir_dst info[AUFS_MVDOWN_LOWER].hdir
  25261. +#define mvd_h_dst_dir info[AUFS_MVDOWN_LOWER].h_dir
  25262. +#define mvd_h_dst_inode info[AUFS_MVDOWN_LOWER].h_inode
  25263. +#define mvd_pin_dst info[AUFS_MVDOWN_LOWER].pin
  25264. +
  25265. +#define AU_MVD_PR(flag, ...) do { \
  25266. + if (flag) \
  25267. + pr_err(__VA_ARGS__); \
  25268. + } while (0)
  25269. +
  25270. +static int find_lower_writable(struct au_mvd_args *a)
  25271. +{
  25272. + struct super_block *sb;
  25273. + aufs_bindex_t bindex, bbot;
  25274. + struct au_branch *br;
  25275. +
  25276. + sb = a->sb;
  25277. + bindex = a->mvd_bsrc;
  25278. + bbot = au_sbbot(sb);
  25279. + if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
  25280. + for (bindex++; bindex <= bbot; bindex++) {
  25281. + br = au_sbr(sb, bindex);
  25282. + if (au_br_fhsm(br->br_perm)
  25283. + && !sb_rdonly(au_br_sb(br)))
  25284. + return bindex;
  25285. + }
  25286. + else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
  25287. + for (bindex++; bindex <= bbot; bindex++) {
  25288. + br = au_sbr(sb, bindex);
  25289. + if (!au_br_rdonly(br))
  25290. + return bindex;
  25291. + }
  25292. + else
  25293. + for (bindex++; bindex <= bbot; bindex++) {
  25294. + br = au_sbr(sb, bindex);
  25295. + if (!sb_rdonly(au_br_sb(br))) {
  25296. + if (au_br_rdonly(br))
  25297. + a->mvdown.flags
  25298. + |= AUFS_MVDOWN_ROLOWER_R;
  25299. + return bindex;
  25300. + }
  25301. + }
  25302. +
  25303. + return -1;
  25304. +}
  25305. +
  25306. +/* make the parent dir on bdst */
  25307. +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
  25308. +{
  25309. + int err;
  25310. +
  25311. + err = 0;
  25312. + a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
  25313. + a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
  25314. + a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
  25315. + a->mvd_h_dst_parent = NULL;
  25316. + if (au_dbbot(a->parent) >= a->mvd_bdst)
  25317. + a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
  25318. + if (!a->mvd_h_dst_parent) {
  25319. + err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
  25320. + if (unlikely(err)) {
  25321. + AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
  25322. + goto out;
  25323. + }
  25324. + a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
  25325. + }
  25326. +
  25327. +out:
  25328. + AuTraceErr(err);
  25329. + return err;
  25330. +}
  25331. +
  25332. +/* lock them all */
  25333. +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
  25334. +{
  25335. + int err;
  25336. + struct dentry *h_trap;
  25337. +
  25338. + a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
  25339. + a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
  25340. + err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
  25341. + au_opt_udba(a->sb),
  25342. + AuPin_MNT_WRITE | AuPin_DI_LOCKED);
  25343. + AuTraceErr(err);
  25344. + if (unlikely(err)) {
  25345. + AU_MVD_PR(dmsg, "pin_dst failed\n");
  25346. + goto out;
  25347. + }
  25348. +
  25349. + if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
  25350. + a->rename_lock = 0;
  25351. + au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
  25352. + AuLsc_DI_PARENT, AuLsc_I_PARENT3,
  25353. + au_opt_udba(a->sb),
  25354. + AuPin_MNT_WRITE | AuPin_DI_LOCKED);
  25355. + err = au_do_pin(&a->mvd_pin_src);
  25356. + AuTraceErr(err);
  25357. + a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
  25358. + if (unlikely(err)) {
  25359. + AU_MVD_PR(dmsg, "pin_src failed\n");
  25360. + goto out_dst;
  25361. + }
  25362. + goto out; /* success */
  25363. + }
  25364. +
  25365. + a->rename_lock = 1;
  25366. + au_pin_hdir_unlock(&a->mvd_pin_dst);
  25367. + err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
  25368. + au_opt_udba(a->sb),
  25369. + AuPin_MNT_WRITE | AuPin_DI_LOCKED);
  25370. + AuTraceErr(err);
  25371. + a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
  25372. + if (unlikely(err)) {
  25373. + AU_MVD_PR(dmsg, "pin_src failed\n");
  25374. + au_pin_hdir_lock(&a->mvd_pin_dst);
  25375. + goto out_dst;
  25376. + }
  25377. + au_pin_hdir_unlock(&a->mvd_pin_src);
  25378. + h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
  25379. + a->mvd_h_dst_parent, a->mvd_hdir_dst);
  25380. + if (h_trap) {
  25381. + err = (h_trap != a->mvd_h_src_parent);
  25382. + if (err)
  25383. + err = (h_trap != a->mvd_h_dst_parent);
  25384. + }
  25385. + BUG_ON(err); /* it should never happen */
  25386. + if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
  25387. + err = -EBUSY;
  25388. + AuTraceErr(err);
  25389. + vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
  25390. + a->mvd_h_dst_parent, a->mvd_hdir_dst);
  25391. + au_pin_hdir_lock(&a->mvd_pin_src);
  25392. + au_unpin(&a->mvd_pin_src);
  25393. + au_pin_hdir_lock(&a->mvd_pin_dst);
  25394. + goto out_dst;
  25395. + }
  25396. + goto out; /* success */
  25397. +
  25398. +out_dst:
  25399. + au_unpin(&a->mvd_pin_dst);
  25400. +out:
  25401. + AuTraceErr(err);
  25402. + return err;
  25403. +}
  25404. +
  25405. +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
  25406. +{
  25407. + if (!a->rename_lock)
  25408. + au_unpin(&a->mvd_pin_src);
  25409. + else {
  25410. + vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
  25411. + a->mvd_h_dst_parent, a->mvd_hdir_dst);
  25412. + au_pin_hdir_lock(&a->mvd_pin_src);
  25413. + au_unpin(&a->mvd_pin_src);
  25414. + au_pin_hdir_lock(&a->mvd_pin_dst);
  25415. + }
  25416. + au_unpin(&a->mvd_pin_dst);
  25417. +}
  25418. +
  25419. +/* copy-down the file */
  25420. +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
  25421. +{
  25422. + int err;
  25423. + struct au_cp_generic cpg = {
  25424. + .dentry = a->dentry,
  25425. + .bdst = a->mvd_bdst,
  25426. + .bsrc = a->mvd_bsrc,
  25427. + .len = -1,
  25428. + .pin = &a->mvd_pin_dst,
  25429. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  25430. + };
  25431. +
  25432. + AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
  25433. + if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
  25434. + au_fset_cpup(cpg.flags, OVERWRITE);
  25435. + if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
  25436. + au_fset_cpup(cpg.flags, RWDST);
  25437. + err = au_sio_cpdown_simple(&cpg);
  25438. + if (unlikely(err))
  25439. + AU_MVD_PR(dmsg, "cpdown failed\n");
  25440. +
  25441. + AuTraceErr(err);
  25442. + return err;
  25443. +}
  25444. +
  25445. +/*
  25446. + * unlink the whiteout on bdst if exist which may be created by UDBA while we
  25447. + * were sleeping
  25448. + */
  25449. +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
  25450. +{
  25451. + int err;
  25452. + struct path h_path;
  25453. + struct au_branch *br;
  25454. + struct inode *delegated;
  25455. +
  25456. + br = au_sbr(a->sb, a->mvd_bdst);
  25457. + h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
  25458. + err = PTR_ERR(h_path.dentry);
  25459. + if (IS_ERR(h_path.dentry)) {
  25460. + AU_MVD_PR(dmsg, "wh_lkup failed\n");
  25461. + goto out;
  25462. + }
  25463. +
  25464. + err = 0;
  25465. + if (d_is_positive(h_path.dentry)) {
  25466. + h_path.mnt = au_br_mnt(br);
  25467. + delegated = NULL;
  25468. + err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
  25469. + &delegated, /*force*/0);
  25470. + if (unlikely(err == -EWOULDBLOCK)) {
  25471. + pr_warn("cannot retry for NFSv4 delegation"
  25472. + " for an internal unlink\n");
  25473. + iput(delegated);
  25474. + }
  25475. + if (unlikely(err))
  25476. + AU_MVD_PR(dmsg, "wh_unlink failed\n");
  25477. + }
  25478. + dput(h_path.dentry);
  25479. +
  25480. +out:
  25481. + AuTraceErr(err);
  25482. + return err;
  25483. +}
  25484. +
  25485. +/*
  25486. + * unlink the topmost h_dentry
  25487. + */
  25488. +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
  25489. +{
  25490. + int err;
  25491. + struct path h_path;
  25492. + struct inode *delegated;
  25493. +
  25494. + h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
  25495. + h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
  25496. + delegated = NULL;
  25497. + err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
  25498. + if (unlikely(err == -EWOULDBLOCK)) {
  25499. + pr_warn("cannot retry for NFSv4 delegation"
  25500. + " for an internal unlink\n");
  25501. + iput(delegated);
  25502. + }
  25503. + if (unlikely(err))
  25504. + AU_MVD_PR(dmsg, "unlink failed\n");
  25505. +
  25506. + AuTraceErr(err);
  25507. + return err;
  25508. +}
  25509. +
  25510. +/* Since mvdown succeeded, we ignore an error of this function */
  25511. +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
  25512. +{
  25513. + int err;
  25514. + struct au_branch *br;
  25515. +
  25516. + a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
  25517. + br = au_sbr(a->sb, a->mvd_bsrc);
  25518. + err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
  25519. + if (!err) {
  25520. + br = au_sbr(a->sb, a->mvd_bdst);
  25521. + a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
  25522. + err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
  25523. + }
  25524. + if (!err)
  25525. + a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
  25526. + else
  25527. + AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
  25528. +}
  25529. +
  25530. +/*
  25531. + * copy-down the file and unlink the bsrc file.
  25532. + * - unlink the bdst whout if exist
  25533. + * - copy-down the file (with whtmp name and rename)
  25534. + * - unlink the bsrc file
  25535. + */
  25536. +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
  25537. +{
  25538. + int err;
  25539. +
  25540. + err = au_do_mkdir(dmsg, a);
  25541. + if (!err)
  25542. + err = au_do_lock(dmsg, a);
  25543. + if (unlikely(err))
  25544. + goto out;
  25545. +
  25546. + /*
  25547. + * do not revert the activities we made on bdst since they should be
  25548. + * harmless in aufs.
  25549. + */
  25550. +
  25551. + err = au_do_cpdown(dmsg, a);
  25552. + if (!err)
  25553. + err = au_do_unlink_wh(dmsg, a);
  25554. + if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
  25555. + err = au_do_unlink(dmsg, a);
  25556. + if (unlikely(err))
  25557. + goto out_unlock;
  25558. +
  25559. + AuDbg("%pd2, 0x%x, %d --> %d\n",
  25560. + a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
  25561. + if (find_lower_writable(a) < 0)
  25562. + a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
  25563. +
  25564. + if (a->mvdown.flags & AUFS_MVDOWN_STFS)
  25565. + au_do_stfs(dmsg, a);
  25566. +
  25567. + /* maintain internal array */
  25568. + if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
  25569. + au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
  25570. + au_set_dbtop(a->dentry, a->mvd_bdst);
  25571. + au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
  25572. + au_set_ibtop(a->inode, a->mvd_bdst);
  25573. + } else {
  25574. + /* hide the lower */
  25575. + au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
  25576. + au_set_dbbot(a->dentry, a->mvd_bsrc);
  25577. + au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
  25578. + au_set_ibbot(a->inode, a->mvd_bsrc);
  25579. + }
  25580. + if (au_dbbot(a->dentry) < a->mvd_bdst)
  25581. + au_set_dbbot(a->dentry, a->mvd_bdst);
  25582. + if (au_ibbot(a->inode) < a->mvd_bdst)
  25583. + au_set_ibbot(a->inode, a->mvd_bdst);
  25584. +
  25585. +out_unlock:
  25586. + au_do_unlock(dmsg, a);
  25587. +out:
  25588. + AuTraceErr(err);
  25589. + return err;
  25590. +}
  25591. +
  25592. +/* ---------------------------------------------------------------------- */
  25593. +
  25594. +/* make sure the file is idle */
  25595. +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
  25596. +{
  25597. + int err, plinked;
  25598. +
  25599. + err = 0;
  25600. + plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
  25601. + if (au_dbtop(a->dentry) == a->mvd_bsrc
  25602. + && au_dcount(a->dentry) == 1
  25603. + && atomic_read(&a->inode->i_count) == 1
  25604. + /* && a->mvd_h_src_inode->i_nlink == 1 */
  25605. + && (!plinked || !au_plink_test(a->inode))
  25606. + && a->inode->i_nlink == 1)
  25607. + goto out;
  25608. +
  25609. + err = -EBUSY;
  25610. + AU_MVD_PR(dmsg,
  25611. + "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
  25612. + a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
  25613. + atomic_read(&a->inode->i_count), a->inode->i_nlink,
  25614. + a->mvd_h_src_inode->i_nlink,
  25615. + plinked, plinked ? au_plink_test(a->inode) : 0);
  25616. +
  25617. +out:
  25618. + AuTraceErr(err);
  25619. + return err;
  25620. +}
  25621. +
  25622. +/* make sure the parent dir is fine */
  25623. +static int au_mvd_args_parent(const unsigned char dmsg,
  25624. + struct au_mvd_args *a)
  25625. +{
  25626. + int err;
  25627. + aufs_bindex_t bindex;
  25628. +
  25629. + err = 0;
  25630. + if (unlikely(au_alive_dir(a->parent))) {
  25631. + err = -ENOENT;
  25632. + AU_MVD_PR(dmsg, "parent dir is dead\n");
  25633. + goto out;
  25634. + }
  25635. +
  25636. + a->bopq = au_dbdiropq(a->parent);
  25637. + bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
  25638. + AuDbg("b%d\n", bindex);
  25639. + if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
  25640. + || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
  25641. + err = -EINVAL;
  25642. + a->mvd_errno = EAU_MVDOWN_OPAQUE;
  25643. + AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
  25644. + a->bopq, a->mvd_bdst);
  25645. + }
  25646. +
  25647. +out:
  25648. + AuTraceErr(err);
  25649. + return err;
  25650. +}
  25651. +
  25652. +static int au_mvd_args_intermediate(const unsigned char dmsg,
  25653. + struct au_mvd_args *a)
  25654. +{
  25655. + int err;
  25656. + struct au_dinfo *dinfo, *tmp;
  25657. +
  25658. + /* lookup the next lower positive entry */
  25659. + err = -ENOMEM;
  25660. + tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
  25661. + if (unlikely(!tmp))
  25662. + goto out;
  25663. +
  25664. + a->bfound = -1;
  25665. + a->bwh = -1;
  25666. + dinfo = au_di(a->dentry);
  25667. + au_di_cp(tmp, dinfo);
  25668. + au_di_swap(tmp, dinfo);
  25669. +
  25670. + /* returns the number of positive dentries */
  25671. + err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
  25672. + /* AuLkup_IGNORE_PERM */ 0);
  25673. + if (!err)
  25674. + a->bwh = au_dbwh(a->dentry);
  25675. + else if (err > 0)
  25676. + a->bfound = au_dbtop(a->dentry);
  25677. +
  25678. + au_di_swap(tmp, dinfo);
  25679. + au_rw_write_unlock(&tmp->di_rwsem);
  25680. + au_di_free(tmp);
  25681. + if (unlikely(err < 0))
  25682. + AU_MVD_PR(dmsg, "failed look-up lower\n");
  25683. +
  25684. + /*
  25685. + * here, we have these cases.
  25686. + * bfound == -1
  25687. + * no positive dentry under bsrc. there are more sub-cases.
  25688. + * bwh < 0
  25689. + * there no whiteout, we can safely move-down.
  25690. + * bwh <= bsrc
  25691. + * impossible
  25692. + * bsrc < bwh && bwh < bdst
  25693. + * there is a whiteout on RO branch. cannot proceed.
  25694. + * bwh == bdst
  25695. + * there is a whiteout on the RW target branch. it should
  25696. + * be removed.
  25697. + * bdst < bwh
  25698. + * there is a whiteout somewhere unrelated branch.
  25699. + * -1 < bfound && bfound <= bsrc
  25700. + * impossible.
  25701. + * bfound < bdst
  25702. + * found, but it is on RO branch between bsrc and bdst. cannot
  25703. + * proceed.
  25704. + * bfound == bdst
  25705. + * found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
  25706. + * error.
  25707. + * bdst < bfound
  25708. + * found, after we create the file on bdst, it will be hidden.
  25709. + */
  25710. +
  25711. + AuDebugOn(a->bfound == -1
  25712. + && a->bwh != -1
  25713. + && a->bwh <= a->mvd_bsrc);
  25714. + AuDebugOn(-1 < a->bfound
  25715. + && a->bfound <= a->mvd_bsrc);
  25716. +
  25717. + err = -EINVAL;
  25718. + if (a->bfound == -1
  25719. + && a->mvd_bsrc < a->bwh
  25720. + && a->bwh != -1
  25721. + && a->bwh < a->mvd_bdst) {
  25722. + a->mvd_errno = EAU_MVDOWN_WHITEOUT;
  25723. + AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
  25724. + a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
  25725. + goto out;
  25726. + } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
  25727. + a->mvd_errno = EAU_MVDOWN_UPPER;
  25728. + AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
  25729. + a->mvd_bdst, a->bfound);
  25730. + goto out;
  25731. + }
  25732. +
  25733. + err = 0; /* success */
  25734. +
  25735. +out:
  25736. + AuTraceErr(err);
  25737. + return err;
  25738. +}
  25739. +
  25740. +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
  25741. +{
  25742. + int err;
  25743. +
  25744. + err = 0;
  25745. + if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
  25746. + && a->bfound == a->mvd_bdst)
  25747. + err = -EEXIST;
  25748. + AuTraceErr(err);
  25749. + return err;
  25750. +}
  25751. +
  25752. +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
  25753. +{
  25754. + int err;
  25755. + struct au_branch *br;
  25756. +
  25757. + err = -EISDIR;
  25758. + if (unlikely(S_ISDIR(a->inode->i_mode)))
  25759. + goto out;
  25760. +
  25761. + err = -EINVAL;
  25762. + if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
  25763. + a->mvd_bsrc = au_ibtop(a->inode);
  25764. + else {
  25765. + a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
  25766. + if (unlikely(a->mvd_bsrc < 0
  25767. + || (a->mvd_bsrc < au_dbtop(a->dentry)
  25768. + || au_dbbot(a->dentry) < a->mvd_bsrc
  25769. + || !au_h_dptr(a->dentry, a->mvd_bsrc))
  25770. + || (a->mvd_bsrc < au_ibtop(a->inode)
  25771. + || au_ibbot(a->inode) < a->mvd_bsrc
  25772. + || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
  25773. + a->mvd_errno = EAU_MVDOWN_NOUPPER;
  25774. + AU_MVD_PR(dmsg, "no upper\n");
  25775. + goto out;
  25776. + }
  25777. + }
  25778. + if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
  25779. + a->mvd_errno = EAU_MVDOWN_BOTTOM;
  25780. + AU_MVD_PR(dmsg, "on the bottom\n");
  25781. + goto out;
  25782. + }
  25783. + a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
  25784. + br = au_sbr(a->sb, a->mvd_bsrc);
  25785. + err = au_br_rdonly(br);
  25786. + if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
  25787. + if (unlikely(err))
  25788. + goto out;
  25789. + } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
  25790. + || IS_APPEND(a->mvd_h_src_inode))) {
  25791. + if (err)
  25792. + a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
  25793. + /* go on */
  25794. + } else
  25795. + goto out;
  25796. +
  25797. + err = -EINVAL;
  25798. + if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
  25799. + a->mvd_bdst = find_lower_writable(a);
  25800. + if (unlikely(a->mvd_bdst < 0)) {
  25801. + a->mvd_errno = EAU_MVDOWN_BOTTOM;
  25802. + AU_MVD_PR(dmsg, "no writable lower branch\n");
  25803. + goto out;
  25804. + }
  25805. + } else {
  25806. + a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
  25807. + if (unlikely(a->mvd_bdst < 0
  25808. + || au_sbbot(a->sb) < a->mvd_bdst)) {
  25809. + a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
  25810. + AU_MVD_PR(dmsg, "no lower brid\n");
  25811. + goto out;
  25812. + }
  25813. + }
  25814. +
  25815. + err = au_mvd_args_busy(dmsg, a);
  25816. + if (!err)
  25817. + err = au_mvd_args_parent(dmsg, a);
  25818. + if (!err)
  25819. + err = au_mvd_args_intermediate(dmsg, a);
  25820. + if (!err)
  25821. + err = au_mvd_args_exist(dmsg, a);
  25822. + if (!err)
  25823. + AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
  25824. +
  25825. +out:
  25826. + AuTraceErr(err);
  25827. + return err;
  25828. +}
  25829. +
  25830. +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
  25831. +{
  25832. + int err, e;
  25833. + unsigned char dmsg;
  25834. + struct au_mvd_args *args;
  25835. + struct inode *inode;
  25836. +
  25837. + inode = d_inode(dentry);
  25838. + err = -EPERM;
  25839. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  25840. + goto out;
  25841. +
  25842. + err = -ENOMEM;
  25843. + args = kmalloc(sizeof(*args), GFP_NOFS);
  25844. + if (unlikely(!args))
  25845. + goto out;
  25846. +
  25847. + err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
  25848. + if (!err)
  25849. + /* VERIFY_WRITE */
  25850. + err = !access_ok(uarg, sizeof(*uarg));
  25851. + if (unlikely(err)) {
  25852. + err = -EFAULT;
  25853. + AuTraceErr(err);
  25854. + goto out_free;
  25855. + }
  25856. + AuDbg("flags 0x%x\n", args->mvdown.flags);
  25857. + args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
  25858. + args->mvdown.au_errno = 0;
  25859. + args->dentry = dentry;
  25860. + args->inode = inode;
  25861. + args->sb = dentry->d_sb;
  25862. +
  25863. + err = -ENOENT;
  25864. + dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
  25865. + args->parent = dget_parent(dentry);
  25866. + args->dir = d_inode(args->parent);
  25867. + inode_lock_nested(args->dir, I_MUTEX_PARENT);
  25868. + dput(args->parent);
  25869. + if (unlikely(args->parent != dentry->d_parent)) {
  25870. + AU_MVD_PR(dmsg, "parent dir is moved\n");
  25871. + goto out_dir;
  25872. + }
  25873. +
  25874. + inode_lock_nested(inode, I_MUTEX_CHILD);
  25875. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
  25876. + if (unlikely(err))
  25877. + goto out_inode;
  25878. +
  25879. + di_write_lock_parent(args->parent);
  25880. + err = au_mvd_args(dmsg, args);
  25881. + if (unlikely(err))
  25882. + goto out_parent;
  25883. +
  25884. + err = au_do_mvdown(dmsg, args);
  25885. + if (unlikely(err))
  25886. + goto out_parent;
  25887. +
  25888. + au_cpup_attr_timesizes(args->dir);
  25889. + au_cpup_attr_timesizes(inode);
  25890. + if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
  25891. + au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
  25892. + /* au_digen_dec(dentry); */
  25893. +
  25894. +out_parent:
  25895. + di_write_unlock(args->parent);
  25896. + aufs_read_unlock(dentry, AuLock_DW);
  25897. +out_inode:
  25898. + inode_unlock(inode);
  25899. +out_dir:
  25900. + inode_unlock(args->dir);
  25901. +out_free:
  25902. + e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
  25903. + if (unlikely(e))
  25904. + err = -EFAULT;
  25905. + au_kfree_rcu(args);
  25906. +out:
  25907. + AuTraceErr(err);
  25908. + return err;
  25909. +}
  25910. diff -Naur null/fs/aufs/opts.c linux-5.15.131/fs/aufs/opts.c
  25911. --- /dev/null
  25912. +++ linux-5.15.131/fs/aufs/opts.c 2023-09-11 14:32:48.151722514 +0300
  25913. @@ -0,0 +1,1032 @@
  25914. +// SPDX-License-Identifier: GPL-2.0
  25915. +/*
  25916. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  25917. + *
  25918. + * This program, aufs is free software; you can redistribute it and/or modify
  25919. + * it under the terms of the GNU General Public License as published by
  25920. + * the Free Software Foundation; either version 2 of the License, or
  25921. + * (at your option) any later version.
  25922. + *
  25923. + * This program is distributed in the hope that it will be useful,
  25924. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25925. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25926. + * GNU General Public License for more details.
  25927. + *
  25928. + * You should have received a copy of the GNU General Public License
  25929. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  25930. + */
  25931. +
  25932. +/*
  25933. + * mount options/flags
  25934. + */
  25935. +
  25936. +#include <linux/types.h> /* a distribution requires */
  25937. +#include <linux/parser.h>
  25938. +#include "aufs.h"
  25939. +
  25940. +/* ---------------------------------------------------------------------- */
  25941. +
  25942. +static const char *au_parser_pattern(int val, match_table_t tbl)
  25943. +{
  25944. + struct match_token *p;
  25945. +
  25946. + p = tbl;
  25947. + while (p->pattern) {
  25948. + if (p->token == val)
  25949. + return p->pattern;
  25950. + p++;
  25951. + }
  25952. + BUG();
  25953. + return "??";
  25954. +}
  25955. +
  25956. +static const char *au_optstr(int *val, match_table_t tbl)
  25957. +{
  25958. + struct match_token *p;
  25959. + int v;
  25960. +
  25961. + v = *val;
  25962. + if (!v)
  25963. + goto out;
  25964. + p = tbl;
  25965. + while (p->pattern) {
  25966. + if (p->token
  25967. + && (v & p->token) == p->token) {
  25968. + *val &= ~p->token;
  25969. + return p->pattern;
  25970. + }
  25971. + p++;
  25972. + }
  25973. +
  25974. +out:
  25975. + return NULL;
  25976. +}
  25977. +
  25978. +/* ---------------------------------------------------------------------- */
  25979. +
  25980. +static match_table_t brperm = {
  25981. + {AuBrPerm_RO, AUFS_BRPERM_RO},
  25982. + {AuBrPerm_RR, AUFS_BRPERM_RR},
  25983. + {AuBrPerm_RW, AUFS_BRPERM_RW},
  25984. + {0, NULL}
  25985. +};
  25986. +
  25987. +static match_table_t brattr = {
  25988. + /* general */
  25989. + {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
  25990. + {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
  25991. + /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
  25992. + {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
  25993. +#ifdef CONFIG_AUFS_FHSM
  25994. + {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
  25995. +#endif
  25996. +#ifdef CONFIG_AUFS_XATTR
  25997. + {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
  25998. + {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
  25999. + {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
  26000. + {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
  26001. + {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
  26002. + {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
  26003. +#endif
  26004. +
  26005. + /* ro/rr branch */
  26006. + {AuBrRAttr_WH, AUFS_BRRATTR_WH},
  26007. +
  26008. + /* rw branch */
  26009. + {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
  26010. + {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
  26011. +
  26012. + {0, NULL}
  26013. +};
  26014. +
  26015. +static int br_attr_val(char *str, match_table_t table, substring_t args[])
  26016. +{
  26017. + int attr, v;
  26018. + char *p;
  26019. +
  26020. + attr = 0;
  26021. + do {
  26022. + p = strchr(str, '+');
  26023. + if (p)
  26024. + *p = 0;
  26025. + v = match_token(str, table, args);
  26026. + if (v) {
  26027. + if (v & AuBrAttr_CMOO_Mask)
  26028. + attr &= ~AuBrAttr_CMOO_Mask;
  26029. + attr |= v;
  26030. + } else {
  26031. + if (p)
  26032. + *p = '+';
  26033. + pr_warn("ignored branch attribute %s\n", str);
  26034. + break;
  26035. + }
  26036. + if (p)
  26037. + str = p + 1;
  26038. + } while (p);
  26039. +
  26040. + return attr;
  26041. +}
  26042. +
  26043. +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
  26044. +{
  26045. + int sz;
  26046. + const char *p;
  26047. + char *q;
  26048. +
  26049. + q = str->a;
  26050. + *q = 0;
  26051. + p = au_optstr(&perm, brattr);
  26052. + if (p) {
  26053. + sz = strlen(p);
  26054. + memcpy(q, p, sz + 1);
  26055. + q += sz;
  26056. + } else
  26057. + goto out;
  26058. +
  26059. + do {
  26060. + p = au_optstr(&perm, brattr);
  26061. + if (p) {
  26062. + *q++ = '+';
  26063. + sz = strlen(p);
  26064. + memcpy(q, p, sz + 1);
  26065. + q += sz;
  26066. + }
  26067. + } while (p);
  26068. +
  26069. +out:
  26070. + return q - str->a;
  26071. +}
  26072. +
  26073. +int au_br_perm_val(char *perm)
  26074. +{
  26075. + int val, bad, sz;
  26076. + char *p;
  26077. + substring_t args[MAX_OPT_ARGS];
  26078. + au_br_perm_str_t attr;
  26079. +
  26080. + p = strchr(perm, '+');
  26081. + if (p)
  26082. + *p = 0;
  26083. + val = match_token(perm, brperm, args);
  26084. + if (!val) {
  26085. + if (p)
  26086. + *p = '+';
  26087. + pr_warn("ignored branch permission %s\n", perm);
  26088. + val = AuBrPerm_RO;
  26089. + goto out;
  26090. + }
  26091. + if (!p)
  26092. + goto out;
  26093. +
  26094. + val |= br_attr_val(p + 1, brattr, args);
  26095. +
  26096. + bad = 0;
  26097. + switch (val & AuBrPerm_Mask) {
  26098. + case AuBrPerm_RO:
  26099. + case AuBrPerm_RR:
  26100. + bad = val & AuBrWAttr_Mask;
  26101. + val &= ~AuBrWAttr_Mask;
  26102. + break;
  26103. + case AuBrPerm_RW:
  26104. + bad = val & AuBrRAttr_Mask;
  26105. + val &= ~AuBrRAttr_Mask;
  26106. + break;
  26107. + }
  26108. +
  26109. + /*
  26110. + * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
  26111. + * does not treat it as an error, just warning.
  26112. + * this is a tiny guard for the user operation.
  26113. + */
  26114. + if (val & AuBrAttr_UNPIN) {
  26115. + bad |= AuBrAttr_UNPIN;
  26116. + val &= ~AuBrAttr_UNPIN;
  26117. + }
  26118. +
  26119. + if (unlikely(bad)) {
  26120. + sz = au_do_optstr_br_attr(&attr, bad);
  26121. + AuDebugOn(!sz);
  26122. + pr_warn("ignored branch attribute %s\n", attr.a);
  26123. + }
  26124. +
  26125. +out:
  26126. + return val;
  26127. +}
  26128. +
  26129. +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
  26130. +{
  26131. + au_br_perm_str_t attr;
  26132. + const char *p;
  26133. + char *q;
  26134. + int sz;
  26135. +
  26136. + q = str->a;
  26137. + p = au_optstr(&perm, brperm);
  26138. + AuDebugOn(!p || !*p);
  26139. + sz = strlen(p);
  26140. + memcpy(q, p, sz + 1);
  26141. + q += sz;
  26142. +
  26143. + sz = au_do_optstr_br_attr(&attr, perm);
  26144. + if (sz) {
  26145. + *q++ = '+';
  26146. + memcpy(q, attr.a, sz + 1);
  26147. + }
  26148. +
  26149. + AuDebugOn(strlen(str->a) >= sizeof(str->a));
  26150. +}
  26151. +
  26152. +/* ---------------------------------------------------------------------- */
  26153. +
  26154. +static match_table_t udbalevel = {
  26155. + {AuOpt_UDBA_REVAL, "reval"},
  26156. + {AuOpt_UDBA_NONE, "none"},
  26157. +#ifdef CONFIG_AUFS_HNOTIFY
  26158. + {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
  26159. +#ifdef CONFIG_AUFS_HFSNOTIFY
  26160. + {AuOpt_UDBA_HNOTIFY, "fsnotify"},
  26161. +#endif
  26162. +#endif
  26163. + {-1, NULL}
  26164. +};
  26165. +
  26166. +int au_udba_val(char *str)
  26167. +{
  26168. + substring_t args[MAX_OPT_ARGS];
  26169. +
  26170. + return match_token(str, udbalevel, args);
  26171. +}
  26172. +
  26173. +const char *au_optstr_udba(int udba)
  26174. +{
  26175. + return au_parser_pattern(udba, udbalevel);
  26176. +}
  26177. +
  26178. +/* ---------------------------------------------------------------------- */
  26179. +
  26180. +static match_table_t au_wbr_create_policy = {
  26181. + {AuWbrCreate_TDP, "tdp"},
  26182. + {AuWbrCreate_TDP, "top-down-parent"},
  26183. + {AuWbrCreate_RR, "rr"},
  26184. + {AuWbrCreate_RR, "round-robin"},
  26185. + {AuWbrCreate_MFS, "mfs"},
  26186. + {AuWbrCreate_MFS, "most-free-space"},
  26187. + {AuWbrCreate_MFSV, "mfs:%d"},
  26188. + {AuWbrCreate_MFSV, "most-free-space:%d"},
  26189. +
  26190. + /* top-down regardless the parent, and then mfs */
  26191. + {AuWbrCreate_TDMFS, "tdmfs:%d"},
  26192. + {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
  26193. +
  26194. + {AuWbrCreate_MFSRR, "mfsrr:%d"},
  26195. + {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
  26196. + {AuWbrCreate_PMFS, "pmfs"},
  26197. + {AuWbrCreate_PMFSV, "pmfs:%d"},
  26198. + {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
  26199. + {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
  26200. +
  26201. + {-1, NULL}
  26202. +};
  26203. +
  26204. +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
  26205. + struct au_opt_wbr_create *create)
  26206. +{
  26207. + int err;
  26208. + unsigned long long ull;
  26209. +
  26210. + err = 0;
  26211. + if (!match_u64(arg, &ull))
  26212. + create->mfsrr_watermark = ull;
  26213. + else {
  26214. + pr_err("bad integer in %s\n", str);
  26215. + err = -EINVAL;
  26216. + }
  26217. +
  26218. + return err;
  26219. +}
  26220. +
  26221. +static int au_wbr_mfs_sec(substring_t *arg, char *str,
  26222. + struct au_opt_wbr_create *create)
  26223. +{
  26224. + int n, err;
  26225. +
  26226. + err = 0;
  26227. + if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
  26228. + create->mfs_second = n;
  26229. + else {
  26230. + pr_err("bad integer in %s\n", str);
  26231. + err = -EINVAL;
  26232. + }
  26233. +
  26234. + return err;
  26235. +}
  26236. +
  26237. +int au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
  26238. +{
  26239. + int err, e;
  26240. + substring_t args[MAX_OPT_ARGS];
  26241. +
  26242. + err = match_token(str, au_wbr_create_policy, args);
  26243. + create->wbr_create = err;
  26244. + switch (err) {
  26245. + case AuWbrCreate_MFSRRV:
  26246. + case AuWbrCreate_TDMFSV:
  26247. + case AuWbrCreate_PMFSRRV:
  26248. + e = au_wbr_mfs_wmark(&args[0], str, create);
  26249. + if (!e)
  26250. + e = au_wbr_mfs_sec(&args[1], str, create);
  26251. + if (unlikely(e))
  26252. + err = e;
  26253. + break;
  26254. + case AuWbrCreate_MFSRR:
  26255. + case AuWbrCreate_TDMFS:
  26256. + case AuWbrCreate_PMFSRR:
  26257. + e = au_wbr_mfs_wmark(&args[0], str, create);
  26258. + if (unlikely(e)) {
  26259. + err = e;
  26260. + break;
  26261. + }
  26262. + fallthrough;
  26263. + case AuWbrCreate_MFS:
  26264. + case AuWbrCreate_PMFS:
  26265. + create->mfs_second = AUFS_MFS_DEF_SEC;
  26266. + break;
  26267. + case AuWbrCreate_MFSV:
  26268. + case AuWbrCreate_PMFSV:
  26269. + e = au_wbr_mfs_sec(&args[0], str, create);
  26270. + if (unlikely(e))
  26271. + err = e;
  26272. + break;
  26273. + }
  26274. +
  26275. + return err;
  26276. +}
  26277. +
  26278. +const char *au_optstr_wbr_create(int wbr_create)
  26279. +{
  26280. + return au_parser_pattern(wbr_create, au_wbr_create_policy);
  26281. +}
  26282. +
  26283. +static match_table_t au_wbr_copyup_policy = {
  26284. + {AuWbrCopyup_TDP, "tdp"},
  26285. + {AuWbrCopyup_TDP, "top-down-parent"},
  26286. + {AuWbrCopyup_BUP, "bup"},
  26287. + {AuWbrCopyup_BUP, "bottom-up-parent"},
  26288. + {AuWbrCopyup_BU, "bu"},
  26289. + {AuWbrCopyup_BU, "bottom-up"},
  26290. + {-1, NULL}
  26291. +};
  26292. +
  26293. +int au_wbr_copyup_val(char *str)
  26294. +{
  26295. + substring_t args[MAX_OPT_ARGS];
  26296. +
  26297. + return match_token(str, au_wbr_copyup_policy, args);
  26298. +}
  26299. +
  26300. +const char *au_optstr_wbr_copyup(int wbr_copyup)
  26301. +{
  26302. + return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
  26303. +}
  26304. +
  26305. +/* ---------------------------------------------------------------------- */
  26306. +
  26307. +int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
  26308. + aufs_bindex_t bindex)
  26309. +{
  26310. + int err;
  26311. + struct au_opt_add *add = &opt->add;
  26312. + char *p;
  26313. +
  26314. + add->bindex = bindex;
  26315. + add->perm = AuBrPerm_RO;
  26316. + add->pathname = opt_str;
  26317. + p = strchr(opt_str, '=');
  26318. + if (p) {
  26319. + *p++ = 0;
  26320. + if (*p)
  26321. + add->perm = au_br_perm_val(p);
  26322. + }
  26323. +
  26324. + err = vfsub_kern_path(add->pathname, AuOpt_LkupDirFlags, &add->path);
  26325. + if (!err) {
  26326. + if (!p) {
  26327. + add->perm = AuBrPerm_RO;
  26328. + if (au_test_fs_rr(add->path.dentry->d_sb))
  26329. + add->perm = AuBrPerm_RR;
  26330. + else if (!bindex && !(sb_flags & SB_RDONLY))
  26331. + add->perm = AuBrPerm_RW;
  26332. + }
  26333. + opt->type = Opt_add;
  26334. + goto out;
  26335. + }
  26336. + pr_err("lookup failed %s (%d)\n", add->pathname, err);
  26337. + err = -EINVAL;
  26338. +
  26339. +out:
  26340. + return err;
  26341. +}
  26342. +
  26343. +static int au_opt_wbr_create(struct super_block *sb,
  26344. + struct au_opt_wbr_create *create)
  26345. +{
  26346. + int err;
  26347. + struct au_sbinfo *sbinfo;
  26348. +
  26349. + SiMustWriteLock(sb);
  26350. +
  26351. + err = 1; /* handled */
  26352. + sbinfo = au_sbi(sb);
  26353. + if (sbinfo->si_wbr_create_ops->fin) {
  26354. + err = sbinfo->si_wbr_create_ops->fin(sb);
  26355. + if (!err)
  26356. + err = 1;
  26357. + }
  26358. +
  26359. + sbinfo->si_wbr_create = create->wbr_create;
  26360. + sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
  26361. + switch (create->wbr_create) {
  26362. + case AuWbrCreate_MFSRRV:
  26363. + case AuWbrCreate_MFSRR:
  26364. + case AuWbrCreate_TDMFS:
  26365. + case AuWbrCreate_TDMFSV:
  26366. + case AuWbrCreate_PMFSRR:
  26367. + case AuWbrCreate_PMFSRRV:
  26368. + sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
  26369. + fallthrough;
  26370. + case AuWbrCreate_MFS:
  26371. + case AuWbrCreate_MFSV:
  26372. + case AuWbrCreate_PMFS:
  26373. + case AuWbrCreate_PMFSV:
  26374. + sbinfo->si_wbr_mfs.mfs_expire
  26375. + = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
  26376. + break;
  26377. + }
  26378. +
  26379. + if (sbinfo->si_wbr_create_ops->init)
  26380. + sbinfo->si_wbr_create_ops->init(sb); /* ignore */
  26381. +
  26382. + return err;
  26383. +}
  26384. +
  26385. +/*
  26386. + * returns,
  26387. + * plus: processed without an error
  26388. + * zero: unprocessed
  26389. + */
  26390. +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
  26391. + struct au_opts *opts)
  26392. +{
  26393. + int err;
  26394. + struct au_sbinfo *sbinfo;
  26395. +
  26396. + SiMustWriteLock(sb);
  26397. +
  26398. + err = 1; /* handled */
  26399. + sbinfo = au_sbi(sb);
  26400. + switch (opt->type) {
  26401. + case Opt_udba:
  26402. + sbinfo->si_mntflags &= ~AuOptMask_UDBA;
  26403. + sbinfo->si_mntflags |= opt->udba;
  26404. + opts->given_udba |= opt->udba;
  26405. + break;
  26406. +
  26407. + case Opt_plink:
  26408. + if (opt->tf)
  26409. + au_opt_set(sbinfo->si_mntflags, PLINK);
  26410. + else {
  26411. + if (au_opt_test(sbinfo->si_mntflags, PLINK))
  26412. + au_plink_put(sb, /*verbose*/1);
  26413. + au_opt_clr(sbinfo->si_mntflags, PLINK);
  26414. + }
  26415. + break;
  26416. + case Opt_list_plink:
  26417. + if (au_opt_test(sbinfo->si_mntflags, PLINK))
  26418. + au_plink_list(sb);
  26419. + break;
  26420. +
  26421. + case Opt_dio:
  26422. + if (opt->tf) {
  26423. + au_opt_set(sbinfo->si_mntflags, DIO);
  26424. + au_fset_opts(opts->flags, REFRESH_DYAOP);
  26425. + } else {
  26426. + au_opt_clr(sbinfo->si_mntflags, DIO);
  26427. + au_fset_opts(opts->flags, REFRESH_DYAOP);
  26428. + }
  26429. + break;
  26430. +
  26431. + case Opt_fhsm_sec:
  26432. + au_fhsm_set(sbinfo, opt->fhsm_second);
  26433. + break;
  26434. +
  26435. + case Opt_diropq_a:
  26436. + au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
  26437. + break;
  26438. + case Opt_diropq_w:
  26439. + au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
  26440. + break;
  26441. +
  26442. + case Opt_warn_perm:
  26443. + if (opt->tf)
  26444. + au_opt_set(sbinfo->si_mntflags, WARN_PERM);
  26445. + else
  26446. + au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
  26447. + break;
  26448. +
  26449. + case Opt_verbose:
  26450. + if (opt->tf)
  26451. + au_opt_set(sbinfo->si_mntflags, VERBOSE);
  26452. + else
  26453. + au_opt_clr(sbinfo->si_mntflags, VERBOSE);
  26454. + break;
  26455. +
  26456. + case Opt_sum:
  26457. + if (opt->tf)
  26458. + au_opt_set(sbinfo->si_mntflags, SUM);
  26459. + else {
  26460. + au_opt_clr(sbinfo->si_mntflags, SUM);
  26461. + au_opt_clr(sbinfo->si_mntflags, SUM_W);
  26462. + }
  26463. + break;
  26464. + case Opt_wsum:
  26465. + au_opt_clr(sbinfo->si_mntflags, SUM);
  26466. + au_opt_set(sbinfo->si_mntflags, SUM_W);
  26467. + break;
  26468. +
  26469. + case Opt_wbr_create:
  26470. + err = au_opt_wbr_create(sb, &opt->wbr_create);
  26471. + break;
  26472. + case Opt_wbr_copyup:
  26473. + sbinfo->si_wbr_copyup = opt->wbr_copyup;
  26474. + sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
  26475. + break;
  26476. +
  26477. + case Opt_dirwh:
  26478. + sbinfo->si_dirwh = opt->dirwh;
  26479. + break;
  26480. +
  26481. + case Opt_rdcache:
  26482. + sbinfo->si_rdcache
  26483. + = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
  26484. + break;
  26485. + case Opt_rdblk:
  26486. + sbinfo->si_rdblk = opt->rdblk;
  26487. + break;
  26488. + case Opt_rdhash:
  26489. + sbinfo->si_rdhash = opt->rdhash;
  26490. + break;
  26491. +
  26492. + case Opt_shwh:
  26493. + if (opt->tf)
  26494. + au_opt_set(sbinfo->si_mntflags, SHWH);
  26495. + else
  26496. + au_opt_clr(sbinfo->si_mntflags, SHWH);
  26497. + break;
  26498. +
  26499. + case Opt_dirperm1:
  26500. + if (opt->tf)
  26501. + au_opt_set(sbinfo->si_mntflags, DIRPERM1);
  26502. + else
  26503. + au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
  26504. + break;
  26505. +
  26506. + case Opt_trunc_xino:
  26507. + if (opt->tf)
  26508. + au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
  26509. + else
  26510. + au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
  26511. + break;
  26512. +
  26513. + case Opt_trunc_xino_path:
  26514. + case Opt_itrunc_xino:
  26515. + err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
  26516. + /*idx_begin*/0);
  26517. + if (!err)
  26518. + err = 1;
  26519. + break;
  26520. +
  26521. + case Opt_trunc_xib:
  26522. + if (opt->tf)
  26523. + au_fset_opts(opts->flags, TRUNC_XIB);
  26524. + else
  26525. + au_fclr_opts(opts->flags, TRUNC_XIB);
  26526. + break;
  26527. +
  26528. + case Opt_dirren:
  26529. + err = 1;
  26530. + if (opt->tf) {
  26531. + if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
  26532. + err = au_dr_opt_set(sb);
  26533. + if (!err)
  26534. + err = 1;
  26535. + }
  26536. + if (err == 1)
  26537. + au_opt_set(sbinfo->si_mntflags, DIRREN);
  26538. + } else {
  26539. + if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
  26540. + err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
  26541. + DR_FLUSHED));
  26542. + if (!err)
  26543. + err = 1;
  26544. + }
  26545. + if (err == 1)
  26546. + au_opt_clr(sbinfo->si_mntflags, DIRREN);
  26547. + }
  26548. + break;
  26549. +
  26550. + case Opt_acl:
  26551. + if (opt->tf)
  26552. + sb->s_flags |= SB_POSIXACL;
  26553. + else
  26554. + sb->s_flags &= ~SB_POSIXACL;
  26555. + break;
  26556. +
  26557. + default:
  26558. + err = 0;
  26559. + break;
  26560. + }
  26561. +
  26562. + return err;
  26563. +}
  26564. +
  26565. +/*
  26566. + * returns tri-state.
  26567. + * plus: processed without an error
  26568. + * zero: unprocessed
  26569. + * minus: error
  26570. + */
  26571. +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
  26572. + struct au_opts *opts)
  26573. +{
  26574. + int err, do_refresh;
  26575. +
  26576. + err = 0;
  26577. + switch (opt->type) {
  26578. + case Opt_append:
  26579. + opt->add.bindex = au_sbbot(sb) + 1;
  26580. + if (opt->add.bindex < 0)
  26581. + opt->add.bindex = 0;
  26582. + goto add;
  26583. + /* Always goto add, not fallthrough */
  26584. + case Opt_prepend:
  26585. + opt->add.bindex = 0;
  26586. + fallthrough;
  26587. + add: /* indented label */
  26588. + case Opt_add:
  26589. + err = au_br_add(sb, &opt->add,
  26590. + au_ftest_opts(opts->flags, REMOUNT));
  26591. + if (!err) {
  26592. + err = 1;
  26593. + au_fset_opts(opts->flags, REFRESH);
  26594. + }
  26595. + break;
  26596. +
  26597. + case Opt_del:
  26598. + case Opt_idel:
  26599. + err = au_br_del(sb, &opt->del,
  26600. + au_ftest_opts(opts->flags, REMOUNT));
  26601. + if (!err) {
  26602. + err = 1;
  26603. + au_fset_opts(opts->flags, TRUNC_XIB);
  26604. + au_fset_opts(opts->flags, REFRESH);
  26605. + }
  26606. + break;
  26607. +
  26608. + case Opt_mod:
  26609. + case Opt_imod:
  26610. + err = au_br_mod(sb, &opt->mod,
  26611. + au_ftest_opts(opts->flags, REMOUNT),
  26612. + &do_refresh);
  26613. + if (!err) {
  26614. + err = 1;
  26615. + if (do_refresh)
  26616. + au_fset_opts(opts->flags, REFRESH);
  26617. + }
  26618. + break;
  26619. + }
  26620. + return err;
  26621. +}
  26622. +
  26623. +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
  26624. + struct au_opt_xino **opt_xino,
  26625. + struct au_opts *opts)
  26626. +{
  26627. + int err;
  26628. +
  26629. + err = 0;
  26630. + switch (opt->type) {
  26631. + case Opt_xino:
  26632. + err = au_xino_set(sb, &opt->xino,
  26633. + !!au_ftest_opts(opts->flags, REMOUNT));
  26634. + if (!err)
  26635. + *opt_xino = &opt->xino;
  26636. + break;
  26637. + case Opt_noxino:
  26638. + au_xino_clr(sb);
  26639. + *opt_xino = (void *)-1;
  26640. + break;
  26641. + }
  26642. +
  26643. + return err;
  26644. +}
  26645. +
  26646. +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
  26647. + unsigned int pending)
  26648. +{
  26649. + int err, fhsm;
  26650. + aufs_bindex_t bindex, bbot;
  26651. + unsigned char do_plink, skip, do_free, can_no_dreval;
  26652. + struct au_branch *br;
  26653. + struct au_wbr *wbr;
  26654. + struct dentry *root, *dentry;
  26655. + struct inode *dir, *h_dir;
  26656. + struct au_sbinfo *sbinfo;
  26657. + struct au_hinode *hdir;
  26658. +
  26659. + SiMustAnyLock(sb);
  26660. +
  26661. + sbinfo = au_sbi(sb);
  26662. + AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
  26663. +
  26664. + if (!(sb_flags & SB_RDONLY)) {
  26665. + if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
  26666. + pr_warn("first branch should be rw\n");
  26667. + if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
  26668. + pr_warn_once("shwh should be used with ro\n");
  26669. + }
  26670. +
  26671. + if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
  26672. + && !au_opt_test(sbinfo->si_mntflags, XINO))
  26673. + pr_warn_once("udba=*notify requires xino\n");
  26674. +
  26675. + if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
  26676. + pr_warn_once("dirperm1 breaks the protection"
  26677. + " by the permission bits on the lower branch\n");
  26678. +
  26679. + err = 0;
  26680. + fhsm = 0;
  26681. + root = sb->s_root;
  26682. + dir = d_inode(root);
  26683. + do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
  26684. + can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
  26685. + UDBA_NONE);
  26686. + bbot = au_sbbot(sb);
  26687. + for (bindex = 0; !err && bindex <= bbot; bindex++) {
  26688. + skip = 0;
  26689. + h_dir = au_h_iptr(dir, bindex);
  26690. + br = au_sbr(sb, bindex);
  26691. +
  26692. + if ((br->br_perm & AuBrAttr_ICEX)
  26693. + && !h_dir->i_op->listxattr)
  26694. + br->br_perm &= ~AuBrAttr_ICEX;
  26695. +#if 0 /* untested */
  26696. + if ((br->br_perm & AuBrAttr_ICEX_SEC)
  26697. + && (au_br_sb(br)->s_flags & SB_NOSEC))
  26698. + br->br_perm &= ~AuBrAttr_ICEX_SEC;
  26699. +#endif
  26700. +
  26701. + do_free = 0;
  26702. + wbr = br->br_wbr;
  26703. + if (wbr)
  26704. + wbr_wh_read_lock(wbr);
  26705. +
  26706. + if (!au_br_writable(br->br_perm)) {
  26707. + do_free = !!wbr;
  26708. + skip = (!wbr
  26709. + || (!wbr->wbr_whbase
  26710. + && !wbr->wbr_plink
  26711. + && !wbr->wbr_orph));
  26712. + } else if (!au_br_wh_linkable(br->br_perm)) {
  26713. + /* skip = (!br->br_whbase && !br->br_orph); */
  26714. + skip = (!wbr || !wbr->wbr_whbase);
  26715. + if (skip && wbr) {
  26716. + if (do_plink)
  26717. + skip = !!wbr->wbr_plink;
  26718. + else
  26719. + skip = !wbr->wbr_plink;
  26720. + }
  26721. + } else {
  26722. + /* skip = (br->br_whbase && br->br_ohph); */
  26723. + skip = (wbr && wbr->wbr_whbase);
  26724. + if (skip) {
  26725. + if (do_plink)
  26726. + skip = !!wbr->wbr_plink;
  26727. + else
  26728. + skip = !wbr->wbr_plink;
  26729. + }
  26730. + }
  26731. + if (wbr)
  26732. + wbr_wh_read_unlock(wbr);
  26733. +
  26734. + if (can_no_dreval) {
  26735. + dentry = br->br_path.dentry;
  26736. + spin_lock(&dentry->d_lock);
  26737. + if (dentry->d_flags &
  26738. + (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
  26739. + can_no_dreval = 0;
  26740. + spin_unlock(&dentry->d_lock);
  26741. + }
  26742. +
  26743. + if (au_br_fhsm(br->br_perm)) {
  26744. + fhsm++;
  26745. + AuDebugOn(!br->br_fhsm);
  26746. + }
  26747. +
  26748. + if (skip)
  26749. + continue;
  26750. +
  26751. + hdir = au_hi(dir, bindex);
  26752. + au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
  26753. + if (wbr)
  26754. + wbr_wh_write_lock(wbr);
  26755. + err = au_wh_init(br, sb);
  26756. + if (wbr)
  26757. + wbr_wh_write_unlock(wbr);
  26758. + au_hn_inode_unlock(hdir);
  26759. +
  26760. + if (!err && do_free) {
  26761. + au_kfree_rcu(wbr);
  26762. + br->br_wbr = NULL;
  26763. + }
  26764. + }
  26765. +
  26766. + if (can_no_dreval)
  26767. + au_fset_si(sbinfo, NO_DREVAL);
  26768. + else
  26769. + au_fclr_si(sbinfo, NO_DREVAL);
  26770. +
  26771. + if (fhsm >= 2) {
  26772. + au_fset_si(sbinfo, FHSM);
  26773. + for (bindex = bbot; bindex >= 0; bindex--) {
  26774. + br = au_sbr(sb, bindex);
  26775. + if (au_br_fhsm(br->br_perm)) {
  26776. + au_fhsm_set_bottom(sb, bindex);
  26777. + break;
  26778. + }
  26779. + }
  26780. + } else {
  26781. + au_fclr_si(sbinfo, FHSM);
  26782. + au_fhsm_set_bottom(sb, -1);
  26783. + }
  26784. +
  26785. + return err;
  26786. +}
  26787. +
  26788. +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
  26789. +{
  26790. + int err;
  26791. + unsigned int tmp;
  26792. + aufs_bindex_t bindex, bbot;
  26793. + struct au_opt *opt;
  26794. + struct au_opt_xino *opt_xino, xino;
  26795. + struct au_sbinfo *sbinfo;
  26796. + struct au_branch *br;
  26797. + struct inode *dir;
  26798. +
  26799. + SiMustWriteLock(sb);
  26800. +
  26801. + err = 0;
  26802. + opt_xino = NULL;
  26803. + opt = opts->opt;
  26804. + while (err >= 0 && opt->type != Opt_tail)
  26805. + err = au_opt_simple(sb, opt++, opts);
  26806. + if (err > 0)
  26807. + err = 0;
  26808. + else if (unlikely(err < 0))
  26809. + goto out;
  26810. +
  26811. + /* disable xino and udba temporary */
  26812. + sbinfo = au_sbi(sb);
  26813. + tmp = sbinfo->si_mntflags;
  26814. + au_opt_clr(sbinfo->si_mntflags, XINO);
  26815. + au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
  26816. +
  26817. + opt = opts->opt;
  26818. + while (err >= 0 && opt->type != Opt_tail)
  26819. + err = au_opt_br(sb, opt++, opts);
  26820. + if (err > 0)
  26821. + err = 0;
  26822. + else if (unlikely(err < 0))
  26823. + goto out;
  26824. +
  26825. + bbot = au_sbbot(sb);
  26826. + if (unlikely(bbot < 0)) {
  26827. + err = -EINVAL;
  26828. + pr_err("no branches\n");
  26829. + goto out;
  26830. + }
  26831. +
  26832. + if (au_opt_test(tmp, XINO))
  26833. + au_opt_set(sbinfo->si_mntflags, XINO);
  26834. + opt = opts->opt;
  26835. + while (!err && opt->type != Opt_tail)
  26836. + err = au_opt_xino(sb, opt++, &opt_xino, opts);
  26837. + if (unlikely(err))
  26838. + goto out;
  26839. +
  26840. + err = au_opts_verify(sb, sb->s_flags, tmp);
  26841. + if (unlikely(err))
  26842. + goto out;
  26843. +
  26844. + /* restore xino */
  26845. + if (au_opt_test(tmp, XINO) && !opt_xino) {
  26846. + xino.file = au_xino_def(sb);
  26847. + err = PTR_ERR(xino.file);
  26848. + if (IS_ERR(xino.file))
  26849. + goto out;
  26850. +
  26851. + err = au_xino_set(sb, &xino, /*remount*/0);
  26852. + fput(xino.file);
  26853. + if (unlikely(err))
  26854. + goto out;
  26855. + }
  26856. +
  26857. + /* restore udba */
  26858. + tmp &= AuOptMask_UDBA;
  26859. + sbinfo->si_mntflags &= ~AuOptMask_UDBA;
  26860. + sbinfo->si_mntflags |= tmp;
  26861. + bbot = au_sbbot(sb);
  26862. + for (bindex = 0; bindex <= bbot; bindex++) {
  26863. + br = au_sbr(sb, bindex);
  26864. + err = au_hnotify_reset_br(tmp, br, br->br_perm);
  26865. + if (unlikely(err))
  26866. + AuIOErr("hnotify failed on br %d, %d, ignored\n",
  26867. + bindex, err);
  26868. + /* go on even if err */
  26869. + }
  26870. + if (au_opt_test(tmp, UDBA_HNOTIFY)) {
  26871. + dir = d_inode(sb->s_root);
  26872. + au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
  26873. + }
  26874. +
  26875. +out:
  26876. + return err;
  26877. +}
  26878. +
  26879. +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
  26880. +{
  26881. + int err, rerr;
  26882. + unsigned char no_dreval;
  26883. + struct inode *dir;
  26884. + struct au_opt_xino *opt_xino;
  26885. + struct au_opt *opt;
  26886. + struct au_sbinfo *sbinfo;
  26887. +
  26888. + SiMustWriteLock(sb);
  26889. +
  26890. + err = au_dr_opt_flush(sb);
  26891. + if (unlikely(err))
  26892. + goto out;
  26893. + au_fset_opts(opts->flags, DR_FLUSHED);
  26894. +
  26895. + dir = d_inode(sb->s_root);
  26896. + sbinfo = au_sbi(sb);
  26897. + opt_xino = NULL;
  26898. + opt = opts->opt;
  26899. + while (err >= 0 && opt->type != Opt_tail) {
  26900. + err = au_opt_simple(sb, opt, opts);
  26901. + if (!err)
  26902. + err = au_opt_br(sb, opt, opts);
  26903. + if (!err)
  26904. + err = au_opt_xino(sb, opt, &opt_xino, opts);
  26905. + opt++;
  26906. + }
  26907. + if (err > 0)
  26908. + err = 0;
  26909. + AuTraceErr(err);
  26910. + /* go on even err */
  26911. +
  26912. + no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
  26913. + rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
  26914. + if (unlikely(rerr && !err))
  26915. + err = rerr;
  26916. +
  26917. + if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
  26918. + au_fset_opts(opts->flags, REFRESH_IDOP);
  26919. +
  26920. + if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
  26921. + rerr = au_xib_trunc(sb);
  26922. + if (unlikely(rerr && !err))
  26923. + err = rerr;
  26924. + }
  26925. +
  26926. + /* will be handled by the caller */
  26927. + if (!au_ftest_opts(opts->flags, REFRESH)
  26928. + && (opts->given_udba
  26929. + || au_opt_test(sbinfo->si_mntflags, XINO)
  26930. + || au_ftest_opts(opts->flags, REFRESH_IDOP)
  26931. + ))
  26932. + au_fset_opts(opts->flags, REFRESH);
  26933. +
  26934. + AuDbg("status 0x%x\n", opts->flags);
  26935. +
  26936. +out:
  26937. + return err;
  26938. +}
  26939. +
  26940. +/* ---------------------------------------------------------------------- */
  26941. +
  26942. +unsigned int au_opt_udba(struct super_block *sb)
  26943. +{
  26944. + return au_mntflags(sb) & AuOptMask_UDBA;
  26945. +}
  26946. diff -Naur null/fs/aufs/opts.h linux-5.15.131/fs/aufs/opts.h
  26947. --- /dev/null
  26948. +++ linux-5.15.131/fs/aufs/opts.h 2023-09-11 14:32:48.151722514 +0300
  26949. @@ -0,0 +1,263 @@
  26950. +/* SPDX-License-Identifier: GPL-2.0 */
  26951. +/*
  26952. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  26953. + *
  26954. + * This program, aufs is free software; you can redistribute it and/or modify
  26955. + * it under the terms of the GNU General Public License as published by
  26956. + * the Free Software Foundation; either version 2 of the License, or
  26957. + * (at your option) any later version.
  26958. + *
  26959. + * This program is distributed in the hope that it will be useful,
  26960. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26961. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26962. + * GNU General Public License for more details.
  26963. + *
  26964. + * You should have received a copy of the GNU General Public License
  26965. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26966. + */
  26967. +
  26968. +/*
  26969. + * mount options/flags
  26970. + */
  26971. +
  26972. +#ifndef __AUFS_OPTS_H__
  26973. +#define __AUFS_OPTS_H__
  26974. +
  26975. +#ifdef __KERNEL__
  26976. +
  26977. +#include <linux/fs_parser.h>
  26978. +#include <linux/namei.h>
  26979. +#include <linux/path.h>
  26980. +
  26981. +enum {
  26982. + Opt_br,
  26983. + Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
  26984. + Opt_idel, Opt_imod,
  26985. + Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
  26986. + Opt_xino, Opt_noxino,
  26987. + Opt_trunc_xino, Opt_trunc_xino_v,
  26988. + Opt_trunc_xino_path, Opt_itrunc_xino,
  26989. + Opt_trunc_xib,
  26990. + Opt_shwh,
  26991. + Opt_plink, Opt_list_plink,
  26992. + Opt_udba,
  26993. + Opt_dio,
  26994. + Opt_diropq, Opt_diropq_a, Opt_diropq_w,
  26995. + Opt_warn_perm,
  26996. + Opt_wbr_copyup, Opt_wbr_create,
  26997. + Opt_fhsm_sec,
  26998. + Opt_verbose, Opt_noverbose,
  26999. + Opt_sum, Opt_wsum,
  27000. + Opt_dirperm1,
  27001. + Opt_dirren,
  27002. + Opt_acl,
  27003. + Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
  27004. +};
  27005. +
  27006. +/* ---------------------------------------------------------------------- */
  27007. +
  27008. +/* mount flags */
  27009. +#define AuOpt_XINO 1 /* external inode number bitmap
  27010. + and translation table */
  27011. +#define AuOpt_TRUNC_XINO (1 << 1) /* truncate xino files */
  27012. +#define AuOpt_UDBA_NONE (1 << 2) /* users direct branch access */
  27013. +#define AuOpt_UDBA_REVAL (1 << 3)
  27014. +#define AuOpt_UDBA_HNOTIFY (1 << 4)
  27015. +#define AuOpt_SHWH (1 << 5) /* show whiteout */
  27016. +#define AuOpt_PLINK (1 << 6) /* pseudo-link */
  27017. +#define AuOpt_DIRPERM1 (1 << 7) /* ignore the lower dir's perm
  27018. + bits */
  27019. +#define AuOpt_ALWAYS_DIROPQ (1 << 9) /* policy to creating diropq */
  27020. +#define AuOpt_SUM (1 << 10) /* summation for statfs(2) */
  27021. +#define AuOpt_SUM_W (1 << 11) /* unimplemented */
  27022. +#define AuOpt_WARN_PERM (1 << 12) /* warn when add-branch */
  27023. +#define AuOpt_VERBOSE (1 << 13) /* print the cause of error */
  27024. +#define AuOpt_DIO (1 << 14) /* direct io */
  27025. +#define AuOpt_DIRREN (1 << 15) /* directory rename */
  27026. +
  27027. +#ifndef CONFIG_AUFS_HNOTIFY
  27028. +#undef AuOpt_UDBA_HNOTIFY
  27029. +#define AuOpt_UDBA_HNOTIFY 0
  27030. +#endif
  27031. +#ifndef CONFIG_AUFS_DIRREN
  27032. +#undef AuOpt_DIRREN
  27033. +#define AuOpt_DIRREN 0
  27034. +#endif
  27035. +#ifndef CONFIG_AUFS_SHWH
  27036. +#undef AuOpt_SHWH
  27037. +#define AuOpt_SHWH 0
  27038. +#endif
  27039. +
  27040. +#define AuOpt_Def (AuOpt_XINO \
  27041. + | AuOpt_UDBA_REVAL \
  27042. + | AuOpt_PLINK \
  27043. + /* | AuOpt_DIRPERM1 */ \
  27044. + | AuOpt_WARN_PERM)
  27045. +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
  27046. + | AuOpt_UDBA_REVAL \
  27047. + | AuOpt_UDBA_HNOTIFY)
  27048. +
  27049. +#define AuOpt_LkupDirFlags (LOOKUP_FOLLOW | LOOKUP_DIRECTORY)
  27050. +
  27051. +#define au_opt_test(flags, name) (flags & AuOpt_##name)
  27052. +#define au_opt_set(flags, name) do { \
  27053. + BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
  27054. + ((flags) |= AuOpt_##name); \
  27055. +} while (0)
  27056. +#define au_opt_set_udba(flags, name) do { \
  27057. + (flags) &= ~AuOptMask_UDBA; \
  27058. + ((flags) |= AuOpt_##name); \
  27059. +} while (0)
  27060. +#define au_opt_clr(flags, name) do { \
  27061. + ((flags) &= ~AuOpt_##name); \
  27062. +} while (0)
  27063. +
  27064. +static inline unsigned int au_opts_plink(unsigned int mntflags)
  27065. +{
  27066. +#ifdef CONFIG_PROC_FS
  27067. + return mntflags;
  27068. +#else
  27069. + return mntflags & ~AuOpt_PLINK;
  27070. +#endif
  27071. +}
  27072. +
  27073. +/* ---------------------------------------------------------------------- */
  27074. +
  27075. +/* policies to select one among multiple writable branches */
  27076. +enum {
  27077. + AuWbrCreate_TDP, /* top down parent */
  27078. + AuWbrCreate_RR, /* round robin */
  27079. + AuWbrCreate_MFS, /* most free space */
  27080. + AuWbrCreate_MFSV, /* mfs with seconds */
  27081. + AuWbrCreate_MFSRR, /* mfs then rr */
  27082. + AuWbrCreate_MFSRRV, /* mfs then rr with seconds */
  27083. + AuWbrCreate_TDMFS, /* top down regardless parent and mfs */
  27084. + AuWbrCreate_TDMFSV, /* top down regardless parent and mfs */
  27085. + AuWbrCreate_PMFS, /* parent and mfs */
  27086. + AuWbrCreate_PMFSV, /* parent and mfs with seconds */
  27087. + AuWbrCreate_PMFSRR, /* parent, mfs and round-robin */
  27088. + AuWbrCreate_PMFSRRV, /* plus seconds */
  27089. +
  27090. + AuWbrCreate_Def = AuWbrCreate_TDP
  27091. +};
  27092. +
  27093. +enum {
  27094. + AuWbrCopyup_TDP, /* top down parent */
  27095. + AuWbrCopyup_BUP, /* bottom up parent */
  27096. + AuWbrCopyup_BU, /* bottom up */
  27097. +
  27098. + AuWbrCopyup_Def = AuWbrCopyup_TDP
  27099. +};
  27100. +
  27101. +/* ---------------------------------------------------------------------- */
  27102. +
  27103. +struct file;
  27104. +
  27105. +struct au_opt_add {
  27106. + aufs_bindex_t bindex;
  27107. + char *pathname;
  27108. + int perm;
  27109. + struct path path;
  27110. +};
  27111. +
  27112. +struct au_opt_del {
  27113. + char *pathname;
  27114. + struct path h_path;
  27115. +};
  27116. +
  27117. +struct au_opt_mod {
  27118. + char *path;
  27119. + int perm;
  27120. + struct dentry *h_root;
  27121. +};
  27122. +
  27123. +struct au_opt_xino {
  27124. + char *path;
  27125. + struct file *file;
  27126. +};
  27127. +
  27128. +struct au_opt_xino_itrunc {
  27129. + aufs_bindex_t bindex;
  27130. +};
  27131. +
  27132. +struct au_opt_wbr_create {
  27133. + int wbr_create;
  27134. + int mfs_second;
  27135. + unsigned long long mfsrr_watermark;
  27136. +};
  27137. +
  27138. +struct au_opt {
  27139. + int type;
  27140. + union {
  27141. + struct au_opt_xino xino;
  27142. + struct au_opt_xino_itrunc xino_itrunc;
  27143. + struct au_opt_add add;
  27144. + struct au_opt_del del;
  27145. + struct au_opt_mod mod;
  27146. + int dirwh;
  27147. + int rdcache;
  27148. + unsigned int rdblk;
  27149. + unsigned int rdhash;
  27150. + int udba;
  27151. + struct au_opt_wbr_create wbr_create;
  27152. + int wbr_copyup;
  27153. + unsigned int fhsm_second;
  27154. + bool tf; /* generic flag, true or false */
  27155. + };
  27156. +};
  27157. +
  27158. +/* opts flags */
  27159. +#define AuOpts_REMOUNT 1
  27160. +#define AuOpts_REFRESH (1 << 1)
  27161. +#define AuOpts_TRUNC_XIB (1 << 2)
  27162. +#define AuOpts_REFRESH_DYAOP (1 << 3)
  27163. +#define AuOpts_REFRESH_IDOP (1 << 4)
  27164. +#define AuOpts_DR_FLUSHED (1 << 5)
  27165. +#define au_ftest_opts(flags, name) ((flags) & AuOpts_##name)
  27166. +#define au_fset_opts(flags, name) \
  27167. + do { (flags) |= AuOpts_##name; } while (0)
  27168. +#define au_fclr_opts(flags, name) \
  27169. + do { (flags) &= ~AuOpts_##name; } while (0)
  27170. +
  27171. +#ifndef CONFIG_AUFS_DIRREN
  27172. +#undef AuOpts_DR_FLUSHED
  27173. +#define AuOpts_DR_FLUSHED 0
  27174. +#endif
  27175. +
  27176. +struct au_opts {
  27177. + struct au_opt *opt;
  27178. + int max_opt;
  27179. +
  27180. + unsigned int given_udba;
  27181. + unsigned int flags;
  27182. + unsigned long sb_flags;
  27183. +};
  27184. +
  27185. +/* ---------------------------------------------------------------------- */
  27186. +
  27187. +/* opts.c */
  27188. +int au_br_perm_val(char *perm);
  27189. +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
  27190. +int au_udba_val(char *str);
  27191. +const char *au_optstr_udba(int udba);
  27192. +int au_wbr_create_val(char *str, struct au_opt_wbr_create *create);
  27193. +const char *au_optstr_wbr_create(int wbr_create);
  27194. +int au_wbr_copyup_val(char *str);
  27195. +const char *au_optstr_wbr_copyup(int wbr_copyup);
  27196. +
  27197. +int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
  27198. + aufs_bindex_t bindex);
  27199. +struct super_block;
  27200. +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
  27201. + unsigned int pending);
  27202. +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
  27203. +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
  27204. +
  27205. +unsigned int au_opt_udba(struct super_block *sb);
  27206. +
  27207. +/* fsctx.c */
  27208. +int aufs_fsctx_init(struct fs_context *fc);
  27209. +extern const struct fs_parameter_spec aufs_fsctx_paramspec[];
  27210. +
  27211. +#endif /* __KERNEL__ */
  27212. +#endif /* __AUFS_OPTS_H__ */
  27213. diff -Naur null/fs/aufs/plink.c linux-5.15.131/fs/aufs/plink.c
  27214. --- /dev/null
  27215. +++ linux-5.15.131/fs/aufs/plink.c 2023-09-11 14:32:48.151722514 +0300
  27216. @@ -0,0 +1,516 @@
  27217. +// SPDX-License-Identifier: GPL-2.0
  27218. +/*
  27219. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  27220. + *
  27221. + * This program, aufs is free software; you can redistribute it and/or modify
  27222. + * it under the terms of the GNU General Public License as published by
  27223. + * the Free Software Foundation; either version 2 of the License, or
  27224. + * (at your option) any later version.
  27225. + *
  27226. + * This program is distributed in the hope that it will be useful,
  27227. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27228. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27229. + * GNU General Public License for more details.
  27230. + *
  27231. + * You should have received a copy of the GNU General Public License
  27232. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27233. + */
  27234. +
  27235. +/*
  27236. + * pseudo-link
  27237. + */
  27238. +
  27239. +#include "aufs.h"
  27240. +
  27241. +/*
  27242. + * the pseudo-link maintenance mode.
  27243. + * during a user process maintains the pseudo-links,
  27244. + * prohibit adding a new plink and branch manipulation.
  27245. + *
  27246. + * Flags
  27247. + * NOPLM:
  27248. + * For entry functions which will handle plink, and i_mutex is already held
  27249. + * in VFS.
  27250. + * They cannot wait and should return an error at once.
  27251. + * Callers has to check the error.
  27252. + * NOPLMW:
  27253. + * For entry functions which will handle plink, but i_mutex is not held
  27254. + * in VFS.
  27255. + * They can wait the plink maintenance mode to finish.
  27256. + *
  27257. + * They behave like F_SETLK and F_SETLKW.
  27258. + * If the caller never handle plink, then both flags are unnecessary.
  27259. + */
  27260. +
  27261. +int au_plink_maint(struct super_block *sb, int flags)
  27262. +{
  27263. + int err;
  27264. + pid_t pid, ppid;
  27265. + struct task_struct *parent, *prev;
  27266. + struct au_sbinfo *sbi;
  27267. +
  27268. + SiMustAnyLock(sb);
  27269. +
  27270. + err = 0;
  27271. + if (!au_opt_test(au_mntflags(sb), PLINK))
  27272. + goto out;
  27273. +
  27274. + sbi = au_sbi(sb);
  27275. + pid = sbi->si_plink_maint_pid;
  27276. + if (!pid || pid == current->pid)
  27277. + goto out;
  27278. +
  27279. + /* todo: it highly depends upon /sbin/mount.aufs */
  27280. + prev = NULL;
  27281. + parent = current;
  27282. + ppid = 0;
  27283. + rcu_read_lock();
  27284. + while (1) {
  27285. + parent = rcu_dereference(parent->real_parent);
  27286. + if (parent == prev)
  27287. + break;
  27288. + ppid = task_pid_vnr(parent);
  27289. + if (pid == ppid) {
  27290. + rcu_read_unlock();
  27291. + goto out;
  27292. + }
  27293. + prev = parent;
  27294. + }
  27295. + rcu_read_unlock();
  27296. +
  27297. + if (au_ftest_lock(flags, NOPLMW)) {
  27298. + /* if there is no i_mutex lock in VFS, we don't need to wait */
  27299. + /* AuDebugOn(!lockdep_depth(current)); */
  27300. + while (sbi->si_plink_maint_pid) {
  27301. + si_read_unlock(sb);
  27302. + /* gave up wake_up_bit() */
  27303. + wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
  27304. +
  27305. + if (au_ftest_lock(flags, FLUSH))
  27306. + au_nwt_flush(&sbi->si_nowait);
  27307. + si_noflush_read_lock(sb);
  27308. + }
  27309. + } else if (au_ftest_lock(flags, NOPLM)) {
  27310. + AuDbg("ppid %d, pid %d\n", ppid, pid);
  27311. + err = -EAGAIN;
  27312. + }
  27313. +
  27314. +out:
  27315. + return err;
  27316. +}
  27317. +
  27318. +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
  27319. +{
  27320. + spin_lock(&sbinfo->si_plink_maint_lock);
  27321. + sbinfo->si_plink_maint_pid = 0;
  27322. + spin_unlock(&sbinfo->si_plink_maint_lock);
  27323. + wake_up_all(&sbinfo->si_plink_wq);
  27324. +}
  27325. +
  27326. +int au_plink_maint_enter(struct super_block *sb)
  27327. +{
  27328. + int err;
  27329. + struct au_sbinfo *sbinfo;
  27330. +
  27331. + err = 0;
  27332. + sbinfo = au_sbi(sb);
  27333. + /* make sure i am the only one in this fs */
  27334. + si_write_lock(sb, AuLock_FLUSH);
  27335. + if (au_opt_test(au_mntflags(sb), PLINK)) {
  27336. + spin_lock(&sbinfo->si_plink_maint_lock);
  27337. + if (!sbinfo->si_plink_maint_pid)
  27338. + sbinfo->si_plink_maint_pid = current->pid;
  27339. + else
  27340. + err = -EBUSY;
  27341. + spin_unlock(&sbinfo->si_plink_maint_lock);
  27342. + }
  27343. + si_write_unlock(sb);
  27344. +
  27345. + return err;
  27346. +}
  27347. +
  27348. +/* ---------------------------------------------------------------------- */
  27349. +
  27350. +#ifdef CONFIG_AUFS_DEBUG
  27351. +void au_plink_list(struct super_block *sb)
  27352. +{
  27353. + int i;
  27354. + struct au_sbinfo *sbinfo;
  27355. + struct hlist_bl_head *hbl;
  27356. + struct hlist_bl_node *pos;
  27357. + struct au_icntnr *icntnr;
  27358. +
  27359. + SiMustAnyLock(sb);
  27360. +
  27361. + sbinfo = au_sbi(sb);
  27362. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  27363. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  27364. +
  27365. + for (i = 0; i < AuPlink_NHASH; i++) {
  27366. + hbl = sbinfo->si_plink + i;
  27367. + hlist_bl_lock(hbl);
  27368. + hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
  27369. + AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
  27370. + hlist_bl_unlock(hbl);
  27371. + }
  27372. +}
  27373. +#endif
  27374. +
  27375. +/* is the inode pseudo-linked? */
  27376. +int au_plink_test(struct inode *inode)
  27377. +{
  27378. + int found, i;
  27379. + struct au_sbinfo *sbinfo;
  27380. + struct hlist_bl_head *hbl;
  27381. + struct hlist_bl_node *pos;
  27382. + struct au_icntnr *icntnr;
  27383. +
  27384. + sbinfo = au_sbi(inode->i_sb);
  27385. + AuRwMustAnyLock(&sbinfo->si_rwsem);
  27386. + AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
  27387. + AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
  27388. +
  27389. + found = 0;
  27390. + i = au_plink_hash(inode->i_ino);
  27391. + hbl = sbinfo->si_plink + i;
  27392. + hlist_bl_lock(hbl);
  27393. + hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
  27394. + if (&icntnr->vfs_inode == inode) {
  27395. + found = 1;
  27396. + break;
  27397. + }
  27398. + hlist_bl_unlock(hbl);
  27399. + return found;
  27400. +}
  27401. +
  27402. +/* ---------------------------------------------------------------------- */
  27403. +
  27404. +/*
  27405. + * generate a name for plink.
  27406. + * the file will be stored under AUFS_WH_PLINKDIR.
  27407. + */
  27408. +/* 20 is max digits length of ulong 64 */
  27409. +#define PLINK_NAME_LEN ((20 + 1) * 2)
  27410. +
  27411. +static int plink_name(char *name, int len, struct inode *inode,
  27412. + aufs_bindex_t bindex)
  27413. +{
  27414. + int rlen;
  27415. + struct inode *h_inode;
  27416. +
  27417. + h_inode = au_h_iptr(inode, bindex);
  27418. + rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
  27419. + return rlen;
  27420. +}
  27421. +
  27422. +struct au_do_plink_lkup_args {
  27423. + struct dentry **errp;
  27424. + struct qstr *tgtname;
  27425. + struct path *h_ppath;
  27426. +};
  27427. +
  27428. +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
  27429. + struct path *h_ppath)
  27430. +{
  27431. + struct dentry *h_dentry;
  27432. + struct inode *h_inode;
  27433. +
  27434. + h_inode = d_inode(h_ppath->dentry);
  27435. + inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
  27436. + h_dentry = vfsub_lkup_one(tgtname, h_ppath);
  27437. + inode_unlock_shared(h_inode);
  27438. +
  27439. + return h_dentry;
  27440. +}
  27441. +
  27442. +static void au_call_do_plink_lkup(void *args)
  27443. +{
  27444. + struct au_do_plink_lkup_args *a = args;
  27445. + *a->errp = au_do_plink_lkup(a->tgtname, a->h_ppath);
  27446. +}
  27447. +
  27448. +/* lookup the plink-ed @inode under the branch at @bindex */
  27449. +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
  27450. +{
  27451. + struct dentry *h_dentry;
  27452. + struct au_branch *br;
  27453. + struct path h_ppath;
  27454. + int wkq_err;
  27455. + char a[PLINK_NAME_LEN];
  27456. + struct qstr tgtname = QSTR_INIT(a, 0);
  27457. +
  27458. + AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
  27459. +
  27460. + br = au_sbr(inode->i_sb, bindex);
  27461. + h_ppath.dentry = br->br_wbr->wbr_plink;
  27462. + h_ppath.mnt = au_br_mnt(br);
  27463. + tgtname.len = plink_name(a, sizeof(a), inode, bindex);
  27464. +
  27465. + if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
  27466. + struct au_do_plink_lkup_args args = {
  27467. + .errp = &h_dentry,
  27468. + .tgtname = &tgtname,
  27469. + .h_ppath = &h_ppath
  27470. + };
  27471. +
  27472. + wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
  27473. + if (unlikely(wkq_err))
  27474. + h_dentry = ERR_PTR(wkq_err);
  27475. + } else
  27476. + h_dentry = au_do_plink_lkup(&tgtname, &h_ppath);
  27477. +
  27478. + return h_dentry;
  27479. +}
  27480. +
  27481. +/* create a pseudo-link */
  27482. +static int do_whplink(struct qstr *tgt, struct path *h_ppath,
  27483. + struct dentry *h_dentry)
  27484. +{
  27485. + int err;
  27486. + struct path h_path;
  27487. + struct inode *h_dir, *delegated;
  27488. +
  27489. + h_dir = d_inode(h_ppath->dentry);
  27490. + inode_lock_nested(h_dir, AuLsc_I_CHILD2);
  27491. + h_path.mnt = h_ppath->mnt;
  27492. +again:
  27493. + h_path.dentry = vfsub_lkup_one(tgt, h_ppath);
  27494. + err = PTR_ERR(h_path.dentry);
  27495. + if (IS_ERR(h_path.dentry))
  27496. + goto out;
  27497. +
  27498. + err = 0;
  27499. + /* wh.plink dir is not monitored */
  27500. + /* todo: is it really safe? */
  27501. + if (d_is_positive(h_path.dentry)
  27502. + && d_inode(h_path.dentry) != d_inode(h_dentry)) {
  27503. + delegated = NULL;
  27504. + err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
  27505. + if (unlikely(err == -EWOULDBLOCK)) {
  27506. + pr_warn("cannot retry for NFSv4 delegation"
  27507. + " for an internal unlink\n");
  27508. + iput(delegated);
  27509. + }
  27510. + dput(h_path.dentry);
  27511. + h_path.dentry = NULL;
  27512. + if (!err)
  27513. + goto again;
  27514. + }
  27515. + if (!err && d_is_negative(h_path.dentry)) {
  27516. + delegated = NULL;
  27517. + err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
  27518. + if (unlikely(err == -EWOULDBLOCK)) {
  27519. + pr_warn("cannot retry for NFSv4 delegation"
  27520. + " for an internal link\n");
  27521. + iput(delegated);
  27522. + }
  27523. + }
  27524. + dput(h_path.dentry);
  27525. +
  27526. +out:
  27527. + inode_unlock(h_dir);
  27528. + return err;
  27529. +}
  27530. +
  27531. +struct do_whplink_args {
  27532. + int *errp;
  27533. + struct qstr *tgt;
  27534. + struct path *h_ppath;
  27535. + struct dentry *h_dentry;
  27536. +};
  27537. +
  27538. +static void call_do_whplink(void *args)
  27539. +{
  27540. + struct do_whplink_args *a = args;
  27541. + *a->errp = do_whplink(a->tgt, a->h_ppath, a->h_dentry);
  27542. +}
  27543. +
  27544. +static int whplink(struct dentry *h_dentry, struct inode *inode,
  27545. + aufs_bindex_t bindex)
  27546. +{
  27547. + int err, wkq_err;
  27548. + struct au_branch *br;
  27549. + struct au_wbr *wbr;
  27550. + struct path h_ppath;
  27551. + char a[PLINK_NAME_LEN];
  27552. + struct qstr tgtname = QSTR_INIT(a, 0);
  27553. +
  27554. + br = au_sbr(inode->i_sb, bindex);
  27555. + wbr = br->br_wbr;
  27556. + h_ppath.dentry = wbr->wbr_plink;
  27557. + h_ppath.mnt = au_br_mnt(br);
  27558. + tgtname.len = plink_name(a, sizeof(a), inode, bindex);
  27559. +
  27560. + /* always superio. */
  27561. + if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
  27562. + struct do_whplink_args args = {
  27563. + .errp = &err,
  27564. + .tgt = &tgtname,
  27565. + .h_ppath = &h_ppath,
  27566. + .h_dentry = h_dentry
  27567. + };
  27568. + wkq_err = au_wkq_wait(call_do_whplink, &args);
  27569. + if (unlikely(wkq_err))
  27570. + err = wkq_err;
  27571. + } else
  27572. + err = do_whplink(&tgtname, &h_ppath, h_dentry);
  27573. +
  27574. + return err;
  27575. +}
  27576. +
  27577. +/*
  27578. + * create a new pseudo-link for @h_dentry on @bindex.
  27579. + * the linked inode is held in aufs @inode.
  27580. + */
  27581. +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
  27582. + struct dentry *h_dentry)
  27583. +{
  27584. + struct super_block *sb;
  27585. + struct au_sbinfo *sbinfo;
  27586. + struct hlist_bl_head *hbl;
  27587. + struct hlist_bl_node *pos;
  27588. + struct au_icntnr *icntnr;
  27589. + int found, err, cnt, i;
  27590. +
  27591. + sb = inode->i_sb;
  27592. + sbinfo = au_sbi(sb);
  27593. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  27594. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  27595. +
  27596. + found = au_plink_test(inode);
  27597. + if (found)
  27598. + return;
  27599. +
  27600. + i = au_plink_hash(inode->i_ino);
  27601. + hbl = sbinfo->si_plink + i;
  27602. + au_igrab(inode);
  27603. +
  27604. + hlist_bl_lock(hbl);
  27605. + hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
  27606. + if (&icntnr->vfs_inode == inode) {
  27607. + found = 1;
  27608. + break;
  27609. + }
  27610. + }
  27611. + if (!found) {
  27612. + icntnr = container_of(inode, struct au_icntnr, vfs_inode);
  27613. + hlist_bl_add_head(&icntnr->plink, hbl);
  27614. + }
  27615. + hlist_bl_unlock(hbl);
  27616. + if (!found) {
  27617. + cnt = au_hbl_count(hbl);
  27618. +#define msg "unexpectedly unbalanced or too many pseudo-links"
  27619. + if (cnt > AUFS_PLINK_WARN)
  27620. + AuWarn1(msg ", %d\n", cnt);
  27621. +#undef msg
  27622. + err = whplink(h_dentry, inode, bindex);
  27623. + if (unlikely(err)) {
  27624. + pr_warn("err %d, damaged pseudo link.\n", err);
  27625. + au_hbl_del(&icntnr->plink, hbl);
  27626. + iput(&icntnr->vfs_inode);
  27627. + }
  27628. + } else
  27629. + iput(&icntnr->vfs_inode);
  27630. +}
  27631. +
  27632. +/* free all plinks */
  27633. +void au_plink_put(struct super_block *sb, int verbose)
  27634. +{
  27635. + int i, warned;
  27636. + struct au_sbinfo *sbinfo;
  27637. + struct hlist_bl_head *hbl;
  27638. + struct hlist_bl_node *pos, *tmp;
  27639. + struct au_icntnr *icntnr;
  27640. +
  27641. + SiMustWriteLock(sb);
  27642. +
  27643. + sbinfo = au_sbi(sb);
  27644. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  27645. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  27646. +
  27647. + /* no spin_lock since sbinfo is write-locked */
  27648. + warned = 0;
  27649. + for (i = 0; i < AuPlink_NHASH; i++) {
  27650. + hbl = sbinfo->si_plink + i;
  27651. + if (!warned && verbose && !hlist_bl_empty(hbl)) {
  27652. + pr_warn("pseudo-link is not flushed");
  27653. + warned = 1;
  27654. + }
  27655. + hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
  27656. + iput(&icntnr->vfs_inode);
  27657. + INIT_HLIST_BL_HEAD(hbl);
  27658. + }
  27659. +}
  27660. +
  27661. +void au_plink_clean(struct super_block *sb, int verbose)
  27662. +{
  27663. + struct dentry *root;
  27664. +
  27665. + root = sb->s_root;
  27666. + aufs_write_lock(root);
  27667. + if (au_opt_test(au_mntflags(sb), PLINK))
  27668. + au_plink_put(sb, verbose);
  27669. + aufs_write_unlock(root);
  27670. +}
  27671. +
  27672. +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
  27673. +{
  27674. + int do_put;
  27675. + aufs_bindex_t btop, bbot, bindex;
  27676. +
  27677. + do_put = 0;
  27678. + btop = au_ibtop(inode);
  27679. + bbot = au_ibbot(inode);
  27680. + if (btop >= 0) {
  27681. + for (bindex = btop; bindex <= bbot; bindex++) {
  27682. + if (!au_h_iptr(inode, bindex)
  27683. + || au_ii_br_id(inode, bindex) != br_id)
  27684. + continue;
  27685. + au_set_h_iptr(inode, bindex, NULL, 0);
  27686. + do_put = 1;
  27687. + break;
  27688. + }
  27689. + if (do_put)
  27690. + for (bindex = btop; bindex <= bbot; bindex++)
  27691. + if (au_h_iptr(inode, bindex)) {
  27692. + do_put = 0;
  27693. + break;
  27694. + }
  27695. + } else
  27696. + do_put = 1;
  27697. +
  27698. + return do_put;
  27699. +}
  27700. +
  27701. +/* free the plinks on a branch specified by @br_id */
  27702. +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
  27703. +{
  27704. + struct au_sbinfo *sbinfo;
  27705. + struct hlist_bl_head *hbl;
  27706. + struct hlist_bl_node *pos, *tmp;
  27707. + struct au_icntnr *icntnr;
  27708. + struct inode *inode;
  27709. + int i, do_put;
  27710. +
  27711. + SiMustWriteLock(sb);
  27712. +
  27713. + sbinfo = au_sbi(sb);
  27714. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  27715. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  27716. +
  27717. + /* no bit_lock since sbinfo is write-locked */
  27718. + for (i = 0; i < AuPlink_NHASH; i++) {
  27719. + hbl = sbinfo->si_plink + i;
  27720. + hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
  27721. + inode = au_igrab(&icntnr->vfs_inode);
  27722. + ii_write_lock_child(inode);
  27723. + do_put = au_plink_do_half_refresh(inode, br_id);
  27724. + if (do_put) {
  27725. + hlist_bl_del(&icntnr->plink);
  27726. + iput(inode);
  27727. + }
  27728. + ii_write_unlock(inode);
  27729. + iput(inode);
  27730. + }
  27731. + }
  27732. +}
  27733. diff -Naur null/fs/aufs/poll.c linux-5.15.131/fs/aufs/poll.c
  27734. --- /dev/null
  27735. +++ linux-5.15.131/fs/aufs/poll.c 2023-09-11 14:32:48.151722514 +0300
  27736. @@ -0,0 +1,51 @@
  27737. +// SPDX-License-Identifier: GPL-2.0
  27738. +/*
  27739. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  27740. + *
  27741. + * This program, aufs is free software; you can redistribute it and/or modify
  27742. + * it under the terms of the GNU General Public License as published by
  27743. + * the Free Software Foundation; either version 2 of the License, or
  27744. + * (at your option) any later version.
  27745. + *
  27746. + * This program is distributed in the hope that it will be useful,
  27747. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27748. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27749. + * GNU General Public License for more details.
  27750. + *
  27751. + * You should have received a copy of the GNU General Public License
  27752. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27753. + */
  27754. +
  27755. +/*
  27756. + * poll operation
  27757. + * There is only one filesystem which implements ->poll operation, currently.
  27758. + */
  27759. +
  27760. +#include "aufs.h"
  27761. +
  27762. +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
  27763. +{
  27764. + __poll_t mask;
  27765. + struct file *h_file;
  27766. + struct super_block *sb;
  27767. +
  27768. + /* We should pretend an error happened. */
  27769. + mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
  27770. + sb = file->f_path.dentry->d_sb;
  27771. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  27772. +
  27773. + h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
  27774. + if (IS_ERR(h_file)) {
  27775. + AuDbg("h_file %ld\n", PTR_ERR(h_file));
  27776. + goto out;
  27777. + }
  27778. +
  27779. + mask = vfs_poll(h_file, pt);
  27780. + fput(h_file); /* instead of au_read_post() */
  27781. +
  27782. +out:
  27783. + si_read_unlock(sb);
  27784. + if (mask & EPOLLERR)
  27785. + AuDbg("mask 0x%x\n", mask);
  27786. + return mask;
  27787. +}
  27788. diff -Naur null/fs/aufs/posix_acl.c linux-5.15.131/fs/aufs/posix_acl.c
  27789. --- /dev/null
  27790. +++ linux-5.15.131/fs/aufs/posix_acl.c 2023-09-11 14:32:48.151722514 +0300
  27791. @@ -0,0 +1,111 @@
  27792. +// SPDX-License-Identifier: GPL-2.0
  27793. +/*
  27794. + * Copyright (C) 2014-2021 Junjiro R. Okajima
  27795. + *
  27796. + * This program, aufs is free software; you can redistribute it and/or modify
  27797. + * it under the terms of the GNU General Public License as published by
  27798. + * the Free Software Foundation; either version 2 of the License, or
  27799. + * (at your option) any later version.
  27800. + *
  27801. + * This program is distributed in the hope that it will be useful,
  27802. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27803. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27804. + * GNU General Public License for more details.
  27805. + *
  27806. + * You should have received a copy of the GNU General Public License
  27807. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27808. + */
  27809. +
  27810. +/*
  27811. + * posix acl operations
  27812. + */
  27813. +
  27814. +#include <linux/fs.h>
  27815. +#include "aufs.h"
  27816. +
  27817. +struct posix_acl *aufs_get_acl(struct inode *inode, int type, bool rcu)
  27818. +{
  27819. + struct posix_acl *acl;
  27820. + int err;
  27821. + aufs_bindex_t bindex;
  27822. + struct inode *h_inode;
  27823. + struct super_block *sb;
  27824. +
  27825. + acl = ERR_PTR(-ECHILD);
  27826. + if (rcu)
  27827. + goto out;
  27828. +
  27829. + acl = NULL;
  27830. + sb = inode->i_sb;
  27831. + si_read_lock(sb, AuLock_FLUSH);
  27832. + ii_read_lock_child(inode);
  27833. + if (!(sb->s_flags & SB_POSIXACL))
  27834. + goto unlock;
  27835. +
  27836. + bindex = au_ibtop(inode);
  27837. + h_inode = au_h_iptr(inode, bindex);
  27838. + if (unlikely(!h_inode
  27839. + || ((h_inode->i_mode & S_IFMT)
  27840. + != (inode->i_mode & S_IFMT)))) {
  27841. + err = au_busy_or_stale();
  27842. + acl = ERR_PTR(err);
  27843. + goto unlock;
  27844. + }
  27845. +
  27846. + /* always topmost only */
  27847. + acl = get_acl(h_inode, type);
  27848. + if (IS_ERR(acl))
  27849. + forget_cached_acl(inode, type);
  27850. + else
  27851. + set_cached_acl(inode, type, acl);
  27852. +
  27853. +unlock:
  27854. + ii_read_unlock(inode);
  27855. + si_read_unlock(sb);
  27856. +
  27857. +out:
  27858. + AuTraceErrPtr(acl);
  27859. + return acl;
  27860. +}
  27861. +
  27862. +int aufs_set_acl(struct user_namespace *userns, struct inode *inode,
  27863. + struct posix_acl *acl, int type)
  27864. +{
  27865. + int err;
  27866. + ssize_t ssz;
  27867. + struct dentry *dentry;
  27868. + struct au_sxattr arg = {
  27869. + .type = AU_ACL_SET,
  27870. + .u.acl_set = {
  27871. + .acl = acl,
  27872. + .type = type
  27873. + },
  27874. + };
  27875. +
  27876. + IMustLock(inode);
  27877. +
  27878. + if (inode->i_ino == AUFS_ROOT_INO)
  27879. + dentry = dget(inode->i_sb->s_root);
  27880. + else {
  27881. + dentry = d_find_alias(inode);
  27882. + if (!dentry)
  27883. + dentry = d_find_any_alias(inode);
  27884. + if (!dentry) {
  27885. + pr_warn("cannot handle this inode, "
  27886. + "please report to aufs-users ML\n");
  27887. + err = -ENOENT;
  27888. + goto out;
  27889. + }
  27890. + }
  27891. +
  27892. + ssz = au_sxattr(dentry, inode, &arg);
  27893. + /* forget even it if succeeds since the branch might set differently */
  27894. + forget_cached_acl(inode, type);
  27895. + dput(dentry);
  27896. + err = ssz;
  27897. + if (ssz >= 0)
  27898. + err = 0;
  27899. +
  27900. +out:
  27901. + return err;
  27902. +}
  27903. diff -Naur null/fs/aufs/procfs.c linux-5.15.131/fs/aufs/procfs.c
  27904. --- /dev/null
  27905. +++ linux-5.15.131/fs/aufs/procfs.c 2023-09-11 14:32:48.151722514 +0300
  27906. @@ -0,0 +1,170 @@
  27907. +// SPDX-License-Identifier: GPL-2.0
  27908. +/*
  27909. + * Copyright (C) 2010-2021 Junjiro R. Okajima
  27910. + *
  27911. + * This program, aufs is free software; you can redistribute it and/or modify
  27912. + * it under the terms of the GNU General Public License as published by
  27913. + * the Free Software Foundation; either version 2 of the License, or
  27914. + * (at your option) any later version.
  27915. + *
  27916. + * This program is distributed in the hope that it will be useful,
  27917. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27918. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27919. + * GNU General Public License for more details.
  27920. + *
  27921. + * You should have received a copy of the GNU General Public License
  27922. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27923. + */
  27924. +
  27925. +/*
  27926. + * procfs interfaces
  27927. + */
  27928. +
  27929. +#include <linux/proc_fs.h>
  27930. +#include "aufs.h"
  27931. +
  27932. +static int au_procfs_plm_release(struct inode *inode, struct file *file)
  27933. +{
  27934. + struct au_sbinfo *sbinfo;
  27935. +
  27936. + sbinfo = file->private_data;
  27937. + if (sbinfo) {
  27938. + au_plink_maint_leave(sbinfo);
  27939. + kobject_put(&sbinfo->si_kobj);
  27940. + }
  27941. +
  27942. + return 0;
  27943. +}
  27944. +
  27945. +static void au_procfs_plm_write_clean(struct file *file)
  27946. +{
  27947. + struct au_sbinfo *sbinfo;
  27948. +
  27949. + sbinfo = file->private_data;
  27950. + if (sbinfo)
  27951. + au_plink_clean(sbinfo->si_sb, /*verbose*/0);
  27952. +}
  27953. +
  27954. +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
  27955. +{
  27956. + int err;
  27957. + struct super_block *sb;
  27958. + struct au_sbinfo *sbinfo;
  27959. + struct hlist_bl_node *pos;
  27960. +
  27961. + err = -EBUSY;
  27962. + if (unlikely(file->private_data))
  27963. + goto out;
  27964. +
  27965. + sb = NULL;
  27966. + /* don't use au_sbilist_lock() here */
  27967. + hlist_bl_lock(&au_sbilist);
  27968. + hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
  27969. + if (id == sysaufs_si_id(sbinfo)) {
  27970. + if (kobject_get_unless_zero(&sbinfo->si_kobj))
  27971. + sb = sbinfo->si_sb;
  27972. + break;
  27973. + }
  27974. + hlist_bl_unlock(&au_sbilist);
  27975. +
  27976. + err = -EINVAL;
  27977. + if (unlikely(!sb))
  27978. + goto out;
  27979. +
  27980. + err = au_plink_maint_enter(sb);
  27981. + if (!err)
  27982. + /* keep kobject_get() */
  27983. + file->private_data = sbinfo;
  27984. + else
  27985. + kobject_put(&sbinfo->si_kobj);
  27986. +out:
  27987. + return err;
  27988. +}
  27989. +
  27990. +/*
  27991. + * Accept a valid "si=xxxx" only.
  27992. + * Once it is accepted successfully, accept "clean" too.
  27993. + */
  27994. +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
  27995. + size_t count, loff_t *ppos)
  27996. +{
  27997. + ssize_t err;
  27998. + unsigned long id;
  27999. + /* last newline is allowed */
  28000. + char buf[3 + sizeof(unsigned long) * 2 + 1];
  28001. +
  28002. + err = -EACCES;
  28003. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  28004. + goto out;
  28005. +
  28006. + err = -EINVAL;
  28007. + if (unlikely(count > sizeof(buf)))
  28008. + goto out;
  28009. +
  28010. + err = copy_from_user(buf, ubuf, count);
  28011. + if (unlikely(err)) {
  28012. + err = -EFAULT;
  28013. + goto out;
  28014. + }
  28015. + buf[count] = 0;
  28016. +
  28017. + err = -EINVAL;
  28018. + if (!strcmp("clean", buf)) {
  28019. + au_procfs_plm_write_clean(file);
  28020. + goto out_success;
  28021. + } else if (unlikely(strncmp("si=", buf, 3)))
  28022. + goto out;
  28023. +
  28024. + err = kstrtoul(buf + 3, 16, &id);
  28025. + if (unlikely(err))
  28026. + goto out;
  28027. +
  28028. + err = au_procfs_plm_write_si(file, id);
  28029. + if (unlikely(err))
  28030. + goto out;
  28031. +
  28032. +out_success:
  28033. + err = count; /* success */
  28034. +out:
  28035. + return err;
  28036. +}
  28037. +
  28038. +static const struct proc_ops au_procfs_plm_op = {
  28039. + .proc_write = au_procfs_plm_write,
  28040. + .proc_release = au_procfs_plm_release
  28041. +};
  28042. +
  28043. +/* ---------------------------------------------------------------------- */
  28044. +
  28045. +static struct proc_dir_entry *au_procfs_dir;
  28046. +
  28047. +void au_procfs_fin(void)
  28048. +{
  28049. + remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
  28050. + remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
  28051. +}
  28052. +
  28053. +int __init au_procfs_init(void)
  28054. +{
  28055. + int err;
  28056. + struct proc_dir_entry *entry;
  28057. +
  28058. + err = -ENOMEM;
  28059. + au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
  28060. + if (unlikely(!au_procfs_dir))
  28061. + goto out;
  28062. +
  28063. + entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
  28064. + au_procfs_dir, &au_procfs_plm_op);
  28065. + if (unlikely(!entry))
  28066. + goto out_dir;
  28067. +
  28068. + err = 0;
  28069. + goto out; /* success */
  28070. +
  28071. +
  28072. +out_dir:
  28073. + remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
  28074. +out:
  28075. + return err;
  28076. +}
  28077. diff -Naur null/fs/aufs/rdu.c linux-5.15.131/fs/aufs/rdu.c
  28078. --- /dev/null
  28079. +++ linux-5.15.131/fs/aufs/rdu.c 2023-09-11 14:32:48.151722514 +0300
  28080. @@ -0,0 +1,384 @@
  28081. +// SPDX-License-Identifier: GPL-2.0
  28082. +/*
  28083. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  28084. + *
  28085. + * This program, aufs is free software; you can redistribute it and/or modify
  28086. + * it under the terms of the GNU General Public License as published by
  28087. + * the Free Software Foundation; either version 2 of the License, or
  28088. + * (at your option) any later version.
  28089. + *
  28090. + * This program is distributed in the hope that it will be useful,
  28091. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28092. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28093. + * GNU General Public License for more details.
  28094. + *
  28095. + * You should have received a copy of the GNU General Public License
  28096. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28097. + */
  28098. +
  28099. +/*
  28100. + * readdir in userspace.
  28101. + */
  28102. +
  28103. +#include <linux/compat.h>
  28104. +#include <linux/fs_stack.h>
  28105. +#include <linux/security.h>
  28106. +#include "aufs.h"
  28107. +
  28108. +/* bits for struct aufs_rdu.flags */
  28109. +#define AuRdu_CALLED 1
  28110. +#define AuRdu_CONT (1 << 1)
  28111. +#define AuRdu_FULL (1 << 2)
  28112. +#define au_ftest_rdu(flags, name) ((flags) & AuRdu_##name)
  28113. +#define au_fset_rdu(flags, name) \
  28114. + do { (flags) |= AuRdu_##name; } while (0)
  28115. +#define au_fclr_rdu(flags, name) \
  28116. + do { (flags) &= ~AuRdu_##name; } while (0)
  28117. +
  28118. +struct au_rdu_arg {
  28119. + struct dir_context ctx;
  28120. + struct aufs_rdu *rdu;
  28121. + union au_rdu_ent_ul ent;
  28122. + unsigned long end;
  28123. +
  28124. + struct super_block *sb;
  28125. + int err;
  28126. +};
  28127. +
  28128. +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
  28129. + loff_t offset, u64 h_ino, unsigned int d_type)
  28130. +{
  28131. + int err, len;
  28132. + struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
  28133. + struct aufs_rdu *rdu = arg->rdu;
  28134. + struct au_rdu_ent ent;
  28135. +
  28136. + err = 0;
  28137. + arg->err = 0;
  28138. + au_fset_rdu(rdu->cookie.flags, CALLED);
  28139. + len = au_rdu_len(nlen);
  28140. + if (arg->ent.ul + len < arg->end) {
  28141. + ent.ino = h_ino;
  28142. + ent.bindex = rdu->cookie.bindex;
  28143. + ent.type = d_type;
  28144. + ent.nlen = nlen;
  28145. + if (unlikely(nlen > AUFS_MAX_NAMELEN))
  28146. + ent.type = DT_UNKNOWN;
  28147. +
  28148. + /* unnecessary to support mmap_sem since this is a dir */
  28149. + err = -EFAULT;
  28150. + if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
  28151. + goto out;
  28152. + if (copy_to_user(arg->ent.e->name, name, nlen))
  28153. + goto out;
  28154. + /* the terminating NULL */
  28155. + if (__put_user(0, arg->ent.e->name + nlen))
  28156. + goto out;
  28157. + err = 0;
  28158. + /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
  28159. + arg->ent.ul += len;
  28160. + rdu->rent++;
  28161. + } else {
  28162. + err = -EFAULT;
  28163. + au_fset_rdu(rdu->cookie.flags, FULL);
  28164. + rdu->full = 1;
  28165. + rdu->tail = arg->ent;
  28166. + }
  28167. +
  28168. +out:
  28169. + /* AuTraceErr(err); */
  28170. + return err;
  28171. +}
  28172. +
  28173. +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
  28174. +{
  28175. + int err;
  28176. + loff_t offset;
  28177. + struct au_rdu_cookie *cookie = &arg->rdu->cookie;
  28178. +
  28179. + /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
  28180. + offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
  28181. + err = offset;
  28182. + if (unlikely(offset != cookie->h_pos))
  28183. + goto out;
  28184. +
  28185. + err = 0;
  28186. + do {
  28187. + arg->err = 0;
  28188. + au_fclr_rdu(cookie->flags, CALLED);
  28189. + /* smp_mb(); */
  28190. + err = vfsub_iterate_dir(h_file, &arg->ctx);
  28191. + if (err >= 0)
  28192. + err = arg->err;
  28193. + } while (!err
  28194. + && au_ftest_rdu(cookie->flags, CALLED)
  28195. + && !au_ftest_rdu(cookie->flags, FULL));
  28196. + cookie->h_pos = h_file->f_pos;
  28197. +
  28198. +out:
  28199. + AuTraceErr(err);
  28200. + return err;
  28201. +}
  28202. +
  28203. +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
  28204. +{
  28205. + int err;
  28206. + aufs_bindex_t bbot;
  28207. + struct au_rdu_arg arg = {
  28208. + .ctx = {
  28209. + .actor = au_rdu_fill
  28210. + }
  28211. + };
  28212. + struct dentry *dentry;
  28213. + struct inode *inode;
  28214. + struct file *h_file;
  28215. + struct au_rdu_cookie *cookie = &rdu->cookie;
  28216. +
  28217. + /* VERIFY_WRITE */
  28218. + err = !access_ok(rdu->ent.e, rdu->sz);
  28219. + if (unlikely(err)) {
  28220. + err = -EFAULT;
  28221. + AuTraceErr(err);
  28222. + goto out;
  28223. + }
  28224. + rdu->rent = 0;
  28225. + rdu->tail = rdu->ent;
  28226. + rdu->full = 0;
  28227. + arg.rdu = rdu;
  28228. + arg.ent = rdu->ent;
  28229. + arg.end = arg.ent.ul;
  28230. + arg.end += rdu->sz;
  28231. +
  28232. + err = -ENOTDIR;
  28233. + if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
  28234. + goto out;
  28235. +
  28236. + err = security_file_permission(file, MAY_READ);
  28237. + AuTraceErr(err);
  28238. + if (unlikely(err))
  28239. + goto out;
  28240. +
  28241. + dentry = file->f_path.dentry;
  28242. + inode = d_inode(dentry);
  28243. + inode_lock_shared(inode);
  28244. +
  28245. + arg.sb = inode->i_sb;
  28246. + err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
  28247. + if (unlikely(err))
  28248. + goto out_mtx;
  28249. + err = au_alive_dir(dentry);
  28250. + if (unlikely(err))
  28251. + goto out_si;
  28252. + /* todo: reval? */
  28253. + fi_read_lock(file);
  28254. +
  28255. + err = -EAGAIN;
  28256. + if (unlikely(au_ftest_rdu(cookie->flags, CONT)
  28257. + && cookie->generation != au_figen(file)))
  28258. + goto out_unlock;
  28259. +
  28260. + err = 0;
  28261. + if (!rdu->blk) {
  28262. + rdu->blk = au_sbi(arg.sb)->si_rdblk;
  28263. + if (!rdu->blk)
  28264. + rdu->blk = au_dir_size(file, /*dentry*/NULL);
  28265. + }
  28266. + bbot = au_fbtop(file);
  28267. + if (cookie->bindex < bbot)
  28268. + cookie->bindex = bbot;
  28269. + bbot = au_fbbot_dir(file);
  28270. + /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
  28271. + for (; !err && cookie->bindex <= bbot;
  28272. + cookie->bindex++, cookie->h_pos = 0) {
  28273. + h_file = au_hf_dir(file, cookie->bindex);
  28274. + if (!h_file)
  28275. + continue;
  28276. +
  28277. + au_fclr_rdu(cookie->flags, FULL);
  28278. + err = au_rdu_do(h_file, &arg);
  28279. + AuTraceErr(err);
  28280. + if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
  28281. + break;
  28282. + }
  28283. + AuDbg("rent %llu\n", rdu->rent);
  28284. +
  28285. + if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
  28286. + rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
  28287. + au_fset_rdu(cookie->flags, CONT);
  28288. + cookie->generation = au_figen(file);
  28289. + }
  28290. +
  28291. + ii_read_lock_child(inode);
  28292. + fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
  28293. + ii_read_unlock(inode);
  28294. +
  28295. +out_unlock:
  28296. + fi_read_unlock(file);
  28297. +out_si:
  28298. + si_read_unlock(arg.sb);
  28299. +out_mtx:
  28300. + inode_unlock_shared(inode);
  28301. +out:
  28302. + AuTraceErr(err);
  28303. + return err;
  28304. +}
  28305. +
  28306. +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
  28307. +{
  28308. + int err;
  28309. + ino_t ino;
  28310. + unsigned long long nent;
  28311. + union au_rdu_ent_ul *u;
  28312. + struct au_rdu_ent ent;
  28313. + struct super_block *sb;
  28314. +
  28315. + err = 0;
  28316. + nent = rdu->nent;
  28317. + u = &rdu->ent;
  28318. + sb = file->f_path.dentry->d_sb;
  28319. + si_read_lock(sb, AuLock_FLUSH);
  28320. + while (nent-- > 0) {
  28321. + /* unnecessary to support mmap_sem since this is a dir */
  28322. + err = copy_from_user(&ent, u->e, sizeof(ent));
  28323. + if (!err)
  28324. + /* VERIFY_WRITE */
  28325. + err = !access_ok(&u->e->ino, sizeof(ino));
  28326. + if (unlikely(err)) {
  28327. + err = -EFAULT;
  28328. + AuTraceErr(err);
  28329. + break;
  28330. + }
  28331. +
  28332. + /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
  28333. + if (!ent.wh)
  28334. + err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
  28335. + else
  28336. + err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
  28337. + &ino);
  28338. + if (unlikely(err)) {
  28339. + AuTraceErr(err);
  28340. + break;
  28341. + }
  28342. +
  28343. + err = __put_user(ino, &u->e->ino);
  28344. + if (unlikely(err)) {
  28345. + err = -EFAULT;
  28346. + AuTraceErr(err);
  28347. + break;
  28348. + }
  28349. + u->ul += au_rdu_len(ent.nlen);
  28350. + }
  28351. + si_read_unlock(sb);
  28352. +
  28353. + return err;
  28354. +}
  28355. +
  28356. +/* ---------------------------------------------------------------------- */
  28357. +
  28358. +static int au_rdu_verify(struct aufs_rdu *rdu)
  28359. +{
  28360. + AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
  28361. + "%llu, b%d, 0x%x, g%u}\n",
  28362. + rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
  28363. + rdu->blk,
  28364. + rdu->rent, rdu->shwh, rdu->full,
  28365. + rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
  28366. + rdu->cookie.generation);
  28367. +
  28368. + if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
  28369. + return 0;
  28370. +
  28371. + AuDbg("%u:%u\n",
  28372. + rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
  28373. + return -EINVAL;
  28374. +}
  28375. +
  28376. +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  28377. +{
  28378. + long err, e;
  28379. + struct aufs_rdu rdu;
  28380. + void __user *p = (void __user *)arg;
  28381. +
  28382. + err = copy_from_user(&rdu, p, sizeof(rdu));
  28383. + if (unlikely(err)) {
  28384. + err = -EFAULT;
  28385. + AuTraceErr(err);
  28386. + goto out;
  28387. + }
  28388. + err = au_rdu_verify(&rdu);
  28389. + if (unlikely(err))
  28390. + goto out;
  28391. +
  28392. + switch (cmd) {
  28393. + case AUFS_CTL_RDU:
  28394. + err = au_rdu(file, &rdu);
  28395. + if (unlikely(err))
  28396. + break;
  28397. +
  28398. + e = copy_to_user(p, &rdu, sizeof(rdu));
  28399. + if (unlikely(e)) {
  28400. + err = -EFAULT;
  28401. + AuTraceErr(err);
  28402. + }
  28403. + break;
  28404. + case AUFS_CTL_RDU_INO:
  28405. + err = au_rdu_ino(file, &rdu);
  28406. + break;
  28407. +
  28408. + default:
  28409. + /* err = -ENOTTY; */
  28410. + err = -EINVAL;
  28411. + }
  28412. +
  28413. +out:
  28414. + AuTraceErr(err);
  28415. + return err;
  28416. +}
  28417. +
  28418. +#ifdef CONFIG_COMPAT
  28419. +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  28420. +{
  28421. + long err, e;
  28422. + struct aufs_rdu rdu;
  28423. + void __user *p = compat_ptr(arg);
  28424. +
  28425. + /* todo: get_user()? */
  28426. + err = copy_from_user(&rdu, p, sizeof(rdu));
  28427. + if (unlikely(err)) {
  28428. + err = -EFAULT;
  28429. + AuTraceErr(err);
  28430. + goto out;
  28431. + }
  28432. + rdu.ent.e = compat_ptr(rdu.ent.ul);
  28433. + err = au_rdu_verify(&rdu);
  28434. + if (unlikely(err))
  28435. + goto out;
  28436. +
  28437. + switch (cmd) {
  28438. + case AUFS_CTL_RDU:
  28439. + err = au_rdu(file, &rdu);
  28440. + if (unlikely(err))
  28441. + break;
  28442. +
  28443. + rdu.ent.ul = ptr_to_compat(rdu.ent.e);
  28444. + rdu.tail.ul = ptr_to_compat(rdu.tail.e);
  28445. + e = copy_to_user(p, &rdu, sizeof(rdu));
  28446. + if (unlikely(e)) {
  28447. + err = -EFAULT;
  28448. + AuTraceErr(err);
  28449. + }
  28450. + break;
  28451. + case AUFS_CTL_RDU_INO:
  28452. + err = au_rdu_ino(file, &rdu);
  28453. + break;
  28454. +
  28455. + default:
  28456. + /* err = -ENOTTY; */
  28457. + err = -EINVAL;
  28458. + }
  28459. +
  28460. +out:
  28461. + AuTraceErr(err);
  28462. + return err;
  28463. +}
  28464. +#endif
  28465. diff -Naur null/fs/aufs/rwsem.h linux-5.15.131/fs/aufs/rwsem.h
  28466. --- /dev/null
  28467. +++ linux-5.15.131/fs/aufs/rwsem.h 2023-09-11 14:32:48.152722502 +0300
  28468. @@ -0,0 +1,85 @@
  28469. +/* SPDX-License-Identifier: GPL-2.0 */
  28470. +/*
  28471. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  28472. + *
  28473. + * This program, aufs is free software; you can redistribute it and/or modify
  28474. + * it under the terms of the GNU General Public License as published by
  28475. + * the Free Software Foundation; either version 2 of the License, or
  28476. + * (at your option) any later version.
  28477. + *
  28478. + * This program is distributed in the hope that it will be useful,
  28479. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28480. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28481. + * GNU General Public License for more details.
  28482. + *
  28483. + * You should have received a copy of the GNU General Public License
  28484. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28485. + */
  28486. +
  28487. +/*
  28488. + * simple read-write semaphore wrappers
  28489. + */
  28490. +
  28491. +#ifndef __AUFS_RWSEM_H__
  28492. +#define __AUFS_RWSEM_H__
  28493. +
  28494. +#ifdef __KERNEL__
  28495. +
  28496. +#include "debug.h"
  28497. +
  28498. +/* in the future, the name 'au_rwsem' will be totally gone */
  28499. +#define au_rwsem rw_semaphore
  28500. +
  28501. +/* to debug easier, do not make them inlined functions */
  28502. +#define AuRwMustNoWaiters(rw) AuDebugOn(rwsem_is_contended(rw))
  28503. +
  28504. +#ifdef CONFIG_LOCKDEP
  28505. +/* rwsem_is_locked() is unusable */
  28506. +#define AuRwMustReadLock(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
  28507. + && !lockdep_recursing(current) \
  28508. + && debug_locks \
  28509. + && !lockdep_is_held_type(rw, 1))
  28510. +#define AuRwMustWriteLock(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
  28511. + && !lockdep_recursing(current) \
  28512. + && debug_locks \
  28513. + && !lockdep_is_held_type(rw, 0))
  28514. +#define AuRwMustAnyLock(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
  28515. + && !lockdep_recursing(current) \
  28516. + && debug_locks \
  28517. + && !lockdep_is_held(rw))
  28518. +#define AuRwDestroy(rw) AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP) \
  28519. + && !lockdep_recursing(current) \
  28520. + && debug_locks \
  28521. + && lockdep_is_held(rw))
  28522. +#else
  28523. +#define AuRwMustReadLock(rw) do {} while (0)
  28524. +#define AuRwMustWriteLock(rw) do {} while (0)
  28525. +#define AuRwMustAnyLock(rw) do {} while (0)
  28526. +#define AuRwDestroy(rw) do {} while (0)
  28527. +#endif
  28528. +
  28529. +#define au_rw_init(rw) init_rwsem(rw)
  28530. +
  28531. +#define au_rw_init_wlock(rw) do { \
  28532. + au_rw_init(rw); \
  28533. + down_write(rw); \
  28534. + } while (0)
  28535. +
  28536. +#define au_rw_init_wlock_nested(rw, lsc) do { \
  28537. + au_rw_init(rw); \
  28538. + down_write_nested(rw, lsc); \
  28539. + } while (0)
  28540. +
  28541. +#define au_rw_read_lock(rw) down_read(rw)
  28542. +#define au_rw_read_lock_nested(rw, lsc) down_read_nested(rw, lsc)
  28543. +#define au_rw_read_unlock(rw) up_read(rw)
  28544. +#define au_rw_dgrade_lock(rw) downgrade_write(rw)
  28545. +#define au_rw_write_lock(rw) down_write(rw)
  28546. +#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
  28547. +#define au_rw_write_unlock(rw) up_write(rw)
  28548. +/* why is not _nested version defined? */
  28549. +#define au_rw_read_trylock(rw) down_read_trylock(rw)
  28550. +#define au_rw_write_trylock(rw) down_write_trylock(rw)
  28551. +
  28552. +#endif /* __KERNEL__ */
  28553. +#endif /* __AUFS_RWSEM_H__ */
  28554. diff -Naur null/fs/aufs/sbinfo.c linux-5.15.131/fs/aufs/sbinfo.c
  28555. --- /dev/null
  28556. +++ linux-5.15.131/fs/aufs/sbinfo.c 2023-09-11 14:32:48.152722502 +0300
  28557. @@ -0,0 +1,316 @@
  28558. +// SPDX-License-Identifier: GPL-2.0
  28559. +/*
  28560. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  28561. + *
  28562. + * This program, aufs is free software; you can redistribute it and/or modify
  28563. + * it under the terms of the GNU General Public License as published by
  28564. + * the Free Software Foundation; either version 2 of the License, or
  28565. + * (at your option) any later version.
  28566. + *
  28567. + * This program is distributed in the hope that it will be useful,
  28568. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28569. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28570. + * GNU General Public License for more details.
  28571. + *
  28572. + * You should have received a copy of the GNU General Public License
  28573. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28574. + */
  28575. +
  28576. +/*
  28577. + * superblock private data
  28578. + */
  28579. +
  28580. +#include <linux/iversion.h>
  28581. +#include "aufs.h"
  28582. +
  28583. +/*
  28584. + * they are necessary regardless sysfs is disabled.
  28585. + */
  28586. +void au_si_free(struct kobject *kobj)
  28587. +{
  28588. + int i;
  28589. + struct au_sbinfo *sbinfo;
  28590. + char *locked __maybe_unused; /* debug only */
  28591. +
  28592. + sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
  28593. + for (i = 0; i < AuPlink_NHASH; i++)
  28594. + AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
  28595. + AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
  28596. +
  28597. + AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
  28598. + au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
  28599. + AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
  28600. + au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
  28601. +
  28602. + dbgaufs_si_fin(sbinfo);
  28603. + au_rw_write_lock(&sbinfo->si_rwsem);
  28604. + au_br_free(sbinfo);
  28605. + au_rw_write_unlock(&sbinfo->si_rwsem);
  28606. +
  28607. + au_kfree_try_rcu(sbinfo->si_branch);
  28608. + mutex_destroy(&sbinfo->si_xib_mtx);
  28609. + AuRwDestroy(&sbinfo->si_rwsem);
  28610. +
  28611. + au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
  28612. + /* si_nfiles is waited too */
  28613. + au_kfree_rcu(sbinfo);
  28614. +}
  28615. +
  28616. +struct au_sbinfo *au_si_alloc(struct super_block *sb)
  28617. +{
  28618. + struct au_sbinfo *sbinfo;
  28619. + int err, i;
  28620. +
  28621. + err = -ENOMEM;
  28622. + sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
  28623. + if (unlikely(!sbinfo))
  28624. + goto out;
  28625. +
  28626. + /* will be reallocated separately */
  28627. + sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
  28628. + if (unlikely(!sbinfo->si_branch))
  28629. + goto out_sbinfo;
  28630. +
  28631. + err = sysaufs_si_init(sbinfo);
  28632. + if (!err) {
  28633. + dbgaufs_si_null(sbinfo);
  28634. + err = dbgaufs_si_init(sbinfo);
  28635. + if (unlikely(err))
  28636. + kobject_put(&sbinfo->si_kobj);
  28637. + }
  28638. + if (unlikely(err))
  28639. + goto out_br;
  28640. +
  28641. + au_nwt_init(&sbinfo->si_nowait);
  28642. + au_rw_init_wlock(&sbinfo->si_rwsem);
  28643. +
  28644. + au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
  28645. + au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
  28646. +
  28647. + sbinfo->si_bbot = -1;
  28648. + sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
  28649. +
  28650. + sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
  28651. + sbinfo->si_wbr_create = AuWbrCreate_Def;
  28652. + sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
  28653. + sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
  28654. +
  28655. + au_fhsm_init(sbinfo);
  28656. +
  28657. + sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
  28658. +
  28659. + sbinfo->si_xino_jiffy = jiffies;
  28660. + sbinfo->si_xino_expire
  28661. + = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
  28662. + mutex_init(&sbinfo->si_xib_mtx);
  28663. + /* leave si_xib_last_pindex and si_xib_next_bit */
  28664. +
  28665. + INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
  28666. +
  28667. + sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
  28668. + sbinfo->si_rdblk = AUFS_RDBLK_DEF;
  28669. + sbinfo->si_rdhash = AUFS_RDHASH_DEF;
  28670. + sbinfo->si_dirwh = AUFS_DIRWH_DEF;
  28671. +
  28672. + for (i = 0; i < AuPlink_NHASH; i++)
  28673. + INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
  28674. + init_waitqueue_head(&sbinfo->si_plink_wq);
  28675. + spin_lock_init(&sbinfo->si_plink_maint_lock);
  28676. +
  28677. + INIT_HLIST_BL_HEAD(&sbinfo->si_files);
  28678. +
  28679. + /* with getattr by default */
  28680. + sbinfo->si_iop_array = aufs_iop;
  28681. +
  28682. + /* leave other members for sysaufs and si_mnt. */
  28683. + sbinfo->si_sb = sb;
  28684. + if (sb) {
  28685. + sb->s_fs_info = sbinfo;
  28686. + si_pid_set(sb);
  28687. + }
  28688. + return sbinfo; /* success */
  28689. +
  28690. +out_br:
  28691. + au_kfree_try_rcu(sbinfo->si_branch);
  28692. +out_sbinfo:
  28693. + au_kfree_rcu(sbinfo);
  28694. +out:
  28695. + return ERR_PTR(err);
  28696. +}
  28697. +
  28698. +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
  28699. +{
  28700. + int err, sz;
  28701. + struct au_branch **brp;
  28702. +
  28703. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  28704. +
  28705. + err = -ENOMEM;
  28706. + sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
  28707. + if (unlikely(!sz))
  28708. + sz = sizeof(*brp);
  28709. + brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
  28710. + may_shrink);
  28711. + if (brp) {
  28712. + sbinfo->si_branch = brp;
  28713. + err = 0;
  28714. + }
  28715. +
  28716. + return err;
  28717. +}
  28718. +
  28719. +/* ---------------------------------------------------------------------- */
  28720. +
  28721. +unsigned int au_sigen_inc(struct super_block *sb)
  28722. +{
  28723. + unsigned int gen;
  28724. + struct inode *inode;
  28725. +
  28726. + SiMustWriteLock(sb);
  28727. +
  28728. + gen = ++au_sbi(sb)->si_generation;
  28729. + au_update_digen(sb->s_root);
  28730. + inode = d_inode(sb->s_root);
  28731. + au_update_iigen(inode, /*half*/0);
  28732. + inode_inc_iversion(inode);
  28733. + return gen;
  28734. +}
  28735. +
  28736. +aufs_bindex_t au_new_br_id(struct super_block *sb)
  28737. +{
  28738. + aufs_bindex_t br_id;
  28739. + int i;
  28740. + struct au_sbinfo *sbinfo;
  28741. +
  28742. + SiMustWriteLock(sb);
  28743. +
  28744. + sbinfo = au_sbi(sb);
  28745. + for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
  28746. + br_id = ++sbinfo->si_last_br_id;
  28747. + AuDebugOn(br_id < 0);
  28748. + if (br_id && au_br_index(sb, br_id) < 0)
  28749. + return br_id;
  28750. + }
  28751. +
  28752. + return -1;
  28753. +}
  28754. +
  28755. +/* ---------------------------------------------------------------------- */
  28756. +
  28757. +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
  28758. +int si_read_lock(struct super_block *sb, int flags)
  28759. +{
  28760. + int err;
  28761. +
  28762. + err = 0;
  28763. + if (au_ftest_lock(flags, FLUSH))
  28764. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  28765. +
  28766. + si_noflush_read_lock(sb);
  28767. + err = au_plink_maint(sb, flags);
  28768. + if (unlikely(err))
  28769. + si_read_unlock(sb);
  28770. +
  28771. + return err;
  28772. +}
  28773. +
  28774. +int si_write_lock(struct super_block *sb, int flags)
  28775. +{
  28776. + int err;
  28777. +
  28778. + if (au_ftest_lock(flags, FLUSH))
  28779. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  28780. +
  28781. + si_noflush_write_lock(sb);
  28782. + err = au_plink_maint(sb, flags);
  28783. + if (unlikely(err))
  28784. + si_write_unlock(sb);
  28785. +
  28786. + return err;
  28787. +}
  28788. +
  28789. +/* dentry and super_block lock. call at entry point */
  28790. +int aufs_read_lock(struct dentry *dentry, int flags)
  28791. +{
  28792. + int err;
  28793. + struct super_block *sb;
  28794. +
  28795. + sb = dentry->d_sb;
  28796. + err = si_read_lock(sb, flags);
  28797. + if (unlikely(err))
  28798. + goto out;
  28799. +
  28800. + if (au_ftest_lock(flags, DW))
  28801. + di_write_lock_child(dentry);
  28802. + else
  28803. + di_read_lock_child(dentry, flags);
  28804. +
  28805. + if (au_ftest_lock(flags, GEN)) {
  28806. + err = au_digen_test(dentry, au_sigen(sb));
  28807. + if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
  28808. + AuDebugOn(!err && au_dbrange_test(dentry));
  28809. + else if (!err)
  28810. + err = au_dbrange_test(dentry);
  28811. + if (unlikely(err))
  28812. + aufs_read_unlock(dentry, flags);
  28813. + }
  28814. +
  28815. +out:
  28816. + return err;
  28817. +}
  28818. +
  28819. +void aufs_read_unlock(struct dentry *dentry, int flags)
  28820. +{
  28821. + if (au_ftest_lock(flags, DW))
  28822. + di_write_unlock(dentry);
  28823. + else
  28824. + di_read_unlock(dentry, flags);
  28825. + si_read_unlock(dentry->d_sb);
  28826. +}
  28827. +
  28828. +void aufs_write_lock(struct dentry *dentry)
  28829. +{
  28830. + si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
  28831. + di_write_lock_child(dentry);
  28832. +}
  28833. +
  28834. +void aufs_write_unlock(struct dentry *dentry)
  28835. +{
  28836. + di_write_unlock(dentry);
  28837. + si_write_unlock(dentry->d_sb);
  28838. +}
  28839. +
  28840. +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
  28841. +{
  28842. + int err;
  28843. + unsigned int sigen;
  28844. + struct super_block *sb;
  28845. +
  28846. + sb = d1->d_sb;
  28847. + err = si_read_lock(sb, flags);
  28848. + if (unlikely(err))
  28849. + goto out;
  28850. +
  28851. + di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
  28852. +
  28853. + if (au_ftest_lock(flags, GEN)) {
  28854. + sigen = au_sigen(sb);
  28855. + err = au_digen_test(d1, sigen);
  28856. + AuDebugOn(!err && au_dbrange_test(d1));
  28857. + if (!err) {
  28858. + err = au_digen_test(d2, sigen);
  28859. + AuDebugOn(!err && au_dbrange_test(d2));
  28860. + }
  28861. + if (unlikely(err))
  28862. + aufs_read_and_write_unlock2(d1, d2);
  28863. + }
  28864. +
  28865. +out:
  28866. + return err;
  28867. +}
  28868. +
  28869. +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
  28870. +{
  28871. + di_write_unlock2(d1, d2);
  28872. + si_read_unlock(d1->d_sb);
  28873. +}
  28874. diff -Naur null/fs/aufs/super.c linux-5.15.131/fs/aufs/super.c
  28875. --- /dev/null
  28876. +++ linux-5.15.131/fs/aufs/super.c 2023-09-11 14:32:48.152722502 +0300
  28877. @@ -0,0 +1,871 @@
  28878. +// SPDX-License-Identifier: GPL-2.0
  28879. +/*
  28880. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  28881. + *
  28882. + * This program, aufs is free software; you can redistribute it and/or modify
  28883. + * it under the terms of the GNU General Public License as published by
  28884. + * the Free Software Foundation; either version 2 of the License, or
  28885. + * (at your option) any later version.
  28886. + *
  28887. + * This program is distributed in the hope that it will be useful,
  28888. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28889. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28890. + * GNU General Public License for more details.
  28891. + *
  28892. + * You should have received a copy of the GNU General Public License
  28893. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28894. + */
  28895. +
  28896. +/*
  28897. + * mount and super_block operations
  28898. + */
  28899. +
  28900. +#include <linux/iversion.h>
  28901. +#include <linux/mm.h>
  28902. +#include <linux/seq_file.h>
  28903. +#include <linux/statfs.h>
  28904. +#include <linux/vmalloc.h>
  28905. +#include "aufs.h"
  28906. +
  28907. +/*
  28908. + * super_operations
  28909. + */
  28910. +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
  28911. +{
  28912. + struct au_icntnr *c;
  28913. +
  28914. + c = au_cache_alloc_icntnr();
  28915. + if (c) {
  28916. + au_icntnr_init(c);
  28917. + inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
  28918. + c->iinfo.ii_hinode = NULL;
  28919. + return &c->vfs_inode;
  28920. + }
  28921. + return NULL;
  28922. +}
  28923. +
  28924. +static void aufs_destroy_inode(struct inode *inode)
  28925. +{
  28926. + if (!au_is_bad_inode(inode))
  28927. + au_iinfo_fin(inode);
  28928. +}
  28929. +
  28930. +static void aufs_free_inode(struct inode *inode)
  28931. +{
  28932. + au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
  28933. +}
  28934. +
  28935. +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
  28936. +{
  28937. + struct inode *inode;
  28938. + int err;
  28939. +
  28940. + inode = iget_locked(sb, ino);
  28941. + if (unlikely(!inode)) {
  28942. + inode = ERR_PTR(-ENOMEM);
  28943. + goto out;
  28944. + }
  28945. + if (!(inode->i_state & I_NEW))
  28946. + goto out;
  28947. +
  28948. + err = au_xigen_new(inode);
  28949. + if (!err)
  28950. + err = au_iinfo_init(inode);
  28951. + if (!err)
  28952. + inode_inc_iversion(inode);
  28953. + else {
  28954. + iget_failed(inode);
  28955. + inode = ERR_PTR(err);
  28956. + }
  28957. +
  28958. +out:
  28959. + /* never return NULL */
  28960. + AuDebugOn(!inode);
  28961. + AuTraceErrPtr(inode);
  28962. + return inode;
  28963. +}
  28964. +
  28965. +/* lock free root dinfo */
  28966. +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
  28967. +{
  28968. + int err;
  28969. + aufs_bindex_t bindex, bbot;
  28970. + struct path path;
  28971. + struct au_hdentry *hdp;
  28972. + struct au_branch *br;
  28973. + au_br_perm_str_t perm;
  28974. +
  28975. + err = 0;
  28976. + bbot = au_sbbot(sb);
  28977. + bindex = 0;
  28978. + hdp = au_hdentry(au_di(sb->s_root), bindex);
  28979. + for (; !err && bindex <= bbot; bindex++, hdp++) {
  28980. + br = au_sbr(sb, bindex);
  28981. + path.mnt = au_br_mnt(br);
  28982. + path.dentry = hdp->hd_dentry;
  28983. + err = au_seq_path(seq, &path);
  28984. + if (!err) {
  28985. + au_optstr_br_perm(&perm, br->br_perm);
  28986. + seq_printf(seq, "=%s", perm.a);
  28987. + if (bindex != bbot)
  28988. + seq_putc(seq, ':');
  28989. + }
  28990. + }
  28991. + if (unlikely(err || seq_has_overflowed(seq)))
  28992. + err = -E2BIG;
  28993. +
  28994. + return err;
  28995. +}
  28996. +
  28997. +static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
  28998. + const char *append)
  28999. +{
  29000. + char *p;
  29001. +
  29002. + p = fmt;
  29003. + while (*pat != ':')
  29004. + *p++ = *pat++;
  29005. + *p++ = *pat++;
  29006. + strcpy(p, append);
  29007. + AuDebugOn(strlen(fmt) >= len);
  29008. +}
  29009. +
  29010. +static void au_show_wbr_create(struct seq_file *m, int v,
  29011. + struct au_sbinfo *sbinfo)
  29012. +{
  29013. + const char *pat;
  29014. + char fmt[32];
  29015. + struct au_wbr_mfs *mfs;
  29016. +
  29017. + AuRwMustAnyLock(&sbinfo->si_rwsem);
  29018. +
  29019. + seq_puts(m, ",create=");
  29020. + pat = au_optstr_wbr_create(v);
  29021. + mfs = &sbinfo->si_wbr_mfs;
  29022. + switch (v) {
  29023. + case AuWbrCreate_TDP:
  29024. + case AuWbrCreate_RR:
  29025. + case AuWbrCreate_MFS:
  29026. + case AuWbrCreate_PMFS:
  29027. + seq_puts(m, pat);
  29028. + break;
  29029. + case AuWbrCreate_MFSRR:
  29030. + case AuWbrCreate_TDMFS:
  29031. + case AuWbrCreate_PMFSRR:
  29032. + au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
  29033. + seq_printf(m, fmt, mfs->mfsrr_watermark);
  29034. + break;
  29035. + case AuWbrCreate_MFSV:
  29036. + case AuWbrCreate_PMFSV:
  29037. + au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
  29038. + seq_printf(m, fmt,
  29039. + jiffies_to_msecs(mfs->mfs_expire)
  29040. + / MSEC_PER_SEC);
  29041. + break;
  29042. + case AuWbrCreate_MFSRRV:
  29043. + case AuWbrCreate_TDMFSV:
  29044. + case AuWbrCreate_PMFSRRV:
  29045. + au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
  29046. + seq_printf(m, fmt, mfs->mfsrr_watermark,
  29047. + jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
  29048. + break;
  29049. + default:
  29050. + BUG();
  29051. + }
  29052. +}
  29053. +
  29054. +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
  29055. +{
  29056. +#ifdef CONFIG_SYSFS
  29057. + return 0;
  29058. +#else
  29059. + int err;
  29060. + const int len = sizeof(AUFS_XINO_FNAME) - 1;
  29061. + aufs_bindex_t bindex, brid;
  29062. + struct qstr *name;
  29063. + struct file *f;
  29064. + struct dentry *d, *h_root;
  29065. + struct au_branch *br;
  29066. +
  29067. + AuRwMustAnyLock(&sbinfo->si_rwsem);
  29068. +
  29069. + err = 0;
  29070. + f = au_sbi(sb)->si_xib;
  29071. + if (!f)
  29072. + goto out;
  29073. +
  29074. + /* stop printing the default xino path on the first writable branch */
  29075. + h_root = NULL;
  29076. + bindex = au_xi_root(sb, f->f_path.dentry);
  29077. + if (bindex >= 0) {
  29078. + br = au_sbr_sb(sb, bindex);
  29079. + h_root = au_br_dentry(br);
  29080. + }
  29081. +
  29082. + d = f->f_path.dentry;
  29083. + name = &d->d_name;
  29084. + /* safe ->d_parent because the file is unlinked */
  29085. + if (d->d_parent == h_root
  29086. + && name->len == len
  29087. + && !memcmp(name->name, AUFS_XINO_FNAME, len))
  29088. + goto out;
  29089. +
  29090. + seq_puts(seq, ",xino=");
  29091. + err = au_xino_path(seq, f);
  29092. +
  29093. +out:
  29094. + return err;
  29095. +#endif
  29096. +}
  29097. +
  29098. +/* seq_file will re-call me in case of too long string */
  29099. +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
  29100. +{
  29101. + int err;
  29102. + unsigned int mnt_flags, v;
  29103. + struct super_block *sb;
  29104. + struct au_sbinfo *sbinfo;
  29105. +
  29106. +#define AuBool(name, str) do { \
  29107. + v = au_opt_test(mnt_flags, name); \
  29108. + if (v != au_opt_test(AuOpt_Def, name)) \
  29109. + seq_printf(m, ",%s" #str, v ? "" : "no"); \
  29110. +} while (0)
  29111. +
  29112. +#define AuStr(name, str) do { \
  29113. + v = mnt_flags & AuOptMask_##name; \
  29114. + if (v != (AuOpt_Def & AuOptMask_##name)) \
  29115. + seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
  29116. +} while (0)
  29117. +
  29118. +#define AuUInt(name, str, val) do { \
  29119. + if (val != AUFS_##name##_DEF) \
  29120. + seq_printf(m, "," #str "=%u", val); \
  29121. +} while (0)
  29122. +
  29123. + sb = dentry->d_sb;
  29124. + if (sb->s_flags & SB_POSIXACL)
  29125. + seq_puts(m, ",acl");
  29126. +#if 0 /* reserved for future use */
  29127. + if (sb->s_flags & SB_I_VERSION)
  29128. + seq_puts(m, ",i_version");
  29129. +#endif
  29130. +
  29131. + /* lock free root dinfo */
  29132. + si_noflush_read_lock(sb);
  29133. + sbinfo = au_sbi(sb);
  29134. + seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
  29135. +
  29136. + mnt_flags = au_mntflags(sb);
  29137. + if (au_opt_test(mnt_flags, XINO)) {
  29138. + err = au_show_xino(m, sb);
  29139. + if (unlikely(err))
  29140. + goto out;
  29141. + } else
  29142. + seq_puts(m, ",noxino");
  29143. +
  29144. + AuBool(TRUNC_XINO, trunc_xino);
  29145. + AuStr(UDBA, udba);
  29146. + AuBool(SHWH, shwh);
  29147. + AuBool(PLINK, plink);
  29148. + AuBool(DIO, dio);
  29149. + AuBool(DIRPERM1, dirperm1);
  29150. +
  29151. + v = sbinfo->si_wbr_create;
  29152. + if (v != AuWbrCreate_Def)
  29153. + au_show_wbr_create(m, v, sbinfo);
  29154. +
  29155. + v = sbinfo->si_wbr_copyup;
  29156. + if (v != AuWbrCopyup_Def)
  29157. + seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
  29158. +
  29159. + v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
  29160. + if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
  29161. + seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
  29162. +
  29163. + AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
  29164. +
  29165. + v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
  29166. + AuUInt(RDCACHE, rdcache, v);
  29167. +
  29168. + AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
  29169. + AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
  29170. +
  29171. + au_fhsm_show(m, sbinfo);
  29172. +
  29173. + AuBool(DIRREN, dirren);
  29174. + AuBool(SUM, sum);
  29175. + /* AuBool(SUM_W, wsum); */
  29176. + AuBool(WARN_PERM, warn_perm);
  29177. + AuBool(VERBOSE, verbose);
  29178. +
  29179. +out:
  29180. + /* be sure to print "br:" last */
  29181. + if (!sysaufs_brs) {
  29182. + seq_puts(m, ",br:");
  29183. + au_show_brs(m, sb);
  29184. + }
  29185. + si_read_unlock(sb);
  29186. + return 0;
  29187. +
  29188. +#undef AuBool
  29189. +#undef AuStr
  29190. +#undef AuUInt
  29191. +}
  29192. +
  29193. +/* ---------------------------------------------------------------------- */
  29194. +
  29195. +/* sum mode which returns the summation for statfs(2) */
  29196. +
  29197. +static u64 au_add_till_max(u64 a, u64 b)
  29198. +{
  29199. + u64 old;
  29200. +
  29201. + old = a;
  29202. + a += b;
  29203. + if (old <= a)
  29204. + return a;
  29205. + return ULLONG_MAX;
  29206. +}
  29207. +
  29208. +static u64 au_mul_till_max(u64 a, long mul)
  29209. +{
  29210. + u64 old;
  29211. +
  29212. + old = a;
  29213. + a *= mul;
  29214. + if (old <= a)
  29215. + return a;
  29216. + return ULLONG_MAX;
  29217. +}
  29218. +
  29219. +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
  29220. +{
  29221. + int err;
  29222. + long bsize, factor;
  29223. + u64 blocks, bfree, bavail, files, ffree;
  29224. + aufs_bindex_t bbot, bindex, i;
  29225. + unsigned char shared;
  29226. + struct path h_path;
  29227. + struct super_block *h_sb;
  29228. +
  29229. + err = 0;
  29230. + bsize = LONG_MAX;
  29231. + files = 0;
  29232. + ffree = 0;
  29233. + blocks = 0;
  29234. + bfree = 0;
  29235. + bavail = 0;
  29236. + bbot = au_sbbot(sb);
  29237. + for (bindex = 0; bindex <= bbot; bindex++) {
  29238. + h_path.mnt = au_sbr_mnt(sb, bindex);
  29239. + h_sb = h_path.mnt->mnt_sb;
  29240. + shared = 0;
  29241. + for (i = 0; !shared && i < bindex; i++)
  29242. + shared = (au_sbr_sb(sb, i) == h_sb);
  29243. + if (shared)
  29244. + continue;
  29245. +
  29246. + /* sb->s_root for NFS is unreliable */
  29247. + h_path.dentry = h_path.mnt->mnt_root;
  29248. + err = vfs_statfs(&h_path, buf);
  29249. + if (unlikely(err))
  29250. + goto out;
  29251. +
  29252. + if (bsize > buf->f_bsize) {
  29253. + /*
  29254. + * we will reduce bsize, so we have to expand blocks
  29255. + * etc. to match them again
  29256. + */
  29257. + factor = (bsize / buf->f_bsize);
  29258. + blocks = au_mul_till_max(blocks, factor);
  29259. + bfree = au_mul_till_max(bfree, factor);
  29260. + bavail = au_mul_till_max(bavail, factor);
  29261. + bsize = buf->f_bsize;
  29262. + }
  29263. +
  29264. + factor = (buf->f_bsize / bsize);
  29265. + blocks = au_add_till_max(blocks,
  29266. + au_mul_till_max(buf->f_blocks, factor));
  29267. + bfree = au_add_till_max(bfree,
  29268. + au_mul_till_max(buf->f_bfree, factor));
  29269. + bavail = au_add_till_max(bavail,
  29270. + au_mul_till_max(buf->f_bavail, factor));
  29271. + files = au_add_till_max(files, buf->f_files);
  29272. + ffree = au_add_till_max(ffree, buf->f_ffree);
  29273. + }
  29274. +
  29275. + buf->f_bsize = bsize;
  29276. + buf->f_blocks = blocks;
  29277. + buf->f_bfree = bfree;
  29278. + buf->f_bavail = bavail;
  29279. + buf->f_files = files;
  29280. + buf->f_ffree = ffree;
  29281. + buf->f_frsize = 0;
  29282. +
  29283. +out:
  29284. + return err;
  29285. +}
  29286. +
  29287. +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
  29288. +{
  29289. + int err;
  29290. + struct path h_path;
  29291. + struct super_block *sb;
  29292. +
  29293. + /* lock free root dinfo */
  29294. + sb = dentry->d_sb;
  29295. + si_noflush_read_lock(sb);
  29296. + if (!au_opt_test(au_mntflags(sb), SUM)) {
  29297. + /* sb->s_root for NFS is unreliable */
  29298. + h_path.mnt = au_sbr_mnt(sb, 0);
  29299. + h_path.dentry = h_path.mnt->mnt_root;
  29300. + err = vfs_statfs(&h_path, buf);
  29301. + } else
  29302. + err = au_statfs_sum(sb, buf);
  29303. + si_read_unlock(sb);
  29304. +
  29305. + if (!err) {
  29306. + buf->f_type = AUFS_SUPER_MAGIC;
  29307. + buf->f_namelen = AUFS_MAX_NAMELEN;
  29308. + memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
  29309. + }
  29310. + /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
  29311. +
  29312. + return err;
  29313. +}
  29314. +
  29315. +/* ---------------------------------------------------------------------- */
  29316. +
  29317. +static int aufs_sync_fs(struct super_block *sb, int wait)
  29318. +{
  29319. + int err, e;
  29320. + aufs_bindex_t bbot, bindex;
  29321. + struct au_branch *br;
  29322. + struct super_block *h_sb;
  29323. +
  29324. + err = 0;
  29325. + si_noflush_read_lock(sb);
  29326. + bbot = au_sbbot(sb);
  29327. + for (bindex = 0; bindex <= bbot; bindex++) {
  29328. + br = au_sbr(sb, bindex);
  29329. + if (!au_br_writable(br->br_perm))
  29330. + continue;
  29331. +
  29332. + h_sb = au_sbr_sb(sb, bindex);
  29333. + e = vfsub_sync_filesystem(h_sb);
  29334. + if (unlikely(e && !err))
  29335. + err = e;
  29336. + /* go on even if an error happens */
  29337. + }
  29338. + si_read_unlock(sb);
  29339. +
  29340. + return err;
  29341. +}
  29342. +
  29343. +/* ---------------------------------------------------------------------- */
  29344. +
  29345. +/* final actions when unmounting a file system */
  29346. +static void aufs_put_super(struct super_block *sb)
  29347. +{
  29348. + struct au_sbinfo *sbinfo;
  29349. +
  29350. + sbinfo = au_sbi(sb);
  29351. + if (sbinfo)
  29352. + kobject_put(&sbinfo->si_kobj);
  29353. +}
  29354. +
  29355. +/* ---------------------------------------------------------------------- */
  29356. +
  29357. +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
  29358. + struct super_block *sb, void *arg)
  29359. +{
  29360. + void *array;
  29361. + unsigned long long n, sz;
  29362. +
  29363. + array = NULL;
  29364. + n = 0;
  29365. + if (!*hint)
  29366. + goto out;
  29367. +
  29368. + if (*hint > ULLONG_MAX / sizeof(array)) {
  29369. + array = ERR_PTR(-EMFILE);
  29370. + pr_err("hint %llu\n", *hint);
  29371. + goto out;
  29372. + }
  29373. +
  29374. + sz = sizeof(array) * *hint;
  29375. + array = kzalloc(sz, GFP_NOFS);
  29376. + if (unlikely(!array))
  29377. + array = vzalloc(sz);
  29378. + if (unlikely(!array)) {
  29379. + array = ERR_PTR(-ENOMEM);
  29380. + goto out;
  29381. + }
  29382. +
  29383. + n = cb(sb, array, *hint, arg);
  29384. + AuDebugOn(n > *hint);
  29385. +
  29386. +out:
  29387. + *hint = n;
  29388. + return array;
  29389. +}
  29390. +
  29391. +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
  29392. + unsigned long long max __maybe_unused,
  29393. + void *arg)
  29394. +{
  29395. + unsigned long long n;
  29396. + struct inode **p, *inode;
  29397. + struct list_head *head;
  29398. +
  29399. + n = 0;
  29400. + p = a;
  29401. + head = arg;
  29402. + spin_lock(&sb->s_inode_list_lock);
  29403. + list_for_each_entry(inode, head, i_sb_list) {
  29404. + if (!au_is_bad_inode(inode)
  29405. + && au_ii(inode)->ii_btop >= 0) {
  29406. + spin_lock(&inode->i_lock);
  29407. + if (atomic_read(&inode->i_count)) {
  29408. + au_igrab(inode);
  29409. + *p++ = inode;
  29410. + n++;
  29411. + AuDebugOn(n > max);
  29412. + }
  29413. + spin_unlock(&inode->i_lock);
  29414. + }
  29415. + }
  29416. + spin_unlock(&sb->s_inode_list_lock);
  29417. +
  29418. + return n;
  29419. +}
  29420. +
  29421. +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
  29422. +{
  29423. + struct au_sbinfo *sbi;
  29424. +
  29425. + sbi = au_sbi(sb);
  29426. + *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
  29427. + return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
  29428. +}
  29429. +
  29430. +void au_iarray_free(struct inode **a, unsigned long long max)
  29431. +{
  29432. + unsigned long long ull;
  29433. +
  29434. + for (ull = 0; ull < max; ull++)
  29435. + iput(a[ull]);
  29436. + kvfree(a);
  29437. +}
  29438. +
  29439. +/* ---------------------------------------------------------------------- */
  29440. +
  29441. +/*
  29442. + * refresh dentry and inode at remount time.
  29443. + */
  29444. +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
  29445. +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
  29446. + struct dentry *parent)
  29447. +{
  29448. + int err;
  29449. +
  29450. + di_write_lock_child(dentry);
  29451. + di_read_lock_parent(parent, AuLock_IR);
  29452. + err = au_refresh_dentry(dentry, parent);
  29453. + if (!err && dir_flags)
  29454. + au_hn_reset(d_inode(dentry), dir_flags);
  29455. + di_read_unlock(parent, AuLock_IR);
  29456. + di_write_unlock(dentry);
  29457. +
  29458. + return err;
  29459. +}
  29460. +
  29461. +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
  29462. + struct au_sbinfo *sbinfo,
  29463. + const unsigned int dir_flags, unsigned int do_idop)
  29464. +{
  29465. + int err;
  29466. + struct dentry *parent;
  29467. +
  29468. + err = 0;
  29469. + parent = dget_parent(dentry);
  29470. + if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
  29471. + if (d_really_is_positive(dentry)) {
  29472. + if (!d_is_dir(dentry))
  29473. + err = au_do_refresh(dentry, /*dir_flags*/0,
  29474. + parent);
  29475. + else {
  29476. + err = au_do_refresh(dentry, dir_flags, parent);
  29477. + if (unlikely(err))
  29478. + au_fset_si(sbinfo, FAILED_REFRESH_DIR);
  29479. + }
  29480. + } else
  29481. + err = au_do_refresh(dentry, /*dir_flags*/0, parent);
  29482. + AuDbgDentry(dentry);
  29483. + }
  29484. + dput(parent);
  29485. +
  29486. + if (!err) {
  29487. + if (do_idop)
  29488. + au_refresh_dop(dentry, /*force_reval*/0);
  29489. + } else
  29490. + au_refresh_dop(dentry, /*force_reval*/1);
  29491. +
  29492. + AuTraceErr(err);
  29493. + return err;
  29494. +}
  29495. +
  29496. +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
  29497. +{
  29498. + int err, i, j, ndentry, e;
  29499. + unsigned int sigen;
  29500. + struct au_dcsub_pages dpages;
  29501. + struct au_dpage *dpage;
  29502. + struct dentry **dentries, *d;
  29503. + struct au_sbinfo *sbinfo;
  29504. + struct dentry *root = sb->s_root;
  29505. + const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
  29506. +
  29507. + if (do_idop)
  29508. + au_refresh_dop(root, /*force_reval*/0);
  29509. +
  29510. + err = au_dpages_init(&dpages, GFP_NOFS);
  29511. + if (unlikely(err))
  29512. + goto out;
  29513. + err = au_dcsub_pages(&dpages, root, NULL, NULL);
  29514. + if (unlikely(err))
  29515. + goto out_dpages;
  29516. +
  29517. + sigen = au_sigen(sb);
  29518. + sbinfo = au_sbi(sb);
  29519. + for (i = 0; i < dpages.ndpage; i++) {
  29520. + dpage = dpages.dpages + i;
  29521. + dentries = dpage->dentries;
  29522. + ndentry = dpage->ndentry;
  29523. + for (j = 0; j < ndentry; j++) {
  29524. + d = dentries[j];
  29525. + e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
  29526. + do_idop);
  29527. + if (unlikely(e && !err))
  29528. + err = e;
  29529. + /* go on even err */
  29530. + }
  29531. + }
  29532. +
  29533. +out_dpages:
  29534. + au_dpages_free(&dpages);
  29535. +out:
  29536. + return err;
  29537. +}
  29538. +
  29539. +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
  29540. +{
  29541. + int err, e;
  29542. + unsigned int sigen;
  29543. + unsigned long long max, ull;
  29544. + struct inode *inode, **array;
  29545. +
  29546. + array = au_iarray_alloc(sb, &max);
  29547. + err = PTR_ERR(array);
  29548. + if (IS_ERR(array))
  29549. + goto out;
  29550. +
  29551. + err = 0;
  29552. + sigen = au_sigen(sb);
  29553. + for (ull = 0; ull < max; ull++) {
  29554. + inode = array[ull];
  29555. + if (unlikely(!inode))
  29556. + break;
  29557. +
  29558. + e = 0;
  29559. + ii_write_lock_child(inode);
  29560. + if (au_iigen(inode, NULL) != sigen) {
  29561. + e = au_refresh_hinode_self(inode);
  29562. + if (unlikely(e)) {
  29563. + au_refresh_iop(inode, /*force_getattr*/1);
  29564. + pr_err("error %d, i%lu\n", e, inode->i_ino);
  29565. + if (!err)
  29566. + err = e;
  29567. + /* go on even if err */
  29568. + }
  29569. + }
  29570. + if (!e && do_idop)
  29571. + au_refresh_iop(inode, /*force_getattr*/0);
  29572. + ii_write_unlock(inode);
  29573. + }
  29574. +
  29575. + au_iarray_free(array, max);
  29576. +
  29577. +out:
  29578. + return err;
  29579. +}
  29580. +
  29581. +void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
  29582. +{
  29583. + int err, e;
  29584. + unsigned int udba;
  29585. + aufs_bindex_t bindex, bbot;
  29586. + struct dentry *root;
  29587. + struct inode *inode;
  29588. + struct au_branch *br;
  29589. + struct au_sbinfo *sbi;
  29590. +
  29591. + au_sigen_inc(sb);
  29592. + sbi = au_sbi(sb);
  29593. + au_fclr_si(sbi, FAILED_REFRESH_DIR);
  29594. +
  29595. + root = sb->s_root;
  29596. + DiMustNoWaiters(root);
  29597. + inode = d_inode(root);
  29598. + IiMustNoWaiters(inode);
  29599. +
  29600. + udba = au_opt_udba(sb);
  29601. + bbot = au_sbbot(sb);
  29602. + for (bindex = 0; bindex <= bbot; bindex++) {
  29603. + br = au_sbr(sb, bindex);
  29604. + err = au_hnotify_reset_br(udba, br, br->br_perm);
  29605. + if (unlikely(err))
  29606. + AuIOErr("hnotify failed on br %d, %d, ignored\n",
  29607. + bindex, err);
  29608. + /* go on even if err */
  29609. + }
  29610. + au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
  29611. +
  29612. + if (do_idop) {
  29613. + if (au_ftest_si(sbi, NO_DREVAL)) {
  29614. + AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
  29615. + sb->s_d_op = &aufs_dop_noreval;
  29616. + AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
  29617. + sbi->si_iop_array = aufs_iop_nogetattr;
  29618. + } else {
  29619. + AuDebugOn(sb->s_d_op == &aufs_dop);
  29620. + sb->s_d_op = &aufs_dop;
  29621. + AuDebugOn(sbi->si_iop_array == aufs_iop);
  29622. + sbi->si_iop_array = aufs_iop;
  29623. + }
  29624. + pr_info("reset to %ps and %ps\n",
  29625. + sb->s_d_op, sbi->si_iop_array);
  29626. + }
  29627. +
  29628. + di_write_unlock(root);
  29629. + err = au_refresh_d(sb, do_idop);
  29630. + e = au_refresh_i(sb, do_idop);
  29631. + if (unlikely(e && !err))
  29632. + err = e;
  29633. + /* aufs_write_lock() calls ..._child() */
  29634. + di_write_lock_child(root);
  29635. +
  29636. + au_cpup_attr_all(inode, /*force*/1);
  29637. +
  29638. + if (unlikely(err))
  29639. + AuIOErr("refresh failed, ignored, %d\n", err);
  29640. +}
  29641. +
  29642. +const struct super_operations aufs_sop = {
  29643. + .alloc_inode = aufs_alloc_inode,
  29644. + .destroy_inode = aufs_destroy_inode,
  29645. + .free_inode = aufs_free_inode,
  29646. + /* always deleting, no clearing */
  29647. + .drop_inode = generic_delete_inode,
  29648. + .show_options = aufs_show_options,
  29649. + .statfs = aufs_statfs,
  29650. + .put_super = aufs_put_super,
  29651. + .sync_fs = aufs_sync_fs
  29652. +};
  29653. +
  29654. +/* ---------------------------------------------------------------------- */
  29655. +
  29656. +int au_alloc_root(struct super_block *sb)
  29657. +{
  29658. + int err;
  29659. + struct inode *inode;
  29660. + struct dentry *root;
  29661. +
  29662. + err = -ENOMEM;
  29663. + inode = au_iget_locked(sb, AUFS_ROOT_INO);
  29664. + err = PTR_ERR(inode);
  29665. + if (IS_ERR(inode))
  29666. + goto out;
  29667. +
  29668. + inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
  29669. + inode->i_fop = &aufs_dir_fop;
  29670. + inode->i_mode = S_IFDIR;
  29671. + set_nlink(inode, 2);
  29672. + unlock_new_inode(inode);
  29673. +
  29674. + root = d_make_root(inode);
  29675. + if (unlikely(!root))
  29676. + goto out;
  29677. + err = PTR_ERR(root);
  29678. + if (IS_ERR(root))
  29679. + goto out;
  29680. +
  29681. + err = au_di_init(root);
  29682. + if (!err) {
  29683. + sb->s_root = root;
  29684. + return 0; /* success */
  29685. + }
  29686. + dput(root);
  29687. +
  29688. +out:
  29689. + return err;
  29690. +}
  29691. +
  29692. +/* ---------------------------------------------------------------------- */
  29693. +
  29694. +static void aufs_kill_sb(struct super_block *sb)
  29695. +{
  29696. + struct au_sbinfo *sbinfo;
  29697. + struct dentry *root;
  29698. +
  29699. + sbinfo = au_sbi(sb);
  29700. + if (!sbinfo)
  29701. + goto out;
  29702. +
  29703. + au_sbilist_del(sb);
  29704. +
  29705. + root = sb->s_root;
  29706. + if (root)
  29707. + aufs_write_lock(root);
  29708. + else
  29709. + __si_write_lock(sb);
  29710. +
  29711. + au_fhsm_fin(sb);
  29712. + if (sbinfo->si_wbr_create_ops->fin)
  29713. + sbinfo->si_wbr_create_ops->fin(sb);
  29714. + if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
  29715. + au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
  29716. + au_remount_refresh(sb, /*do_idop*/0);
  29717. + }
  29718. + if (au_opt_test(sbinfo->si_mntflags, PLINK))
  29719. + au_plink_put(sb, /*verbose*/1);
  29720. + au_xino_clr(sb);
  29721. + if (root)
  29722. + au_dr_opt_flush(sb);
  29723. +
  29724. + if (root)
  29725. + aufs_write_unlock(root);
  29726. + else
  29727. + __si_write_unlock(sb);
  29728. +
  29729. + sbinfo->si_sb = NULL;
  29730. + au_nwt_flush(&sbinfo->si_nowait);
  29731. +
  29732. +out:
  29733. + kill_anon_super(sb);
  29734. +}
  29735. +
  29736. +struct file_system_type aufs_fs_type = {
  29737. + .name = AUFS_FSTYPE,
  29738. + /* a race between rename and others */
  29739. + .fs_flags = FS_RENAME_DOES_D_MOVE
  29740. + /* untested */
  29741. + /*| FS_ALLOW_IDMAP*/
  29742. + ,
  29743. + .init_fs_context = aufs_fsctx_init,
  29744. + .parameters = aufs_fsctx_paramspec,
  29745. + .kill_sb = aufs_kill_sb,
  29746. + /* no need to __module_get() and module_put(). */
  29747. + .owner = THIS_MODULE,
  29748. +};
  29749. diff -Naur null/fs/aufs/super.h linux-5.15.131/fs/aufs/super.h
  29750. --- /dev/null
  29751. +++ linux-5.15.131/fs/aufs/super.h 2023-09-11 14:32:48.152722502 +0300
  29752. @@ -0,0 +1,592 @@
  29753. +/* SPDX-License-Identifier: GPL-2.0 */
  29754. +/*
  29755. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  29756. + *
  29757. + * This program, aufs is free software; you can redistribute it and/or modify
  29758. + * it under the terms of the GNU General Public License as published by
  29759. + * the Free Software Foundation; either version 2 of the License, or
  29760. + * (at your option) any later version.
  29761. + *
  29762. + * This program is distributed in the hope that it will be useful,
  29763. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29764. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29765. + * GNU General Public License for more details.
  29766. + *
  29767. + * You should have received a copy of the GNU General Public License
  29768. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29769. + */
  29770. +
  29771. +/*
  29772. + * super_block operations
  29773. + */
  29774. +
  29775. +#ifndef __AUFS_SUPER_H__
  29776. +#define __AUFS_SUPER_H__
  29777. +
  29778. +#ifdef __KERNEL__
  29779. +
  29780. +#include <linux/fs.h>
  29781. +#include <linux/kobject.h>
  29782. +#include "hbl.h"
  29783. +#include "lcnt.h"
  29784. +#include "rwsem.h"
  29785. +#include "wkq.h"
  29786. +
  29787. +/* policies to select one among multiple writable branches */
  29788. +struct au_wbr_copyup_operations {
  29789. + int (*copyup)(struct dentry *dentry);
  29790. +};
  29791. +
  29792. +#define AuWbr_DIR 1 /* target is a dir */
  29793. +#define AuWbr_PARENT (1 << 1) /* always require a parent */
  29794. +
  29795. +#define au_ftest_wbr(flags, name) ((flags) & AuWbr_##name)
  29796. +#define au_fset_wbr(flags, name) { (flags) |= AuWbr_##name; }
  29797. +#define au_fclr_wbr(flags, name) { (flags) &= ~AuWbr_##name; }
  29798. +
  29799. +struct au_wbr_create_operations {
  29800. + int (*create)(struct dentry *dentry, unsigned int flags);
  29801. + int (*init)(struct super_block *sb);
  29802. + int (*fin)(struct super_block *sb);
  29803. +};
  29804. +
  29805. +struct au_wbr_mfs {
  29806. + struct mutex mfs_lock; /* protect this structure */
  29807. + unsigned long mfs_jiffy;
  29808. + unsigned long mfs_expire;
  29809. + aufs_bindex_t mfs_bindex;
  29810. +
  29811. + unsigned long long mfsrr_bytes;
  29812. + unsigned long long mfsrr_watermark;
  29813. +};
  29814. +
  29815. +#define AuPlink_NHASH 100
  29816. +static inline int au_plink_hash(ino_t ino)
  29817. +{
  29818. + return ino % AuPlink_NHASH;
  29819. +}
  29820. +
  29821. +/* File-based Hierarchical Storage Management */
  29822. +struct au_fhsm {
  29823. +#ifdef CONFIG_AUFS_FHSM
  29824. + /* allow only one process who can receive the notification */
  29825. + spinlock_t fhsm_spin;
  29826. + pid_t fhsm_pid;
  29827. + wait_queue_head_t fhsm_wqh;
  29828. + atomic_t fhsm_readable;
  29829. +
  29830. + /* these are protected by si_rwsem */
  29831. + unsigned long fhsm_expire;
  29832. + aufs_bindex_t fhsm_bottom;
  29833. +#endif
  29834. +};
  29835. +
  29836. +struct au_branch;
  29837. +struct au_sbinfo {
  29838. + /* nowait tasks in the system-wide workqueue */
  29839. + struct au_nowait_tasks si_nowait;
  29840. +
  29841. + /*
  29842. + * tried sb->s_umount, but failed due to the dependency between i_mutex.
  29843. + * rwsem for au_sbinfo is necessary.
  29844. + */
  29845. + struct au_rwsem si_rwsem;
  29846. +
  29847. + /*
  29848. + * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
  29849. + * remount.
  29850. + */
  29851. + au_lcnt_t si_ninodes, si_nfiles;
  29852. +
  29853. + /* branch management */
  29854. + unsigned int si_generation;
  29855. +
  29856. + /* see AuSi_ flags */
  29857. + unsigned char au_si_status;
  29858. +
  29859. + aufs_bindex_t si_bbot;
  29860. +
  29861. + /* dirty trick to keep br_id plus */
  29862. + unsigned int si_last_br_id :
  29863. + sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
  29864. + struct au_branch **si_branch;
  29865. +
  29866. + /* policy to select a writable branch */
  29867. + unsigned char si_wbr_copyup;
  29868. + unsigned char si_wbr_create;
  29869. + struct au_wbr_copyup_operations *si_wbr_copyup_ops;
  29870. + struct au_wbr_create_operations *si_wbr_create_ops;
  29871. +
  29872. + /* round robin */
  29873. + atomic_t si_wbr_rr_next;
  29874. +
  29875. + /* most free space */
  29876. + struct au_wbr_mfs si_wbr_mfs;
  29877. +
  29878. + /* File-based Hierarchical Storage Management */
  29879. + struct au_fhsm si_fhsm;
  29880. +
  29881. + /* mount flags */
  29882. + /* include/asm-ia64/siginfo.h defines a macro named si_flags */
  29883. + unsigned int si_mntflags;
  29884. +
  29885. + /* external inode number (bitmap and translation table) */
  29886. + loff_t si_ximaxent; /* max entries in a xino */
  29887. +
  29888. + struct file *si_xib;
  29889. + struct mutex si_xib_mtx; /* protect xib members */
  29890. + unsigned long *si_xib_buf;
  29891. + unsigned long si_xib_last_pindex;
  29892. + int si_xib_next_bit;
  29893. +
  29894. + unsigned long si_xino_jiffy;
  29895. + unsigned long si_xino_expire;
  29896. + /* reserved for future use */
  29897. + /* unsigned long long si_xib_limit; */ /* Max xib file size */
  29898. +
  29899. +#ifdef CONFIG_AUFS_EXPORT
  29900. + /* i_generation */
  29901. + /* todo: make xigen file an array to support many inode numbers */
  29902. + struct file *si_xigen;
  29903. + atomic_t si_xigen_next;
  29904. +#endif
  29905. +
  29906. + /* dirty trick to support atomic_open */
  29907. + struct hlist_bl_head si_aopen;
  29908. +
  29909. + /* vdir parameters */
  29910. + unsigned long si_rdcache; /* max cache time in jiffies */
  29911. + unsigned int si_rdblk; /* deblk size */
  29912. + unsigned int si_rdhash; /* hash size */
  29913. +
  29914. + /*
  29915. + * If the number of whiteouts are larger than si_dirwh, leave all of
  29916. + * them after au_whtmp_ren to reduce the cost of rmdir(2).
  29917. + * future fsck.aufs or kernel thread will remove them later.
  29918. + * Otherwise, remove all whiteouts and the dir in rmdir(2).
  29919. + */
  29920. + unsigned int si_dirwh;
  29921. +
  29922. + /* pseudo_link list */
  29923. + struct hlist_bl_head si_plink[AuPlink_NHASH];
  29924. + wait_queue_head_t si_plink_wq;
  29925. + spinlock_t si_plink_maint_lock;
  29926. + pid_t si_plink_maint_pid;
  29927. +
  29928. + /* file list */
  29929. + struct hlist_bl_head si_files;
  29930. +
  29931. + /* with/without getattr, brother of sb->s_d_op */
  29932. + const struct inode_operations *si_iop_array;
  29933. +
  29934. + /*
  29935. + * sysfs and lifetime management.
  29936. + * this is not a small structure and it may be a waste of memory in case
  29937. + * of sysfs is disabled, particularly when many aufs-es are mounted.
  29938. + * but using sysfs is majority.
  29939. + */
  29940. + struct kobject si_kobj;
  29941. +#ifdef CONFIG_DEBUG_FS
  29942. + struct dentry *si_dbgaufs;
  29943. + struct dentry *si_dbgaufs_plink;
  29944. + struct dentry *si_dbgaufs_xib;
  29945. +#ifdef CONFIG_AUFS_EXPORT
  29946. + struct dentry *si_dbgaufs_xigen;
  29947. +#endif
  29948. +#endif
  29949. +
  29950. +#ifdef CONFIG_AUFS_SBILIST
  29951. + struct hlist_bl_node si_list;
  29952. +#endif
  29953. +
  29954. + /* dirty, necessary for unmounting, sysfs and sysrq */
  29955. + struct super_block *si_sb;
  29956. +};
  29957. +
  29958. +/* sbinfo status flags */
  29959. +/*
  29960. + * set true when refresh_dirs() failed at remount time.
  29961. + * then try refreshing dirs at access time again.
  29962. + * if it is false, refreshing dirs at access time is unnecessary
  29963. + */
  29964. +#define AuSi_FAILED_REFRESH_DIR 1
  29965. +#define AuSi_FHSM (1 << 1) /* fhsm is active now */
  29966. +#define AuSi_NO_DREVAL (1 << 2) /* disable all d_revalidate */
  29967. +
  29968. +#ifndef CONFIG_AUFS_FHSM
  29969. +#undef AuSi_FHSM
  29970. +#define AuSi_FHSM 0
  29971. +#endif
  29972. +
  29973. +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
  29974. + unsigned int flag)
  29975. +{
  29976. + AuRwMustAnyLock(&sbi->si_rwsem);
  29977. + return sbi->au_si_status & flag;
  29978. +}
  29979. +#define au_ftest_si(sbinfo, name) au_do_ftest_si(sbinfo, AuSi_##name)
  29980. +#define au_fset_si(sbinfo, name) do { \
  29981. + AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
  29982. + (sbinfo)->au_si_status |= AuSi_##name; \
  29983. +} while (0)
  29984. +#define au_fclr_si(sbinfo, name) do { \
  29985. + AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
  29986. + (sbinfo)->au_si_status &= ~AuSi_##name; \
  29987. +} while (0)
  29988. +
  29989. +/* ---------------------------------------------------------------------- */
  29990. +
  29991. +/* policy to select one among writable branches */
  29992. +#define AuWbrCopyup(sbinfo, ...) \
  29993. + ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
  29994. +#define AuWbrCreate(sbinfo, ...) \
  29995. + ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
  29996. +
  29997. +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
  29998. +#define AuLock_DW 1 /* write-lock dentry */
  29999. +#define AuLock_IR (1 << 1) /* read-lock inode */
  30000. +#define AuLock_IW (1 << 2) /* write-lock inode */
  30001. +#define AuLock_FLUSH (1 << 3) /* wait for 'nowait' tasks */
  30002. +#define AuLock_DIRS (1 << 4) /* target is a pair of dirs */
  30003. + /* except RENAME_EXCHANGE */
  30004. +#define AuLock_NOPLM (1 << 5) /* return err in plm mode */
  30005. +#define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
  30006. +#define AuLock_GEN (1 << 7) /* test digen/iigen */
  30007. +#define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
  30008. +#define au_fset_lock(flags, name) \
  30009. + do { (flags) |= AuLock_##name; } while (0)
  30010. +#define au_fclr_lock(flags, name) \
  30011. + do { (flags) &= ~AuLock_##name; } while (0)
  30012. +
  30013. +/* ---------------------------------------------------------------------- */
  30014. +
  30015. +/* super.c */
  30016. +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
  30017. +
  30018. +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
  30019. + unsigned long long max, void *arg);
  30020. +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
  30021. + struct super_block *sb, void *arg);
  30022. +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
  30023. +void au_iarray_free(struct inode **a, unsigned long long max);
  30024. +
  30025. +void au_remount_refresh(struct super_block *sb, unsigned int do_idop);
  30026. +extern const struct super_operations aufs_sop;
  30027. +int au_alloc_root(struct super_block *sb);
  30028. +extern struct file_system_type aufs_fs_type;
  30029. +
  30030. +/* sbinfo.c */
  30031. +void au_si_free(struct kobject *kobj);
  30032. +struct au_sbinfo *au_si_alloc(struct super_block *sb);
  30033. +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
  30034. +
  30035. +unsigned int au_sigen_inc(struct super_block *sb);
  30036. +aufs_bindex_t au_new_br_id(struct super_block *sb);
  30037. +
  30038. +int si_read_lock(struct super_block *sb, int flags);
  30039. +int si_write_lock(struct super_block *sb, int flags);
  30040. +int aufs_read_lock(struct dentry *dentry, int flags);
  30041. +void aufs_read_unlock(struct dentry *dentry, int flags);
  30042. +void aufs_write_lock(struct dentry *dentry);
  30043. +void aufs_write_unlock(struct dentry *dentry);
  30044. +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
  30045. +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
  30046. +
  30047. +/* wbr_policy.c */
  30048. +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
  30049. +extern struct au_wbr_create_operations au_wbr_create_ops[];
  30050. +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
  30051. +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
  30052. +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
  30053. +
  30054. +/* mvdown.c */
  30055. +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
  30056. +
  30057. +#ifdef CONFIG_AUFS_FHSM
  30058. +/* fhsm.c */
  30059. +
  30060. +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
  30061. +{
  30062. + pid_t pid;
  30063. +
  30064. + spin_lock(&fhsm->fhsm_spin);
  30065. + pid = fhsm->fhsm_pid;
  30066. + spin_unlock(&fhsm->fhsm_spin);
  30067. +
  30068. + return pid;
  30069. +}
  30070. +
  30071. +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
  30072. +void au_fhsm_wrote_all(struct super_block *sb, int force);
  30073. +int au_fhsm_fd(struct super_block *sb, int oflags);
  30074. +int au_fhsm_br_alloc(struct au_branch *br);
  30075. +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
  30076. +void au_fhsm_fin(struct super_block *sb);
  30077. +void au_fhsm_init(struct au_sbinfo *sbinfo);
  30078. +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
  30079. +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
  30080. +#else
  30081. +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
  30082. + int force)
  30083. +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
  30084. +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
  30085. +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
  30086. +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
  30087. +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
  30088. +AuStubVoid(au_fhsm_fin, struct super_block *sb)
  30089. +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
  30090. +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
  30091. +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
  30092. +#endif
  30093. +
  30094. +/* ---------------------------------------------------------------------- */
  30095. +
  30096. +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
  30097. +{
  30098. + return sb->s_fs_info;
  30099. +}
  30100. +
  30101. +/* ---------------------------------------------------------------------- */
  30102. +
  30103. +#ifdef CONFIG_AUFS_EXPORT
  30104. +int au_test_nfsd(void);
  30105. +void au_export_init(struct super_block *sb);
  30106. +void au_xigen_inc(struct inode *inode);
  30107. +int au_xigen_new(struct inode *inode);
  30108. +int au_xigen_set(struct super_block *sb, struct path *path);
  30109. +void au_xigen_clr(struct super_block *sb);
  30110. +
  30111. +static inline int au_busy_or_stale(void)
  30112. +{
  30113. + if (!au_test_nfsd())
  30114. + return -EBUSY;
  30115. + return -ESTALE;
  30116. +}
  30117. +#else
  30118. +AuStubInt0(au_test_nfsd, void)
  30119. +AuStubVoid(au_export_init, struct super_block *sb)
  30120. +AuStubVoid(au_xigen_inc, struct inode *inode)
  30121. +AuStubInt0(au_xigen_new, struct inode *inode)
  30122. +AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
  30123. +AuStubVoid(au_xigen_clr, struct super_block *sb)
  30124. +AuStub(int, au_busy_or_stale, return -EBUSY, void)
  30125. +#endif /* CONFIG_AUFS_EXPORT */
  30126. +
  30127. +/* ---------------------------------------------------------------------- */
  30128. +
  30129. +#ifdef CONFIG_AUFS_SBILIST
  30130. +/* module.c */
  30131. +extern struct hlist_bl_head au_sbilist;
  30132. +
  30133. +static inline void au_sbilist_init(void)
  30134. +{
  30135. + INIT_HLIST_BL_HEAD(&au_sbilist);
  30136. +}
  30137. +
  30138. +static inline void au_sbilist_add(struct super_block *sb)
  30139. +{
  30140. + au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
  30141. +}
  30142. +
  30143. +static inline void au_sbilist_del(struct super_block *sb)
  30144. +{
  30145. + au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
  30146. +}
  30147. +
  30148. +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
  30149. +static inline void au_sbilist_lock(void)
  30150. +{
  30151. + hlist_bl_lock(&au_sbilist);
  30152. +}
  30153. +
  30154. +static inline void au_sbilist_unlock(void)
  30155. +{
  30156. + hlist_bl_unlock(&au_sbilist);
  30157. +}
  30158. +#define AuGFP_SBILIST GFP_ATOMIC
  30159. +#else
  30160. +AuStubVoid(au_sbilist_lock, void)
  30161. +AuStubVoid(au_sbilist_unlock, void)
  30162. +#define AuGFP_SBILIST GFP_NOFS
  30163. +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
  30164. +#else
  30165. +AuStubVoid(au_sbilist_init, void)
  30166. +AuStubVoid(au_sbilist_add, struct super_block *sb)
  30167. +AuStubVoid(au_sbilist_del, struct super_block *sb)
  30168. +AuStubVoid(au_sbilist_lock, void)
  30169. +AuStubVoid(au_sbilist_unlock, void)
  30170. +#define AuGFP_SBILIST GFP_NOFS
  30171. +#endif
  30172. +
  30173. +/* ---------------------------------------------------------------------- */
  30174. +
  30175. +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
  30176. +{
  30177. + /*
  30178. + * This function is a dynamic '__init' function actually,
  30179. + * so the tiny check for si_rwsem is unnecessary.
  30180. + */
  30181. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  30182. +#ifdef CONFIG_DEBUG_FS
  30183. + sbinfo->si_dbgaufs = NULL;
  30184. + sbinfo->si_dbgaufs_plink = NULL;
  30185. + sbinfo->si_dbgaufs_xib = NULL;
  30186. +#ifdef CONFIG_AUFS_EXPORT
  30187. + sbinfo->si_dbgaufs_xigen = NULL;
  30188. +#endif
  30189. +#endif
  30190. +}
  30191. +
  30192. +/* ---------------------------------------------------------------------- */
  30193. +
  30194. +/* current->atomic_flags */
  30195. +/* this value should never corrupt the ones defined in linux/sched.h */
  30196. +#define PFA_AUFS 0x10
  30197. +
  30198. +TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
  30199. +TASK_PFA_SET(AUFS, aufs) /* task_set_aufs */
  30200. +TASK_PFA_CLEAR(AUFS, aufs) /* task_clear_aufs */
  30201. +
  30202. +static inline int si_pid_test(struct super_block *sb)
  30203. +{
  30204. + return !!task_test_aufs(current);
  30205. +}
  30206. +
  30207. +static inline void si_pid_clr(struct super_block *sb)
  30208. +{
  30209. + AuDebugOn(!task_test_aufs(current));
  30210. + task_clear_aufs(current);
  30211. +}
  30212. +
  30213. +static inline void si_pid_set(struct super_block *sb)
  30214. +{
  30215. + AuDebugOn(task_test_aufs(current));
  30216. + task_set_aufs(current);
  30217. +}
  30218. +
  30219. +/* ---------------------------------------------------------------------- */
  30220. +
  30221. +/* lock superblock. mainly for entry point functions */
  30222. +#define __si_read_lock(sb) au_rw_read_lock(&au_sbi(sb)->si_rwsem)
  30223. +#define __si_write_lock(sb) au_rw_write_lock(&au_sbi(sb)->si_rwsem)
  30224. +#define __si_read_trylock(sb) au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
  30225. +#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
  30226. +/*
  30227. +#define __si_read_trylock_nested(sb) \
  30228. + au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
  30229. +#define __si_write_trylock_nested(sb) \
  30230. + au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
  30231. +*/
  30232. +
  30233. +#define __si_read_unlock(sb) au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
  30234. +#define __si_write_unlock(sb) au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
  30235. +#define __si_downgrade_lock(sb) au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
  30236. +
  30237. +#define SiMustNoWaiters(sb) AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
  30238. +#define SiMustAnyLock(sb) AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
  30239. +#define SiMustWriteLock(sb) AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
  30240. +
  30241. +static inline void si_noflush_read_lock(struct super_block *sb)
  30242. +{
  30243. + __si_read_lock(sb);
  30244. + si_pid_set(sb);
  30245. +}
  30246. +
  30247. +static inline int si_noflush_read_trylock(struct super_block *sb)
  30248. +{
  30249. + int locked;
  30250. +
  30251. + locked = __si_read_trylock(sb);
  30252. + if (locked)
  30253. + si_pid_set(sb);
  30254. + return locked;
  30255. +}
  30256. +
  30257. +static inline void si_noflush_write_lock(struct super_block *sb)
  30258. +{
  30259. + __si_write_lock(sb);
  30260. + si_pid_set(sb);
  30261. +}
  30262. +
  30263. +static inline int si_noflush_write_trylock(struct super_block *sb)
  30264. +{
  30265. + int locked;
  30266. +
  30267. + locked = __si_write_trylock(sb);
  30268. + if (locked)
  30269. + si_pid_set(sb);
  30270. + return locked;
  30271. +}
  30272. +
  30273. +#if 0 /* reserved */
  30274. +static inline int si_read_trylock(struct super_block *sb, int flags)
  30275. +{
  30276. + if (au_ftest_lock(flags, FLUSH))
  30277. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  30278. + return si_noflush_read_trylock(sb);
  30279. +}
  30280. +#endif
  30281. +
  30282. +static inline void si_read_unlock(struct super_block *sb)
  30283. +{
  30284. + si_pid_clr(sb);
  30285. + __si_read_unlock(sb);
  30286. +}
  30287. +
  30288. +#if 0 /* reserved */
  30289. +static inline int si_write_trylock(struct super_block *sb, int flags)
  30290. +{
  30291. + if (au_ftest_lock(flags, FLUSH))
  30292. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  30293. + return si_noflush_write_trylock(sb);
  30294. +}
  30295. +#endif
  30296. +
  30297. +static inline void si_write_unlock(struct super_block *sb)
  30298. +{
  30299. + si_pid_clr(sb);
  30300. + __si_write_unlock(sb);
  30301. +}
  30302. +
  30303. +#if 0 /* reserved */
  30304. +static inline void si_downgrade_lock(struct super_block *sb)
  30305. +{
  30306. + __si_downgrade_lock(sb);
  30307. +}
  30308. +#endif
  30309. +
  30310. +/* ---------------------------------------------------------------------- */
  30311. +
  30312. +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
  30313. +{
  30314. + SiMustAnyLock(sb);
  30315. + return au_sbi(sb)->si_bbot;
  30316. +}
  30317. +
  30318. +static inline unsigned int au_mntflags(struct super_block *sb)
  30319. +{
  30320. + SiMustAnyLock(sb);
  30321. + return au_sbi(sb)->si_mntflags;
  30322. +}
  30323. +
  30324. +static inline unsigned int au_sigen(struct super_block *sb)
  30325. +{
  30326. + SiMustAnyLock(sb);
  30327. + return au_sbi(sb)->si_generation;
  30328. +}
  30329. +
  30330. +static inline struct au_branch *au_sbr(struct super_block *sb,
  30331. + aufs_bindex_t bindex)
  30332. +{
  30333. + SiMustAnyLock(sb);
  30334. + return au_sbi(sb)->si_branch[0 + bindex];
  30335. +}
  30336. +
  30337. +static inline loff_t au_xi_maxent(struct super_block *sb)
  30338. +{
  30339. + SiMustAnyLock(sb);
  30340. + return au_sbi(sb)->si_ximaxent;
  30341. +}
  30342. +
  30343. +#endif /* __KERNEL__ */
  30344. +#endif /* __AUFS_SUPER_H__ */
  30345. diff -Naur null/fs/aufs/sysaufs.c linux-5.15.131/fs/aufs/sysaufs.c
  30346. --- /dev/null
  30347. +++ linux-5.15.131/fs/aufs/sysaufs.c 2023-09-11 14:32:48.152722502 +0300
  30348. @@ -0,0 +1,93 @@
  30349. +// SPDX-License-Identifier: GPL-2.0
  30350. +/*
  30351. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  30352. + *
  30353. + * This program, aufs is free software; you can redistribute it and/or modify
  30354. + * it under the terms of the GNU General Public License as published by
  30355. + * the Free Software Foundation; either version 2 of the License, or
  30356. + * (at your option) any later version.
  30357. + *
  30358. + * This program is distributed in the hope that it will be useful,
  30359. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30360. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30361. + * GNU General Public License for more details.
  30362. + *
  30363. + * You should have received a copy of the GNU General Public License
  30364. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30365. + */
  30366. +
  30367. +/*
  30368. + * sysfs interface and lifetime management
  30369. + * they are necessary regardless sysfs is disabled.
  30370. + */
  30371. +
  30372. +#include <linux/random.h>
  30373. +#include "aufs.h"
  30374. +
  30375. +unsigned long sysaufs_si_mask;
  30376. +struct kset *sysaufs_kset;
  30377. +
  30378. +#define AuSiAttr(_name) { \
  30379. + .attr = { .name = __stringify(_name), .mode = 0444 }, \
  30380. + .show = sysaufs_si_##_name, \
  30381. +}
  30382. +
  30383. +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
  30384. +struct attribute *sysaufs_si_attrs[] = {
  30385. + &sysaufs_si_attr_xi_path.attr,
  30386. + NULL,
  30387. +};
  30388. +
  30389. +static const struct sysfs_ops au_sbi_ops = {
  30390. + .show = sysaufs_si_show
  30391. +};
  30392. +
  30393. +static struct kobj_type au_sbi_ktype = {
  30394. + .release = au_si_free,
  30395. + .sysfs_ops = &au_sbi_ops,
  30396. + .default_attrs = sysaufs_si_attrs
  30397. +};
  30398. +
  30399. +/* ---------------------------------------------------------------------- */
  30400. +
  30401. +int sysaufs_si_init(struct au_sbinfo *sbinfo)
  30402. +{
  30403. + int err;
  30404. +
  30405. + sbinfo->si_kobj.kset = sysaufs_kset;
  30406. + /* cf. sysaufs_name() */
  30407. + err = kobject_init_and_add
  30408. + (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
  30409. + SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
  30410. +
  30411. + return err;
  30412. +}
  30413. +
  30414. +void sysaufs_fin(void)
  30415. +{
  30416. + sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
  30417. + kset_unregister(sysaufs_kset);
  30418. +}
  30419. +
  30420. +int __init sysaufs_init(void)
  30421. +{
  30422. + int err;
  30423. +
  30424. + do {
  30425. + get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
  30426. + } while (!sysaufs_si_mask);
  30427. +
  30428. + err = -EINVAL;
  30429. + sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
  30430. + if (unlikely(!sysaufs_kset))
  30431. + goto out;
  30432. + err = PTR_ERR(sysaufs_kset);
  30433. + if (IS_ERR(sysaufs_kset))
  30434. + goto out;
  30435. + err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
  30436. + if (unlikely(err))
  30437. + kset_unregister(sysaufs_kset);
  30438. +
  30439. +out:
  30440. + return err;
  30441. +}
  30442. diff -Naur null/fs/aufs/sysaufs.h linux-5.15.131/fs/aufs/sysaufs.h
  30443. --- /dev/null
  30444. +++ linux-5.15.131/fs/aufs/sysaufs.h 2023-09-11 14:32:48.152722502 +0300
  30445. @@ -0,0 +1,102 @@
  30446. +/* SPDX-License-Identifier: GPL-2.0 */
  30447. +/*
  30448. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  30449. + *
  30450. + * This program, aufs is free software; you can redistribute it and/or modify
  30451. + * it under the terms of the GNU General Public License as published by
  30452. + * the Free Software Foundation; either version 2 of the License, or
  30453. + * (at your option) any later version.
  30454. + *
  30455. + * This program is distributed in the hope that it will be useful,
  30456. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30457. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30458. + * GNU General Public License for more details.
  30459. + *
  30460. + * You should have received a copy of the GNU General Public License
  30461. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30462. + */
  30463. +
  30464. +/*
  30465. + * sysfs interface and mount lifetime management
  30466. + */
  30467. +
  30468. +#ifndef __SYSAUFS_H__
  30469. +#define __SYSAUFS_H__
  30470. +
  30471. +#ifdef __KERNEL__
  30472. +
  30473. +#include <linux/sysfs.h>
  30474. +#include "module.h"
  30475. +
  30476. +struct super_block;
  30477. +struct au_sbinfo;
  30478. +
  30479. +struct sysaufs_si_attr {
  30480. + struct attribute attr;
  30481. + int (*show)(struct seq_file *seq, struct super_block *sb);
  30482. +};
  30483. +
  30484. +/* ---------------------------------------------------------------------- */
  30485. +
  30486. +/* sysaufs.c */
  30487. +extern unsigned long sysaufs_si_mask;
  30488. +extern struct kset *sysaufs_kset;
  30489. +extern struct attribute *sysaufs_si_attrs[];
  30490. +int sysaufs_si_init(struct au_sbinfo *sbinfo);
  30491. +int __init sysaufs_init(void);
  30492. +void sysaufs_fin(void);
  30493. +
  30494. +/* ---------------------------------------------------------------------- */
  30495. +
  30496. +/* some people doesn't like to show a pointer in kernel */
  30497. +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
  30498. +{
  30499. + return sysaufs_si_mask ^ (unsigned long)sbinfo;
  30500. +}
  30501. +
  30502. +#define SysaufsSiNamePrefix "si_"
  30503. +#define SysaufsSiNameLen (sizeof(SysaufsSiNamePrefix) + 16)
  30504. +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
  30505. +{
  30506. + snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
  30507. + sysaufs_si_id(sbinfo));
  30508. +}
  30509. +
  30510. +struct au_branch;
  30511. +#ifdef CONFIG_SYSFS
  30512. +/* sysfs.c */
  30513. +extern struct attribute_group *sysaufs_attr_group;
  30514. +
  30515. +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
  30516. +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
  30517. + char *buf);
  30518. +long au_brinfo_ioctl(struct file *file, unsigned long arg);
  30519. +#ifdef CONFIG_COMPAT
  30520. +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
  30521. +#endif
  30522. +
  30523. +void sysaufs_br_init(struct au_branch *br);
  30524. +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
  30525. +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
  30526. +
  30527. +#define sysaufs_brs_init() do {} while (0)
  30528. +
  30529. +#else
  30530. +#define sysaufs_attr_group NULL
  30531. +
  30532. +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
  30533. +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
  30534. + struct attribute *attr, char *buf)
  30535. +AuStubVoid(sysaufs_br_init, struct au_branch *br)
  30536. +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
  30537. +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
  30538. +
  30539. +static inline void sysaufs_brs_init(void)
  30540. +{
  30541. + sysaufs_brs = 0;
  30542. +}
  30543. +
  30544. +#endif /* CONFIG_SYSFS */
  30545. +
  30546. +#endif /* __KERNEL__ */
  30547. +#endif /* __SYSAUFS_H__ */
  30548. diff -Naur null/fs/aufs/sysfs.c linux-5.15.131/fs/aufs/sysfs.c
  30549. --- /dev/null
  30550. +++ linux-5.15.131/fs/aufs/sysfs.c 2023-09-11 14:32:48.152722502 +0300
  30551. @@ -0,0 +1,374 @@
  30552. +// SPDX-License-Identifier: GPL-2.0
  30553. +/*
  30554. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  30555. + *
  30556. + * This program, aufs is free software; you can redistribute it and/or modify
  30557. + * it under the terms of the GNU General Public License as published by
  30558. + * the Free Software Foundation; either version 2 of the License, or
  30559. + * (at your option) any later version.
  30560. + *
  30561. + * This program is distributed in the hope that it will be useful,
  30562. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30563. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30564. + * GNU General Public License for more details.
  30565. + *
  30566. + * You should have received a copy of the GNU General Public License
  30567. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30568. + */
  30569. +
  30570. +/*
  30571. + * sysfs interface
  30572. + */
  30573. +
  30574. +#include <linux/compat.h>
  30575. +#include <linux/seq_file.h>
  30576. +#include "aufs.h"
  30577. +
  30578. +#ifdef CONFIG_AUFS_FS_MODULE
  30579. +/* this entry violates the "one line per file" policy of sysfs */
  30580. +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
  30581. + char *buf)
  30582. +{
  30583. + ssize_t err;
  30584. + static char *conf =
  30585. +/* this file is generated at compiling */
  30586. +#include "conf.str"
  30587. + ;
  30588. +
  30589. + err = snprintf(buf, PAGE_SIZE, conf);
  30590. + if (unlikely(err >= PAGE_SIZE))
  30591. + err = -EFBIG;
  30592. + return err;
  30593. +}
  30594. +
  30595. +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
  30596. +#endif
  30597. +
  30598. +static struct attribute *au_attr[] = {
  30599. +#ifdef CONFIG_AUFS_FS_MODULE
  30600. + &au_config_attr.attr,
  30601. +#endif
  30602. + NULL, /* need to NULL terminate the list of attributes */
  30603. +};
  30604. +
  30605. +static struct attribute_group sysaufs_attr_group_body = {
  30606. + .attrs = au_attr
  30607. +};
  30608. +
  30609. +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
  30610. +
  30611. +/* ---------------------------------------------------------------------- */
  30612. +
  30613. +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
  30614. +{
  30615. + int err;
  30616. +
  30617. + SiMustAnyLock(sb);
  30618. +
  30619. + err = 0;
  30620. + if (au_opt_test(au_mntflags(sb), XINO)) {
  30621. + err = au_xino_path(seq, au_sbi(sb)->si_xib);
  30622. + seq_putc(seq, '\n');
  30623. + }
  30624. + return err;
  30625. +}
  30626. +
  30627. +/*
  30628. + * the lifetime of branch is independent from the entry under sysfs.
  30629. + * sysfs handles the lifetime of the entry, and never call ->show() after it is
  30630. + * unlinked.
  30631. + */
  30632. +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
  30633. + aufs_bindex_t bindex, int idx)
  30634. +{
  30635. + int err;
  30636. + struct path path;
  30637. + struct dentry *root;
  30638. + struct au_branch *br;
  30639. + au_br_perm_str_t perm;
  30640. +
  30641. + AuDbg("b%d\n", bindex);
  30642. +
  30643. + err = 0;
  30644. + root = sb->s_root;
  30645. + di_read_lock_parent(root, !AuLock_IR);
  30646. + br = au_sbr(sb, bindex);
  30647. +
  30648. + switch (idx) {
  30649. + case AuBrSysfs_BR:
  30650. + path.mnt = au_br_mnt(br);
  30651. + path.dentry = au_h_dptr(root, bindex);
  30652. + err = au_seq_path(seq, &path);
  30653. + if (!err) {
  30654. + au_optstr_br_perm(&perm, br->br_perm);
  30655. + seq_printf(seq, "=%s\n", perm.a);
  30656. + }
  30657. + break;
  30658. + case AuBrSysfs_BRID:
  30659. + seq_printf(seq, "%d\n", br->br_id);
  30660. + break;
  30661. + }
  30662. + di_read_unlock(root, !AuLock_IR);
  30663. + if (unlikely(err || seq_has_overflowed(seq)))
  30664. + err = -E2BIG;
  30665. +
  30666. + return err;
  30667. +}
  30668. +
  30669. +/* ---------------------------------------------------------------------- */
  30670. +
  30671. +static struct seq_file *au_seq(char *p, ssize_t len)
  30672. +{
  30673. + struct seq_file *seq;
  30674. +
  30675. + seq = kzalloc(sizeof(*seq), GFP_NOFS);
  30676. + if (seq) {
  30677. + /* mutex_init(&seq.lock); */
  30678. + seq->buf = p;
  30679. + seq->size = len;
  30680. + return seq; /* success */
  30681. + }
  30682. +
  30683. + seq = ERR_PTR(-ENOMEM);
  30684. + return seq;
  30685. +}
  30686. +
  30687. +#define SysaufsBr_PREFIX "br"
  30688. +#define SysaufsBrid_PREFIX "brid"
  30689. +
  30690. +/* todo: file size may exceed PAGE_SIZE */
  30691. +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
  30692. + char *buf)
  30693. +{
  30694. + ssize_t err;
  30695. + int idx;
  30696. + long l;
  30697. + aufs_bindex_t bbot;
  30698. + struct au_sbinfo *sbinfo;
  30699. + struct super_block *sb;
  30700. + struct seq_file *seq;
  30701. + char *name;
  30702. + struct attribute **cattr;
  30703. +
  30704. + sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
  30705. + sb = sbinfo->si_sb;
  30706. +
  30707. + /*
  30708. + * prevent a race condition between sysfs and aufs.
  30709. + * for instance, sysfs_file_read() calls sysfs_get_active_two() which
  30710. + * prohibits maintaining the sysfs entries.
  30711. + * hew we acquire read lock after sysfs_get_active_two().
  30712. + * on the other hand, the remount process may maintain the sysfs/aufs
  30713. + * entries after acquiring write lock.
  30714. + * it can cause a deadlock.
  30715. + * simply we gave up processing read here.
  30716. + */
  30717. + err = -EBUSY;
  30718. + if (unlikely(!si_noflush_read_trylock(sb)))
  30719. + goto out;
  30720. +
  30721. + seq = au_seq(buf, PAGE_SIZE);
  30722. + err = PTR_ERR(seq);
  30723. + if (IS_ERR(seq))
  30724. + goto out_unlock;
  30725. +
  30726. + name = (void *)attr->name;
  30727. + cattr = sysaufs_si_attrs;
  30728. + while (*cattr) {
  30729. + if (!strcmp(name, (*cattr)->name)) {
  30730. + err = container_of(*cattr, struct sysaufs_si_attr, attr)
  30731. + ->show(seq, sb);
  30732. + goto out_seq;
  30733. + }
  30734. + cattr++;
  30735. + }
  30736. +
  30737. + if (!strncmp(name, SysaufsBrid_PREFIX,
  30738. + sizeof(SysaufsBrid_PREFIX) - 1)) {
  30739. + idx = AuBrSysfs_BRID;
  30740. + name += sizeof(SysaufsBrid_PREFIX) - 1;
  30741. + } else if (!strncmp(name, SysaufsBr_PREFIX,
  30742. + sizeof(SysaufsBr_PREFIX) - 1)) {
  30743. + idx = AuBrSysfs_BR;
  30744. + name += sizeof(SysaufsBr_PREFIX) - 1;
  30745. + } else
  30746. + BUG();
  30747. +
  30748. + err = kstrtol(name, 10, &l);
  30749. + if (!err) {
  30750. + bbot = au_sbbot(sb);
  30751. + if (l <= bbot)
  30752. + err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
  30753. + else
  30754. + err = -ENOENT;
  30755. + }
  30756. +
  30757. +out_seq:
  30758. + if (!err) {
  30759. + err = seq->count;
  30760. + /* sysfs limit */
  30761. + if (unlikely(err == PAGE_SIZE))
  30762. + err = -EFBIG;
  30763. + }
  30764. + au_kfree_rcu(seq);
  30765. +out_unlock:
  30766. + si_read_unlock(sb);
  30767. +out:
  30768. + return err;
  30769. +}
  30770. +
  30771. +/* ---------------------------------------------------------------------- */
  30772. +
  30773. +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
  30774. +{
  30775. + int err;
  30776. + int16_t brid;
  30777. + aufs_bindex_t bindex, bbot;
  30778. + size_t sz;
  30779. + char *buf;
  30780. + struct seq_file *seq;
  30781. + struct au_branch *br;
  30782. +
  30783. + si_read_lock(sb, AuLock_FLUSH);
  30784. + bbot = au_sbbot(sb);
  30785. + err = bbot + 1;
  30786. + if (!arg)
  30787. + goto out;
  30788. +
  30789. + err = -ENOMEM;
  30790. + buf = (void *)__get_free_page(GFP_NOFS);
  30791. + if (unlikely(!buf))
  30792. + goto out;
  30793. +
  30794. + seq = au_seq(buf, PAGE_SIZE);
  30795. + err = PTR_ERR(seq);
  30796. + if (IS_ERR(seq))
  30797. + goto out_buf;
  30798. +
  30799. + sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
  30800. + for (bindex = 0; bindex <= bbot; bindex++, arg++) {
  30801. + /* VERIFY_WRITE */
  30802. + err = !access_ok(arg, sizeof(*arg));
  30803. + if (unlikely(err))
  30804. + break;
  30805. +
  30806. + br = au_sbr(sb, bindex);
  30807. + brid = br->br_id;
  30808. + BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
  30809. + err = __put_user(brid, &arg->id);
  30810. + if (unlikely(err))
  30811. + break;
  30812. +
  30813. + BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
  30814. + err = __put_user(br->br_perm, &arg->perm);
  30815. + if (unlikely(err))
  30816. + break;
  30817. +
  30818. + err = au_seq_path(seq, &br->br_path);
  30819. + if (unlikely(err))
  30820. + break;
  30821. + seq_putc(seq, '\0');
  30822. + if (!seq_has_overflowed(seq)) {
  30823. + err = copy_to_user(arg->path, seq->buf, seq->count);
  30824. + seq->count = 0;
  30825. + if (unlikely(err))
  30826. + break;
  30827. + } else {
  30828. + err = -E2BIG;
  30829. + goto out_seq;
  30830. + }
  30831. + }
  30832. + if (unlikely(err))
  30833. + err = -EFAULT;
  30834. +
  30835. +out_seq:
  30836. + au_kfree_rcu(seq);
  30837. +out_buf:
  30838. + free_page((unsigned long)buf);
  30839. +out:
  30840. + si_read_unlock(sb);
  30841. + return err;
  30842. +}
  30843. +
  30844. +long au_brinfo_ioctl(struct file *file, unsigned long arg)
  30845. +{
  30846. + return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
  30847. +}
  30848. +
  30849. +#ifdef CONFIG_COMPAT
  30850. +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
  30851. +{
  30852. + return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
  30853. +}
  30854. +#endif
  30855. +
  30856. +/* ---------------------------------------------------------------------- */
  30857. +
  30858. +void sysaufs_br_init(struct au_branch *br)
  30859. +{
  30860. + int i;
  30861. + struct au_brsysfs *br_sysfs;
  30862. + struct attribute *attr;
  30863. +
  30864. + br_sysfs = br->br_sysfs;
  30865. + for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
  30866. + attr = &br_sysfs->attr;
  30867. + sysfs_attr_init(attr);
  30868. + attr->name = br_sysfs->name;
  30869. + attr->mode = 0444;
  30870. + br_sysfs++;
  30871. + }
  30872. +}
  30873. +
  30874. +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
  30875. +{
  30876. + struct au_branch *br;
  30877. + struct kobject *kobj;
  30878. + struct au_brsysfs *br_sysfs;
  30879. + int i;
  30880. + aufs_bindex_t bbot;
  30881. +
  30882. + if (!sysaufs_brs)
  30883. + return;
  30884. +
  30885. + kobj = &au_sbi(sb)->si_kobj;
  30886. + bbot = au_sbbot(sb);
  30887. + for (; bindex <= bbot; bindex++) {
  30888. + br = au_sbr(sb, bindex);
  30889. + br_sysfs = br->br_sysfs;
  30890. + for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
  30891. + sysfs_remove_file(kobj, &br_sysfs->attr);
  30892. + br_sysfs++;
  30893. + }
  30894. + }
  30895. +}
  30896. +
  30897. +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
  30898. +{
  30899. + int err, i;
  30900. + aufs_bindex_t bbot;
  30901. + struct kobject *kobj;
  30902. + struct au_branch *br;
  30903. + struct au_brsysfs *br_sysfs;
  30904. +
  30905. + if (!sysaufs_brs)
  30906. + return;
  30907. +
  30908. + kobj = &au_sbi(sb)->si_kobj;
  30909. + bbot = au_sbbot(sb);
  30910. + for (; bindex <= bbot; bindex++) {
  30911. + br = au_sbr(sb, bindex);
  30912. + br_sysfs = br->br_sysfs;
  30913. + snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
  30914. + SysaufsBr_PREFIX "%d", bindex);
  30915. + snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
  30916. + SysaufsBrid_PREFIX "%d", bindex);
  30917. + for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
  30918. + err = sysfs_create_file(kobj, &br_sysfs->attr);
  30919. + if (unlikely(err))
  30920. + pr_warn("failed %s under sysfs(%d)\n",
  30921. + br_sysfs->name, err);
  30922. + br_sysfs++;
  30923. + }
  30924. + }
  30925. +}
  30926. diff -Naur null/fs/aufs/sysrq.c linux-5.15.131/fs/aufs/sysrq.c
  30927. --- /dev/null
  30928. +++ linux-5.15.131/fs/aufs/sysrq.c 2023-09-11 14:32:48.152722502 +0300
  30929. @@ -0,0 +1,149 @@
  30930. +// SPDX-License-Identifier: GPL-2.0
  30931. +/*
  30932. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  30933. + *
  30934. + * This program, aufs is free software; you can redistribute it and/or modify
  30935. + * it under the terms of the GNU General Public License as published by
  30936. + * the Free Software Foundation; either version 2 of the License, or
  30937. + * (at your option) any later version.
  30938. + *
  30939. + * This program is distributed in the hope that it will be useful,
  30940. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30941. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30942. + * GNU General Public License for more details.
  30943. + *
  30944. + * You should have received a copy of the GNU General Public License
  30945. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30946. + */
  30947. +
  30948. +/*
  30949. + * magic sysrq handler
  30950. + */
  30951. +
  30952. +/* #include <linux/sysrq.h> */
  30953. +#include <linux/writeback.h>
  30954. +#include "aufs.h"
  30955. +
  30956. +/* ---------------------------------------------------------------------- */
  30957. +
  30958. +static void sysrq_sb(struct super_block *sb)
  30959. +{
  30960. + char *plevel;
  30961. + struct au_sbinfo *sbinfo;
  30962. + struct file *file;
  30963. + struct hlist_bl_head *files;
  30964. + struct hlist_bl_node *pos;
  30965. + struct au_finfo *finfo;
  30966. + struct inode *i;
  30967. +
  30968. + plevel = au_plevel;
  30969. + au_plevel = KERN_WARNING;
  30970. +
  30971. + /* since we define pr_fmt, call printk directly */
  30972. +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
  30973. +
  30974. + sbinfo = au_sbi(sb);
  30975. + printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
  30976. + pr("superblock\n");
  30977. + au_dpri_sb(sb);
  30978. +
  30979. +#if 0 /* reserved */
  30980. + do {
  30981. + int err, i, j, ndentry;
  30982. + struct au_dcsub_pages dpages;
  30983. + struct au_dpage *dpage;
  30984. +
  30985. + err = au_dpages_init(&dpages, GFP_ATOMIC);
  30986. + if (unlikely(err))
  30987. + break;
  30988. + err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
  30989. + if (!err)
  30990. + for (i = 0; i < dpages.ndpage; i++) {
  30991. + dpage = dpages.dpages + i;
  30992. + ndentry = dpage->ndentry;
  30993. + for (j = 0; j < ndentry; j++)
  30994. + au_dpri_dentry(dpage->dentries[j]);
  30995. + }
  30996. + au_dpages_free(&dpages);
  30997. + } while (0);
  30998. +#endif
  30999. +
  31000. + pr("isolated inode\n");
  31001. + spin_lock(&sb->s_inode_list_lock);
  31002. + list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
  31003. + spin_lock(&i->i_lock);
  31004. + if (hlist_empty(&i->i_dentry))
  31005. + au_dpri_inode(i);
  31006. + spin_unlock(&i->i_lock);
  31007. + }
  31008. + spin_unlock(&sb->s_inode_list_lock);
  31009. +
  31010. + pr("files\n");
  31011. + files = &au_sbi(sb)->si_files;
  31012. + hlist_bl_lock(files);
  31013. + hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
  31014. + umode_t mode;
  31015. +
  31016. + file = finfo->fi_file;
  31017. + mode = file_inode(file)->i_mode;
  31018. + if (!special_file(mode))
  31019. + au_dpri_file(file);
  31020. + }
  31021. + hlist_bl_unlock(files);
  31022. + pr("done\n");
  31023. +
  31024. +#undef pr
  31025. + au_plevel = plevel;
  31026. +}
  31027. +
  31028. +/* ---------------------------------------------------------------------- */
  31029. +
  31030. +/* module parameter */
  31031. +static char *aufs_sysrq_key = "a";
  31032. +module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
  31033. +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
  31034. +
  31035. +static void au_sysrq(int key __maybe_unused)
  31036. +{
  31037. + struct au_sbinfo *sbinfo;
  31038. + struct hlist_bl_node *pos;
  31039. +
  31040. + lockdep_off();
  31041. + au_sbilist_lock();
  31042. + hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
  31043. + sysrq_sb(sbinfo->si_sb);
  31044. + au_sbilist_unlock();
  31045. + lockdep_on();
  31046. +}
  31047. +
  31048. +static struct sysrq_key_op au_sysrq_op = {
  31049. + .handler = au_sysrq,
  31050. + .help_msg = "Aufs",
  31051. + .action_msg = "Aufs",
  31052. + .enable_mask = SYSRQ_ENABLE_DUMP
  31053. +};
  31054. +
  31055. +/* ---------------------------------------------------------------------- */
  31056. +
  31057. +int __init au_sysrq_init(void)
  31058. +{
  31059. + int err;
  31060. + char key;
  31061. +
  31062. + err = -1;
  31063. + key = *aufs_sysrq_key;
  31064. + if ('a' <= key && key <= 'z')
  31065. + err = register_sysrq_key(key, &au_sysrq_op);
  31066. + if (unlikely(err))
  31067. + pr_err("err %d, sysrq=%c\n", err, key);
  31068. + return err;
  31069. +}
  31070. +
  31071. +void au_sysrq_fin(void)
  31072. +{
  31073. + int err;
  31074. +
  31075. + err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
  31076. + if (unlikely(err))
  31077. + pr_err("err %d (ignored)\n", err);
  31078. +}
  31079. diff -Naur null/fs/aufs/vdir.c linux-5.15.131/fs/aufs/vdir.c
  31080. --- /dev/null
  31081. +++ linux-5.15.131/fs/aufs/vdir.c 2023-09-11 14:32:48.153722489 +0300
  31082. @@ -0,0 +1,896 @@
  31083. +// SPDX-License-Identifier: GPL-2.0
  31084. +/*
  31085. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  31086. + *
  31087. + * This program, aufs is free software; you can redistribute it and/or modify
  31088. + * it under the terms of the GNU General Public License as published by
  31089. + * the Free Software Foundation; either version 2 of the License, or
  31090. + * (at your option) any later version.
  31091. + *
  31092. + * This program is distributed in the hope that it will be useful,
  31093. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31094. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31095. + * GNU General Public License for more details.
  31096. + *
  31097. + * You should have received a copy of the GNU General Public License
  31098. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  31099. + */
  31100. +
  31101. +/*
  31102. + * virtual or vertical directory
  31103. + */
  31104. +
  31105. +#include <linux/iversion.h>
  31106. +#include "aufs.h"
  31107. +
  31108. +static unsigned int calc_size(int nlen)
  31109. +{
  31110. + return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
  31111. +}
  31112. +
  31113. +static int set_deblk_end(union au_vdir_deblk_p *p,
  31114. + union au_vdir_deblk_p *deblk_end)
  31115. +{
  31116. + if (calc_size(0) <= deblk_end->deblk - p->deblk) {
  31117. + p->de->de_str.len = 0;
  31118. + /* smp_mb(); */
  31119. + return 0;
  31120. + }
  31121. + return -1; /* error */
  31122. +}
  31123. +
  31124. +/* returns true or false */
  31125. +static int is_deblk_end(union au_vdir_deblk_p *p,
  31126. + union au_vdir_deblk_p *deblk_end)
  31127. +{
  31128. + if (calc_size(0) <= deblk_end->deblk - p->deblk)
  31129. + return !p->de->de_str.len;
  31130. + return 1;
  31131. +}
  31132. +
  31133. +static unsigned char *last_deblk(struct au_vdir *vdir)
  31134. +{
  31135. + return vdir->vd_deblk[vdir->vd_nblk - 1];
  31136. +}
  31137. +
  31138. +/* ---------------------------------------------------------------------- */
  31139. +
  31140. +/* estimate the appropriate size for name hash table */
  31141. +unsigned int au_rdhash_est(loff_t sz)
  31142. +{
  31143. + unsigned int n;
  31144. +
  31145. + n = UINT_MAX;
  31146. + sz >>= 10;
  31147. + if (sz < n)
  31148. + n = sz;
  31149. + if (sz < AUFS_RDHASH_DEF)
  31150. + n = AUFS_RDHASH_DEF;
  31151. + /* pr_info("n %u\n", n); */
  31152. + return n;
  31153. +}
  31154. +
  31155. +/*
  31156. + * the allocated memory has to be freed by
  31157. + * au_nhash_wh_free() or au_nhash_de_free().
  31158. + */
  31159. +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
  31160. +{
  31161. + struct hlist_head *head;
  31162. + unsigned int u;
  31163. + size_t sz;
  31164. +
  31165. + sz = sizeof(*nhash->nh_head) * num_hash;
  31166. + head = kmalloc(sz, gfp);
  31167. + if (head) {
  31168. + nhash->nh_num = num_hash;
  31169. + nhash->nh_head = head;
  31170. + for (u = 0; u < num_hash; u++)
  31171. + INIT_HLIST_HEAD(head++);
  31172. + return 0; /* success */
  31173. + }
  31174. +
  31175. + return -ENOMEM;
  31176. +}
  31177. +
  31178. +static void nhash_count(struct hlist_head *head)
  31179. +{
  31180. +#if 0 /* debugging */
  31181. + unsigned long n;
  31182. + struct hlist_node *pos;
  31183. +
  31184. + n = 0;
  31185. + hlist_for_each(pos, head)
  31186. + n++;
  31187. + pr_info("%lu\n", n);
  31188. +#endif
  31189. +}
  31190. +
  31191. +static void au_nhash_wh_do_free(struct hlist_head *head)
  31192. +{
  31193. + struct au_vdir_wh *pos;
  31194. + struct hlist_node *node;
  31195. +
  31196. + hlist_for_each_entry_safe(pos, node, head, wh_hash)
  31197. + au_kfree_rcu(pos);
  31198. +}
  31199. +
  31200. +static void au_nhash_de_do_free(struct hlist_head *head)
  31201. +{
  31202. + struct au_vdir_dehstr *pos;
  31203. + struct hlist_node *node;
  31204. +
  31205. + hlist_for_each_entry_safe(pos, node, head, hash)
  31206. + au_cache_free_vdir_dehstr(pos);
  31207. +}
  31208. +
  31209. +static void au_nhash_do_free(struct au_nhash *nhash,
  31210. + void (*free)(struct hlist_head *head))
  31211. +{
  31212. + unsigned int n;
  31213. + struct hlist_head *head;
  31214. +
  31215. + n = nhash->nh_num;
  31216. + if (!n)
  31217. + return;
  31218. +
  31219. + head = nhash->nh_head;
  31220. + while (n-- > 0) {
  31221. + nhash_count(head);
  31222. + free(head++);
  31223. + }
  31224. + au_kfree_try_rcu(nhash->nh_head);
  31225. +}
  31226. +
  31227. +void au_nhash_wh_free(struct au_nhash *whlist)
  31228. +{
  31229. + au_nhash_do_free(whlist, au_nhash_wh_do_free);
  31230. +}
  31231. +
  31232. +static void au_nhash_de_free(struct au_nhash *delist)
  31233. +{
  31234. + au_nhash_do_free(delist, au_nhash_de_do_free);
  31235. +}
  31236. +
  31237. +/* ---------------------------------------------------------------------- */
  31238. +
  31239. +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
  31240. + int limit)
  31241. +{
  31242. + int num;
  31243. + unsigned int u, n;
  31244. + struct hlist_head *head;
  31245. + struct au_vdir_wh *pos;
  31246. +
  31247. + num = 0;
  31248. + n = whlist->nh_num;
  31249. + head = whlist->nh_head;
  31250. + for (u = 0; u < n; u++, head++)
  31251. + hlist_for_each_entry(pos, head, wh_hash)
  31252. + if (pos->wh_bindex == btgt && ++num > limit)
  31253. + return 1;
  31254. + return 0;
  31255. +}
  31256. +
  31257. +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
  31258. + unsigned char *name,
  31259. + unsigned int len)
  31260. +{
  31261. + unsigned int v;
  31262. + /* const unsigned int magic_bit = 12; */
  31263. +
  31264. + AuDebugOn(!nhash->nh_num || !nhash->nh_head);
  31265. +
  31266. + v = 0;
  31267. + if (len > 8)
  31268. + len = 8;
  31269. + while (len--)
  31270. + v += *name++;
  31271. + /* v = hash_long(v, magic_bit); */
  31272. + v %= nhash->nh_num;
  31273. + return nhash->nh_head + v;
  31274. +}
  31275. +
  31276. +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
  31277. + int nlen)
  31278. +{
  31279. + return str->len == nlen && !memcmp(str->name, name, nlen);
  31280. +}
  31281. +
  31282. +/* returns found or not */
  31283. +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
  31284. +{
  31285. + struct hlist_head *head;
  31286. + struct au_vdir_wh *pos;
  31287. + struct au_vdir_destr *str;
  31288. +
  31289. + head = au_name_hash(whlist, name, nlen);
  31290. + hlist_for_each_entry(pos, head, wh_hash) {
  31291. + str = &pos->wh_str;
  31292. + AuDbg("%.*s\n", str->len, str->name);
  31293. + if (au_nhash_test_name(str, name, nlen))
  31294. + return 1;
  31295. + }
  31296. + return 0;
  31297. +}
  31298. +
  31299. +/* returns found(true) or not */
  31300. +static int test_known(struct au_nhash *delist, char *name, int nlen)
  31301. +{
  31302. + struct hlist_head *head;
  31303. + struct au_vdir_dehstr *pos;
  31304. + struct au_vdir_destr *str;
  31305. +
  31306. + head = au_name_hash(delist, name, nlen);
  31307. + hlist_for_each_entry(pos, head, hash) {
  31308. + str = pos->str;
  31309. + AuDbg("%.*s\n", str->len, str->name);
  31310. + if (au_nhash_test_name(str, name, nlen))
  31311. + return 1;
  31312. + }
  31313. + return 0;
  31314. +}
  31315. +
  31316. +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
  31317. + unsigned char d_type)
  31318. +{
  31319. +#ifdef CONFIG_AUFS_SHWH
  31320. + wh->wh_ino = ino;
  31321. + wh->wh_type = d_type;
  31322. +#endif
  31323. +}
  31324. +
  31325. +/* ---------------------------------------------------------------------- */
  31326. +
  31327. +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
  31328. + unsigned int d_type, aufs_bindex_t bindex,
  31329. + unsigned char shwh)
  31330. +{
  31331. + int err;
  31332. + struct au_vdir_destr *str;
  31333. + struct au_vdir_wh *wh;
  31334. +
  31335. + AuDbg("%.*s\n", nlen, name);
  31336. + AuDebugOn(!whlist->nh_num || !whlist->nh_head);
  31337. +
  31338. + err = -ENOMEM;
  31339. + wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
  31340. + if (unlikely(!wh))
  31341. + goto out;
  31342. +
  31343. + err = 0;
  31344. + wh->wh_bindex = bindex;
  31345. + if (shwh)
  31346. + au_shwh_init_wh(wh, ino, d_type);
  31347. + str = &wh->wh_str;
  31348. + str->len = nlen;
  31349. + memcpy(str->name, name, nlen);
  31350. + hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
  31351. + /* smp_mb(); */
  31352. +
  31353. +out:
  31354. + return err;
  31355. +}
  31356. +
  31357. +static int append_deblk(struct au_vdir *vdir)
  31358. +{
  31359. + int err;
  31360. + unsigned long ul;
  31361. + const unsigned int deblk_sz = vdir->vd_deblk_sz;
  31362. + union au_vdir_deblk_p p, deblk_end;
  31363. + unsigned char **o;
  31364. +
  31365. + err = -ENOMEM;
  31366. + o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
  31367. + GFP_NOFS, /*may_shrink*/0);
  31368. + if (unlikely(!o))
  31369. + goto out;
  31370. +
  31371. + vdir->vd_deblk = o;
  31372. + p.deblk = kmalloc(deblk_sz, GFP_NOFS);
  31373. + if (p.deblk) {
  31374. + ul = vdir->vd_nblk++;
  31375. + vdir->vd_deblk[ul] = p.deblk;
  31376. + vdir->vd_last.ul = ul;
  31377. + vdir->vd_last.p.deblk = p.deblk;
  31378. + deblk_end.deblk = p.deblk + deblk_sz;
  31379. + err = set_deblk_end(&p, &deblk_end);
  31380. + }
  31381. +
  31382. +out:
  31383. + return err;
  31384. +}
  31385. +
  31386. +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
  31387. + unsigned int d_type, struct au_nhash *delist)
  31388. +{
  31389. + int err;
  31390. + unsigned int sz;
  31391. + const unsigned int deblk_sz = vdir->vd_deblk_sz;
  31392. + union au_vdir_deblk_p p, *room, deblk_end;
  31393. + struct au_vdir_dehstr *dehstr;
  31394. +
  31395. + p.deblk = last_deblk(vdir);
  31396. + deblk_end.deblk = p.deblk + deblk_sz;
  31397. + room = &vdir->vd_last.p;
  31398. + AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
  31399. + || !is_deblk_end(room, &deblk_end));
  31400. +
  31401. + sz = calc_size(nlen);
  31402. + if (unlikely(sz > deblk_end.deblk - room->deblk)) {
  31403. + err = append_deblk(vdir);
  31404. + if (unlikely(err))
  31405. + goto out;
  31406. +
  31407. + p.deblk = last_deblk(vdir);
  31408. + deblk_end.deblk = p.deblk + deblk_sz;
  31409. + /* smp_mb(); */
  31410. + AuDebugOn(room->deblk != p.deblk);
  31411. + }
  31412. +
  31413. + err = -ENOMEM;
  31414. + dehstr = au_cache_alloc_vdir_dehstr();
  31415. + if (unlikely(!dehstr))
  31416. + goto out;
  31417. +
  31418. + dehstr->str = &room->de->de_str;
  31419. + hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
  31420. + room->de->de_ino = ino;
  31421. + room->de->de_type = d_type;
  31422. + room->de->de_str.len = nlen;
  31423. + memcpy(room->de->de_str.name, name, nlen);
  31424. +
  31425. + err = 0;
  31426. + room->deblk += sz;
  31427. + if (unlikely(set_deblk_end(room, &deblk_end)))
  31428. + err = append_deblk(vdir);
  31429. + /* smp_mb(); */
  31430. +
  31431. +out:
  31432. + return err;
  31433. +}
  31434. +
  31435. +/* ---------------------------------------------------------------------- */
  31436. +
  31437. +void au_vdir_free(struct au_vdir *vdir)
  31438. +{
  31439. + unsigned char **deblk;
  31440. +
  31441. + deblk = vdir->vd_deblk;
  31442. + while (vdir->vd_nblk--)
  31443. + au_kfree_try_rcu(*deblk++);
  31444. + au_kfree_try_rcu(vdir->vd_deblk);
  31445. + au_cache_free_vdir(vdir);
  31446. +}
  31447. +
  31448. +static struct au_vdir *alloc_vdir(struct file *file)
  31449. +{
  31450. + struct au_vdir *vdir;
  31451. + struct super_block *sb;
  31452. + int err;
  31453. +
  31454. + sb = file->f_path.dentry->d_sb;
  31455. + SiMustAnyLock(sb);
  31456. +
  31457. + err = -ENOMEM;
  31458. + vdir = au_cache_alloc_vdir();
  31459. + if (unlikely(!vdir))
  31460. + goto out;
  31461. +
  31462. + vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
  31463. + if (unlikely(!vdir->vd_deblk))
  31464. + goto out_free;
  31465. +
  31466. + vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
  31467. + if (!vdir->vd_deblk_sz) {
  31468. + /* estimate the appropriate size for deblk */
  31469. + vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
  31470. + /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
  31471. + }
  31472. + vdir->vd_nblk = 0;
  31473. + vdir->vd_version = 0;
  31474. + vdir->vd_jiffy = 0;
  31475. + err = append_deblk(vdir);
  31476. + if (!err)
  31477. + return vdir; /* success */
  31478. +
  31479. + au_kfree_try_rcu(vdir->vd_deblk);
  31480. +
  31481. +out_free:
  31482. + au_cache_free_vdir(vdir);
  31483. +out:
  31484. + vdir = ERR_PTR(err);
  31485. + return vdir;
  31486. +}
  31487. +
  31488. +static int reinit_vdir(struct au_vdir *vdir)
  31489. +{
  31490. + int err;
  31491. + union au_vdir_deblk_p p, deblk_end;
  31492. +
  31493. + while (vdir->vd_nblk > 1) {
  31494. + au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
  31495. + /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
  31496. + vdir->vd_nblk--;
  31497. + }
  31498. + p.deblk = vdir->vd_deblk[0];
  31499. + deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
  31500. + err = set_deblk_end(&p, &deblk_end);
  31501. + /* keep vd_dblk_sz */
  31502. + vdir->vd_last.ul = 0;
  31503. + vdir->vd_last.p.deblk = vdir->vd_deblk[0];
  31504. + vdir->vd_version = 0;
  31505. + vdir->vd_jiffy = 0;
  31506. + /* smp_mb(); */
  31507. + return err;
  31508. +}
  31509. +
  31510. +/* ---------------------------------------------------------------------- */
  31511. +
  31512. +#define AuFillVdir_CALLED 1
  31513. +#define AuFillVdir_WHABLE (1 << 1)
  31514. +#define AuFillVdir_SHWH (1 << 2)
  31515. +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
  31516. +#define au_fset_fillvdir(flags, name) \
  31517. + do { (flags) |= AuFillVdir_##name; } while (0)
  31518. +#define au_fclr_fillvdir(flags, name) \
  31519. + do { (flags) &= ~AuFillVdir_##name; } while (0)
  31520. +
  31521. +#ifndef CONFIG_AUFS_SHWH
  31522. +#undef AuFillVdir_SHWH
  31523. +#define AuFillVdir_SHWH 0
  31524. +#endif
  31525. +
  31526. +struct fillvdir_arg {
  31527. + struct dir_context ctx;
  31528. + struct file *file;
  31529. + struct au_vdir *vdir;
  31530. + struct au_nhash delist;
  31531. + struct au_nhash whlist;
  31532. + aufs_bindex_t bindex;
  31533. + unsigned int flags;
  31534. + int err;
  31535. +};
  31536. +
  31537. +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
  31538. + loff_t offset __maybe_unused, u64 h_ino,
  31539. + unsigned int d_type)
  31540. +{
  31541. + struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
  31542. + char *name = (void *)__name;
  31543. + struct super_block *sb;
  31544. + ino_t ino;
  31545. + const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
  31546. +
  31547. + arg->err = 0;
  31548. + sb = arg->file->f_path.dentry->d_sb;
  31549. + au_fset_fillvdir(arg->flags, CALLED);
  31550. + /* smp_mb(); */
  31551. + if (nlen <= AUFS_WH_PFX_LEN
  31552. + || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
  31553. + if (test_known(&arg->delist, name, nlen)
  31554. + || au_nhash_test_known_wh(&arg->whlist, name, nlen))
  31555. + goto out; /* already exists or whiteouted */
  31556. +
  31557. + arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
  31558. + if (!arg->err) {
  31559. + if (unlikely(nlen > AUFS_MAX_NAMELEN))
  31560. + d_type = DT_UNKNOWN;
  31561. + arg->err = append_de(arg->vdir, name, nlen, ino,
  31562. + d_type, &arg->delist);
  31563. + }
  31564. + } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
  31565. + name += AUFS_WH_PFX_LEN;
  31566. + nlen -= AUFS_WH_PFX_LEN;
  31567. + if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
  31568. + goto out; /* already whiteouted */
  31569. +
  31570. + ino = 0; /* just to suppress a warning */
  31571. + if (shwh)
  31572. + arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
  31573. + &ino);
  31574. + if (!arg->err) {
  31575. + if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
  31576. + d_type = DT_UNKNOWN;
  31577. + arg->err = au_nhash_append_wh
  31578. + (&arg->whlist, name, nlen, ino, d_type,
  31579. + arg->bindex, shwh);
  31580. + }
  31581. + }
  31582. +
  31583. +out:
  31584. + if (!arg->err)
  31585. + arg->vdir->vd_jiffy = jiffies;
  31586. + /* smp_mb(); */
  31587. + AuTraceErr(arg->err);
  31588. + return arg->err;
  31589. +}
  31590. +
  31591. +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
  31592. + struct au_nhash *whlist, struct au_nhash *delist)
  31593. +{
  31594. +#ifdef CONFIG_AUFS_SHWH
  31595. + int err;
  31596. + unsigned int nh, u;
  31597. + struct hlist_head *head;
  31598. + struct au_vdir_wh *pos;
  31599. + struct hlist_node *n;
  31600. + char *p, *o;
  31601. + struct au_vdir_destr *destr;
  31602. +
  31603. + AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
  31604. +
  31605. + err = -ENOMEM;
  31606. + o = p = (void *)__get_free_page(GFP_NOFS);
  31607. + if (unlikely(!p))
  31608. + goto out;
  31609. +
  31610. + err = 0;
  31611. + nh = whlist->nh_num;
  31612. + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
  31613. + p += AUFS_WH_PFX_LEN;
  31614. + for (u = 0; u < nh; u++) {
  31615. + head = whlist->nh_head + u;
  31616. + hlist_for_each_entry_safe(pos, n, head, wh_hash) {
  31617. + destr = &pos->wh_str;
  31618. + memcpy(p, destr->name, destr->len);
  31619. + err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
  31620. + pos->wh_ino, pos->wh_type, delist);
  31621. + if (unlikely(err))
  31622. + break;
  31623. + }
  31624. + }
  31625. +
  31626. + free_page((unsigned long)o);
  31627. +
  31628. +out:
  31629. + AuTraceErr(err);
  31630. + return err;
  31631. +#else
  31632. + return 0;
  31633. +#endif
  31634. +}
  31635. +
  31636. +static int au_do_read_vdir(struct fillvdir_arg *arg)
  31637. +{
  31638. + int err;
  31639. + unsigned int rdhash;
  31640. + loff_t offset;
  31641. + aufs_bindex_t bbot, bindex, btop;
  31642. + unsigned char shwh;
  31643. + struct file *hf, *file;
  31644. + struct super_block *sb;
  31645. +
  31646. + file = arg->file;
  31647. + sb = file->f_path.dentry->d_sb;
  31648. + SiMustAnyLock(sb);
  31649. +
  31650. + rdhash = au_sbi(sb)->si_rdhash;
  31651. + if (!rdhash)
  31652. + rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
  31653. + err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
  31654. + if (unlikely(err))
  31655. + goto out;
  31656. + err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
  31657. + if (unlikely(err))
  31658. + goto out_delist;
  31659. +
  31660. + err = 0;
  31661. + arg->flags = 0;
  31662. + shwh = 0;
  31663. + if (au_opt_test(au_mntflags(sb), SHWH)) {
  31664. + shwh = 1;
  31665. + au_fset_fillvdir(arg->flags, SHWH);
  31666. + }
  31667. + btop = au_fbtop(file);
  31668. + bbot = au_fbbot_dir(file);
  31669. + for (bindex = btop; !err && bindex <= bbot; bindex++) {
  31670. + hf = au_hf_dir(file, bindex);
  31671. + if (!hf)
  31672. + continue;
  31673. +
  31674. + offset = vfsub_llseek(hf, 0, SEEK_SET);
  31675. + err = offset;
  31676. + if (unlikely(offset))
  31677. + break;
  31678. +
  31679. + arg->bindex = bindex;
  31680. + au_fclr_fillvdir(arg->flags, WHABLE);
  31681. + if (shwh
  31682. + || (bindex != bbot
  31683. + && au_br_whable(au_sbr_perm(sb, bindex))))
  31684. + au_fset_fillvdir(arg->flags, WHABLE);
  31685. + do {
  31686. + arg->err = 0;
  31687. + au_fclr_fillvdir(arg->flags, CALLED);
  31688. + /* smp_mb(); */
  31689. + err = vfsub_iterate_dir(hf, &arg->ctx);
  31690. + if (err >= 0)
  31691. + err = arg->err;
  31692. + } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
  31693. +
  31694. + /*
  31695. + * dir_relax() may be good for concurrency, but aufs should not
  31696. + * use it since it will cause a lockdep problem.
  31697. + */
  31698. + }
  31699. +
  31700. + if (!err && shwh)
  31701. + err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
  31702. +
  31703. + au_nhash_wh_free(&arg->whlist);
  31704. +
  31705. +out_delist:
  31706. + au_nhash_de_free(&arg->delist);
  31707. +out:
  31708. + return err;
  31709. +}
  31710. +
  31711. +static int read_vdir(struct file *file, int may_read)
  31712. +{
  31713. + int err;
  31714. + unsigned long expire;
  31715. + unsigned char do_read;
  31716. + struct fillvdir_arg arg = {
  31717. + .ctx = {
  31718. + .actor = fillvdir
  31719. + }
  31720. + };
  31721. + struct inode *inode;
  31722. + struct au_vdir *vdir, *allocated;
  31723. +
  31724. + err = 0;
  31725. + inode = file_inode(file);
  31726. + IMustLock(inode);
  31727. + IiMustWriteLock(inode);
  31728. + SiMustAnyLock(inode->i_sb);
  31729. +
  31730. + allocated = NULL;
  31731. + do_read = 0;
  31732. + expire = au_sbi(inode->i_sb)->si_rdcache;
  31733. + vdir = au_ivdir(inode);
  31734. + if (!vdir) {
  31735. + do_read = 1;
  31736. + vdir = alloc_vdir(file);
  31737. + err = PTR_ERR(vdir);
  31738. + if (IS_ERR(vdir))
  31739. + goto out;
  31740. + err = 0;
  31741. + allocated = vdir;
  31742. + } else if (may_read
  31743. + && (!inode_eq_iversion(inode, vdir->vd_version)
  31744. + || time_after(jiffies, vdir->vd_jiffy + expire))) {
  31745. + do_read = 1;
  31746. + err = reinit_vdir(vdir);
  31747. + if (unlikely(err))
  31748. + goto out;
  31749. + }
  31750. +
  31751. + if (!do_read)
  31752. + return 0; /* success */
  31753. +
  31754. + arg.file = file;
  31755. + arg.vdir = vdir;
  31756. + err = au_do_read_vdir(&arg);
  31757. + if (!err) {
  31758. + /* file->f_pos = 0; */ /* todo: ctx->pos? */
  31759. + vdir->vd_version = inode_query_iversion(inode);
  31760. + vdir->vd_last.ul = 0;
  31761. + vdir->vd_last.p.deblk = vdir->vd_deblk[0];
  31762. + if (allocated)
  31763. + au_set_ivdir(inode, allocated);
  31764. + } else if (allocated)
  31765. + au_vdir_free(allocated);
  31766. +
  31767. +out:
  31768. + return err;
  31769. +}
  31770. +
  31771. +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
  31772. +{
  31773. + int err, rerr;
  31774. + unsigned long ul, n;
  31775. + const unsigned int deblk_sz = src->vd_deblk_sz;
  31776. +
  31777. + AuDebugOn(tgt->vd_nblk != 1);
  31778. +
  31779. + err = -ENOMEM;
  31780. + if (tgt->vd_nblk < src->vd_nblk) {
  31781. + unsigned char **p;
  31782. +
  31783. + p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
  31784. + GFP_NOFS, /*may_shrink*/0);
  31785. + if (unlikely(!p))
  31786. + goto out;
  31787. + tgt->vd_deblk = p;
  31788. + }
  31789. +
  31790. + if (tgt->vd_deblk_sz != deblk_sz) {
  31791. + unsigned char *p;
  31792. +
  31793. + tgt->vd_deblk_sz = deblk_sz;
  31794. + p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
  31795. + /*may_shrink*/1);
  31796. + if (unlikely(!p))
  31797. + goto out;
  31798. + tgt->vd_deblk[0] = p;
  31799. + }
  31800. + memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
  31801. + tgt->vd_version = src->vd_version;
  31802. + tgt->vd_jiffy = src->vd_jiffy;
  31803. +
  31804. + n = src->vd_nblk;
  31805. + for (ul = 1; ul < n; ul++) {
  31806. + tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
  31807. + GFP_NOFS);
  31808. + if (unlikely(!tgt->vd_deblk[ul]))
  31809. + goto out;
  31810. + tgt->vd_nblk++;
  31811. + }
  31812. + tgt->vd_nblk = n;
  31813. + tgt->vd_last.ul = tgt->vd_last.ul;
  31814. + tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
  31815. + tgt->vd_last.p.deblk += src->vd_last.p.deblk
  31816. + - src->vd_deblk[src->vd_last.ul];
  31817. + /* smp_mb(); */
  31818. + return 0; /* success */
  31819. +
  31820. +out:
  31821. + rerr = reinit_vdir(tgt);
  31822. + BUG_ON(rerr);
  31823. + return err;
  31824. +}
  31825. +
  31826. +int au_vdir_init(struct file *file)
  31827. +{
  31828. + int err;
  31829. + struct inode *inode;
  31830. + struct au_vdir *vdir_cache, *allocated;
  31831. +
  31832. + /* test file->f_pos here instead of ctx->pos */
  31833. + err = read_vdir(file, !file->f_pos);
  31834. + if (unlikely(err))
  31835. + goto out;
  31836. +
  31837. + allocated = NULL;
  31838. + vdir_cache = au_fvdir_cache(file);
  31839. + if (!vdir_cache) {
  31840. + vdir_cache = alloc_vdir(file);
  31841. + err = PTR_ERR(vdir_cache);
  31842. + if (IS_ERR(vdir_cache))
  31843. + goto out;
  31844. + allocated = vdir_cache;
  31845. + } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
  31846. + /* test file->f_pos here instead of ctx->pos */
  31847. + err = reinit_vdir(vdir_cache);
  31848. + if (unlikely(err))
  31849. + goto out;
  31850. + } else
  31851. + return 0; /* success */
  31852. +
  31853. + inode = file_inode(file);
  31854. + err = copy_vdir(vdir_cache, au_ivdir(inode));
  31855. + if (!err) {
  31856. + file->f_version = inode_query_iversion(inode);
  31857. + if (allocated)
  31858. + au_set_fvdir_cache(file, allocated);
  31859. + } else if (allocated)
  31860. + au_vdir_free(allocated);
  31861. +
  31862. +out:
  31863. + return err;
  31864. +}
  31865. +
  31866. +static loff_t calc_offset(struct au_vdir *vdir)
  31867. +{
  31868. + loff_t offset;
  31869. + union au_vdir_deblk_p p;
  31870. +
  31871. + p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
  31872. + offset = vdir->vd_last.p.deblk - p.deblk;
  31873. + offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
  31874. + return offset;
  31875. +}
  31876. +
  31877. +/* returns true or false */
  31878. +static int seek_vdir(struct file *file, struct dir_context *ctx)
  31879. +{
  31880. + int valid;
  31881. + unsigned int deblk_sz;
  31882. + unsigned long ul, n;
  31883. + loff_t offset;
  31884. + union au_vdir_deblk_p p, deblk_end;
  31885. + struct au_vdir *vdir_cache;
  31886. +
  31887. + valid = 1;
  31888. + vdir_cache = au_fvdir_cache(file);
  31889. + offset = calc_offset(vdir_cache);
  31890. + AuDbg("offset %lld\n", offset);
  31891. + if (ctx->pos == offset)
  31892. + goto out;
  31893. +
  31894. + vdir_cache->vd_last.ul = 0;
  31895. + vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
  31896. + if (!ctx->pos)
  31897. + goto out;
  31898. +
  31899. + valid = 0;
  31900. + deblk_sz = vdir_cache->vd_deblk_sz;
  31901. + ul = div64_u64(ctx->pos, deblk_sz);
  31902. + AuDbg("ul %lu\n", ul);
  31903. + if (ul >= vdir_cache->vd_nblk)
  31904. + goto out;
  31905. +
  31906. + n = vdir_cache->vd_nblk;
  31907. + for (; ul < n; ul++) {
  31908. + p.deblk = vdir_cache->vd_deblk[ul];
  31909. + deblk_end.deblk = p.deblk + deblk_sz;
  31910. + offset = ul;
  31911. + offset *= deblk_sz;
  31912. + while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
  31913. + unsigned int l;
  31914. +
  31915. + l = calc_size(p.de->de_str.len);
  31916. + offset += l;
  31917. + p.deblk += l;
  31918. + }
  31919. + if (!is_deblk_end(&p, &deblk_end)) {
  31920. + valid = 1;
  31921. + vdir_cache->vd_last.ul = ul;
  31922. + vdir_cache->vd_last.p = p;
  31923. + break;
  31924. + }
  31925. + }
  31926. +
  31927. +out:
  31928. + /* smp_mb(); */
  31929. + if (!valid)
  31930. + AuDbg("valid %d\n", !valid);
  31931. + return valid;
  31932. +}
  31933. +
  31934. +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
  31935. +{
  31936. + unsigned int l, deblk_sz;
  31937. + union au_vdir_deblk_p deblk_end;
  31938. + struct au_vdir *vdir_cache;
  31939. + struct au_vdir_de *de;
  31940. +
  31941. + if (!seek_vdir(file, ctx))
  31942. + return 0;
  31943. +
  31944. + vdir_cache = au_fvdir_cache(file);
  31945. + deblk_sz = vdir_cache->vd_deblk_sz;
  31946. + while (1) {
  31947. + deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
  31948. + deblk_end.deblk += deblk_sz;
  31949. + while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
  31950. + de = vdir_cache->vd_last.p.de;
  31951. + AuDbg("%.*s, off%lld, i%lu, dt%d\n",
  31952. + de->de_str.len, de->de_str.name, ctx->pos,
  31953. + (unsigned long)de->de_ino, de->de_type);
  31954. + if (unlikely(!dir_emit(ctx, de->de_str.name,
  31955. + de->de_str.len, de->de_ino,
  31956. + de->de_type))) {
  31957. + /* todo: ignore the error caused by udba? */
  31958. + /* return err; */
  31959. + return 0;
  31960. + }
  31961. +
  31962. + l = calc_size(de->de_str.len);
  31963. + vdir_cache->vd_last.p.deblk += l;
  31964. + ctx->pos += l;
  31965. + }
  31966. + if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
  31967. + vdir_cache->vd_last.ul++;
  31968. + vdir_cache->vd_last.p.deblk
  31969. + = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
  31970. + ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
  31971. + continue;
  31972. + }
  31973. + break;
  31974. + }
  31975. +
  31976. + /* smp_mb(); */
  31977. + return 0;
  31978. +}
  31979. diff -Naur null/fs/aufs/vfsub.c linux-5.15.131/fs/aufs/vfsub.c
  31980. --- /dev/null
  31981. +++ linux-5.15.131/fs/aufs/vfsub.c 2023-09-11 14:32:48.153722489 +0300
  31982. @@ -0,0 +1,918 @@
  31983. +// SPDX-License-Identifier: GPL-2.0
  31984. +/*
  31985. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  31986. + *
  31987. + * This program, aufs is free software; you can redistribute it and/or modify
  31988. + * it under the terms of the GNU General Public License as published by
  31989. + * the Free Software Foundation; either version 2 of the License, or
  31990. + * (at your option) any later version.
  31991. + *
  31992. + * This program is distributed in the hope that it will be useful,
  31993. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31994. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31995. + * GNU General Public License for more details.
  31996. + *
  31997. + * You should have received a copy of the GNU General Public License
  31998. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  31999. + */
  32000. +
  32001. +/*
  32002. + * sub-routines for VFS
  32003. + */
  32004. +
  32005. +#include <linux/mnt_namespace.h>
  32006. +#include <linux/nsproxy.h>
  32007. +#include <linux/security.h>
  32008. +#include <linux/splice.h>
  32009. +#include "aufs.h"
  32010. +
  32011. +#ifdef CONFIG_AUFS_BR_FUSE
  32012. +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
  32013. +{
  32014. + if (!au_test_fuse(h_sb) || !au_userns)
  32015. + return 0;
  32016. +
  32017. + return is_current_mnt_ns(mnt) ? 0 : -EACCES;
  32018. +}
  32019. +#endif
  32020. +
  32021. +int vfsub_sync_filesystem(struct super_block *h_sb)
  32022. +{
  32023. + int err;
  32024. +
  32025. + lockdep_off();
  32026. + down_read(&h_sb->s_umount);
  32027. + err = sync_filesystem(h_sb);
  32028. + up_read(&h_sb->s_umount);
  32029. + lockdep_on();
  32030. +
  32031. + return err;
  32032. +}
  32033. +
  32034. +/* ---------------------------------------------------------------------- */
  32035. +
  32036. +int vfsub_update_h_iattr(struct path *h_path, int *did)
  32037. +{
  32038. + int err;
  32039. + struct kstat st;
  32040. + struct super_block *h_sb;
  32041. +
  32042. + /*
  32043. + * Always needs h_path->mnt for LSM or FUSE branch.
  32044. + */
  32045. + AuDebugOn(!h_path->mnt);
  32046. +
  32047. + /* for remote fs, leave work for its getattr or d_revalidate */
  32048. + /* for bad i_attr fs, handle them in aufs_getattr() */
  32049. + /* still some fs may acquire i_mutex. we need to skip them */
  32050. + err = 0;
  32051. + if (!did)
  32052. + did = &err;
  32053. + h_sb = h_path->dentry->d_sb;
  32054. + *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
  32055. + if (*did)
  32056. + err = vfsub_getattr(h_path, &st);
  32057. +
  32058. + return err;
  32059. +}
  32060. +
  32061. +/* ---------------------------------------------------------------------- */
  32062. +
  32063. +struct file *vfsub_dentry_open(struct path *path, int flags)
  32064. +{
  32065. + return dentry_open(path, flags /* | __FMODE_NONOTIFY */,
  32066. + current_cred());
  32067. +}
  32068. +
  32069. +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
  32070. +{
  32071. + struct file *file;
  32072. +
  32073. + lockdep_off();
  32074. + file = filp_open(path,
  32075. + oflags /* | __FMODE_NONOTIFY */,
  32076. + mode);
  32077. + lockdep_on();
  32078. + if (IS_ERR(file))
  32079. + goto out;
  32080. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  32081. +
  32082. +out:
  32083. + return file;
  32084. +}
  32085. +
  32086. +/*
  32087. + * Ideally this function should call VFS:do_last() in order to keep all its
  32088. + * checkings. But it is very hard for aufs to regenerate several VFS internal
  32089. + * structure such as nameidata. This is a second (or third) best approach.
  32090. + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
  32091. + */
  32092. +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
  32093. + struct vfsub_aopen_args *args)
  32094. +{
  32095. + int err;
  32096. + struct au_branch *br = args->br;
  32097. + struct file *file = args->file;
  32098. + /* copied from linux/fs/namei.c:atomic_open() */
  32099. + struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
  32100. +
  32101. + IMustLock(dir);
  32102. + AuDebugOn(!dir->i_op->atomic_open);
  32103. +
  32104. + err = au_br_test_oflag(args->open_flag, br);
  32105. + if (unlikely(err))
  32106. + goto out;
  32107. +
  32108. + au_lcnt_inc(&br->br_nfiles);
  32109. + file->f_path.dentry = DENTRY_NOT_SET;
  32110. + file->f_path.mnt = au_br_mnt(br);
  32111. + AuDbg("%ps\n", dir->i_op->atomic_open);
  32112. + err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
  32113. + args->create_mode);
  32114. + if (unlikely(err < 0)) {
  32115. + au_lcnt_dec(&br->br_nfiles);
  32116. + goto out;
  32117. + }
  32118. +
  32119. + /* temporary workaround for nfsv4 branch */
  32120. + if (au_test_nfs(dir->i_sb))
  32121. + nfs_mark_for_revalidate(dir);
  32122. +
  32123. + if (file->f_mode & FMODE_CREATED)
  32124. + fsnotify_create(dir, dentry);
  32125. + if (!(file->f_mode & FMODE_OPENED)) {
  32126. + au_lcnt_dec(&br->br_nfiles);
  32127. + goto out;
  32128. + }
  32129. +
  32130. + /* todo: call VFS:may_open() here */
  32131. + /* todo: ima_file_check() too? */
  32132. + if (!err && (args->open_flag & __FMODE_EXEC))
  32133. + err = deny_write_access(file);
  32134. + if (!err)
  32135. + fsnotify_open(file);
  32136. + else
  32137. + au_lcnt_dec(&br->br_nfiles);
  32138. + /* note that the file is created and still opened */
  32139. +
  32140. +out:
  32141. + return err;
  32142. +}
  32143. +
  32144. +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
  32145. +{
  32146. + int err;
  32147. +
  32148. + err = kern_path(name, flags, path);
  32149. + if (!err && d_is_positive(path->dentry))
  32150. + vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
  32151. + return err;
  32152. +}
  32153. +
  32154. +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
  32155. + struct path *ppath, int len)
  32156. +{
  32157. + struct path path;
  32158. +
  32159. + path.dentry = lookup_one_len_unlocked(name, ppath->dentry, len);
  32160. + if (IS_ERR(path.dentry))
  32161. + goto out;
  32162. + if (d_is_positive(path.dentry)) {
  32163. + path.mnt = ppath->mnt;
  32164. + vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
  32165. + }
  32166. +
  32167. +out:
  32168. + AuTraceErrPtr(path.dentry);
  32169. + return path.dentry;
  32170. +}
  32171. +
  32172. +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
  32173. + int len)
  32174. +{
  32175. + struct path path;
  32176. +
  32177. + /* VFS checks it too, but by WARN_ON_ONCE() */
  32178. + IMustLock(d_inode(ppath->dentry));
  32179. +
  32180. + path.dentry = lookup_one_len(name, ppath->dentry, len);
  32181. + if (IS_ERR(path.dentry))
  32182. + goto out;
  32183. + if (d_is_positive(path.dentry)) {
  32184. + path.mnt = ppath->mnt;
  32185. + vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
  32186. + }
  32187. +
  32188. +out:
  32189. + AuTraceErrPtr(path.dentry);
  32190. + return path.dentry;
  32191. +}
  32192. +
  32193. +void vfsub_call_lkup_one(void *args)
  32194. +{
  32195. + struct vfsub_lkup_one_args *a = args;
  32196. + *a->errp = vfsub_lkup_one(a->name, a->ppath);
  32197. +}
  32198. +
  32199. +/* ---------------------------------------------------------------------- */
  32200. +
  32201. +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
  32202. + struct dentry *d2, struct au_hinode *hdir2)
  32203. +{
  32204. + struct dentry *d;
  32205. +
  32206. + lockdep_off();
  32207. + d = lock_rename(d1, d2);
  32208. + lockdep_on();
  32209. + au_hn_suspend(hdir1);
  32210. + if (hdir1 != hdir2)
  32211. + au_hn_suspend(hdir2);
  32212. +
  32213. + return d;
  32214. +}
  32215. +
  32216. +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
  32217. + struct dentry *d2, struct au_hinode *hdir2)
  32218. +{
  32219. + au_hn_resume(hdir1);
  32220. + if (hdir1 != hdir2)
  32221. + au_hn_resume(hdir2);
  32222. + lockdep_off();
  32223. + unlock_rename(d1, d2);
  32224. + lockdep_on();
  32225. +}
  32226. +
  32227. +/* ---------------------------------------------------------------------- */
  32228. +
  32229. +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
  32230. +{
  32231. + int err;
  32232. + struct dentry *d;
  32233. + struct user_namespace *userns;
  32234. +
  32235. + IMustLock(dir);
  32236. +
  32237. + d = path->dentry;
  32238. + path->dentry = d->d_parent;
  32239. + err = security_path_mknod(path, d, mode, 0);
  32240. + path->dentry = d;
  32241. + if (unlikely(err))
  32242. + goto out;
  32243. + userns = mnt_user_ns(path->mnt);
  32244. +
  32245. + lockdep_off();
  32246. + err = vfs_create(userns, dir, path->dentry, mode, want_excl);
  32247. + lockdep_on();
  32248. + if (!err) {
  32249. + struct path tmp = *path;
  32250. + int did;
  32251. +
  32252. + vfsub_update_h_iattr(&tmp, &did);
  32253. + if (did) {
  32254. + tmp.dentry = path->dentry->d_parent;
  32255. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  32256. + }
  32257. + /*ignore*/
  32258. + }
  32259. +
  32260. +out:
  32261. + return err;
  32262. +}
  32263. +
  32264. +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
  32265. +{
  32266. + int err;
  32267. + struct dentry *d;
  32268. + struct user_namespace *userns;
  32269. +
  32270. + IMustLock(dir);
  32271. +
  32272. + d = path->dentry;
  32273. + path->dentry = d->d_parent;
  32274. + err = security_path_symlink(path, d, symname);
  32275. + path->dentry = d;
  32276. + if (unlikely(err))
  32277. + goto out;
  32278. + userns = mnt_user_ns(path->mnt);
  32279. +
  32280. + lockdep_off();
  32281. + err = vfs_symlink(userns, dir, path->dentry, symname);
  32282. + lockdep_on();
  32283. + if (!err) {
  32284. + struct path tmp = *path;
  32285. + int did;
  32286. +
  32287. + vfsub_update_h_iattr(&tmp, &did);
  32288. + if (did) {
  32289. + tmp.dentry = path->dentry->d_parent;
  32290. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  32291. + }
  32292. + /*ignore*/
  32293. + }
  32294. +
  32295. +out:
  32296. + return err;
  32297. +}
  32298. +
  32299. +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
  32300. +{
  32301. + int err;
  32302. + struct dentry *d;
  32303. + struct user_namespace *userns;
  32304. +
  32305. + IMustLock(dir);
  32306. +
  32307. + d = path->dentry;
  32308. + path->dentry = d->d_parent;
  32309. + err = security_path_mknod(path, d, mode, new_encode_dev(dev));
  32310. + path->dentry = d;
  32311. + if (unlikely(err))
  32312. + goto out;
  32313. + userns = mnt_user_ns(path->mnt);
  32314. +
  32315. + lockdep_off();
  32316. + err = vfs_mknod(userns, dir, path->dentry, mode, dev);
  32317. + lockdep_on();
  32318. + if (!err) {
  32319. + struct path tmp = *path;
  32320. + int did;
  32321. +
  32322. + vfsub_update_h_iattr(&tmp, &did);
  32323. + if (did) {
  32324. + tmp.dentry = path->dentry->d_parent;
  32325. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  32326. + }
  32327. + /*ignore*/
  32328. + }
  32329. +
  32330. +out:
  32331. + return err;
  32332. +}
  32333. +
  32334. +static int au_test_nlink(struct inode *inode)
  32335. +{
  32336. + const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
  32337. +
  32338. + if (!au_test_fs_no_limit_nlink(inode->i_sb)
  32339. + || inode->i_nlink < link_max)
  32340. + return 0;
  32341. + return -EMLINK;
  32342. +}
  32343. +
  32344. +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
  32345. + struct inode **delegated_inode)
  32346. +{
  32347. + int err;
  32348. + struct dentry *d;
  32349. + struct user_namespace *userns;
  32350. +
  32351. + IMustLock(dir);
  32352. +
  32353. + err = au_test_nlink(d_inode(src_dentry));
  32354. + if (unlikely(err))
  32355. + return err;
  32356. +
  32357. + /* we don't call may_linkat() */
  32358. + d = path->dentry;
  32359. + path->dentry = d->d_parent;
  32360. + err = security_path_link(src_dentry, path, d);
  32361. + path->dentry = d;
  32362. + if (unlikely(err))
  32363. + goto out;
  32364. + userns = mnt_user_ns(path->mnt);
  32365. +
  32366. + lockdep_off();
  32367. + err = vfs_link(src_dentry, userns, dir, path->dentry, delegated_inode);
  32368. + lockdep_on();
  32369. + if (!err) {
  32370. + struct path tmp = *path;
  32371. + int did;
  32372. +
  32373. + /* fuse has different memory inode for the same inumber */
  32374. + vfsub_update_h_iattr(&tmp, &did);
  32375. + if (did) {
  32376. + tmp.dentry = path->dentry->d_parent;
  32377. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  32378. + tmp.dentry = src_dentry;
  32379. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  32380. + }
  32381. + /*ignore*/
  32382. + }
  32383. +
  32384. +out:
  32385. + return err;
  32386. +}
  32387. +
  32388. +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
  32389. + struct inode *dir, struct path *path,
  32390. + struct inode **delegated_inode, unsigned int flags)
  32391. +{
  32392. + int err;
  32393. + struct renamedata rd;
  32394. + struct path tmp = {
  32395. + .mnt = path->mnt
  32396. + };
  32397. + struct dentry *d;
  32398. +
  32399. + IMustLock(dir);
  32400. + IMustLock(src_dir);
  32401. +
  32402. + d = path->dentry;
  32403. + path->dentry = d->d_parent;
  32404. + tmp.dentry = src_dentry->d_parent;
  32405. + err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
  32406. + path->dentry = d;
  32407. + if (unlikely(err))
  32408. + goto out;
  32409. +
  32410. + rd.old_mnt_userns = mnt_user_ns(path->mnt);
  32411. + rd.old_dir = src_dir;
  32412. + rd.old_dentry = src_dentry;
  32413. + rd.new_mnt_userns = rd.old_mnt_userns;
  32414. + rd.new_dir = dir;
  32415. + rd.new_dentry = path->dentry;
  32416. + rd.delegated_inode = delegated_inode;
  32417. + rd.flags = flags;
  32418. + lockdep_off();
  32419. + err = vfs_rename(&rd);
  32420. + lockdep_on();
  32421. + if (!err) {
  32422. + int did;
  32423. +
  32424. + tmp.dentry = d->d_parent;
  32425. + vfsub_update_h_iattr(&tmp, &did);
  32426. + if (did) {
  32427. + tmp.dentry = src_dentry;
  32428. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  32429. + tmp.dentry = src_dentry->d_parent;
  32430. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  32431. + }
  32432. + /*ignore*/
  32433. + }
  32434. +
  32435. +out:
  32436. + return err;
  32437. +}
  32438. +
  32439. +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
  32440. +{
  32441. + int err;
  32442. + struct dentry *d;
  32443. + struct user_namespace *userns;
  32444. +
  32445. + IMustLock(dir);
  32446. +
  32447. + d = path->dentry;
  32448. + path->dentry = d->d_parent;
  32449. + err = security_path_mkdir(path, d, mode);
  32450. + path->dentry = d;
  32451. + if (unlikely(err))
  32452. + goto out;
  32453. + userns = mnt_user_ns(path->mnt);
  32454. +
  32455. + lockdep_off();
  32456. + err = vfs_mkdir(userns, dir, path->dentry, mode);
  32457. + lockdep_on();
  32458. + if (!err) {
  32459. + struct path tmp = *path;
  32460. + int did;
  32461. +
  32462. + vfsub_update_h_iattr(&tmp, &did);
  32463. + if (did) {
  32464. + tmp.dentry = path->dentry->d_parent;
  32465. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  32466. + }
  32467. + /*ignore*/
  32468. + }
  32469. +
  32470. +out:
  32471. + return err;
  32472. +}
  32473. +
  32474. +int vfsub_rmdir(struct inode *dir, struct path *path)
  32475. +{
  32476. + int err;
  32477. + struct dentry *d;
  32478. + struct user_namespace *userns;
  32479. +
  32480. + IMustLock(dir);
  32481. +
  32482. + d = path->dentry;
  32483. + path->dentry = d->d_parent;
  32484. + err = security_path_rmdir(path, d);
  32485. + path->dentry = d;
  32486. + if (unlikely(err))
  32487. + goto out;
  32488. + userns = mnt_user_ns(path->mnt);
  32489. +
  32490. + lockdep_off();
  32491. + err = vfs_rmdir(userns, dir, path->dentry);
  32492. + lockdep_on();
  32493. + if (!err) {
  32494. + struct path tmp = {
  32495. + .dentry = path->dentry->d_parent,
  32496. + .mnt = path->mnt
  32497. + };
  32498. +
  32499. + vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
  32500. + }
  32501. +
  32502. +out:
  32503. + return err;
  32504. +}
  32505. +
  32506. +/* ---------------------------------------------------------------------- */
  32507. +
  32508. +/* todo: support mmap_sem? */
  32509. +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
  32510. + loff_t *ppos)
  32511. +{
  32512. + ssize_t err;
  32513. +
  32514. + lockdep_off();
  32515. + err = vfs_read(file, ubuf, count, ppos);
  32516. + lockdep_on();
  32517. + if (err >= 0)
  32518. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  32519. + return err;
  32520. +}
  32521. +
  32522. +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
  32523. + loff_t *ppos)
  32524. +{
  32525. + ssize_t err;
  32526. +
  32527. + lockdep_off();
  32528. + err = kernel_read(file, kbuf, count, ppos);
  32529. + lockdep_on();
  32530. + AuTraceErr(err);
  32531. + if (err >= 0)
  32532. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  32533. + return err;
  32534. +}
  32535. +
  32536. +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
  32537. + loff_t *ppos)
  32538. +{
  32539. + ssize_t err;
  32540. +
  32541. + lockdep_off();
  32542. + err = vfs_write(file, ubuf, count, ppos);
  32543. + lockdep_on();
  32544. + if (err >= 0)
  32545. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  32546. + return err;
  32547. +}
  32548. +
  32549. +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
  32550. +{
  32551. + ssize_t err;
  32552. +
  32553. + lockdep_off();
  32554. + err = kernel_write(file, kbuf, count, ppos);
  32555. + lockdep_on();
  32556. + if (err >= 0)
  32557. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  32558. + return err;
  32559. +}
  32560. +
  32561. +int vfsub_flush(struct file *file, fl_owner_t id)
  32562. +{
  32563. + int err;
  32564. +
  32565. + err = 0;
  32566. + if (file->f_op->flush) {
  32567. + if (!au_test_nfs(file->f_path.dentry->d_sb))
  32568. + err = file->f_op->flush(file, id);
  32569. + else {
  32570. + lockdep_off();
  32571. + err = file->f_op->flush(file, id);
  32572. + lockdep_on();
  32573. + }
  32574. + if (!err)
  32575. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
  32576. + /*ignore*/
  32577. + }
  32578. + return err;
  32579. +}
  32580. +
  32581. +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
  32582. +{
  32583. + int err;
  32584. +
  32585. + AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
  32586. +
  32587. + lockdep_off();
  32588. + err = iterate_dir(file, ctx);
  32589. + lockdep_on();
  32590. + if (err >= 0)
  32591. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  32592. +
  32593. + return err;
  32594. +}
  32595. +
  32596. +long vfsub_splice_to(struct file *in, loff_t *ppos,
  32597. + struct pipe_inode_info *pipe, size_t len,
  32598. + unsigned int flags)
  32599. +{
  32600. + long err;
  32601. +
  32602. + lockdep_off();
  32603. + err = do_splice_to(in, ppos, pipe, len, flags);
  32604. + lockdep_on();
  32605. + file_accessed(in);
  32606. + if (err >= 0)
  32607. + vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
  32608. + return err;
  32609. +}
  32610. +
  32611. +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
  32612. + loff_t *ppos, size_t len, unsigned int flags)
  32613. +{
  32614. + long err;
  32615. +
  32616. + lockdep_off();
  32617. + err = do_splice_from(pipe, out, ppos, len, flags);
  32618. + lockdep_on();
  32619. + if (err >= 0)
  32620. + vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
  32621. + return err;
  32622. +}
  32623. +
  32624. +int vfsub_fsync(struct file *file, struct path *path, int datasync)
  32625. +{
  32626. + int err;
  32627. +
  32628. + /* file can be NULL */
  32629. + lockdep_off();
  32630. + err = vfs_fsync(file, datasync);
  32631. + lockdep_on();
  32632. + if (!err) {
  32633. + if (!path) {
  32634. + AuDebugOn(!file);
  32635. + path = &file->f_path;
  32636. + }
  32637. + vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
  32638. + }
  32639. + return err;
  32640. +}
  32641. +
  32642. +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
  32643. +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
  32644. + struct file *h_file)
  32645. +{
  32646. + int err;
  32647. + struct inode *h_inode;
  32648. + struct super_block *h_sb;
  32649. + struct user_namespace *h_userns;
  32650. +
  32651. + if (!h_file) {
  32652. + err = vfsub_truncate(h_path, length);
  32653. + goto out;
  32654. + }
  32655. +
  32656. + h_inode = d_inode(h_path->dentry);
  32657. + h_sb = h_inode->i_sb;
  32658. + lockdep_off();
  32659. + sb_start_write(h_sb);
  32660. + lockdep_on();
  32661. + err = security_path_truncate(h_path);
  32662. + if (!err) {
  32663. + h_userns = mnt_user_ns(h_path->mnt);
  32664. + lockdep_off();
  32665. + err = do_truncate(h_userns, h_path->dentry, length, attr,
  32666. + h_file);
  32667. + lockdep_on();
  32668. + }
  32669. + lockdep_off();
  32670. + sb_end_write(h_sb);
  32671. + lockdep_on();
  32672. +
  32673. +out:
  32674. + return err;
  32675. +}
  32676. +
  32677. +/* ---------------------------------------------------------------------- */
  32678. +
  32679. +struct au_vfsub_mkdir_args {
  32680. + int *errp;
  32681. + struct inode *dir;
  32682. + struct path *path;
  32683. + int mode;
  32684. +};
  32685. +
  32686. +static void au_call_vfsub_mkdir(void *args)
  32687. +{
  32688. + struct au_vfsub_mkdir_args *a = args;
  32689. + *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
  32690. +}
  32691. +
  32692. +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
  32693. +{
  32694. + int err, do_sio, wkq_err;
  32695. + struct user_namespace *userns;
  32696. +
  32697. + userns = mnt_user_ns(path->mnt);
  32698. + do_sio = au_test_h_perm_sio(userns, dir, MAY_EXEC | MAY_WRITE);
  32699. + if (!do_sio) {
  32700. + lockdep_off();
  32701. + err = vfsub_mkdir(dir, path, mode);
  32702. + lockdep_on();
  32703. + } else {
  32704. + struct au_vfsub_mkdir_args args = {
  32705. + .errp = &err,
  32706. + .dir = dir,
  32707. + .path = path,
  32708. + .mode = mode
  32709. + };
  32710. + wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
  32711. + if (unlikely(wkq_err))
  32712. + err = wkq_err;
  32713. + }
  32714. +
  32715. + return err;
  32716. +}
  32717. +
  32718. +struct au_vfsub_rmdir_args {
  32719. + int *errp;
  32720. + struct inode *dir;
  32721. + struct path *path;
  32722. +};
  32723. +
  32724. +static void au_call_vfsub_rmdir(void *args)
  32725. +{
  32726. + struct au_vfsub_rmdir_args *a = args;
  32727. + *a->errp = vfsub_rmdir(a->dir, a->path);
  32728. +}
  32729. +
  32730. +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
  32731. +{
  32732. + int err, do_sio, wkq_err;
  32733. + struct user_namespace *userns;
  32734. +
  32735. + userns = mnt_user_ns(path->mnt);
  32736. + do_sio = au_test_h_perm_sio(userns, dir, MAY_EXEC | MAY_WRITE);
  32737. + if (!do_sio) {
  32738. + lockdep_off();
  32739. + err = vfsub_rmdir(dir, path);
  32740. + lockdep_on();
  32741. + } else {
  32742. + struct au_vfsub_rmdir_args args = {
  32743. + .errp = &err,
  32744. + .dir = dir,
  32745. + .path = path
  32746. + };
  32747. + wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
  32748. + if (unlikely(wkq_err))
  32749. + err = wkq_err;
  32750. + }
  32751. +
  32752. + return err;
  32753. +}
  32754. +
  32755. +/* ---------------------------------------------------------------------- */
  32756. +
  32757. +struct notify_change_args {
  32758. + int *errp;
  32759. + struct path *path;
  32760. + struct iattr *ia;
  32761. + struct inode **delegated_inode;
  32762. +};
  32763. +
  32764. +static void call_notify_change(void *args)
  32765. +{
  32766. + struct notify_change_args *a = args;
  32767. + struct inode *h_inode;
  32768. + struct user_namespace *userns;
  32769. +
  32770. + h_inode = d_inode(a->path->dentry);
  32771. + IMustLock(h_inode);
  32772. +
  32773. + *a->errp = -EPERM;
  32774. + if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
  32775. + userns = mnt_user_ns(a->path->mnt);
  32776. + lockdep_off();
  32777. + *a->errp = notify_change(userns, a->path->dentry, a->ia,
  32778. + a->delegated_inode);
  32779. + lockdep_on();
  32780. + if (!*a->errp)
  32781. + vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
  32782. + }
  32783. + AuTraceErr(*a->errp);
  32784. +}
  32785. +
  32786. +int vfsub_notify_change(struct path *path, struct iattr *ia,
  32787. + struct inode **delegated_inode)
  32788. +{
  32789. + int err;
  32790. + struct notify_change_args args = {
  32791. + .errp = &err,
  32792. + .path = path,
  32793. + .ia = ia,
  32794. + .delegated_inode = delegated_inode
  32795. + };
  32796. +
  32797. + call_notify_change(&args);
  32798. +
  32799. + return err;
  32800. +}
  32801. +
  32802. +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
  32803. + struct inode **delegated_inode)
  32804. +{
  32805. + int err, wkq_err;
  32806. + struct notify_change_args args = {
  32807. + .errp = &err,
  32808. + .path = path,
  32809. + .ia = ia,
  32810. + .delegated_inode = delegated_inode
  32811. + };
  32812. +
  32813. + wkq_err = au_wkq_wait(call_notify_change, &args);
  32814. + if (unlikely(wkq_err))
  32815. + err = wkq_err;
  32816. +
  32817. + return err;
  32818. +}
  32819. +
  32820. +/* ---------------------------------------------------------------------- */
  32821. +
  32822. +struct unlink_args {
  32823. + int *errp;
  32824. + struct inode *dir;
  32825. + struct path *path;
  32826. + struct inode **delegated_inode;
  32827. +};
  32828. +
  32829. +static void call_unlink(void *args)
  32830. +{
  32831. + struct unlink_args *a = args;
  32832. + struct dentry *d = a->path->dentry;
  32833. + struct inode *h_inode;
  32834. + struct user_namespace *userns;
  32835. + const int stop_sillyrename = (au_test_nfs(d->d_sb)
  32836. + && au_dcount(d) == 1);
  32837. +
  32838. + IMustLock(a->dir);
  32839. +
  32840. + a->path->dentry = d->d_parent;
  32841. + *a->errp = security_path_unlink(a->path, d);
  32842. + a->path->dentry = d;
  32843. + if (unlikely(*a->errp))
  32844. + return;
  32845. +
  32846. + if (!stop_sillyrename)
  32847. + dget(d);
  32848. + h_inode = NULL;
  32849. + if (d_is_positive(d)) {
  32850. + h_inode = d_inode(d);
  32851. + ihold(h_inode);
  32852. + }
  32853. +
  32854. + userns = mnt_user_ns(a->path->mnt);
  32855. + lockdep_off();
  32856. + *a->errp = vfs_unlink(userns, a->dir, d, a->delegated_inode);
  32857. + lockdep_on();
  32858. + if (!*a->errp) {
  32859. + struct path tmp = {
  32860. + .dentry = d->d_parent,
  32861. + .mnt = a->path->mnt
  32862. + };
  32863. + vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
  32864. + }
  32865. +
  32866. + if (!stop_sillyrename)
  32867. + dput(d);
  32868. + if (h_inode)
  32869. + iput(h_inode);
  32870. +
  32871. + AuTraceErr(*a->errp);
  32872. +}
  32873. +
  32874. +/*
  32875. + * @dir: must be locked.
  32876. + * @dentry: target dentry.
  32877. + */
  32878. +int vfsub_unlink(struct inode *dir, struct path *path,
  32879. + struct inode **delegated_inode, int force)
  32880. +{
  32881. + int err;
  32882. + struct unlink_args args = {
  32883. + .errp = &err,
  32884. + .dir = dir,
  32885. + .path = path,
  32886. + .delegated_inode = delegated_inode
  32887. + };
  32888. +
  32889. + if (!force)
  32890. + call_unlink(&args);
  32891. + else {
  32892. + int wkq_err;
  32893. +
  32894. + wkq_err = au_wkq_wait(call_unlink, &args);
  32895. + if (unlikely(wkq_err))
  32896. + err = wkq_err;
  32897. + }
  32898. +
  32899. + return err;
  32900. +}
  32901. diff -Naur null/fs/aufs/vfsub.h linux-5.15.131/fs/aufs/vfsub.h
  32902. --- /dev/null
  32903. +++ linux-5.15.131/fs/aufs/vfsub.h 2023-09-11 14:32:48.153722489 +0300
  32904. @@ -0,0 +1,358 @@
  32905. +/* SPDX-License-Identifier: GPL-2.0 */
  32906. +/*
  32907. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  32908. + *
  32909. + * This program, aufs is free software; you can redistribute it and/or modify
  32910. + * it under the terms of the GNU General Public License as published by
  32911. + * the Free Software Foundation; either version 2 of the License, or
  32912. + * (at your option) any later version.
  32913. + *
  32914. + * This program is distributed in the hope that it will be useful,
  32915. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32916. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32917. + * GNU General Public License for more details.
  32918. + *
  32919. + * You should have received a copy of the GNU General Public License
  32920. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  32921. + */
  32922. +
  32923. +/*
  32924. + * sub-routines for VFS
  32925. + */
  32926. +
  32927. +#ifndef __AUFS_VFSUB_H__
  32928. +#define __AUFS_VFSUB_H__
  32929. +
  32930. +#ifdef __KERNEL__
  32931. +
  32932. +#include <linux/fs.h>
  32933. +#include <linux/mount.h>
  32934. +#include <linux/posix_acl.h>
  32935. +#include <linux/xattr.h>
  32936. +#include "debug.h"
  32937. +
  32938. +/* copied from linux/fs/internal.h */
  32939. +/* todo: BAD approach!! */
  32940. +extern void __mnt_drop_write(struct vfsmount *);
  32941. +extern struct file *alloc_empty_file(int, const struct cred *);
  32942. +
  32943. +/* ---------------------------------------------------------------------- */
  32944. +
  32945. +/* lock subclass for lower inode */
  32946. +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
  32947. +/* reduce? gave up. */
  32948. +enum {
  32949. + AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
  32950. + AuLsc_I_PARENT, /* lower inode, parent first */
  32951. + AuLsc_I_PARENT2, /* copyup dirs */
  32952. + AuLsc_I_PARENT3, /* copyup wh */
  32953. + AuLsc_I_CHILD,
  32954. + AuLsc_I_CHILD2,
  32955. + AuLsc_I_End
  32956. +};
  32957. +
  32958. +/* to debug easier, do not make them inlined functions */
  32959. +#define MtxMustLock(mtx) AuDebugOn(!mutex_is_locked(mtx))
  32960. +#define IMustLock(i) AuDebugOn(!inode_is_locked(i))
  32961. +
  32962. +/* ---------------------------------------------------------------------- */
  32963. +
  32964. +static inline void vfsub_drop_nlink(struct inode *inode)
  32965. +{
  32966. + AuDebugOn(!inode->i_nlink);
  32967. + drop_nlink(inode);
  32968. +}
  32969. +
  32970. +static inline void vfsub_dead_dir(struct inode *inode)
  32971. +{
  32972. + AuDebugOn(!S_ISDIR(inode->i_mode));
  32973. + inode->i_flags |= S_DEAD;
  32974. + clear_nlink(inode);
  32975. +}
  32976. +
  32977. +static inline int vfsub_native_ro(struct inode *inode)
  32978. +{
  32979. + return sb_rdonly(inode->i_sb)
  32980. + || IS_RDONLY(inode)
  32981. + /* || IS_APPEND(inode) */
  32982. + || IS_IMMUTABLE(inode);
  32983. +}
  32984. +
  32985. +#ifdef CONFIG_AUFS_BR_FUSE
  32986. +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
  32987. +#else
  32988. +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
  32989. +#endif
  32990. +
  32991. +int vfsub_sync_filesystem(struct super_block *h_sb);
  32992. +
  32993. +/* ---------------------------------------------------------------------- */
  32994. +
  32995. +int vfsub_update_h_iattr(struct path *h_path, int *did);
  32996. +struct file *vfsub_dentry_open(struct path *path, int flags);
  32997. +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
  32998. +struct au_branch;
  32999. +struct vfsub_aopen_args {
  33000. + struct file *file;
  33001. + unsigned int open_flag;
  33002. + umode_t create_mode;
  33003. + struct au_branch *br;
  33004. +};
  33005. +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
  33006. + struct vfsub_aopen_args *args);
  33007. +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
  33008. +
  33009. +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
  33010. + struct path *ppath, int len);
  33011. +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
  33012. + int len);
  33013. +
  33014. +struct vfsub_lkup_one_args {
  33015. + struct dentry **errp;
  33016. + struct qstr *name;
  33017. + struct path *ppath;
  33018. +};
  33019. +
  33020. +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
  33021. + struct path *ppath)
  33022. +{
  33023. + return vfsub_lookup_one_len(name->name, ppath, name->len);
  33024. +}
  33025. +
  33026. +void vfsub_call_lkup_one(void *args);
  33027. +
  33028. +/* ---------------------------------------------------------------------- */
  33029. +
  33030. +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
  33031. +{
  33032. + int err;
  33033. +
  33034. + lockdep_off();
  33035. + err = mnt_want_write(mnt);
  33036. + lockdep_on();
  33037. + return err;
  33038. +}
  33039. +
  33040. +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
  33041. +{
  33042. + lockdep_off();
  33043. + mnt_drop_write(mnt);
  33044. + lockdep_on();
  33045. +}
  33046. +
  33047. +#if 0 /* reserved */
  33048. +static inline void vfsub_mnt_drop_write_file(struct file *file)
  33049. +{
  33050. + lockdep_off();
  33051. + mnt_drop_write_file(file);
  33052. + lockdep_on();
  33053. +}
  33054. +#endif
  33055. +
  33056. +/* ---------------------------------------------------------------------- */
  33057. +
  33058. +struct au_hinode;
  33059. +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
  33060. + struct dentry *d2, struct au_hinode *hdir2);
  33061. +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
  33062. + struct dentry *d2, struct au_hinode *hdir2);
  33063. +
  33064. +int vfsub_create(struct inode *dir, struct path *path, int mode,
  33065. + bool want_excl);
  33066. +int vfsub_symlink(struct inode *dir, struct path *path,
  33067. + const char *symname);
  33068. +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
  33069. +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
  33070. + struct path *path, struct inode **delegated_inode);
  33071. +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
  33072. + struct inode *hdir, struct path *path,
  33073. + struct inode **delegated_inode, unsigned int flags);
  33074. +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
  33075. +int vfsub_rmdir(struct inode *dir, struct path *path);
  33076. +
  33077. +/* ---------------------------------------------------------------------- */
  33078. +
  33079. +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
  33080. + loff_t *ppos);
  33081. +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
  33082. + loff_t *ppos);
  33083. +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
  33084. + loff_t *ppos);
  33085. +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
  33086. + loff_t *ppos);
  33087. +int vfsub_flush(struct file *file, fl_owner_t id);
  33088. +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
  33089. +
  33090. +static inline loff_t vfsub_f_size_read(struct file *file)
  33091. +{
  33092. + return i_size_read(file_inode(file));
  33093. +}
  33094. +
  33095. +static inline unsigned int vfsub_file_flags(struct file *file)
  33096. +{
  33097. + unsigned int flags;
  33098. +
  33099. + spin_lock(&file->f_lock);
  33100. + flags = file->f_flags;
  33101. + spin_unlock(&file->f_lock);
  33102. +
  33103. + return flags;
  33104. +}
  33105. +
  33106. +static inline int vfsub_file_execed(struct file *file)
  33107. +{
  33108. + /* todo: direct access f_flags */
  33109. + return !!(vfsub_file_flags(file) & __FMODE_EXEC);
  33110. +}
  33111. +
  33112. +#if 0 /* reserved */
  33113. +static inline void vfsub_file_accessed(struct file *h_file)
  33114. +{
  33115. + file_accessed(h_file);
  33116. + vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
  33117. +}
  33118. +#endif
  33119. +
  33120. +#if 0 /* reserved */
  33121. +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
  33122. + struct dentry *h_dentry)
  33123. +{
  33124. + struct path h_path = {
  33125. + .dentry = h_dentry,
  33126. + .mnt = h_mnt
  33127. + };
  33128. + touch_atime(&h_path);
  33129. + vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
  33130. +}
  33131. +#endif
  33132. +
  33133. +static inline int vfsub_update_time(struct inode *h_inode,
  33134. + struct timespec64 *ts, int flags)
  33135. +{
  33136. + return inode_update_time(h_inode, ts, flags);
  33137. + /* no vfsub_update_h_iattr() since we don't have struct path */
  33138. +}
  33139. +
  33140. +#ifdef CONFIG_FS_POSIX_ACL
  33141. +static inline int vfsub_acl_chmod(struct user_namespace *h_userns,
  33142. + struct inode *h_inode, umode_t h_mode)
  33143. +{
  33144. + int err;
  33145. +
  33146. + err = posix_acl_chmod(h_userns, h_inode, h_mode);
  33147. + if (err == -EOPNOTSUPP)
  33148. + err = 0;
  33149. + return err;
  33150. +}
  33151. +#else
  33152. +AuStubInt0(vfsub_acl_chmod, struct user_namespace *h_userns,
  33153. + struct inode *h_inode, umode_t h_mode);
  33154. +#endif
  33155. +
  33156. +long vfsub_splice_to(struct file *in, loff_t *ppos,
  33157. + struct pipe_inode_info *pipe, size_t len,
  33158. + unsigned int flags);
  33159. +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
  33160. + loff_t *ppos, size_t len, unsigned int flags);
  33161. +
  33162. +static inline long vfsub_truncate(struct path *path, loff_t length)
  33163. +{
  33164. + long err;
  33165. +
  33166. + lockdep_off();
  33167. + err = vfs_truncate(path, length);
  33168. + lockdep_on();
  33169. + return err;
  33170. +}
  33171. +
  33172. +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
  33173. + struct file *h_file);
  33174. +int vfsub_fsync(struct file *file, struct path *path, int datasync);
  33175. +
  33176. +/*
  33177. + * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
  33178. + * ioctl.
  33179. + */
  33180. +static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
  33181. + loff_t len)
  33182. +{
  33183. + loff_t err;
  33184. +
  33185. + lockdep_off();
  33186. + err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
  33187. + lockdep_on();
  33188. +
  33189. + return err;
  33190. +}
  33191. +
  33192. +/* copy_file_range(2) is a systemcall */
  33193. +static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
  33194. + struct file *dst, loff_t dst_pos,
  33195. + size_t len, unsigned int flags)
  33196. +{
  33197. + ssize_t ssz;
  33198. +
  33199. + lockdep_off();
  33200. + ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
  33201. + lockdep_on();
  33202. +
  33203. + return ssz;
  33204. +}
  33205. +
  33206. +/* ---------------------------------------------------------------------- */
  33207. +
  33208. +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
  33209. +{
  33210. + loff_t err;
  33211. +
  33212. + lockdep_off();
  33213. + err = vfs_llseek(file, offset, origin);
  33214. + lockdep_on();
  33215. + return err;
  33216. +}
  33217. +
  33218. +/* ---------------------------------------------------------------------- */
  33219. +
  33220. +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
  33221. +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
  33222. +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
  33223. + struct inode **delegated_inode);
  33224. +int vfsub_notify_change(struct path *path, struct iattr *ia,
  33225. + struct inode **delegated_inode);
  33226. +int vfsub_unlink(struct inode *dir, struct path *path,
  33227. + struct inode **delegated_inode, int force);
  33228. +
  33229. +static inline int vfsub_getattr(const struct path *path, struct kstat *st)
  33230. +{
  33231. + return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
  33232. +}
  33233. +
  33234. +/* ---------------------------------------------------------------------- */
  33235. +
  33236. +static inline int vfsub_setxattr(struct user_namespace *userns,
  33237. + struct dentry *dentry, const char *name,
  33238. + const void *value, size_t size, int flags)
  33239. +{
  33240. + int err;
  33241. +
  33242. + lockdep_off();
  33243. + err = vfs_setxattr(userns, dentry, name, value, size, flags);
  33244. + lockdep_on();
  33245. +
  33246. + return err;
  33247. +}
  33248. +
  33249. +static inline int vfsub_removexattr(struct user_namespace *userns,
  33250. + struct dentry *dentry, const char *name)
  33251. +{
  33252. + int err;
  33253. +
  33254. + lockdep_off();
  33255. + err = vfs_removexattr(userns, dentry, name);
  33256. + lockdep_on();
  33257. +
  33258. + return err;
  33259. +}
  33260. +
  33261. +#endif /* __KERNEL__ */
  33262. +#endif /* __AUFS_VFSUB_H__ */
  33263. diff -Naur null/fs/aufs/wbr_policy.c linux-5.15.131/fs/aufs/wbr_policy.c
  33264. --- /dev/null
  33265. +++ linux-5.15.131/fs/aufs/wbr_policy.c 2023-09-11 14:32:48.153722489 +0300
  33266. @@ -0,0 +1,830 @@
  33267. +// SPDX-License-Identifier: GPL-2.0
  33268. +/*
  33269. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  33270. + *
  33271. + * This program, aufs is free software; you can redistribute it and/or modify
  33272. + * it under the terms of the GNU General Public License as published by
  33273. + * the Free Software Foundation; either version 2 of the License, or
  33274. + * (at your option) any later version.
  33275. + *
  33276. + * This program is distributed in the hope that it will be useful,
  33277. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33278. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33279. + * GNU General Public License for more details.
  33280. + *
  33281. + * You should have received a copy of the GNU General Public License
  33282. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  33283. + */
  33284. +
  33285. +/*
  33286. + * policies for selecting one among multiple writable branches
  33287. + */
  33288. +
  33289. +#include <linux/statfs.h>
  33290. +#include "aufs.h"
  33291. +
  33292. +/* subset of cpup_attr() */
  33293. +static noinline_for_stack
  33294. +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
  33295. +{
  33296. + int err, sbits;
  33297. + struct iattr ia;
  33298. + struct inode *h_isrc;
  33299. +
  33300. + h_isrc = d_inode(h_src);
  33301. + ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
  33302. + ia.ia_mode = h_isrc->i_mode;
  33303. + ia.ia_uid = h_isrc->i_uid;
  33304. + ia.ia_gid = h_isrc->i_gid;
  33305. + sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
  33306. + au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
  33307. + /* no delegation since it is just created */
  33308. + err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
  33309. +
  33310. + /* is this nfs only? */
  33311. + if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
  33312. + ia.ia_valid = ATTR_FORCE | ATTR_MODE;
  33313. + ia.ia_mode = h_isrc->i_mode;
  33314. + err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
  33315. + }
  33316. +
  33317. + return err;
  33318. +}
  33319. +
  33320. +#define AuCpdown_PARENT_OPQ 1
  33321. +#define AuCpdown_WHED (1 << 1)
  33322. +#define AuCpdown_MADE_DIR (1 << 2)
  33323. +#define AuCpdown_DIROPQ (1 << 3)
  33324. +#define au_ftest_cpdown(flags, name) ((flags) & AuCpdown_##name)
  33325. +#define au_fset_cpdown(flags, name) \
  33326. + do { (flags) |= AuCpdown_##name; } while (0)
  33327. +#define au_fclr_cpdown(flags, name) \
  33328. + do { (flags) &= ~AuCpdown_##name; } while (0)
  33329. +
  33330. +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
  33331. + unsigned int *flags)
  33332. +{
  33333. + int err;
  33334. + struct dentry *opq_dentry;
  33335. +
  33336. + opq_dentry = au_diropq_create(dentry, bdst);
  33337. + err = PTR_ERR(opq_dentry);
  33338. + if (IS_ERR(opq_dentry))
  33339. + goto out;
  33340. + dput(opq_dentry);
  33341. + au_fset_cpdown(*flags, DIROPQ);
  33342. +
  33343. +out:
  33344. + return err;
  33345. +}
  33346. +
  33347. +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
  33348. + struct inode *dir, aufs_bindex_t bdst)
  33349. +{
  33350. + int err;
  33351. + struct path h_path;
  33352. + struct au_branch *br;
  33353. +
  33354. + br = au_sbr(dentry->d_sb, bdst);
  33355. + h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
  33356. + err = PTR_ERR(h_path.dentry);
  33357. + if (IS_ERR(h_path.dentry))
  33358. + goto out;
  33359. +
  33360. + err = 0;
  33361. + if (d_is_positive(h_path.dentry)) {
  33362. + h_path.mnt = au_br_mnt(br);
  33363. + err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
  33364. + dentry);
  33365. + }
  33366. + dput(h_path.dentry);
  33367. +
  33368. +out:
  33369. + return err;
  33370. +}
  33371. +
  33372. +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
  33373. + struct au_pin *pin,
  33374. + struct dentry *h_parent, void *arg)
  33375. +{
  33376. + int err, rerr;
  33377. + aufs_bindex_t bopq, btop;
  33378. + struct path h_path;
  33379. + struct dentry *parent;
  33380. + struct inode *h_dir, *h_inode, *inode, *dir;
  33381. + unsigned int *flags = arg;
  33382. +
  33383. + btop = au_dbtop(dentry);
  33384. + /* dentry is di-locked */
  33385. + parent = dget_parent(dentry);
  33386. + dir = d_inode(parent);
  33387. + h_dir = d_inode(h_parent);
  33388. + AuDebugOn(h_dir != au_h_iptr(dir, bdst));
  33389. + IMustLock(h_dir);
  33390. +
  33391. + err = au_lkup_neg(dentry, bdst, /*wh*/0);
  33392. + if (unlikely(err < 0))
  33393. + goto out;
  33394. + h_path.dentry = au_h_dptr(dentry, bdst);
  33395. + h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
  33396. + err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
  33397. + if (unlikely(err))
  33398. + goto out_put;
  33399. + au_fset_cpdown(*flags, MADE_DIR);
  33400. +
  33401. + bopq = au_dbdiropq(dentry);
  33402. + au_fclr_cpdown(*flags, WHED);
  33403. + au_fclr_cpdown(*flags, DIROPQ);
  33404. + if (au_dbwh(dentry) == bdst)
  33405. + au_fset_cpdown(*flags, WHED);
  33406. + if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
  33407. + au_fset_cpdown(*flags, PARENT_OPQ);
  33408. + h_inode = d_inode(h_path.dentry);
  33409. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  33410. + if (au_ftest_cpdown(*flags, WHED)) {
  33411. + err = au_cpdown_dir_opq(dentry, bdst, flags);
  33412. + if (unlikely(err)) {
  33413. + inode_unlock(h_inode);
  33414. + goto out_dir;
  33415. + }
  33416. + }
  33417. +
  33418. + err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
  33419. + inode_unlock(h_inode);
  33420. + if (unlikely(err))
  33421. + goto out_opq;
  33422. +
  33423. + if (au_ftest_cpdown(*flags, WHED)) {
  33424. + err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
  33425. + if (unlikely(err))
  33426. + goto out_opq;
  33427. + }
  33428. +
  33429. + inode = d_inode(dentry);
  33430. + if (au_ibbot(inode) < bdst)
  33431. + au_set_ibbot(inode, bdst);
  33432. + au_set_h_iptr(inode, bdst, au_igrab(h_inode),
  33433. + au_hi_flags(inode, /*isdir*/1));
  33434. + au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
  33435. + goto out; /* success */
  33436. +
  33437. + /* revert */
  33438. +out_opq:
  33439. + if (au_ftest_cpdown(*flags, DIROPQ)) {
  33440. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  33441. + rerr = au_diropq_remove(dentry, bdst);
  33442. + inode_unlock(h_inode);
  33443. + if (unlikely(rerr)) {
  33444. + AuIOErr("failed removing diropq for %pd b%d (%d)\n",
  33445. + dentry, bdst, rerr);
  33446. + err = -EIO;
  33447. + goto out;
  33448. + }
  33449. + }
  33450. +out_dir:
  33451. + if (au_ftest_cpdown(*flags, MADE_DIR)) {
  33452. + rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
  33453. + if (unlikely(rerr)) {
  33454. + AuIOErr("failed removing %pd b%d (%d)\n",
  33455. + dentry, bdst, rerr);
  33456. + err = -EIO;
  33457. + }
  33458. + }
  33459. +out_put:
  33460. + au_set_h_dptr(dentry, bdst, NULL);
  33461. + if (au_dbbot(dentry) == bdst)
  33462. + au_update_dbbot(dentry);
  33463. +out:
  33464. + dput(parent);
  33465. + return err;
  33466. +}
  33467. +
  33468. +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
  33469. +{
  33470. + int err;
  33471. + unsigned int flags;
  33472. +
  33473. + flags = 0;
  33474. + err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
  33475. +
  33476. + return err;
  33477. +}
  33478. +
  33479. +/* ---------------------------------------------------------------------- */
  33480. +
  33481. +/* policies for create */
  33482. +
  33483. +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
  33484. +{
  33485. + int err, i, j, ndentry;
  33486. + aufs_bindex_t bopq;
  33487. + struct au_dcsub_pages dpages;
  33488. + struct au_dpage *dpage;
  33489. + struct dentry **dentries, *parent, *d;
  33490. +
  33491. + err = au_dpages_init(&dpages, GFP_NOFS);
  33492. + if (unlikely(err))
  33493. + goto out;
  33494. + parent = dget_parent(dentry);
  33495. + err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
  33496. + if (unlikely(err))
  33497. + goto out_free;
  33498. +
  33499. + err = bindex;
  33500. + for (i = 0; i < dpages.ndpage; i++) {
  33501. + dpage = dpages.dpages + i;
  33502. + dentries = dpage->dentries;
  33503. + ndentry = dpage->ndentry;
  33504. + for (j = 0; j < ndentry; j++) {
  33505. + d = dentries[j];
  33506. + di_read_lock_parent2(d, !AuLock_IR);
  33507. + bopq = au_dbdiropq(d);
  33508. + di_read_unlock(d, !AuLock_IR);
  33509. + if (bopq >= 0 && bopq < err)
  33510. + err = bopq;
  33511. + }
  33512. + }
  33513. +
  33514. +out_free:
  33515. + dput(parent);
  33516. + au_dpages_free(&dpages);
  33517. +out:
  33518. + return err;
  33519. +}
  33520. +
  33521. +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
  33522. +{
  33523. + for (; bindex >= 0; bindex--)
  33524. + if (!au_br_rdonly(au_sbr(sb, bindex)))
  33525. + return bindex;
  33526. + return -EROFS;
  33527. +}
  33528. +
  33529. +/* top down parent */
  33530. +static int au_wbr_create_tdp(struct dentry *dentry,
  33531. + unsigned int flags __maybe_unused)
  33532. +{
  33533. + int err;
  33534. + aufs_bindex_t btop, bindex;
  33535. + struct super_block *sb;
  33536. + struct dentry *parent, *h_parent;
  33537. +
  33538. + sb = dentry->d_sb;
  33539. + btop = au_dbtop(dentry);
  33540. + err = btop;
  33541. + if (!au_br_rdonly(au_sbr(sb, btop)))
  33542. + goto out;
  33543. +
  33544. + err = -EROFS;
  33545. + parent = dget_parent(dentry);
  33546. + for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
  33547. + h_parent = au_h_dptr(parent, bindex);
  33548. + if (!h_parent || d_is_negative(h_parent))
  33549. + continue;
  33550. +
  33551. + if (!au_br_rdonly(au_sbr(sb, bindex))) {
  33552. + err = bindex;
  33553. + break;
  33554. + }
  33555. + }
  33556. + dput(parent);
  33557. +
  33558. + /* bottom up here */
  33559. + if (unlikely(err < 0)) {
  33560. + err = au_wbr_bu(sb, btop - 1);
  33561. + if (err >= 0)
  33562. + err = au_wbr_nonopq(dentry, err);
  33563. + }
  33564. +
  33565. +out:
  33566. + AuDbg("b%d\n", err);
  33567. + return err;
  33568. +}
  33569. +
  33570. +/* ---------------------------------------------------------------------- */
  33571. +
  33572. +/* an exception for the policy other than tdp */
  33573. +static int au_wbr_create_exp(struct dentry *dentry)
  33574. +{
  33575. + int err;
  33576. + aufs_bindex_t bwh, bdiropq;
  33577. + struct dentry *parent;
  33578. +
  33579. + err = -1;
  33580. + bwh = au_dbwh(dentry);
  33581. + parent = dget_parent(dentry);
  33582. + bdiropq = au_dbdiropq(parent);
  33583. + if (bwh >= 0) {
  33584. + if (bdiropq >= 0)
  33585. + err = min(bdiropq, bwh);
  33586. + else
  33587. + err = bwh;
  33588. + AuDbg("%d\n", err);
  33589. + } else if (bdiropq >= 0) {
  33590. + err = bdiropq;
  33591. + AuDbg("%d\n", err);
  33592. + }
  33593. + dput(parent);
  33594. +
  33595. + if (err >= 0)
  33596. + err = au_wbr_nonopq(dentry, err);
  33597. +
  33598. + if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
  33599. + err = -1;
  33600. +
  33601. + AuDbg("%d\n", err);
  33602. + return err;
  33603. +}
  33604. +
  33605. +/* ---------------------------------------------------------------------- */
  33606. +
  33607. +/* round robin */
  33608. +static int au_wbr_create_init_rr(struct super_block *sb)
  33609. +{
  33610. + int err;
  33611. +
  33612. + err = au_wbr_bu(sb, au_sbbot(sb));
  33613. + atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
  33614. + /* smp_mb(); */
  33615. +
  33616. + AuDbg("b%d\n", err);
  33617. + return err;
  33618. +}
  33619. +
  33620. +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
  33621. +{
  33622. + int err, nbr;
  33623. + unsigned int u;
  33624. + aufs_bindex_t bindex, bbot;
  33625. + struct super_block *sb;
  33626. + atomic_t *next;
  33627. +
  33628. + err = au_wbr_create_exp(dentry);
  33629. + if (err >= 0)
  33630. + goto out;
  33631. +
  33632. + sb = dentry->d_sb;
  33633. + next = &au_sbi(sb)->si_wbr_rr_next;
  33634. + bbot = au_sbbot(sb);
  33635. + nbr = bbot + 1;
  33636. + for (bindex = 0; bindex <= bbot; bindex++) {
  33637. + if (!au_ftest_wbr(flags, DIR)) {
  33638. + err = atomic_dec_return(next) + 1;
  33639. + /* modulo for 0 is meaningless */
  33640. + if (unlikely(!err))
  33641. + err = atomic_dec_return(next) + 1;
  33642. + } else
  33643. + err = atomic_read(next);
  33644. + AuDbg("%d\n", err);
  33645. + u = err;
  33646. + err = u % nbr;
  33647. + AuDbg("%d\n", err);
  33648. + if (!au_br_rdonly(au_sbr(sb, err)))
  33649. + break;
  33650. + err = -EROFS;
  33651. + }
  33652. +
  33653. + if (err >= 0)
  33654. + err = au_wbr_nonopq(dentry, err);
  33655. +
  33656. +out:
  33657. + AuDbg("%d\n", err);
  33658. + return err;
  33659. +}
  33660. +
  33661. +/* ---------------------------------------------------------------------- */
  33662. +
  33663. +/* most free space */
  33664. +static void au_mfs(struct dentry *dentry, struct dentry *parent)
  33665. +{
  33666. + struct super_block *sb;
  33667. + struct au_branch *br;
  33668. + struct au_wbr_mfs *mfs;
  33669. + struct dentry *h_parent;
  33670. + aufs_bindex_t bindex, bbot;
  33671. + int err;
  33672. + unsigned long long b, bavail;
  33673. + struct path h_path;
  33674. + /* reduce the stack usage */
  33675. + struct kstatfs *st;
  33676. +
  33677. + st = kmalloc(sizeof(*st), GFP_NOFS);
  33678. + if (unlikely(!st)) {
  33679. + AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
  33680. + return;
  33681. + }
  33682. +
  33683. + bavail = 0;
  33684. + sb = dentry->d_sb;
  33685. + mfs = &au_sbi(sb)->si_wbr_mfs;
  33686. + MtxMustLock(&mfs->mfs_lock);
  33687. + mfs->mfs_bindex = -EROFS;
  33688. + mfs->mfsrr_bytes = 0;
  33689. + if (!parent) {
  33690. + bindex = 0;
  33691. + bbot = au_sbbot(sb);
  33692. + } else {
  33693. + bindex = au_dbtop(parent);
  33694. + bbot = au_dbtaildir(parent);
  33695. + }
  33696. +
  33697. + for (; bindex <= bbot; bindex++) {
  33698. + if (parent) {
  33699. + h_parent = au_h_dptr(parent, bindex);
  33700. + if (!h_parent || d_is_negative(h_parent))
  33701. + continue;
  33702. + }
  33703. + br = au_sbr(sb, bindex);
  33704. + if (au_br_rdonly(br))
  33705. + continue;
  33706. +
  33707. + /* sb->s_root for NFS is unreliable */
  33708. + h_path.mnt = au_br_mnt(br);
  33709. + h_path.dentry = h_path.mnt->mnt_root;
  33710. + err = vfs_statfs(&h_path, st);
  33711. + if (unlikely(err)) {
  33712. + AuWarn1("failed statfs, b%d, %d\n", bindex, err);
  33713. + continue;
  33714. + }
  33715. +
  33716. + /* when the available size is equal, select the lower one */
  33717. + BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
  33718. + || sizeof(b) < sizeof(st->f_bsize));
  33719. + b = st->f_bavail * st->f_bsize;
  33720. + br->br_wbr->wbr_bytes = b;
  33721. + if (b >= bavail) {
  33722. + bavail = b;
  33723. + mfs->mfs_bindex = bindex;
  33724. + mfs->mfs_jiffy = jiffies;
  33725. + }
  33726. + }
  33727. +
  33728. + mfs->mfsrr_bytes = bavail;
  33729. + AuDbg("b%d\n", mfs->mfs_bindex);
  33730. + au_kfree_rcu(st);
  33731. +}
  33732. +
  33733. +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
  33734. +{
  33735. + int err;
  33736. + struct dentry *parent;
  33737. + struct super_block *sb;
  33738. + struct au_wbr_mfs *mfs;
  33739. +
  33740. + err = au_wbr_create_exp(dentry);
  33741. + if (err >= 0)
  33742. + goto out;
  33743. +
  33744. + sb = dentry->d_sb;
  33745. + parent = NULL;
  33746. + if (au_ftest_wbr(flags, PARENT))
  33747. + parent = dget_parent(dentry);
  33748. + mfs = &au_sbi(sb)->si_wbr_mfs;
  33749. + mutex_lock(&mfs->mfs_lock);
  33750. + if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
  33751. + || mfs->mfs_bindex < 0
  33752. + || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
  33753. + au_mfs(dentry, parent);
  33754. + mutex_unlock(&mfs->mfs_lock);
  33755. + err = mfs->mfs_bindex;
  33756. + dput(parent);
  33757. +
  33758. + if (err >= 0)
  33759. + err = au_wbr_nonopq(dentry, err);
  33760. +
  33761. +out:
  33762. + AuDbg("b%d\n", err);
  33763. + return err;
  33764. +}
  33765. +
  33766. +static int au_wbr_create_init_mfs(struct super_block *sb)
  33767. +{
  33768. + struct au_wbr_mfs *mfs;
  33769. +
  33770. + mfs = &au_sbi(sb)->si_wbr_mfs;
  33771. + mutex_init(&mfs->mfs_lock);
  33772. + mfs->mfs_jiffy = 0;
  33773. + mfs->mfs_bindex = -EROFS;
  33774. +
  33775. + return 0;
  33776. +}
  33777. +
  33778. +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
  33779. +{
  33780. + mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
  33781. + return 0;
  33782. +}
  33783. +
  33784. +/* ---------------------------------------------------------------------- */
  33785. +
  33786. +/* top down regardless parent, and then mfs */
  33787. +static int au_wbr_create_tdmfs(struct dentry *dentry,
  33788. + unsigned int flags __maybe_unused)
  33789. +{
  33790. + int err;
  33791. + aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
  33792. + unsigned long long watermark;
  33793. + struct super_block *sb;
  33794. + struct au_wbr_mfs *mfs;
  33795. + struct au_branch *br;
  33796. + struct dentry *parent;
  33797. +
  33798. + sb = dentry->d_sb;
  33799. + mfs = &au_sbi(sb)->si_wbr_mfs;
  33800. + mutex_lock(&mfs->mfs_lock);
  33801. + if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
  33802. + || mfs->mfs_bindex < 0)
  33803. + au_mfs(dentry, /*parent*/NULL);
  33804. + watermark = mfs->mfsrr_watermark;
  33805. + bmfs = mfs->mfs_bindex;
  33806. + mutex_unlock(&mfs->mfs_lock);
  33807. +
  33808. + /* another style of au_wbr_create_exp() */
  33809. + bwh = au_dbwh(dentry);
  33810. + parent = dget_parent(dentry);
  33811. + btail = au_dbtaildir(parent);
  33812. + if (bwh >= 0 && bwh < btail)
  33813. + btail = bwh;
  33814. +
  33815. + err = au_wbr_nonopq(dentry, btail);
  33816. + if (unlikely(err < 0))
  33817. + goto out;
  33818. + btail = err;
  33819. + bfound = -1;
  33820. + for (bindex = 0; bindex <= btail; bindex++) {
  33821. + br = au_sbr(sb, bindex);
  33822. + if (au_br_rdonly(br))
  33823. + continue;
  33824. + if (br->br_wbr->wbr_bytes > watermark) {
  33825. + bfound = bindex;
  33826. + break;
  33827. + }
  33828. + }
  33829. + err = bfound;
  33830. + if (err < 0)
  33831. + err = bmfs;
  33832. +
  33833. +out:
  33834. + dput(parent);
  33835. + AuDbg("b%d\n", err);
  33836. + return err;
  33837. +}
  33838. +
  33839. +/* ---------------------------------------------------------------------- */
  33840. +
  33841. +/* most free space and then round robin */
  33842. +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
  33843. +{
  33844. + int err;
  33845. + struct au_wbr_mfs *mfs;
  33846. +
  33847. + err = au_wbr_create_mfs(dentry, flags);
  33848. + if (err >= 0) {
  33849. + mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
  33850. + mutex_lock(&mfs->mfs_lock);
  33851. + if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
  33852. + err = au_wbr_create_rr(dentry, flags);
  33853. + mutex_unlock(&mfs->mfs_lock);
  33854. + }
  33855. +
  33856. + AuDbg("b%d\n", err);
  33857. + return err;
  33858. +}
  33859. +
  33860. +static int au_wbr_create_init_mfsrr(struct super_block *sb)
  33861. +{
  33862. + int err;
  33863. +
  33864. + au_wbr_create_init_mfs(sb); /* ignore */
  33865. + err = au_wbr_create_init_rr(sb);
  33866. +
  33867. + return err;
  33868. +}
  33869. +
  33870. +/* ---------------------------------------------------------------------- */
  33871. +
  33872. +/* top down parent and most free space */
  33873. +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
  33874. +{
  33875. + int err, e2;
  33876. + unsigned long long b;
  33877. + aufs_bindex_t bindex, btop, bbot;
  33878. + struct super_block *sb;
  33879. + struct dentry *parent, *h_parent;
  33880. + struct au_branch *br;
  33881. +
  33882. + err = au_wbr_create_tdp(dentry, flags);
  33883. + if (unlikely(err < 0))
  33884. + goto out;
  33885. + parent = dget_parent(dentry);
  33886. + btop = au_dbtop(parent);
  33887. + bbot = au_dbtaildir(parent);
  33888. + if (btop == bbot)
  33889. + goto out_parent; /* success */
  33890. +
  33891. + e2 = au_wbr_create_mfs(dentry, flags);
  33892. + if (e2 < 0)
  33893. + goto out_parent; /* success */
  33894. +
  33895. + /* when the available size is equal, select upper one */
  33896. + sb = dentry->d_sb;
  33897. + br = au_sbr(sb, err);
  33898. + b = br->br_wbr->wbr_bytes;
  33899. + AuDbg("b%d, %llu\n", err, b);
  33900. +
  33901. + for (bindex = btop; bindex <= bbot; bindex++) {
  33902. + h_parent = au_h_dptr(parent, bindex);
  33903. + if (!h_parent || d_is_negative(h_parent))
  33904. + continue;
  33905. +
  33906. + br = au_sbr(sb, bindex);
  33907. + if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
  33908. + b = br->br_wbr->wbr_bytes;
  33909. + err = bindex;
  33910. + AuDbg("b%d, %llu\n", err, b);
  33911. + }
  33912. + }
  33913. +
  33914. + if (err >= 0)
  33915. + err = au_wbr_nonopq(dentry, err);
  33916. +
  33917. +out_parent:
  33918. + dput(parent);
  33919. +out:
  33920. + AuDbg("b%d\n", err);
  33921. + return err;
  33922. +}
  33923. +
  33924. +/* ---------------------------------------------------------------------- */
  33925. +
  33926. +/*
  33927. + * - top down parent
  33928. + * - most free space with parent
  33929. + * - most free space round-robin regardless parent
  33930. + */
  33931. +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
  33932. +{
  33933. + int err;
  33934. + unsigned long long watermark;
  33935. + struct super_block *sb;
  33936. + struct au_branch *br;
  33937. + struct au_wbr_mfs *mfs;
  33938. +
  33939. + err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
  33940. + if (unlikely(err < 0))
  33941. + goto out;
  33942. +
  33943. + sb = dentry->d_sb;
  33944. + br = au_sbr(sb, err);
  33945. + mfs = &au_sbi(sb)->si_wbr_mfs;
  33946. + mutex_lock(&mfs->mfs_lock);
  33947. + watermark = mfs->mfsrr_watermark;
  33948. + mutex_unlock(&mfs->mfs_lock);
  33949. + if (br->br_wbr->wbr_bytes < watermark)
  33950. + /* regardless the parent dir */
  33951. + err = au_wbr_create_mfsrr(dentry, flags);
  33952. +
  33953. +out:
  33954. + AuDbg("b%d\n", err);
  33955. + return err;
  33956. +}
  33957. +
  33958. +/* ---------------------------------------------------------------------- */
  33959. +
  33960. +/* policies for copyup */
  33961. +
  33962. +/* top down parent */
  33963. +static int au_wbr_copyup_tdp(struct dentry *dentry)
  33964. +{
  33965. + return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
  33966. +}
  33967. +
  33968. +/* bottom up parent */
  33969. +static int au_wbr_copyup_bup(struct dentry *dentry)
  33970. +{
  33971. + int err;
  33972. + aufs_bindex_t bindex, btop;
  33973. + struct dentry *parent, *h_parent;
  33974. + struct super_block *sb;
  33975. +
  33976. + err = -EROFS;
  33977. + sb = dentry->d_sb;
  33978. + parent = dget_parent(dentry);
  33979. + btop = au_dbtop(parent);
  33980. + for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
  33981. + h_parent = au_h_dptr(parent, bindex);
  33982. + if (!h_parent || d_is_negative(h_parent))
  33983. + continue;
  33984. +
  33985. + if (!au_br_rdonly(au_sbr(sb, bindex))) {
  33986. + err = bindex;
  33987. + break;
  33988. + }
  33989. + }
  33990. + dput(parent);
  33991. +
  33992. + /* bottom up here */
  33993. + if (unlikely(err < 0))
  33994. + err = au_wbr_bu(sb, btop - 1);
  33995. +
  33996. + AuDbg("b%d\n", err);
  33997. + return err;
  33998. +}
  33999. +
  34000. +/* bottom up */
  34001. +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
  34002. +{
  34003. + int err;
  34004. +
  34005. + err = au_wbr_bu(dentry->d_sb, btop);
  34006. + AuDbg("b%d\n", err);
  34007. + if (err > btop)
  34008. + err = au_wbr_nonopq(dentry, err);
  34009. +
  34010. + AuDbg("b%d\n", err);
  34011. + return err;
  34012. +}
  34013. +
  34014. +static int au_wbr_copyup_bu(struct dentry *dentry)
  34015. +{
  34016. + int err;
  34017. + aufs_bindex_t btop;
  34018. +
  34019. + btop = au_dbtop(dentry);
  34020. + err = au_wbr_do_copyup_bu(dentry, btop);
  34021. + return err;
  34022. +}
  34023. +
  34024. +/* ---------------------------------------------------------------------- */
  34025. +
  34026. +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
  34027. + [AuWbrCopyup_TDP] = {
  34028. + .copyup = au_wbr_copyup_tdp
  34029. + },
  34030. + [AuWbrCopyup_BUP] = {
  34031. + .copyup = au_wbr_copyup_bup
  34032. + },
  34033. + [AuWbrCopyup_BU] = {
  34034. + .copyup = au_wbr_copyup_bu
  34035. + }
  34036. +};
  34037. +
  34038. +struct au_wbr_create_operations au_wbr_create_ops[] = {
  34039. + [AuWbrCreate_TDP] = {
  34040. + .create = au_wbr_create_tdp
  34041. + },
  34042. + [AuWbrCreate_RR] = {
  34043. + .create = au_wbr_create_rr,
  34044. + .init = au_wbr_create_init_rr
  34045. + },
  34046. + [AuWbrCreate_MFS] = {
  34047. + .create = au_wbr_create_mfs,
  34048. + .init = au_wbr_create_init_mfs,
  34049. + .fin = au_wbr_create_fin_mfs
  34050. + },
  34051. + [AuWbrCreate_MFSV] = {
  34052. + .create = au_wbr_create_mfs,
  34053. + .init = au_wbr_create_init_mfs,
  34054. + .fin = au_wbr_create_fin_mfs
  34055. + },
  34056. + [AuWbrCreate_MFSRR] = {
  34057. + .create = au_wbr_create_mfsrr,
  34058. + .init = au_wbr_create_init_mfsrr,
  34059. + .fin = au_wbr_create_fin_mfs
  34060. + },
  34061. + [AuWbrCreate_MFSRRV] = {
  34062. + .create = au_wbr_create_mfsrr,
  34063. + .init = au_wbr_create_init_mfsrr,
  34064. + .fin = au_wbr_create_fin_mfs
  34065. + },
  34066. + [AuWbrCreate_TDMFS] = {
  34067. + .create = au_wbr_create_tdmfs,
  34068. + .init = au_wbr_create_init_mfs,
  34069. + .fin = au_wbr_create_fin_mfs
  34070. + },
  34071. + [AuWbrCreate_TDMFSV] = {
  34072. + .create = au_wbr_create_tdmfs,
  34073. + .init = au_wbr_create_init_mfs,
  34074. + .fin = au_wbr_create_fin_mfs
  34075. + },
  34076. + [AuWbrCreate_PMFS] = {
  34077. + .create = au_wbr_create_pmfs,
  34078. + .init = au_wbr_create_init_mfs,
  34079. + .fin = au_wbr_create_fin_mfs
  34080. + },
  34081. + [AuWbrCreate_PMFSV] = {
  34082. + .create = au_wbr_create_pmfs,
  34083. + .init = au_wbr_create_init_mfs,
  34084. + .fin = au_wbr_create_fin_mfs
  34085. + },
  34086. + [AuWbrCreate_PMFSRR] = {
  34087. + .create = au_wbr_create_pmfsrr,
  34088. + .init = au_wbr_create_init_mfsrr,
  34089. + .fin = au_wbr_create_fin_mfs
  34090. + },
  34091. + [AuWbrCreate_PMFSRRV] = {
  34092. + .create = au_wbr_create_pmfsrr,
  34093. + .init = au_wbr_create_init_mfsrr,
  34094. + .fin = au_wbr_create_fin_mfs
  34095. + }
  34096. +};
  34097. diff -Naur null/fs/aufs/whout.c linux-5.15.131/fs/aufs/whout.c
  34098. --- /dev/null
  34099. +++ linux-5.15.131/fs/aufs/whout.c 2023-09-11 14:32:48.153722489 +0300
  34100. @@ -0,0 +1,1072 @@
  34101. +// SPDX-License-Identifier: GPL-2.0
  34102. +/*
  34103. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  34104. + *
  34105. + * This program, aufs is free software; you can redistribute it and/or modify
  34106. + * it under the terms of the GNU General Public License as published by
  34107. + * the Free Software Foundation; either version 2 of the License, or
  34108. + * (at your option) any later version.
  34109. + *
  34110. + * This program is distributed in the hope that it will be useful,
  34111. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  34112. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34113. + * GNU General Public License for more details.
  34114. + *
  34115. + * You should have received a copy of the GNU General Public License
  34116. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  34117. + */
  34118. +
  34119. +/*
  34120. + * whiteout for logical deletion and opaque directory
  34121. + */
  34122. +
  34123. +#include "aufs.h"
  34124. +
  34125. +#define WH_MASK 0444
  34126. +
  34127. +/*
  34128. + * If a directory contains this file, then it is opaque. We start with the
  34129. + * .wh. flag so that it is blocked by lookup.
  34130. + */
  34131. +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
  34132. + sizeof(AUFS_WH_DIROPQ) - 1);
  34133. +
  34134. +/*
  34135. + * generate whiteout name, which is NOT terminated by NULL.
  34136. + * @name: original d_name.name
  34137. + * @len: original d_name.len
  34138. + * @wh: whiteout qstr
  34139. + * returns zero when succeeds, otherwise error.
  34140. + * succeeded value as wh->name should be freed by kfree().
  34141. + */
  34142. +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
  34143. +{
  34144. + char *p;
  34145. +
  34146. + if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
  34147. + return -ENAMETOOLONG;
  34148. +
  34149. + wh->len = name->len + AUFS_WH_PFX_LEN;
  34150. + p = kmalloc(wh->len, GFP_NOFS);
  34151. + wh->name = p;
  34152. + if (p) {
  34153. + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
  34154. + memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
  34155. + /* smp_mb(); */
  34156. + return 0;
  34157. + }
  34158. + return -ENOMEM;
  34159. +}
  34160. +
  34161. +/* ---------------------------------------------------------------------- */
  34162. +
  34163. +/*
  34164. + * test if the @wh_name exists under @h_ppath.
  34165. + * @try_sio specifies the necessary of super-io.
  34166. + */
  34167. +int au_wh_test(struct user_namespace *h_userns, struct path *h_ppath,
  34168. + struct qstr *wh_name, int try_sio)
  34169. +{
  34170. + int err;
  34171. + struct dentry *wh_dentry;
  34172. +
  34173. + if (!try_sio)
  34174. + wh_dentry = vfsub_lkup_one(wh_name, h_ppath);
  34175. + else
  34176. + wh_dentry = au_sio_lkup_one(h_userns, wh_name, h_ppath);
  34177. + err = PTR_ERR(wh_dentry);
  34178. + if (IS_ERR(wh_dentry)) {
  34179. + if (err == -ENAMETOOLONG)
  34180. + err = 0;
  34181. + goto out;
  34182. + }
  34183. +
  34184. + err = 0;
  34185. + if (d_is_negative(wh_dentry))
  34186. + goto out_wh; /* success */
  34187. +
  34188. + err = 1;
  34189. + if (d_is_reg(wh_dentry))
  34190. + goto out_wh; /* success */
  34191. +
  34192. + err = -EIO;
  34193. + AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
  34194. + wh_dentry, d_inode(wh_dentry)->i_mode);
  34195. +
  34196. +out_wh:
  34197. + dput(wh_dentry);
  34198. +out:
  34199. + return err;
  34200. +}
  34201. +
  34202. +/*
  34203. + * test if the @h_path->dentry sets opaque or not.
  34204. + */
  34205. +int au_diropq_test(struct user_namespace *h_userns, struct path *h_path)
  34206. +{
  34207. + int err;
  34208. + struct inode *h_dir;
  34209. +
  34210. + h_dir = d_inode(h_path->dentry);
  34211. + err = au_wh_test(h_userns, h_path, &diropq_name,
  34212. + au_test_h_perm_sio(h_userns, h_dir, MAY_EXEC));
  34213. + return err;
  34214. +}
  34215. +
  34216. +/*
  34217. + * returns a negative dentry whose name is unique and temporary.
  34218. + */
  34219. +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
  34220. + struct qstr *prefix)
  34221. +{
  34222. + struct dentry *dentry;
  34223. + int i;
  34224. + char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
  34225. + *name, *p;
  34226. + /* strict atomic_t is unnecessary here */
  34227. + static unsigned short cnt;
  34228. + struct qstr qs;
  34229. + struct path h_ppath;
  34230. + struct user_namespace *h_userns;
  34231. +
  34232. + BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
  34233. +
  34234. + name = defname;
  34235. + qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
  34236. + if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
  34237. + dentry = ERR_PTR(-ENAMETOOLONG);
  34238. + if (unlikely(qs.len > NAME_MAX))
  34239. + goto out;
  34240. + dentry = ERR_PTR(-ENOMEM);
  34241. + name = kmalloc(qs.len + 1, GFP_NOFS);
  34242. + if (unlikely(!name))
  34243. + goto out;
  34244. + }
  34245. +
  34246. + /* doubly whiteout-ed */
  34247. + memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
  34248. + p = name + AUFS_WH_PFX_LEN * 2;
  34249. + memcpy(p, prefix->name, prefix->len);
  34250. + p += prefix->len;
  34251. + *p++ = '.';
  34252. + AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
  34253. +
  34254. + h_ppath.dentry = h_parent;
  34255. + h_ppath.mnt = au_br_mnt(br);
  34256. + h_userns = au_br_userns(br);
  34257. + qs.name = name;
  34258. + for (i = 0; i < 3; i++) {
  34259. + sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
  34260. + dentry = au_sio_lkup_one(h_userns, &qs, &h_ppath);
  34261. + if (IS_ERR(dentry) || d_is_negative(dentry))
  34262. + goto out_name;
  34263. + dput(dentry);
  34264. + }
  34265. + /* pr_warn("could not get random name\n"); */
  34266. + dentry = ERR_PTR(-EEXIST);
  34267. + AuDbg("%.*s\n", AuLNPair(&qs));
  34268. + BUG();
  34269. +
  34270. +out_name:
  34271. + if (name != defname)
  34272. + au_kfree_try_rcu(name);
  34273. +out:
  34274. + AuTraceErrPtr(dentry);
  34275. + return dentry;
  34276. +}
  34277. +
  34278. +/*
  34279. + * rename the @h_dentry on @br to the whiteouted temporary name.
  34280. + */
  34281. +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
  34282. +{
  34283. + int err;
  34284. + struct path h_path = {
  34285. + .mnt = au_br_mnt(br)
  34286. + };
  34287. + struct inode *h_dir, *delegated;
  34288. + struct dentry *h_parent;
  34289. +
  34290. + h_parent = h_dentry->d_parent; /* dir inode is locked */
  34291. + h_dir = d_inode(h_parent);
  34292. + IMustLock(h_dir);
  34293. +
  34294. + h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
  34295. + err = PTR_ERR(h_path.dentry);
  34296. + if (IS_ERR(h_path.dentry))
  34297. + goto out;
  34298. +
  34299. + /* under the same dir, no need to lock_rename() */
  34300. + delegated = NULL;
  34301. + err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
  34302. + /*flags*/0);
  34303. + AuTraceErr(err);
  34304. + if (unlikely(err == -EWOULDBLOCK)) {
  34305. + pr_warn("cannot retry for NFSv4 delegation"
  34306. + " for an internal rename\n");
  34307. + iput(delegated);
  34308. + }
  34309. + dput(h_path.dentry);
  34310. +
  34311. +out:
  34312. + AuTraceErr(err);
  34313. + return err;
  34314. +}
  34315. +
  34316. +/* ---------------------------------------------------------------------- */
  34317. +/*
  34318. + * functions for removing a whiteout
  34319. + */
  34320. +
  34321. +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
  34322. +{
  34323. + int err, force;
  34324. + struct inode *delegated;
  34325. +
  34326. + /*
  34327. + * forces superio when the dir has a sticky bit.
  34328. + * this may be a violation of unix fs semantics.
  34329. + */
  34330. + force = (h_dir->i_mode & S_ISVTX)
  34331. + && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
  34332. + delegated = NULL;
  34333. + err = vfsub_unlink(h_dir, h_path, &delegated, force);
  34334. + if (unlikely(err == -EWOULDBLOCK)) {
  34335. + pr_warn("cannot retry for NFSv4 delegation"
  34336. + " for an internal unlink\n");
  34337. + iput(delegated);
  34338. + }
  34339. + return err;
  34340. +}
  34341. +
  34342. +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
  34343. + struct dentry *dentry)
  34344. +{
  34345. + int err;
  34346. +
  34347. + err = do_unlink_wh(h_dir, h_path);
  34348. + if (!err && dentry)
  34349. + au_set_dbwh(dentry, -1);
  34350. +
  34351. + return err;
  34352. +}
  34353. +
  34354. +static int unlink_wh_name(struct path *h_ppath, struct qstr *wh)
  34355. +{
  34356. + int err;
  34357. + struct path h_path;
  34358. +
  34359. + err = 0;
  34360. + h_path.dentry = vfsub_lkup_one(wh, h_ppath);
  34361. + if (IS_ERR(h_path.dentry))
  34362. + err = PTR_ERR(h_path.dentry);
  34363. + else {
  34364. + if (d_is_reg(h_path.dentry)) {
  34365. + h_path.mnt = h_ppath->mnt;
  34366. + err = do_unlink_wh(d_inode(h_ppath->dentry), &h_path);
  34367. + }
  34368. + dput(h_path.dentry);
  34369. + }
  34370. +
  34371. + return err;
  34372. +}
  34373. +
  34374. +/* ---------------------------------------------------------------------- */
  34375. +/*
  34376. + * initialize/clean whiteout for a branch
  34377. + */
  34378. +
  34379. +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
  34380. + const int isdir)
  34381. +{
  34382. + int err;
  34383. + struct inode *delegated;
  34384. +
  34385. + if (d_is_negative(whpath->dentry))
  34386. + return;
  34387. +
  34388. + if (isdir)
  34389. + err = vfsub_rmdir(h_dir, whpath);
  34390. + else {
  34391. + delegated = NULL;
  34392. + err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
  34393. + if (unlikely(err == -EWOULDBLOCK)) {
  34394. + pr_warn("cannot retry for NFSv4 delegation"
  34395. + " for an internal unlink\n");
  34396. + iput(delegated);
  34397. + }
  34398. + }
  34399. + if (unlikely(err))
  34400. + pr_warn("failed removing %pd (%d), ignored.\n",
  34401. + whpath->dentry, err);
  34402. +}
  34403. +
  34404. +static int test_linkable(struct dentry *h_root)
  34405. +{
  34406. + struct inode *h_dir = d_inode(h_root);
  34407. +
  34408. + if (h_dir->i_op->link)
  34409. + return 0;
  34410. +
  34411. + pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
  34412. + h_root, au_sbtype(h_root->d_sb));
  34413. + return -ENOSYS; /* the branch doesn't have its ->link() */
  34414. +}
  34415. +
  34416. +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
  34417. +static int au_whdir(struct inode *h_dir, struct path *path)
  34418. +{
  34419. + int err;
  34420. +
  34421. + err = -EEXIST;
  34422. + if (d_is_negative(path->dentry)) {
  34423. + int mode = 0700;
  34424. +
  34425. + if (au_test_nfs(path->dentry->d_sb))
  34426. + mode |= 0111;
  34427. + err = vfsub_mkdir(h_dir, path, mode);
  34428. + } else if (d_is_dir(path->dentry))
  34429. + err = 0;
  34430. + else
  34431. + pr_err("unknown %pd exists\n", path->dentry);
  34432. +
  34433. + return err;
  34434. +}
  34435. +
  34436. +struct au_wh_base {
  34437. + const struct qstr *name;
  34438. + struct dentry *dentry;
  34439. +};
  34440. +
  34441. +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
  34442. + struct path *h_path)
  34443. +{
  34444. + h_path->dentry = base[AuBrWh_BASE].dentry;
  34445. + au_wh_clean(h_dir, h_path, /*isdir*/0);
  34446. + h_path->dentry = base[AuBrWh_PLINK].dentry;
  34447. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  34448. + h_path->dentry = base[AuBrWh_ORPH].dentry;
  34449. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  34450. +}
  34451. +
  34452. +/*
  34453. + * returns tri-state,
  34454. + * minus: error, caller should print the message
  34455. + * zero: success
  34456. + * plus: error, caller should NOT print the message
  34457. + */
  34458. +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
  34459. + int do_plink, struct au_wh_base base[],
  34460. + struct path *h_path)
  34461. +{
  34462. + int err;
  34463. + struct inode *h_dir;
  34464. +
  34465. + h_dir = d_inode(h_root);
  34466. + h_path->dentry = base[AuBrWh_BASE].dentry;
  34467. + au_wh_clean(h_dir, h_path, /*isdir*/0);
  34468. + h_path->dentry = base[AuBrWh_PLINK].dentry;
  34469. + if (do_plink) {
  34470. + err = test_linkable(h_root);
  34471. + if (unlikely(err)) {
  34472. + err = 1;
  34473. + goto out;
  34474. + }
  34475. +
  34476. + err = au_whdir(h_dir, h_path);
  34477. + if (unlikely(err))
  34478. + goto out;
  34479. + wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
  34480. + } else
  34481. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  34482. + h_path->dentry = base[AuBrWh_ORPH].dentry;
  34483. + err = au_whdir(h_dir, h_path);
  34484. + if (unlikely(err))
  34485. + goto out;
  34486. + wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
  34487. +
  34488. +out:
  34489. + return err;
  34490. +}
  34491. +
  34492. +/*
  34493. + * for the moment, aufs supports the branch filesystem which does not support
  34494. + * link(2). testing on FAT which does not support i_op->setattr() fully either,
  34495. + * copyup failed. finally, such filesystem will not be used as the writable
  34496. + * branch.
  34497. + *
  34498. + * returns tri-state, see above.
  34499. + */
  34500. +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
  34501. + int do_plink, struct au_wh_base base[],
  34502. + struct path *h_path)
  34503. +{
  34504. + int err;
  34505. + struct inode *h_dir;
  34506. +
  34507. + WbrWhMustWriteLock(wbr);
  34508. +
  34509. + err = test_linkable(h_root);
  34510. + if (unlikely(err)) {
  34511. + err = 1;
  34512. + goto out;
  34513. + }
  34514. +
  34515. + /*
  34516. + * todo: should this create be done in /sbin/mount.aufs helper?
  34517. + */
  34518. + err = -EEXIST;
  34519. + h_dir = d_inode(h_root);
  34520. + if (d_is_negative(base[AuBrWh_BASE].dentry)) {
  34521. + h_path->dentry = base[AuBrWh_BASE].dentry;
  34522. + err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
  34523. + } else if (d_is_reg(base[AuBrWh_BASE].dentry))
  34524. + err = 0;
  34525. + else
  34526. + pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
  34527. + if (unlikely(err))
  34528. + goto out;
  34529. +
  34530. + h_path->dentry = base[AuBrWh_PLINK].dentry;
  34531. + if (do_plink) {
  34532. + err = au_whdir(h_dir, h_path);
  34533. + if (unlikely(err))
  34534. + goto out;
  34535. + wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
  34536. + } else
  34537. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  34538. + wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
  34539. +
  34540. + h_path->dentry = base[AuBrWh_ORPH].dentry;
  34541. + err = au_whdir(h_dir, h_path);
  34542. + if (unlikely(err))
  34543. + goto out;
  34544. + wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
  34545. +
  34546. +out:
  34547. + return err;
  34548. +}
  34549. +
  34550. +/*
  34551. + * initialize the whiteout base file/dir for @br.
  34552. + */
  34553. +int au_wh_init(struct au_branch *br, struct super_block *sb)
  34554. +{
  34555. + int err, i;
  34556. + const unsigned char do_plink
  34557. + = !!au_opt_test(au_mntflags(sb), PLINK);
  34558. + struct inode *h_dir;
  34559. + struct path path = br->br_path;
  34560. + struct dentry *h_root = path.dentry;
  34561. + struct au_wbr *wbr = br->br_wbr;
  34562. + static const struct qstr base_name[] = {
  34563. + [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
  34564. + sizeof(AUFS_BASE_NAME) - 1),
  34565. + [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
  34566. + sizeof(AUFS_PLINKDIR_NAME) - 1),
  34567. + [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
  34568. + sizeof(AUFS_ORPHDIR_NAME) - 1)
  34569. + };
  34570. + struct au_wh_base base[] = {
  34571. + [AuBrWh_BASE] = {
  34572. + .name = base_name + AuBrWh_BASE,
  34573. + .dentry = NULL
  34574. + },
  34575. + [AuBrWh_PLINK] = {
  34576. + .name = base_name + AuBrWh_PLINK,
  34577. + .dentry = NULL
  34578. + },
  34579. + [AuBrWh_ORPH] = {
  34580. + .name = base_name + AuBrWh_ORPH,
  34581. + .dentry = NULL
  34582. + }
  34583. + };
  34584. +
  34585. + if (wbr)
  34586. + WbrWhMustWriteLock(wbr);
  34587. +
  34588. + for (i = 0; i < AuBrWh_Last; i++) {
  34589. + /* doubly whiteouted */
  34590. + struct dentry *d;
  34591. +
  34592. + d = au_wh_lkup(h_root, (void *)base[i].name, br);
  34593. + err = PTR_ERR(d);
  34594. + if (IS_ERR(d))
  34595. + goto out;
  34596. +
  34597. + base[i].dentry = d;
  34598. + AuDebugOn(wbr
  34599. + && wbr->wbr_wh[i]
  34600. + && wbr->wbr_wh[i] != base[i].dentry);
  34601. + }
  34602. +
  34603. + if (wbr)
  34604. + for (i = 0; i < AuBrWh_Last; i++) {
  34605. + dput(wbr->wbr_wh[i]);
  34606. + wbr->wbr_wh[i] = NULL;
  34607. + }
  34608. +
  34609. + err = 0;
  34610. + if (!au_br_writable(br->br_perm)) {
  34611. + h_dir = d_inode(h_root);
  34612. + au_wh_init_ro(h_dir, base, &path);
  34613. + } else if (!au_br_wh_linkable(br->br_perm)) {
  34614. + err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
  34615. + if (err > 0)
  34616. + goto out;
  34617. + else if (err)
  34618. + goto out_err;
  34619. + } else {
  34620. + err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
  34621. + if (err > 0)
  34622. + goto out;
  34623. + else if (err)
  34624. + goto out_err;
  34625. + }
  34626. + goto out; /* success */
  34627. +
  34628. +out_err:
  34629. + pr_err("an error(%d) on the writable branch %pd(%s)\n",
  34630. + err, h_root, au_sbtype(h_root->d_sb));
  34631. +out:
  34632. + for (i = 0; i < AuBrWh_Last; i++)
  34633. + dput(base[i].dentry);
  34634. + return err;
  34635. +}
  34636. +
  34637. +/* ---------------------------------------------------------------------- */
  34638. +/*
  34639. + * whiteouts are all hard-linked usually.
  34640. + * when its link count reaches a ceiling, we create a new whiteout base
  34641. + * asynchronously.
  34642. + */
  34643. +
  34644. +struct reinit_br_wh {
  34645. + struct super_block *sb;
  34646. + struct au_branch *br;
  34647. +};
  34648. +
  34649. +static void reinit_br_wh(void *arg)
  34650. +{
  34651. + int err;
  34652. + aufs_bindex_t bindex;
  34653. + struct path h_path;
  34654. + struct reinit_br_wh *a = arg;
  34655. + struct au_wbr *wbr;
  34656. + struct inode *dir, *delegated;
  34657. + struct dentry *h_root;
  34658. + struct au_hinode *hdir;
  34659. +
  34660. + err = 0;
  34661. + wbr = a->br->br_wbr;
  34662. + /* big aufs lock */
  34663. + si_noflush_write_lock(a->sb);
  34664. + if (!au_br_writable(a->br->br_perm))
  34665. + goto out;
  34666. + bindex = au_br_index(a->sb, a->br->br_id);
  34667. + if (unlikely(bindex < 0))
  34668. + goto out;
  34669. +
  34670. + di_read_lock_parent(a->sb->s_root, AuLock_IR);
  34671. + dir = d_inode(a->sb->s_root);
  34672. + hdir = au_hi(dir, bindex);
  34673. + h_root = au_h_dptr(a->sb->s_root, bindex);
  34674. + AuDebugOn(h_root != au_br_dentry(a->br));
  34675. +
  34676. + au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
  34677. + wbr_wh_write_lock(wbr);
  34678. + err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
  34679. + h_root, a->br);
  34680. + if (!err) {
  34681. + h_path.dentry = wbr->wbr_whbase;
  34682. + h_path.mnt = au_br_mnt(a->br);
  34683. + delegated = NULL;
  34684. + err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
  34685. + /*force*/0);
  34686. + if (unlikely(err == -EWOULDBLOCK)) {
  34687. + pr_warn("cannot retry for NFSv4 delegation"
  34688. + " for an internal unlink\n");
  34689. + iput(delegated);
  34690. + }
  34691. + } else {
  34692. + pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
  34693. + err = 0;
  34694. + }
  34695. + dput(wbr->wbr_whbase);
  34696. + wbr->wbr_whbase = NULL;
  34697. + if (!err)
  34698. + err = au_wh_init(a->br, a->sb);
  34699. + wbr_wh_write_unlock(wbr);
  34700. + au_hn_inode_unlock(hdir);
  34701. + di_read_unlock(a->sb->s_root, AuLock_IR);
  34702. + if (!err)
  34703. + au_fhsm_wrote(a->sb, bindex, /*force*/0);
  34704. +
  34705. +out:
  34706. + if (wbr)
  34707. + atomic_dec(&wbr->wbr_wh_running);
  34708. + au_lcnt_dec(&a->br->br_count);
  34709. + si_write_unlock(a->sb);
  34710. + au_nwt_done(&au_sbi(a->sb)->si_nowait);
  34711. + au_kfree_rcu(a);
  34712. + if (unlikely(err))
  34713. + AuIOErr("err %d\n", err);
  34714. +}
  34715. +
  34716. +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
  34717. +{
  34718. + int do_dec, wkq_err;
  34719. + struct reinit_br_wh *arg;
  34720. +
  34721. + do_dec = 1;
  34722. + if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
  34723. + goto out;
  34724. +
  34725. + /* ignore ENOMEM */
  34726. + arg = kmalloc(sizeof(*arg), GFP_NOFS);
  34727. + if (arg) {
  34728. + /*
  34729. + * dec(wh_running), kfree(arg) and dec(br_count)
  34730. + * in reinit function
  34731. + */
  34732. + arg->sb = sb;
  34733. + arg->br = br;
  34734. + au_lcnt_inc(&br->br_count);
  34735. + wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
  34736. + if (unlikely(wkq_err)) {
  34737. + atomic_dec(&br->br_wbr->wbr_wh_running);
  34738. + au_lcnt_dec(&br->br_count);
  34739. + au_kfree_rcu(arg);
  34740. + }
  34741. + do_dec = 0;
  34742. + }
  34743. +
  34744. +out:
  34745. + if (do_dec)
  34746. + atomic_dec(&br->br_wbr->wbr_wh_running);
  34747. +}
  34748. +
  34749. +/* ---------------------------------------------------------------------- */
  34750. +
  34751. +/*
  34752. + * create the whiteout @wh.
  34753. + */
  34754. +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
  34755. + struct dentry *wh)
  34756. +{
  34757. + int err;
  34758. + struct path h_path = {
  34759. + .dentry = wh
  34760. + };
  34761. + struct au_branch *br;
  34762. + struct au_wbr *wbr;
  34763. + struct dentry *h_parent;
  34764. + struct inode *h_dir, *delegated;
  34765. +
  34766. + h_parent = wh->d_parent; /* dir inode is locked */
  34767. + h_dir = d_inode(h_parent);
  34768. + IMustLock(h_dir);
  34769. +
  34770. + br = au_sbr(sb, bindex);
  34771. + h_path.mnt = au_br_mnt(br);
  34772. + wbr = br->br_wbr;
  34773. + wbr_wh_read_lock(wbr);
  34774. + if (wbr->wbr_whbase) {
  34775. + delegated = NULL;
  34776. + err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
  34777. + if (unlikely(err == -EWOULDBLOCK)) {
  34778. + pr_warn("cannot retry for NFSv4 delegation"
  34779. + " for an internal link\n");
  34780. + iput(delegated);
  34781. + }
  34782. + if (!err || err != -EMLINK)
  34783. + goto out;
  34784. +
  34785. + /* link count full. re-initialize br_whbase. */
  34786. + kick_reinit_br_wh(sb, br);
  34787. + }
  34788. +
  34789. + /* return this error in this context */
  34790. + err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
  34791. + if (!err)
  34792. + au_fhsm_wrote(sb, bindex, /*force*/0);
  34793. +
  34794. +out:
  34795. + wbr_wh_read_unlock(wbr);
  34796. + return err;
  34797. +}
  34798. +
  34799. +/* ---------------------------------------------------------------------- */
  34800. +
  34801. +/*
  34802. + * create or remove the diropq.
  34803. + */
  34804. +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
  34805. + unsigned int flags)
  34806. +{
  34807. + struct dentry *opq_dentry;
  34808. + struct super_block *sb;
  34809. + struct au_branch *br;
  34810. + struct path h_path;
  34811. + int err;
  34812. +
  34813. + sb = dentry->d_sb;
  34814. + br = au_sbr(sb, bindex);
  34815. + h_path.dentry = au_h_dptr(dentry, bindex);
  34816. + h_path.mnt = au_br_mnt(br);
  34817. + opq_dentry = vfsub_lkup_one(&diropq_name, &h_path);
  34818. + if (IS_ERR(opq_dentry))
  34819. + goto out;
  34820. +
  34821. + if (au_ftest_diropq(flags, CREATE)) {
  34822. + err = link_or_create_wh(sb, bindex, opq_dentry);
  34823. + if (!err) {
  34824. + au_set_dbdiropq(dentry, bindex);
  34825. + goto out; /* success */
  34826. + }
  34827. + } else {
  34828. + h_path.dentry = opq_dentry;
  34829. + err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &h_path);
  34830. + if (!err)
  34831. + au_set_dbdiropq(dentry, -1);
  34832. + }
  34833. + dput(opq_dentry);
  34834. + opq_dentry = ERR_PTR(err);
  34835. +
  34836. +out:
  34837. + return opq_dentry;
  34838. +}
  34839. +
  34840. +struct do_diropq_args {
  34841. + struct dentry **errp;
  34842. + struct dentry *dentry;
  34843. + aufs_bindex_t bindex;
  34844. + unsigned int flags;
  34845. +};
  34846. +
  34847. +static void call_do_diropq(void *args)
  34848. +{
  34849. + struct do_diropq_args *a = args;
  34850. + *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
  34851. +}
  34852. +
  34853. +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
  34854. + unsigned int flags)
  34855. +{
  34856. + struct dentry *diropq, *h_dentry;
  34857. + struct user_namespace *h_userns;
  34858. +
  34859. + h_userns = au_sbr_userns(dentry->d_sb, bindex);
  34860. + h_dentry = au_h_dptr(dentry, bindex);
  34861. + if (!au_test_h_perm_sio(h_userns, d_inode(h_dentry),
  34862. + MAY_EXEC | MAY_WRITE))
  34863. + diropq = do_diropq(dentry, bindex, flags);
  34864. + else {
  34865. + int wkq_err;
  34866. + struct do_diropq_args args = {
  34867. + .errp = &diropq,
  34868. + .dentry = dentry,
  34869. + .bindex = bindex,
  34870. + .flags = flags
  34871. + };
  34872. +
  34873. + wkq_err = au_wkq_wait(call_do_diropq, &args);
  34874. + if (unlikely(wkq_err))
  34875. + diropq = ERR_PTR(wkq_err);
  34876. + }
  34877. +
  34878. + return diropq;
  34879. +}
  34880. +
  34881. +/* ---------------------------------------------------------------------- */
  34882. +
  34883. +/*
  34884. + * lookup whiteout dentry.
  34885. + * @h_parent: lower parent dentry which must exist and be locked
  34886. + * @base_name: name of dentry which will be whiteouted
  34887. + * returns dentry for whiteout.
  34888. + */
  34889. +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
  34890. + struct au_branch *br)
  34891. +{
  34892. + int err;
  34893. + struct qstr wh_name;
  34894. + struct dentry *wh_dentry;
  34895. + struct path h_path;
  34896. +
  34897. + err = au_wh_name_alloc(&wh_name, base_name);
  34898. + wh_dentry = ERR_PTR(err);
  34899. + if (!err) {
  34900. + h_path.dentry = h_parent;
  34901. + h_path.mnt = au_br_mnt(br);
  34902. + wh_dentry = vfsub_lkup_one(&wh_name, &h_path);
  34903. + au_kfree_try_rcu(wh_name.name);
  34904. + }
  34905. + return wh_dentry;
  34906. +}
  34907. +
  34908. +/*
  34909. + * link/create a whiteout for @dentry on @bindex.
  34910. + */
  34911. +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
  34912. + struct dentry *h_parent)
  34913. +{
  34914. + struct dentry *wh_dentry;
  34915. + struct super_block *sb;
  34916. + int err;
  34917. +
  34918. + sb = dentry->d_sb;
  34919. + wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
  34920. + if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
  34921. + err = link_or_create_wh(sb, bindex, wh_dentry);
  34922. + if (!err) {
  34923. + au_set_dbwh(dentry, bindex);
  34924. + au_fhsm_wrote(sb, bindex, /*force*/0);
  34925. + } else {
  34926. + dput(wh_dentry);
  34927. + wh_dentry = ERR_PTR(err);
  34928. + }
  34929. + }
  34930. +
  34931. + return wh_dentry;
  34932. +}
  34933. +
  34934. +/* ---------------------------------------------------------------------- */
  34935. +
  34936. +/* Delete all whiteouts in this directory on branch bindex. */
  34937. +static int del_wh_children(struct path *h_path, struct au_nhash *whlist,
  34938. + aufs_bindex_t bindex)
  34939. +{
  34940. + int err;
  34941. + unsigned long ul, n;
  34942. + struct qstr wh_name;
  34943. + char *p;
  34944. + struct hlist_head *head;
  34945. + struct au_vdir_wh *pos;
  34946. + struct au_vdir_destr *str;
  34947. +
  34948. + err = -ENOMEM;
  34949. + p = (void *)__get_free_page(GFP_NOFS);
  34950. + wh_name.name = p;
  34951. + if (unlikely(!wh_name.name))
  34952. + goto out;
  34953. +
  34954. + err = 0;
  34955. + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
  34956. + p += AUFS_WH_PFX_LEN;
  34957. + n = whlist->nh_num;
  34958. + head = whlist->nh_head;
  34959. + for (ul = 0; !err && ul < n; ul++, head++) {
  34960. + hlist_for_each_entry(pos, head, wh_hash) {
  34961. + if (pos->wh_bindex != bindex)
  34962. + continue;
  34963. +
  34964. + str = &pos->wh_str;
  34965. + if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
  34966. + memcpy(p, str->name, str->len);
  34967. + wh_name.len = AUFS_WH_PFX_LEN + str->len;
  34968. + err = unlink_wh_name(h_path, &wh_name);
  34969. + if (!err)
  34970. + continue;
  34971. + break;
  34972. + }
  34973. + AuIOErr("whiteout name too long %.*s\n",
  34974. + str->len, str->name);
  34975. + err = -EIO;
  34976. + break;
  34977. + }
  34978. + }
  34979. + free_page((unsigned long)wh_name.name);
  34980. +
  34981. +out:
  34982. + return err;
  34983. +}
  34984. +
  34985. +struct del_wh_children_args {
  34986. + int *errp;
  34987. + struct path *h_path;
  34988. + struct au_nhash *whlist;
  34989. + aufs_bindex_t bindex;
  34990. +};
  34991. +
  34992. +static void call_del_wh_children(void *args)
  34993. +{
  34994. + struct del_wh_children_args *a = args;
  34995. + *a->errp = del_wh_children(a->h_path, a->whlist, a->bindex);
  34996. +}
  34997. +
  34998. +/* ---------------------------------------------------------------------- */
  34999. +
  35000. +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
  35001. +{
  35002. + struct au_whtmp_rmdir *whtmp;
  35003. + int err;
  35004. + unsigned int rdhash;
  35005. +
  35006. + SiMustAnyLock(sb);
  35007. +
  35008. + whtmp = kzalloc(sizeof(*whtmp), gfp);
  35009. + if (unlikely(!whtmp)) {
  35010. + whtmp = ERR_PTR(-ENOMEM);
  35011. + goto out;
  35012. + }
  35013. +
  35014. + /* no estimation for dir size */
  35015. + rdhash = au_sbi(sb)->si_rdhash;
  35016. + if (!rdhash)
  35017. + rdhash = AUFS_RDHASH_DEF;
  35018. + err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
  35019. + if (unlikely(err)) {
  35020. + au_kfree_rcu(whtmp);
  35021. + whtmp = ERR_PTR(err);
  35022. + }
  35023. +
  35024. +out:
  35025. + return whtmp;
  35026. +}
  35027. +
  35028. +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
  35029. +{
  35030. + if (whtmp->br)
  35031. + au_lcnt_dec(&whtmp->br->br_count);
  35032. + dput(whtmp->wh_dentry);
  35033. + iput(whtmp->dir);
  35034. + au_nhash_wh_free(&whtmp->whlist);
  35035. + au_kfree_rcu(whtmp);
  35036. +}
  35037. +
  35038. +/*
  35039. + * rmdir the whiteouted temporary named dir @h_dentry.
  35040. + * @whlist: whiteouted children.
  35041. + */
  35042. +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
  35043. + struct dentry *wh_dentry, struct au_nhash *whlist)
  35044. +{
  35045. + int err;
  35046. + unsigned int h_nlink;
  35047. + struct path wh_path;
  35048. + struct inode *wh_inode, *h_dir;
  35049. + struct au_branch *br;
  35050. + struct user_namespace *h_userns;
  35051. +
  35052. + h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
  35053. + IMustLock(h_dir);
  35054. +
  35055. + br = au_sbr(dir->i_sb, bindex);
  35056. + wh_path.dentry = wh_dentry;
  35057. + wh_path.mnt = au_br_mnt(br);
  35058. + h_userns = au_br_userns(br);
  35059. + wh_inode = d_inode(wh_dentry);
  35060. + inode_lock_nested(wh_inode, AuLsc_I_CHILD);
  35061. +
  35062. + /*
  35063. + * someone else might change some whiteouts while we were sleeping.
  35064. + * it means this whlist may have an obsoleted entry.
  35065. + */
  35066. + if (!au_test_h_perm_sio(h_userns, wh_inode, MAY_EXEC | MAY_WRITE))
  35067. + err = del_wh_children(&wh_path, whlist, bindex);
  35068. + else {
  35069. + int wkq_err;
  35070. + struct del_wh_children_args args = {
  35071. + .errp = &err,
  35072. + .h_path = &wh_path,
  35073. + .whlist = whlist,
  35074. + .bindex = bindex
  35075. + };
  35076. +
  35077. + wkq_err = au_wkq_wait(call_del_wh_children, &args);
  35078. + if (unlikely(wkq_err))
  35079. + err = wkq_err;
  35080. + }
  35081. + inode_unlock(wh_inode);
  35082. +
  35083. + if (!err) {
  35084. + h_nlink = h_dir->i_nlink;
  35085. + err = vfsub_rmdir(h_dir, &wh_path);
  35086. + /* some fs doesn't change the parent nlink in some cases */
  35087. + h_nlink -= h_dir->i_nlink;
  35088. + }
  35089. +
  35090. + if (!err) {
  35091. + if (au_ibtop(dir) == bindex) {
  35092. + /* todo: dir->i_mutex is necessary */
  35093. + au_cpup_attr_timesizes(dir);
  35094. + if (h_nlink)
  35095. + vfsub_drop_nlink(dir);
  35096. + }
  35097. + return 0; /* success */
  35098. + }
  35099. +
  35100. + pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
  35101. + return err;
  35102. +}
  35103. +
  35104. +static void call_rmdir_whtmp(void *args)
  35105. +{
  35106. + int err;
  35107. + aufs_bindex_t bindex;
  35108. + struct au_whtmp_rmdir *a = args;
  35109. + struct super_block *sb;
  35110. + struct dentry *h_parent;
  35111. + struct inode *h_dir;
  35112. + struct au_hinode *hdir;
  35113. +
  35114. + /* rmdir by nfsd may cause deadlock with this i_mutex */
  35115. + /* inode_lock(a->dir); */
  35116. + err = -EROFS;
  35117. + sb = a->dir->i_sb;
  35118. + si_read_lock(sb, !AuLock_FLUSH);
  35119. + if (!au_br_writable(a->br->br_perm))
  35120. + goto out;
  35121. + bindex = au_br_index(sb, a->br->br_id);
  35122. + if (unlikely(bindex < 0))
  35123. + goto out;
  35124. +
  35125. + err = -EIO;
  35126. + ii_write_lock_parent(a->dir);
  35127. + h_parent = dget_parent(a->wh_dentry);
  35128. + h_dir = d_inode(h_parent);
  35129. + hdir = au_hi(a->dir, bindex);
  35130. + err = vfsub_mnt_want_write(au_br_mnt(a->br));
  35131. + if (unlikely(err))
  35132. + goto out_mnt;
  35133. + au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
  35134. + err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
  35135. + a->br);
  35136. + if (!err)
  35137. + err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
  35138. + au_hn_inode_unlock(hdir);
  35139. + vfsub_mnt_drop_write(au_br_mnt(a->br));
  35140. +
  35141. +out_mnt:
  35142. + dput(h_parent);
  35143. + ii_write_unlock(a->dir);
  35144. +out:
  35145. + /* inode_unlock(a->dir); */
  35146. + au_whtmp_rmdir_free(a);
  35147. + si_read_unlock(sb);
  35148. + au_nwt_done(&au_sbi(sb)->si_nowait);
  35149. + if (unlikely(err))
  35150. + AuIOErr("err %d\n", err);
  35151. +}
  35152. +
  35153. +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
  35154. + struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
  35155. +{
  35156. + int wkq_err;
  35157. + struct super_block *sb;
  35158. +
  35159. + IMustLock(dir);
  35160. +
  35161. + /* all post-process will be done in do_rmdir_whtmp(). */
  35162. + sb = dir->i_sb;
  35163. + args->dir = au_igrab(dir);
  35164. + args->br = au_sbr(sb, bindex);
  35165. + au_lcnt_inc(&args->br->br_count);
  35166. + args->wh_dentry = dget(wh_dentry);
  35167. + wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
  35168. + if (unlikely(wkq_err)) {
  35169. + pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
  35170. + au_whtmp_rmdir_free(args);
  35171. + }
  35172. +}
  35173. diff -Naur null/fs/aufs/whout.h linux-5.15.131/fs/aufs/whout.h
  35174. --- /dev/null
  35175. +++ linux-5.15.131/fs/aufs/whout.h 2023-09-11 14:32:48.153722489 +0300
  35176. @@ -0,0 +1,87 @@
  35177. +/* SPDX-License-Identifier: GPL-2.0 */
  35178. +/*
  35179. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  35180. + *
  35181. + * This program, aufs is free software; you can redistribute it and/or modify
  35182. + * it under the terms of the GNU General Public License as published by
  35183. + * the Free Software Foundation; either version 2 of the License, or
  35184. + * (at your option) any later version.
  35185. + *
  35186. + * This program is distributed in the hope that it will be useful,
  35187. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35188. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  35189. + * GNU General Public License for more details.
  35190. + *
  35191. + * You should have received a copy of the GNU General Public License
  35192. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  35193. + */
  35194. +
  35195. +/*
  35196. + * whiteout for logical deletion and opaque directory
  35197. + */
  35198. +
  35199. +#ifndef __AUFS_WHOUT_H__
  35200. +#define __AUFS_WHOUT_H__
  35201. +
  35202. +#ifdef __KERNEL__
  35203. +
  35204. +#include "dir.h"
  35205. +
  35206. +/* whout.c */
  35207. +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
  35208. +int au_wh_test(struct user_namespace *h_userns, struct path *h_ppath,
  35209. + struct qstr *wh_name, int try_sio);
  35210. +int au_diropq_test(struct user_namespace *h_userns, struct path *h_path);
  35211. +struct au_branch;
  35212. +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
  35213. + struct qstr *prefix);
  35214. +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
  35215. +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
  35216. + struct dentry *dentry);
  35217. +int au_wh_init(struct au_branch *br, struct super_block *sb);
  35218. +
  35219. +/* diropq flags */
  35220. +#define AuDiropq_CREATE 1
  35221. +#define au_ftest_diropq(flags, name) ((flags) & AuDiropq_##name)
  35222. +#define au_fset_diropq(flags, name) \
  35223. + do { (flags) |= AuDiropq_##name; } while (0)
  35224. +#define au_fclr_diropq(flags, name) \
  35225. + do { (flags) &= ~AuDiropq_##name; } while (0)
  35226. +
  35227. +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
  35228. + unsigned int flags);
  35229. +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
  35230. + struct au_branch *br);
  35231. +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
  35232. + struct dentry *h_parent);
  35233. +
  35234. +/* real rmdir for the whiteout-ed dir */
  35235. +struct au_whtmp_rmdir {
  35236. + struct inode *dir;
  35237. + struct au_branch *br;
  35238. + struct dentry *wh_dentry;
  35239. + struct au_nhash whlist;
  35240. +};
  35241. +
  35242. +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
  35243. +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
  35244. +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
  35245. + struct dentry *wh_dentry, struct au_nhash *whlist);
  35246. +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
  35247. + struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
  35248. +
  35249. +/* ---------------------------------------------------------------------- */
  35250. +
  35251. +static inline struct dentry *au_diropq_create(struct dentry *dentry,
  35252. + aufs_bindex_t bindex)
  35253. +{
  35254. + return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
  35255. +}
  35256. +
  35257. +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
  35258. +{
  35259. + return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
  35260. +}
  35261. +
  35262. +#endif /* __KERNEL__ */
  35263. +#endif /* __AUFS_WHOUT_H__ */
  35264. diff -Naur null/fs/aufs/wkq.c linux-5.15.131/fs/aufs/wkq.c
  35265. --- /dev/null
  35266. +++ linux-5.15.131/fs/aufs/wkq.c 2023-09-11 14:32:48.153722489 +0300
  35267. @@ -0,0 +1,372 @@
  35268. +// SPDX-License-Identifier: GPL-2.0
  35269. +/*
  35270. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  35271. + *
  35272. + * This program, aufs is free software; you can redistribute it and/or modify
  35273. + * it under the terms of the GNU General Public License as published by
  35274. + * the Free Software Foundation; either version 2 of the License, or
  35275. + * (at your option) any later version.
  35276. + *
  35277. + * This program is distributed in the hope that it will be useful,
  35278. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35279. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  35280. + * GNU General Public License for more details.
  35281. + *
  35282. + * You should have received a copy of the GNU General Public License
  35283. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  35284. + */
  35285. +
  35286. +/*
  35287. + * workqueue for asynchronous/super-io operations
  35288. + * todo: try new credential scheme
  35289. + */
  35290. +
  35291. +#include <linux/module.h>
  35292. +#include "aufs.h"
  35293. +
  35294. +/* internal workqueue named AUFS_WKQ_NAME */
  35295. +
  35296. +static struct workqueue_struct *au_wkq;
  35297. +
  35298. +struct au_wkinfo {
  35299. + struct work_struct wk;
  35300. + struct kobject *kobj;
  35301. +
  35302. + unsigned int flags; /* see wkq.h */
  35303. +
  35304. + au_wkq_func_t func;
  35305. + void *args;
  35306. +
  35307. +#ifdef CONFIG_LOCKDEP
  35308. + int dont_check;
  35309. + struct held_lock **hlock;
  35310. +#endif
  35311. +
  35312. + struct completion *comp;
  35313. +};
  35314. +
  35315. +/* ---------------------------------------------------------------------- */
  35316. +/*
  35317. + * Aufs passes some operations to the workqueue such as the internal copyup.
  35318. + * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
  35319. + * job run by workqueue depends upon the locks acquired in the other task.
  35320. + * Delegating a small operation to the workqueue, aufs passes its lockdep
  35321. + * information too. And the job in the workqueue restores the info in order to
  35322. + * pretend as if it acquired those locks. This is just to make LOCKDEP work
  35323. + * correctly and expectedly.
  35324. + */
  35325. +
  35326. +#ifndef CONFIG_LOCKDEP
  35327. +AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
  35328. +AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
  35329. +AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
  35330. +AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
  35331. +AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
  35332. +#else
  35333. +static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
  35334. +{
  35335. + wkinfo->hlock = NULL;
  35336. + wkinfo->dont_check = 0;
  35337. +}
  35338. +
  35339. +/*
  35340. + * 1: matched
  35341. + * 0: unmatched
  35342. + */
  35343. +static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
  35344. +{
  35345. + static DEFINE_SPINLOCK(spin);
  35346. + static struct {
  35347. + char *name;
  35348. + struct lock_class_key *key;
  35349. + } a[] = {
  35350. + { .name = "&sbinfo->si_rwsem" },
  35351. + { .name = "&finfo->fi_rwsem" },
  35352. + { .name = "&dinfo->di_rwsem" },
  35353. + { .name = "&iinfo->ii_rwsem" }
  35354. + };
  35355. + static int set;
  35356. + int i;
  35357. +
  35358. + /* lockless read from 'set.' see below */
  35359. + if (set == ARRAY_SIZE(a)) {
  35360. + for (i = 0; i < ARRAY_SIZE(a); i++)
  35361. + if (a[i].key == key)
  35362. + goto match;
  35363. + goto unmatch;
  35364. + }
  35365. +
  35366. + spin_lock(&spin);
  35367. + if (set)
  35368. + for (i = 0; i < ARRAY_SIZE(a); i++)
  35369. + if (a[i].key == key) {
  35370. + spin_unlock(&spin);
  35371. + goto match;
  35372. + }
  35373. + for (i = 0; i < ARRAY_SIZE(a); i++) {
  35374. + if (a[i].key) {
  35375. + if (unlikely(a[i].key == key)) { /* rare but possible */
  35376. + spin_unlock(&spin);
  35377. + goto match;
  35378. + } else
  35379. + continue;
  35380. + }
  35381. + if (strstr(a[i].name, name)) {
  35382. + /*
  35383. + * the order of these three lines is important for the
  35384. + * lockless read above.
  35385. + */
  35386. + a[i].key = key;
  35387. + spin_unlock(&spin);
  35388. + set++;
  35389. + /* AuDbg("%d, %s\n", set, name); */
  35390. + goto match;
  35391. + }
  35392. + }
  35393. + spin_unlock(&spin);
  35394. + goto unmatch;
  35395. +
  35396. +match:
  35397. + return 1;
  35398. +unmatch:
  35399. + return 0;
  35400. +}
  35401. +
  35402. +static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
  35403. +{
  35404. + int err, n;
  35405. + struct task_struct *curr;
  35406. + struct held_lock **hl, *held_locks, *p;
  35407. +
  35408. + err = 0;
  35409. + curr = current;
  35410. + wkinfo->dont_check = lockdep_recursing(curr);
  35411. + if (wkinfo->dont_check)
  35412. + goto out;
  35413. + n = curr->lockdep_depth;
  35414. + if (!n)
  35415. + goto out;
  35416. +
  35417. + err = -ENOMEM;
  35418. + wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
  35419. + if (unlikely(!wkinfo->hlock))
  35420. + goto out;
  35421. +
  35422. + err = 0;
  35423. +#if 0 /* left for debugging */
  35424. + if (0 && au_debug_test())
  35425. + lockdep_print_held_locks(curr);
  35426. +#endif
  35427. + held_locks = curr->held_locks;
  35428. + hl = wkinfo->hlock;
  35429. + while (n--) {
  35430. + p = held_locks++;
  35431. + if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
  35432. + *hl++ = p;
  35433. + }
  35434. + *hl = NULL;
  35435. +
  35436. +out:
  35437. + return err;
  35438. +}
  35439. +
  35440. +static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
  35441. +{
  35442. + au_kfree_try_rcu(wkinfo->hlock);
  35443. +}
  35444. +
  35445. +static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
  35446. +{
  35447. + struct held_lock *p, **hl = wkinfo->hlock;
  35448. + int subclass;
  35449. +
  35450. + if (wkinfo->dont_check)
  35451. + lockdep_off();
  35452. + if (!hl)
  35453. + return;
  35454. + while ((p = *hl++)) { /* assignment */
  35455. + subclass = lockdep_hlock_class(p)->subclass;
  35456. + /* AuDbg("%s, %d\n", p->instance->name, subclass); */
  35457. + if (p->read)
  35458. + rwsem_acquire_read(p->instance, subclass, 0,
  35459. + /*p->acquire_ip*/_RET_IP_);
  35460. + else
  35461. + rwsem_acquire(p->instance, subclass, 0,
  35462. + /*p->acquire_ip*/_RET_IP_);
  35463. + }
  35464. +}
  35465. +
  35466. +static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
  35467. +{
  35468. + struct held_lock *p, **hl = wkinfo->hlock;
  35469. +
  35470. + if (wkinfo->dont_check)
  35471. + lockdep_on();
  35472. + if (!hl)
  35473. + return;
  35474. + while ((p = *hl++)) /* assignment */
  35475. + rwsem_release(p->instance, /*p->acquire_ip*/_RET_IP_);
  35476. +}
  35477. +#endif
  35478. +
  35479. +static void wkq_func(struct work_struct *wk)
  35480. +{
  35481. + struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
  35482. +
  35483. + AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
  35484. + AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
  35485. +
  35486. + au_wkq_lockdep_pre(wkinfo);
  35487. + wkinfo->func(wkinfo->args);
  35488. + au_wkq_lockdep_post(wkinfo);
  35489. + if (au_ftest_wkq(wkinfo->flags, WAIT))
  35490. + complete(wkinfo->comp);
  35491. + else {
  35492. + kobject_put(wkinfo->kobj);
  35493. + module_put(THIS_MODULE); /* todo: ?? */
  35494. + au_kfree_rcu(wkinfo);
  35495. + }
  35496. +}
  35497. +
  35498. +/*
  35499. + * Since struct completion is large, try allocating it dynamically.
  35500. + */
  35501. +#define AuWkqCompDeclare(name) struct completion *comp = NULL
  35502. +
  35503. +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
  35504. +{
  35505. + *comp = kmalloc(sizeof(**comp), GFP_NOFS);
  35506. + if (*comp) {
  35507. + init_completion(*comp);
  35508. + wkinfo->comp = *comp;
  35509. + return 0;
  35510. + }
  35511. + return -ENOMEM;
  35512. +}
  35513. +
  35514. +static void au_wkq_comp_free(struct completion *comp)
  35515. +{
  35516. + au_kfree_rcu(comp);
  35517. +}
  35518. +
  35519. +static void au_wkq_run(struct au_wkinfo *wkinfo)
  35520. +{
  35521. + if (au_ftest_wkq(wkinfo->flags, NEST)) {
  35522. + if (au_wkq_test()) {
  35523. + AuWarn1("wkq from wkq, unless silly-rename on NFS,"
  35524. + " due to a dead dir by UDBA,"
  35525. + " or async xino write?\n");
  35526. + AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
  35527. + }
  35528. + } else
  35529. + au_dbg_verify_kthread();
  35530. +
  35531. + if (au_ftest_wkq(wkinfo->flags, WAIT)) {
  35532. + INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
  35533. + queue_work(au_wkq, &wkinfo->wk);
  35534. + } else {
  35535. + INIT_WORK(&wkinfo->wk, wkq_func);
  35536. + schedule_work(&wkinfo->wk);
  35537. + }
  35538. +}
  35539. +
  35540. +/*
  35541. + * Be careful. It is easy to make deadlock happen.
  35542. + * processA: lock, wkq and wait
  35543. + * processB: wkq and wait, lock in wkq
  35544. + * --> deadlock
  35545. + */
  35546. +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
  35547. +{
  35548. + int err;
  35549. + AuWkqCompDeclare(comp);
  35550. + struct au_wkinfo wkinfo = {
  35551. + .flags = flags,
  35552. + .func = func,
  35553. + .args = args
  35554. + };
  35555. +
  35556. + err = au_wkq_comp_alloc(&wkinfo, &comp);
  35557. + if (unlikely(err))
  35558. + goto out;
  35559. + err = au_wkq_lockdep_alloc(&wkinfo);
  35560. + if (unlikely(err))
  35561. + goto out_comp;
  35562. + if (!err) {
  35563. + au_wkq_run(&wkinfo);
  35564. + /* no timeout, no interrupt */
  35565. + wait_for_completion(wkinfo.comp);
  35566. + }
  35567. + au_wkq_lockdep_free(&wkinfo);
  35568. +
  35569. +out_comp:
  35570. + au_wkq_comp_free(comp);
  35571. +out:
  35572. + destroy_work_on_stack(&wkinfo.wk);
  35573. + return err;
  35574. +}
  35575. +
  35576. +/*
  35577. + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
  35578. + * problem in a concurrent umounting.
  35579. + */
  35580. +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
  35581. + unsigned int flags)
  35582. +{
  35583. + int err;
  35584. + struct au_wkinfo *wkinfo;
  35585. +
  35586. + atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
  35587. +
  35588. + /*
  35589. + * wkq_func() must free this wkinfo.
  35590. + * it highly depends upon the implementation of workqueue.
  35591. + */
  35592. + err = 0;
  35593. + wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
  35594. + if (wkinfo) {
  35595. + wkinfo->kobj = &au_sbi(sb)->si_kobj;
  35596. + wkinfo->flags = flags & ~AuWkq_WAIT;
  35597. + wkinfo->func = func;
  35598. + wkinfo->args = args;
  35599. + wkinfo->comp = NULL;
  35600. + au_wkq_lockdep_init(wkinfo);
  35601. + kobject_get(wkinfo->kobj);
  35602. + __module_get(THIS_MODULE); /* todo: ?? */
  35603. +
  35604. + au_wkq_run(wkinfo);
  35605. + } else {
  35606. + err = -ENOMEM;
  35607. + au_nwt_done(&au_sbi(sb)->si_nowait);
  35608. + }
  35609. +
  35610. + return err;
  35611. +}
  35612. +
  35613. +/* ---------------------------------------------------------------------- */
  35614. +
  35615. +void au_nwt_init(struct au_nowait_tasks *nwt)
  35616. +{
  35617. + atomic_set(&nwt->nw_len, 0);
  35618. + /* smp_mb(); */ /* atomic_set */
  35619. + init_waitqueue_head(&nwt->nw_wq);
  35620. +}
  35621. +
  35622. +void au_wkq_fin(void)
  35623. +{
  35624. + destroy_workqueue(au_wkq);
  35625. +}
  35626. +
  35627. +int __init au_wkq_init(void)
  35628. +{
  35629. + int err;
  35630. +
  35631. + err = 0;
  35632. + au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
  35633. + if (IS_ERR(au_wkq))
  35634. + err = PTR_ERR(au_wkq);
  35635. + else if (!au_wkq)
  35636. + err = -ENOMEM;
  35637. +
  35638. + return err;
  35639. +}
  35640. diff -Naur null/fs/aufs/wkq.h linux-5.15.131/fs/aufs/wkq.h
  35641. --- /dev/null
  35642. +++ linux-5.15.131/fs/aufs/wkq.h 2023-09-11 14:32:48.154722477 +0300
  35643. @@ -0,0 +1,89 @@
  35644. +/* SPDX-License-Identifier: GPL-2.0 */
  35645. +/*
  35646. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  35647. + *
  35648. + * This program, aufs is free software; you can redistribute it and/or modify
  35649. + * it under the terms of the GNU General Public License as published by
  35650. + * the Free Software Foundation; either version 2 of the License, or
  35651. + * (at your option) any later version.
  35652. + *
  35653. + * This program is distributed in the hope that it will be useful,
  35654. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35655. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  35656. + * GNU General Public License for more details.
  35657. + *
  35658. + * You should have received a copy of the GNU General Public License
  35659. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  35660. + */
  35661. +
  35662. +/*
  35663. + * workqueue for asynchronous/super-io operations
  35664. + * todo: try new credentials management scheme
  35665. + */
  35666. +
  35667. +#ifndef __AUFS_WKQ_H__
  35668. +#define __AUFS_WKQ_H__
  35669. +
  35670. +#ifdef __KERNEL__
  35671. +
  35672. +#include <linux/wait.h>
  35673. +
  35674. +struct super_block;
  35675. +
  35676. +/* ---------------------------------------------------------------------- */
  35677. +
  35678. +/*
  35679. + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
  35680. + */
  35681. +struct au_nowait_tasks {
  35682. + atomic_t nw_len;
  35683. + wait_queue_head_t nw_wq;
  35684. +};
  35685. +
  35686. +/* ---------------------------------------------------------------------- */
  35687. +
  35688. +typedef void (*au_wkq_func_t)(void *args);
  35689. +
  35690. +/* wkq flags */
  35691. +#define AuWkq_WAIT 1
  35692. +#define AuWkq_NEST (1 << 1)
  35693. +#define au_ftest_wkq(flags, name) ((flags) & AuWkq_##name)
  35694. +#define au_fset_wkq(flags, name) \
  35695. + do { (flags) |= AuWkq_##name; } while (0)
  35696. +#define au_fclr_wkq(flags, name) \
  35697. + do { (flags) &= ~AuWkq_##name; } while (0)
  35698. +
  35699. +/* wkq.c */
  35700. +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
  35701. +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
  35702. + unsigned int flags);
  35703. +void au_nwt_init(struct au_nowait_tasks *nwt);
  35704. +int __init au_wkq_init(void);
  35705. +void au_wkq_fin(void);
  35706. +
  35707. +/* ---------------------------------------------------------------------- */
  35708. +
  35709. +static inline int au_wkq_test(void)
  35710. +{
  35711. + return current->flags & PF_WQ_WORKER;
  35712. +}
  35713. +
  35714. +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
  35715. +{
  35716. + return au_wkq_do_wait(AuWkq_WAIT, func, args);
  35717. +}
  35718. +
  35719. +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
  35720. +{
  35721. + if (atomic_dec_and_test(&nwt->nw_len))
  35722. + wake_up_all(&nwt->nw_wq);
  35723. +}
  35724. +
  35725. +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
  35726. +{
  35727. + wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
  35728. + return 0;
  35729. +}
  35730. +
  35731. +#endif /* __KERNEL__ */
  35732. +#endif /* __AUFS_WKQ_H__ */
  35733. diff -Naur null/fs/aufs/xattr.c linux-5.15.131/fs/aufs/xattr.c
  35734. --- /dev/null
  35735. +++ linux-5.15.131/fs/aufs/xattr.c 2023-09-11 14:32:48.154722477 +0300
  35736. @@ -0,0 +1,368 @@
  35737. +// SPDX-License-Identifier: GPL-2.0
  35738. +/*
  35739. + * Copyright (C) 2014-2021 Junjiro R. Okajima
  35740. + *
  35741. + * This program, aufs is free software; you can redistribute it and/or modify
  35742. + * it under the terms of the GNU General Public License as published by
  35743. + * the Free Software Foundation; either version 2 of the License, or
  35744. + * (at your option) any later version.
  35745. + *
  35746. + * This program is distributed in the hope that it will be useful,
  35747. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35748. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  35749. + * GNU General Public License for more details.
  35750. + *
  35751. + * You should have received a copy of the GNU General Public License
  35752. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  35753. + */
  35754. +
  35755. +/*
  35756. + * handling xattr functions
  35757. + */
  35758. +
  35759. +#include <linux/fs.h>
  35760. +#include <linux/posix_acl_xattr.h>
  35761. +#include <linux/xattr.h>
  35762. +#include "aufs.h"
  35763. +
  35764. +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
  35765. +{
  35766. + if (!ignore_flags)
  35767. + goto out;
  35768. + switch (err) {
  35769. + case -ENOMEM:
  35770. + case -EDQUOT:
  35771. + goto out;
  35772. + }
  35773. +
  35774. + if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
  35775. + err = 0;
  35776. + goto out;
  35777. + }
  35778. +
  35779. +#define cmp(brattr, prefix) do { \
  35780. + if (!strncmp(name, XATTR_##prefix##_PREFIX, \
  35781. + XATTR_##prefix##_PREFIX_LEN)) { \
  35782. + if (ignore_flags & AuBrAttr_ICEX_##brattr) \
  35783. + err = 0; \
  35784. + goto out; \
  35785. + } \
  35786. + } while (0)
  35787. +
  35788. + cmp(SEC, SECURITY);
  35789. + cmp(SYS, SYSTEM);
  35790. + cmp(TR, TRUSTED);
  35791. + cmp(USR, USER);
  35792. +#undef cmp
  35793. +
  35794. + if (ignore_flags & AuBrAttr_ICEX_OTH)
  35795. + err = 0;
  35796. +
  35797. +out:
  35798. + return err;
  35799. +}
  35800. +
  35801. +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
  35802. +
  35803. +static int au_do_cpup_xattr(struct path *h_dst, struct path *h_src,
  35804. + char *name, char **buf, unsigned int ignore_flags,
  35805. + unsigned int verbose)
  35806. +{
  35807. + int err;
  35808. + ssize_t ssz;
  35809. + struct inode *h_idst;
  35810. + struct dentry *h_dst_dentry, *h_src_dentry;
  35811. + struct user_namespace *h_dst_userns, *h_src_userns;
  35812. +
  35813. + h_src_userns = mnt_user_ns(h_src->mnt);
  35814. + h_src_dentry = h_src->dentry;
  35815. + ssz = vfs_getxattr_alloc(h_src_userns, h_src_dentry, name, buf, 0,
  35816. + GFP_NOFS);
  35817. + err = ssz;
  35818. + if (unlikely(err <= 0)) {
  35819. + if (err == -ENODATA
  35820. + || (err == -EOPNOTSUPP
  35821. + && ((ignore_flags & au_xattr_out_of_list)
  35822. + || (au_test_nfs_noacl(d_inode(h_src_dentry))
  35823. + && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
  35824. + || !strcmp(name,
  35825. + XATTR_NAME_POSIX_ACL_DEFAULT))))
  35826. + ))
  35827. + err = 0;
  35828. + if (err && (verbose || au_debug_test()))
  35829. + pr_err("%s, err %d\n", name, err);
  35830. + goto out;
  35831. + }
  35832. +
  35833. + /* unlock it temporary */
  35834. + h_dst_userns = mnt_user_ns(h_dst->mnt);
  35835. + h_dst_dentry = h_dst->dentry;
  35836. + h_idst = d_inode(h_dst_dentry);
  35837. + inode_unlock(h_idst);
  35838. + err = vfsub_setxattr(h_dst_userns, h_dst_dentry, name, *buf, ssz,
  35839. + /*flags*/0);
  35840. + inode_lock_nested(h_idst, AuLsc_I_CHILD2);
  35841. + if (unlikely(err)) {
  35842. + if (verbose || au_debug_test())
  35843. + pr_err("%s, err %d\n", name, err);
  35844. + err = au_xattr_ignore(err, name, ignore_flags);
  35845. + }
  35846. +
  35847. +out:
  35848. + return err;
  35849. +}
  35850. +
  35851. +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
  35852. + unsigned int verbose)
  35853. +{
  35854. + int err, unlocked, acl_access, acl_default;
  35855. + ssize_t ssz;
  35856. + struct dentry *h_dst_dentry, *h_src_dentry;
  35857. + struct inode *h_isrc, *h_idst;
  35858. + char *value, *p, *o, *e;
  35859. +
  35860. + /* try stopping to update the source inode while we are referencing */
  35861. + /* there should not be the parent-child relationship between them */
  35862. + h_dst_dentry = h_dst->dentry;
  35863. + h_idst = d_inode(h_dst_dentry);
  35864. + h_src_dentry = h_src->dentry;
  35865. + h_isrc = d_inode(h_src_dentry);
  35866. + inode_unlock(h_idst);
  35867. + inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
  35868. + inode_lock_nested(h_idst, AuLsc_I_CHILD2);
  35869. + unlocked = 0;
  35870. +
  35871. + /* some filesystems don't list POSIX ACL, for example tmpfs */
  35872. + ssz = vfs_listxattr(h_src_dentry, NULL, 0);
  35873. + err = ssz;
  35874. + if (unlikely(err < 0)) {
  35875. + AuTraceErr(err);
  35876. + if (err == -ENODATA
  35877. + || err == -EOPNOTSUPP)
  35878. + err = 0; /* ignore */
  35879. + goto out;
  35880. + }
  35881. +
  35882. + err = 0;
  35883. + p = NULL;
  35884. + o = NULL;
  35885. + if (ssz) {
  35886. + err = -ENOMEM;
  35887. + p = kmalloc(ssz, GFP_NOFS);
  35888. + o = p;
  35889. + if (unlikely(!p))
  35890. + goto out;
  35891. + err = vfs_listxattr(h_src_dentry, p, ssz);
  35892. + }
  35893. + inode_unlock_shared(h_isrc);
  35894. + unlocked = 1;
  35895. + AuDbg("err %d, ssz %zd\n", err, ssz);
  35896. + if (unlikely(err < 0))
  35897. + goto out_free;
  35898. +
  35899. + err = 0;
  35900. + e = p + ssz;
  35901. + value = NULL;
  35902. + acl_access = 0;
  35903. + acl_default = 0;
  35904. + while (!err && p < e) {
  35905. + acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
  35906. + sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
  35907. + acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
  35908. + sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
  35909. + - 1);
  35910. + err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
  35911. + verbose);
  35912. + p += strlen(p) + 1;
  35913. + }
  35914. + AuTraceErr(err);
  35915. + ignore_flags |= au_xattr_out_of_list;
  35916. + if (!err && !acl_access) {
  35917. + err = au_do_cpup_xattr(h_dst, h_src,
  35918. + XATTR_NAME_POSIX_ACL_ACCESS, &value,
  35919. + ignore_flags, verbose);
  35920. + AuTraceErr(err);
  35921. + }
  35922. + if (!err && !acl_default) {
  35923. + err = au_do_cpup_xattr(h_dst, h_src,
  35924. + XATTR_NAME_POSIX_ACL_DEFAULT, &value,
  35925. + ignore_flags, verbose);
  35926. + AuTraceErr(err);
  35927. + }
  35928. +
  35929. + au_kfree_try_rcu(value);
  35930. +
  35931. +out_free:
  35932. + au_kfree_try_rcu(o);
  35933. +out:
  35934. + if (!unlocked)
  35935. + inode_unlock_shared(h_isrc);
  35936. + AuTraceErr(err);
  35937. + return err;
  35938. +}
  35939. +
  35940. +/* ---------------------------------------------------------------------- */
  35941. +
  35942. +static int au_smack_reentering(struct super_block *sb)
  35943. +{
  35944. +#if IS_ENABLED(CONFIG_SECURITY_SMACK) || IS_ENABLED(CONFIG_SECURITY_SELINUX)
  35945. + /*
  35946. + * as a part of lookup, smack_d_instantiate() is called, and it calls
  35947. + * i_op->getxattr(). ouch.
  35948. + */
  35949. + return si_pid_test(sb);
  35950. +#else
  35951. + return 0;
  35952. +#endif
  35953. +}
  35954. +
  35955. +enum {
  35956. + AU_XATTR_LIST,
  35957. + AU_XATTR_GET
  35958. +};
  35959. +
  35960. +struct au_lgxattr {
  35961. + int type;
  35962. + union {
  35963. + struct {
  35964. + char *list;
  35965. + size_t size;
  35966. + } list;
  35967. + struct {
  35968. + const char *name;
  35969. + void *value;
  35970. + size_t size;
  35971. + } get;
  35972. + } u;
  35973. +};
  35974. +
  35975. +static ssize_t au_lgxattr(struct dentry *dentry, struct inode *inode,
  35976. + struct au_lgxattr *arg)
  35977. +{
  35978. + ssize_t err;
  35979. + int reenter;
  35980. + struct path h_path;
  35981. + struct super_block *sb;
  35982. +
  35983. + sb = dentry->d_sb;
  35984. + reenter = au_smack_reentering(sb);
  35985. + if (!reenter) {
  35986. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  35987. + if (unlikely(err))
  35988. + goto out;
  35989. + }
  35990. + err = au_h_path_getattr(dentry, inode, /*force*/1, &h_path, reenter);
  35991. + if (unlikely(err))
  35992. + goto out_si;
  35993. + if (unlikely(!h_path.dentry))
  35994. + /* illegally overlapped or something */
  35995. + goto out_di; /* pretending success */
  35996. +
  35997. + /* always topmost entry only */
  35998. + switch (arg->type) {
  35999. + case AU_XATTR_LIST:
  36000. + err = vfs_listxattr(h_path.dentry,
  36001. + arg->u.list.list, arg->u.list.size);
  36002. + break;
  36003. + case AU_XATTR_GET:
  36004. + AuDebugOn(d_is_negative(h_path.dentry));
  36005. + err = vfs_getxattr(mnt_user_ns(h_path.mnt), h_path.dentry,
  36006. + arg->u.get.name, arg->u.get.value,
  36007. + arg->u.get.size);
  36008. + break;
  36009. + }
  36010. +
  36011. +out_di:
  36012. + if (!reenter)
  36013. + di_read_unlock(dentry, AuLock_IR);
  36014. +out_si:
  36015. + if (!reenter)
  36016. + si_read_unlock(sb);
  36017. +out:
  36018. + AuTraceErr(err);
  36019. + return err;
  36020. +}
  36021. +
  36022. +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
  36023. +{
  36024. + struct au_lgxattr arg = {
  36025. + .type = AU_XATTR_LIST,
  36026. + .u.list = {
  36027. + .list = list,
  36028. + .size = size
  36029. + },
  36030. + };
  36031. +
  36032. + return au_lgxattr(dentry, /*inode*/NULL, &arg);
  36033. +}
  36034. +
  36035. +static ssize_t au_getxattr(struct dentry *dentry, struct inode *inode,
  36036. + const char *name, void *value, size_t size)
  36037. +{
  36038. + struct au_lgxattr arg = {
  36039. + .type = AU_XATTR_GET,
  36040. + .u.get = {
  36041. + .name = name,
  36042. + .value = value,
  36043. + .size = size
  36044. + },
  36045. + };
  36046. +
  36047. + return au_lgxattr(dentry, inode, &arg);
  36048. +}
  36049. +
  36050. +static int au_setxattr(struct dentry *dentry, struct inode *inode,
  36051. + const char *name, const void *value, size_t size,
  36052. + int flags)
  36053. +{
  36054. + struct au_sxattr arg = {
  36055. + .type = AU_XATTR_SET,
  36056. + .u.set = {
  36057. + .name = name,
  36058. + .value = value,
  36059. + .size = size,
  36060. + .flags = flags
  36061. + },
  36062. + };
  36063. +
  36064. + return au_sxattr(dentry, inode, &arg);
  36065. +}
  36066. +
  36067. +/* ---------------------------------------------------------------------- */
  36068. +
  36069. +static int au_xattr_get(const struct xattr_handler *handler,
  36070. + struct dentry *dentry, struct inode *inode,
  36071. + const char *name, void *buffer, size_t size)
  36072. +{
  36073. + return au_getxattr(dentry, inode, name, buffer, size);
  36074. +}
  36075. +
  36076. +static int au_xattr_set(const struct xattr_handler *handler,
  36077. + struct user_namespace *userns,
  36078. + struct dentry *dentry, struct inode *inode,
  36079. + const char *name, const void *value, size_t size,
  36080. + int flags)
  36081. +{
  36082. + return au_setxattr(dentry, inode, name, value, size, flags);
  36083. +}
  36084. +
  36085. +static const struct xattr_handler au_xattr_handler = {
  36086. + .name = "",
  36087. + .prefix = "",
  36088. + .get = au_xattr_get,
  36089. + .set = au_xattr_set
  36090. +};
  36091. +
  36092. +static const struct xattr_handler *au_xattr_handlers[] = {
  36093. +#ifdef CONFIG_FS_POSIX_ACL
  36094. + &posix_acl_access_xattr_handler,
  36095. + &posix_acl_default_xattr_handler,
  36096. +#endif
  36097. + &au_xattr_handler, /* must be last */
  36098. + NULL
  36099. +};
  36100. +
  36101. +void au_xattr_init(struct super_block *sb)
  36102. +{
  36103. + sb->s_xattr = au_xattr_handlers;
  36104. +}
  36105. diff -Naur null/fs/aufs/xino.c linux-5.15.131/fs/aufs/xino.c
  36106. --- /dev/null
  36107. +++ linux-5.15.131/fs/aufs/xino.c 2023-09-11 14:32:48.154722477 +0300
  36108. @@ -0,0 +1,1926 @@
  36109. +// SPDX-License-Identifier: GPL-2.0
  36110. +/*
  36111. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  36112. + *
  36113. + * This program, aufs is free software; you can redistribute it and/or modify
  36114. + * it under the terms of the GNU General Public License as published by
  36115. + * the Free Software Foundation; either version 2 of the License, or
  36116. + * (at your option) any later version.
  36117. + *
  36118. + * This program is distributed in the hope that it will be useful,
  36119. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  36120. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  36121. + * GNU General Public License for more details.
  36122. + *
  36123. + * You should have received a copy of the GNU General Public License
  36124. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  36125. + */
  36126. +
  36127. +/*
  36128. + * external inode number translation table and bitmap
  36129. + *
  36130. + * things to consider
  36131. + * - the lifetime
  36132. + * + au_xino object
  36133. + * + XINO files (xino, xib, xigen)
  36134. + * + dynamic debugfs entries (xiN)
  36135. + * + static debugfs entries (xib, xigen)
  36136. + * + static sysfs entry (xi_path)
  36137. + * - several entry points to handle them.
  36138. + * + mount(2) without xino option (default)
  36139. + * + mount(2) with xino option
  36140. + * + mount(2) with noxino option
  36141. + * + umount(2)
  36142. + * + remount with add/del branches
  36143. + * + remount with xino/noxino options
  36144. + */
  36145. +
  36146. +#include <linux/seq_file.h>
  36147. +#include <linux/statfs.h>
  36148. +#include "aufs.h"
  36149. +
  36150. +static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
  36151. + aufs_bindex_t bbot,
  36152. + struct super_block *h_sb)
  36153. +{
  36154. + /* todo: try binary-search if the branches are many */
  36155. + for (; btop <= bbot; btop++)
  36156. + if (h_sb == au_sbr_sb(sb, btop))
  36157. + return btop;
  36158. + return -1;
  36159. +}
  36160. +
  36161. +/*
  36162. + * find another branch who is on the same filesystem of the specified
  36163. + * branch{@btgt}. search until @bbot.
  36164. + */
  36165. +static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
  36166. + aufs_bindex_t bbot)
  36167. +{
  36168. + aufs_bindex_t bindex;
  36169. + struct super_block *tgt_sb;
  36170. +
  36171. + tgt_sb = au_sbr_sb(sb, btgt);
  36172. + bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
  36173. + if (bindex < 0)
  36174. + bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
  36175. +
  36176. + return bindex;
  36177. +}
  36178. +
  36179. +/* ---------------------------------------------------------------------- */
  36180. +
  36181. +/*
  36182. + * stop unnecessary notify events at creating xino files
  36183. + */
  36184. +
  36185. +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
  36186. +{
  36187. + aufs_bindex_t bfound, bindex, bbot;
  36188. + struct dentry *parent;
  36189. + struct au_branch *br;
  36190. +
  36191. + bfound = -1;
  36192. + parent = dentry->d_parent; /* safe d_parent access */
  36193. + bbot = au_sbbot(sb);
  36194. + for (bindex = 0; bindex <= bbot; bindex++) {
  36195. + br = au_sbr(sb, bindex);
  36196. + if (au_br_dentry(br) == parent) {
  36197. + bfound = bindex;
  36198. + break;
  36199. + }
  36200. + }
  36201. +
  36202. + AuDbg("bfound b%d\n", bfound);
  36203. + return bfound;
  36204. +}
  36205. +
  36206. +struct au_xino_lock_dir {
  36207. + struct au_hinode *hdir;
  36208. + struct dentry *parent;
  36209. + struct inode *dir;
  36210. +};
  36211. +
  36212. +static struct dentry *au_dget_parent_lock(struct dentry *dentry,
  36213. + unsigned int lsc)
  36214. +{
  36215. + struct dentry *parent;
  36216. + struct inode *dir;
  36217. +
  36218. + parent = dget_parent(dentry);
  36219. + dir = d_inode(parent);
  36220. + inode_lock_nested(dir, lsc);
  36221. +#if 0 /* it should not happen */
  36222. + spin_lock(&dentry->d_lock);
  36223. + if (unlikely(dentry->d_parent != parent)) {
  36224. + spin_unlock(&dentry->d_lock);
  36225. + inode_unlock(dir);
  36226. + dput(parent);
  36227. + parent = NULL;
  36228. + goto out;
  36229. + }
  36230. + spin_unlock(&dentry->d_lock);
  36231. +
  36232. +out:
  36233. +#endif
  36234. + return parent;
  36235. +}
  36236. +
  36237. +static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
  36238. + struct au_xino_lock_dir *ldir)
  36239. +{
  36240. + aufs_bindex_t bindex;
  36241. +
  36242. + ldir->hdir = NULL;
  36243. + bindex = au_xi_root(sb, xipath->dentry);
  36244. + if (bindex >= 0) {
  36245. + /* rw branch root */
  36246. + ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
  36247. + au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
  36248. + } else {
  36249. + /* other */
  36250. + ldir->parent = au_dget_parent_lock(xipath->dentry,
  36251. + AuLsc_I_PARENT);
  36252. + ldir->dir = d_inode(ldir->parent);
  36253. + }
  36254. +}
  36255. +
  36256. +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
  36257. +{
  36258. + if (ldir->hdir)
  36259. + au_hn_inode_unlock(ldir->hdir);
  36260. + else {
  36261. + inode_unlock(ldir->dir);
  36262. + dput(ldir->parent);
  36263. + }
  36264. +}
  36265. +
  36266. +/* ---------------------------------------------------------------------- */
  36267. +
  36268. +/*
  36269. + * create and set a new xino file
  36270. + */
  36271. +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
  36272. + int wbrtop)
  36273. +{
  36274. + struct file *file;
  36275. + struct dentry *h_parent, *d;
  36276. + struct inode *h_dir, *inode;
  36277. + int err;
  36278. + static DEFINE_MUTEX(mtx);
  36279. +
  36280. + /*
  36281. + * at mount-time, and the xino file is the default path,
  36282. + * hnotify is disabled so we have no notify events to ignore.
  36283. + * when a user specified the xino, we cannot get au_hdir to be ignored.
  36284. + */
  36285. + if (!wbrtop)
  36286. + mutex_lock(&mtx);
  36287. + file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
  36288. + /* | __FMODE_NONOTIFY */,
  36289. + 0666);
  36290. + if (IS_ERR(file)) {
  36291. + if (!wbrtop)
  36292. + mutex_unlock(&mtx);
  36293. + if (!silent)
  36294. + pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
  36295. + return file;
  36296. + }
  36297. +
  36298. + /* keep file count */
  36299. + err = 0;
  36300. + d = file->f_path.dentry;
  36301. + h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
  36302. + if (!wbrtop)
  36303. + mutex_unlock(&mtx);
  36304. + /* mnt_want_write() is unnecessary here */
  36305. + h_dir = d_inode(h_parent);
  36306. + inode = file_inode(file);
  36307. + /* no delegation since it is just created */
  36308. + if (inode->i_nlink)
  36309. + err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
  36310. + /*force*/0);
  36311. + inode_unlock(h_dir);
  36312. + dput(h_parent);
  36313. + if (unlikely(err)) {
  36314. + if (!silent)
  36315. + pr_err("unlink %s(%d)\n", fpath, err);
  36316. + goto out;
  36317. + }
  36318. +
  36319. + err = -EINVAL;
  36320. + if (unlikely(sb && sb == d->d_sb)) {
  36321. + if (!silent)
  36322. + pr_err("%s must be outside\n", fpath);
  36323. + goto out;
  36324. + }
  36325. + if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
  36326. + if (!silent)
  36327. + pr_err("xino doesn't support %s(%s)\n",
  36328. + fpath, au_sbtype(d->d_sb));
  36329. + goto out;
  36330. + }
  36331. + return file; /* success */
  36332. +
  36333. +out:
  36334. + fput(file);
  36335. + file = ERR_PTR(err);
  36336. + return file;
  36337. +}
  36338. +
  36339. +/*
  36340. + * create a new xinofile at the same place/path as @base.
  36341. + */
  36342. +struct file *au_xino_create2(struct super_block *sb, struct path *base,
  36343. + struct file *copy_src)
  36344. +{
  36345. + struct file *file;
  36346. + struct dentry *dentry;
  36347. + struct inode *dir, *delegated;
  36348. + struct qstr *name;
  36349. + struct path ppath, path;
  36350. + int err, do_unlock;
  36351. + struct au_xino_lock_dir ldir;
  36352. +
  36353. + do_unlock = 1;
  36354. + au_xino_lock_dir(sb, base, &ldir);
  36355. + dentry = base->dentry;
  36356. + ppath.dentry = dentry->d_parent; /* dir inode is locked */
  36357. + ppath.mnt = base->mnt;
  36358. + dir = d_inode(ppath.dentry);
  36359. + IMustLock(dir);
  36360. +
  36361. + name = &dentry->d_name;
  36362. + path.dentry = vfsub_lookup_one_len(name->name, &ppath, name->len);
  36363. + if (IS_ERR(path.dentry)) {
  36364. + file = (void *)path.dentry;
  36365. + pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
  36366. + goto out;
  36367. + }
  36368. +
  36369. + /* no need to mnt_want_write() since we call dentry_open() later */
  36370. + err = vfs_create(mnt_user_ns(base->mnt), dir, path.dentry, 0666, NULL);
  36371. + if (unlikely(err)) {
  36372. + file = ERR_PTR(err);
  36373. + pr_err("%pd create err %d\n", dentry, err);
  36374. + goto out_dput;
  36375. + }
  36376. +
  36377. + path.mnt = base->mnt;
  36378. + file = vfsub_dentry_open(&path,
  36379. + O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
  36380. + /* | __FMODE_NONOTIFY */);
  36381. + if (IS_ERR(file)) {
  36382. + pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
  36383. + goto out_dput;
  36384. + }
  36385. +
  36386. + delegated = NULL;
  36387. + err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
  36388. + au_xino_unlock_dir(&ldir);
  36389. + do_unlock = 0;
  36390. + if (unlikely(err == -EWOULDBLOCK)) {
  36391. + pr_warn("cannot retry for NFSv4 delegation"
  36392. + " for an internal unlink\n");
  36393. + iput(delegated);
  36394. + }
  36395. + if (unlikely(err)) {
  36396. + pr_err("%pd unlink err %d\n", dentry, err);
  36397. + goto out_fput;
  36398. + }
  36399. +
  36400. + if (copy_src) {
  36401. + /* no one can touch copy_src xino */
  36402. + err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
  36403. + if (unlikely(err)) {
  36404. + pr_err("%pd copy err %d\n", dentry, err);
  36405. + goto out_fput;
  36406. + }
  36407. + }
  36408. + goto out_dput; /* success */
  36409. +
  36410. +out_fput:
  36411. + fput(file);
  36412. + file = ERR_PTR(err);
  36413. +out_dput:
  36414. + dput(path.dentry);
  36415. +out:
  36416. + if (do_unlock)
  36417. + au_xino_unlock_dir(&ldir);
  36418. + return file;
  36419. +}
  36420. +
  36421. +struct file *au_xino_file1(struct au_xino *xi)
  36422. +{
  36423. + struct file *file;
  36424. + unsigned int u, nfile;
  36425. +
  36426. + file = NULL;
  36427. + nfile = xi->xi_nfile;
  36428. + for (u = 0; u < nfile; u++) {
  36429. + file = xi->xi_file[u];
  36430. + if (file)
  36431. + break;
  36432. + }
  36433. +
  36434. + return file;
  36435. +}
  36436. +
  36437. +static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
  36438. +{
  36439. + int err;
  36440. + struct file *f;
  36441. + void *p;
  36442. +
  36443. + if (file)
  36444. + get_file(file);
  36445. +
  36446. + err = 0;
  36447. + f = NULL;
  36448. + if (idx < xi->xi_nfile) {
  36449. + f = xi->xi_file[idx];
  36450. + if (f)
  36451. + fput(f);
  36452. + } else {
  36453. + p = au_kzrealloc(xi->xi_file,
  36454. + sizeof(*xi->xi_file) * xi->xi_nfile,
  36455. + sizeof(*xi->xi_file) * (idx + 1),
  36456. + GFP_NOFS, /*may_shrink*/0);
  36457. + if (p) {
  36458. + MtxMustLock(&xi->xi_mtx);
  36459. + xi->xi_file = p;
  36460. + xi->xi_nfile = idx + 1;
  36461. + } else {
  36462. + err = -ENOMEM;
  36463. + if (file)
  36464. + fput(file);
  36465. + goto out;
  36466. + }
  36467. + }
  36468. + xi->xi_file[idx] = file;
  36469. +
  36470. +out:
  36471. + return err;
  36472. +}
  36473. +
  36474. +/*
  36475. + * if @xinew->xi is not set, then create new xigen file.
  36476. + */
  36477. +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
  36478. +{
  36479. + struct file *file;
  36480. + int err;
  36481. +
  36482. + SiMustAnyLock(sb);
  36483. +
  36484. + file = au_xino_create2(sb, xinew->base, xinew->copy_src);
  36485. + if (IS_ERR(file)) {
  36486. + err = PTR_ERR(file);
  36487. + pr_err("%s[%d], err %d\n",
  36488. + xinew->xi ? "xino" : "xigen",
  36489. + xinew->idx, err);
  36490. + goto out;
  36491. + }
  36492. +
  36493. + if (xinew->xi)
  36494. + err = au_xino_file_set(xinew->xi, xinew->idx, file);
  36495. + else {
  36496. + BUG();
  36497. + /* todo: make xigen file an array */
  36498. + /* err = au_xigen_file_set(sb, xinew->idx, file); */
  36499. + }
  36500. + fput(file);
  36501. + if (unlikely(err))
  36502. + file = ERR_PTR(err);
  36503. +
  36504. +out:
  36505. + return file;
  36506. +}
  36507. +
  36508. +/* ---------------------------------------------------------------------- */
  36509. +
  36510. +/*
  36511. + * truncate xino files
  36512. + */
  36513. +static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
  36514. + int idx, struct kstatfs *st)
  36515. +{
  36516. + int err;
  36517. + blkcnt_t blocks;
  36518. + struct file *file, *new_xino;
  36519. + struct au_xi_new xinew = {
  36520. + .idx = idx
  36521. + };
  36522. +
  36523. + err = 0;
  36524. + xinew.xi = au_sbr(sb, bindex)->br_xino;
  36525. + file = au_xino_file(xinew.xi, idx);
  36526. + if (!file)
  36527. + goto out;
  36528. +
  36529. + xinew.base = &file->f_path;
  36530. + err = vfs_statfs(xinew.base, st);
  36531. + if (unlikely(err)) {
  36532. + AuErr1("statfs err %d, ignored\n", err);
  36533. + err = 0;
  36534. + goto out;
  36535. + }
  36536. +
  36537. + blocks = file_inode(file)->i_blocks;
  36538. + pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
  36539. + bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
  36540. +
  36541. + xinew.copy_src = file;
  36542. + new_xino = au_xi_new(sb, &xinew);
  36543. + if (IS_ERR(new_xino)) {
  36544. + err = PTR_ERR(new_xino);
  36545. + pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
  36546. + goto out;
  36547. + }
  36548. +
  36549. + err = vfs_statfs(&new_xino->f_path, st);
  36550. + if (!err)
  36551. + pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
  36552. + bindex, idx, (u64)file_inode(new_xino)->i_blocks,
  36553. + st->f_bfree, st->f_blocks);
  36554. + else {
  36555. + AuErr1("statfs err %d, ignored\n", err);
  36556. + err = 0;
  36557. + }
  36558. +
  36559. +out:
  36560. + return err;
  36561. +}
  36562. +
  36563. +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
  36564. +{
  36565. + int err, i;
  36566. + unsigned long jiffy;
  36567. + aufs_bindex_t bbot;
  36568. + struct kstatfs *st;
  36569. + struct au_branch *br;
  36570. + struct au_xino *xi;
  36571. +
  36572. + err = -ENOMEM;
  36573. + st = kmalloc(sizeof(*st), GFP_NOFS);
  36574. + if (unlikely(!st))
  36575. + goto out;
  36576. +
  36577. + err = -EINVAL;
  36578. + bbot = au_sbbot(sb);
  36579. + if (unlikely(bindex < 0 || bbot < bindex))
  36580. + goto out_st;
  36581. +
  36582. + err = 0;
  36583. + jiffy = jiffies;
  36584. + br = au_sbr(sb, bindex);
  36585. + xi = br->br_xino;
  36586. + for (i = idx_begin; !err && i < xi->xi_nfile; i++)
  36587. + err = au_xino_do_trunc(sb, bindex, i, st);
  36588. + if (!err)
  36589. + au_sbi(sb)->si_xino_jiffy = jiffy;
  36590. +
  36591. +out_st:
  36592. + au_kfree_rcu(st);
  36593. +out:
  36594. + return err;
  36595. +}
  36596. +
  36597. +struct xino_do_trunc_args {
  36598. + struct super_block *sb;
  36599. + struct au_branch *br;
  36600. + int idx;
  36601. +};
  36602. +
  36603. +static void xino_do_trunc(void *_args)
  36604. +{
  36605. + struct xino_do_trunc_args *args = _args;
  36606. + struct super_block *sb;
  36607. + struct au_branch *br;
  36608. + struct inode *dir;
  36609. + int err, idx;
  36610. + aufs_bindex_t bindex;
  36611. +
  36612. + err = 0;
  36613. + sb = args->sb;
  36614. + dir = d_inode(sb->s_root);
  36615. + br = args->br;
  36616. + idx = args->idx;
  36617. +
  36618. + si_noflush_write_lock(sb);
  36619. + ii_read_lock_parent(dir);
  36620. + bindex = au_br_index(sb, br->br_id);
  36621. + err = au_xino_trunc(sb, bindex, idx);
  36622. + ii_read_unlock(dir);
  36623. + if (unlikely(err))
  36624. + pr_warn("err b%d, (%d)\n", bindex, err);
  36625. + atomic_dec(&br->br_xino->xi_truncating);
  36626. + au_lcnt_dec(&br->br_count);
  36627. + si_write_unlock(sb);
  36628. + au_nwt_done(&au_sbi(sb)->si_nowait);
  36629. + au_kfree_rcu(args);
  36630. +}
  36631. +
  36632. +/*
  36633. + * returns the index in the xi_file array whose corresponding file is necessary
  36634. + * to truncate, or -1 which means no need to truncate.
  36635. + */
  36636. +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
  36637. +{
  36638. + int err;
  36639. + unsigned int u;
  36640. + struct kstatfs st;
  36641. + struct au_sbinfo *sbinfo;
  36642. + struct au_xino *xi;
  36643. + struct file *file;
  36644. +
  36645. + /* todo: si_xino_expire and the ratio should be customizable */
  36646. + sbinfo = au_sbi(sb);
  36647. + if (time_before(jiffies,
  36648. + sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
  36649. + return -1;
  36650. +
  36651. + /* truncation border */
  36652. + xi = br->br_xino;
  36653. + for (u = 0; u < xi->xi_nfile; u++) {
  36654. + file = au_xino_file(xi, u);
  36655. + if (!file)
  36656. + continue;
  36657. +
  36658. + err = vfs_statfs(&file->f_path, &st);
  36659. + if (unlikely(err)) {
  36660. + AuErr1("statfs err %d, ignored\n", err);
  36661. + return -1;
  36662. + }
  36663. + if (div64_u64(st.f_bfree * 100, st.f_blocks)
  36664. + >= AUFS_XINO_DEF_TRUNC)
  36665. + return u;
  36666. + }
  36667. +
  36668. + return -1;
  36669. +}
  36670. +
  36671. +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
  36672. +{
  36673. + int idx;
  36674. + struct xino_do_trunc_args *args;
  36675. + int wkq_err;
  36676. +
  36677. + idx = xino_trunc_test(sb, br);
  36678. + if (idx < 0)
  36679. + return;
  36680. +
  36681. + if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
  36682. + goto out;
  36683. +
  36684. + /* lock and kfree() will be called in trunc_xino() */
  36685. + args = kmalloc(sizeof(*args), GFP_NOFS);
  36686. + if (unlikely(!args)) {
  36687. + AuErr1("no memory\n");
  36688. + goto out;
  36689. + }
  36690. +
  36691. + au_lcnt_inc(&br->br_count);
  36692. + args->sb = sb;
  36693. + args->br = br;
  36694. + args->idx = idx;
  36695. + wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
  36696. + if (!wkq_err)
  36697. + return; /* success */
  36698. +
  36699. + pr_err("wkq %d\n", wkq_err);
  36700. + au_lcnt_dec(&br->br_count);
  36701. + au_kfree_rcu(args);
  36702. +
  36703. +out:
  36704. + atomic_dec(&br->br_xino->xi_truncating);
  36705. +}
  36706. +
  36707. +/* ---------------------------------------------------------------------- */
  36708. +
  36709. +struct au_xi_calc {
  36710. + int idx;
  36711. + loff_t pos;
  36712. +};
  36713. +
  36714. +static void au_xi_calc(struct super_block *sb, ino_t h_ino,
  36715. + struct au_xi_calc *calc)
  36716. +{
  36717. + loff_t maxent;
  36718. +
  36719. + maxent = au_xi_maxent(sb);
  36720. + calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
  36721. + calc->pos *= sizeof(ino_t);
  36722. +}
  36723. +
  36724. +static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
  36725. + struct au_xi_calc *calc)
  36726. +{
  36727. + int err;
  36728. + struct file *file;
  36729. + struct au_xino *xi = br->br_xino;
  36730. + struct au_xi_new xinew = {
  36731. + .xi = xi
  36732. + };
  36733. +
  36734. + SiMustAnyLock(sb);
  36735. +
  36736. + err = 0;
  36737. + if (!xi)
  36738. + goto out;
  36739. +
  36740. + mutex_lock(&xi->xi_mtx);
  36741. + file = au_xino_file(xi, calc->idx);
  36742. + if (file)
  36743. + goto out_mtx;
  36744. +
  36745. + file = au_xino_file(xi, /*idx*/-1);
  36746. + AuDebugOn(!file);
  36747. + xinew.idx = calc->idx;
  36748. + xinew.base = &file->f_path;
  36749. + /* xinew.copy_src = NULL; */
  36750. + file = au_xi_new(sb, &xinew);
  36751. + if (IS_ERR(file))
  36752. + err = PTR_ERR(file);
  36753. +
  36754. +out_mtx:
  36755. + mutex_unlock(&xi->xi_mtx);
  36756. +out:
  36757. + return err;
  36758. +}
  36759. +
  36760. +struct au_xino_do_new_async_args {
  36761. + struct super_block *sb;
  36762. + struct au_branch *br;
  36763. + struct au_xi_calc calc;
  36764. + ino_t ino;
  36765. +};
  36766. +
  36767. +struct au_xi_writing {
  36768. + struct hlist_bl_node node;
  36769. + ino_t h_ino, ino;
  36770. +};
  36771. +
  36772. +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
  36773. + ino_t ino);
  36774. +
  36775. +static void au_xino_call_do_new_async(void *args)
  36776. +{
  36777. + struct au_xino_do_new_async_args *a = args;
  36778. + struct au_branch *br;
  36779. + struct super_block *sb;
  36780. + struct au_sbinfo *sbi;
  36781. + struct inode *root;
  36782. + struct file *file;
  36783. + struct au_xi_writing *del, *p;
  36784. + struct hlist_bl_head *hbl;
  36785. + struct hlist_bl_node *pos;
  36786. + int err;
  36787. +
  36788. + br = a->br;
  36789. + sb = a->sb;
  36790. + sbi = au_sbi(sb);
  36791. + si_noflush_read_lock(sb);
  36792. + root = d_inode(sb->s_root);
  36793. + ii_read_lock_child(root);
  36794. + err = au_xino_do_new_async(sb, br, &a->calc);
  36795. + if (unlikely(err)) {
  36796. + AuIOErr("err %d\n", err);
  36797. + goto out;
  36798. + }
  36799. +
  36800. + file = au_xino_file(br->br_xino, a->calc.idx);
  36801. + AuDebugOn(!file);
  36802. + err = au_xino_do_write(file, &a->calc, a->ino);
  36803. + if (unlikely(err)) {
  36804. + AuIOErr("err %d\n", err);
  36805. + goto out;
  36806. + }
  36807. +
  36808. + del = NULL;
  36809. + hbl = &br->br_xino->xi_writing;
  36810. + hlist_bl_lock(hbl);
  36811. + au_hbl_for_each(pos, hbl) {
  36812. + p = container_of(pos, struct au_xi_writing, node);
  36813. + if (p->ino == a->ino) {
  36814. + del = p;
  36815. + hlist_bl_del(&p->node);
  36816. + break;
  36817. + }
  36818. + }
  36819. + hlist_bl_unlock(hbl);
  36820. + au_kfree_rcu(del);
  36821. +
  36822. +out:
  36823. + au_lcnt_dec(&br->br_count);
  36824. + ii_read_unlock(root);
  36825. + si_read_unlock(sb);
  36826. + au_nwt_done(&sbi->si_nowait);
  36827. + au_kfree_rcu(a);
  36828. +}
  36829. +
  36830. +/*
  36831. + * create a new xino file asynchronously
  36832. + */
  36833. +static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
  36834. + struct au_xi_calc *calc, ino_t ino)
  36835. +{
  36836. + int err;
  36837. + struct au_xino_do_new_async_args *arg;
  36838. +
  36839. + err = -ENOMEM;
  36840. + arg = kmalloc(sizeof(*arg), GFP_NOFS);
  36841. + if (unlikely(!arg))
  36842. + goto out;
  36843. +
  36844. + arg->sb = sb;
  36845. + arg->br = br;
  36846. + arg->calc = *calc;
  36847. + arg->ino = ino;
  36848. + au_lcnt_inc(&br->br_count);
  36849. + err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
  36850. + if (unlikely(err)) {
  36851. + pr_err("wkq %d\n", err);
  36852. + au_lcnt_dec(&br->br_count);
  36853. + au_kfree_rcu(arg);
  36854. + }
  36855. +
  36856. +out:
  36857. + return err;
  36858. +}
  36859. +
  36860. +/*
  36861. + * read @ino from xinofile for the specified branch{@sb, @bindex}
  36862. + * at the position of @h_ino.
  36863. + */
  36864. +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  36865. + ino_t *ino)
  36866. +{
  36867. + int err;
  36868. + ssize_t sz;
  36869. + struct au_xi_calc calc;
  36870. + struct au_sbinfo *sbinfo;
  36871. + struct file *file;
  36872. + struct au_xino *xi;
  36873. + struct hlist_bl_head *hbl;
  36874. + struct hlist_bl_node *pos;
  36875. + struct au_xi_writing *p;
  36876. +
  36877. + *ino = 0;
  36878. + if (!au_opt_test(au_mntflags(sb), XINO))
  36879. + return 0; /* no xino */
  36880. +
  36881. + err = 0;
  36882. + au_xi_calc(sb, h_ino, &calc);
  36883. + xi = au_sbr(sb, bindex)->br_xino;
  36884. + file = au_xino_file(xi, calc.idx);
  36885. + if (!file) {
  36886. + hbl = &xi->xi_writing;
  36887. + hlist_bl_lock(hbl);
  36888. + au_hbl_for_each(pos, hbl) {
  36889. + p = container_of(pos, struct au_xi_writing, node);
  36890. + if (p->h_ino == h_ino) {
  36891. + AuDbg("hi%llu, i%llu, found\n",
  36892. + (u64)p->h_ino, (u64)p->ino);
  36893. + *ino = p->ino;
  36894. + break;
  36895. + }
  36896. + }
  36897. + hlist_bl_unlock(hbl);
  36898. + return 0;
  36899. + } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
  36900. + return 0; /* no xino */
  36901. +
  36902. + sbinfo = au_sbi(sb);
  36903. + sz = xino_fread(file, ino, sizeof(*ino), &calc.pos);
  36904. + if (sz == sizeof(*ino))
  36905. + return 0; /* success */
  36906. +
  36907. + err = sz;
  36908. + if (unlikely(sz >= 0)) {
  36909. + err = -EIO;
  36910. + AuIOErr("xino read error (%zd)\n", sz);
  36911. + }
  36912. + return err;
  36913. +}
  36914. +
  36915. +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
  36916. + ino_t ino)
  36917. +{
  36918. + ssize_t sz;
  36919. +
  36920. + sz = xino_fwrite(file, &ino, sizeof(ino), &calc->pos);
  36921. + if (sz == sizeof(ino))
  36922. + return 0; /* success */
  36923. +
  36924. + AuIOErr("write failed (%zd)\n", sz);
  36925. + return -EIO;
  36926. +}
  36927. +
  36928. +/*
  36929. + * write @ino to the xinofile for the specified branch{@sb, @bindex}
  36930. + * at the position of @h_ino.
  36931. + * even if @ino is zero, it is written to the xinofile and means no entry.
  36932. + * if the size of the xino file on a specific filesystem exceeds the watermark,
  36933. + * try truncating it.
  36934. + */
  36935. +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  36936. + ino_t ino)
  36937. +{
  36938. + int err;
  36939. + unsigned int mnt_flags;
  36940. + struct au_xi_calc calc;
  36941. + struct file *file;
  36942. + struct au_branch *br;
  36943. + struct au_xino *xi;
  36944. + struct au_xi_writing *p;
  36945. +
  36946. + SiMustAnyLock(sb);
  36947. +
  36948. + mnt_flags = au_mntflags(sb);
  36949. + if (!au_opt_test(mnt_flags, XINO))
  36950. + return 0;
  36951. +
  36952. + au_xi_calc(sb, h_ino, &calc);
  36953. + br = au_sbr(sb, bindex);
  36954. + xi = br->br_xino;
  36955. + file = au_xino_file(xi, calc.idx);
  36956. + if (!file) {
  36957. + /* store the inum pair into the list */
  36958. + p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
  36959. + p->h_ino = h_ino;
  36960. + p->ino = ino;
  36961. + au_hbl_add(&p->node, &xi->xi_writing);
  36962. +
  36963. + /* create and write a new xino file asynchronously */
  36964. + err = au_xino_new_async(sb, br, &calc, ino);
  36965. + if (!err)
  36966. + return 0; /* success */
  36967. + goto out;
  36968. + }
  36969. +
  36970. + err = au_xino_do_write(file, &calc, ino);
  36971. + if (!err) {
  36972. + br = au_sbr(sb, bindex);
  36973. + if (au_opt_test(mnt_flags, TRUNC_XINO)
  36974. + && au_test_fs_trunc_xino(au_br_sb(br)))
  36975. + xino_try_trunc(sb, br);
  36976. + return 0; /* success */
  36977. + }
  36978. +
  36979. +out:
  36980. + AuIOErr("write failed (%d)\n", err);
  36981. + return -EIO;
  36982. +}
  36983. +
  36984. +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
  36985. + loff_t *pos);
  36986. +
  36987. +/* todo: unnecessary to support mmap_sem since kernel-space? */
  36988. +ssize_t xino_fread(struct file *file, void *kbuf, size_t size, loff_t *pos)
  36989. +{
  36990. + ssize_t err;
  36991. + int i;
  36992. + const int prevent_endless = 10;
  36993. +
  36994. + i = 0;
  36995. + do {
  36996. + err = vfsub_read_k(file, kbuf, size, pos);
  36997. + if (err == -EINTR
  36998. + && !au_wkq_test()
  36999. + && fatal_signal_pending(current)) {
  37000. + err = xino_fread_wkq(file, kbuf, size, pos);
  37001. + BUG_ON(err == -EINTR);
  37002. + }
  37003. + } while (i++ < prevent_endless
  37004. + && (err == -EAGAIN || err == -EINTR));
  37005. +
  37006. +#if 0 /* reserved for future use */
  37007. + if (err > 0)
  37008. + fsnotify_access(file->f_path.dentry);
  37009. +#endif
  37010. +
  37011. + return err;
  37012. +}
  37013. +
  37014. +struct xino_fread_args {
  37015. + ssize_t *errp;
  37016. + struct file *file;
  37017. + void *buf;
  37018. + size_t size;
  37019. + loff_t *pos;
  37020. +};
  37021. +
  37022. +static void call_xino_fread(void *args)
  37023. +{
  37024. + struct xino_fread_args *a = args;
  37025. + *a->errp = xino_fread(a->file, a->buf, a->size, a->pos);
  37026. +}
  37027. +
  37028. +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
  37029. + loff_t *pos)
  37030. +{
  37031. + ssize_t err;
  37032. + int wkq_err;
  37033. + struct xino_fread_args args = {
  37034. + .errp = &err,
  37035. + .file = file,
  37036. + .buf = buf,
  37037. + .size = size,
  37038. + .pos = pos
  37039. + };
  37040. +
  37041. + wkq_err = au_wkq_wait(call_xino_fread, &args);
  37042. + if (unlikely(wkq_err))
  37043. + err = wkq_err;
  37044. +
  37045. + return err;
  37046. +}
  37047. +
  37048. +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
  37049. + loff_t *pos);
  37050. +
  37051. +static ssize_t do_xino_fwrite(struct file *file, void *kbuf, size_t size,
  37052. + loff_t *pos)
  37053. +{
  37054. + ssize_t err;
  37055. + int i;
  37056. + const int prevent_endless = 10;
  37057. +
  37058. + i = 0;
  37059. + do {
  37060. + err = vfsub_write_k(file, kbuf, size, pos);
  37061. + if (err == -EINTR
  37062. + && !au_wkq_test()
  37063. + && fatal_signal_pending(current)) {
  37064. + err = xino_fwrite_wkq(file, kbuf, size, pos);
  37065. + BUG_ON(err == -EINTR);
  37066. + }
  37067. + } while (i++ < prevent_endless
  37068. + && (err == -EAGAIN || err == -EINTR));
  37069. +
  37070. +#if 0 /* reserved for future use */
  37071. + if (err > 0)
  37072. + fsnotify_modify(file->f_path.dentry);
  37073. +#endif
  37074. +
  37075. + return err;
  37076. +}
  37077. +
  37078. +struct do_xino_fwrite_args {
  37079. + ssize_t *errp;
  37080. + struct file *file;
  37081. + void *buf;
  37082. + size_t size;
  37083. + loff_t *pos;
  37084. +};
  37085. +
  37086. +static void call_do_xino_fwrite(void *args)
  37087. +{
  37088. + struct do_xino_fwrite_args *a = args;
  37089. + *a->errp = do_xino_fwrite(a->file, a->buf, a->size, a->pos);
  37090. +}
  37091. +
  37092. +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
  37093. + loff_t *pos)
  37094. +{
  37095. + ssize_t err;
  37096. + int wkq_err;
  37097. + struct do_xino_fwrite_args args = {
  37098. + .errp = &err,
  37099. + .file = file,
  37100. + .buf = buf,
  37101. + .size = size,
  37102. + .pos = pos
  37103. + };
  37104. +
  37105. + /*
  37106. + * it breaks RLIMIT_FSIZE and normal user's limit,
  37107. + * users should care about quota and real 'filesystem full.'
  37108. + */
  37109. + wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
  37110. + if (unlikely(wkq_err))
  37111. + err = wkq_err;
  37112. +
  37113. + return err;
  37114. +}
  37115. +
  37116. +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos)
  37117. +{
  37118. + ssize_t err;
  37119. +
  37120. + if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
  37121. + lockdep_off();
  37122. + err = do_xino_fwrite(file, buf, size, pos);
  37123. + lockdep_on();
  37124. + } else {
  37125. + lockdep_off();
  37126. + err = xino_fwrite_wkq(file, buf, size, pos);
  37127. + lockdep_on();
  37128. + }
  37129. +
  37130. + return err;
  37131. +}
  37132. +
  37133. +/* ---------------------------------------------------------------------- */
  37134. +
  37135. +/*
  37136. + * inode number bitmap
  37137. + */
  37138. +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
  37139. +static ino_t xib_calc_ino(unsigned long pindex, int bit)
  37140. +{
  37141. + ino_t ino;
  37142. +
  37143. + AuDebugOn(bit < 0 || page_bits <= bit);
  37144. + ino = AUFS_FIRST_INO + pindex * page_bits + bit;
  37145. + return ino;
  37146. +}
  37147. +
  37148. +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
  37149. +{
  37150. + AuDebugOn(ino < AUFS_FIRST_INO);
  37151. + ino -= AUFS_FIRST_INO;
  37152. + *pindex = ino / page_bits;
  37153. + *bit = ino % page_bits;
  37154. +}
  37155. +
  37156. +static int xib_pindex(struct super_block *sb, unsigned long pindex)
  37157. +{
  37158. + int err;
  37159. + loff_t pos;
  37160. + ssize_t sz;
  37161. + struct au_sbinfo *sbinfo;
  37162. + struct file *xib;
  37163. + unsigned long *p;
  37164. +
  37165. + sbinfo = au_sbi(sb);
  37166. + MtxMustLock(&sbinfo->si_xib_mtx);
  37167. + AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
  37168. + || !au_opt_test(sbinfo->si_mntflags, XINO));
  37169. +
  37170. + if (pindex == sbinfo->si_xib_last_pindex)
  37171. + return 0;
  37172. +
  37173. + xib = sbinfo->si_xib;
  37174. + p = sbinfo->si_xib_buf;
  37175. + pos = sbinfo->si_xib_last_pindex;
  37176. + pos *= PAGE_SIZE;
  37177. + sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
  37178. + if (unlikely(sz != PAGE_SIZE))
  37179. + goto out;
  37180. +
  37181. + pos = pindex;
  37182. + pos *= PAGE_SIZE;
  37183. + if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
  37184. + sz = xino_fread(xib, p, PAGE_SIZE, &pos);
  37185. + else {
  37186. + memset(p, 0, PAGE_SIZE);
  37187. + sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
  37188. + }
  37189. + if (sz == PAGE_SIZE) {
  37190. + sbinfo->si_xib_last_pindex = pindex;
  37191. + return 0; /* success */
  37192. + }
  37193. +
  37194. +out:
  37195. + AuIOErr1("write failed (%zd)\n", sz);
  37196. + err = sz;
  37197. + if (sz >= 0)
  37198. + err = -EIO;
  37199. + return err;
  37200. +}
  37201. +
  37202. +static void au_xib_clear_bit(struct inode *inode)
  37203. +{
  37204. + int err, bit;
  37205. + unsigned long pindex;
  37206. + struct super_block *sb;
  37207. + struct au_sbinfo *sbinfo;
  37208. +
  37209. + AuDebugOn(inode->i_nlink);
  37210. +
  37211. + sb = inode->i_sb;
  37212. + xib_calc_bit(inode->i_ino, &pindex, &bit);
  37213. + AuDebugOn(page_bits <= bit);
  37214. + sbinfo = au_sbi(sb);
  37215. + mutex_lock(&sbinfo->si_xib_mtx);
  37216. + err = xib_pindex(sb, pindex);
  37217. + if (!err) {
  37218. + clear_bit(bit, sbinfo->si_xib_buf);
  37219. + sbinfo->si_xib_next_bit = bit;
  37220. + }
  37221. + mutex_unlock(&sbinfo->si_xib_mtx);
  37222. +}
  37223. +
  37224. +/* ---------------------------------------------------------------------- */
  37225. +
  37226. +/*
  37227. + * truncate a xino bitmap file
  37228. + */
  37229. +
  37230. +/* todo: slow */
  37231. +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
  37232. +{
  37233. + int err, bit;
  37234. + ssize_t sz;
  37235. + unsigned long pindex;
  37236. + loff_t pos, pend;
  37237. + struct au_sbinfo *sbinfo;
  37238. + ino_t *ino;
  37239. + unsigned long *p;
  37240. +
  37241. + err = 0;
  37242. + sbinfo = au_sbi(sb);
  37243. + MtxMustLock(&sbinfo->si_xib_mtx);
  37244. + p = sbinfo->si_xib_buf;
  37245. + pend = vfsub_f_size_read(file);
  37246. + pos = 0;
  37247. + while (pos < pend) {
  37248. + sz = xino_fread(file, page, PAGE_SIZE, &pos);
  37249. + err = sz;
  37250. + if (unlikely(sz <= 0))
  37251. + goto out;
  37252. +
  37253. + err = 0;
  37254. + for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
  37255. + if (unlikely(*ino < AUFS_FIRST_INO))
  37256. + continue;
  37257. +
  37258. + xib_calc_bit(*ino, &pindex, &bit);
  37259. + AuDebugOn(page_bits <= bit);
  37260. + err = xib_pindex(sb, pindex);
  37261. + if (!err)
  37262. + set_bit(bit, p);
  37263. + else
  37264. + goto out;
  37265. + }
  37266. + }
  37267. +
  37268. +out:
  37269. + return err;
  37270. +}
  37271. +
  37272. +static int xib_restore(struct super_block *sb)
  37273. +{
  37274. + int err, i;
  37275. + unsigned int nfile;
  37276. + aufs_bindex_t bindex, bbot;
  37277. + void *page;
  37278. + struct au_branch *br;
  37279. + struct au_xino *xi;
  37280. + struct file *file;
  37281. +
  37282. + err = -ENOMEM;
  37283. + page = (void *)__get_free_page(GFP_NOFS);
  37284. + if (unlikely(!page))
  37285. + goto out;
  37286. +
  37287. + err = 0;
  37288. + bbot = au_sbbot(sb);
  37289. + for (bindex = 0; !err && bindex <= bbot; bindex++)
  37290. + if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
  37291. + br = au_sbr(sb, bindex);
  37292. + xi = br->br_xino;
  37293. + nfile = xi->xi_nfile;
  37294. + for (i = 0; i < nfile; i++) {
  37295. + file = au_xino_file(xi, i);
  37296. + if (file)
  37297. + err = do_xib_restore(sb, file, page);
  37298. + }
  37299. + } else
  37300. + AuDbg("skip shared b%d\n", bindex);
  37301. + free_page((unsigned long)page);
  37302. +
  37303. +out:
  37304. + return err;
  37305. +}
  37306. +
  37307. +int au_xib_trunc(struct super_block *sb)
  37308. +{
  37309. + int err;
  37310. + ssize_t sz;
  37311. + loff_t pos;
  37312. + struct au_sbinfo *sbinfo;
  37313. + unsigned long *p;
  37314. + struct file *file;
  37315. +
  37316. + SiMustWriteLock(sb);
  37317. +
  37318. + err = 0;
  37319. + sbinfo = au_sbi(sb);
  37320. + if (!au_opt_test(sbinfo->si_mntflags, XINO))
  37321. + goto out;
  37322. +
  37323. + file = sbinfo->si_xib;
  37324. + if (vfsub_f_size_read(file) <= PAGE_SIZE)
  37325. + goto out;
  37326. +
  37327. + file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
  37328. + err = PTR_ERR(file);
  37329. + if (IS_ERR(file))
  37330. + goto out;
  37331. + fput(sbinfo->si_xib);
  37332. + sbinfo->si_xib = file;
  37333. +
  37334. + p = sbinfo->si_xib_buf;
  37335. + memset(p, 0, PAGE_SIZE);
  37336. + pos = 0;
  37337. + sz = xino_fwrite(sbinfo->si_xib, p, PAGE_SIZE, &pos);
  37338. + if (unlikely(sz != PAGE_SIZE)) {
  37339. + err = sz;
  37340. + AuIOErr("err %d\n", err);
  37341. + if (sz >= 0)
  37342. + err = -EIO;
  37343. + goto out;
  37344. + }
  37345. +
  37346. + mutex_lock(&sbinfo->si_xib_mtx);
  37347. + /* mnt_want_write() is unnecessary here */
  37348. + err = xib_restore(sb);
  37349. + mutex_unlock(&sbinfo->si_xib_mtx);
  37350. +
  37351. +out:
  37352. + return err;
  37353. +}
  37354. +
  37355. +/* ---------------------------------------------------------------------- */
  37356. +
  37357. +struct au_xino *au_xino_alloc(unsigned int nfile)
  37358. +{
  37359. + struct au_xino *xi;
  37360. +
  37361. + xi = kzalloc(sizeof(*xi), GFP_NOFS);
  37362. + if (unlikely(!xi))
  37363. + goto out;
  37364. + xi->xi_nfile = nfile;
  37365. + xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
  37366. + if (unlikely(!xi->xi_file))
  37367. + goto out_free;
  37368. +
  37369. + xi->xi_nondir.total = 8; /* initial size */
  37370. + xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
  37371. + GFP_NOFS);
  37372. + if (unlikely(!xi->xi_nondir.array))
  37373. + goto out_file;
  37374. +
  37375. + spin_lock_init(&xi->xi_nondir.spin);
  37376. + init_waitqueue_head(&xi->xi_nondir.wqh);
  37377. + mutex_init(&xi->xi_mtx);
  37378. + INIT_HLIST_BL_HEAD(&xi->xi_writing);
  37379. + atomic_set(&xi->xi_truncating, 0);
  37380. + kref_init(&xi->xi_kref);
  37381. + goto out; /* success */
  37382. +
  37383. +out_file:
  37384. + au_kfree_try_rcu(xi->xi_file);
  37385. +out_free:
  37386. + au_kfree_rcu(xi);
  37387. + xi = NULL;
  37388. +out:
  37389. + return xi;
  37390. +}
  37391. +
  37392. +static int au_xino_init(struct au_branch *br, int idx, struct file *file)
  37393. +{
  37394. + int err;
  37395. + struct au_xino *xi;
  37396. +
  37397. + err = 0;
  37398. + xi = au_xino_alloc(idx + 1);
  37399. + if (unlikely(!xi)) {
  37400. + err = -ENOMEM;
  37401. + goto out;
  37402. + }
  37403. +
  37404. + if (file)
  37405. + get_file(file);
  37406. + xi->xi_file[idx] = file;
  37407. + AuDebugOn(br->br_xino);
  37408. + br->br_xino = xi;
  37409. +
  37410. +out:
  37411. + return err;
  37412. +}
  37413. +
  37414. +static void au_xino_release(struct kref *kref)
  37415. +{
  37416. + struct au_xino *xi;
  37417. + int i;
  37418. + unsigned long ul;
  37419. + struct hlist_bl_head *hbl;
  37420. + struct hlist_bl_node *pos, *n;
  37421. + struct au_xi_writing *p;
  37422. +
  37423. + xi = container_of(kref, struct au_xino, xi_kref);
  37424. + for (i = 0; i < xi->xi_nfile; i++)
  37425. + if (xi->xi_file[i])
  37426. + fput(xi->xi_file[i]);
  37427. + for (i = xi->xi_nondir.total - 1; i >= 0; i--)
  37428. + AuDebugOn(xi->xi_nondir.array[i]);
  37429. + mutex_destroy(&xi->xi_mtx);
  37430. + hbl = &xi->xi_writing;
  37431. + ul = au_hbl_count(hbl);
  37432. + if (unlikely(ul)) {
  37433. + pr_warn("xi_writing %lu\n", ul);
  37434. + hlist_bl_lock(hbl);
  37435. + hlist_bl_for_each_entry_safe(p, pos, n, hbl, node) {
  37436. + hlist_bl_del(&p->node);
  37437. + /* kmemleak reported au_kfree_rcu() doesn't free it */
  37438. + kfree(p);
  37439. + }
  37440. + hlist_bl_unlock(hbl);
  37441. + }
  37442. + au_kfree_try_rcu(xi->xi_file);
  37443. + au_kfree_try_rcu(xi->xi_nondir.array);
  37444. + au_kfree_rcu(xi);
  37445. +}
  37446. +
  37447. +int au_xino_put(struct au_branch *br)
  37448. +{
  37449. + int ret;
  37450. + struct au_xino *xi;
  37451. +
  37452. + ret = 0;
  37453. + xi = br->br_xino;
  37454. + if (xi) {
  37455. + br->br_xino = NULL;
  37456. + ret = kref_put(&xi->xi_kref, au_xino_release);
  37457. + }
  37458. +
  37459. + return ret;
  37460. +}
  37461. +
  37462. +/* ---------------------------------------------------------------------- */
  37463. +
  37464. +/*
  37465. + * xino mount option handlers
  37466. + */
  37467. +
  37468. +/* xino bitmap */
  37469. +static void xino_clear_xib(struct super_block *sb)
  37470. +{
  37471. + struct au_sbinfo *sbinfo;
  37472. +
  37473. + SiMustWriteLock(sb);
  37474. +
  37475. + sbinfo = au_sbi(sb);
  37476. + if (sbinfo->si_xib)
  37477. + fput(sbinfo->si_xib);
  37478. + sbinfo->si_xib = NULL;
  37479. + if (sbinfo->si_xib_buf)
  37480. + free_page((unsigned long)sbinfo->si_xib_buf);
  37481. + sbinfo->si_xib_buf = NULL;
  37482. +}
  37483. +
  37484. +static int au_xino_set_xib(struct super_block *sb, struct path *path)
  37485. +{
  37486. + int err;
  37487. + loff_t pos;
  37488. + struct au_sbinfo *sbinfo;
  37489. + struct file *file;
  37490. + struct super_block *xi_sb;
  37491. +
  37492. + SiMustWriteLock(sb);
  37493. +
  37494. + sbinfo = au_sbi(sb);
  37495. + file = au_xino_create2(sb, path, sbinfo->si_xib);
  37496. + err = PTR_ERR(file);
  37497. + if (IS_ERR(file))
  37498. + goto out;
  37499. + if (sbinfo->si_xib)
  37500. + fput(sbinfo->si_xib);
  37501. + sbinfo->si_xib = file;
  37502. + xi_sb = file_inode(file)->i_sb;
  37503. + sbinfo->si_ximaxent = xi_sb->s_maxbytes;
  37504. + if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
  37505. + err = -EIO;
  37506. + pr_err("s_maxbytes(%llu) on %s is too small\n",
  37507. + (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
  37508. + goto out_unset;
  37509. + }
  37510. + sbinfo->si_ximaxent /= sizeof(ino_t);
  37511. +
  37512. + err = -ENOMEM;
  37513. + if (!sbinfo->si_xib_buf)
  37514. + sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
  37515. + if (unlikely(!sbinfo->si_xib_buf))
  37516. + goto out_unset;
  37517. +
  37518. + sbinfo->si_xib_last_pindex = 0;
  37519. + sbinfo->si_xib_next_bit = 0;
  37520. + if (vfsub_f_size_read(file) < PAGE_SIZE) {
  37521. + pos = 0;
  37522. + err = xino_fwrite(file, sbinfo->si_xib_buf, PAGE_SIZE, &pos);
  37523. + if (unlikely(err != PAGE_SIZE))
  37524. + goto out_free;
  37525. + }
  37526. + err = 0;
  37527. + goto out; /* success */
  37528. +
  37529. +out_free:
  37530. + if (sbinfo->si_xib_buf)
  37531. + free_page((unsigned long)sbinfo->si_xib_buf);
  37532. + sbinfo->si_xib_buf = NULL;
  37533. + if (err >= 0)
  37534. + err = -EIO;
  37535. +out_unset:
  37536. + fput(sbinfo->si_xib);
  37537. + sbinfo->si_xib = NULL;
  37538. +out:
  37539. + AuTraceErr(err);
  37540. + return err;
  37541. +}
  37542. +
  37543. +/* xino for each branch */
  37544. +static void xino_clear_br(struct super_block *sb)
  37545. +{
  37546. + aufs_bindex_t bindex, bbot;
  37547. + struct au_branch *br;
  37548. +
  37549. + bbot = au_sbbot(sb);
  37550. + for (bindex = 0; bindex <= bbot; bindex++) {
  37551. + br = au_sbr(sb, bindex);
  37552. + AuDebugOn(!br);
  37553. + au_xino_put(br);
  37554. + }
  37555. +}
  37556. +
  37557. +static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
  37558. + aufs_bindex_t bshared)
  37559. +{
  37560. + struct au_branch *brshared;
  37561. +
  37562. + brshared = au_sbr(sb, bshared);
  37563. + AuDebugOn(!brshared->br_xino);
  37564. + AuDebugOn(!brshared->br_xino->xi_file);
  37565. + if (br->br_xino != brshared->br_xino) {
  37566. + au_xino_get(brshared);
  37567. + au_xino_put(br);
  37568. + br->br_xino = brshared->br_xino;
  37569. + }
  37570. +}
  37571. +
  37572. +struct au_xino_do_set_br {
  37573. + struct au_branch *br;
  37574. + ino_t h_ino;
  37575. + aufs_bindex_t bshared;
  37576. +};
  37577. +
  37578. +static int au_xino_do_set_br(struct super_block *sb, struct path *path,
  37579. + struct au_xino_do_set_br *args)
  37580. +{
  37581. + int err;
  37582. + struct au_xi_calc calc;
  37583. + struct file *file;
  37584. + struct au_branch *br;
  37585. + struct au_xi_new xinew = {
  37586. + .base = path
  37587. + };
  37588. +
  37589. + br = args->br;
  37590. + xinew.xi = br->br_xino;
  37591. + au_xi_calc(sb, args->h_ino, &calc);
  37592. + xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
  37593. + if (args->bshared >= 0)
  37594. + /* shared xino */
  37595. + au_xino_set_br_shared(sb, br, args->bshared);
  37596. + else if (!xinew.xi) {
  37597. + /* new xino */
  37598. + err = au_xino_init(br, calc.idx, xinew.copy_src);
  37599. + if (unlikely(err))
  37600. + goto out;
  37601. + }
  37602. +
  37603. + /* force re-creating */
  37604. + xinew.xi = br->br_xino;
  37605. + xinew.idx = calc.idx;
  37606. + mutex_lock(&xinew.xi->xi_mtx);
  37607. + file = au_xi_new(sb, &xinew);
  37608. + mutex_unlock(&xinew.xi->xi_mtx);
  37609. + err = PTR_ERR(file);
  37610. + if (IS_ERR(file))
  37611. + goto out;
  37612. + AuDebugOn(!file);
  37613. +
  37614. + err = au_xino_do_write(file, &calc, AUFS_ROOT_INO);
  37615. + if (unlikely(err))
  37616. + au_xino_put(br);
  37617. +
  37618. +out:
  37619. + AuTraceErr(err);
  37620. + return err;
  37621. +}
  37622. +
  37623. +static int au_xino_set_br(struct super_block *sb, struct path *path)
  37624. +{
  37625. + int err;
  37626. + aufs_bindex_t bindex, bbot;
  37627. + struct au_xino_do_set_br args;
  37628. + struct inode *inode;
  37629. +
  37630. + SiMustWriteLock(sb);
  37631. +
  37632. + bbot = au_sbbot(sb);
  37633. + inode = d_inode(sb->s_root);
  37634. + for (bindex = 0; bindex <= bbot; bindex++) {
  37635. + args.h_ino = au_h_iptr(inode, bindex)->i_ino;
  37636. + args.br = au_sbr(sb, bindex);
  37637. + args.bshared = is_sb_shared(sb, bindex, bindex - 1);
  37638. + err = au_xino_do_set_br(sb, path, &args);
  37639. + if (unlikely(err))
  37640. + break;
  37641. + }
  37642. +
  37643. + AuTraceErr(err);
  37644. + return err;
  37645. +}
  37646. +
  37647. +void au_xino_clr(struct super_block *sb)
  37648. +{
  37649. + struct au_sbinfo *sbinfo;
  37650. +
  37651. + au_xigen_clr(sb);
  37652. + xino_clear_xib(sb);
  37653. + xino_clear_br(sb);
  37654. + dbgaufs_brs_del(sb, 0);
  37655. + sbinfo = au_sbi(sb);
  37656. + /* lvalue, do not call au_mntflags() */
  37657. + au_opt_clr(sbinfo->si_mntflags, XINO);
  37658. +}
  37659. +
  37660. +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
  37661. +{
  37662. + int err, skip;
  37663. + struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
  37664. + struct qstr *dname, *cur_name;
  37665. + struct file *cur_xino;
  37666. + struct au_sbinfo *sbinfo;
  37667. + struct path *path, *cur_path;
  37668. +
  37669. + SiMustWriteLock(sb);
  37670. +
  37671. + err = 0;
  37672. + sbinfo = au_sbi(sb);
  37673. + path = &xiopt->file->f_path;
  37674. + dentry = path->dentry;
  37675. + parent = dget_parent(dentry);
  37676. + if (remount) {
  37677. + skip = 0;
  37678. + cur_xino = sbinfo->si_xib;
  37679. + if (cur_xino) {
  37680. + cur_path = &cur_xino->f_path;
  37681. + cur_dentry = cur_path->dentry;
  37682. + cur_parent = dget_parent(cur_dentry);
  37683. + cur_name = &cur_dentry->d_name;
  37684. + dname = &dentry->d_name;
  37685. + skip = (cur_parent == parent
  37686. + && au_qstreq(dname, cur_name));
  37687. + dput(cur_parent);
  37688. + }
  37689. + if (skip)
  37690. + goto out;
  37691. + }
  37692. +
  37693. + au_opt_set(sbinfo->si_mntflags, XINO);
  37694. + err = au_xino_set_xib(sb, path);
  37695. + /* si_x{read,write} are set */
  37696. + if (!err)
  37697. + err = au_xigen_set(sb, path);
  37698. + if (!err)
  37699. + err = au_xino_set_br(sb, path);
  37700. + if (!err) {
  37701. + dbgaufs_brs_add(sb, 0, /*topdown*/1);
  37702. + goto out; /* success */
  37703. + }
  37704. +
  37705. + /* reset all */
  37706. + AuIOErr("failed setting xino(%d).\n", err);
  37707. + au_xino_clr(sb);
  37708. +
  37709. +out:
  37710. + dput(parent);
  37711. + return err;
  37712. +}
  37713. +
  37714. +/*
  37715. + * create a xinofile at the default place/path.
  37716. + */
  37717. +struct file *au_xino_def(struct super_block *sb)
  37718. +{
  37719. + struct file *file;
  37720. + char *page, *p;
  37721. + struct au_branch *br;
  37722. + struct super_block *h_sb;
  37723. + struct path path;
  37724. + aufs_bindex_t bbot, bindex, bwr;
  37725. +
  37726. + br = NULL;
  37727. + bbot = au_sbbot(sb);
  37728. + bwr = -1;
  37729. + for (bindex = 0; bindex <= bbot; bindex++) {
  37730. + br = au_sbr(sb, bindex);
  37731. + if (au_br_writable(br->br_perm)
  37732. + && !au_test_fs_bad_xino(au_br_sb(br))) {
  37733. + bwr = bindex;
  37734. + break;
  37735. + }
  37736. + }
  37737. +
  37738. + if (bwr >= 0) {
  37739. + file = ERR_PTR(-ENOMEM);
  37740. + page = (void *)__get_free_page(GFP_NOFS);
  37741. + if (unlikely(!page))
  37742. + goto out;
  37743. + path.mnt = au_br_mnt(br);
  37744. + path.dentry = au_h_dptr(sb->s_root, bwr);
  37745. + p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
  37746. + file = (void *)p;
  37747. + if (!IS_ERR(p)) {
  37748. + strcat(p, "/" AUFS_XINO_FNAME);
  37749. + AuDbg("%s\n", p);
  37750. + file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
  37751. + }
  37752. + free_page((unsigned long)page);
  37753. + } else {
  37754. + file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
  37755. + /*wbrtop*/0);
  37756. + if (IS_ERR(file))
  37757. + goto out;
  37758. + h_sb = file->f_path.dentry->d_sb;
  37759. + if (unlikely(au_test_fs_bad_xino(h_sb))) {
  37760. + pr_err("xino doesn't support %s(%s)\n",
  37761. + AUFS_XINO_DEFPATH, au_sbtype(h_sb));
  37762. + fput(file);
  37763. + file = ERR_PTR(-EINVAL);
  37764. + }
  37765. + }
  37766. +
  37767. +out:
  37768. + return file;
  37769. +}
  37770. +
  37771. +/* ---------------------------------------------------------------------- */
  37772. +
  37773. +/*
  37774. + * initialize the xinofile for the specified branch @br
  37775. + * at the place/path where @base_file indicates.
  37776. + * test whether another branch is on the same filesystem or not,
  37777. + * if found then share the xinofile with another branch.
  37778. + */
  37779. +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
  37780. + struct path *base)
  37781. +{
  37782. + int err;
  37783. + struct au_xino_do_set_br args = {
  37784. + .h_ino = h_ino,
  37785. + .br = br
  37786. + };
  37787. +
  37788. + args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
  37789. + au_br_sb(br));
  37790. + err = au_xino_do_set_br(sb, base, &args);
  37791. + if (unlikely(err))
  37792. + au_xino_put(br);
  37793. +
  37794. + return err;
  37795. +}
  37796. +
  37797. +/* ---------------------------------------------------------------------- */
  37798. +
  37799. +/*
  37800. + * get an unused inode number from bitmap
  37801. + */
  37802. +ino_t au_xino_new_ino(struct super_block *sb)
  37803. +{
  37804. + ino_t ino;
  37805. + unsigned long *p, pindex, ul, pend;
  37806. + struct au_sbinfo *sbinfo;
  37807. + struct file *file;
  37808. + int free_bit, err;
  37809. +
  37810. + if (!au_opt_test(au_mntflags(sb), XINO))
  37811. + return iunique(sb, AUFS_FIRST_INO);
  37812. +
  37813. + sbinfo = au_sbi(sb);
  37814. + mutex_lock(&sbinfo->si_xib_mtx);
  37815. + p = sbinfo->si_xib_buf;
  37816. + free_bit = sbinfo->si_xib_next_bit;
  37817. + if (free_bit < page_bits && !test_bit(free_bit, p))
  37818. + goto out; /* success */
  37819. + free_bit = find_first_zero_bit(p, page_bits);
  37820. + if (free_bit < page_bits)
  37821. + goto out; /* success */
  37822. +
  37823. + pindex = sbinfo->si_xib_last_pindex;
  37824. + for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
  37825. + err = xib_pindex(sb, ul);
  37826. + if (unlikely(err))
  37827. + goto out_err;
  37828. + free_bit = find_first_zero_bit(p, page_bits);
  37829. + if (free_bit < page_bits)
  37830. + goto out; /* success */
  37831. + }
  37832. +
  37833. + file = sbinfo->si_xib;
  37834. + pend = vfsub_f_size_read(file) / PAGE_SIZE;
  37835. + for (ul = pindex + 1; ul <= pend; ul++) {
  37836. + err = xib_pindex(sb, ul);
  37837. + if (unlikely(err))
  37838. + goto out_err;
  37839. + free_bit = find_first_zero_bit(p, page_bits);
  37840. + if (free_bit < page_bits)
  37841. + goto out; /* success */
  37842. + }
  37843. + BUG();
  37844. +
  37845. +out:
  37846. + set_bit(free_bit, p);
  37847. + sbinfo->si_xib_next_bit = free_bit + 1;
  37848. + pindex = sbinfo->si_xib_last_pindex;
  37849. + mutex_unlock(&sbinfo->si_xib_mtx);
  37850. + ino = xib_calc_ino(pindex, free_bit);
  37851. + AuDbg("i%lu\n", (unsigned long)ino);
  37852. + return ino;
  37853. +out_err:
  37854. + mutex_unlock(&sbinfo->si_xib_mtx);
  37855. + AuDbg("i0\n");
  37856. + return 0;
  37857. +}
  37858. +
  37859. +/* for s_op->delete_inode() */
  37860. +void au_xino_delete_inode(struct inode *inode, const int unlinked)
  37861. +{
  37862. + int err;
  37863. + unsigned int mnt_flags;
  37864. + aufs_bindex_t bindex, bbot, bi;
  37865. + unsigned char try_trunc;
  37866. + struct au_iinfo *iinfo;
  37867. + struct super_block *sb;
  37868. + struct au_hinode *hi;
  37869. + struct inode *h_inode;
  37870. + struct au_branch *br;
  37871. + struct au_xi_calc calc;
  37872. + struct file *file;
  37873. +
  37874. + AuDebugOn(au_is_bad_inode(inode));
  37875. +
  37876. + sb = inode->i_sb;
  37877. + mnt_flags = au_mntflags(sb);
  37878. + if (!au_opt_test(mnt_flags, XINO)
  37879. + || inode->i_ino == AUFS_ROOT_INO)
  37880. + return;
  37881. +
  37882. + if (unlinked) {
  37883. + au_xigen_inc(inode);
  37884. + au_xib_clear_bit(inode);
  37885. + }
  37886. +
  37887. + iinfo = au_ii(inode);
  37888. + bindex = iinfo->ii_btop;
  37889. + if (bindex < 0)
  37890. + return;
  37891. +
  37892. + try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
  37893. + hi = au_hinode(iinfo, bindex);
  37894. + bbot = iinfo->ii_bbot;
  37895. + for (; bindex <= bbot; bindex++, hi++) {
  37896. + h_inode = hi->hi_inode;
  37897. + if (!h_inode
  37898. + || (!unlinked && h_inode->i_nlink))
  37899. + continue;
  37900. +
  37901. + /* inode may not be revalidated */
  37902. + bi = au_br_index(sb, hi->hi_id);
  37903. + if (bi < 0)
  37904. + continue;
  37905. +
  37906. + br = au_sbr(sb, bi);
  37907. + au_xi_calc(sb, h_inode->i_ino, &calc);
  37908. + file = au_xino_file(br->br_xino, calc.idx);
  37909. + if (IS_ERR_OR_NULL(file))
  37910. + continue;
  37911. +
  37912. + err = au_xino_do_write(file, &calc, /*ino*/0);
  37913. + if (!err && try_trunc
  37914. + && au_test_fs_trunc_xino(au_br_sb(br)))
  37915. + xino_try_trunc(sb, br);
  37916. + }
  37917. +}
  37918. +
  37919. +/* ---------------------------------------------------------------------- */
  37920. +
  37921. +static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
  37922. +{
  37923. + int found, total, i;
  37924. +
  37925. + found = -1;
  37926. + total = xi->xi_nondir.total;
  37927. + for (i = 0; i < total; i++) {
  37928. + if (xi->xi_nondir.array[i] != h_ino)
  37929. + continue;
  37930. + found = i;
  37931. + break;
  37932. + }
  37933. +
  37934. + return found;
  37935. +}
  37936. +
  37937. +static int au_xinondir_expand(struct au_xino *xi)
  37938. +{
  37939. + int err, sz;
  37940. + ino_t *p;
  37941. +
  37942. + BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
  37943. +
  37944. + err = -ENOMEM;
  37945. + sz = xi->xi_nondir.total * sizeof(ino_t);
  37946. + if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
  37947. + goto out;
  37948. + p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
  37949. + /*may_shrink*/0);
  37950. + if (p) {
  37951. + xi->xi_nondir.array = p;
  37952. + xi->xi_nondir.total <<= 1;
  37953. + AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
  37954. + err = 0;
  37955. + }
  37956. +
  37957. +out:
  37958. + return err;
  37959. +}
  37960. +
  37961. +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
  37962. + ino_t h_ino, int idx)
  37963. +{
  37964. + struct au_xino *xi;
  37965. +
  37966. + AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
  37967. + xi = au_sbr(sb, bindex)->br_xino;
  37968. + AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
  37969. +
  37970. + spin_lock(&xi->xi_nondir.spin);
  37971. + AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
  37972. + xi->xi_nondir.array[idx] = 0;
  37973. + spin_unlock(&xi->xi_nondir.spin);
  37974. + wake_up_all(&xi->xi_nondir.wqh);
  37975. +}
  37976. +
  37977. +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  37978. + int *idx)
  37979. +{
  37980. + int err, found, empty;
  37981. + struct au_xino *xi;
  37982. +
  37983. + err = 0;
  37984. + *idx = -1;
  37985. + if (!au_opt_test(au_mntflags(sb), XINO))
  37986. + goto out; /* no xino */
  37987. +
  37988. + xi = au_sbr(sb, bindex)->br_xino;
  37989. +
  37990. +again:
  37991. + spin_lock(&xi->xi_nondir.spin);
  37992. + found = au_xinondir_find(xi, h_ino);
  37993. + if (found == -1) {
  37994. + empty = au_xinondir_find(xi, /*h_ino*/0);
  37995. + if (empty == -1) {
  37996. + empty = xi->xi_nondir.total;
  37997. + err = au_xinondir_expand(xi);
  37998. + if (unlikely(err))
  37999. + goto out_unlock;
  38000. + }
  38001. + xi->xi_nondir.array[empty] = h_ino;
  38002. + *idx = empty;
  38003. + } else {
  38004. + spin_unlock(&xi->xi_nondir.spin);
  38005. + wait_event(xi->xi_nondir.wqh,
  38006. + xi->xi_nondir.array[found] != h_ino);
  38007. + goto again;
  38008. + }
  38009. +
  38010. +out_unlock:
  38011. + spin_unlock(&xi->xi_nondir.spin);
  38012. +out:
  38013. + return err;
  38014. +}
  38015. +
  38016. +/* ---------------------------------------------------------------------- */
  38017. +
  38018. +int au_xino_path(struct seq_file *seq, struct file *file)
  38019. +{
  38020. + int err;
  38021. +
  38022. + err = au_seq_path(seq, &file->f_path);
  38023. + if (unlikely(err))
  38024. + goto out;
  38025. +
  38026. +#define Deleted "\\040(deleted)"
  38027. + seq->count -= sizeof(Deleted) - 1;
  38028. + AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
  38029. + sizeof(Deleted) - 1));
  38030. +#undef Deleted
  38031. +
  38032. +out:
  38033. + return err;
  38034. +}
  38035. diff -Naur null/include/uapi/linux/aufs_type.h linux-5.15.131/include/uapi/linux/aufs_type.h
  38036. --- /dev/null
  38037. +++ linux-5.15.131/include/uapi/linux/aufs_type.h 2023-09-11 14:32:48.154722477 +0300
  38038. @@ -0,0 +1,452 @@
  38039. +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
  38040. +/*
  38041. + * Copyright (C) 2005-2021 Junjiro R. Okajima
  38042. + *
  38043. + * This program, aufs is free software; you can redistribute it and/or modify
  38044. + * it under the terms of the GNU General Public License as published by
  38045. + * the Free Software Foundation; either version 2 of the License, or
  38046. + * (at your option) any later version.
  38047. + *
  38048. + * This program is distributed in the hope that it will be useful,
  38049. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  38050. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  38051. + * GNU General Public License for more details.
  38052. + *
  38053. + * You should have received a copy of the GNU General Public License
  38054. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  38055. + */
  38056. +
  38057. +#ifndef __AUFS_TYPE_H__
  38058. +#define __AUFS_TYPE_H__
  38059. +
  38060. +#define AUFS_NAME "aufs"
  38061. +
  38062. +#ifdef __KERNEL__
  38063. +/*
  38064. + * define it before including all other headers.
  38065. + * sched.h may use pr_* macros before defining "current", so define the
  38066. + * no-current version first, and re-define later.
  38067. + */
  38068. +#define pr_fmt(fmt) AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
  38069. +#include <linux/sched.h>
  38070. +#undef pr_fmt
  38071. +#define pr_fmt(fmt) \
  38072. + AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
  38073. + (int)sizeof(current->comm), current->comm, current->pid
  38074. +#include <linux/limits.h>
  38075. +#else
  38076. +#include <stdint.h>
  38077. +#include <sys/types.h>
  38078. +#include <limits.h>
  38079. +#endif /* __KERNEL__ */
  38080. +
  38081. +#define AUFS_VERSION "5.15.41-20230828"
  38082. +
  38083. +/* todo? move this to linux-2.6.19/include/magic.h */
  38084. +#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
  38085. +
  38086. +/* ---------------------------------------------------------------------- */
  38087. +
  38088. +#ifdef __KERNEL__
  38089. +#ifdef CONFIG_AUFS_BRANCH_MAX_127
  38090. +typedef int8_t aufs_bindex_t;
  38091. +#define AUFS_BRANCH_MAX 127
  38092. +#else
  38093. +typedef int16_t aufs_bindex_t;
  38094. +#ifdef CONFIG_AUFS_BRANCH_MAX_511
  38095. +#define AUFS_BRANCH_MAX 511
  38096. +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
  38097. +#define AUFS_BRANCH_MAX 1023
  38098. +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
  38099. +#define AUFS_BRANCH_MAX 32767
  38100. +#endif
  38101. +#endif
  38102. +
  38103. +#ifndef AUFS_BRANCH_MAX
  38104. +#error unknown CONFIG_AUFS_BRANCH_MAX value
  38105. +#endif
  38106. +#endif /* __KERNEL__ */
  38107. +
  38108. +/* ---------------------------------------------------------------------- */
  38109. +
  38110. +#define AUFS_FSTYPE AUFS_NAME
  38111. +
  38112. +#define AUFS_ROOT_INO 2
  38113. +#define AUFS_FIRST_INO 11
  38114. +
  38115. +#define AUFS_WH_PFX ".wh."
  38116. +#define AUFS_WH_PFX_LEN ((int)sizeof(AUFS_WH_PFX) - 1)
  38117. +#define AUFS_WH_TMP_LEN 4
  38118. +/* a limit for rmdir/rename a dir and copyup */
  38119. +#define AUFS_MAX_NAMELEN (NAME_MAX \
  38120. + - AUFS_WH_PFX_LEN * 2 /* doubly whiteouted */\
  38121. + - 1 /* dot */\
  38122. + - AUFS_WH_TMP_LEN) /* hex */
  38123. +#define AUFS_XINO_FNAME "." AUFS_NAME ".xino"
  38124. +#define AUFS_XINO_DEFPATH "/tmp/" AUFS_XINO_FNAME
  38125. +#define AUFS_XINO_DEF_SEC 30 /* seconds */
  38126. +#define AUFS_XINO_DEF_TRUNC 45 /* percentage */
  38127. +#define AUFS_DIRWH_DEF 3
  38128. +#define AUFS_RDCACHE_DEF 10 /* seconds */
  38129. +#define AUFS_RDCACHE_MAX 3600 /* seconds */
  38130. +#define AUFS_RDBLK_DEF 512 /* bytes */
  38131. +#define AUFS_RDHASH_DEF 32
  38132. +#define AUFS_WKQ_NAME AUFS_NAME "d"
  38133. +#define AUFS_MFS_DEF_SEC 30 /* seconds */
  38134. +#define AUFS_MFS_MAX_SEC 3600 /* seconds */
  38135. +#define AUFS_FHSM_CACHE_DEF_SEC 30 /* seconds */
  38136. +#define AUFS_PLINK_WARN 50 /* number of plinks in a single bucket */
  38137. +
  38138. +/* pseudo-link maintenace under /proc */
  38139. +#define AUFS_PLINK_MAINT_NAME "plink_maint"
  38140. +#define AUFS_PLINK_MAINT_DIR "fs/" AUFS_NAME
  38141. +#define AUFS_PLINK_MAINT_PATH AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
  38142. +
  38143. +/* dirren, renamed dir */
  38144. +#define AUFS_DR_INFO_PFX AUFS_WH_PFX ".dr."
  38145. +#define AUFS_DR_BRHINO_NAME AUFS_WH_PFX "hino"
  38146. +/* whiteouted doubly */
  38147. +#define AUFS_WH_DR_INFO_PFX AUFS_WH_PFX AUFS_DR_INFO_PFX
  38148. +#define AUFS_WH_DR_BRHINO AUFS_WH_PFX AUFS_DR_BRHINO_NAME
  38149. +
  38150. +#define AUFS_DIROPQ_NAME AUFS_WH_PFX ".opq" /* whiteouted doubly */
  38151. +#define AUFS_WH_DIROPQ AUFS_WH_PFX AUFS_DIROPQ_NAME
  38152. +
  38153. +#define AUFS_BASE_NAME AUFS_WH_PFX AUFS_NAME
  38154. +#define AUFS_PLINKDIR_NAME AUFS_WH_PFX "plnk"
  38155. +#define AUFS_ORPHDIR_NAME AUFS_WH_PFX "orph"
  38156. +
  38157. +/* doubly whiteouted */
  38158. +#define AUFS_WH_BASE AUFS_WH_PFX AUFS_BASE_NAME
  38159. +#define AUFS_WH_PLINKDIR AUFS_WH_PFX AUFS_PLINKDIR_NAME
  38160. +#define AUFS_WH_ORPHDIR AUFS_WH_PFX AUFS_ORPHDIR_NAME
  38161. +
  38162. +/* branch permissions and attributes */
  38163. +#define AUFS_BRPERM_RW "rw"
  38164. +#define AUFS_BRPERM_RO "ro"
  38165. +#define AUFS_BRPERM_RR "rr"
  38166. +#define AUFS_BRATTR_COO_REG "coo_reg"
  38167. +#define AUFS_BRATTR_COO_ALL "coo_all"
  38168. +#define AUFS_BRATTR_FHSM "fhsm"
  38169. +#define AUFS_BRATTR_UNPIN "unpin"
  38170. +#define AUFS_BRATTR_ICEX "icex"
  38171. +#define AUFS_BRATTR_ICEX_SEC "icexsec"
  38172. +#define AUFS_BRATTR_ICEX_SYS "icexsys"
  38173. +#define AUFS_BRATTR_ICEX_TR "icextr"
  38174. +#define AUFS_BRATTR_ICEX_USR "icexusr"
  38175. +#define AUFS_BRATTR_ICEX_OTH "icexoth"
  38176. +#define AUFS_BRRATTR_WH "wh"
  38177. +#define AUFS_BRWATTR_NLWH "nolwh"
  38178. +#define AUFS_BRWATTR_MOO "moo"
  38179. +
  38180. +#define AuBrPerm_RW 1 /* writable, hardlinkable wh */
  38181. +#define AuBrPerm_RO (1 << 1) /* readonly */
  38182. +#define AuBrPerm_RR (1 << 2) /* natively readonly */
  38183. +#define AuBrPerm_Mask (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
  38184. +
  38185. +#define AuBrAttr_COO_REG (1 << 3) /* copy-up on open */
  38186. +#define AuBrAttr_COO_ALL (1 << 4)
  38187. +#define AuBrAttr_COO_Mask (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
  38188. +
  38189. +#define AuBrAttr_FHSM (1 << 5) /* file-based hsm */
  38190. +#define AuBrAttr_UNPIN (1 << 6) /* rename-able top dir of
  38191. + branch. meaningless since
  38192. + linux-3.18-rc1 */
  38193. +
  38194. +/* ignore error in copying XATTR */
  38195. +#define AuBrAttr_ICEX_SEC (1 << 7)
  38196. +#define AuBrAttr_ICEX_SYS (1 << 8)
  38197. +#define AuBrAttr_ICEX_TR (1 << 9)
  38198. +#define AuBrAttr_ICEX_USR (1 << 10)
  38199. +#define AuBrAttr_ICEX_OTH (1 << 11)
  38200. +#define AuBrAttr_ICEX (AuBrAttr_ICEX_SEC \
  38201. + | AuBrAttr_ICEX_SYS \
  38202. + | AuBrAttr_ICEX_TR \
  38203. + | AuBrAttr_ICEX_USR \
  38204. + | AuBrAttr_ICEX_OTH)
  38205. +
  38206. +#define AuBrRAttr_WH (1 << 12) /* whiteout-able */
  38207. +#define AuBrRAttr_Mask AuBrRAttr_WH
  38208. +
  38209. +#define AuBrWAttr_NoLinkWH (1 << 13) /* un-hardlinkable whiteouts */
  38210. +#define AuBrWAttr_MOO (1 << 14) /* move-up on open */
  38211. +#define AuBrWAttr_Mask (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
  38212. +
  38213. +#define AuBrAttr_CMOO_Mask (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
  38214. +
  38215. +/* #warning test userspace */
  38216. +#ifdef __KERNEL__
  38217. +#ifndef CONFIG_AUFS_FHSM
  38218. +#undef AuBrAttr_FHSM
  38219. +#define AuBrAttr_FHSM 0
  38220. +#endif
  38221. +#ifndef CONFIG_AUFS_XATTR
  38222. +#undef AuBrAttr_ICEX
  38223. +#define AuBrAttr_ICEX 0
  38224. +#undef AuBrAttr_ICEX_SEC
  38225. +#define AuBrAttr_ICEX_SEC 0
  38226. +#undef AuBrAttr_ICEX_SYS
  38227. +#define AuBrAttr_ICEX_SYS 0
  38228. +#undef AuBrAttr_ICEX_TR
  38229. +#define AuBrAttr_ICEX_TR 0
  38230. +#undef AuBrAttr_ICEX_USR
  38231. +#define AuBrAttr_ICEX_USR 0
  38232. +#undef AuBrAttr_ICEX_OTH
  38233. +#define AuBrAttr_ICEX_OTH 0
  38234. +#endif
  38235. +#endif
  38236. +
  38237. +/* the longest combination */
  38238. +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
  38239. +#define AuBrPermStrSz sizeof(AUFS_BRPERM_RW \
  38240. + "+" AUFS_BRATTR_COO_REG \
  38241. + "+" AUFS_BRATTR_FHSM \
  38242. + "+" AUFS_BRATTR_UNPIN \
  38243. + "+" AUFS_BRATTR_ICEX_SEC \
  38244. + "+" AUFS_BRATTR_ICEX_SYS \
  38245. + "+" AUFS_BRATTR_ICEX_USR \
  38246. + "+" AUFS_BRATTR_ICEX_OTH \
  38247. + "+" AUFS_BRWATTR_NLWH)
  38248. +
  38249. +typedef struct {
  38250. + char a[AuBrPermStrSz];
  38251. +} au_br_perm_str_t;
  38252. +
  38253. +static inline int au_br_writable(int brperm)
  38254. +{
  38255. + return brperm & AuBrPerm_RW;
  38256. +}
  38257. +
  38258. +static inline int au_br_whable(int brperm)
  38259. +{
  38260. + return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
  38261. +}
  38262. +
  38263. +static inline int au_br_wh_linkable(int brperm)
  38264. +{
  38265. + return !(brperm & AuBrWAttr_NoLinkWH);
  38266. +}
  38267. +
  38268. +static inline int au_br_cmoo(int brperm)
  38269. +{
  38270. + return brperm & AuBrAttr_CMOO_Mask;
  38271. +}
  38272. +
  38273. +static inline int au_br_fhsm(int brperm)
  38274. +{
  38275. + return brperm & AuBrAttr_FHSM;
  38276. +}
  38277. +
  38278. +/* ---------------------------------------------------------------------- */
  38279. +
  38280. +/* ioctl */
  38281. +enum {
  38282. + /* readdir in userspace */
  38283. + AuCtl_RDU,
  38284. + AuCtl_RDU_INO,
  38285. +
  38286. + AuCtl_WBR_FD, /* pathconf wrapper */
  38287. + AuCtl_IBUSY, /* busy inode */
  38288. + AuCtl_MVDOWN, /* move-down */
  38289. + AuCtl_BR, /* info about branches */
  38290. + AuCtl_FHSM_FD /* connection for fhsm */
  38291. +};
  38292. +
  38293. +/* borrowed from linux/include/linux/kernel.h */
  38294. +#ifndef ALIGN
  38295. +#ifdef _GNU_SOURCE
  38296. +#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
  38297. +#else
  38298. +#define ALIGN(x, a) (((x) + (a) - 1) & ~((a) - 1))
  38299. +#endif
  38300. +#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
  38301. +#endif
  38302. +
  38303. +/* borrowed from linux/include/linux/compiler-gcc3.h */
  38304. +#ifndef __aligned
  38305. +#define __aligned(x) __attribute__((aligned(x)))
  38306. +#endif
  38307. +
  38308. +#ifdef __KERNEL__
  38309. +#ifndef __packed
  38310. +#define __packed __attribute__((packed))
  38311. +#endif
  38312. +#endif
  38313. +
  38314. +struct au_rdu_cookie {
  38315. + uint64_t h_pos;
  38316. + int16_t bindex;
  38317. + uint8_t flags;
  38318. + uint8_t pad;
  38319. + uint32_t generation;
  38320. +} __aligned(8);
  38321. +
  38322. +struct au_rdu_ent {
  38323. + uint64_t ino;
  38324. + int16_t bindex;
  38325. + uint8_t type;
  38326. + uint8_t nlen;
  38327. + uint8_t wh;
  38328. + char name[];
  38329. +} __aligned(8);
  38330. +
  38331. +static inline int au_rdu_len(int nlen)
  38332. +{
  38333. + /* include the terminating NULL */
  38334. + return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
  38335. + sizeof(uint64_t));
  38336. +}
  38337. +
  38338. +union au_rdu_ent_ul {
  38339. + struct au_rdu_ent __user *e;
  38340. + uint64_t ul;
  38341. +};
  38342. +
  38343. +enum {
  38344. + AufsCtlRduV_SZ,
  38345. + AufsCtlRduV_End
  38346. +};
  38347. +
  38348. +struct aufs_rdu {
  38349. + /* input */
  38350. + union {
  38351. + uint64_t sz; /* AuCtl_RDU */
  38352. + uint64_t nent; /* AuCtl_RDU_INO */
  38353. + };
  38354. + union au_rdu_ent_ul ent;
  38355. + uint16_t verify[AufsCtlRduV_End];
  38356. +
  38357. + /* input/output */
  38358. + uint32_t blk;
  38359. +
  38360. + /* output */
  38361. + union au_rdu_ent_ul tail;
  38362. + /* number of entries which were added in a single call */
  38363. + uint64_t rent;
  38364. + uint8_t full;
  38365. + uint8_t shwh;
  38366. +
  38367. + struct au_rdu_cookie cookie;
  38368. +} __aligned(8);
  38369. +
  38370. +/* ---------------------------------------------------------------------- */
  38371. +
  38372. +/* dirren. the branch is identified by the filename who contains this */
  38373. +struct au_drinfo {
  38374. + uint64_t ino;
  38375. + union {
  38376. + uint8_t oldnamelen;
  38377. + uint64_t _padding;
  38378. + };
  38379. + uint8_t oldname[];
  38380. +} __aligned(8);
  38381. +
  38382. +struct au_drinfo_fdata {
  38383. + uint32_t magic;
  38384. + struct au_drinfo drinfo;
  38385. +} __aligned(8);
  38386. +
  38387. +#define AUFS_DRINFO_MAGIC_V1 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
  38388. +/* future */
  38389. +#define AUFS_DRINFO_MAGIC_V2 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
  38390. +
  38391. +/* ---------------------------------------------------------------------- */
  38392. +
  38393. +struct aufs_wbr_fd {
  38394. + uint32_t oflags;
  38395. + int16_t brid;
  38396. +} __aligned(8);
  38397. +
  38398. +/* ---------------------------------------------------------------------- */
  38399. +
  38400. +struct aufs_ibusy {
  38401. + uint64_t ino, h_ino;
  38402. + int16_t bindex;
  38403. +} __aligned(8);
  38404. +
  38405. +/* ---------------------------------------------------------------------- */
  38406. +
  38407. +/* error code for move-down */
  38408. +/* the actual message strings are implemented in aufs-util.git */
  38409. +enum {
  38410. + EAU_MVDOWN_OPAQUE = 1,
  38411. + EAU_MVDOWN_WHITEOUT,
  38412. + EAU_MVDOWN_UPPER,
  38413. + EAU_MVDOWN_BOTTOM,
  38414. + EAU_MVDOWN_NOUPPER,
  38415. + EAU_MVDOWN_NOLOWERBR,
  38416. + EAU_Last
  38417. +};
  38418. +
  38419. +/* flags for move-down */
  38420. +#define AUFS_MVDOWN_DMSG 1
  38421. +#define AUFS_MVDOWN_OWLOWER (1 << 1) /* overwrite lower */
  38422. +#define AUFS_MVDOWN_KUPPER (1 << 2) /* keep upper */
  38423. +#define AUFS_MVDOWN_ROLOWER (1 << 3) /* do even if lower is RO */
  38424. +#define AUFS_MVDOWN_ROLOWER_R (1 << 4) /* did on lower RO */
  38425. +#define AUFS_MVDOWN_ROUPPER (1 << 5) /* do even if upper is RO */
  38426. +#define AUFS_MVDOWN_ROUPPER_R (1 << 6) /* did on upper RO */
  38427. +#define AUFS_MVDOWN_BRID_UPPER (1 << 7) /* upper brid */
  38428. +#define AUFS_MVDOWN_BRID_LOWER (1 << 8) /* lower brid */
  38429. +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9) /* find fhsm attr for lower */
  38430. +#define AUFS_MVDOWN_STFS (1 << 10) /* req. stfs */
  38431. +#define AUFS_MVDOWN_STFS_FAILED (1 << 11) /* output: stfs is unusable */
  38432. +#define AUFS_MVDOWN_BOTTOM (1 << 12) /* output: no more lowers */
  38433. +
  38434. +/* index for move-down */
  38435. +enum {
  38436. + AUFS_MVDOWN_UPPER,
  38437. + AUFS_MVDOWN_LOWER,
  38438. + AUFS_MVDOWN_NARRAY
  38439. +};
  38440. +
  38441. +/*
  38442. + * additional info of move-down
  38443. + * number of free blocks and inodes.
  38444. + * subset of struct kstatfs, but smaller and always 64bit.
  38445. + */
  38446. +struct aufs_stfs {
  38447. + uint64_t f_blocks;
  38448. + uint64_t f_bavail;
  38449. + uint64_t f_files;
  38450. + uint64_t f_ffree;
  38451. +};
  38452. +
  38453. +struct aufs_stbr {
  38454. + int16_t brid; /* optional input */
  38455. + int16_t bindex; /* output */
  38456. + struct aufs_stfs stfs; /* output when AUFS_MVDOWN_STFS set */
  38457. +} __aligned(8);
  38458. +
  38459. +struct aufs_mvdown {
  38460. + uint32_t flags; /* input/output */
  38461. + struct aufs_stbr stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
  38462. + int8_t au_errno; /* output */
  38463. +} __aligned(8);
  38464. +
  38465. +/* ---------------------------------------------------------------------- */
  38466. +
  38467. +union aufs_brinfo {
  38468. + /* PATH_MAX may differ between kernel-space and user-space */
  38469. + char _spacer[4096];
  38470. + struct {
  38471. + int16_t id;
  38472. + int perm;
  38473. + char path[];
  38474. + };
  38475. +} __aligned(8);
  38476. +
  38477. +/* ---------------------------------------------------------------------- */
  38478. +
  38479. +#define AuCtlType 'A'
  38480. +#define AUFS_CTL_RDU _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
  38481. +#define AUFS_CTL_RDU_INO _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
  38482. +#define AUFS_CTL_WBR_FD _IOW(AuCtlType, AuCtl_WBR_FD, \
  38483. + struct aufs_wbr_fd)
  38484. +#define AUFS_CTL_IBUSY _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
  38485. +#define AUFS_CTL_MVDOWN _IOWR(AuCtlType, AuCtl_MVDOWN, \
  38486. + struct aufs_mvdown)
  38487. +#define AUFS_CTL_BRINFO _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
  38488. +#define AUFS_CTL_FHSM_FD _IOW(AuCtlType, AuCtl_FHSM_FD, int)
  38489. +
  38490. +#endif /* __AUFS_TYPE_H__ */