aufs4.patch 962 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819338203382133822338233382433825338263382733828338293383033831338323383333834338353383633837338383383933840338413384233843338443384533846338473384833849338503385133852338533385433855338563385733858338593386033861338623386333864338653386633867338683386933870338713387233873338743387533876338773387833879338803388133882338833388433885338863388733888338893389033891338923389333894338953389633897338983389933900339013390233903339043390533906339073390833909339103391133912339133391433915339163391733918339193392033921339223392333924339253392633927339283392933930339313393233933339343393533936339373393833939339403394133942339433394433945339463394733948339493395033951339523395333954339553395633957339583395933960339613396233963339643396533966339673396833969339703397133972339733397433975339763397733978339793398033981339823398333984339853398633987339883398933990339913399233993339943399533996339973399833999340003400134002340033400434005340063400734008340093401034011340123401334014340153401634017340183401934020340213402234023340243402534026340273402834029340303403134032340333403434035340363403734038340393404034041340423404334044340453404634047340483404934050340513405234053340543405534056340573405834059340603406134062340633406434065340663406734068340693407034071340723407334074340753407634077340783407934080340813408234083340843408534086340873408834089340903409134092340933409434095340963409734098340993410034101341023410334104341053410634107341083410934110341113411234113341143411534116341173411834119341203412134122341233412434125341263412734128341293413034131341323413334134341353413634137341383413934140341413414234143341443414534146341473414834149341503415134152341533415434155341563415734158341593416034161341623416334164341653416634167341683416934170341713417234173341743417534176341773417834179341803418134182341833418434185341863418734188341893419034191341923419334194341953419634197341983419934200342013420234203342043420534206342073420834209342103421134212342133421434215342163421734218342193422034221342223422334224342253422634227342283422934230342313423234233342343423534236342373423834239342403424134242342433424434245342463424734248342493425034251342523425334254342553425634257342583425934260342613426234263342643426534266342673426834269342703427134272342733427434275342763427734278342793428034281342823428334284342853428634287342883428934290342913429234293342943429534296342973429834299343003430134302343033430434305343063430734308343093431034311343123431334314343153431634317343183431934320343213432234323343243432534326343273432834329343303433134332343333433434335343363433734338343393434034341343423434334344343453434634347343483434934350343513435234353343543435534356343573435834359343603436134362343633436434365343663436734368343693437034371343723437334374343753437634377343783437934380343813438234383343843438534386343873438834389343903439134392343933439434395343963439734398343993440034401344023440334404344053440634407344083440934410344113441234413344143441534416344173441834419344203442134422344233442434425344263442734428344293443034431344323443334434344353443634437344383443934440344413444234443344443444534446344473444834449344503445134452344533445434455344563445734458344593446034461344623446334464344653446634467344683446934470344713447234473344743447534476344773447834479344803448134482344833448434485344863448734488344893449034491344923449334494344953449634497344983449934500345013450234503345043450534506345073450834509345103451134512345133451434515345163451734518345193452034521345223452334524345253452634527345283452934530345313453234533345343453534536345373453834539345403454134542345433454434545345463454734548345493455034551345523455334554345553455634557345583455934560345613456234563345643456534566345673456834569345703457134572345733457434575345763457734578345793458034581345823458334584345853458634587345883458934590345913459234593345943459534596345973459834599346003460134602346033460434605346063460734608346093461034611346123461334614346153461634617346183461934620346213462234623346243462534626346273462834629346303463134632346333463434635346363463734638346393464034641346423464334644346453464634647346483464934650346513465234653346543465534656346573465834659346603466134662346633466434665346663466734668346693467034671346723467334674346753467634677346783467934680346813468234683346843468534686346873468834689346903469134692346933469434695346963469734698346993470034701347023470334704347053470634707347083470934710347113471234713347143471534716347173471834719347203472134722347233472434725347263472734728347293473034731347323473334734347353473634737347383473934740347413474234743347443474534746347473474834749347503475134752347533475434755347563475734758347593476034761347623476334764347653476634767347683476934770347713477234773347743477534776347773477834779347803478134782347833478434785347863478734788347893479034791347923479334794347953479634797347983479934800348013480234803348043480534806348073480834809348103481134812348133481434815348163481734818348193482034821348223482334824348253482634827348283482934830348313483234833348343483534836348373483834839348403484134842348433484434845348463484734848348493485034851348523485334854348553485634857348583485934860348613486234863348643486534866348673486834869348703487134872348733487434875348763487734878348793488034881348823488334884348853488634887348883488934890348913489234893348943489534896348973489834899349003490134902349033490434905349063490734908349093491034911349123491334914349153491634917349183491934920349213492234923349243492534926349273492834929349303493134932349333493434935349363493734938349393494034941349423494334944349453494634947349483494934950349513495234953349543495534956349573495834959349603496134962349633496434965349663496734968349693497034971349723497334974349753497634977349783497934980349813498234983349843498534986349873498834989349903499134992349933499434995349963499734998349993500035001350023500335004350053500635007350083500935010350113501235013350143501535016350173501835019350203502135022350233502435025350263502735028350293503035031350323503335034350353503635037350383503935040350413504235043350443504535046350473504835049350503505135052350533505435055350563505735058350593506035061350623506335064350653506635067350683506935070350713507235073350743507535076350773507835079350803508135082350833508435085350863508735088350893509035091350923509335094350953509635097350983509935100351013510235103351043510535106351073510835109351103511135112351133511435115351163511735118351193512035121351223512335124351253512635127351283512935130351313513235133351343513535136351373513835139351403514135142351433514435145351463514735148351493515035151351523515335154351553515635157351583515935160351613516235163351643516535166351673516835169351703517135172351733517435175351763517735178351793518035181351823518335184351853518635187351883518935190351913519235193351943519535196351973519835199352003520135202352033520435205352063520735208352093521035211352123521335214352153521635217352183521935220352213522235223352243522535226352273522835229352303523135232352333523435235352363523735238352393524035241352423524335244352453524635247352483524935250352513525235253352543525535256352573525835259352603526135262352633526435265352663526735268352693527035271352723527335274352753527635277352783527935280352813528235283352843528535286352873528835289352903529135292352933529435295352963529735298352993530035301353023530335304353053530635307353083530935310353113531235313353143531535316353173531835319353203532135322353233532435325353263532735328353293533035331353323533335334353353533635337353383533935340353413534235343353443534535346353473534835349353503535135352353533535435355353563535735358353593536035361353623536335364353653536635367353683536935370353713537235373353743537535376353773537835379353803538135382353833538435385353863538735388353893539035391353923539335394353953539635397353983539935400354013540235403354043540535406354073540835409354103541135412354133541435415354163541735418354193542035421354223542335424354253542635427354283542935430354313543235433354343543535436354373543835439354403544135442354433544435445354463544735448354493545035451354523545335454354553545635457354583545935460354613546235463354643546535466354673546835469354703547135472354733547435475354763547735478354793548035481354823548335484354853548635487354883548935490354913549235493354943549535496354973549835499355003550135502355033550435505355063550735508355093551035511355123551335514355153551635517355183551935520355213552235523355243552535526355273552835529355303553135532355333553435535355363553735538355393554035541355423554335544355453554635547355483554935550355513555235553355543555535556355573555835559355603556135562355633556435565355663556735568355693557035571355723557335574355753557635577355783557935580355813558235583355843558535586355873558835589355903559135592355933559435595355963559735598355993560035601356023560335604356053560635607356083560935610356113561235613356143561535616356173561835619356203562135622356233562435625356263562735628356293563035631356323563335634356353563635637356383563935640356413564235643356443564535646356473564835649356503565135652356533565435655356563565735658356593566035661356623566335664356653566635667356683566935670356713567235673356743567535676356773567835679356803568135682356833568435685356863568735688356893569035691356923569335694356953569635697356983569935700357013570235703357043570535706357073570835709357103571135712357133571435715357163571735718357193572035721357223572335724357253572635727357283572935730357313573235733357343573535736357373573835739357403574135742357433574435745357463574735748357493575035751357523575335754357553575635757357583575935760357613576235763357643576535766357673576835769357703577135772357733577435775357763577735778357793578035781357823578335784357853578635787357883578935790357913579235793357943579535796357973579835799358003580135802358033580435805358063580735808358093581035811358123581335814358153581635817358183581935820358213582235823358243582535826358273582835829358303583135832358333583435835358363583735838358393584035841358423584335844358453584635847358483584935850358513585235853358543585535856358573585835859358603586135862358633586435865358663586735868358693587035871358723587335874358753587635877358783587935880358813588235883358843588535886358873588835889358903589135892358933589435895358963589735898358993590035901359023590335904359053590635907359083590935910359113591235913359143591535916359173591835919359203592135922359233592435925359263592735928359293593035931359323593335934359353593635937359383593935940359413594235943359443594535946359473594835949359503595135952359533595435955359563595735958359593596035961359623596335964359653596635967359683596935970359713597235973359743597535976359773597835979359803598135982359833598435985359863598735988359893599035991359923599335994359953599635997359983599936000360013600236003360043600536006360073600836009360103601136012360133601436015360163601736018360193602036021360223602336024360253602636027360283602936030360313603236033360343603536036360373603836039360403604136042360433604436045360463604736048360493605036051360523605336054360553605636057360583605936060360613606236063360643606536066360673606836069360703607136072360733607436075360763607736078360793608036081360823608336084360853608636087360883608936090360913609236093360943609536096360973609836099361003610136102361033610436105361063610736108361093611036111361123611336114361153611636117361183611936120361213612236123361243612536126361273612836129361303613136132361333613436135361363613736138361393614036141361423614336144361453614636147361483614936150361513615236153361543615536156361573615836159361603616136162361633616436165361663616736168361693617036171361723617336174361753617636177361783617936180361813618236183361843618536186361873618836189361903619136192361933619436195361963619736198361993620036201362023620336204362053620636207362083620936210362113621236213362143621536216362173621836219362203622136222362233622436225362263622736228362293623036231362323623336234362353623636237362383623936240362413624236243362443624536246362473624836249362503625136252362533625436255362563625736258362593626036261362623626336264362653626636267362683626936270362713627236273362743627536276362773627836279362803628136282362833628436285362863628736288362893629036291362923629336294362953629636297362983629936300363013630236303363043630536306363073630836309363103631136312363133631436315363163631736318363193632036321363223632336324363253632636327363283632936330363313633236333363343633536336363373633836339363403634136342363433634436345363463634736348363493635036351363523635336354363553635636357363583635936360363613636236363363643636536366363673636836369363703637136372363733637436375363763637736378363793638036381363823638336384363853638636387363883638936390363913639236393363943639536396363973639836399364003640136402364033640436405364063640736408364093641036411364123641336414364153641636417364183641936420364213642236423364243642536426364273642836429364303643136432364333643436435364363643736438364393644036441364423644336444364453644636447364483644936450364513645236453364543645536456364573645836459364603646136462364633646436465364663646736468364693647036471364723647336474364753647636477364783647936480364813648236483364843648536486364873648836489364903649136492364933649436495364963649736498364993650036501365023650336504365053650636507365083650936510365113651236513365143651536516365173651836519365203652136522365233652436525365263652736528365293653036531365323653336534365353653636537365383653936540365413654236543365443654536546365473654836549365503655136552365533655436555365563655736558365593656036561365623656336564365653656636567365683656936570365713657236573365743657536576365773657836579365803658136582365833658436585365863658736588365893659036591365923659336594365953659636597365983659936600366013660236603366043660536606366073660836609366103661136612366133661436615366163661736618366193662036621366223662336624366253662636627366283662936630366313663236633366343663536636366373663836639366403664136642366433664436645366463664736648366493665036651366523665336654366553665636657366583665936660366613666236663366643666536666366673666836669366703667136672366733667436675366763667736678366793668036681366823668336684366853668636687366883668936690366913669236693366943669536696366973669836699367003670136702367033670436705367063670736708367093671036711367123671336714367153671636717367183671936720367213672236723367243672536726367273672836729367303673136732367333673436735367363673736738367393674036741367423674336744367453674636747367483674936750367513675236753367543675536756367573675836759367603676136762367633676436765367663676736768367693677036771367723677336774367753677636777367783677936780367813678236783367843678536786367873678836789367903679136792367933679436795367963679736798367993680036801368023680336804368053680636807368083680936810368113681236813368143681536816368173681836819368203682136822368233682436825368263682736828368293683036831368323683336834368353683636837368383683936840368413684236843368443684536846368473684836849368503685136852368533685436855368563685736858368593686036861368623686336864368653686636867368683686936870368713687236873368743687536876368773687836879368803688136882368833688436885368863688736888368893689036891368923689336894368953689636897368983689936900369013690236903369043690536906369073690836909369103691136912369133691436915369163691736918369193692036921369223692336924369253692636927369283692936930369313693236933369343693536936369373693836939369403694136942369433694436945369463694736948369493695036951369523695336954369553695636957369583695936960369613696236963369643696536966369673696836969369703697136972369733697436975369763697736978369793698036981369823698336984369853698636987369883698936990369913699236993369943699536996369973699836999370003700137002370033700437005370063700737008370093701037011370123701337014370153701637017370183701937020370213702237023370243702537026370273702837029370303703137032370333703437035370363703737038370393704037041370423704337044370453704637047370483704937050370513705237053370543705537056370573705837059370603706137062370633706437065370663706737068370693707037071370723707337074370753707637077370783707937080370813708237083370843708537086370873708837089370903709137092370933709437095370963709737098370993710037101371023710337104371053710637107371083710937110371113711237113371143711537116371173711837119371203712137122371233712437125371263712737128371293713037131371323713337134371353713637137371383713937140371413714237143371443714537146371473714837149371503715137152371533715437155371563715737158371593716037161371623716337164371653716637167371683716937170371713717237173371743717537176371773717837179371803718137182371833718437185371863718737188371893719037191371923719337194371953719637197371983719937200372013720237203372043720537206372073720837209372103721137212372133721437215372163721737218372193722037221372223722337224372253722637227372283722937230372313723237233372343723537236372373723837239372403724137242372433724437245372463724737248372493725037251372523725337254372553725637257372583725937260372613726237263372643726537266372673726837269372703727137272372733727437275372763727737278372793728037281372823728337284372853728637287372883728937290372913729237293372943729537296372973729837299373003730137302373033730437305373063730737308373093731037311373123731337314373153731637317373183731937320373213732237323373243732537326373273732837329373303733137332373333733437335373363733737338373393734037341373423734337344373453734637347373483734937350373513735237353373543735537356373573735837359373603736137362373633736437365373663736737368373693737037371373723737337374373753737637377373783737937380373813738237383373843738537386373873738837389373903739137392373933739437395373963739737398373993740037401374023740337404374053740637407374083740937410374113741237413374143741537416374173741837419374203742137422374233742437425374263742737428374293743037431374323743337434374353743637437374383743937440374413744237443374443744537446374473744837449374503745137452374533745437455374563745737458374593746037461374623746337464374653746637467374683746937470374713747237473374743747537476374773747837479374803748137482374833748437485374863748737488374893749037491374923749337494374953749637497374983749937500375013750237503375043750537506375073750837509375103751137512375133751437515375163751737518375193752037521375223752337524375253752637527375283752937530375313753237533375343753537536375373753837539375403754137542375433754437545375463754737548375493755037551375523755337554375553755637557375583755937560375613756237563375643756537566375673756837569375703757137572375733757437575375763757737578375793758037581375823758337584375853758637587375883758937590375913759237593375943759537596375973759837599376003760137602376033760437605376063760737608376093761037611376123761337614376153761637617376183761937620376213762237623376243762537626376273762837629376303763137632376333763437635376363763737638376393764037641376423764337644376453764637647376483764937650376513765237653376543765537656376573765837659376603766137662376633766437665376663766737668376693767037671376723767337674376753767637677376783767937680376813768237683376843768537686376873768837689376903769137692376933769437695376963769737698376993770037701377023770337704377053770637707377083770937710377113771237713377143771537716377173771837719377203772137722377233772437725377263772737728377293773037731377323773337734377353773637737377383773937740377413774237743377443774537746377473774837749377503775137752377533775437755377563775737758377593776037761377623776337764377653776637767377683776937770377713777237773377743777537776377773777837779377803778137782377833778437785377863778737788377893779037791377923779337794377953779637797377983779937800378013780237803378043780537806378073780837809378103781137812378133781437815378163781737818378193782037821378223782337824378253782637827378283782937830378313783237833378343783537836378373783837839378403784137842378433784437845378463784737848378493785037851378523785337854378553785637857378583785937860378613786237863378643786537866378673786837869378703787137872378733787437875378763787737878378793788037881378823788337884378853788637887378883788937890378913789237893378943789537896378973789837899379003790137902379033790437905379063790737908379093791037911379123791337914379153791637917379183791937920379213792237923379243792537926379273792837929379303793137932379333793437935379363793737938379393794037941379423794337944379453794637947379483794937950379513795237953379543795537956379573795837959379603796137962379633796437965379663796737968379693797037971379723797337974379753797637977379783797937980379813798237983379843798537986379873798837989379903799137992379933799437995379963799737998
  1. --- /dev/null
  2. +++ linux-4.19/Documentation/ABI/testing/debugfs-aufs 2018-11-20 09:35:14.996195503 +0200
  3. @@ -0,0 +1,55 @@
  4. +What: /debug/aufs/si_<id>/
  5. +Date: March 2009
  6. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  7. +Description:
  8. + Under /debug/aufs, a directory named si_<id> is created
  9. + per aufs mount, where <id> is a unique id generated
  10. + internally.
  11. +
  12. +What: /debug/aufs/si_<id>/plink
  13. +Date: Apr 2013
  14. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  15. +Description:
  16. + It has three lines and shows the information about the
  17. + pseudo-link. The first line is a single number
  18. + representing a number of buckets. The second line is a
  19. + number of pseudo-links per buckets (separated by a
  20. + blank). The last line is a single number representing a
  21. + total number of psedo-links.
  22. + When the aufs mount option 'noplink' is specified, it
  23. + will show "1\n0\n0\n".
  24. +
  25. +What: /debug/aufs/si_<id>/xib
  26. +Date: March 2009
  27. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  28. +Description:
  29. + It shows the consumed blocks by xib (External Inode Number
  30. + Bitmap), its block size and file size.
  31. + When the aufs mount option 'noxino' is specified, it
  32. + will be empty. About XINO files, see the aufs manual.
  33. +
  34. +What: /debug/aufs/si_<id>/xi0, xi1 ... xiN and xiN-N
  35. +Date: March 2009
  36. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  37. +Description:
  38. + It shows the consumed blocks by xino (External Inode Number
  39. + Translation Table), its link count, block size and file
  40. + size.
  41. + Due to the file size limit, there may exist multiple
  42. + xino files per branch. In this case, "-N" is added to
  43. + the filename and it corresponds to the index of the
  44. + internal xino array. "-0" is omitted.
  45. + When the aufs mount option 'noxino' is specified, Those
  46. + entries won't exist. About XINO files, see the aufs
  47. + manual.
  48. +
  49. +What: /debug/aufs/si_<id>/xigen
  50. +Date: March 2009
  51. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  52. +Description:
  53. + It shows the consumed blocks by xigen (External Inode
  54. + Generation Table), its block size and file size.
  55. + If CONFIG_AUFS_EXPORT is disabled, this entry will not
  56. + be created.
  57. + When the aufs mount option 'noxino' is specified, it
  58. + will be empty. About XINO files, see the aufs manual.
  59. --- /dev/null
  60. +++ linux-4.19/Documentation/ABI/testing/sysfs-aufs 2018-11-20 09:35:14.996195503 +0200
  61. @@ -0,0 +1,31 @@
  62. +What: /sys/fs/aufs/si_<id>/
  63. +Date: March 2009
  64. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  65. +Description:
  66. + Under /sys/fs/aufs, a directory named si_<id> is created
  67. + per aufs mount, where <id> is a unique id generated
  68. + internally.
  69. +
  70. +What: /sys/fs/aufs/si_<id>/br0, br1 ... brN
  71. +Date: March 2009
  72. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  73. +Description:
  74. + It shows the abolute path of a member directory (which
  75. + is called branch) in aufs, and its permission.
  76. +
  77. +What: /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
  78. +Date: July 2013
  79. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  80. +Description:
  81. + It shows the id of a member directory (which is called
  82. + branch) in aufs.
  83. +
  84. +What: /sys/fs/aufs/si_<id>/xi_path
  85. +Date: March 2009
  86. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  87. +Description:
  88. + It shows the abolute path of XINO (External Inode Number
  89. + Bitmap, Translation Table and Generation Table) file
  90. + even if it is the default path.
  91. + When the aufs mount option 'noxino' is specified, it
  92. + will be empty. About XINO files, see the aufs manual.
  93. --- /dev/null
  94. +++ linux-4.19/Documentation/filesystems/aufs/design/01intro.txt 2018-11-20 09:35:14.998195503 +0200
  95. @@ -0,0 +1,171 @@
  96. +
  97. +# Copyright (C) 2005-2018 Junjiro R. Okajima
  98. +#
  99. +# This program is free software; you can redistribute it and/or modify
  100. +# it under the terms of the GNU General Public License as published by
  101. +# the Free Software Foundation; either version 2 of the License, or
  102. +# (at your option) any later version.
  103. +#
  104. +# This program is distributed in the hope that it will be useful,
  105. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  106. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  107. +# GNU General Public License for more details.
  108. +#
  109. +# You should have received a copy of the GNU General Public License
  110. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  111. +
  112. +Introduction
  113. +----------------------------------------
  114. +
  115. +aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
  116. +1. abbrev. for "advanced multi-layered unification filesystem".
  117. +2. abbrev. for "another unionfs".
  118. +3. abbrev. for "auf das" in German which means "on the" in English.
  119. + Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
  120. + But "Filesystem aufs Filesystem" is hard to understand.
  121. +4. abbrev. for "African Urban Fashion Show".
  122. +
  123. +AUFS is a filesystem with features:
  124. +- multi layered stackable unification filesystem, the member directory
  125. + is called as a branch.
  126. +- branch permission and attribute, 'readonly', 'real-readonly',
  127. + 'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
  128. + combination.
  129. +- internal "file copy-on-write".
  130. +- logical deletion, whiteout.
  131. +- dynamic branch manipulation, adding, deleting and changing permission.
  132. +- allow bypassing aufs, user's direct branch access.
  133. +- external inode number translation table and bitmap which maintains the
  134. + persistent aufs inode number.
  135. +- seekable directory, including NFS readdir.
  136. +- file mapping, mmap and sharing pages.
  137. +- pseudo-link, hardlink over branches.
  138. +- loopback mounted filesystem as a branch.
  139. +- several policies to select one among multiple writable branches.
  140. +- revert a single systemcall when an error occurs in aufs.
  141. +- and more...
  142. +
  143. +
  144. +Multi Layered Stackable Unification Filesystem
  145. +----------------------------------------------------------------------
  146. +Most people already knows what it is.
  147. +It is a filesystem which unifies several directories and provides a
  148. +merged single directory. When users access a file, the access will be
  149. +passed/re-directed/converted (sorry, I am not sure which English word is
  150. +correct) to the real file on the member filesystem. The member
  151. +filesystem is called 'lower filesystem' or 'branch' and has a mode
  152. +'readonly' and 'readwrite.' And the deletion for a file on the lower
  153. +readonly branch is handled by creating 'whiteout' on the upper writable
  154. +branch.
  155. +
  156. +On LKML, there have been discussions about UnionMount (Jan Blunck,
  157. +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
  158. +different approaches to implement the merged-view.
  159. +The former tries putting it into VFS, and the latter implements as a
  160. +separate filesystem.
  161. +(If I misunderstand about these implementations, please let me know and
  162. +I shall correct it. Because it is a long time ago when I read their
  163. +source files last time).
  164. +
  165. +UnionMount's approach will be able to small, but may be hard to share
  166. +branches between several UnionMount since the whiteout in it is
  167. +implemented in the inode on branch filesystem and always
  168. +shared. According to Bharata's post, readdir does not seems to be
  169. +finished yet.
  170. +There are several missing features known in this implementations such as
  171. +- for users, the inode number may change silently. eg. copy-up.
  172. +- link(2) may break by copy-up.
  173. +- read(2) may get an obsoleted filedata (fstat(2) too).
  174. +- fcntl(F_SETLK) may be broken by copy-up.
  175. +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
  176. + open(O_RDWR).
  177. +
  178. +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
  179. +merged into mainline. This is another implementation of UnionMount as a
  180. +separated filesystem. All the limitations and known problems which
  181. +UnionMount are equally inherited to "overlay" filesystem.
  182. +
  183. +Unionfs has a longer history. When I started implementing a stackable
  184. +filesystem (Aug 2005), it already existed. It has virtual super_block,
  185. +inode, dentry and file objects and they have an array pointing lower
  186. +same kind objects. After contributing many patches for Unionfs, I
  187. +re-started my project AUFS (Jun 2006).
  188. +
  189. +In AUFS, the structure of filesystem resembles to Unionfs, but I
  190. +implemented my own ideas, approaches and enhancements and it became
  191. +totally different one.
  192. +
  193. +Comparing DM snapshot and fs based implementation
  194. +- the number of bytes to be copied between devices is much smaller.
  195. +- the type of filesystem must be one and only.
  196. +- the fs must be writable, no readonly fs, even for the lower original
  197. + device. so the compression fs will not be usable. but if we use
  198. + loopback mount, we may address this issue.
  199. + for instance,
  200. + mount /cdrom/squashfs.img /sq
  201. + losetup /sq/ext2.img
  202. + losetup /somewhere/cow
  203. + dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
  204. +- it will be difficult (or needs more operations) to extract the
  205. + difference between the original device and COW.
  206. +- DM snapshot-merge may help a lot when users try merging. in the
  207. + fs-layer union, users will use rsync(1).
  208. +
  209. +You may want to read my old paper "Filesystems in LiveCD"
  210. +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
  211. +
  212. +
  213. +Several characters/aspects/persona of aufs
  214. +----------------------------------------------------------------------
  215. +
  216. +Aufs has several characters, aspects or persona.
  217. +1. a filesystem, callee of VFS helper
  218. +2. sub-VFS, caller of VFS helper for branches
  219. +3. a virtual filesystem which maintains persistent inode number
  220. +4. reader/writer of files on branches such like an application
  221. +
  222. +1. Callee of VFS Helper
  223. +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
  224. +unlink(2) from an application reaches sys_unlink() kernel function and
  225. +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
  226. +calls filesystem specific unlink operation. Actually aufs implements the
  227. +unlink operation but it behaves like a redirector.
  228. +
  229. +2. Caller of VFS Helper for Branches
  230. +aufs_unlink() passes the unlink request to the branch filesystem as if
  231. +it were called from VFS. So the called unlink operation of the branch
  232. +filesystem acts as usual. As a caller of VFS helper, aufs should handle
  233. +every necessary pre/post operation for the branch filesystem.
  234. +- acquire the lock for the parent dir on a branch
  235. +- lookup in a branch
  236. +- revalidate dentry on a branch
  237. +- mnt_want_write() for a branch
  238. +- vfs_unlink() for a branch
  239. +- mnt_drop_write() for a branch
  240. +- release the lock on a branch
  241. +
  242. +3. Persistent Inode Number
  243. +One of the most important issue for a filesystem is to maintain inode
  244. +numbers. This is particularly important to support exporting a
  245. +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
  246. +backend block device for its own. But some storage is necessary to
  247. +keep and maintain the inode numbers. It may be a large space and may not
  248. +suit to keep in memory. Aufs rents some space from its first writable
  249. +branch filesystem (by default) and creates file(s) on it. These files
  250. +are created by aufs internally and removed soon (currently) keeping
  251. +opened.
  252. +Note: Because these files are removed, they are totally gone after
  253. + unmounting aufs. It means the inode numbers are not persistent
  254. + across unmount or reboot. I have a plan to make them really
  255. + persistent which will be important for aufs on NFS server.
  256. +
  257. +4. Read/Write Files Internally (copy-on-write)
  258. +Because a branch can be readonly, when you write a file on it, aufs will
  259. +"copy-up" it to the upper writable branch internally. And then write the
  260. +originally requested thing to the file. Generally kernel doesn't
  261. +open/read/write file actively. In aufs, even a single write may cause a
  262. +internal "file copy". This behaviour is very similar to cp(1) command.
  263. +
  264. +Some people may think it is better to pass such work to user space
  265. +helper, instead of doing in kernel space. Actually I am still thinking
  266. +about it. But currently I have implemented it in kernel space.
  267. --- /dev/null
  268. +++ linux-4.19/Documentation/filesystems/aufs/design/02struct.txt 2018-11-20 09:35:14.999195503 +0200
  269. @@ -0,0 +1,258 @@
  270. +
  271. +# Copyright (C) 2005-2018 Junjiro R. Okajima
  272. +#
  273. +# This program is free software; you can redistribute it and/or modify
  274. +# it under the terms of the GNU General Public License as published by
  275. +# the Free Software Foundation; either version 2 of the License, or
  276. +# (at your option) any later version.
  277. +#
  278. +# This program is distributed in the hope that it will be useful,
  279. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  280. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  281. +# GNU General Public License for more details.
  282. +#
  283. +# You should have received a copy of the GNU General Public License
  284. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  285. +
  286. +Basic Aufs Internal Structure
  287. +
  288. +Superblock/Inode/Dentry/File Objects
  289. +----------------------------------------------------------------------
  290. +As like an ordinary filesystem, aufs has its own
  291. +superblock/inode/dentry/file objects. All these objects have a
  292. +dynamically allocated array and store the same kind of pointers to the
  293. +lower filesystem, branch.
  294. +For example, when you build a union with one readwrite branch and one
  295. +readonly, mounted /au, /rw and /ro respectively.
  296. +- /au = /rw + /ro
  297. +- /ro/fileA exists but /rw/fileA
  298. +
  299. +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
  300. +pointers are stored in a aufs dentry. The array in aufs dentry will be,
  301. +- [0] = NULL (because /rw/fileA doesn't exist)
  302. +- [1] = /ro/fileA
  303. +
  304. +This style of an array is essentially same to the aufs
  305. +superblock/inode/dentry/file objects.
  306. +
  307. +Because aufs supports manipulating branches, ie. add/delete/change
  308. +branches dynamically, these objects has its own generation. When
  309. +branches are changed, the generation in aufs superblock is
  310. +incremented. And a generation in other object are compared when it is
  311. +accessed. When a generation in other objects are obsoleted, aufs
  312. +refreshes the internal array.
  313. +
  314. +
  315. +Superblock
  316. +----------------------------------------------------------------------
  317. +Additionally aufs superblock has some data for policies to select one
  318. +among multiple writable branches, XIB files, pseudo-links and kobject.
  319. +See below in detail.
  320. +About the policies which supports copy-down a directory, see
  321. +wbr_policy.txt too.
  322. +
  323. +
  324. +Branch and XINO(External Inode Number Translation Table)
  325. +----------------------------------------------------------------------
  326. +Every branch has its own xino (external inode number translation table)
  327. +file. The xino file is created and unlinked by aufs internally. When two
  328. +members of a union exist on the same filesystem, they share the single
  329. +xino file.
  330. +The struct of a xino file is simple, just a sequence of aufs inode
  331. +numbers which is indexed by the lower inode number.
  332. +In the above sample, assume the inode number of /ro/fileA is i111 and
  333. +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
  334. +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
  335. +
  336. +When the inode numbers are not contiguous, the xino file will be sparse
  337. +which has a hole in it and doesn't consume as much disk space as it
  338. +might appear. If your branch filesystem consumes disk space for such
  339. +holes, then you should specify 'xino=' option at mounting aufs.
  340. +
  341. +Aufs has a mount option to free the disk blocks for such holes in XINO
  342. +files on tmpfs or ramdisk. But it is not so effective actually. If you
  343. +meet a problem of disk shortage due to XINO files, then you should try
  344. +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
  345. +The patch localizes the assignment inumbers per tmpfs-mount and avoid
  346. +the holes in XINO files.
  347. +
  348. +Also a writable branch has three kinds of "whiteout bases". All these
  349. +are existed when the branch is joined to aufs, and their names are
  350. +whiteout-ed doubly, so that users will never see their names in aufs
  351. +hierarchy.
  352. +1. a regular file which will be hardlinked to all whiteouts.
  353. +2. a directory to store a pseudo-link.
  354. +3. a directory to store an "orphan"-ed file temporary.
  355. +
  356. +1. Whiteout Base
  357. + When you remove a file on a readonly branch, aufs handles it as a
  358. + logical deletion and creates a whiteout on the upper writable branch
  359. + as a hardlink of this file in order not to consume inode on the
  360. + writable branch.
  361. +2. Pseudo-link Dir
  362. + See below, Pseudo-link.
  363. +3. Step-Parent Dir
  364. + When "fileC" exists on the lower readonly branch only and it is
  365. + opened and removed with its parent dir, and then user writes
  366. + something into it, then aufs copies-up fileC to this
  367. + directory. Because there is no other dir to store fileC. After
  368. + creating a file under this dir, the file is unlinked.
  369. +
  370. +Because aufs supports manipulating branches, ie. add/delete/change
  371. +dynamically, a branch has its own id. When the branch order changes,
  372. +aufs finds the new index by searching the branch id.
  373. +
  374. +
  375. +Pseudo-link
  376. +----------------------------------------------------------------------
  377. +Assume "fileA" exists on the lower readonly branch only and it is
  378. +hardlinked to "fileB" on the branch. When you write something to fileA,
  379. +aufs copies-up it to the upper writable branch. Additionally aufs
  380. +creates a hardlink under the Pseudo-link Directory of the writable
  381. +branch. The inode of a pseudo-link is kept in aufs super_block as a
  382. +simple list. If fileB is read after unlinking fileA, aufs returns
  383. +filedata from the pseudo-link instead of the lower readonly
  384. +branch. Because the pseudo-link is based upon the inode, to keep the
  385. +inode number by xino (see above) is essentially necessary.
  386. +
  387. +All the hardlinks under the Pseudo-link Directory of the writable branch
  388. +should be restored in a proper location later. Aufs provides a utility
  389. +to do this. The userspace helpers executed at remounting and unmounting
  390. +aufs by default.
  391. +During this utility is running, it puts aufs into the pseudo-link
  392. +maintenance mode. In this mode, only the process which began the
  393. +maintenance mode (and its child processes) is allowed to operate in
  394. +aufs. Some other processes which are not related to the pseudo-link will
  395. +be allowed to run too, but the rest have to return an error or wait
  396. +until the maintenance mode ends. If a process already acquires an inode
  397. +mutex (in VFS), it has to return an error.
  398. +
  399. +
  400. +XIB(external inode number bitmap)
  401. +----------------------------------------------------------------------
  402. +Addition to the xino file per a branch, aufs has an external inode number
  403. +bitmap in a superblock object. It is also an internal file such like a
  404. +xino file.
  405. +It is a simple bitmap to mark whether the aufs inode number is in-use or
  406. +not.
  407. +To reduce the file I/O, aufs prepares a single memory page to cache xib.
  408. +
  409. +As well as XINO files, aufs has a feature to truncate/refresh XIB to
  410. +reduce the number of consumed disk blocks for these files.
  411. +
  412. +
  413. +Virtual or Vertical Dir, and Readdir in Userspace
  414. +----------------------------------------------------------------------
  415. +In order to support multiple layers (branches), aufs readdir operation
  416. +constructs a virtual dir block on memory. For readdir, aufs calls
  417. +vfs_readdir() internally for each dir on branches, merges their entries
  418. +with eliminating the whiteout-ed ones, and sets it to file (dir)
  419. +object. So the file object has its entry list until it is closed. The
  420. +entry list will be updated when the file position is zero and becomes
  421. +obsoleted. This decision is made in aufs automatically.
  422. +
  423. +The dynamically allocated memory block for the name of entries has a
  424. +unit of 512 bytes (by default) and stores the names contiguously (no
  425. +padding). Another block for each entry is handled by kmem_cache too.
  426. +During building dir blocks, aufs creates hash list and judging whether
  427. +the entry is whiteouted by its upper branch or already listed.
  428. +The merged result is cached in the corresponding inode object and
  429. +maintained by a customizable life-time option.
  430. +
  431. +Some people may call it can be a security hole or invite DoS attack
  432. +since the opened and once readdir-ed dir (file object) holds its entry
  433. +list and becomes a pressure for system memory. But I'd say it is similar
  434. +to files under /proc or /sys. The virtual files in them also holds a
  435. +memory page (generally) while they are opened. When an idea to reduce
  436. +memory for them is introduced, it will be applied to aufs too.
  437. +For those who really hate this situation, I've developed readdir(3)
  438. +library which operates this merging in userspace. You just need to set
  439. +LD_PRELOAD environment variable, and aufs will not consume no memory in
  440. +kernel space for readdir(3).
  441. +
  442. +
  443. +Workqueue
  444. +----------------------------------------------------------------------
  445. +Aufs sometimes requires privilege access to a branch. For instance,
  446. +in copy-up/down operation. When a user process is going to make changes
  447. +to a file which exists in the lower readonly branch only, and the mode
  448. +of one of ancestor directories may not be writable by a user
  449. +process. Here aufs copy-up the file with its ancestors and they may
  450. +require privilege to set its owner/group/mode/etc.
  451. +This is a typical case of a application character of aufs (see
  452. +Introduction).
  453. +
  454. +Aufs uses workqueue synchronously for this case. It creates its own
  455. +workqueue. The workqueue is a kernel thread and has privilege. Aufs
  456. +passes the request to call mkdir or write (for example), and wait for
  457. +its completion. This approach solves a problem of a signal handler
  458. +simply.
  459. +If aufs didn't adopt the workqueue and changed the privilege of the
  460. +process, then the process may receive the unexpected SIGXFSZ or other
  461. +signals.
  462. +
  463. +Also aufs uses the system global workqueue ("events" kernel thread) too
  464. +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
  465. +whiteout base and etc. This is unrelated to a privilege.
  466. +Most of aufs operation tries acquiring a rw_semaphore for aufs
  467. +superblock at the beginning, at the same time waits for the completion
  468. +of all queued asynchronous tasks.
  469. +
  470. +
  471. +Whiteout
  472. +----------------------------------------------------------------------
  473. +The whiteout in aufs is very similar to Unionfs's. That is represented
  474. +by its filename. UnionMount takes an approach of a file mode, but I am
  475. +afraid several utilities (find(1) or something) will have to support it.
  476. +
  477. +Basically the whiteout represents "logical deletion" which stops aufs to
  478. +lookup further, but also it represents "dir is opaque" which also stop
  479. +further lookup.
  480. +
  481. +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
  482. +In order to make several functions in a single systemcall to be
  483. +revertible, aufs adopts an approach to rename a directory to a temporary
  484. +unique whiteouted name.
  485. +For example, in rename(2) dir where the target dir already existed, aufs
  486. +renames the target dir to a temporary unique whiteouted name before the
  487. +actual rename on a branch, and then handles other actions (make it opaque,
  488. +update the attributes, etc). If an error happens in these actions, aufs
  489. +simply renames the whiteouted name back and returns an error. If all are
  490. +succeeded, aufs registers a function to remove the whiteouted unique
  491. +temporary name completely and asynchronously to the system global
  492. +workqueue.
  493. +
  494. +
  495. +Copy-up
  496. +----------------------------------------------------------------------
  497. +It is a well-known feature or concept.
  498. +When user modifies a file on a readonly branch, aufs operate "copy-up"
  499. +internally and makes change to the new file on the upper writable branch.
  500. +When the trigger systemcall does not update the timestamps of the parent
  501. +dir, aufs reverts it after copy-up.
  502. +
  503. +
  504. +Move-down (aufs3.9 and later)
  505. +----------------------------------------------------------------------
  506. +"Copy-up" is one of the essential feature in aufs. It copies a file from
  507. +the lower readonly branch to the upper writable branch when a user
  508. +changes something about the file.
  509. +"Move-down" is an opposite action of copy-up. Basically this action is
  510. +ran manually instead of automatically and internally.
  511. +For desgin and implementation, aufs has to consider these issues.
  512. +- whiteout for the file may exist on the lower branch.
  513. +- ancestor directories may not exist on the lower branch.
  514. +- diropq for the ancestor directories may exist on the upper branch.
  515. +- free space on the lower branch will reduce.
  516. +- another access to the file may happen during moving-down, including
  517. + UDBA (see "Revalidate Dentry and UDBA").
  518. +- the file should not be hard-linked nor pseudo-linked. they should be
  519. + handled by auplink utility later.
  520. +
  521. +Sometimes users want to move-down a file from the upper writable branch
  522. +to the lower readonly or writable branch. For instance,
  523. +- the free space of the upper writable branch is going to run out.
  524. +- create a new intermediate branch between the upper and lower branch.
  525. +- etc.
  526. +
  527. +For this purpose, use "aumvdown" command in aufs-util.git.
  528. --- /dev/null
  529. +++ linux-4.19/Documentation/filesystems/aufs/design/03atomic_open.txt 2018-11-20 09:35:14.999195503 +0200
  530. @@ -0,0 +1,85 @@
  531. +
  532. +# Copyright (C) 2015-2018 Junjiro R. Okajima
  533. +#
  534. +# This program is free software; you can redistribute it and/or modify
  535. +# it under the terms of the GNU General Public License as published by
  536. +# the Free Software Foundation; either version 2 of the License, or
  537. +# (at your option) any later version.
  538. +#
  539. +# This program is distributed in the hope that it will be useful,
  540. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  541. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  542. +# GNU General Public License for more details.
  543. +#
  544. +# You should have received a copy of the GNU General Public License
  545. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  546. +
  547. +Support for a branch who has its ->atomic_open()
  548. +----------------------------------------------------------------------
  549. +The filesystems who implement its ->atomic_open() are not majority. For
  550. +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
  551. +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
  552. +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
  553. +sure whether all filesystems who have ->atomic_open() behave like this,
  554. +but NFSv4 surely returns the error.
  555. +
  556. +In order to support ->atomic_open() for aufs, there are a few
  557. +approaches.
  558. +
  559. +A. Introduce aufs_atomic_open()
  560. + - calls one of VFS:do_last(), lookup_open() or atomic_open() for
  561. + branch fs.
  562. +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
  563. + an aufs user Pip Cet's approach
  564. + - calls aufs_create(), VFS finish_open() and notify_change().
  565. + - pass fake-mode to finish_open(), and then correct the mode by
  566. + notify_change().
  567. +C. Extend aufs_open() to call branch fs's ->atomic_open()
  568. + - no aufs_atomic_open().
  569. + - aufs_lookup() registers the TID to an aufs internal object.
  570. + - aufs_create() does nothing when the matching TID is registered, but
  571. + registers the mode.
  572. + - aufs_open() calls branch fs's ->atomic_open() when the matching
  573. + TID is registered.
  574. +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
  575. + credential
  576. + - no aufs_atomic_open().
  577. + - aufs_create() registers the TID to an internal object. this info
  578. + represents "this process created this file just now."
  579. + - when aufs gets EACCES from branch fs's ->open(), then confirm the
  580. + registered TID and re-try open() with superuser's credential.
  581. +
  582. +Pros and cons for each approach.
  583. +
  584. +A.
  585. + - straightforward but highly depends upon VFS internal.
  586. + - the atomic behavaiour is kept.
  587. + - some of parameters such as nameidata are hard to reproduce for
  588. + branch fs.
  589. + - large overhead.
  590. +B.
  591. + - easy to implement.
  592. + - the atomic behavaiour is lost.
  593. +C.
  594. + - the atomic behavaiour is kept.
  595. + - dirty and tricky.
  596. + - VFS checks whether the file is created correctly after calling
  597. + ->create(), which means this approach doesn't work.
  598. +D.
  599. + - easy to implement.
  600. + - the atomic behavaiour is lost.
  601. + - to open a file with superuser's credential and give it to a user
  602. + process is a bad idea, since the file object keeps the credential
  603. + in it. It may affect LSM or something. This approach doesn't work
  604. + either.
  605. +
  606. +The approach A is ideal, but it hard to implement. So here is a
  607. +variation of A, which is to be implemented.
  608. +
  609. +A-1. Introduce aufs_atomic_open()
  610. + - calls branch fs ->atomic_open() if exists. otherwise calls
  611. + vfs_create() and finish_open().
  612. + - the demerit is that the several checks after branch fs
  613. + ->atomic_open() are lost. in the ordinary case, the checks are
  614. + done by VFS:do_last(), lookup_open() and atomic_open(). some can
  615. + be implemented in aufs, but not all I am afraid.
  616. --- /dev/null
  617. +++ linux-4.19/Documentation/filesystems/aufs/design/03lookup.txt 2018-11-20 09:35:15.000195503 +0200
  618. @@ -0,0 +1,113 @@
  619. +
  620. +# Copyright (C) 2005-2018 Junjiro R. Okajima
  621. +#
  622. +# This program is free software; you can redistribute it and/or modify
  623. +# it under the terms of the GNU General Public License as published by
  624. +# the Free Software Foundation; either version 2 of the License, or
  625. +# (at your option) any later version.
  626. +#
  627. +# This program is distributed in the hope that it will be useful,
  628. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  629. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  630. +# GNU General Public License for more details.
  631. +#
  632. +# You should have received a copy of the GNU General Public License
  633. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  634. +
  635. +Lookup in a Branch
  636. +----------------------------------------------------------------------
  637. +Since aufs has a character of sub-VFS (see Introduction), it operates
  638. +lookup for branches as VFS does. It may be a heavy work. But almost all
  639. +lookup operation in aufs is the simplest case, ie. lookup only an entry
  640. +directly connected to its parent. Digging down the directory hierarchy
  641. +is unnecessary. VFS has a function lookup_one_len() for that use, and
  642. +aufs calls it.
  643. +
  644. +When a branch is a remote filesystem, aufs basically relies upon its
  645. +->d_revalidate(), also aufs forces the hardest revalidate tests for
  646. +them.
  647. +For d_revalidate, aufs implements three levels of revalidate tests. See
  648. +"Revalidate Dentry and UDBA" in detail.
  649. +
  650. +
  651. +Test Only the Highest One for the Directory Permission (dirperm1 option)
  652. +----------------------------------------------------------------------
  653. +Let's try case study.
  654. +- aufs has two branches, upper readwrite and lower readonly.
  655. + /au = /rw + /ro
  656. +- "dirA" exists under /ro, but /rw. and its mode is 0700.
  657. +- user invoked "chmod a+rx /au/dirA"
  658. +- the internal copy-up is activated and "/rw/dirA" is created and its
  659. + permission bits are set to world readable.
  660. +- then "/au/dirA" becomes world readable?
  661. +
  662. +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
  663. +or it may be a natively readonly filesystem. If aufs respects the lower
  664. +branch, it should not respond readdir request from other users. But user
  665. +allowed it by chmod. Should really aufs rejects showing the entries
  666. +under /ro/dirA?
  667. +
  668. +To be honest, I don't have a good solution for this case. So aufs
  669. +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
  670. +users.
  671. +When dirperm1 is specified, aufs checks only the highest one for the
  672. +directory permission, and shows the entries. Otherwise, as usual, checks
  673. +every dir existing on all branches and rejects the request.
  674. +
  675. +As a side effect, dirperm1 option improves the performance of aufs
  676. +because the number of permission check is reduced when the number of
  677. +branch is many.
  678. +
  679. +
  680. +Revalidate Dentry and UDBA (User's Direct Branch Access)
  681. +----------------------------------------------------------------------
  682. +Generally VFS helpers re-validate a dentry as a part of lookup.
  683. +0. digging down the directory hierarchy.
  684. +1. lock the parent dir by its i_mutex.
  685. +2. lookup the final (child) entry.
  686. +3. revalidate it.
  687. +4. call the actual operation (create, unlink, etc.)
  688. +5. unlock the parent dir
  689. +
  690. +If the filesystem implements its ->d_revalidate() (step 3), then it is
  691. +called. Actually aufs implements it and checks the dentry on a branch is
  692. +still valid.
  693. +But it is not enough. Because aufs has to release the lock for the
  694. +parent dir on a branch at the end of ->lookup() (step 2) and
  695. +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
  696. +held by VFS.
  697. +If the file on a branch is changed directly, eg. bypassing aufs, after
  698. +aufs released the lock, then the subsequent operation may cause
  699. +something unpleasant result.
  700. +
  701. +This situation is a result of VFS architecture, ->lookup() and
  702. +->d_revalidate() is separated. But I never say it is wrong. It is a good
  703. +design from VFS's point of view. It is just not suitable for sub-VFS
  704. +character in aufs.
  705. +
  706. +Aufs supports such case by three level of revalidation which is
  707. +selectable by user.
  708. +1. Simple Revalidate
  709. + Addition to the native flow in VFS's, confirm the child-parent
  710. + relationship on the branch just after locking the parent dir on the
  711. + branch in the "actual operation" (step 4). When this validation
  712. + fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
  713. + checks the validation of the dentry on branches.
  714. +2. Monitor Changes Internally by Inotify/Fsnotify
  715. + Addition to above, in the "actual operation" (step 4) aufs re-lookup
  716. + the dentry on the branch, and returns EBUSY if it finds different
  717. + dentry.
  718. + Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
  719. + during it is in cache. When the event is notified, aufs registers a
  720. + function to kernel 'events' thread by schedule_work(). And the
  721. + function sets some special status to the cached aufs dentry and inode
  722. + private data. If they are not cached, then aufs has nothing to
  723. + do. When the same file is accessed through aufs (step 0-3) later,
  724. + aufs will detect the status and refresh all necessary data.
  725. + In this mode, aufs has to ignore the event which is fired by aufs
  726. + itself.
  727. +3. No Extra Validation
  728. + This is the simplest test and doesn't add any additional revalidation
  729. + test, and skip the revalidation in step 4. It is useful and improves
  730. + aufs performance when system surely hide the aufs branches from user,
  731. + by over-mounting something (or another method).
  732. --- /dev/null
  733. +++ linux-4.19/Documentation/filesystems/aufs/design/04branch.txt 2018-11-20 09:35:15.000195503 +0200
  734. @@ -0,0 +1,74 @@
  735. +
  736. +# Copyright (C) 2005-2018 Junjiro R. Okajima
  737. +#
  738. +# This program is free software; you can redistribute it and/or modify
  739. +# it under the terms of the GNU General Public License as published by
  740. +# the Free Software Foundation; either version 2 of the License, or
  741. +# (at your option) any later version.
  742. +#
  743. +# This program is distributed in the hope that it will be useful,
  744. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  745. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  746. +# GNU General Public License for more details.
  747. +#
  748. +# You should have received a copy of the GNU General Public License
  749. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  750. +
  751. +Branch Manipulation
  752. +
  753. +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
  754. +and changing its permission/attribute, there are a lot of works to do.
  755. +
  756. +
  757. +Add a Branch
  758. +----------------------------------------------------------------------
  759. +o Confirm the adding dir exists outside of aufs, including loopback
  760. + mount, and its various attributes.
  761. +o Initialize the xino file and whiteout bases if necessary.
  762. + See struct.txt.
  763. +
  764. +o Check the owner/group/mode of the directory
  765. + When the owner/group/mode of the adding directory differs from the
  766. + existing branch, aufs issues a warning because it may impose a
  767. + security risk.
  768. + For example, when a upper writable branch has a world writable empty
  769. + top directory, a malicious user can create any files on the writable
  770. + branch directly, like copy-up and modify manually. If something like
  771. + /etc/{passwd,shadow} exists on the lower readonly branch but the upper
  772. + writable branch, and the writable branch is world-writable, then a
  773. + malicious guy may create /etc/passwd on the writable branch directly
  774. + and the infected file will be valid in aufs.
  775. + I am afraid it can be a security issue, but aufs can do nothing except
  776. + producing a warning.
  777. +
  778. +
  779. +Delete a Branch
  780. +----------------------------------------------------------------------
  781. +o Confirm the deleting branch is not busy
  782. + To be general, there is one merit to adopt "remount" interface to
  783. + manipulate branches. It is to discard caches. At deleting a branch,
  784. + aufs checks the still cached (and connected) dentries and inodes. If
  785. + there are any, then they are all in-use. An inode without its
  786. + corresponding dentry can be alive alone (for example, inotify/fsnotify case).
  787. +
  788. + For the cached one, aufs checks whether the same named entry exists on
  789. + other branches.
  790. + If the cached one is a directory, because aufs provides a merged view
  791. + to users, as long as one dir is left on any branch aufs can show the
  792. + dir to users. In this case, the branch can be removed from aufs.
  793. + Otherwise aufs rejects deleting the branch.
  794. +
  795. + If any file on the deleting branch is opened by aufs, then aufs
  796. + rejects deleting.
  797. +
  798. +
  799. +Modify the Permission of a Branch
  800. +----------------------------------------------------------------------
  801. +o Re-initialize or remove the xino file and whiteout bases if necessary.
  802. + See struct.txt.
  803. +
  804. +o rw --> ro: Confirm the modifying branch is not busy
  805. + Aufs rejects the request if any of these conditions are true.
  806. + - a file on the branch is mmap-ed.
  807. + - a regular file on the branch is opened for write and there is no
  808. + same named entry on the upper branch.
  809. --- /dev/null
  810. +++ linux-4.19/Documentation/filesystems/aufs/design/05wbr_policy.txt 2018-11-20 09:35:15.000195503 +0200
  811. @@ -0,0 +1,64 @@
  812. +
  813. +# Copyright (C) 2005-2018 Junjiro R. Okajima
  814. +#
  815. +# This program is free software; you can redistribute it and/or modify
  816. +# it under the terms of the GNU General Public License as published by
  817. +# the Free Software Foundation; either version 2 of the License, or
  818. +# (at your option) any later version.
  819. +#
  820. +# This program is distributed in the hope that it will be useful,
  821. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  822. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  823. +# GNU General Public License for more details.
  824. +#
  825. +# You should have received a copy of the GNU General Public License
  826. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  827. +
  828. +Policies to Select One among Multiple Writable Branches
  829. +----------------------------------------------------------------------
  830. +When the number of writable branch is more than one, aufs has to decide
  831. +the target branch for file creation or copy-up. By default, the highest
  832. +writable branch which has the parent (or ancestor) dir of the target
  833. +file is chosen (top-down-parent policy).
  834. +By user's request, aufs implements some other policies to select the
  835. +writable branch, for file creation several policies, round-robin,
  836. +most-free-space, and other policies. For copy-up, top-down-parent,
  837. +bottom-up-parent, bottom-up and others.
  838. +
  839. +As expected, the round-robin policy selects the branch in circular. When
  840. +you have two writable branches and creates 10 new files, 5 files will be
  841. +created for each branch. mkdir(2) systemcall is an exception. When you
  842. +create 10 new directories, all will be created on the same branch.
  843. +And the most-free-space policy selects the one which has most free
  844. +space among the writable branches. The amount of free space will be
  845. +checked by aufs internally, and users can specify its time interval.
  846. +
  847. +The policies for copy-up is more simple,
  848. +top-down-parent is equivalent to the same named on in create policy,
  849. +bottom-up-parent selects the writable branch where the parent dir
  850. +exists and the nearest upper one from the copyup-source,
  851. +bottom-up selects the nearest upper writable branch from the
  852. +copyup-source, regardless the existence of the parent dir.
  853. +
  854. +There are some rules or exceptions to apply these policies.
  855. +- If there is a readonly branch above the policy-selected branch and
  856. + the parent dir is marked as opaque (a variation of whiteout), or the
  857. + target (creating) file is whiteout-ed on the upper readonly branch,
  858. + then the result of the policy is ignored and the target file will be
  859. + created on the nearest upper writable branch than the readonly branch.
  860. +- If there is a writable branch above the policy-selected branch and
  861. + the parent dir is marked as opaque or the target file is whiteouted
  862. + on the branch, then the result of the policy is ignored and the target
  863. + file will be created on the highest one among the upper writable
  864. + branches who has diropq or whiteout. In case of whiteout, aufs removes
  865. + it as usual.
  866. +- link(2) and rename(2) systemcalls are exceptions in every policy.
  867. + They try selecting the branch where the source exists as possible
  868. + since copyup a large file will take long time. If it can't be,
  869. + ie. the branch where the source exists is readonly, then they will
  870. + follow the copyup policy.
  871. +- There is an exception for rename(2) when the target exists.
  872. + If the rename target exists, aufs compares the index of the branches
  873. + where the source and the target exists and selects the higher
  874. + one. If the selected branch is readonly, then aufs follows the
  875. + copyup policy.
  876. --- /dev/null
  877. +++ linux-4.19/Documentation/filesystems/aufs/design/06dirren.dot 2018-11-20 09:35:15.000195503 +0200
  878. @@ -0,0 +1,31 @@
  879. +
  880. +// to view this graph, run dot(1) command in GRAPHVIZ.
  881. +
  882. +digraph G {
  883. +node [shape=box];
  884. +whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
  885. +
  886. +node [shape=oval];
  887. +
  888. +aufs_rename -> whinfo [label="store/remove"];
  889. +
  890. +node [shape=oval];
  891. +inode_list [label="h_inum list in branch\ncache"];
  892. +
  893. +node [shape=box];
  894. +whinode [label="h_inum list file"];
  895. +
  896. +node [shape=oval];
  897. +brmgmt [label="br_add/del/mod/umount"];
  898. +
  899. +brmgmt -> inode_list [label="create/remove"];
  900. +brmgmt -> whinode [label="load/store"];
  901. +
  902. +inode_list -> whinode [style=dashed,dir=both];
  903. +
  904. +aufs_rename -> inode_list [label="add/del"];
  905. +
  906. +aufs_lookup -> inode_list [label="search"];
  907. +
  908. +aufs_lookup -> whinfo [label="load/remove"];
  909. +}
  910. --- /dev/null
  911. +++ linux-4.19/Documentation/filesystems/aufs/design/06dirren.txt 2018-11-20 09:35:15.001195503 +0200
  912. @@ -0,0 +1,102 @@
  913. +
  914. +# Copyright (C) 2017-2018 Junjiro R. Okajima
  915. +#
  916. +# This program is free software; you can redistribute it and/or modify
  917. +# it under the terms of the GNU General Public License as published by
  918. +# the Free Software Foundation; either version 2 of the License, or
  919. +# (at your option) any later version.
  920. +#
  921. +# This program is distributed in the hope that it will be useful,
  922. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  923. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  924. +# GNU General Public License for more details.
  925. +#
  926. +# You should have received a copy of the GNU General Public License
  927. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  928. +
  929. +Special handling for renaming a directory (DIRREN)
  930. +----------------------------------------------------------------------
  931. +First, let's assume we have a simple usecase.
  932. +
  933. +- /u = /rw + /ro
  934. +- /rw/dirA exists
  935. +- /ro/dirA and /ro/dirA/file exist too
  936. +- there is no dirB on both branches
  937. +- a user issues rename("dirA", "dirB")
  938. +
  939. +Now, what should aufs behave against this rename(2)?
  940. +There are a few possible cases.
  941. +
  942. +A. returns EROFS.
  943. + since dirA exists on a readonly branch which cannot be renamed.
  944. +B. returns EXDEV.
  945. + it is possible to copy-up dirA (only the dir itself), but the child
  946. + entries ("file" in this case) should not be. it must be a bad
  947. + approach to copy-up recursively.
  948. +C. returns a success.
  949. + even the branch /ro is readonly, aufs tries renaming it. Obviously it
  950. + is a violation of aufs' policy.
  951. +D. construct an extra information which indicates that /ro/dirA should
  952. + be handled as the name of dirB.
  953. + overlayfs has a similar feature called REDIRECT.
  954. +
  955. +Until now, aufs implements the case B only which returns EXDEV, and
  956. +expects the userspace application behaves like mv(1) which tries
  957. +issueing rename(2) recursively.
  958. +
  959. +A new aufs feature called DIRREN is introduced which implements the case
  960. +D. There are several "extra information" added.
  961. +
  962. +1. detailed info per renamed directory
  963. + path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
  964. +2. the inode-number list of directories on a branch
  965. + path: /rw/dirB/$AUFS_WH_DR_BRHINO
  966. +
  967. +The filename of "detailed info per directory" represents the lower
  968. +branch, and its format is
  969. +- a type of the branch id
  970. + one of these.
  971. + + uuid (not implemented yet)
  972. + + fsid
  973. + + dev
  974. +- the inode-number of the branch root dir
  975. +
  976. +And it contains these info in a single regular file.
  977. +- magic number
  978. +- branch's inode-number of the logically renamed dir
  979. +- the name of the before-renamed dir
  980. +
  981. +The "detailed info per directory" file is created in aufs rename(2), and
  982. +loaded in any lookup.
  983. +The info is considered in lookup for the matching case only. Here
  984. +"matching" means that the root of branch (in the info filename) is same
  985. +to the current looking-up branch. After looking-up the before-renamed
  986. +name, the inode-number is compared. And the matched dentry is used.
  987. +
  988. +The "inode-number list of directories" is a regular file which contains
  989. +simply the inode-numbers on the branch. The file is created or updated
  990. +in removing the branch, and loaded in adding the branch. Its lifetime is
  991. +equal to the branch.
  992. +The list is refered in lookup, and when the current target inode is
  993. +found in the list, the aufs tries loading the "detailed info per
  994. +directory" and get the changed and valid name of the dir.
  995. +
  996. +Theoretically these "extra informaiton" may be able to be put into XATTR
  997. +in the dir inode. But aufs doesn't choose this way because
  998. +1. XATTR may not be supported by the branch (or its configuration)
  999. +2. XATTR may have its size limit.
  1000. +3. XATTR may be less easy to convert than a regular file, when the
  1001. + format of the info is changed in the future.
  1002. +At the same time, I agree that the regular file approach is much slower
  1003. +than XATTR approach. So, in the future, aufs may take the XATTR or other
  1004. +better approach.
  1005. +
  1006. +This DIRREN feature is enabled by aufs configuration, and is activated
  1007. +by a new mount option.
  1008. +
  1009. +For the more complicated case, there is a work with UDBA option, which
  1010. +is to dected the direct access to the branches (by-passing aufs) and to
  1011. +maintain the cashes in aufs. Since a single cached aufs dentry may
  1012. +contains two names, before- and after-rename, the name comparision in
  1013. +UDBA handler may not work correctly. In this case, the behaviour will be
  1014. +equivalen to udba=reval case.
  1015. --- /dev/null
  1016. +++ linux-4.19/Documentation/filesystems/aufs/design/06fhsm.txt 2018-11-20 09:35:15.001195503 +0200
  1017. @@ -0,0 +1,120 @@
  1018. +
  1019. +# Copyright (C) 2011-2018 Junjiro R. Okajima
  1020. +#
  1021. +# This program is free software; you can redistribute it and/or modify
  1022. +# it under the terms of the GNU General Public License as published by
  1023. +# the Free Software Foundation; either version 2 of the License, or
  1024. +# (at your option) any later version.
  1025. +#
  1026. +# This program is distributed in the hope that it will be useful,
  1027. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1028. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1029. +# GNU General Public License for more details.
  1030. +#
  1031. +# You should have received a copy of the GNU General Public License
  1032. +# along with this program; if not, write to the Free Software
  1033. +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  1034. +
  1035. +
  1036. +File-based Hierarchical Storage Management (FHSM)
  1037. +----------------------------------------------------------------------
  1038. +Hierarchical Storage Management (or HSM) is a well-known feature in the
  1039. +storage world. Aufs provides this feature as file-based with multiple
  1040. +writable branches, based upon the principle of "Colder, the Lower".
  1041. +Here the word "colder" means that the less used files, and "lower" means
  1042. +that the position in the order of the stacked branches vertically.
  1043. +These multiple writable branches are prioritized, ie. the topmost one
  1044. +should be the fastest drive and be used heavily.
  1045. +
  1046. +o Characters in aufs FHSM story
  1047. +- aufs itself and a new branch attribute.
  1048. +- a new ioctl interface to move-down and to establish a connection with
  1049. + the daemon ("move-down" is a converse of "copy-up").
  1050. +- userspace tool and daemon.
  1051. +
  1052. +The userspace daemon establishes a connection with aufs and waits for
  1053. +the notification. The notified information is very similar to struct
  1054. +statfs containing the number of consumed blocks and inodes.
  1055. +When the consumed blocks/inodes of a branch exceeds the user-specified
  1056. +upper watermark, the daemon activates its move-down process until the
  1057. +consumed blocks/inodes reaches the user-specified lower watermark.
  1058. +
  1059. +The actual move-down is done by aufs based upon the request from
  1060. +user-space since we need to maintain the inode number and the internal
  1061. +pointer arrays in aufs.
  1062. +
  1063. +Currently aufs FHSM handles the regular files only. Additionally they
  1064. +must not be hard-linked nor pseudo-linked.
  1065. +
  1066. +
  1067. +o Cowork of aufs and the user-space daemon
  1068. + During the userspace daemon established the connection, aufs sends a
  1069. + small notification to it whenever aufs writes something into the
  1070. + writable branch. But it may cost high since aufs issues statfs(2)
  1071. + internally. So user can specify a new option to cache the
  1072. + info. Actually the notification is controlled by these factors.
  1073. + + the specified cache time.
  1074. + + classified as "force" by aufs internally.
  1075. + Until the specified time expires, aufs doesn't send the info
  1076. + except the forced cases. When aufs decide forcing, the info is always
  1077. + notified to userspace.
  1078. + For example, the number of free inodes is generally large enough and
  1079. + the shortage of it happens rarely. So aufs doesn't force the
  1080. + notification when creating a new file, directory and others. This is
  1081. + the typical case which aufs doesn't force.
  1082. + When aufs writes the actual filedata and the files consumes any of new
  1083. + blocks, the aufs forces notifying.
  1084. +
  1085. +
  1086. +o Interfaces in aufs
  1087. +- New branch attribute.
  1088. + + fhsm
  1089. + Specifies that the branch is managed by FHSM feature. In other word,
  1090. + participant in the FHSM.
  1091. + When nofhsm is set to the branch, it will not be the source/target
  1092. + branch of the move-down operation. This attribute is set
  1093. + independently from coo and moo attributes, and if you want full
  1094. + FHSM, you should specify them as well.
  1095. +- New mount option.
  1096. + + fhsm_sec
  1097. + Specifies a second to suppress many less important info to be
  1098. + notified.
  1099. +- New ioctl.
  1100. + + AUFS_CTL_FHSM_FD
  1101. + create a new file descriptor which userspace can read the notification
  1102. + (a subset of struct statfs) from aufs.
  1103. +- Module parameter 'brs'
  1104. + It has to be set to 1. Otherwise the new mount option 'fhsm' will not
  1105. + be set.
  1106. +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
  1107. + When there are two or more branches with fhsm attributes,
  1108. + /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
  1109. + terminates it. As a result of remounting and branch-manipulation, the
  1110. + number of branches with fhsm attribute can be one. In this case,
  1111. + /sbin/mount.aufs will terminate the user-space daemon.
  1112. +
  1113. +
  1114. +Finally the operation is done as these steps in kernel-space.
  1115. +- make sure that,
  1116. + + no one else is using the file.
  1117. + + the file is not hard-linked.
  1118. + + the file is not pseudo-linked.
  1119. + + the file is a regular file.
  1120. + + the parent dir is not opaqued.
  1121. +- find the target writable branch.
  1122. +- make sure the file is not whiteout-ed by the upper (than the target)
  1123. + branch.
  1124. +- make the parent dir on the target branch.
  1125. +- mutex lock the inode on the branch.
  1126. +- unlink the whiteout on the target branch (if exists).
  1127. +- lookup and create the whiteout-ed temporary name on the target branch.
  1128. +- copy the file as the whiteout-ed temporary name on the target branch.
  1129. +- rename the whiteout-ed temporary name to the original name.
  1130. +- unlink the file on the source branch.
  1131. +- maintain the internal pointer array and the external inode number
  1132. + table (XINO).
  1133. +- maintain the timestamps and other attributes of the parent dir and the
  1134. + file.
  1135. +
  1136. +And of course, in every step, an error may happen. So the operation
  1137. +should restore the original file state after an error happens.
  1138. --- /dev/null
  1139. +++ linux-4.19/Documentation/filesystems/aufs/design/06mmap.txt 2018-11-20 09:35:15.002195503 +0200
  1140. @@ -0,0 +1,72 @@
  1141. +
  1142. +# Copyright (C) 2005-2018 Junjiro R. Okajima
  1143. +#
  1144. +# This program is free software; you can redistribute it and/or modify
  1145. +# it under the terms of the GNU General Public License as published by
  1146. +# the Free Software Foundation; either version 2 of the License, or
  1147. +# (at your option) any later version.
  1148. +#
  1149. +# This program is distributed in the hope that it will be useful,
  1150. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1151. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1152. +# GNU General Public License for more details.
  1153. +#
  1154. +# You should have received a copy of the GNU General Public License
  1155. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  1156. +
  1157. +mmap(2) -- File Memory Mapping
  1158. +----------------------------------------------------------------------
  1159. +In aufs, the file-mapped pages are handled by a branch fs directly, no
  1160. +interaction with aufs. It means aufs_mmap() calls the branch fs's
  1161. +->mmap().
  1162. +This approach is simple and good, but there is one problem.
  1163. +Under /proc, several entries show the mmapped files by its path (with
  1164. +device and inode number), and the printed path will be the path on the
  1165. +branch fs's instead of virtual aufs's.
  1166. +This is not a problem in most cases, but some utilities lsof(1) (and its
  1167. +user) may expect the path on aufs.
  1168. +
  1169. +To address this issue, aufs adds a new member called vm_prfile in struct
  1170. +vm_area_struct (and struct vm_region). The original vm_file points to
  1171. +the file on the branch fs in order to handle everything correctly as
  1172. +usual. The new vm_prfile points to a virtual file in aufs, and the
  1173. +show-functions in procfs refers to vm_prfile if it is set.
  1174. +Also we need to maintain several other places where touching vm_file
  1175. +such like
  1176. +- fork()/clone() copies vma and the reference count of vm_file is
  1177. + incremented.
  1178. +- merging vma maintains the ref count too.
  1179. +
  1180. +This is not a good approach. It just fakes the printed path. But it
  1181. +leaves all behaviour around f_mapping unchanged. This is surely an
  1182. +advantage.
  1183. +Actually aufs had adopted another complicated approach which calls
  1184. +generic_file_mmap() and handles struct vm_operations_struct. In this
  1185. +approach, aufs met a hard problem and I could not solve it without
  1186. +switching the approach.
  1187. +
  1188. +There may be one more another approach which is
  1189. +- bind-mount the branch-root onto the aufs-root internally
  1190. +- grab the new vfsmount (ie. struct mount)
  1191. +- lazy-umount the branch-root internally
  1192. +- in open(2) the aufs-file, open the branch-file with the hidden
  1193. + vfsmount (instead of the original branch's vfsmount)
  1194. +- ideally this "bind-mount and lazy-umount" should be done atomically,
  1195. + but it may be possible from userspace by the mount helper.
  1196. +
  1197. +Adding the internal hidden vfsmount and using it in opening a file, the
  1198. +file path under /proc will be printed correctly. This approach looks
  1199. +smarter, but is not possible I am afraid.
  1200. +- aufs-root may be bind-mount later. when it happens, another hidden
  1201. + vfsmount will be required.
  1202. +- it is hard to get the chance to bind-mount and lazy-umount
  1203. + + in kernel-space, FS can have vfsmount in open(2) via
  1204. + file->f_path, and aufs can know its vfsmount. But several locks are
  1205. + already acquired, and if aufs tries to bind-mount and lazy-umount
  1206. + here, then it may cause a deadlock.
  1207. + + in user-space, bind-mount doesn't invoke the mount helper.
  1208. +- since /proc shows dev and ino, aufs has to give vma these info. it
  1209. + means a new member vm_prinode will be necessary. this is essentially
  1210. + equivalent to vm_prfile described above.
  1211. +
  1212. +I have to give up this "looks-smater" approach.
  1213. --- /dev/null
  1214. +++ linux-4.19/Documentation/filesystems/aufs/design/06xattr.txt 2018-11-20 09:35:15.002195503 +0200
  1215. @@ -0,0 +1,96 @@
  1216. +
  1217. +# Copyright (C) 2014-2018 Junjiro R. Okajima
  1218. +#
  1219. +# This program is free software; you can redistribute it and/or modify
  1220. +# it under the terms of the GNU General Public License as published by
  1221. +# the Free Software Foundation; either version 2 of the License, or
  1222. +# (at your option) any later version.
  1223. +#
  1224. +# This program is distributed in the hope that it will be useful,
  1225. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1226. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1227. +# GNU General Public License for more details.
  1228. +#
  1229. +# You should have received a copy of the GNU General Public License
  1230. +# along with this program; if not, write to the Free Software
  1231. +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  1232. +
  1233. +
  1234. +Listing XATTR/EA and getting the value
  1235. +----------------------------------------------------------------------
  1236. +For the inode standard attributes (owner, group, timestamps, etc.), aufs
  1237. +shows the values from the topmost existing file. This behaviour is good
  1238. +for the non-dir entries since the bahaviour exactly matches the shown
  1239. +information. But for the directories, aufs considers all the same named
  1240. +entries on the lower branches. Which means, if one of the lower entry
  1241. +rejects readdir call, then aufs returns an error even if the topmost
  1242. +entry allows it. This behaviour is necessary to respect the branch fs's
  1243. +security, but can make users confused since the user-visible standard
  1244. +attributes don't match the behaviour.
  1245. +To address this issue, aufs has a mount option called dirperm1 which
  1246. +checks the permission for the topmost entry only, and ignores the lower
  1247. +entry's permission.
  1248. +
  1249. +A similar issue can happen around XATTR.
  1250. +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
  1251. +always set. Otherwise these very unpleasant situation would happen.
  1252. +- listxattr(2) may return the duplicated entries.
  1253. +- users may not be able to remove or reset the XATTR forever,
  1254. +
  1255. +
  1256. +XATTR/EA support in the internal (copy,move)-(up,down)
  1257. +----------------------------------------------------------------------
  1258. +Generally the extended attributes of inode are categorized as these.
  1259. +- "security" for LSM and capability.
  1260. +- "system" for posix ACL, 'acl' mount option is required for the branch
  1261. + fs generally.
  1262. +- "trusted" for userspace, CAP_SYS_ADMIN is required.
  1263. +- "user" for userspace, 'user_xattr' mount option is required for the
  1264. + branch fs generally.
  1265. +
  1266. +Moreover there are some other categories. Aufs handles these rather
  1267. +unpopular categories as the ordinary ones, ie. there is no special
  1268. +condition nor exception.
  1269. +
  1270. +In copy-up, the support for XATTR on the dst branch may differ from the
  1271. +src branch. In this case, the copy-up operation will get an error and
  1272. +the original user operation which triggered the copy-up will fail. It
  1273. +can happen that even all copy-up will fail.
  1274. +When both of src and dst branches support XATTR and if an error occurs
  1275. +during copying XATTR, then the copy-up should fail obviously. That is a
  1276. +good reason and aufs should return an error to userspace. But when only
  1277. +the src branch support that XATTR, aufs should not return an error.
  1278. +For example, the src branch supports ACL but the dst branch doesn't
  1279. +because the dst branch may natively un-support it or temporary
  1280. +un-support it due to "noacl" mount option. Of course, the dst branch fs
  1281. +may NOT return an error even if the XATTR is not supported. It is
  1282. +totally up to the branch fs.
  1283. +
  1284. +Anyway when the aufs internal copy-up gets an error from the dst branch
  1285. +fs, then aufs tries removing the just copied entry and returns the error
  1286. +to the userspace. The worst case of this situation will be all copy-up
  1287. +will fail.
  1288. +
  1289. +For the copy-up operation, there two basic approaches.
  1290. +- copy the specified XATTR only (by category above), and return the
  1291. + error unconditionally if it happens.
  1292. +- copy all XATTR, and ignore the error on the specified category only.
  1293. +
  1294. +In order to support XATTR and to implement the correct behaviour, aufs
  1295. +chooses the latter approach and introduces some new branch attributes,
  1296. +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
  1297. +They correspond to the XATTR namespaces (see above). Additionally, to be
  1298. +convenient, "icex" is also provided which means all "icex*" attributes
  1299. +are set (here the word "icex" stands for "ignore copy-error on XATTR").
  1300. +
  1301. +The meaning of these attributes is to ignore the error from setting
  1302. +XATTR on that branch.
  1303. +Note that aufs tries copying all XATTR unconditionally, and ignores the
  1304. +error from the dst branch according to the specified attributes.
  1305. +
  1306. +Some XATTR may have its default value. The default value may come from
  1307. +the parent dir or the environment. If the default value is set at the
  1308. +file creating-time, it will be overwritten by copy-up.
  1309. +Some contradiction may happen I am afraid.
  1310. +Do we need another attribute to stop copying XATTR? I am unsure. For
  1311. +now, aufs implements the branch attributes to ignore the error.
  1312. --- /dev/null
  1313. +++ linux-4.19/Documentation/filesystems/aufs/design/07export.txt 2018-11-20 09:35:15.002195503 +0200
  1314. @@ -0,0 +1,58 @@
  1315. +
  1316. +# Copyright (C) 2005-2018 Junjiro R. Okajima
  1317. +#
  1318. +# This program is free software; you can redistribute it and/or modify
  1319. +# it under the terms of the GNU General Public License as published by
  1320. +# the Free Software Foundation; either version 2 of the License, or
  1321. +# (at your option) any later version.
  1322. +#
  1323. +# This program is distributed in the hope that it will be useful,
  1324. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1325. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1326. +# GNU General Public License for more details.
  1327. +#
  1328. +# You should have received a copy of the GNU General Public License
  1329. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  1330. +
  1331. +Export Aufs via NFS
  1332. +----------------------------------------------------------------------
  1333. +Here is an approach.
  1334. +- like xino/xib, add a new file 'xigen' which stores aufs inode
  1335. + generation.
  1336. +- iget_locked(): initialize aufs inode generation for a new inode, and
  1337. + store it in xigen file.
  1338. +- destroy_inode(): increment aufs inode generation and store it in xigen
  1339. + file. it is necessary even if it is not unlinked, because any data of
  1340. + inode may be changed by UDBA.
  1341. +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
  1342. + build file handle by
  1343. + + branch id (4 bytes)
  1344. + + superblock generation (4 bytes)
  1345. + + inode number (4 or 8 bytes)
  1346. + + parent dir inode number (4 or 8 bytes)
  1347. + + inode generation (4 bytes))
  1348. + + return value of exportfs_encode_fh() for the parent on a branch (4
  1349. + bytes)
  1350. + + file handle for a branch (by exportfs_encode_fh())
  1351. +- fh_to_dentry():
  1352. + + find the index of a branch from its id in handle, and check it is
  1353. + still exist in aufs.
  1354. + + 1st level: get the inode number from handle and search it in cache.
  1355. + + 2nd level: if not found in cache, get the parent inode number from
  1356. + the handle and search it in cache. and then open the found parent
  1357. + dir, find the matching inode number by vfs_readdir() and get its
  1358. + name, and call lookup_one_len() for the target dentry.
  1359. + + 3rd level: if the parent dir is not cached, call
  1360. + exportfs_decode_fh() for a branch and get the parent on a branch,
  1361. + build a pathname of it, convert it a pathname in aufs, call
  1362. + path_lookup(). now aufs gets a parent dir dentry, then handle it as
  1363. + the 2nd level.
  1364. + + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
  1365. + for every branch, but not itself. to get this, (currently) aufs
  1366. + searches in current->nsproxy->mnt_ns list. it may not be a good
  1367. + idea, but I didn't get other approach.
  1368. + + test the generation of the gotten inode.
  1369. +- every inode operation: they may get EBUSY due to UDBA. in this case,
  1370. + convert it into ESTALE for NFSD.
  1371. +- readdir(): call lockdep_on/off() because filldir in NFSD calls
  1372. + lookup_one_len(), vfs_getattr(), encode_fh() and others.
  1373. --- /dev/null
  1374. +++ linux-4.19/Documentation/filesystems/aufs/design/08shwh.txt 2018-11-20 09:35:15.003195503 +0200
  1375. @@ -0,0 +1,52 @@
  1376. +
  1377. +# Copyright (C) 2005-2018 Junjiro R. Okajima
  1378. +#
  1379. +# This program is free software; you can redistribute it and/or modify
  1380. +# it under the terms of the GNU General Public License as published by
  1381. +# the Free Software Foundation; either version 2 of the License, or
  1382. +# (at your option) any later version.
  1383. +#
  1384. +# This program is distributed in the hope that it will be useful,
  1385. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1386. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1387. +# GNU General Public License for more details.
  1388. +#
  1389. +# You should have received a copy of the GNU General Public License
  1390. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  1391. +
  1392. +Show Whiteout Mode (shwh)
  1393. +----------------------------------------------------------------------
  1394. +Generally aufs hides the name of whiteouts. But in some cases, to show
  1395. +them is very useful for users. For instance, creating a new middle layer
  1396. +(branch) by merging existing layers.
  1397. +
  1398. +(borrowing aufs1 HOW-TO from a user, Michael Towers)
  1399. +When you have three branches,
  1400. +- Bottom: 'system', squashfs (underlying base system), read-only
  1401. +- Middle: 'mods', squashfs, read-only
  1402. +- Top: 'overlay', ram (tmpfs), read-write
  1403. +
  1404. +The top layer is loaded at boot time and saved at shutdown, to preserve
  1405. +the changes made to the system during the session.
  1406. +When larger changes have been made, or smaller changes have accumulated,
  1407. +the size of the saved top layer data grows. At this point, it would be
  1408. +nice to be able to merge the two overlay branches ('mods' and 'overlay')
  1409. +and rewrite the 'mods' squashfs, clearing the top layer and thus
  1410. +restoring save and load speed.
  1411. +
  1412. +This merging is simplified by the use of another aufs mount, of just the
  1413. +two overlay branches using the 'shwh' option.
  1414. +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
  1415. + aufs /livesys/merge_union
  1416. +
  1417. +A merged view of these two branches is then available at
  1418. +/livesys/merge_union, and the new feature is that the whiteouts are
  1419. +visible!
  1420. +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
  1421. +writing to all branches. Also the default mode for all branches is 'ro'.
  1422. +It is now possible to save the combined contents of the two overlay
  1423. +branches to a new squashfs, e.g.:
  1424. +# mksquashfs /livesys/merge_union /path/to/newmods.squash
  1425. +
  1426. +This new squashfs archive can be stored on the boot device and the
  1427. +initramfs will use it to replace the old one at the next boot.
  1428. --- /dev/null
  1429. +++ linux-4.19/Documentation/filesystems/aufs/design/10dynop.txt 2018-11-20 09:35:15.003195503 +0200
  1430. @@ -0,0 +1,47 @@
  1431. +
  1432. +# Copyright (C) 2010-2018 Junjiro R. Okajima
  1433. +#
  1434. +# This program is free software; you can redistribute it and/or modify
  1435. +# it under the terms of the GNU General Public License as published by
  1436. +# the Free Software Foundation; either version 2 of the License, or
  1437. +# (at your option) any later version.
  1438. +#
  1439. +# This program is distributed in the hope that it will be useful,
  1440. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1441. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1442. +# GNU General Public License for more details.
  1443. +#
  1444. +# You should have received a copy of the GNU General Public License
  1445. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  1446. +
  1447. +Dynamically customizable FS operations
  1448. +----------------------------------------------------------------------
  1449. +Generally FS operations (struct inode_operations, struct
  1450. +address_space_operations, struct file_operations, etc.) are defined as
  1451. +"static const", but it never means that FS have only one set of
  1452. +operation. Some FS have multiple sets of them. For instance, ext2 has
  1453. +three sets, one for XIP, for NOBH, and for normal.
  1454. +Since aufs overrides and redirects these operations, sometimes aufs has
  1455. +to change its behaviour according to the branch FS type. More importantly
  1456. +VFS acts differently if a function (member in the struct) is set or
  1457. +not. It means aufs should have several sets of operations and select one
  1458. +among them according to the branch FS definition.
  1459. +
  1460. +In order to solve this problem and not to affect the behaviour of VFS,
  1461. +aufs defines these operations dynamically. For instance, aufs defines
  1462. +dummy direct_IO function for struct address_space_operations, but it may
  1463. +not be set to the address_space_operations actually. When the branch FS
  1464. +doesn't have it, aufs doesn't set it to its address_space_operations
  1465. +while the function definition itself is still alive. So the behaviour
  1466. +itself will not change, and it will return an error when direct_IO is
  1467. +not set.
  1468. +
  1469. +The lifetime of these dynamically generated operation object is
  1470. +maintained by aufs branch object. When the branch is removed from aufs,
  1471. +the reference counter of the object is decremented. When it reaches
  1472. +zero, the dynamically generated operation object will be freed.
  1473. +
  1474. +This approach is designed to support AIO (io_submit), Direct I/O and
  1475. +XIP (DAX) mainly.
  1476. +Currently this approach is applied to address_space_operations for
  1477. +regular files only.
  1478. --- /dev/null
  1479. +++ linux-4.19/Documentation/filesystems/aufs/README 2018-11-20 09:35:14.997195503 +0200
  1480. @@ -0,0 +1,395 @@
  1481. +
  1482. +Aufs4 -- advanced multi layered unification filesystem version 4.x
  1483. +http://aufs.sf.net
  1484. +Junjiro R. Okajima
  1485. +
  1486. +
  1487. +0. Introduction
  1488. +----------------------------------------
  1489. +In the early days, aufs was entirely re-designed and re-implemented
  1490. +Unionfs Version 1.x series. Adding many original ideas, approaches,
  1491. +improvements and implementations, it becomes totally different from
  1492. +Unionfs while keeping the basic features.
  1493. +Recently, Unionfs Version 2.x series begin taking some of the same
  1494. +approaches to aufs1's.
  1495. +Unionfs is being developed by Professor Erez Zadok at Stony Brook
  1496. +University and his team.
  1497. +
  1498. +Aufs4 supports linux-4.0 and later, and for linux-3.x series try aufs3.
  1499. +If you want older kernel version support, try aufs2-2.6.git or
  1500. +aufs2-standalone.git repository, aufs1 from CVS on SourceForge.
  1501. +
  1502. +Note: it becomes clear that "Aufs was rejected. Let's give it up."
  1503. + According to Christoph Hellwig, linux rejects all union-type
  1504. + filesystems but UnionMount.
  1505. +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
  1506. +
  1507. +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
  1508. + UnionMount, and he pointed out an issue around a directory mutex
  1509. + lock and aufs addressed it. But it is still unsure whether aufs will
  1510. + be merged (or any other union solution).
  1511. +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
  1512. +
  1513. +
  1514. +1. Features
  1515. +----------------------------------------
  1516. +- unite several directories into a single virtual filesystem. The member
  1517. + directory is called as a branch.
  1518. +- you can specify the permission flags to the branch, which are 'readonly',
  1519. + 'readwrite' and 'whiteout-able.'
  1520. +- by upper writable branch, internal copyup and whiteout, files/dirs on
  1521. + readonly branch are modifiable logically.
  1522. +- dynamic branch manipulation, add, del.
  1523. +- etc...
  1524. +
  1525. +Also there are many enhancements in aufs, such as:
  1526. +- test only the highest one for the directory permission (dirperm1)
  1527. +- copyup on open (coo=)
  1528. +- 'move' policy for copy-up between two writable branches, after
  1529. + checking free space.
  1530. +- xattr, acl
  1531. +- readdir(3) in userspace.
  1532. +- keep inode number by external inode number table
  1533. +- keep the timestamps of file/dir in internal copyup operation
  1534. +- seekable directory, supporting NFS readdir.
  1535. +- whiteout is hardlinked in order to reduce the consumption of inodes
  1536. + on branch
  1537. +- do not copyup, nor create a whiteout when it is unnecessary
  1538. +- revert a single systemcall when an error occurs in aufs
  1539. +- remount interface instead of ioctl
  1540. +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
  1541. +- loopback mounted filesystem as a branch
  1542. +- kernel thread for removing the dir who has a plenty of whiteouts
  1543. +- support copyup sparse file (a file which has a 'hole' in it)
  1544. +- default permission flags for branches
  1545. +- selectable permission flags for ro branch, whether whiteout can
  1546. + exist or not
  1547. +- export via NFS.
  1548. +- support <sysfs>/fs/aufs and <debugfs>/aufs.
  1549. +- support multiple writable branches, some policies to select one
  1550. + among multiple writable branches.
  1551. +- a new semantics for link(2) and rename(2) to support multiple
  1552. + writable branches.
  1553. +- no glibc changes are required.
  1554. +- pseudo hardlink (hardlink over branches)
  1555. +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
  1556. + including NFS or remote filesystem branch.
  1557. +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
  1558. +- and more...
  1559. +
  1560. +Currently these features are dropped temporary from aufs4.
  1561. +See design/08plan.txt in detail.
  1562. +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
  1563. + (robr)
  1564. +- statistics of aufs thread (/sys/fs/aufs/stat)
  1565. +
  1566. +Features or just an idea in the future (see also design/*.txt),
  1567. +- reorder the branch index without del/re-add.
  1568. +- permanent xino files for NFSD
  1569. +- an option for refreshing the opened files after add/del branches
  1570. +- light version, without branch manipulation. (unnecessary?)
  1571. +- copyup in userspace
  1572. +- inotify in userspace
  1573. +- readv/writev
  1574. +
  1575. +
  1576. +2. Download
  1577. +----------------------------------------
  1578. +There are three GIT trees for aufs4, aufs4-linux.git,
  1579. +aufs4-standalone.git, and aufs-util.git. Note that there is no "4" in
  1580. +"aufs-util.git."
  1581. +While the aufs-util is always necessary, you need either of aufs4-linux
  1582. +or aufs4-standalone.
  1583. +
  1584. +The aufs4-linux tree includes the whole linux mainline GIT tree,
  1585. +git://git.kernel.org/.../torvalds/linux.git.
  1586. +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
  1587. +build aufs4 as an external kernel module.
  1588. +Several extra patches are not included in this tree. Only
  1589. +aufs4-standalone tree contains them. They are described in the later
  1590. +section "Configuration and Compilation."
  1591. +
  1592. +On the other hand, the aufs4-standalone tree has only aufs source files
  1593. +and necessary patches, and you can select CONFIG_AUFS_FS=m.
  1594. +But you need to apply all aufs patches manually.
  1595. +
  1596. +You will find GIT branches whose name is in form of "aufs4.x" where "x"
  1597. +represents the linux kernel version, "linux-4.x". For instance,
  1598. +"aufs4.0" is for linux-4.0. For latest "linux-4.x-rcN", use
  1599. +"aufs4.x-rcN" branch.
  1600. +
  1601. +o aufs4-linux tree
  1602. +$ git clone --reference /your/linux/git/tree \
  1603. + git://github.com/sfjro/aufs4-linux.git aufs4-linux.git
  1604. +- if you don't have linux GIT tree, then remove "--reference ..."
  1605. +$ cd aufs4-linux.git
  1606. +$ git checkout origin/aufs4.0
  1607. +
  1608. +Or You may want to directly git-pull aufs into your linux GIT tree, and
  1609. +leave the patch-work to GIT.
  1610. +$ cd /your/linux/git/tree
  1611. +$ git remote add aufs4 git://github.com/sfjro/aufs4-linux.git
  1612. +$ git fetch aufs4
  1613. +$ git checkout -b my4.0 v4.0
  1614. +$ (add your local change...)
  1615. +$ git pull aufs4 aufs4.0
  1616. +- now you have v4.0 + your_changes + aufs4.0 in you my4.0 branch.
  1617. +- you may need to solve some conflicts between your_changes and
  1618. + aufs4.0. in this case, git-rerere is recommended so that you can
  1619. + solve the similar conflicts automatically when you upgrade to 4.1 or
  1620. + later in the future.
  1621. +
  1622. +o aufs4-standalone tree
  1623. +$ git clone git://github.com/sfjro/aufs4-standalone.git aufs4-standalone.git
  1624. +$ cd aufs4-standalone.git
  1625. +$ git checkout origin/aufs4.0
  1626. +
  1627. +o aufs-util tree
  1628. +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
  1629. +- note that the public aufs-util.git is on SourceForge instead of
  1630. + GitHUB.
  1631. +$ cd aufs-util.git
  1632. +$ git checkout origin/aufs4.0
  1633. +
  1634. +Note: The 4.x-rcN branch is to be used with `rc' kernel versions ONLY.
  1635. +The minor version number, 'x' in '4.x', of aufs may not always
  1636. +follow the minor version number of the kernel.
  1637. +Because changes in the kernel that cause the use of a new
  1638. +minor version number do not always require changes to aufs-util.
  1639. +
  1640. +Since aufs-util has its own minor version number, you may not be
  1641. +able to find a GIT branch in aufs-util for your kernel's
  1642. +exact minor version number.
  1643. +In this case, you should git-checkout the branch for the
  1644. +nearest lower number.
  1645. +
  1646. +For (an unreleased) example:
  1647. +If you are using "linux-4.10" and the "aufs4.10" branch
  1648. +does not exist in aufs-util repository, then "aufs4.9", "aufs4.8"
  1649. +or something numerically smaller is the branch for your kernel.
  1650. +
  1651. +Also you can view all branches by
  1652. + $ git branch -a
  1653. +
  1654. +
  1655. +3. Configuration and Compilation
  1656. +----------------------------------------
  1657. +Make sure you have git-checkout'ed the correct branch.
  1658. +
  1659. +For aufs4-linux tree,
  1660. +- enable CONFIG_AUFS_FS.
  1661. +- set other aufs configurations if necessary.
  1662. +
  1663. +For aufs4-standalone tree,
  1664. +There are several ways to build.
  1665. +
  1666. +1.
  1667. +- apply ./aufs4-kbuild.patch to your kernel source files.
  1668. +- apply ./aufs4-base.patch too.
  1669. +- apply ./aufs4-mmap.patch too.
  1670. +- apply ./aufs4-standalone.patch too, if you have a plan to set
  1671. + CONFIG_AUFS_FS=m. otherwise you don't need ./aufs4-standalone.patch.
  1672. +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
  1673. + kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
  1674. +- enable CONFIG_AUFS_FS, you can select either
  1675. + =m or =y.
  1676. +- and build your kernel as usual.
  1677. +- install the built kernel.
  1678. + Note: Since linux-3.9, every filesystem module requires an alias
  1679. + "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
  1680. + modules.aliases file if you set CONFIG_AUFS_FS=m.
  1681. +- install the header files too by "make headers_install" to the
  1682. + directory where you specify. By default, it is $PWD/usr.
  1683. + "make help" shows a brief note for headers_install.
  1684. +- and reboot your system.
  1685. +
  1686. +2.
  1687. +- module only (CONFIG_AUFS_FS=m).
  1688. +- apply ./aufs4-base.patch to your kernel source files.
  1689. +- apply ./aufs4-mmap.patch too.
  1690. +- apply ./aufs4-standalone.patch too.
  1691. +- build your kernel, don't forget "make headers_install", and reboot.
  1692. +- edit ./config.mk and set other aufs configurations if necessary.
  1693. + Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
  1694. + every aufs configurations.
  1695. +- build the module by simple "make".
  1696. + Note: Since linux-3.9, every filesystem module requires an alias
  1697. + "fs-<fsname>". You should make sure that "fs-aufs" is listed in your
  1698. + modules.aliases file.
  1699. +- you can specify ${KDIR} make variable which points to your kernel
  1700. + source tree.
  1701. +- install the files
  1702. + + run "make install" to install the aufs module, or copy the built
  1703. + $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
  1704. + + run "make install_headers" (instead of headers_install) to install
  1705. + the modified aufs header file (you can specify DESTDIR which is
  1706. + available in aufs standalone version's Makefile only), or copy
  1707. + $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
  1708. + you like manually. By default, the target directory is $PWD/usr.
  1709. +- no need to apply aufs4-kbuild.patch, nor copying source files to your
  1710. + kernel source tree.
  1711. +
  1712. +Note: The header file aufs_type.h is necessary to build aufs-util
  1713. + as well as "make headers_install" in the kernel source tree.
  1714. + headers_install is subject to be forgotten, but it is essentially
  1715. + necessary, not only for building aufs-util.
  1716. + You may not meet problems without headers_install in some older
  1717. + version though.
  1718. +
  1719. +And then,
  1720. +- read README in aufs-util, build and install it
  1721. +- note that your distribution may contain an obsoleted version of
  1722. + aufs_type.h in /usr/include/linux or something. When you build aufs
  1723. + utilities, make sure that your compiler refers the correct aufs header
  1724. + file which is built by "make headers_install."
  1725. +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
  1726. + then run "make install_ulib" too. And refer to the aufs manual in
  1727. + detail.
  1728. +
  1729. +There several other patches in aufs4-standalone.git. They are all
  1730. +optional. When you meet some problems, they will help you.
  1731. +- aufs4-loopback.patch
  1732. + Supports a nested loopback mount in a branch-fs. This patch is
  1733. + unnecessary until aufs produces a message like "you may want to try
  1734. + another patch for loopback file".
  1735. +- vfs-ino.patch
  1736. + Modifies a system global kernel internal function get_next_ino() in
  1737. + order to stop assigning 0 for an inode-number. Not directly related to
  1738. + aufs, but recommended generally.
  1739. +- tmpfs-idr.patch
  1740. + Keeps the tmpfs inode number as the lowest value. Effective to reduce
  1741. + the size of aufs XINO files for tmpfs branch. Also it prevents the
  1742. + duplication of inode number, which is important for backup tools and
  1743. + other utilities. When you find aufs XINO files for tmpfs branch
  1744. + growing too much, try this patch.
  1745. +- lockdep-debug.patch
  1746. + Because aufs is not only an ordinary filesystem (callee of VFS), but
  1747. + also a caller of VFS functions for branch filesystems, subclassing of
  1748. + the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
  1749. + feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
  1750. + need to apply this debug patch to expand several constant values.
  1751. + If don't know what LOCKDEP, then you don't have apply this patch.
  1752. +
  1753. +
  1754. +4. Usage
  1755. +----------------------------------------
  1756. +At first, make sure aufs-util are installed, and please read the aufs
  1757. +manual, aufs.5 in aufs-util.git tree.
  1758. +$ man -l aufs.5
  1759. +
  1760. +And then,
  1761. +$ mkdir /tmp/rw /tmp/aufs
  1762. +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
  1763. +
  1764. +Here is another example. The result is equivalent.
  1765. +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
  1766. + Or
  1767. +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
  1768. +# mount -o remount,append:${HOME} /tmp/aufs
  1769. +
  1770. +Then, you can see whole tree of your home dir through /tmp/aufs. If
  1771. +you modify a file under /tmp/aufs, the one on your home directory is
  1772. +not affected, instead the same named file will be newly created under
  1773. +/tmp/rw. And all of your modification to a file will be applied to
  1774. +the one under /tmp/rw. This is called the file based Copy on Write
  1775. +(COW) method.
  1776. +Aufs mount options are described in aufs.5.
  1777. +If you run chroot or something and make your aufs as a root directory,
  1778. +then you need to customize the shutdown script. See the aufs manual in
  1779. +detail.
  1780. +
  1781. +Additionally, there are some sample usages of aufs which are a
  1782. +diskless system with network booting, and LiveCD over NFS.
  1783. +See sample dir in CVS tree on SourceForge.
  1784. +
  1785. +
  1786. +5. Contact
  1787. +----------------------------------------
  1788. +When you have any problems or strange behaviour in aufs, please let me
  1789. +know with:
  1790. +- /proc/mounts (instead of the output of mount(8))
  1791. +- /sys/module/aufs/*
  1792. +- /sys/fs/aufs/* (if you have them)
  1793. +- /debug/aufs/* (if you have them)
  1794. +- linux kernel version
  1795. + if your kernel is not plain, for example modified by distributor,
  1796. + the url where i can download its source is necessary too.
  1797. +- aufs version which was printed at loading the module or booting the
  1798. + system, instead of the date you downloaded.
  1799. +- configuration (define/undefine CONFIG_AUFS_xxx)
  1800. +- kernel configuration or /proc/config.gz (if you have it)
  1801. +- behaviour which you think to be incorrect
  1802. +- actual operation, reproducible one is better
  1803. +- mailto: aufs-users at lists.sourceforge.net
  1804. +
  1805. +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
  1806. +and Feature Requests) on SourceForge. Please join and write to
  1807. +aufs-users ML.
  1808. +
  1809. +
  1810. +6. Acknowledgements
  1811. +----------------------------------------
  1812. +Thanks to everyone who have tried and are using aufs, whoever
  1813. +have reported a bug or any feedback.
  1814. +
  1815. +Especially donators:
  1816. +Tomas Matejicek(slax.org) made a donation (much more than once).
  1817. + Since Apr 2010, Tomas M (the author of Slax and Linux Live
  1818. + scripts) is making "doubling" donations.
  1819. + Unfortunately I cannot list all of the donators, but I really
  1820. + appreciate.
  1821. + It ends Aug 2010, but the ordinary donation URL is still available.
  1822. + <http://sourceforge.net/donate/index.php?group_id=167503>
  1823. +Dai Itasaka made a donation (2007/8).
  1824. +Chuck Smith made a donation (2008/4, 10 and 12).
  1825. +Henk Schoneveld made a donation (2008/9).
  1826. +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
  1827. +Francois Dupoux made a donation (2008/11).
  1828. +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
  1829. + aufs2 GIT tree (2009/2).
  1830. +William Grant made a donation (2009/3).
  1831. +Patrick Lane made a donation (2009/4).
  1832. +The Mail Archive (mail-archive.com) made donations (2009/5).
  1833. +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
  1834. +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
  1835. +Pavel Pronskiy made a donation (2011/2).
  1836. +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
  1837. + Networks (Ed Wildgoose) made a donation for hardware (2011/3).
  1838. +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
  1839. +11).
  1840. +Sam Liddicott made a donation (2011/9).
  1841. +Era Scarecrow made a donation (2013/4).
  1842. +Bor Ratajc made a donation (2013/4).
  1843. +Alessandro Gorreta made a donation (2013/4).
  1844. +POIRETTE Marc made a donation (2013/4).
  1845. +Alessandro Gorreta made a donation (2013/4).
  1846. +lauri kasvandik made a donation (2013/5).
  1847. +"pemasu from Finland" made a donation (2013/7).
  1848. +The Parted Magic Project made a donation (2013/9 and 11).
  1849. +Pavel Barta made a donation (2013/10).
  1850. +Nikolay Pertsev made a donation (2014/5).
  1851. +James B made a donation (2014/7 and 2015/7).
  1852. +Stefano Di Biase made a donation (2014/8).
  1853. +Daniel Epellei made a donation (2015/1).
  1854. +OmegaPhil made a donation (2016/1, 2018/4).
  1855. +Tomasz Szewczyk made a donation (2016/4).
  1856. +James Burry made a donation (2016/12).
  1857. +Carsten Rose made a donation (2018/9).
  1858. +Porteus Kiosk made a donation (2018/10).
  1859. +
  1860. +Thank you very much.
  1861. +Donations are always, including future donations, very important and
  1862. +helpful for me to keep on developing aufs.
  1863. +
  1864. +
  1865. +7.
  1866. +----------------------------------------
  1867. +If you are an experienced user, no explanation is needed. Aufs is
  1868. +just a linux filesystem.
  1869. +
  1870. +
  1871. +Enjoy!
  1872. +
  1873. +# Local variables: ;
  1874. +# mode: text;
  1875. +# End: ;
  1876. --- /dev/null
  1877. +++ linux-4.19/fs/aufs/aufs.h 2018-11-20 09:35:15.007195503 +0200
  1878. @@ -0,0 +1,62 @@
  1879. +/* SPDX-License-Identifier: GPL-2.0 */
  1880. +/*
  1881. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  1882. + *
  1883. + * This program, aufs is free software; you can redistribute it and/or modify
  1884. + * it under the terms of the GNU General Public License as published by
  1885. + * the Free Software Foundation; either version 2 of the License, or
  1886. + * (at your option) any later version.
  1887. + *
  1888. + * This program is distributed in the hope that it will be useful,
  1889. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1890. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1891. + * GNU General Public License for more details.
  1892. + *
  1893. + * You should have received a copy of the GNU General Public License
  1894. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  1895. + */
  1896. +
  1897. +/*
  1898. + * all header files
  1899. + */
  1900. +
  1901. +#ifndef __AUFS_H__
  1902. +#define __AUFS_H__
  1903. +
  1904. +#ifdef __KERNEL__
  1905. +
  1906. +#define AuStub(type, name, body, ...) \
  1907. + static inline type name(__VA_ARGS__) { body; }
  1908. +
  1909. +#define AuStubVoid(name, ...) \
  1910. + AuStub(void, name, , __VA_ARGS__)
  1911. +#define AuStubInt0(name, ...) \
  1912. + AuStub(int, name, return 0, __VA_ARGS__)
  1913. +
  1914. +#include "debug.h"
  1915. +
  1916. +#include "branch.h"
  1917. +#include "cpup.h"
  1918. +#include "dcsub.h"
  1919. +#include "dbgaufs.h"
  1920. +#include "dentry.h"
  1921. +#include "dir.h"
  1922. +#include "dirren.h"
  1923. +#include "dynop.h"
  1924. +#include "file.h"
  1925. +#include "fstype.h"
  1926. +#include "hbl.h"
  1927. +#include "inode.h"
  1928. +#include "lcnt.h"
  1929. +#include "loop.h"
  1930. +#include "module.h"
  1931. +#include "opts.h"
  1932. +#include "rwsem.h"
  1933. +#include "super.h"
  1934. +#include "sysaufs.h"
  1935. +#include "vfsub.h"
  1936. +#include "whout.h"
  1937. +#include "wkq.h"
  1938. +
  1939. +#endif /* __KERNEL__ */
  1940. +#endif /* __AUFS_H__ */
  1941. --- /dev/null
  1942. +++ linux-4.19/fs/aufs/branch.c 2018-11-20 09:35:15.009195503 +0200
  1943. @@ -0,0 +1,1422 @@
  1944. +// SPDX-License-Identifier: GPL-2.0
  1945. +/*
  1946. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  1947. + *
  1948. + * This program, aufs is free software; you can redistribute it and/or modify
  1949. + * it under the terms of the GNU General Public License as published by
  1950. + * the Free Software Foundation; either version 2 of the License, or
  1951. + * (at your option) any later version.
  1952. + *
  1953. + * This program is distributed in the hope that it will be useful,
  1954. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1955. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1956. + * GNU General Public License for more details.
  1957. + *
  1958. + * You should have received a copy of the GNU General Public License
  1959. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  1960. + */
  1961. +
  1962. +/*
  1963. + * branch management
  1964. + */
  1965. +
  1966. +#include <linux/compat.h>
  1967. +#include <linux/statfs.h>
  1968. +#include "aufs.h"
  1969. +
  1970. +/*
  1971. + * free a single branch
  1972. + */
  1973. +static void au_br_do_free(struct au_branch *br)
  1974. +{
  1975. + int i;
  1976. + struct au_wbr *wbr;
  1977. + struct au_dykey **key;
  1978. +
  1979. + au_hnotify_fin_br(br);
  1980. + /* always, regardless the mount option */
  1981. + au_dr_hino_free(&br->br_dirren);
  1982. + au_xino_put(br);
  1983. +
  1984. + AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
  1985. + au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
  1986. + AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
  1987. + au_lcnt_fin(&br->br_count, /*do_sync*/0);
  1988. +
  1989. + wbr = br->br_wbr;
  1990. + if (wbr) {
  1991. + for (i = 0; i < AuBrWh_Last; i++)
  1992. + dput(wbr->wbr_wh[i]);
  1993. + AuDebugOn(atomic_read(&wbr->wbr_wh_running));
  1994. + AuRwDestroy(&wbr->wbr_wh_rwsem);
  1995. + }
  1996. +
  1997. + if (br->br_fhsm) {
  1998. + au_br_fhsm_fin(br->br_fhsm);
  1999. + kfree(br->br_fhsm);
  2000. + }
  2001. +
  2002. + key = br->br_dykey;
  2003. + for (i = 0; i < AuBrDynOp; i++, key++)
  2004. + if (*key)
  2005. + au_dy_put(*key);
  2006. + else
  2007. + break;
  2008. +
  2009. + /* recursive lock, s_umount of branch's */
  2010. + /* synchronize_rcu(); */ /* why? */
  2011. + lockdep_off();
  2012. + path_put(&br->br_path);
  2013. + lockdep_on();
  2014. + kfree(wbr);
  2015. + au_lcnt_wait_for_fin(&br->br_nfiles);
  2016. + au_lcnt_wait_for_fin(&br->br_count);
  2017. + /* I don't know why, but percpu_refcount requires this */
  2018. + /* synchronize_rcu(); */
  2019. + kfree(br);
  2020. +}
  2021. +
  2022. +/*
  2023. + * frees all branches
  2024. + */
  2025. +void au_br_free(struct au_sbinfo *sbinfo)
  2026. +{
  2027. + aufs_bindex_t bmax;
  2028. + struct au_branch **br;
  2029. +
  2030. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  2031. +
  2032. + bmax = sbinfo->si_bbot + 1;
  2033. + br = sbinfo->si_branch;
  2034. + while (bmax--)
  2035. + au_br_do_free(*br++);
  2036. +}
  2037. +
  2038. +/*
  2039. + * find the index of a branch which is specified by @br_id.
  2040. + */
  2041. +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
  2042. +{
  2043. + aufs_bindex_t bindex, bbot;
  2044. +
  2045. + bbot = au_sbbot(sb);
  2046. + for (bindex = 0; bindex <= bbot; bindex++)
  2047. + if (au_sbr_id(sb, bindex) == br_id)
  2048. + return bindex;
  2049. + return -1;
  2050. +}
  2051. +
  2052. +/* ---------------------------------------------------------------------- */
  2053. +
  2054. +/*
  2055. + * add a branch
  2056. + */
  2057. +
  2058. +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
  2059. + struct dentry *h_root)
  2060. +{
  2061. + if (unlikely(h_adding == h_root
  2062. + || au_test_loopback_overlap(sb, h_adding)))
  2063. + return 1;
  2064. + if (h_adding->d_sb != h_root->d_sb)
  2065. + return 0;
  2066. + return au_test_subdir(h_adding, h_root)
  2067. + || au_test_subdir(h_root, h_adding);
  2068. +}
  2069. +
  2070. +/*
  2071. + * returns a newly allocated branch. @new_nbranch is a number of branches
  2072. + * after adding a branch.
  2073. + */
  2074. +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
  2075. + int perm)
  2076. +{
  2077. + struct au_branch *add_branch;
  2078. + struct dentry *root;
  2079. + struct inode *inode;
  2080. + int err;
  2081. +
  2082. + err = -ENOMEM;
  2083. + add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
  2084. + if (unlikely(!add_branch))
  2085. + goto out;
  2086. + add_branch->br_xino = au_xino_alloc(/*nfile*/1);
  2087. + if (unlikely(!add_branch->br_xino))
  2088. + goto out_br;
  2089. + err = au_hnotify_init_br(add_branch, perm);
  2090. + if (unlikely(err))
  2091. + goto out_xino;
  2092. +
  2093. + if (au_br_writable(perm)) {
  2094. + /* may be freed separately at changing the branch permission */
  2095. + add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
  2096. + GFP_NOFS);
  2097. + if (unlikely(!add_branch->br_wbr))
  2098. + goto out_hnotify;
  2099. + }
  2100. +
  2101. + if (au_br_fhsm(perm)) {
  2102. + err = au_fhsm_br_alloc(add_branch);
  2103. + if (unlikely(err))
  2104. + goto out_wbr;
  2105. + }
  2106. +
  2107. + root = sb->s_root;
  2108. + err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
  2109. + if (!err)
  2110. + err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
  2111. + if (!err) {
  2112. + inode = d_inode(root);
  2113. + err = au_hinode_realloc(au_ii(inode), new_nbranch,
  2114. + /*may_shrink*/0);
  2115. + }
  2116. + if (!err)
  2117. + return add_branch; /* success */
  2118. +
  2119. +out_wbr:
  2120. + kfree(add_branch->br_wbr);
  2121. +out_hnotify:
  2122. + au_hnotify_fin_br(add_branch);
  2123. +out_xino:
  2124. + au_xino_put(add_branch);
  2125. +out_br:
  2126. + kfree(add_branch);
  2127. +out:
  2128. + return ERR_PTR(err);
  2129. +}
  2130. +
  2131. +/*
  2132. + * test if the branch permission is legal or not.
  2133. + */
  2134. +static int test_br(struct inode *inode, int brperm, char *path)
  2135. +{
  2136. + int err;
  2137. +
  2138. + err = (au_br_writable(brperm) && IS_RDONLY(inode));
  2139. + if (!err)
  2140. + goto out;
  2141. +
  2142. + err = -EINVAL;
  2143. + pr_err("write permission for readonly mount or inode, %s\n", path);
  2144. +
  2145. +out:
  2146. + return err;
  2147. +}
  2148. +
  2149. +/*
  2150. + * returns:
  2151. + * 0: success, the caller will add it
  2152. + * plus: success, it is already unified, the caller should ignore it
  2153. + * minus: error
  2154. + */
  2155. +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
  2156. +{
  2157. + int err;
  2158. + aufs_bindex_t bbot, bindex;
  2159. + struct dentry *root, *h_dentry;
  2160. + struct inode *inode, *h_inode;
  2161. +
  2162. + root = sb->s_root;
  2163. + bbot = au_sbbot(sb);
  2164. + if (unlikely(bbot >= 0
  2165. + && au_find_dbindex(root, add->path.dentry) >= 0)) {
  2166. + err = 1;
  2167. + if (!remount) {
  2168. + err = -EINVAL;
  2169. + pr_err("%s duplicated\n", add->pathname);
  2170. + }
  2171. + goto out;
  2172. + }
  2173. +
  2174. + err = -ENOSPC; /* -E2BIG; */
  2175. + if (unlikely(AUFS_BRANCH_MAX <= add->bindex
  2176. + || AUFS_BRANCH_MAX - 1 <= bbot)) {
  2177. + pr_err("number of branches exceeded %s\n", add->pathname);
  2178. + goto out;
  2179. + }
  2180. +
  2181. + err = -EDOM;
  2182. + if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
  2183. + pr_err("bad index %d\n", add->bindex);
  2184. + goto out;
  2185. + }
  2186. +
  2187. + inode = d_inode(add->path.dentry);
  2188. + err = -ENOENT;
  2189. + if (unlikely(!inode->i_nlink)) {
  2190. + pr_err("no existence %s\n", add->pathname);
  2191. + goto out;
  2192. + }
  2193. +
  2194. + err = -EINVAL;
  2195. + if (unlikely(inode->i_sb == sb)) {
  2196. + pr_err("%s must be outside\n", add->pathname);
  2197. + goto out;
  2198. + }
  2199. +
  2200. + if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
  2201. + pr_err("unsupported filesystem, %s (%s)\n",
  2202. + add->pathname, au_sbtype(inode->i_sb));
  2203. + goto out;
  2204. + }
  2205. +
  2206. + if (unlikely(inode->i_sb->s_stack_depth)) {
  2207. + pr_err("already stacked, %s (%s)\n",
  2208. + add->pathname, au_sbtype(inode->i_sb));
  2209. + goto out;
  2210. + }
  2211. +
  2212. + err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
  2213. + if (unlikely(err))
  2214. + goto out;
  2215. +
  2216. + if (bbot < 0)
  2217. + return 0; /* success */
  2218. +
  2219. + err = -EINVAL;
  2220. + for (bindex = 0; bindex <= bbot; bindex++)
  2221. + if (unlikely(test_overlap(sb, add->path.dentry,
  2222. + au_h_dptr(root, bindex)))) {
  2223. + pr_err("%s is overlapped\n", add->pathname);
  2224. + goto out;
  2225. + }
  2226. +
  2227. + err = 0;
  2228. + if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
  2229. + h_dentry = au_h_dptr(root, 0);
  2230. + h_inode = d_inode(h_dentry);
  2231. + if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
  2232. + || !uid_eq(h_inode->i_uid, inode->i_uid)
  2233. + || !gid_eq(h_inode->i_gid, inode->i_gid))
  2234. + pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
  2235. + add->pathname,
  2236. + i_uid_read(inode), i_gid_read(inode),
  2237. + (inode->i_mode & S_IALLUGO),
  2238. + i_uid_read(h_inode), i_gid_read(h_inode),
  2239. + (h_inode->i_mode & S_IALLUGO));
  2240. + }
  2241. +
  2242. +out:
  2243. + return err;
  2244. +}
  2245. +
  2246. +/*
  2247. + * initialize or clean the whiteouts for an adding branch
  2248. + */
  2249. +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
  2250. + int new_perm)
  2251. +{
  2252. + int err, old_perm;
  2253. + aufs_bindex_t bindex;
  2254. + struct inode *h_inode;
  2255. + struct au_wbr *wbr;
  2256. + struct au_hinode *hdir;
  2257. + struct dentry *h_dentry;
  2258. +
  2259. + err = vfsub_mnt_want_write(au_br_mnt(br));
  2260. + if (unlikely(err))
  2261. + goto out;
  2262. +
  2263. + wbr = br->br_wbr;
  2264. + old_perm = br->br_perm;
  2265. + br->br_perm = new_perm;
  2266. + hdir = NULL;
  2267. + h_inode = NULL;
  2268. + bindex = au_br_index(sb, br->br_id);
  2269. + if (0 <= bindex) {
  2270. + hdir = au_hi(d_inode(sb->s_root), bindex);
  2271. + au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
  2272. + } else {
  2273. + h_dentry = au_br_dentry(br);
  2274. + h_inode = d_inode(h_dentry);
  2275. + inode_lock_nested(h_inode, AuLsc_I_PARENT);
  2276. + }
  2277. + if (!wbr)
  2278. + err = au_wh_init(br, sb);
  2279. + else {
  2280. + wbr_wh_write_lock(wbr);
  2281. + err = au_wh_init(br, sb);
  2282. + wbr_wh_write_unlock(wbr);
  2283. + }
  2284. + if (hdir)
  2285. + au_hn_inode_unlock(hdir);
  2286. + else
  2287. + inode_unlock(h_inode);
  2288. + vfsub_mnt_drop_write(au_br_mnt(br));
  2289. + br->br_perm = old_perm;
  2290. +
  2291. + if (!err && wbr && !au_br_writable(new_perm)) {
  2292. + kfree(wbr);
  2293. + br->br_wbr = NULL;
  2294. + }
  2295. +
  2296. +out:
  2297. + return err;
  2298. +}
  2299. +
  2300. +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
  2301. + int perm)
  2302. +{
  2303. + int err;
  2304. + struct kstatfs kst;
  2305. + struct au_wbr *wbr;
  2306. +
  2307. + wbr = br->br_wbr;
  2308. + au_rw_init(&wbr->wbr_wh_rwsem);
  2309. + atomic_set(&wbr->wbr_wh_running, 0);
  2310. +
  2311. + /*
  2312. + * a limit for rmdir/rename a dir
  2313. + * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
  2314. + */
  2315. + err = vfs_statfs(&br->br_path, &kst);
  2316. + if (unlikely(err))
  2317. + goto out;
  2318. + err = -EINVAL;
  2319. + if (kst.f_namelen >= NAME_MAX)
  2320. + err = au_br_init_wh(sb, br, perm);
  2321. + else
  2322. + pr_err("%pd(%s), unsupported namelen %ld\n",
  2323. + au_br_dentry(br),
  2324. + au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
  2325. +
  2326. +out:
  2327. + return err;
  2328. +}
  2329. +
  2330. +/* initialize a new branch */
  2331. +static int au_br_init(struct au_branch *br, struct super_block *sb,
  2332. + struct au_opt_add *add)
  2333. +{
  2334. + int err;
  2335. + struct au_branch *brbase;
  2336. + struct file *xf;
  2337. + struct inode *h_inode;
  2338. +
  2339. + err = 0;
  2340. + br->br_perm = add->perm;
  2341. + br->br_path = add->path; /* set first, path_get() later */
  2342. + spin_lock_init(&br->br_dykey_lock);
  2343. + au_lcnt_init(&br->br_nfiles, /*release*/NULL);
  2344. + au_lcnt_init(&br->br_count, /*release*/NULL);
  2345. + br->br_id = au_new_br_id(sb);
  2346. + AuDebugOn(br->br_id < 0);
  2347. +
  2348. + /* always, regardless the given option */
  2349. + err = au_dr_br_init(sb, br, &add->path);
  2350. + if (unlikely(err))
  2351. + goto out_err;
  2352. +
  2353. + if (au_br_writable(add->perm)) {
  2354. + err = au_wbr_init(br, sb, add->perm);
  2355. + if (unlikely(err))
  2356. + goto out_err;
  2357. + }
  2358. +
  2359. + if (au_opt_test(au_mntflags(sb), XINO)) {
  2360. + brbase = au_sbr(sb, 0);
  2361. + xf = au_xino_file(brbase->br_xino, /*idx*/-1);
  2362. + AuDebugOn(!xf);
  2363. + h_inode = d_inode(add->path.dentry);
  2364. + err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
  2365. + if (unlikely(err)) {
  2366. + AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
  2367. + goto out_err;
  2368. + }
  2369. + }
  2370. +
  2371. + sysaufs_br_init(br);
  2372. + path_get(&br->br_path);
  2373. + goto out; /* success */
  2374. +
  2375. +out_err:
  2376. + memset(&br->br_path, 0, sizeof(br->br_path));
  2377. +out:
  2378. + return err;
  2379. +}
  2380. +
  2381. +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
  2382. + struct au_branch *br, aufs_bindex_t bbot,
  2383. + aufs_bindex_t amount)
  2384. +{
  2385. + struct au_branch **brp;
  2386. +
  2387. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  2388. +
  2389. + brp = sbinfo->si_branch + bindex;
  2390. + memmove(brp + 1, brp, sizeof(*brp) * amount);
  2391. + *brp = br;
  2392. + sbinfo->si_bbot++;
  2393. + if (unlikely(bbot < 0))
  2394. + sbinfo->si_bbot = 0;
  2395. +}
  2396. +
  2397. +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
  2398. + aufs_bindex_t bbot, aufs_bindex_t amount)
  2399. +{
  2400. + struct au_hdentry *hdp;
  2401. +
  2402. + AuRwMustWriteLock(&dinfo->di_rwsem);
  2403. +
  2404. + hdp = au_hdentry(dinfo, bindex);
  2405. + memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
  2406. + au_h_dentry_init(hdp);
  2407. + dinfo->di_bbot++;
  2408. + if (unlikely(bbot < 0))
  2409. + dinfo->di_btop = 0;
  2410. +}
  2411. +
  2412. +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
  2413. + aufs_bindex_t bbot, aufs_bindex_t amount)
  2414. +{
  2415. + struct au_hinode *hip;
  2416. +
  2417. + AuRwMustWriteLock(&iinfo->ii_rwsem);
  2418. +
  2419. + hip = au_hinode(iinfo, bindex);
  2420. + memmove(hip + 1, hip, sizeof(*hip) * amount);
  2421. + au_hinode_init(hip);
  2422. + iinfo->ii_bbot++;
  2423. + if (unlikely(bbot < 0))
  2424. + iinfo->ii_btop = 0;
  2425. +}
  2426. +
  2427. +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
  2428. + aufs_bindex_t bindex)
  2429. +{
  2430. + struct dentry *root, *h_dentry;
  2431. + struct inode *root_inode, *h_inode;
  2432. + aufs_bindex_t bbot, amount;
  2433. +
  2434. + root = sb->s_root;
  2435. + root_inode = d_inode(root);
  2436. + bbot = au_sbbot(sb);
  2437. + amount = bbot + 1 - bindex;
  2438. + h_dentry = au_br_dentry(br);
  2439. + au_sbilist_lock();
  2440. + au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
  2441. + au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
  2442. + au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
  2443. + au_set_h_dptr(root, bindex, dget(h_dentry));
  2444. + h_inode = d_inode(h_dentry);
  2445. + au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
  2446. + au_sbilist_unlock();
  2447. +}
  2448. +
  2449. +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
  2450. +{
  2451. + int err;
  2452. + aufs_bindex_t bbot, add_bindex;
  2453. + struct dentry *root, *h_dentry;
  2454. + struct inode *root_inode;
  2455. + struct au_branch *add_branch;
  2456. +
  2457. + root = sb->s_root;
  2458. + root_inode = d_inode(root);
  2459. + IMustLock(root_inode);
  2460. + IiMustWriteLock(root_inode);
  2461. + err = test_add(sb, add, remount);
  2462. + if (unlikely(err < 0))
  2463. + goto out;
  2464. + if (err) {
  2465. + err = 0;
  2466. + goto out; /* success */
  2467. + }
  2468. +
  2469. + bbot = au_sbbot(sb);
  2470. + add_branch = au_br_alloc(sb, bbot + 2, add->perm);
  2471. + err = PTR_ERR(add_branch);
  2472. + if (IS_ERR(add_branch))
  2473. + goto out;
  2474. +
  2475. + err = au_br_init(add_branch, sb, add);
  2476. + if (unlikely(err)) {
  2477. + au_br_do_free(add_branch);
  2478. + goto out;
  2479. + }
  2480. +
  2481. + add_bindex = add->bindex;
  2482. + sysaufs_brs_del(sb, add_bindex); /* remove successors */
  2483. + au_br_do_add(sb, add_branch, add_bindex);
  2484. + sysaufs_brs_add(sb, add_bindex); /* append successors */
  2485. + dbgaufs_brs_add(sb, add_bindex, /*topdown*/0); /* rename successors */
  2486. +
  2487. + h_dentry = add->path.dentry;
  2488. + if (!add_bindex) {
  2489. + au_cpup_attr_all(root_inode, /*force*/1);
  2490. + sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
  2491. + } else
  2492. + au_add_nlink(root_inode, d_inode(h_dentry));
  2493. +
  2494. +out:
  2495. + return err;
  2496. +}
  2497. +
  2498. +/* ---------------------------------------------------------------------- */
  2499. +
  2500. +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
  2501. + unsigned long long max __maybe_unused,
  2502. + void *arg)
  2503. +{
  2504. + unsigned long long n;
  2505. + struct file **p, *f;
  2506. + struct hlist_bl_head *files;
  2507. + struct hlist_bl_node *pos;
  2508. + struct au_finfo *finfo;
  2509. +
  2510. + n = 0;
  2511. + p = a;
  2512. + files = &au_sbi(sb)->si_files;
  2513. + hlist_bl_lock(files);
  2514. + hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
  2515. + f = finfo->fi_file;
  2516. + if (file_count(f)
  2517. + && !special_file(file_inode(f)->i_mode)) {
  2518. + get_file(f);
  2519. + *p++ = f;
  2520. + n++;
  2521. + AuDebugOn(n > max);
  2522. + }
  2523. + }
  2524. + hlist_bl_unlock(files);
  2525. +
  2526. + return n;
  2527. +}
  2528. +
  2529. +static struct file **au_farray_alloc(struct super_block *sb,
  2530. + unsigned long long *max)
  2531. +{
  2532. + struct au_sbinfo *sbi;
  2533. +
  2534. + sbi = au_sbi(sb);
  2535. + *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
  2536. + return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
  2537. +}
  2538. +
  2539. +static void au_farray_free(struct file **a, unsigned long long max)
  2540. +{
  2541. + unsigned long long ull;
  2542. +
  2543. + for (ull = 0; ull < max; ull++)
  2544. + if (a[ull])
  2545. + fput(a[ull]);
  2546. + kvfree(a);
  2547. +}
  2548. +
  2549. +/* ---------------------------------------------------------------------- */
  2550. +
  2551. +/*
  2552. + * delete a branch
  2553. + */
  2554. +
  2555. +/* to show the line number, do not make it inlined function */
  2556. +#define AuVerbose(do_info, fmt, ...) do { \
  2557. + if (do_info) \
  2558. + pr_info(fmt, ##__VA_ARGS__); \
  2559. +} while (0)
  2560. +
  2561. +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
  2562. + aufs_bindex_t bbot)
  2563. +{
  2564. + return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
  2565. +}
  2566. +
  2567. +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
  2568. + aufs_bindex_t bbot)
  2569. +{
  2570. + return au_test_ibusy(d_inode(dentry), btop, bbot);
  2571. +}
  2572. +
  2573. +/*
  2574. + * test if the branch is deletable or not.
  2575. + */
  2576. +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
  2577. + unsigned int sigen, const unsigned int verbose)
  2578. +{
  2579. + int err, i, j, ndentry;
  2580. + aufs_bindex_t btop, bbot;
  2581. + struct au_dcsub_pages dpages;
  2582. + struct au_dpage *dpage;
  2583. + struct dentry *d;
  2584. +
  2585. + err = au_dpages_init(&dpages, GFP_NOFS);
  2586. + if (unlikely(err))
  2587. + goto out;
  2588. + err = au_dcsub_pages(&dpages, root, NULL, NULL);
  2589. + if (unlikely(err))
  2590. + goto out_dpages;
  2591. +
  2592. + for (i = 0; !err && i < dpages.ndpage; i++) {
  2593. + dpage = dpages.dpages + i;
  2594. + ndentry = dpage->ndentry;
  2595. + for (j = 0; !err && j < ndentry; j++) {
  2596. + d = dpage->dentries[j];
  2597. + AuDebugOn(au_dcount(d) <= 0);
  2598. + if (!au_digen_test(d, sigen)) {
  2599. + di_read_lock_child(d, AuLock_IR);
  2600. + if (unlikely(au_dbrange_test(d))) {
  2601. + di_read_unlock(d, AuLock_IR);
  2602. + continue;
  2603. + }
  2604. + } else {
  2605. + di_write_lock_child(d);
  2606. + if (unlikely(au_dbrange_test(d))) {
  2607. + di_write_unlock(d);
  2608. + continue;
  2609. + }
  2610. + err = au_reval_dpath(d, sigen);
  2611. + if (!err)
  2612. + di_downgrade_lock(d, AuLock_IR);
  2613. + else {
  2614. + di_write_unlock(d);
  2615. + break;
  2616. + }
  2617. + }
  2618. +
  2619. + /* AuDbgDentry(d); */
  2620. + btop = au_dbtop(d);
  2621. + bbot = au_dbbot(d);
  2622. + if (btop <= bindex
  2623. + && bindex <= bbot
  2624. + && au_h_dptr(d, bindex)
  2625. + && au_test_dbusy(d, btop, bbot)) {
  2626. + err = -EBUSY;
  2627. + AuVerbose(verbose, "busy %pd\n", d);
  2628. + AuDbgDentry(d);
  2629. + }
  2630. + di_read_unlock(d, AuLock_IR);
  2631. + }
  2632. + }
  2633. +
  2634. +out_dpages:
  2635. + au_dpages_free(&dpages);
  2636. +out:
  2637. + return err;
  2638. +}
  2639. +
  2640. +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
  2641. + unsigned int sigen, const unsigned int verbose)
  2642. +{
  2643. + int err;
  2644. + unsigned long long max, ull;
  2645. + struct inode *i, **array;
  2646. + aufs_bindex_t btop, bbot;
  2647. +
  2648. + array = au_iarray_alloc(sb, &max);
  2649. + err = PTR_ERR(array);
  2650. + if (IS_ERR(array))
  2651. + goto out;
  2652. +
  2653. + err = 0;
  2654. + AuDbg("b%d\n", bindex);
  2655. + for (ull = 0; !err && ull < max; ull++) {
  2656. + i = array[ull];
  2657. + if (unlikely(!i))
  2658. + break;
  2659. + if (i->i_ino == AUFS_ROOT_INO)
  2660. + continue;
  2661. +
  2662. + /* AuDbgInode(i); */
  2663. + if (au_iigen(i, NULL) == sigen)
  2664. + ii_read_lock_child(i);
  2665. + else {
  2666. + ii_write_lock_child(i);
  2667. + err = au_refresh_hinode_self(i);
  2668. + au_iigen_dec(i);
  2669. + if (!err)
  2670. + ii_downgrade_lock(i);
  2671. + else {
  2672. + ii_write_unlock(i);
  2673. + break;
  2674. + }
  2675. + }
  2676. +
  2677. + btop = au_ibtop(i);
  2678. + bbot = au_ibbot(i);
  2679. + if (btop <= bindex
  2680. + && bindex <= bbot
  2681. + && au_h_iptr(i, bindex)
  2682. + && au_test_ibusy(i, btop, bbot)) {
  2683. + err = -EBUSY;
  2684. + AuVerbose(verbose, "busy i%lu\n", i->i_ino);
  2685. + AuDbgInode(i);
  2686. + }
  2687. + ii_read_unlock(i);
  2688. + }
  2689. + au_iarray_free(array, max);
  2690. +
  2691. +out:
  2692. + return err;
  2693. +}
  2694. +
  2695. +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
  2696. + const unsigned int verbose)
  2697. +{
  2698. + int err;
  2699. + unsigned int sigen;
  2700. +
  2701. + sigen = au_sigen(root->d_sb);
  2702. + DiMustNoWaiters(root);
  2703. + IiMustNoWaiters(d_inode(root));
  2704. + di_write_unlock(root);
  2705. + err = test_dentry_busy(root, bindex, sigen, verbose);
  2706. + if (!err)
  2707. + err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
  2708. + di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
  2709. +
  2710. + return err;
  2711. +}
  2712. +
  2713. +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
  2714. + struct file **to_free, int *idx)
  2715. +{
  2716. + int err;
  2717. + unsigned char matched, root;
  2718. + aufs_bindex_t bindex, bbot;
  2719. + struct au_fidir *fidir;
  2720. + struct au_hfile *hfile;
  2721. +
  2722. + err = 0;
  2723. + root = IS_ROOT(file->f_path.dentry);
  2724. + if (root) {
  2725. + get_file(file);
  2726. + to_free[*idx] = file;
  2727. + (*idx)++;
  2728. + goto out;
  2729. + }
  2730. +
  2731. + matched = 0;
  2732. + fidir = au_fi(file)->fi_hdir;
  2733. + AuDebugOn(!fidir);
  2734. + bbot = au_fbbot_dir(file);
  2735. + for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
  2736. + hfile = fidir->fd_hfile + bindex;
  2737. + if (!hfile->hf_file)
  2738. + continue;
  2739. +
  2740. + if (hfile->hf_br->br_id == br_id) {
  2741. + matched = 1;
  2742. + break;
  2743. + }
  2744. + }
  2745. + if (matched)
  2746. + err = -EBUSY;
  2747. +
  2748. +out:
  2749. + return err;
  2750. +}
  2751. +
  2752. +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
  2753. + struct file **to_free, int opened)
  2754. +{
  2755. + int err, idx;
  2756. + unsigned long long ull, max;
  2757. + aufs_bindex_t btop;
  2758. + struct file *file, **array;
  2759. + struct dentry *root;
  2760. + struct au_hfile *hfile;
  2761. +
  2762. + array = au_farray_alloc(sb, &max);
  2763. + err = PTR_ERR(array);
  2764. + if (IS_ERR(array))
  2765. + goto out;
  2766. +
  2767. + err = 0;
  2768. + idx = 0;
  2769. + root = sb->s_root;
  2770. + di_write_unlock(root);
  2771. + for (ull = 0; ull < max; ull++) {
  2772. + file = array[ull];
  2773. + if (unlikely(!file))
  2774. + break;
  2775. +
  2776. + /* AuDbg("%pD\n", file); */
  2777. + fi_read_lock(file);
  2778. + btop = au_fbtop(file);
  2779. + if (!d_is_dir(file->f_path.dentry)) {
  2780. + hfile = &au_fi(file)->fi_htop;
  2781. + if (hfile->hf_br->br_id == br_id)
  2782. + err = -EBUSY;
  2783. + } else
  2784. + err = test_dir_busy(file, br_id, to_free, &idx);
  2785. + fi_read_unlock(file);
  2786. + if (unlikely(err))
  2787. + break;
  2788. + }
  2789. + di_write_lock_child(root);
  2790. + au_farray_free(array, max);
  2791. + AuDebugOn(idx > opened);
  2792. +
  2793. +out:
  2794. + return err;
  2795. +}
  2796. +
  2797. +static void br_del_file(struct file **to_free, unsigned long long opened,
  2798. + aufs_bindex_t br_id)
  2799. +{
  2800. + unsigned long long ull;
  2801. + aufs_bindex_t bindex, btop, bbot, bfound;
  2802. + struct file *file;
  2803. + struct au_fidir *fidir;
  2804. + struct au_hfile *hfile;
  2805. +
  2806. + for (ull = 0; ull < opened; ull++) {
  2807. + file = to_free[ull];
  2808. + if (unlikely(!file))
  2809. + break;
  2810. +
  2811. + /* AuDbg("%pD\n", file); */
  2812. + AuDebugOn(!d_is_dir(file->f_path.dentry));
  2813. + bfound = -1;
  2814. + fidir = au_fi(file)->fi_hdir;
  2815. + AuDebugOn(!fidir);
  2816. + fi_write_lock(file);
  2817. + btop = au_fbtop(file);
  2818. + bbot = au_fbbot_dir(file);
  2819. + for (bindex = btop; bindex <= bbot; bindex++) {
  2820. + hfile = fidir->fd_hfile + bindex;
  2821. + if (!hfile->hf_file)
  2822. + continue;
  2823. +
  2824. + if (hfile->hf_br->br_id == br_id) {
  2825. + bfound = bindex;
  2826. + break;
  2827. + }
  2828. + }
  2829. + AuDebugOn(bfound < 0);
  2830. + au_set_h_fptr(file, bfound, NULL);
  2831. + if (bfound == btop) {
  2832. + for (btop++; btop <= bbot; btop++)
  2833. + if (au_hf_dir(file, btop)) {
  2834. + au_set_fbtop(file, btop);
  2835. + break;
  2836. + }
  2837. + }
  2838. + fi_write_unlock(file);
  2839. + }
  2840. +}
  2841. +
  2842. +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
  2843. + const aufs_bindex_t bindex,
  2844. + const aufs_bindex_t bbot)
  2845. +{
  2846. + struct au_branch **brp, **p;
  2847. +
  2848. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  2849. +
  2850. + brp = sbinfo->si_branch + bindex;
  2851. + if (bindex < bbot)
  2852. + memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
  2853. + sbinfo->si_branch[0 + bbot] = NULL;
  2854. + sbinfo->si_bbot--;
  2855. +
  2856. + p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
  2857. + /*may_shrink*/1);
  2858. + if (p)
  2859. + sbinfo->si_branch = p;
  2860. + /* harmless error */
  2861. +}
  2862. +
  2863. +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
  2864. + const aufs_bindex_t bbot)
  2865. +{
  2866. + struct au_hdentry *hdp, *p;
  2867. +
  2868. + AuRwMustWriteLock(&dinfo->di_rwsem);
  2869. +
  2870. + hdp = au_hdentry(dinfo, bindex);
  2871. + if (bindex < bbot)
  2872. + memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
  2873. + /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
  2874. + dinfo->di_bbot--;
  2875. +
  2876. + p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
  2877. + /*may_shrink*/1);
  2878. + if (p)
  2879. + dinfo->di_hdentry = p;
  2880. + /* harmless error */
  2881. +}
  2882. +
  2883. +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
  2884. + const aufs_bindex_t bbot)
  2885. +{
  2886. + struct au_hinode *hip, *p;
  2887. +
  2888. + AuRwMustWriteLock(&iinfo->ii_rwsem);
  2889. +
  2890. + hip = au_hinode(iinfo, bindex);
  2891. + if (bindex < bbot)
  2892. + memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
  2893. + /* au_hinode_init(au_hinode(iinfo, bbot)); */
  2894. + iinfo->ii_bbot--;
  2895. +
  2896. + p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
  2897. + /*may_shrink*/1);
  2898. + if (p)
  2899. + iinfo->ii_hinode = p;
  2900. + /* harmless error */
  2901. +}
  2902. +
  2903. +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
  2904. + struct au_branch *br)
  2905. +{
  2906. + aufs_bindex_t bbot;
  2907. + struct au_sbinfo *sbinfo;
  2908. + struct dentry *root, *h_root;
  2909. + struct inode *inode, *h_inode;
  2910. + struct au_hinode *hinode;
  2911. +
  2912. + SiMustWriteLock(sb);
  2913. +
  2914. + root = sb->s_root;
  2915. + inode = d_inode(root);
  2916. + sbinfo = au_sbi(sb);
  2917. + bbot = sbinfo->si_bbot;
  2918. +
  2919. + h_root = au_h_dptr(root, bindex);
  2920. + hinode = au_hi(inode, bindex);
  2921. + h_inode = au_igrab(hinode->hi_inode);
  2922. + au_hiput(hinode);
  2923. +
  2924. + au_sbilist_lock();
  2925. + au_br_do_del_brp(sbinfo, bindex, bbot);
  2926. + au_br_do_del_hdp(au_di(root), bindex, bbot);
  2927. + au_br_do_del_hip(au_ii(inode), bindex, bbot);
  2928. + au_sbilist_unlock();
  2929. +
  2930. + /* ignore an error */
  2931. + au_dr_br_fin(sb, br); /* always, regardless the mount option */
  2932. +
  2933. + dput(h_root);
  2934. + iput(h_inode);
  2935. + au_br_do_free(br);
  2936. +}
  2937. +
  2938. +static unsigned long long empty_cb(struct super_block *sb, void *array,
  2939. + unsigned long long max, void *arg)
  2940. +{
  2941. + return max;
  2942. +}
  2943. +
  2944. +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
  2945. +{
  2946. + int err, rerr, i;
  2947. + unsigned long long opened;
  2948. + unsigned int mnt_flags;
  2949. + aufs_bindex_t bindex, bbot, br_id;
  2950. + unsigned char do_wh, verbose;
  2951. + struct au_branch *br;
  2952. + struct au_wbr *wbr;
  2953. + struct dentry *root;
  2954. + struct file **to_free;
  2955. +
  2956. + err = 0;
  2957. + opened = 0;
  2958. + to_free = NULL;
  2959. + root = sb->s_root;
  2960. + bindex = au_find_dbindex(root, del->h_path.dentry);
  2961. + if (bindex < 0) {
  2962. + if (remount)
  2963. + goto out; /* success */
  2964. + err = -ENOENT;
  2965. + pr_err("%s no such branch\n", del->pathname);
  2966. + goto out;
  2967. + }
  2968. + AuDbg("bindex b%d\n", bindex);
  2969. +
  2970. + err = -EBUSY;
  2971. + mnt_flags = au_mntflags(sb);
  2972. + verbose = !!au_opt_test(mnt_flags, VERBOSE);
  2973. + bbot = au_sbbot(sb);
  2974. + if (unlikely(!bbot)) {
  2975. + AuVerbose(verbose, "no more branches left\n");
  2976. + goto out;
  2977. + }
  2978. +
  2979. + br = au_sbr(sb, bindex);
  2980. + AuDebugOn(!path_equal(&br->br_path, &del->h_path));
  2981. + if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
  2982. + AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
  2983. + goto out;
  2984. + }
  2985. +
  2986. + br_id = br->br_id;
  2987. + opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
  2988. + if (unlikely(opened)) {
  2989. + to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
  2990. + err = PTR_ERR(to_free);
  2991. + if (IS_ERR(to_free))
  2992. + goto out;
  2993. +
  2994. + err = test_file_busy(sb, br_id, to_free, opened);
  2995. + if (unlikely(err)) {
  2996. + AuVerbose(verbose, "%llu file(s) opened\n", opened);
  2997. + goto out;
  2998. + }
  2999. + }
  3000. +
  3001. + wbr = br->br_wbr;
  3002. + do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
  3003. + if (do_wh) {
  3004. + /* instead of WbrWhMustWriteLock(wbr) */
  3005. + SiMustWriteLock(sb);
  3006. + for (i = 0; i < AuBrWh_Last; i++) {
  3007. + dput(wbr->wbr_wh[i]);
  3008. + wbr->wbr_wh[i] = NULL;
  3009. + }
  3010. + }
  3011. +
  3012. + err = test_children_busy(root, bindex, verbose);
  3013. + if (unlikely(err)) {
  3014. + if (do_wh)
  3015. + goto out_wh;
  3016. + goto out;
  3017. + }
  3018. +
  3019. + err = 0;
  3020. + if (to_free) {
  3021. + /*
  3022. + * now we confirmed the branch is deletable.
  3023. + * let's free the remaining opened dirs on the branch.
  3024. + */
  3025. + di_write_unlock(root);
  3026. + br_del_file(to_free, opened, br_id);
  3027. + di_write_lock_child(root);
  3028. + }
  3029. +
  3030. + sysaufs_brs_del(sb, bindex); /* remove successors */
  3031. + dbgaufs_xino_del(br); /* remove one */
  3032. + au_br_do_del(sb, bindex, br);
  3033. + sysaufs_brs_add(sb, bindex); /* append successors */
  3034. + dbgaufs_brs_add(sb, bindex, /*topdown*/1); /* rename successors */
  3035. +
  3036. + if (!bindex) {
  3037. + au_cpup_attr_all(d_inode(root), /*force*/1);
  3038. + sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
  3039. + } else
  3040. + au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
  3041. + if (au_opt_test(mnt_flags, PLINK))
  3042. + au_plink_half_refresh(sb, br_id);
  3043. +
  3044. + goto out; /* success */
  3045. +
  3046. +out_wh:
  3047. + /* revert */
  3048. + rerr = au_br_init_wh(sb, br, br->br_perm);
  3049. + if (rerr)
  3050. + pr_warn("failed re-creating base whiteout, %s. (%d)\n",
  3051. + del->pathname, rerr);
  3052. +out:
  3053. + if (to_free)
  3054. + au_farray_free(to_free, opened);
  3055. + return err;
  3056. +}
  3057. +
  3058. +/* ---------------------------------------------------------------------- */
  3059. +
  3060. +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
  3061. +{
  3062. + int err;
  3063. + aufs_bindex_t btop, bbot;
  3064. + struct aufs_ibusy ibusy;
  3065. + struct inode *inode, *h_inode;
  3066. +
  3067. + err = -EPERM;
  3068. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  3069. + goto out;
  3070. +
  3071. + err = copy_from_user(&ibusy, arg, sizeof(ibusy));
  3072. + if (!err)
  3073. + err = !access_ok(VERIFY_WRITE, &arg->h_ino, sizeof(arg->h_ino));
  3074. + if (unlikely(err)) {
  3075. + err = -EFAULT;
  3076. + AuTraceErr(err);
  3077. + goto out;
  3078. + }
  3079. +
  3080. + err = -EINVAL;
  3081. + si_read_lock(sb, AuLock_FLUSH);
  3082. + if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
  3083. + goto out_unlock;
  3084. +
  3085. + err = 0;
  3086. + ibusy.h_ino = 0; /* invalid */
  3087. + inode = ilookup(sb, ibusy.ino);
  3088. + if (!inode
  3089. + || inode->i_ino == AUFS_ROOT_INO
  3090. + || au_is_bad_inode(inode))
  3091. + goto out_unlock;
  3092. +
  3093. + ii_read_lock_child(inode);
  3094. + btop = au_ibtop(inode);
  3095. + bbot = au_ibbot(inode);
  3096. + if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
  3097. + h_inode = au_h_iptr(inode, ibusy.bindex);
  3098. + if (h_inode && au_test_ibusy(inode, btop, bbot))
  3099. + ibusy.h_ino = h_inode->i_ino;
  3100. + }
  3101. + ii_read_unlock(inode);
  3102. + iput(inode);
  3103. +
  3104. +out_unlock:
  3105. + si_read_unlock(sb);
  3106. + if (!err) {
  3107. + err = __put_user(ibusy.h_ino, &arg->h_ino);
  3108. + if (unlikely(err)) {
  3109. + err = -EFAULT;
  3110. + AuTraceErr(err);
  3111. + }
  3112. + }
  3113. +out:
  3114. + return err;
  3115. +}
  3116. +
  3117. +long au_ibusy_ioctl(struct file *file, unsigned long arg)
  3118. +{
  3119. + return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
  3120. +}
  3121. +
  3122. +#ifdef CONFIG_COMPAT
  3123. +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
  3124. +{
  3125. + return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
  3126. +}
  3127. +#endif
  3128. +
  3129. +/* ---------------------------------------------------------------------- */
  3130. +
  3131. +/*
  3132. + * change a branch permission
  3133. + */
  3134. +
  3135. +static void au_warn_ima(void)
  3136. +{
  3137. +#ifdef CONFIG_IMA
  3138. + /* since it doesn't support mark_files_ro() */
  3139. + AuWarn1("RW -> RO makes IMA to produce wrong message\n");
  3140. +#endif
  3141. +}
  3142. +
  3143. +static int do_need_sigen_inc(int a, int b)
  3144. +{
  3145. + return au_br_whable(a) && !au_br_whable(b);
  3146. +}
  3147. +
  3148. +static int need_sigen_inc(int old, int new)
  3149. +{
  3150. + return do_need_sigen_inc(old, new)
  3151. + || do_need_sigen_inc(new, old);
  3152. +}
  3153. +
  3154. +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
  3155. +{
  3156. + int err, do_warn;
  3157. + unsigned int mnt_flags;
  3158. + unsigned long long ull, max;
  3159. + aufs_bindex_t br_id;
  3160. + unsigned char verbose, writer;
  3161. + struct file *file, *hf, **array;
  3162. + struct au_hfile *hfile;
  3163. +
  3164. + mnt_flags = au_mntflags(sb);
  3165. + verbose = !!au_opt_test(mnt_flags, VERBOSE);
  3166. +
  3167. + array = au_farray_alloc(sb, &max);
  3168. + err = PTR_ERR(array);
  3169. + if (IS_ERR(array))
  3170. + goto out;
  3171. +
  3172. + do_warn = 0;
  3173. + br_id = au_sbr_id(sb, bindex);
  3174. + for (ull = 0; ull < max; ull++) {
  3175. + file = array[ull];
  3176. + if (unlikely(!file))
  3177. + break;
  3178. +
  3179. + /* AuDbg("%pD\n", file); */
  3180. + fi_read_lock(file);
  3181. + if (unlikely(au_test_mmapped(file))) {
  3182. + err = -EBUSY;
  3183. + AuVerbose(verbose, "mmapped %pD\n", file);
  3184. + AuDbgFile(file);
  3185. + FiMustNoWaiters(file);
  3186. + fi_read_unlock(file);
  3187. + goto out_array;
  3188. + }
  3189. +
  3190. + hfile = &au_fi(file)->fi_htop;
  3191. + hf = hfile->hf_file;
  3192. + if (!d_is_reg(file->f_path.dentry)
  3193. + || !(file->f_mode & FMODE_WRITE)
  3194. + || hfile->hf_br->br_id != br_id
  3195. + || !(hf->f_mode & FMODE_WRITE))
  3196. + array[ull] = NULL;
  3197. + else {
  3198. + do_warn = 1;
  3199. + get_file(file);
  3200. + }
  3201. +
  3202. + FiMustNoWaiters(file);
  3203. + fi_read_unlock(file);
  3204. + fput(file);
  3205. + }
  3206. +
  3207. + err = 0;
  3208. + if (do_warn)
  3209. + au_warn_ima();
  3210. +
  3211. + for (ull = 0; ull < max; ull++) {
  3212. + file = array[ull];
  3213. + if (!file)
  3214. + continue;
  3215. +
  3216. + /* todo: already flushed? */
  3217. + /*
  3218. + * fs/super.c:mark_files_ro() is gone, but aufs keeps its
  3219. + * approach which resets f_mode and calls mnt_drop_write() and
  3220. + * file_release_write() for each file, because the branch
  3221. + * attribute in aufs world is totally different from the native
  3222. + * fs rw/ro mode.
  3223. + */
  3224. + /* fi_read_lock(file); */
  3225. + hfile = &au_fi(file)->fi_htop;
  3226. + hf = hfile->hf_file;
  3227. + /* fi_read_unlock(file); */
  3228. + spin_lock(&hf->f_lock);
  3229. + writer = !!(hf->f_mode & FMODE_WRITER);
  3230. + hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
  3231. + spin_unlock(&hf->f_lock);
  3232. + if (writer) {
  3233. + put_write_access(file_inode(hf));
  3234. + __mnt_drop_write(hf->f_path.mnt);
  3235. + }
  3236. + }
  3237. +
  3238. +out_array:
  3239. + au_farray_free(array, max);
  3240. +out:
  3241. + AuTraceErr(err);
  3242. + return err;
  3243. +}
  3244. +
  3245. +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
  3246. + int *do_refresh)
  3247. +{
  3248. + int err, rerr;
  3249. + aufs_bindex_t bindex;
  3250. + struct dentry *root;
  3251. + struct au_branch *br;
  3252. + struct au_br_fhsm *bf;
  3253. +
  3254. + root = sb->s_root;
  3255. + bindex = au_find_dbindex(root, mod->h_root);
  3256. + if (bindex < 0) {
  3257. + if (remount)
  3258. + return 0; /* success */
  3259. + err = -ENOENT;
  3260. + pr_err("%s no such branch\n", mod->path);
  3261. + goto out;
  3262. + }
  3263. + AuDbg("bindex b%d\n", bindex);
  3264. +
  3265. + err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
  3266. + if (unlikely(err))
  3267. + goto out;
  3268. +
  3269. + br = au_sbr(sb, bindex);
  3270. + AuDebugOn(mod->h_root != au_br_dentry(br));
  3271. + if (br->br_perm == mod->perm)
  3272. + return 0; /* success */
  3273. +
  3274. + /* pre-allocate for non-fhsm --> fhsm */
  3275. + bf = NULL;
  3276. + if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
  3277. + err = au_fhsm_br_alloc(br);
  3278. + if (unlikely(err))
  3279. + goto out;
  3280. + bf = br->br_fhsm;
  3281. + br->br_fhsm = NULL;
  3282. + }
  3283. +
  3284. + if (au_br_writable(br->br_perm)) {
  3285. + /* remove whiteout base */
  3286. + err = au_br_init_wh(sb, br, mod->perm);
  3287. + if (unlikely(err))
  3288. + goto out_bf;
  3289. +
  3290. + if (!au_br_writable(mod->perm)) {
  3291. + /* rw --> ro, file might be mmapped */
  3292. + DiMustNoWaiters(root);
  3293. + IiMustNoWaiters(d_inode(root));
  3294. + di_write_unlock(root);
  3295. + err = au_br_mod_files_ro(sb, bindex);
  3296. + /* aufs_write_lock() calls ..._child() */
  3297. + di_write_lock_child(root);
  3298. +
  3299. + if (unlikely(err)) {
  3300. + rerr = -ENOMEM;
  3301. + br->br_wbr = kzalloc(sizeof(*br->br_wbr),
  3302. + GFP_NOFS);
  3303. + if (br->br_wbr)
  3304. + rerr = au_wbr_init(br, sb, br->br_perm);
  3305. + if (unlikely(rerr)) {
  3306. + AuIOErr("nested error %d (%d)\n",
  3307. + rerr, err);
  3308. + br->br_perm = mod->perm;
  3309. + }
  3310. + }
  3311. + }
  3312. + } else if (au_br_writable(mod->perm)) {
  3313. + /* ro --> rw */
  3314. + err = -ENOMEM;
  3315. + br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
  3316. + if (br->br_wbr) {
  3317. + err = au_wbr_init(br, sb, mod->perm);
  3318. + if (unlikely(err)) {
  3319. + kfree(br->br_wbr);
  3320. + br->br_wbr = NULL;
  3321. + }
  3322. + }
  3323. + }
  3324. + if (unlikely(err))
  3325. + goto out_bf;
  3326. +
  3327. + if (au_br_fhsm(br->br_perm)) {
  3328. + if (!au_br_fhsm(mod->perm)) {
  3329. + /* fhsm --> non-fhsm */
  3330. + au_br_fhsm_fin(br->br_fhsm);
  3331. + kfree(br->br_fhsm);
  3332. + br->br_fhsm = NULL;
  3333. + }
  3334. + } else if (au_br_fhsm(mod->perm))
  3335. + /* non-fhsm --> fhsm */
  3336. + br->br_fhsm = bf;
  3337. +
  3338. + *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
  3339. + br->br_perm = mod->perm;
  3340. + goto out; /* success */
  3341. +
  3342. +out_bf:
  3343. + kfree(bf);
  3344. +out:
  3345. + AuTraceErr(err);
  3346. + return err;
  3347. +}
  3348. +
  3349. +/* ---------------------------------------------------------------------- */
  3350. +
  3351. +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
  3352. +{
  3353. + int err;
  3354. + struct kstatfs kstfs;
  3355. +
  3356. + err = vfs_statfs(&br->br_path, &kstfs);
  3357. + if (!err) {
  3358. + stfs->f_blocks = kstfs.f_blocks;
  3359. + stfs->f_bavail = kstfs.f_bavail;
  3360. + stfs->f_files = kstfs.f_files;
  3361. + stfs->f_ffree = kstfs.f_ffree;
  3362. + }
  3363. +
  3364. + return err;
  3365. +}
  3366. --- /dev/null
  3367. +++ linux-4.19/fs/aufs/branch.h 2018-11-20 09:35:15.009195503 +0200
  3368. @@ -0,0 +1,365 @@
  3369. +/* SPDX-License-Identifier: GPL-2.0 */
  3370. +/*
  3371. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  3372. + *
  3373. + * This program, aufs is free software; you can redistribute it and/or modify
  3374. + * it under the terms of the GNU General Public License as published by
  3375. + * the Free Software Foundation; either version 2 of the License, or
  3376. + * (at your option) any later version.
  3377. + *
  3378. + * This program is distributed in the hope that it will be useful,
  3379. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3380. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3381. + * GNU General Public License for more details.
  3382. + *
  3383. + * You should have received a copy of the GNU General Public License
  3384. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  3385. + */
  3386. +
  3387. +/*
  3388. + * branch filesystems and xino for them
  3389. + */
  3390. +
  3391. +#ifndef __AUFS_BRANCH_H__
  3392. +#define __AUFS_BRANCH_H__
  3393. +
  3394. +#ifdef __KERNEL__
  3395. +
  3396. +#include <linux/mount.h>
  3397. +#include "dirren.h"
  3398. +#include "dynop.h"
  3399. +#include "lcnt.h"
  3400. +#include "rwsem.h"
  3401. +#include "super.h"
  3402. +
  3403. +/* ---------------------------------------------------------------------- */
  3404. +
  3405. +/* a xino file */
  3406. +struct au_xino {
  3407. + struct file **xi_file;
  3408. + unsigned int xi_nfile;
  3409. +
  3410. + struct {
  3411. + spinlock_t spin;
  3412. + ino_t *array;
  3413. + int total;
  3414. + /* reserved for future use */
  3415. + /* unsigned long *bitmap; */
  3416. + wait_queue_head_t wqh;
  3417. + } xi_nondir;
  3418. +
  3419. + struct mutex xi_mtx; /* protects xi_file array */
  3420. + struct hlist_bl_head xi_writing;
  3421. +
  3422. + atomic_t xi_truncating;
  3423. +
  3424. + struct kref xi_kref;
  3425. +};
  3426. +
  3427. +/* File-based Hierarchical Storage Management */
  3428. +struct au_br_fhsm {
  3429. +#ifdef CONFIG_AUFS_FHSM
  3430. + struct mutex bf_lock;
  3431. + unsigned long bf_jiffy;
  3432. + struct aufs_stfs bf_stfs;
  3433. + int bf_readable;
  3434. +#endif
  3435. +};
  3436. +
  3437. +/* members for writable branch only */
  3438. +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
  3439. +struct au_wbr {
  3440. + struct au_rwsem wbr_wh_rwsem;
  3441. + struct dentry *wbr_wh[AuBrWh_Last];
  3442. + atomic_t wbr_wh_running;
  3443. +#define wbr_whbase wbr_wh[AuBrWh_BASE] /* whiteout base */
  3444. +#define wbr_plink wbr_wh[AuBrWh_PLINK] /* pseudo-link dir */
  3445. +#define wbr_orph wbr_wh[AuBrWh_ORPH] /* dir for orphans */
  3446. +
  3447. + /* mfs mode */
  3448. + unsigned long long wbr_bytes;
  3449. +};
  3450. +
  3451. +/* ext2 has 3 types of operations at least, ext3 has 4 */
  3452. +#define AuBrDynOp (AuDyLast * 4)
  3453. +
  3454. +#ifdef CONFIG_AUFS_HFSNOTIFY
  3455. +/* support for asynchronous destruction */
  3456. +struct au_br_hfsnotify {
  3457. + struct fsnotify_group *hfsn_group;
  3458. +};
  3459. +#endif
  3460. +
  3461. +/* sysfs entries */
  3462. +struct au_brsysfs {
  3463. + char name[16];
  3464. + struct attribute attr;
  3465. +};
  3466. +
  3467. +enum {
  3468. + AuBrSysfs_BR,
  3469. + AuBrSysfs_BRID,
  3470. + AuBrSysfs_Last
  3471. +};
  3472. +
  3473. +/* protected by superblock rwsem */
  3474. +struct au_branch {
  3475. + struct au_xino *br_xino;
  3476. +
  3477. + aufs_bindex_t br_id;
  3478. +
  3479. + int br_perm;
  3480. + struct path br_path;
  3481. + spinlock_t br_dykey_lock;
  3482. + struct au_dykey *br_dykey[AuBrDynOp];
  3483. + au_lcnt_t br_nfiles; /* opened files */
  3484. + au_lcnt_t br_count; /* in-use for other */
  3485. +
  3486. + struct au_wbr *br_wbr;
  3487. + struct au_br_fhsm *br_fhsm;
  3488. +
  3489. +#ifdef CONFIG_AUFS_HFSNOTIFY
  3490. + struct au_br_hfsnotify *br_hfsn;
  3491. +#endif
  3492. +
  3493. +#ifdef CONFIG_SYSFS
  3494. + /* entries under sysfs per mount-point */
  3495. + struct au_brsysfs br_sysfs[AuBrSysfs_Last];
  3496. +#endif
  3497. +
  3498. +#ifdef CONFIG_DEBUG_FS
  3499. + struct dentry *br_dbgaufs; /* xino */
  3500. +#endif
  3501. +
  3502. + struct au_dr_br br_dirren;
  3503. +};
  3504. +
  3505. +/* ---------------------------------------------------------------------- */
  3506. +
  3507. +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
  3508. +{
  3509. + return br->br_path.mnt;
  3510. +}
  3511. +
  3512. +static inline struct dentry *au_br_dentry(struct au_branch *br)
  3513. +{
  3514. + return br->br_path.dentry;
  3515. +}
  3516. +
  3517. +static inline struct super_block *au_br_sb(struct au_branch *br)
  3518. +{
  3519. + return au_br_mnt(br)->mnt_sb;
  3520. +}
  3521. +
  3522. +static inline int au_br_rdonly(struct au_branch *br)
  3523. +{
  3524. + return (sb_rdonly(au_br_sb(br))
  3525. + || !au_br_writable(br->br_perm))
  3526. + ? -EROFS : 0;
  3527. +}
  3528. +
  3529. +static inline int au_br_hnotifyable(int brperm __maybe_unused)
  3530. +{
  3531. +#ifdef CONFIG_AUFS_HNOTIFY
  3532. + return !(brperm & AuBrPerm_RR);
  3533. +#else
  3534. + return 0;
  3535. +#endif
  3536. +}
  3537. +
  3538. +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
  3539. +{
  3540. + int err, exec_flag;
  3541. +
  3542. + err = 0;
  3543. + exec_flag = oflag & __FMODE_EXEC;
  3544. + if (unlikely(exec_flag && path_noexec(&br->br_path)))
  3545. + err = -EACCES;
  3546. +
  3547. + return err;
  3548. +}
  3549. +
  3550. +static inline void au_xino_get(struct au_branch *br)
  3551. +{
  3552. + struct au_xino *xi;
  3553. +
  3554. + xi = br->br_xino;
  3555. + if (xi)
  3556. + kref_get(&xi->xi_kref);
  3557. +}
  3558. +
  3559. +static inline int au_xino_count(struct au_branch *br)
  3560. +{
  3561. + int v;
  3562. + struct au_xino *xi;
  3563. +
  3564. + v = 0;
  3565. + xi = br->br_xino;
  3566. + if (xi)
  3567. + v = kref_read(&xi->xi_kref);
  3568. +
  3569. + return v;
  3570. +}
  3571. +
  3572. +/* ---------------------------------------------------------------------- */
  3573. +
  3574. +/* branch.c */
  3575. +struct au_sbinfo;
  3576. +void au_br_free(struct au_sbinfo *sinfo);
  3577. +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
  3578. +struct au_opt_add;
  3579. +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
  3580. +struct au_opt_del;
  3581. +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
  3582. +long au_ibusy_ioctl(struct file *file, unsigned long arg);
  3583. +#ifdef CONFIG_COMPAT
  3584. +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
  3585. +#endif
  3586. +struct au_opt_mod;
  3587. +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
  3588. + int *do_refresh);
  3589. +struct aufs_stfs;
  3590. +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
  3591. +
  3592. +/* xino.c */
  3593. +static const loff_t au_loff_max = LLONG_MAX;
  3594. +
  3595. +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
  3596. +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent);
  3597. +struct file *au_xino_create2(struct super_block *sb, struct path *base,
  3598. + struct file *copy_src);
  3599. +struct au_xi_new {
  3600. + struct au_xino *xi; /* switch between xino and xigen */
  3601. + int idx;
  3602. + struct path *base;
  3603. + struct file *copy_src;
  3604. +};
  3605. +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
  3606. +
  3607. +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  3608. + ino_t *ino);
  3609. +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  3610. + ino_t ino);
  3611. +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *buf, size_t size,
  3612. + loff_t *pos);
  3613. +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
  3614. + size_t size, loff_t *pos);
  3615. +
  3616. +int au_xib_trunc(struct super_block *sb);
  3617. +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
  3618. +
  3619. +struct au_xino *au_xino_alloc(unsigned int nfile);
  3620. +int au_xino_put(struct au_branch *br);
  3621. +struct file *au_xino_file1(struct au_xino *xi);
  3622. +
  3623. +struct au_opt_xino;
  3624. +void au_xino_clr(struct super_block *sb);
  3625. +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
  3626. +struct file *au_xino_def(struct super_block *sb);
  3627. +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
  3628. + struct path *base);
  3629. +
  3630. +ino_t au_xino_new_ino(struct super_block *sb);
  3631. +void au_xino_delete_inode(struct inode *inode, const int unlinked);
  3632. +
  3633. +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
  3634. + ino_t h_ino, int idx);
  3635. +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  3636. + int *idx);
  3637. +
  3638. +int au_xino_path(struct seq_file *seq, struct file *file);
  3639. +
  3640. +/* ---------------------------------------------------------------------- */
  3641. +
  3642. +/* @idx is signed to accept -1 meaning the first file */
  3643. +static inline struct file *au_xino_file(struct au_xino *xi, int idx)
  3644. +{
  3645. + struct file *file;
  3646. +
  3647. + file = NULL;
  3648. + if (!xi)
  3649. + goto out;
  3650. +
  3651. + if (idx >= 0) {
  3652. + if (idx < xi->xi_nfile)
  3653. + file = xi->xi_file[idx];
  3654. + } else
  3655. + file = au_xino_file1(xi);
  3656. +
  3657. +out:
  3658. + return file;
  3659. +}
  3660. +
  3661. +/* ---------------------------------------------------------------------- */
  3662. +
  3663. +/* Superblock to branch */
  3664. +static inline
  3665. +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
  3666. +{
  3667. + return au_sbr(sb, bindex)->br_id;
  3668. +}
  3669. +
  3670. +static inline
  3671. +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
  3672. +{
  3673. + return au_br_mnt(au_sbr(sb, bindex));
  3674. +}
  3675. +
  3676. +static inline
  3677. +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
  3678. +{
  3679. + return au_br_sb(au_sbr(sb, bindex));
  3680. +}
  3681. +
  3682. +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
  3683. +{
  3684. + return au_sbr(sb, bindex)->br_perm;
  3685. +}
  3686. +
  3687. +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
  3688. +{
  3689. + return au_br_whable(au_sbr_perm(sb, bindex));
  3690. +}
  3691. +
  3692. +/* ---------------------------------------------------------------------- */
  3693. +
  3694. +#define wbr_wh_read_lock(wbr) au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
  3695. +#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
  3696. +#define wbr_wh_read_trylock(wbr) au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
  3697. +#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
  3698. +/*
  3699. +#define wbr_wh_read_trylock_nested(wbr) \
  3700. + au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
  3701. +#define wbr_wh_write_trylock_nested(wbr) \
  3702. + au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
  3703. +*/
  3704. +
  3705. +#define wbr_wh_read_unlock(wbr) au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
  3706. +#define wbr_wh_write_unlock(wbr) au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
  3707. +#define wbr_wh_downgrade_lock(wbr) au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
  3708. +
  3709. +#define WbrWhMustNoWaiters(wbr) AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
  3710. +#define WbrWhMustAnyLock(wbr) AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
  3711. +#define WbrWhMustWriteLock(wbr) AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
  3712. +
  3713. +/* ---------------------------------------------------------------------- */
  3714. +
  3715. +#ifdef CONFIG_AUFS_FHSM
  3716. +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
  3717. +{
  3718. + mutex_init(&brfhsm->bf_lock);
  3719. + brfhsm->bf_jiffy = 0;
  3720. + brfhsm->bf_readable = 0;
  3721. +}
  3722. +
  3723. +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
  3724. +{
  3725. + mutex_destroy(&brfhsm->bf_lock);
  3726. +}
  3727. +#else
  3728. +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
  3729. +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
  3730. +#endif
  3731. +
  3732. +#endif /* __KERNEL__ */
  3733. +#endif /* __AUFS_BRANCH_H__ */
  3734. --- /dev/null
  3735. +++ linux-4.19/fs/aufs/conf.mk 2018-11-20 09:35:15.010195503 +0200
  3736. @@ -0,0 +1,40 @@
  3737. +# SPDX-License-Identifier: GPL-2.0
  3738. +
  3739. +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
  3740. +
  3741. +define AuConf
  3742. +ifdef ${1}
  3743. +AuConfStr += ${1}=${${1}}
  3744. +endif
  3745. +endef
  3746. +
  3747. +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
  3748. + SBILIST \
  3749. + HNOTIFY HFSNOTIFY \
  3750. + EXPORT INO_T_64 \
  3751. + XATTR \
  3752. + FHSM \
  3753. + RDU \
  3754. + DIRREN \
  3755. + SHWH \
  3756. + BR_RAMFS \
  3757. + BR_FUSE POLL \
  3758. + BR_HFSPLUS \
  3759. + BDEV_LOOP \
  3760. + DEBUG MAGIC_SYSRQ
  3761. +$(foreach i, ${AuConfAll}, \
  3762. + $(eval $(call AuConf,CONFIG_AUFS_${i})))
  3763. +
  3764. +AuConfName = ${obj}/conf.str
  3765. +${AuConfName}.tmp: FORCE
  3766. + @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
  3767. +${AuConfName}: ${AuConfName}.tmp
  3768. + @diff -q $< $@ > /dev/null 2>&1 || { \
  3769. + echo ' GEN ' $@; \
  3770. + cp -p $< $@; \
  3771. + }
  3772. +FORCE:
  3773. +clean-files += ${AuConfName} ${AuConfName}.tmp
  3774. +${obj}/sysfs.o: ${AuConfName}
  3775. +
  3776. +-include ${srctree}/${src}/conf_priv.mk
  3777. --- /dev/null
  3778. +++ linux-4.19/fs/aufs/cpup.c 2018-11-20 09:35:15.011195503 +0200
  3779. @@ -0,0 +1,1445 @@
  3780. +// SPDX-License-Identifier: GPL-2.0
  3781. +/*
  3782. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  3783. + *
  3784. + * This program, aufs is free software; you can redistribute it and/or modify
  3785. + * it under the terms of the GNU General Public License as published by
  3786. + * the Free Software Foundation; either version 2 of the License, or
  3787. + * (at your option) any later version.
  3788. + *
  3789. + * This program is distributed in the hope that it will be useful,
  3790. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3791. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3792. + * GNU General Public License for more details.
  3793. + *
  3794. + * You should have received a copy of the GNU General Public License
  3795. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  3796. + */
  3797. +
  3798. +/*
  3799. + * copy-up functions, see wbr_policy.c for copy-down
  3800. + */
  3801. +
  3802. +#include <linux/fs_stack.h>
  3803. +#include <linux/mm.h>
  3804. +#include <linux/task_work.h>
  3805. +#include "aufs.h"
  3806. +
  3807. +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
  3808. +{
  3809. + const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
  3810. + | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
  3811. +
  3812. + BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
  3813. +
  3814. + dst->i_flags |= iflags & ~mask;
  3815. + if (au_test_fs_notime(dst->i_sb))
  3816. + dst->i_flags |= S_NOATIME | S_NOCMTIME;
  3817. +}
  3818. +
  3819. +void au_cpup_attr_timesizes(struct inode *inode)
  3820. +{
  3821. + struct inode *h_inode;
  3822. +
  3823. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  3824. + fsstack_copy_attr_times(inode, h_inode);
  3825. + fsstack_copy_inode_size(inode, h_inode);
  3826. +}
  3827. +
  3828. +void au_cpup_attr_nlink(struct inode *inode, int force)
  3829. +{
  3830. + struct inode *h_inode;
  3831. + struct super_block *sb;
  3832. + aufs_bindex_t bindex, bbot;
  3833. +
  3834. + sb = inode->i_sb;
  3835. + bindex = au_ibtop(inode);
  3836. + h_inode = au_h_iptr(inode, bindex);
  3837. + if (!force
  3838. + && !S_ISDIR(h_inode->i_mode)
  3839. + && au_opt_test(au_mntflags(sb), PLINK)
  3840. + && au_plink_test(inode))
  3841. + return;
  3842. +
  3843. + /*
  3844. + * 0 can happen in revalidating.
  3845. + * h_inode->i_mutex may not be held here, but it is harmless since once
  3846. + * i_nlink reaches 0, it will never become positive except O_TMPFILE
  3847. + * case.
  3848. + * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
  3849. + * the incorrect link count.
  3850. + */
  3851. + set_nlink(inode, h_inode->i_nlink);
  3852. +
  3853. + /*
  3854. + * fewer nlink makes find(1) noisy, but larger nlink doesn't.
  3855. + * it may includes whplink directory.
  3856. + */
  3857. + if (S_ISDIR(h_inode->i_mode)) {
  3858. + bbot = au_ibbot(inode);
  3859. + for (bindex++; bindex <= bbot; bindex++) {
  3860. + h_inode = au_h_iptr(inode, bindex);
  3861. + if (h_inode)
  3862. + au_add_nlink(inode, h_inode);
  3863. + }
  3864. + }
  3865. +}
  3866. +
  3867. +void au_cpup_attr_changeable(struct inode *inode)
  3868. +{
  3869. + struct inode *h_inode;
  3870. +
  3871. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  3872. + inode->i_mode = h_inode->i_mode;
  3873. + inode->i_uid = h_inode->i_uid;
  3874. + inode->i_gid = h_inode->i_gid;
  3875. + au_cpup_attr_timesizes(inode);
  3876. + au_cpup_attr_flags(inode, h_inode->i_flags);
  3877. +}
  3878. +
  3879. +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
  3880. +{
  3881. + struct au_iinfo *iinfo = au_ii(inode);
  3882. +
  3883. + IiMustWriteLock(inode);
  3884. +
  3885. + iinfo->ii_higen = h_inode->i_generation;
  3886. + iinfo->ii_hsb1 = h_inode->i_sb;
  3887. +}
  3888. +
  3889. +void au_cpup_attr_all(struct inode *inode, int force)
  3890. +{
  3891. + struct inode *h_inode;
  3892. +
  3893. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  3894. + au_cpup_attr_changeable(inode);
  3895. + if (inode->i_nlink > 0)
  3896. + au_cpup_attr_nlink(inode, force);
  3897. + inode->i_rdev = h_inode->i_rdev;
  3898. + inode->i_blkbits = h_inode->i_blkbits;
  3899. + au_cpup_igen(inode, h_inode);
  3900. +}
  3901. +
  3902. +/* ---------------------------------------------------------------------- */
  3903. +
  3904. +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
  3905. +
  3906. +/* keep the timestamps of the parent dir when cpup */
  3907. +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
  3908. + struct path *h_path)
  3909. +{
  3910. + struct inode *h_inode;
  3911. +
  3912. + dt->dt_dentry = dentry;
  3913. + dt->dt_h_path = *h_path;
  3914. + h_inode = d_inode(h_path->dentry);
  3915. + dt->dt_atime = h_inode->i_atime;
  3916. + dt->dt_mtime = h_inode->i_mtime;
  3917. + /* smp_mb(); */
  3918. +}
  3919. +
  3920. +void au_dtime_revert(struct au_dtime *dt)
  3921. +{
  3922. + struct iattr attr;
  3923. + int err;
  3924. +
  3925. + attr.ia_atime = dt->dt_atime;
  3926. + attr.ia_mtime = dt->dt_mtime;
  3927. + attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
  3928. + | ATTR_ATIME | ATTR_ATIME_SET;
  3929. +
  3930. + /* no delegation since this is a directory */
  3931. + err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
  3932. + if (unlikely(err))
  3933. + pr_warn("restoring timestamps failed(%d). ignored\n", err);
  3934. +}
  3935. +
  3936. +/* ---------------------------------------------------------------------- */
  3937. +
  3938. +/* internal use only */
  3939. +struct au_cpup_reg_attr {
  3940. + int valid;
  3941. + struct kstat st;
  3942. + unsigned int iflags; /* inode->i_flags */
  3943. +};
  3944. +
  3945. +static noinline_for_stack
  3946. +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
  3947. + struct au_cpup_reg_attr *h_src_attr)
  3948. +{
  3949. + int err, sbits, icex;
  3950. + unsigned int mnt_flags;
  3951. + unsigned char verbose;
  3952. + struct iattr ia;
  3953. + struct path h_path;
  3954. + struct inode *h_isrc, *h_idst;
  3955. + struct kstat *h_st;
  3956. + struct au_branch *br;
  3957. +
  3958. + h_path.dentry = au_h_dptr(dst, bindex);
  3959. + h_idst = d_inode(h_path.dentry);
  3960. + br = au_sbr(dst->d_sb, bindex);
  3961. + h_path.mnt = au_br_mnt(br);
  3962. + h_isrc = d_inode(h_src);
  3963. + ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
  3964. + | ATTR_ATIME | ATTR_MTIME
  3965. + | ATTR_ATIME_SET | ATTR_MTIME_SET;
  3966. + if (h_src_attr && h_src_attr->valid) {
  3967. + h_st = &h_src_attr->st;
  3968. + ia.ia_uid = h_st->uid;
  3969. + ia.ia_gid = h_st->gid;
  3970. + ia.ia_atime = h_st->atime;
  3971. + ia.ia_mtime = h_st->mtime;
  3972. + if (h_idst->i_mode != h_st->mode
  3973. + && !S_ISLNK(h_idst->i_mode)) {
  3974. + ia.ia_valid |= ATTR_MODE;
  3975. + ia.ia_mode = h_st->mode;
  3976. + }
  3977. + sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
  3978. + au_cpup_attr_flags(h_idst, h_src_attr->iflags);
  3979. + } else {
  3980. + ia.ia_uid = h_isrc->i_uid;
  3981. + ia.ia_gid = h_isrc->i_gid;
  3982. + ia.ia_atime = h_isrc->i_atime;
  3983. + ia.ia_mtime = h_isrc->i_mtime;
  3984. + if (h_idst->i_mode != h_isrc->i_mode
  3985. + && !S_ISLNK(h_idst->i_mode)) {
  3986. + ia.ia_valid |= ATTR_MODE;
  3987. + ia.ia_mode = h_isrc->i_mode;
  3988. + }
  3989. + sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
  3990. + au_cpup_attr_flags(h_idst, h_isrc->i_flags);
  3991. + }
  3992. + /* no delegation since it is just created */
  3993. + err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
  3994. +
  3995. + /* is this nfs only? */
  3996. + if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
  3997. + ia.ia_valid = ATTR_FORCE | ATTR_MODE;
  3998. + ia.ia_mode = h_isrc->i_mode;
  3999. + err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
  4000. + }
  4001. +
  4002. + icex = br->br_perm & AuBrAttr_ICEX;
  4003. + if (!err) {
  4004. + mnt_flags = au_mntflags(dst->d_sb);
  4005. + verbose = !!au_opt_test(mnt_flags, VERBOSE);
  4006. + err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
  4007. + }
  4008. +
  4009. + return err;
  4010. +}
  4011. +
  4012. +/* ---------------------------------------------------------------------- */
  4013. +
  4014. +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
  4015. + char *buf, unsigned long blksize)
  4016. +{
  4017. + int err;
  4018. + size_t sz, rbytes, wbytes;
  4019. + unsigned char all_zero;
  4020. + char *p, *zp;
  4021. + struct inode *h_inode;
  4022. + /* reduce stack usage */
  4023. + struct iattr *ia;
  4024. +
  4025. + zp = page_address(ZERO_PAGE(0));
  4026. + if (unlikely(!zp))
  4027. + return -ENOMEM; /* possible? */
  4028. +
  4029. + err = 0;
  4030. + all_zero = 0;
  4031. + while (len) {
  4032. + AuDbg("len %lld\n", len);
  4033. + sz = blksize;
  4034. + if (len < blksize)
  4035. + sz = len;
  4036. +
  4037. + rbytes = 0;
  4038. + /* todo: signal_pending? */
  4039. + while (!rbytes || err == -EAGAIN || err == -EINTR) {
  4040. + rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
  4041. + err = rbytes;
  4042. + }
  4043. + if (unlikely(err < 0))
  4044. + break;
  4045. +
  4046. + all_zero = 0;
  4047. + if (len >= rbytes && rbytes == blksize)
  4048. + all_zero = !memcmp(buf, zp, rbytes);
  4049. + if (!all_zero) {
  4050. + wbytes = rbytes;
  4051. + p = buf;
  4052. + while (wbytes) {
  4053. + size_t b;
  4054. +
  4055. + b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
  4056. + err = b;
  4057. + /* todo: signal_pending? */
  4058. + if (unlikely(err == -EAGAIN || err == -EINTR))
  4059. + continue;
  4060. + if (unlikely(err < 0))
  4061. + break;
  4062. + wbytes -= b;
  4063. + p += b;
  4064. + }
  4065. + if (unlikely(err < 0))
  4066. + break;
  4067. + } else {
  4068. + loff_t res;
  4069. +
  4070. + AuLabel(hole);
  4071. + res = vfsub_llseek(dst, rbytes, SEEK_CUR);
  4072. + err = res;
  4073. + if (unlikely(res < 0))
  4074. + break;
  4075. + }
  4076. + len -= rbytes;
  4077. + err = 0;
  4078. + }
  4079. +
  4080. + /* the last block may be a hole */
  4081. + if (!err && all_zero) {
  4082. + AuLabel(last hole);
  4083. +
  4084. + err = 1;
  4085. + if (au_test_nfs(dst->f_path.dentry->d_sb)) {
  4086. + /* nfs requires this step to make last hole */
  4087. + /* is this only nfs? */
  4088. + do {
  4089. + /* todo: signal_pending? */
  4090. + err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
  4091. + } while (err == -EAGAIN || err == -EINTR);
  4092. + if (err == 1)
  4093. + dst->f_pos--;
  4094. + }
  4095. +
  4096. + if (err == 1) {
  4097. + ia = (void *)buf;
  4098. + ia->ia_size = dst->f_pos;
  4099. + ia->ia_valid = ATTR_SIZE | ATTR_FILE;
  4100. + ia->ia_file = dst;
  4101. + h_inode = file_inode(dst);
  4102. + inode_lock_nested(h_inode, AuLsc_I_CHILD2);
  4103. + /* no delegation since it is just created */
  4104. + err = vfsub_notify_change(&dst->f_path, ia,
  4105. + /*delegated*/NULL);
  4106. + inode_unlock(h_inode);
  4107. + }
  4108. + }
  4109. +
  4110. + return err;
  4111. +}
  4112. +
  4113. +int au_copy_file(struct file *dst, struct file *src, loff_t len)
  4114. +{
  4115. + int err;
  4116. + unsigned long blksize;
  4117. + unsigned char do_kfree;
  4118. + char *buf;
  4119. +
  4120. + err = -ENOMEM;
  4121. + blksize = dst->f_path.dentry->d_sb->s_blocksize;
  4122. + if (!blksize || PAGE_SIZE < blksize)
  4123. + blksize = PAGE_SIZE;
  4124. + AuDbg("blksize %lu\n", blksize);
  4125. + do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
  4126. + if (do_kfree)
  4127. + buf = kmalloc(blksize, GFP_NOFS);
  4128. + else
  4129. + buf = (void *)__get_free_page(GFP_NOFS);
  4130. + if (unlikely(!buf))
  4131. + goto out;
  4132. +
  4133. + if (len > (1 << 22))
  4134. + AuDbg("copying a large file %lld\n", (long long)len);
  4135. +
  4136. + src->f_pos = 0;
  4137. + dst->f_pos = 0;
  4138. + err = au_do_copy_file(dst, src, len, buf, blksize);
  4139. + if (do_kfree)
  4140. + kfree(buf);
  4141. + else
  4142. + free_page((unsigned long)buf);
  4143. +
  4144. +out:
  4145. + return err;
  4146. +}
  4147. +
  4148. +static int au_do_copy(struct file *dst, struct file *src, loff_t len)
  4149. +{
  4150. + int err;
  4151. + struct super_block *h_src_sb;
  4152. + struct inode *h_src_inode;
  4153. +
  4154. + h_src_inode = file_inode(src);
  4155. + h_src_sb = h_src_inode->i_sb;
  4156. +
  4157. + /* XFS acquires inode_lock */
  4158. + if (!au_test_xfs(h_src_sb))
  4159. + err = au_copy_file(dst, src, len);
  4160. + else {
  4161. + inode_unlock_shared(h_src_inode);
  4162. + err = au_copy_file(dst, src, len);
  4163. + inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
  4164. + }
  4165. +
  4166. + return err;
  4167. +}
  4168. +
  4169. +static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
  4170. +{
  4171. + int err;
  4172. + struct super_block *h_src_sb;
  4173. + struct inode *h_src_inode;
  4174. +
  4175. + h_src_inode = file_inode(src);
  4176. + h_src_sb = h_src_inode->i_sb;
  4177. + if (h_src_sb != file_inode(dst)->i_sb
  4178. + || !dst->f_op->clone_file_range) {
  4179. + err = au_do_copy(dst, src, len);
  4180. + goto out;
  4181. + }
  4182. +
  4183. + if (!au_test_nfs(h_src_sb)) {
  4184. + inode_unlock_shared(h_src_inode);
  4185. + err = vfsub_clone_file_range(src, dst, len);
  4186. + inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
  4187. + } else
  4188. + err = vfsub_clone_file_range(src, dst, len);
  4189. + /* older XFS has a condition in cloning */
  4190. + if (unlikely(err != -EOPNOTSUPP))
  4191. + goto out;
  4192. +
  4193. + /* the backend fs on NFS may not support cloning */
  4194. + err = au_do_copy(dst, src, len);
  4195. +
  4196. +out:
  4197. + AuTraceErr(err);
  4198. + return err;
  4199. +}
  4200. +
  4201. +/*
  4202. + * to support a sparse file which is opened with O_APPEND,
  4203. + * we need to close the file.
  4204. + */
  4205. +static int au_cp_regular(struct au_cp_generic *cpg)
  4206. +{
  4207. + int err, i;
  4208. + enum { SRC, DST };
  4209. + struct {
  4210. + aufs_bindex_t bindex;
  4211. + unsigned int flags;
  4212. + struct dentry *dentry;
  4213. + int force_wr;
  4214. + struct file *file;
  4215. + } *f, file[] = {
  4216. + {
  4217. + .bindex = cpg->bsrc,
  4218. + .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
  4219. + },
  4220. + {
  4221. + .bindex = cpg->bdst,
  4222. + .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
  4223. + .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
  4224. + }
  4225. + };
  4226. + struct au_branch *br;
  4227. + struct super_block *sb, *h_src_sb;
  4228. + struct inode *h_src_inode;
  4229. + struct task_struct *tsk = current;
  4230. +
  4231. + /* bsrc branch can be ro/rw. */
  4232. + sb = cpg->dentry->d_sb;
  4233. + f = file;
  4234. + for (i = 0; i < 2; i++, f++) {
  4235. + f->dentry = au_h_dptr(cpg->dentry, f->bindex);
  4236. + f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
  4237. + /*file*/NULL, f->force_wr);
  4238. + if (IS_ERR(f->file)) {
  4239. + err = PTR_ERR(f->file);
  4240. + if (i == SRC)
  4241. + goto out;
  4242. + else
  4243. + goto out_src;
  4244. + }
  4245. + }
  4246. +
  4247. + /* try stopping to update while we copyup */
  4248. + h_src_inode = d_inode(file[SRC].dentry);
  4249. + h_src_sb = h_src_inode->i_sb;
  4250. + if (!au_test_nfs(h_src_sb))
  4251. + IMustLock(h_src_inode);
  4252. + err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
  4253. +
  4254. + /* i wonder if we had O_NO_DELAY_FPUT flag */
  4255. + if (tsk->flags & PF_KTHREAD)
  4256. + __fput_sync(file[DST].file);
  4257. + else {
  4258. + /* it happened actually */
  4259. + fput(file[DST].file);
  4260. + /*
  4261. + * too bad.
  4262. + * we have to call both since we don't know which place the file
  4263. + * was added to.
  4264. + */
  4265. + task_work_run();
  4266. + flush_delayed_fput();
  4267. + }
  4268. + br = au_sbr(sb, file[DST].bindex);
  4269. + au_lcnt_dec(&br->br_nfiles);
  4270. +
  4271. +out_src:
  4272. + fput(file[SRC].file);
  4273. + br = au_sbr(sb, file[SRC].bindex);
  4274. + au_lcnt_dec(&br->br_nfiles);
  4275. +out:
  4276. + return err;
  4277. +}
  4278. +
  4279. +static int au_do_cpup_regular(struct au_cp_generic *cpg,
  4280. + struct au_cpup_reg_attr *h_src_attr)
  4281. +{
  4282. + int err, rerr;
  4283. + loff_t l;
  4284. + struct path h_path;
  4285. + struct inode *h_src_inode, *h_dst_inode;
  4286. +
  4287. + err = 0;
  4288. + h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
  4289. + l = i_size_read(h_src_inode);
  4290. + if (cpg->len == -1 || l < cpg->len)
  4291. + cpg->len = l;
  4292. + if (cpg->len) {
  4293. + /* try stopping to update while we are referencing */
  4294. + inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
  4295. + au_pin_hdir_unlock(cpg->pin);
  4296. +
  4297. + h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
  4298. + h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
  4299. + h_src_attr->iflags = h_src_inode->i_flags;
  4300. + if (!au_test_nfs(h_src_inode->i_sb))
  4301. + err = vfsub_getattr(&h_path, &h_src_attr->st);
  4302. + else {
  4303. + inode_unlock_shared(h_src_inode);
  4304. + err = vfsub_getattr(&h_path, &h_src_attr->st);
  4305. + inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
  4306. + }
  4307. + if (unlikely(err)) {
  4308. + inode_unlock_shared(h_src_inode);
  4309. + goto out;
  4310. + }
  4311. + h_src_attr->valid = 1;
  4312. + if (!au_test_nfs(h_src_inode->i_sb)) {
  4313. + err = au_cp_regular(cpg);
  4314. + inode_unlock_shared(h_src_inode);
  4315. + } else {
  4316. + inode_unlock_shared(h_src_inode);
  4317. + err = au_cp_regular(cpg);
  4318. + }
  4319. + rerr = au_pin_hdir_relock(cpg->pin);
  4320. + if (!err && rerr)
  4321. + err = rerr;
  4322. + }
  4323. + if (!err && (h_src_inode->i_state & I_LINKABLE)) {
  4324. + h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
  4325. + h_dst_inode = d_inode(h_path.dentry);
  4326. + spin_lock(&h_dst_inode->i_lock);
  4327. + h_dst_inode->i_state |= I_LINKABLE;
  4328. + spin_unlock(&h_dst_inode->i_lock);
  4329. + }
  4330. +
  4331. +out:
  4332. + return err;
  4333. +}
  4334. +
  4335. +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
  4336. + struct inode *h_dir)
  4337. +{
  4338. + int err, symlen;
  4339. + mm_segment_t old_fs;
  4340. + union {
  4341. + char *k;
  4342. + char __user *u;
  4343. + } sym;
  4344. +
  4345. + err = -ENOMEM;
  4346. + sym.k = (void *)__get_free_page(GFP_NOFS);
  4347. + if (unlikely(!sym.k))
  4348. + goto out;
  4349. +
  4350. + /* unnecessary to support mmap_sem since symlink is not mmap-able */
  4351. + old_fs = get_fs();
  4352. + set_fs(KERNEL_DS);
  4353. + symlen = vfs_readlink(h_src, sym.u, PATH_MAX);
  4354. + err = symlen;
  4355. + set_fs(old_fs);
  4356. +
  4357. + if (symlen > 0) {
  4358. + sym.k[symlen] = 0;
  4359. + err = vfsub_symlink(h_dir, h_path, sym.k);
  4360. + }
  4361. + free_page((unsigned long)sym.k);
  4362. +
  4363. +out:
  4364. + return err;
  4365. +}
  4366. +
  4367. +/*
  4368. + * regardless 'acl' option, reset all ACL.
  4369. + * All ACL will be copied up later from the original entry on the lower branch.
  4370. + */
  4371. +static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
  4372. +{
  4373. + int err;
  4374. + struct dentry *h_dentry;
  4375. + struct inode *h_inode;
  4376. +
  4377. + h_dentry = h_path->dentry;
  4378. + h_inode = d_inode(h_dentry);
  4379. + /* forget_all_cached_acls(h_inode)); */
  4380. + err = vfsub_removexattr(h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
  4381. + AuTraceErr(err);
  4382. + if (err == -EOPNOTSUPP)
  4383. + err = 0;
  4384. + if (!err)
  4385. + err = vfsub_acl_chmod(h_inode, mode);
  4386. +
  4387. + AuTraceErr(err);
  4388. + return err;
  4389. +}
  4390. +
  4391. +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
  4392. + struct inode *h_dir, struct path *h_path)
  4393. +{
  4394. + int err;
  4395. + struct inode *dir, *inode;
  4396. +
  4397. + err = vfsub_removexattr(h_path->dentry, XATTR_NAME_POSIX_ACL_DEFAULT);
  4398. + AuTraceErr(err);
  4399. + if (err == -EOPNOTSUPP)
  4400. + err = 0;
  4401. + if (unlikely(err))
  4402. + goto out;
  4403. +
  4404. + /*
  4405. + * strange behaviour from the users view,
  4406. + * particularly setattr case
  4407. + */
  4408. + dir = d_inode(dst_parent);
  4409. + if (au_ibtop(dir) == cpg->bdst)
  4410. + au_cpup_attr_nlink(dir, /*force*/1);
  4411. + inode = d_inode(cpg->dentry);
  4412. + au_cpup_attr_nlink(inode, /*force*/1);
  4413. +
  4414. +out:
  4415. + return err;
  4416. +}
  4417. +
  4418. +static noinline_for_stack
  4419. +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
  4420. + struct au_cpup_reg_attr *h_src_attr)
  4421. +{
  4422. + int err;
  4423. + umode_t mode;
  4424. + unsigned int mnt_flags;
  4425. + unsigned char isdir, isreg, force;
  4426. + const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
  4427. + struct au_dtime dt;
  4428. + struct path h_path;
  4429. + struct dentry *h_src, *h_dst, *h_parent;
  4430. + struct inode *h_inode, *h_dir;
  4431. + struct super_block *sb;
  4432. +
  4433. + /* bsrc branch can be ro/rw. */
  4434. + h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
  4435. + h_inode = d_inode(h_src);
  4436. + AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
  4437. +
  4438. + /* try stopping to be referenced while we are creating */
  4439. + h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
  4440. + if (au_ftest_cpup(cpg->flags, RENAME))
  4441. + AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
  4442. + AUFS_WH_PFX_LEN));
  4443. + h_parent = h_dst->d_parent; /* dir inode is locked */
  4444. + h_dir = d_inode(h_parent);
  4445. + IMustLock(h_dir);
  4446. + AuDebugOn(h_parent != h_dst->d_parent);
  4447. +
  4448. + sb = cpg->dentry->d_sb;
  4449. + h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
  4450. + if (do_dt) {
  4451. + h_path.dentry = h_parent;
  4452. + au_dtime_store(&dt, dst_parent, &h_path);
  4453. + }
  4454. + h_path.dentry = h_dst;
  4455. +
  4456. + isreg = 0;
  4457. + isdir = 0;
  4458. + mode = h_inode->i_mode;
  4459. + switch (mode & S_IFMT) {
  4460. + case S_IFREG:
  4461. + isreg = 1;
  4462. + err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
  4463. + if (!err)
  4464. + err = au_do_cpup_regular(cpg, h_src_attr);
  4465. + break;
  4466. + case S_IFDIR:
  4467. + isdir = 1;
  4468. + err = vfsub_mkdir(h_dir, &h_path, mode);
  4469. + if (!err)
  4470. + err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
  4471. + break;
  4472. + case S_IFLNK:
  4473. + err = au_do_cpup_symlink(&h_path, h_src, h_dir);
  4474. + break;
  4475. + case S_IFCHR:
  4476. + case S_IFBLK:
  4477. + AuDebugOn(!capable(CAP_MKNOD));
  4478. + /*FALLTHROUGH*/
  4479. + case S_IFIFO:
  4480. + case S_IFSOCK:
  4481. + err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
  4482. + break;
  4483. + default:
  4484. + AuIOErr("Unknown inode type 0%o\n", mode);
  4485. + err = -EIO;
  4486. + }
  4487. + if (!err)
  4488. + err = au_reset_acl(h_dir, &h_path, mode);
  4489. +
  4490. + mnt_flags = au_mntflags(sb);
  4491. + if (!au_opt_test(mnt_flags, UDBA_NONE)
  4492. + && !isdir
  4493. + && au_opt_test(mnt_flags, XINO)
  4494. + && (h_inode->i_nlink == 1
  4495. + || (h_inode->i_state & I_LINKABLE))
  4496. + /* todo: unnecessary? */
  4497. + /* && d_inode(cpg->dentry)->i_nlink == 1 */
  4498. + && cpg->bdst < cpg->bsrc
  4499. + && !au_ftest_cpup(cpg->flags, KEEPLINO))
  4500. + au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
  4501. + /* ignore this error */
  4502. +
  4503. + if (!err) {
  4504. + force = 0;
  4505. + if (isreg) {
  4506. + force = !!cpg->len;
  4507. + if (cpg->len == -1)
  4508. + force = !!i_size_read(h_inode);
  4509. + }
  4510. + au_fhsm_wrote(sb, cpg->bdst, force);
  4511. + }
  4512. +
  4513. + if (do_dt)
  4514. + au_dtime_revert(&dt);
  4515. + return err;
  4516. +}
  4517. +
  4518. +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
  4519. +{
  4520. + int err;
  4521. + struct dentry *dentry, *h_dentry, *h_parent, *parent;
  4522. + struct inode *h_dir;
  4523. + aufs_bindex_t bdst;
  4524. +
  4525. + dentry = cpg->dentry;
  4526. + bdst = cpg->bdst;
  4527. + h_dentry = au_h_dptr(dentry, bdst);
  4528. + if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
  4529. + dget(h_dentry);
  4530. + au_set_h_dptr(dentry, bdst, NULL);
  4531. + err = au_lkup_neg(dentry, bdst, /*wh*/0);
  4532. + if (!err)
  4533. + h_path->dentry = dget(au_h_dptr(dentry, bdst));
  4534. + au_set_h_dptr(dentry, bdst, h_dentry);
  4535. + } else {
  4536. + err = 0;
  4537. + parent = dget_parent(dentry);
  4538. + h_parent = au_h_dptr(parent, bdst);
  4539. + dput(parent);
  4540. + h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
  4541. + if (IS_ERR(h_path->dentry))
  4542. + err = PTR_ERR(h_path->dentry);
  4543. + }
  4544. + if (unlikely(err))
  4545. + goto out;
  4546. +
  4547. + h_parent = h_dentry->d_parent; /* dir inode is locked */
  4548. + h_dir = d_inode(h_parent);
  4549. + IMustLock(h_dir);
  4550. + AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
  4551. + /* no delegation since it is just created */
  4552. + err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
  4553. + /*flags*/0);
  4554. + dput(h_path->dentry);
  4555. +
  4556. +out:
  4557. + return err;
  4558. +}
  4559. +
  4560. +/*
  4561. + * copyup the @dentry from @bsrc to @bdst.
  4562. + * the caller must set the both of lower dentries.
  4563. + * @len is for truncating when it is -1 copyup the entire file.
  4564. + * in link/rename cases, @dst_parent may be different from the real one.
  4565. + * basic->bsrc can be larger than basic->bdst.
  4566. + * aufs doesn't touch the credential so
  4567. + * security_inode_copy_up{,_xattr}() are unnecessary.
  4568. + */
  4569. +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
  4570. +{
  4571. + int err, rerr;
  4572. + aufs_bindex_t old_ibtop;
  4573. + unsigned char isdir, plink;
  4574. + struct dentry *h_src, *h_dst, *h_parent;
  4575. + struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
  4576. + struct super_block *sb;
  4577. + struct au_branch *br;
  4578. + /* to reduce stack size */
  4579. + struct {
  4580. + struct au_dtime dt;
  4581. + struct path h_path;
  4582. + struct au_cpup_reg_attr h_src_attr;
  4583. + } *a;
  4584. +
  4585. + err = -ENOMEM;
  4586. + a = kmalloc(sizeof(*a), GFP_NOFS);
  4587. + if (unlikely(!a))
  4588. + goto out;
  4589. + a->h_src_attr.valid = 0;
  4590. +
  4591. + sb = cpg->dentry->d_sb;
  4592. + br = au_sbr(sb, cpg->bdst);
  4593. + a->h_path.mnt = au_br_mnt(br);
  4594. + h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
  4595. + h_parent = h_dst->d_parent; /* dir inode is locked */
  4596. + h_dir = d_inode(h_parent);
  4597. + IMustLock(h_dir);
  4598. +
  4599. + h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
  4600. + inode = d_inode(cpg->dentry);
  4601. +
  4602. + if (!dst_parent)
  4603. + dst_parent = dget_parent(cpg->dentry);
  4604. + else
  4605. + dget(dst_parent);
  4606. +
  4607. + plink = !!au_opt_test(au_mntflags(sb), PLINK);
  4608. + dst_inode = au_h_iptr(inode, cpg->bdst);
  4609. + if (dst_inode) {
  4610. + if (unlikely(!plink)) {
  4611. + err = -EIO;
  4612. + AuIOErr("hi%lu(i%lu) exists on b%d "
  4613. + "but plink is disabled\n",
  4614. + dst_inode->i_ino, inode->i_ino, cpg->bdst);
  4615. + goto out_parent;
  4616. + }
  4617. +
  4618. + if (dst_inode->i_nlink) {
  4619. + const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
  4620. +
  4621. + h_src = au_plink_lkup(inode, cpg->bdst);
  4622. + err = PTR_ERR(h_src);
  4623. + if (IS_ERR(h_src))
  4624. + goto out_parent;
  4625. + if (unlikely(d_is_negative(h_src))) {
  4626. + err = -EIO;
  4627. + AuIOErr("i%lu exists on b%d "
  4628. + "but not pseudo-linked\n",
  4629. + inode->i_ino, cpg->bdst);
  4630. + dput(h_src);
  4631. + goto out_parent;
  4632. + }
  4633. +
  4634. + if (do_dt) {
  4635. + a->h_path.dentry = h_parent;
  4636. + au_dtime_store(&a->dt, dst_parent, &a->h_path);
  4637. + }
  4638. +
  4639. + a->h_path.dentry = h_dst;
  4640. + delegated = NULL;
  4641. + err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
  4642. + if (!err && au_ftest_cpup(cpg->flags, RENAME))
  4643. + err = au_do_ren_after_cpup(cpg, &a->h_path);
  4644. + if (do_dt)
  4645. + au_dtime_revert(&a->dt);
  4646. + if (unlikely(err == -EWOULDBLOCK)) {
  4647. + pr_warn("cannot retry for NFSv4 delegation"
  4648. + " for an internal link\n");
  4649. + iput(delegated);
  4650. + }
  4651. + dput(h_src);
  4652. + goto out_parent;
  4653. + } else
  4654. + /* todo: cpup_wh_file? */
  4655. + /* udba work */
  4656. + au_update_ibrange(inode, /*do_put_zero*/1);
  4657. + }
  4658. +
  4659. + isdir = S_ISDIR(inode->i_mode);
  4660. + old_ibtop = au_ibtop(inode);
  4661. + err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
  4662. + if (unlikely(err))
  4663. + goto out_rev;
  4664. + dst_inode = d_inode(h_dst);
  4665. + inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
  4666. + /* todo: necessary? */
  4667. + /* au_pin_hdir_unlock(cpg->pin); */
  4668. +
  4669. + err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
  4670. + if (unlikely(err)) {
  4671. + /* todo: necessary? */
  4672. + /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
  4673. + inode_unlock(dst_inode);
  4674. + goto out_rev;
  4675. + }
  4676. +
  4677. + if (cpg->bdst < old_ibtop) {
  4678. + if (S_ISREG(inode->i_mode)) {
  4679. + err = au_dy_iaop(inode, cpg->bdst, dst_inode);
  4680. + if (unlikely(err)) {
  4681. + /* ignore an error */
  4682. + /* au_pin_hdir_relock(cpg->pin); */
  4683. + inode_unlock(dst_inode);
  4684. + goto out_rev;
  4685. + }
  4686. + }
  4687. + au_set_ibtop(inode, cpg->bdst);
  4688. + } else
  4689. + au_set_ibbot(inode, cpg->bdst);
  4690. + au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
  4691. + au_hi_flags(inode, isdir));
  4692. +
  4693. + /* todo: necessary? */
  4694. + /* err = au_pin_hdir_relock(cpg->pin); */
  4695. + inode_unlock(dst_inode);
  4696. + if (unlikely(err))
  4697. + goto out_rev;
  4698. +
  4699. + src_inode = d_inode(h_src);
  4700. + if (!isdir
  4701. + && (src_inode->i_nlink > 1
  4702. + || src_inode->i_state & I_LINKABLE)
  4703. + && plink)
  4704. + au_plink_append(inode, cpg->bdst, h_dst);
  4705. +
  4706. + if (au_ftest_cpup(cpg->flags, RENAME)) {
  4707. + a->h_path.dentry = h_dst;
  4708. + err = au_do_ren_after_cpup(cpg, &a->h_path);
  4709. + }
  4710. + if (!err)
  4711. + goto out_parent; /* success */
  4712. +
  4713. + /* revert */
  4714. +out_rev:
  4715. + a->h_path.dentry = h_parent;
  4716. + au_dtime_store(&a->dt, dst_parent, &a->h_path);
  4717. + a->h_path.dentry = h_dst;
  4718. + rerr = 0;
  4719. + if (d_is_positive(h_dst)) {
  4720. + if (!isdir) {
  4721. + /* no delegation since it is just created */
  4722. + rerr = vfsub_unlink(h_dir, &a->h_path,
  4723. + /*delegated*/NULL, /*force*/0);
  4724. + } else
  4725. + rerr = vfsub_rmdir(h_dir, &a->h_path);
  4726. + }
  4727. + au_dtime_revert(&a->dt);
  4728. + if (rerr) {
  4729. + AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
  4730. + err = -EIO;
  4731. + }
  4732. +out_parent:
  4733. + dput(dst_parent);
  4734. + kfree(a);
  4735. +out:
  4736. + return err;
  4737. +}
  4738. +
  4739. +#if 0 /* reserved */
  4740. +struct au_cpup_single_args {
  4741. + int *errp;
  4742. + struct au_cp_generic *cpg;
  4743. + struct dentry *dst_parent;
  4744. +};
  4745. +
  4746. +static void au_call_cpup_single(void *args)
  4747. +{
  4748. + struct au_cpup_single_args *a = args;
  4749. +
  4750. + au_pin_hdir_acquire_nest(a->cpg->pin);
  4751. + *a->errp = au_cpup_single(a->cpg, a->dst_parent);
  4752. + au_pin_hdir_release(a->cpg->pin);
  4753. +}
  4754. +#endif
  4755. +
  4756. +/*
  4757. + * prevent SIGXFSZ in copy-up.
  4758. + * testing CAP_MKNOD is for generic fs,
  4759. + * but CAP_FSETID is for xfs only, currently.
  4760. + */
  4761. +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
  4762. +{
  4763. + int do_sio;
  4764. + struct super_block *sb;
  4765. + struct inode *h_dir;
  4766. +
  4767. + do_sio = 0;
  4768. + sb = au_pinned_parent(pin)->d_sb;
  4769. + if (!au_wkq_test()
  4770. + && (!au_sbi(sb)->si_plink_maint_pid
  4771. + || au_plink_maint(sb, AuLock_NOPLM))) {
  4772. + switch (mode & S_IFMT) {
  4773. + case S_IFREG:
  4774. + /* no condition about RLIMIT_FSIZE and the file size */
  4775. + do_sio = 1;
  4776. + break;
  4777. + case S_IFCHR:
  4778. + case S_IFBLK:
  4779. + do_sio = !capable(CAP_MKNOD);
  4780. + break;
  4781. + }
  4782. + if (!do_sio)
  4783. + do_sio = ((mode & (S_ISUID | S_ISGID))
  4784. + && !capable(CAP_FSETID));
  4785. + /* this workaround may be removed in the future */
  4786. + if (!do_sio) {
  4787. + h_dir = au_pinned_h_dir(pin);
  4788. + do_sio = h_dir->i_mode & S_ISVTX;
  4789. + }
  4790. + }
  4791. +
  4792. + return do_sio;
  4793. +}
  4794. +
  4795. +#if 0 /* reserved */
  4796. +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
  4797. +{
  4798. + int err, wkq_err;
  4799. + struct dentry *h_dentry;
  4800. +
  4801. + h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
  4802. + if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
  4803. + err = au_cpup_single(cpg, dst_parent);
  4804. + else {
  4805. + struct au_cpup_single_args args = {
  4806. + .errp = &err,
  4807. + .cpg = cpg,
  4808. + .dst_parent = dst_parent
  4809. + };
  4810. + wkq_err = au_wkq_wait(au_call_cpup_single, &args);
  4811. + if (unlikely(wkq_err))
  4812. + err = wkq_err;
  4813. + }
  4814. +
  4815. + return err;
  4816. +}
  4817. +#endif
  4818. +
  4819. +/*
  4820. + * copyup the @dentry from the first active lower branch to @bdst,
  4821. + * using au_cpup_single().
  4822. + */
  4823. +static int au_cpup_simple(struct au_cp_generic *cpg)
  4824. +{
  4825. + int err;
  4826. + unsigned int flags_orig;
  4827. + struct dentry *dentry;
  4828. +
  4829. + AuDebugOn(cpg->bsrc < 0);
  4830. +
  4831. + dentry = cpg->dentry;
  4832. + DiMustWriteLock(dentry);
  4833. +
  4834. + err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
  4835. + if (!err) {
  4836. + flags_orig = cpg->flags;
  4837. + au_fset_cpup(cpg->flags, RENAME);
  4838. + err = au_cpup_single(cpg, NULL);
  4839. + cpg->flags = flags_orig;
  4840. + if (!err)
  4841. + return 0; /* success */
  4842. +
  4843. + /* revert */
  4844. + au_set_h_dptr(dentry, cpg->bdst, NULL);
  4845. + au_set_dbtop(dentry, cpg->bsrc);
  4846. + }
  4847. +
  4848. + return err;
  4849. +}
  4850. +
  4851. +struct au_cpup_simple_args {
  4852. + int *errp;
  4853. + struct au_cp_generic *cpg;
  4854. +};
  4855. +
  4856. +static void au_call_cpup_simple(void *args)
  4857. +{
  4858. + struct au_cpup_simple_args *a = args;
  4859. +
  4860. + au_pin_hdir_acquire_nest(a->cpg->pin);
  4861. + *a->errp = au_cpup_simple(a->cpg);
  4862. + au_pin_hdir_release(a->cpg->pin);
  4863. +}
  4864. +
  4865. +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
  4866. +{
  4867. + int err, wkq_err;
  4868. + struct dentry *dentry, *parent;
  4869. + struct file *h_file;
  4870. + struct inode *h_dir;
  4871. +
  4872. + dentry = cpg->dentry;
  4873. + h_file = NULL;
  4874. + if (au_ftest_cpup(cpg->flags, HOPEN)) {
  4875. + AuDebugOn(cpg->bsrc < 0);
  4876. + h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
  4877. + err = PTR_ERR(h_file);
  4878. + if (IS_ERR(h_file))
  4879. + goto out;
  4880. + }
  4881. +
  4882. + parent = dget_parent(dentry);
  4883. + h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
  4884. + if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
  4885. + && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
  4886. + err = au_cpup_simple(cpg);
  4887. + else {
  4888. + struct au_cpup_simple_args args = {
  4889. + .errp = &err,
  4890. + .cpg = cpg
  4891. + };
  4892. + wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
  4893. + if (unlikely(wkq_err))
  4894. + err = wkq_err;
  4895. + }
  4896. +
  4897. + dput(parent);
  4898. + if (h_file)
  4899. + au_h_open_post(dentry, cpg->bsrc, h_file);
  4900. +
  4901. +out:
  4902. + return err;
  4903. +}
  4904. +
  4905. +int au_sio_cpup_simple(struct au_cp_generic *cpg)
  4906. +{
  4907. + aufs_bindex_t bsrc, bbot;
  4908. + struct dentry *dentry, *h_dentry;
  4909. +
  4910. + if (cpg->bsrc < 0) {
  4911. + dentry = cpg->dentry;
  4912. + bbot = au_dbbot(dentry);
  4913. + for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
  4914. + h_dentry = au_h_dptr(dentry, bsrc);
  4915. + if (h_dentry) {
  4916. + AuDebugOn(d_is_negative(h_dentry));
  4917. + break;
  4918. + }
  4919. + }
  4920. + AuDebugOn(bsrc > bbot);
  4921. + cpg->bsrc = bsrc;
  4922. + }
  4923. + AuDebugOn(cpg->bsrc <= cpg->bdst);
  4924. + return au_do_sio_cpup_simple(cpg);
  4925. +}
  4926. +
  4927. +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
  4928. +{
  4929. + AuDebugOn(cpg->bdst <= cpg->bsrc);
  4930. + return au_do_sio_cpup_simple(cpg);
  4931. +}
  4932. +
  4933. +/* ---------------------------------------------------------------------- */
  4934. +
  4935. +/*
  4936. + * copyup the deleted file for writing.
  4937. + */
  4938. +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
  4939. + struct file *file)
  4940. +{
  4941. + int err;
  4942. + unsigned int flags_orig;
  4943. + aufs_bindex_t bsrc_orig;
  4944. + struct au_dinfo *dinfo;
  4945. + struct {
  4946. + struct au_hdentry *hd;
  4947. + struct dentry *h_dentry;
  4948. + } hdst, hsrc;
  4949. +
  4950. + dinfo = au_di(cpg->dentry);
  4951. + AuRwMustWriteLock(&dinfo->di_rwsem);
  4952. +
  4953. + bsrc_orig = cpg->bsrc;
  4954. + cpg->bsrc = dinfo->di_btop;
  4955. + hdst.hd = au_hdentry(dinfo, cpg->bdst);
  4956. + hdst.h_dentry = hdst.hd->hd_dentry;
  4957. + hdst.hd->hd_dentry = wh_dentry;
  4958. + dinfo->di_btop = cpg->bdst;
  4959. +
  4960. + hsrc.h_dentry = NULL;
  4961. + if (file) {
  4962. + hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
  4963. + hsrc.h_dentry = hsrc.hd->hd_dentry;
  4964. + hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
  4965. + }
  4966. + flags_orig = cpg->flags;
  4967. + cpg->flags = !AuCpup_DTIME;
  4968. + err = au_cpup_single(cpg, /*h_parent*/NULL);
  4969. + cpg->flags = flags_orig;
  4970. + if (file) {
  4971. + if (!err)
  4972. + err = au_reopen_nondir(file);
  4973. + hsrc.hd->hd_dentry = hsrc.h_dentry;
  4974. + }
  4975. + hdst.hd->hd_dentry = hdst.h_dentry;
  4976. + dinfo->di_btop = cpg->bsrc;
  4977. + cpg->bsrc = bsrc_orig;
  4978. +
  4979. + return err;
  4980. +}
  4981. +
  4982. +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
  4983. +{
  4984. + int err;
  4985. + aufs_bindex_t bdst;
  4986. + struct au_dtime dt;
  4987. + struct dentry *dentry, *parent, *h_parent, *wh_dentry;
  4988. + struct au_branch *br;
  4989. + struct path h_path;
  4990. +
  4991. + dentry = cpg->dentry;
  4992. + bdst = cpg->bdst;
  4993. + br = au_sbr(dentry->d_sb, bdst);
  4994. + parent = dget_parent(dentry);
  4995. + h_parent = au_h_dptr(parent, bdst);
  4996. + wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
  4997. + err = PTR_ERR(wh_dentry);
  4998. + if (IS_ERR(wh_dentry))
  4999. + goto out;
  5000. +
  5001. + h_path.dentry = h_parent;
  5002. + h_path.mnt = au_br_mnt(br);
  5003. + au_dtime_store(&dt, parent, &h_path);
  5004. + err = au_do_cpup_wh(cpg, wh_dentry, file);
  5005. + if (unlikely(err))
  5006. + goto out_wh;
  5007. +
  5008. + dget(wh_dentry);
  5009. + h_path.dentry = wh_dentry;
  5010. + if (!d_is_dir(wh_dentry)) {
  5011. + /* no delegation since it is just created */
  5012. + err = vfsub_unlink(d_inode(h_parent), &h_path,
  5013. + /*delegated*/NULL, /*force*/0);
  5014. + } else
  5015. + err = vfsub_rmdir(d_inode(h_parent), &h_path);
  5016. + if (unlikely(err)) {
  5017. + AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
  5018. + wh_dentry, err);
  5019. + err = -EIO;
  5020. + }
  5021. + au_dtime_revert(&dt);
  5022. + au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
  5023. +
  5024. +out_wh:
  5025. + dput(wh_dentry);
  5026. +out:
  5027. + dput(parent);
  5028. + return err;
  5029. +}
  5030. +
  5031. +struct au_cpup_wh_args {
  5032. + int *errp;
  5033. + struct au_cp_generic *cpg;
  5034. + struct file *file;
  5035. +};
  5036. +
  5037. +static void au_call_cpup_wh(void *args)
  5038. +{
  5039. + struct au_cpup_wh_args *a = args;
  5040. +
  5041. + au_pin_hdir_acquire_nest(a->cpg->pin);
  5042. + *a->errp = au_cpup_wh(a->cpg, a->file);
  5043. + au_pin_hdir_release(a->cpg->pin);
  5044. +}
  5045. +
  5046. +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
  5047. +{
  5048. + int err, wkq_err;
  5049. + aufs_bindex_t bdst;
  5050. + struct dentry *dentry, *parent, *h_orph, *h_parent;
  5051. + struct inode *dir, *h_dir, *h_tmpdir;
  5052. + struct au_wbr *wbr;
  5053. + struct au_pin wh_pin, *pin_orig;
  5054. +
  5055. + dentry = cpg->dentry;
  5056. + bdst = cpg->bdst;
  5057. + parent = dget_parent(dentry);
  5058. + dir = d_inode(parent);
  5059. + h_orph = NULL;
  5060. + h_parent = NULL;
  5061. + h_dir = au_igrab(au_h_iptr(dir, bdst));
  5062. + h_tmpdir = h_dir;
  5063. + pin_orig = NULL;
  5064. + if (!h_dir->i_nlink) {
  5065. + wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
  5066. + h_orph = wbr->wbr_orph;
  5067. +
  5068. + h_parent = dget(au_h_dptr(parent, bdst));
  5069. + au_set_h_dptr(parent, bdst, dget(h_orph));
  5070. + h_tmpdir = d_inode(h_orph);
  5071. + au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
  5072. +
  5073. + inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
  5074. + /* todo: au_h_open_pre()? */
  5075. +
  5076. + pin_orig = cpg->pin;
  5077. + au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
  5078. + AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
  5079. + cpg->pin = &wh_pin;
  5080. + }
  5081. +
  5082. + if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
  5083. + && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
  5084. + err = au_cpup_wh(cpg, file);
  5085. + else {
  5086. + struct au_cpup_wh_args args = {
  5087. + .errp = &err,
  5088. + .cpg = cpg,
  5089. + .file = file
  5090. + };
  5091. + wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
  5092. + if (unlikely(wkq_err))
  5093. + err = wkq_err;
  5094. + }
  5095. +
  5096. + if (h_orph) {
  5097. + inode_unlock(h_tmpdir);
  5098. + /* todo: au_h_open_post()? */
  5099. + au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
  5100. + au_set_h_dptr(parent, bdst, h_parent);
  5101. + AuDebugOn(!pin_orig);
  5102. + cpg->pin = pin_orig;
  5103. + }
  5104. + iput(h_dir);
  5105. + dput(parent);
  5106. +
  5107. + return err;
  5108. +}
  5109. +
  5110. +/* ---------------------------------------------------------------------- */
  5111. +
  5112. +/*
  5113. + * generic routine for both of copy-up and copy-down.
  5114. + */
  5115. +/* cf. revalidate function in file.c */
  5116. +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
  5117. + int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
  5118. + struct au_pin *pin,
  5119. + struct dentry *h_parent, void *arg),
  5120. + void *arg)
  5121. +{
  5122. + int err;
  5123. + struct au_pin pin;
  5124. + struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
  5125. +
  5126. + err = 0;
  5127. + parent = dget_parent(dentry);
  5128. + if (IS_ROOT(parent))
  5129. + goto out;
  5130. +
  5131. + au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
  5132. + au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
  5133. +
  5134. + /* do not use au_dpage */
  5135. + real_parent = parent;
  5136. + while (1) {
  5137. + dput(parent);
  5138. + parent = dget_parent(dentry);
  5139. + h_parent = au_h_dptr(parent, bdst);
  5140. + if (h_parent)
  5141. + goto out; /* success */
  5142. +
  5143. + /* find top dir which is necessary to cpup */
  5144. + do {
  5145. + d = parent;
  5146. + dput(parent);
  5147. + parent = dget_parent(d);
  5148. + di_read_lock_parent3(parent, !AuLock_IR);
  5149. + h_parent = au_h_dptr(parent, bdst);
  5150. + di_read_unlock(parent, !AuLock_IR);
  5151. + } while (!h_parent);
  5152. +
  5153. + if (d != real_parent)
  5154. + di_write_lock_child3(d);
  5155. +
  5156. + /* somebody else might create while we were sleeping */
  5157. + h_dentry = au_h_dptr(d, bdst);
  5158. + if (!h_dentry || d_is_negative(h_dentry)) {
  5159. + if (h_dentry)
  5160. + au_update_dbtop(d);
  5161. +
  5162. + au_pin_set_dentry(&pin, d);
  5163. + err = au_do_pin(&pin);
  5164. + if (!err) {
  5165. + err = cp(d, bdst, &pin, h_parent, arg);
  5166. + au_unpin(&pin);
  5167. + }
  5168. + }
  5169. +
  5170. + if (d != real_parent)
  5171. + di_write_unlock(d);
  5172. + if (unlikely(err))
  5173. + break;
  5174. + }
  5175. +
  5176. +out:
  5177. + dput(parent);
  5178. + return err;
  5179. +}
  5180. +
  5181. +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
  5182. + struct au_pin *pin,
  5183. + struct dentry *h_parent __maybe_unused,
  5184. + void *arg __maybe_unused)
  5185. +{
  5186. + struct au_cp_generic cpg = {
  5187. + .dentry = dentry,
  5188. + .bdst = bdst,
  5189. + .bsrc = -1,
  5190. + .len = 0,
  5191. + .pin = pin,
  5192. + .flags = AuCpup_DTIME
  5193. + };
  5194. + return au_sio_cpup_simple(&cpg);
  5195. +}
  5196. +
  5197. +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
  5198. +{
  5199. + return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
  5200. +}
  5201. +
  5202. +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
  5203. +{
  5204. + int err;
  5205. + struct dentry *parent;
  5206. + struct inode *dir;
  5207. +
  5208. + parent = dget_parent(dentry);
  5209. + dir = d_inode(parent);
  5210. + err = 0;
  5211. + if (au_h_iptr(dir, bdst))
  5212. + goto out;
  5213. +
  5214. + di_read_unlock(parent, AuLock_IR);
  5215. + di_write_lock_parent(parent);
  5216. + /* someone else might change our inode while we were sleeping */
  5217. + if (!au_h_iptr(dir, bdst))
  5218. + err = au_cpup_dirs(dentry, bdst);
  5219. + di_downgrade_lock(parent, AuLock_IR);
  5220. +
  5221. +out:
  5222. + dput(parent);
  5223. + return err;
  5224. +}
  5225. --- /dev/null
  5226. +++ linux-4.19/fs/aufs/cpup.h 2018-11-20 09:35:15.011195503 +0200
  5227. @@ -0,0 +1,100 @@
  5228. +/* SPDX-License-Identifier: GPL-2.0 */
  5229. +/*
  5230. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  5231. + *
  5232. + * This program, aufs is free software; you can redistribute it and/or modify
  5233. + * it under the terms of the GNU General Public License as published by
  5234. + * the Free Software Foundation; either version 2 of the License, or
  5235. + * (at your option) any later version.
  5236. + *
  5237. + * This program is distributed in the hope that it will be useful,
  5238. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5239. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5240. + * GNU General Public License for more details.
  5241. + *
  5242. + * You should have received a copy of the GNU General Public License
  5243. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5244. + */
  5245. +
  5246. +/*
  5247. + * copy-up/down functions
  5248. + */
  5249. +
  5250. +#ifndef __AUFS_CPUP_H__
  5251. +#define __AUFS_CPUP_H__
  5252. +
  5253. +#ifdef __KERNEL__
  5254. +
  5255. +#include <linux/path.h>
  5256. +
  5257. +struct inode;
  5258. +struct file;
  5259. +struct au_pin;
  5260. +
  5261. +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
  5262. +void au_cpup_attr_timesizes(struct inode *inode);
  5263. +void au_cpup_attr_nlink(struct inode *inode, int force);
  5264. +void au_cpup_attr_changeable(struct inode *inode);
  5265. +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
  5266. +void au_cpup_attr_all(struct inode *inode, int force);
  5267. +
  5268. +/* ---------------------------------------------------------------------- */
  5269. +
  5270. +struct au_cp_generic {
  5271. + struct dentry *dentry;
  5272. + aufs_bindex_t bdst, bsrc;
  5273. + loff_t len;
  5274. + struct au_pin *pin;
  5275. + unsigned int flags;
  5276. +};
  5277. +
  5278. +/* cpup flags */
  5279. +#define AuCpup_DTIME 1 /* do dtime_store/revert */
  5280. +#define AuCpup_KEEPLINO (1 << 1) /* do not clear the lower xino,
  5281. + for link(2) */
  5282. +#define AuCpup_RENAME (1 << 2) /* rename after cpup */
  5283. +#define AuCpup_HOPEN (1 << 3) /* call h_open_pre/post() in
  5284. + cpup */
  5285. +#define AuCpup_OVERWRITE (1 << 4) /* allow overwriting the
  5286. + existing entry */
  5287. +#define AuCpup_RWDST (1 << 5) /* force write target even if
  5288. + the branch is marked as RO */
  5289. +
  5290. +#ifndef CONFIG_AUFS_BR_HFSPLUS
  5291. +#undef AuCpup_HOPEN
  5292. +#define AuCpup_HOPEN 0
  5293. +#endif
  5294. +
  5295. +#define au_ftest_cpup(flags, name) ((flags) & AuCpup_##name)
  5296. +#define au_fset_cpup(flags, name) \
  5297. + do { (flags) |= AuCpup_##name; } while (0)
  5298. +#define au_fclr_cpup(flags, name) \
  5299. + do { (flags) &= ~AuCpup_##name; } while (0)
  5300. +
  5301. +int au_copy_file(struct file *dst, struct file *src, loff_t len);
  5302. +int au_sio_cpup_simple(struct au_cp_generic *cpg);
  5303. +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
  5304. +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
  5305. +
  5306. +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
  5307. + int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
  5308. + struct au_pin *pin,
  5309. + struct dentry *h_parent, void *arg),
  5310. + void *arg);
  5311. +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
  5312. +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
  5313. +
  5314. +/* ---------------------------------------------------------------------- */
  5315. +
  5316. +/* keep timestamps when copyup */
  5317. +struct au_dtime {
  5318. + struct dentry *dt_dentry;
  5319. + struct path dt_h_path;
  5320. + struct timespec64 dt_atime, dt_mtime;
  5321. +};
  5322. +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
  5323. + struct path *h_path);
  5324. +void au_dtime_revert(struct au_dtime *dt);
  5325. +
  5326. +#endif /* __KERNEL__ */
  5327. +#endif /* __AUFS_CPUP_H__ */
  5328. --- /dev/null
  5329. +++ linux-4.19/fs/aufs/dbgaufs.c 2018-11-20 09:35:15.012195503 +0200
  5330. @@ -0,0 +1,519 @@
  5331. +// SPDX-License-Identifier: GPL-2.0
  5332. +/*
  5333. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  5334. + *
  5335. + * This program, aufs is free software; you can redistribute it and/or modify
  5336. + * it under the terms of the GNU General Public License as published by
  5337. + * the Free Software Foundation; either version 2 of the License, or
  5338. + * (at your option) any later version.
  5339. + *
  5340. + * This program is distributed in the hope that it will be useful,
  5341. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5342. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5343. + * GNU General Public License for more details.
  5344. + *
  5345. + * You should have received a copy of the GNU General Public License
  5346. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5347. + */
  5348. +
  5349. +/*
  5350. + * debugfs interface
  5351. + */
  5352. +
  5353. +#include <linux/debugfs.h>
  5354. +#include "aufs.h"
  5355. +
  5356. +#ifndef CONFIG_SYSFS
  5357. +#error DEBUG_FS depends upon SYSFS
  5358. +#endif
  5359. +
  5360. +static struct dentry *dbgaufs;
  5361. +static const mode_t dbgaufs_mode = 0444;
  5362. +
  5363. +/* 20 is max digits length of ulong 64 */
  5364. +struct dbgaufs_arg {
  5365. + int n;
  5366. + char a[20 * 4];
  5367. +};
  5368. +
  5369. +/*
  5370. + * common function for all XINO files
  5371. + */
  5372. +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
  5373. + struct file *file)
  5374. +{
  5375. + kfree(file->private_data);
  5376. + return 0;
  5377. +}
  5378. +
  5379. +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
  5380. + int cnt)
  5381. +{
  5382. + int err;
  5383. + struct kstat st;
  5384. + struct dbgaufs_arg *p;
  5385. +
  5386. + err = -ENOMEM;
  5387. + p = kmalloc(sizeof(*p), GFP_NOFS);
  5388. + if (unlikely(!p))
  5389. + goto out;
  5390. +
  5391. + err = 0;
  5392. + p->n = 0;
  5393. + file->private_data = p;
  5394. + if (!xf)
  5395. + goto out;
  5396. +
  5397. + err = vfsub_getattr(&xf->f_path, &st);
  5398. + if (!err) {
  5399. + if (do_fcnt)
  5400. + p->n = snprintf
  5401. + (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
  5402. + cnt, st.blocks, st.blksize,
  5403. + (long long)st.size);
  5404. + else
  5405. + p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
  5406. + st.blocks, st.blksize,
  5407. + (long long)st.size);
  5408. + AuDebugOn(p->n >= sizeof(p->a));
  5409. + } else {
  5410. + p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
  5411. + err = 0;
  5412. + }
  5413. +
  5414. +out:
  5415. + return err;
  5416. +}
  5417. +
  5418. +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
  5419. + size_t count, loff_t *ppos)
  5420. +{
  5421. + struct dbgaufs_arg *p;
  5422. +
  5423. + p = file->private_data;
  5424. + return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
  5425. +}
  5426. +
  5427. +/* ---------------------------------------------------------------------- */
  5428. +
  5429. +struct dbgaufs_plink_arg {
  5430. + int n;
  5431. + char a[];
  5432. +};
  5433. +
  5434. +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
  5435. + struct file *file)
  5436. +{
  5437. + free_page((unsigned long)file->private_data);
  5438. + return 0;
  5439. +}
  5440. +
  5441. +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
  5442. +{
  5443. + int err, i, limit;
  5444. + unsigned long n, sum;
  5445. + struct dbgaufs_plink_arg *p;
  5446. + struct au_sbinfo *sbinfo;
  5447. + struct super_block *sb;
  5448. + struct hlist_bl_head *hbl;
  5449. +
  5450. + err = -ENOMEM;
  5451. + p = (void *)get_zeroed_page(GFP_NOFS);
  5452. + if (unlikely(!p))
  5453. + goto out;
  5454. +
  5455. + err = -EFBIG;
  5456. + sbinfo = inode->i_private;
  5457. + sb = sbinfo->si_sb;
  5458. + si_noflush_read_lock(sb);
  5459. + if (au_opt_test(au_mntflags(sb), PLINK)) {
  5460. + limit = PAGE_SIZE - sizeof(p->n);
  5461. +
  5462. + /* the number of buckets */
  5463. + n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
  5464. + p->n += n;
  5465. + limit -= n;
  5466. +
  5467. + sum = 0;
  5468. + for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
  5469. + i++, hbl++) {
  5470. + n = au_hbl_count(hbl);
  5471. + sum += n;
  5472. +
  5473. + n = snprintf(p->a + p->n, limit, "%lu ", n);
  5474. + p->n += n;
  5475. + limit -= n;
  5476. + if (unlikely(limit <= 0))
  5477. + goto out_free;
  5478. + }
  5479. + p->a[p->n - 1] = '\n';
  5480. +
  5481. + /* the sum of plinks */
  5482. + n = snprintf(p->a + p->n, limit, "%lu\n", sum);
  5483. + p->n += n;
  5484. + limit -= n;
  5485. + if (unlikely(limit <= 0))
  5486. + goto out_free;
  5487. + } else {
  5488. +#define str "1\n0\n0\n"
  5489. + p->n = sizeof(str) - 1;
  5490. + strcpy(p->a, str);
  5491. +#undef str
  5492. + }
  5493. + si_read_unlock(sb);
  5494. +
  5495. + err = 0;
  5496. + file->private_data = p;
  5497. + goto out; /* success */
  5498. +
  5499. +out_free:
  5500. + free_page((unsigned long)p);
  5501. +out:
  5502. + return err;
  5503. +}
  5504. +
  5505. +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
  5506. + size_t count, loff_t *ppos)
  5507. +{
  5508. + struct dbgaufs_plink_arg *p;
  5509. +
  5510. + p = file->private_data;
  5511. + return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
  5512. +}
  5513. +
  5514. +static const struct file_operations dbgaufs_plink_fop = {
  5515. + .owner = THIS_MODULE,
  5516. + .open = dbgaufs_plink_open,
  5517. + .release = dbgaufs_plink_release,
  5518. + .read = dbgaufs_plink_read
  5519. +};
  5520. +
  5521. +/* ---------------------------------------------------------------------- */
  5522. +
  5523. +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
  5524. +{
  5525. + int err;
  5526. + struct au_sbinfo *sbinfo;
  5527. + struct super_block *sb;
  5528. +
  5529. + sbinfo = inode->i_private;
  5530. + sb = sbinfo->si_sb;
  5531. + si_noflush_read_lock(sb);
  5532. + err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
  5533. + si_read_unlock(sb);
  5534. + return err;
  5535. +}
  5536. +
  5537. +static const struct file_operations dbgaufs_xib_fop = {
  5538. + .owner = THIS_MODULE,
  5539. + .open = dbgaufs_xib_open,
  5540. + .release = dbgaufs_xi_release,
  5541. + .read = dbgaufs_xi_read
  5542. +};
  5543. +
  5544. +/* ---------------------------------------------------------------------- */
  5545. +
  5546. +#define DbgaufsXi_PREFIX "xi"
  5547. +
  5548. +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
  5549. +{
  5550. + int err, idx;
  5551. + long l;
  5552. + aufs_bindex_t bindex;
  5553. + char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
  5554. + struct au_sbinfo *sbinfo;
  5555. + struct super_block *sb;
  5556. + struct au_xino *xi;
  5557. + struct file *xf;
  5558. + struct qstr *name;
  5559. + struct au_branch *br;
  5560. +
  5561. + err = -ENOENT;
  5562. + name = &file->f_path.dentry->d_name;
  5563. + if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
  5564. + || memcmp(name->name, DbgaufsXi_PREFIX,
  5565. + sizeof(DbgaufsXi_PREFIX) - 1)))
  5566. + goto out;
  5567. +
  5568. + AuDebugOn(name->len >= sizeof(a));
  5569. + memcpy(a, name->name, name->len);
  5570. + a[name->len] = '\0';
  5571. + p = strchr(a, '-');
  5572. + if (p)
  5573. + *p = '\0';
  5574. + err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
  5575. + if (unlikely(err))
  5576. + goto out;
  5577. + bindex = l;
  5578. + idx = 0;
  5579. + if (p) {
  5580. + err = kstrtol(p + 1, 10, &l);
  5581. + if (unlikely(err))
  5582. + goto out;
  5583. + idx = l;
  5584. + }
  5585. +
  5586. + err = -ENOENT;
  5587. + sbinfo = inode->i_private;
  5588. + sb = sbinfo->si_sb;
  5589. + si_noflush_read_lock(sb);
  5590. + if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
  5591. + goto out_si;
  5592. + br = au_sbr(sb, bindex);
  5593. + xi = br->br_xino;
  5594. + if (unlikely(idx >= xi->xi_nfile))
  5595. + goto out_si;
  5596. + xf = au_xino_file(xi, idx);
  5597. + if (xf)
  5598. + err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
  5599. + au_xino_count(br));
  5600. +
  5601. +out_si:
  5602. + si_read_unlock(sb);
  5603. +out:
  5604. + AuTraceErr(err);
  5605. + return err;
  5606. +}
  5607. +
  5608. +static const struct file_operations dbgaufs_xino_fop = {
  5609. + .owner = THIS_MODULE,
  5610. + .open = dbgaufs_xino_open,
  5611. + .release = dbgaufs_xi_release,
  5612. + .read = dbgaufs_xi_read
  5613. +};
  5614. +
  5615. +void dbgaufs_xino_del(struct au_branch *br)
  5616. +{
  5617. + struct dentry *dbgaufs;
  5618. +
  5619. + dbgaufs = br->br_dbgaufs;
  5620. + if (!dbgaufs)
  5621. + return;
  5622. +
  5623. + br->br_dbgaufs = NULL;
  5624. + /* debugfs acquires the parent i_mutex */
  5625. + lockdep_off();
  5626. + debugfs_remove(dbgaufs);
  5627. + lockdep_on();
  5628. +}
  5629. +
  5630. +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
  5631. +{
  5632. + aufs_bindex_t bbot;
  5633. + struct au_branch *br;
  5634. +
  5635. + if (!au_sbi(sb)->si_dbgaufs)
  5636. + return;
  5637. +
  5638. + bbot = au_sbbot(sb);
  5639. + for (; bindex <= bbot; bindex++) {
  5640. + br = au_sbr(sb, bindex);
  5641. + dbgaufs_xino_del(br);
  5642. + }
  5643. +}
  5644. +
  5645. +static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
  5646. + unsigned int idx, struct dentry *parent,
  5647. + struct au_sbinfo *sbinfo)
  5648. +{
  5649. + struct au_branch *br;
  5650. + struct dentry *d;
  5651. + /* "xi" bindex(5) "-" idx(2) NULL */
  5652. + char name[sizeof(DbgaufsXi_PREFIX) + 8];
  5653. +
  5654. + if (!idx)
  5655. + snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
  5656. + else
  5657. + snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
  5658. + bindex, idx);
  5659. + br = au_sbr(sb, bindex);
  5660. + if (br->br_dbgaufs) {
  5661. + struct qstr qstr = QSTR_INIT(name, strlen(name));
  5662. +
  5663. + if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
  5664. + /* debugfs acquires the parent i_mutex */
  5665. + lockdep_off();
  5666. + d = debugfs_rename(parent, br->br_dbgaufs, parent,
  5667. + name);
  5668. + lockdep_on();
  5669. + if (unlikely(!d))
  5670. + pr_warn("failed renaming %pd/%s, ignored.\n",
  5671. + parent, name);
  5672. + }
  5673. + } else {
  5674. + lockdep_off();
  5675. + br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
  5676. + sbinfo, &dbgaufs_xino_fop);
  5677. + lockdep_on();
  5678. + if (unlikely(!br->br_dbgaufs))
  5679. + pr_warn("failed creating %pd/%s, ignored.\n",
  5680. + parent, name);
  5681. + }
  5682. +}
  5683. +
  5684. +static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
  5685. + struct dentry *parent, struct au_sbinfo *sbinfo)
  5686. +{
  5687. + struct au_branch *br;
  5688. + struct au_xino *xi;
  5689. + unsigned int u;
  5690. +
  5691. + br = au_sbr(sb, bindex);
  5692. + xi = br->br_xino;
  5693. + for (u = 0; u < xi->xi_nfile; u++)
  5694. + dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
  5695. +}
  5696. +
  5697. +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
  5698. +{
  5699. + struct au_sbinfo *sbinfo;
  5700. + struct dentry *parent;
  5701. + aufs_bindex_t bbot;
  5702. +
  5703. + if (!au_opt_test(au_mntflags(sb), XINO))
  5704. + return;
  5705. +
  5706. + sbinfo = au_sbi(sb);
  5707. + parent = sbinfo->si_dbgaufs;
  5708. + if (!parent)
  5709. + return;
  5710. +
  5711. + bbot = au_sbbot(sb);
  5712. + if (topdown)
  5713. + for (; bindex <= bbot; bindex++)
  5714. + dbgaufs_br_add(sb, bindex, parent, sbinfo);
  5715. + else
  5716. + for (; bbot >= bindex; bbot--)
  5717. + dbgaufs_br_add(sb, bbot, parent, sbinfo);
  5718. +}
  5719. +
  5720. +/* ---------------------------------------------------------------------- */
  5721. +
  5722. +#ifdef CONFIG_AUFS_EXPORT
  5723. +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
  5724. +{
  5725. + int err;
  5726. + struct au_sbinfo *sbinfo;
  5727. + struct super_block *sb;
  5728. +
  5729. + sbinfo = inode->i_private;
  5730. + sb = sbinfo->si_sb;
  5731. + si_noflush_read_lock(sb);
  5732. + err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
  5733. + si_read_unlock(sb);
  5734. + return err;
  5735. +}
  5736. +
  5737. +static const struct file_operations dbgaufs_xigen_fop = {
  5738. + .owner = THIS_MODULE,
  5739. + .open = dbgaufs_xigen_open,
  5740. + .release = dbgaufs_xi_release,
  5741. + .read = dbgaufs_xi_read
  5742. +};
  5743. +
  5744. +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
  5745. +{
  5746. + int err;
  5747. +
  5748. + /*
  5749. + * This function is a dynamic '__init' function actually,
  5750. + * so the tiny check for si_rwsem is unnecessary.
  5751. + */
  5752. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  5753. +
  5754. + err = -EIO;
  5755. + sbinfo->si_dbgaufs_xigen = debugfs_create_file
  5756. + ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
  5757. + &dbgaufs_xigen_fop);
  5758. + if (sbinfo->si_dbgaufs_xigen)
  5759. + err = 0;
  5760. +
  5761. + return err;
  5762. +}
  5763. +#else
  5764. +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
  5765. +{
  5766. + return 0;
  5767. +}
  5768. +#endif /* CONFIG_AUFS_EXPORT */
  5769. +
  5770. +/* ---------------------------------------------------------------------- */
  5771. +
  5772. +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
  5773. +{
  5774. + /*
  5775. + * This function is a dynamic '__fin' function actually,
  5776. + * so the tiny check for si_rwsem is unnecessary.
  5777. + */
  5778. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  5779. +
  5780. + debugfs_remove_recursive(sbinfo->si_dbgaufs);
  5781. + sbinfo->si_dbgaufs = NULL;
  5782. +}
  5783. +
  5784. +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
  5785. +{
  5786. + int err;
  5787. + char name[SysaufsSiNameLen];
  5788. +
  5789. + /*
  5790. + * This function is a dynamic '__init' function actually,
  5791. + * so the tiny check for si_rwsem is unnecessary.
  5792. + */
  5793. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  5794. +
  5795. + err = -ENOENT;
  5796. + if (!dbgaufs) {
  5797. + AuErr1("/debug/aufs is uninitialized\n");
  5798. + goto out;
  5799. + }
  5800. +
  5801. + err = -EIO;
  5802. + sysaufs_name(sbinfo, name);
  5803. + sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
  5804. + if (unlikely(!sbinfo->si_dbgaufs))
  5805. + goto out;
  5806. +
  5807. + /* regardless plink/noplink option */
  5808. + sbinfo->si_dbgaufs_plink = debugfs_create_file
  5809. + ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
  5810. + &dbgaufs_plink_fop);
  5811. + if (unlikely(!sbinfo->si_dbgaufs_plink))
  5812. + goto out_dir;
  5813. +
  5814. + /* regardless xino/noxino option */
  5815. + sbinfo->si_dbgaufs_xib = debugfs_create_file
  5816. + ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
  5817. + &dbgaufs_xib_fop);
  5818. + if (unlikely(!sbinfo->si_dbgaufs_xib))
  5819. + goto out_dir;
  5820. +
  5821. + err = dbgaufs_xigen_init(sbinfo);
  5822. + if (!err)
  5823. + goto out; /* success */
  5824. +
  5825. +out_dir:
  5826. + dbgaufs_si_fin(sbinfo);
  5827. +out:
  5828. + if (unlikely(err))
  5829. + pr_err("debugfs/aufs failed\n");
  5830. + return err;
  5831. +}
  5832. +
  5833. +/* ---------------------------------------------------------------------- */
  5834. +
  5835. +void dbgaufs_fin(void)
  5836. +{
  5837. + debugfs_remove(dbgaufs);
  5838. +}
  5839. +
  5840. +int __init dbgaufs_init(void)
  5841. +{
  5842. + int err;
  5843. +
  5844. + err = -EIO;
  5845. + dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
  5846. + if (dbgaufs)
  5847. + err = 0;
  5848. + return err;
  5849. +}
  5850. --- /dev/null
  5851. +++ linux-4.19/fs/aufs/dbgaufs.h 2018-11-20 09:35:15.012195503 +0200
  5852. @@ -0,0 +1,53 @@
  5853. +/* SPDX-License-Identifier: GPL-2.0 */
  5854. +/*
  5855. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  5856. + *
  5857. + * This program, aufs is free software; you can redistribute it and/or modify
  5858. + * it under the terms of the GNU General Public License as published by
  5859. + * the Free Software Foundation; either version 2 of the License, or
  5860. + * (at your option) any later version.
  5861. + *
  5862. + * This program is distributed in the hope that it will be useful,
  5863. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5864. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5865. + * GNU General Public License for more details.
  5866. + *
  5867. + * You should have received a copy of the GNU General Public License
  5868. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5869. + */
  5870. +
  5871. +/*
  5872. + * debugfs interface
  5873. + */
  5874. +
  5875. +#ifndef __DBGAUFS_H__
  5876. +#define __DBGAUFS_H__
  5877. +
  5878. +#ifdef __KERNEL__
  5879. +
  5880. +struct super_block;
  5881. +struct au_sbinfo;
  5882. +struct au_branch;
  5883. +
  5884. +#ifdef CONFIG_DEBUG_FS
  5885. +/* dbgaufs.c */
  5886. +void dbgaufs_xino_del(struct au_branch *br);
  5887. +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
  5888. +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
  5889. +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
  5890. +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
  5891. +void dbgaufs_fin(void);
  5892. +int __init dbgaufs_init(void);
  5893. +#else
  5894. +AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
  5895. +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
  5896. +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
  5897. + int topdown)
  5898. +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
  5899. +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
  5900. +AuStubVoid(dbgaufs_fin, void)
  5901. +AuStubInt0(__init dbgaufs_init, void)
  5902. +#endif /* CONFIG_DEBUG_FS */
  5903. +
  5904. +#endif /* __KERNEL__ */
  5905. +#endif /* __DBGAUFS_H__ */
  5906. --- /dev/null
  5907. +++ linux-4.19/fs/aufs/dcsub.c 2018-11-20 09:35:15.012195503 +0200
  5908. @@ -0,0 +1,225 @@
  5909. +// SPDX-License-Identifier: GPL-2.0
  5910. +/*
  5911. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  5912. + *
  5913. + * This program, aufs is free software; you can redistribute it and/or modify
  5914. + * it under the terms of the GNU General Public License as published by
  5915. + * the Free Software Foundation; either version 2 of the License, or
  5916. + * (at your option) any later version.
  5917. + *
  5918. + * This program is distributed in the hope that it will be useful,
  5919. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5920. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5921. + * GNU General Public License for more details.
  5922. + *
  5923. + * You should have received a copy of the GNU General Public License
  5924. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5925. + */
  5926. +
  5927. +/*
  5928. + * sub-routines for dentry cache
  5929. + */
  5930. +
  5931. +#include "aufs.h"
  5932. +
  5933. +static void au_dpage_free(struct au_dpage *dpage)
  5934. +{
  5935. + int i;
  5936. + struct dentry **p;
  5937. +
  5938. + p = dpage->dentries;
  5939. + for (i = 0; i < dpage->ndentry; i++)
  5940. + dput(*p++);
  5941. + free_page((unsigned long)dpage->dentries);
  5942. +}
  5943. +
  5944. +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
  5945. +{
  5946. + int err;
  5947. + void *p;
  5948. +
  5949. + err = -ENOMEM;
  5950. + dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
  5951. + if (unlikely(!dpages->dpages))
  5952. + goto out;
  5953. +
  5954. + p = (void *)__get_free_page(gfp);
  5955. + if (unlikely(!p))
  5956. + goto out_dpages;
  5957. +
  5958. + dpages->dpages[0].ndentry = 0;
  5959. + dpages->dpages[0].dentries = p;
  5960. + dpages->ndpage = 1;
  5961. + return 0; /* success */
  5962. +
  5963. +out_dpages:
  5964. + kfree(dpages->dpages);
  5965. +out:
  5966. + return err;
  5967. +}
  5968. +
  5969. +void au_dpages_free(struct au_dcsub_pages *dpages)
  5970. +{
  5971. + int i;
  5972. + struct au_dpage *p;
  5973. +
  5974. + p = dpages->dpages;
  5975. + for (i = 0; i < dpages->ndpage; i++)
  5976. + au_dpage_free(p++);
  5977. + kfree(dpages->dpages);
  5978. +}
  5979. +
  5980. +static int au_dpages_append(struct au_dcsub_pages *dpages,
  5981. + struct dentry *dentry, gfp_t gfp)
  5982. +{
  5983. + int err, sz;
  5984. + struct au_dpage *dpage;
  5985. + void *p;
  5986. +
  5987. + dpage = dpages->dpages + dpages->ndpage - 1;
  5988. + sz = PAGE_SIZE / sizeof(dentry);
  5989. + if (unlikely(dpage->ndentry >= sz)) {
  5990. + AuLabel(new dpage);
  5991. + err = -ENOMEM;
  5992. + sz = dpages->ndpage * sizeof(*dpages->dpages);
  5993. + p = au_kzrealloc(dpages->dpages, sz,
  5994. + sz + sizeof(*dpages->dpages), gfp,
  5995. + /*may_shrink*/0);
  5996. + if (unlikely(!p))
  5997. + goto out;
  5998. +
  5999. + dpages->dpages = p;
  6000. + dpage = dpages->dpages + dpages->ndpage;
  6001. + p = (void *)__get_free_page(gfp);
  6002. + if (unlikely(!p))
  6003. + goto out;
  6004. +
  6005. + dpage->ndentry = 0;
  6006. + dpage->dentries = p;
  6007. + dpages->ndpage++;
  6008. + }
  6009. +
  6010. + AuDebugOn(au_dcount(dentry) <= 0);
  6011. + dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
  6012. + return 0; /* success */
  6013. +
  6014. +out:
  6015. + return err;
  6016. +}
  6017. +
  6018. +/* todo: BAD approach */
  6019. +/* copied from linux/fs/dcache.c */
  6020. +enum d_walk_ret {
  6021. + D_WALK_CONTINUE,
  6022. + D_WALK_QUIT,
  6023. + D_WALK_NORETRY,
  6024. + D_WALK_SKIP,
  6025. +};
  6026. +
  6027. +extern void d_walk(struct dentry *parent, void *data,
  6028. + enum d_walk_ret (*enter)(void *, struct dentry *));
  6029. +
  6030. +struct ac_dpages_arg {
  6031. + int err;
  6032. + struct au_dcsub_pages *dpages;
  6033. + struct super_block *sb;
  6034. + au_dpages_test test;
  6035. + void *arg;
  6036. +};
  6037. +
  6038. +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
  6039. +{
  6040. + enum d_walk_ret ret;
  6041. + struct ac_dpages_arg *arg = _arg;
  6042. +
  6043. + ret = D_WALK_CONTINUE;
  6044. + if (dentry->d_sb == arg->sb
  6045. + && !IS_ROOT(dentry)
  6046. + && au_dcount(dentry) > 0
  6047. + && au_di(dentry)
  6048. + && (!arg->test || arg->test(dentry, arg->arg))) {
  6049. + arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
  6050. + if (unlikely(arg->err))
  6051. + ret = D_WALK_QUIT;
  6052. + }
  6053. +
  6054. + return ret;
  6055. +}
  6056. +
  6057. +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
  6058. + au_dpages_test test, void *arg)
  6059. +{
  6060. + struct ac_dpages_arg args = {
  6061. + .err = 0,
  6062. + .dpages = dpages,
  6063. + .sb = root->d_sb,
  6064. + .test = test,
  6065. + .arg = arg
  6066. + };
  6067. +
  6068. + d_walk(root, &args, au_call_dpages_append);
  6069. +
  6070. + return args.err;
  6071. +}
  6072. +
  6073. +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
  6074. + int do_include, au_dpages_test test, void *arg)
  6075. +{
  6076. + int err;
  6077. +
  6078. + err = 0;
  6079. + write_seqlock(&rename_lock);
  6080. + spin_lock(&dentry->d_lock);
  6081. + if (do_include
  6082. + && au_dcount(dentry) > 0
  6083. + && (!test || test(dentry, arg)))
  6084. + err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
  6085. + spin_unlock(&dentry->d_lock);
  6086. + if (unlikely(err))
  6087. + goto out;
  6088. +
  6089. + /*
  6090. + * RCU for vfsmount is unnecessary since this is a traverse in a single
  6091. + * mount
  6092. + */
  6093. + while (!IS_ROOT(dentry)) {
  6094. + dentry = dentry->d_parent; /* rename_lock is locked */
  6095. + spin_lock(&dentry->d_lock);
  6096. + if (au_dcount(dentry) > 0
  6097. + && (!test || test(dentry, arg)))
  6098. + err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
  6099. + spin_unlock(&dentry->d_lock);
  6100. + if (unlikely(err))
  6101. + break;
  6102. + }
  6103. +
  6104. +out:
  6105. + write_sequnlock(&rename_lock);
  6106. + return err;
  6107. +}
  6108. +
  6109. +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
  6110. +{
  6111. + return au_di(dentry) && dentry->d_sb == arg;
  6112. +}
  6113. +
  6114. +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
  6115. + struct dentry *dentry, int do_include)
  6116. +{
  6117. + return au_dcsub_pages_rev(dpages, dentry, do_include,
  6118. + au_dcsub_dpages_aufs, dentry->d_sb);
  6119. +}
  6120. +
  6121. +int au_test_subdir(struct dentry *d1, struct dentry *d2)
  6122. +{
  6123. + struct path path[2] = {
  6124. + {
  6125. + .dentry = d1
  6126. + },
  6127. + {
  6128. + .dentry = d2
  6129. + }
  6130. + };
  6131. +
  6132. + return path_is_under(path + 0, path + 1);
  6133. +}
  6134. --- /dev/null
  6135. +++ linux-4.19/fs/aufs/dcsub.h 2018-11-20 09:35:15.012195503 +0200
  6136. @@ -0,0 +1,137 @@
  6137. +/* SPDX-License-Identifier: GPL-2.0 */
  6138. +/*
  6139. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  6140. + *
  6141. + * This program, aufs is free software; you can redistribute it and/or modify
  6142. + * it under the terms of the GNU General Public License as published by
  6143. + * the Free Software Foundation; either version 2 of the License, or
  6144. + * (at your option) any later version.
  6145. + *
  6146. + * This program is distributed in the hope that it will be useful,
  6147. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6148. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6149. + * GNU General Public License for more details.
  6150. + *
  6151. + * You should have received a copy of the GNU General Public License
  6152. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  6153. + */
  6154. +
  6155. +/*
  6156. + * sub-routines for dentry cache
  6157. + */
  6158. +
  6159. +#ifndef __AUFS_DCSUB_H__
  6160. +#define __AUFS_DCSUB_H__
  6161. +
  6162. +#ifdef __KERNEL__
  6163. +
  6164. +#include <linux/dcache.h>
  6165. +#include <linux/fs.h>
  6166. +
  6167. +struct au_dpage {
  6168. + int ndentry;
  6169. + struct dentry **dentries;
  6170. +};
  6171. +
  6172. +struct au_dcsub_pages {
  6173. + int ndpage;
  6174. + struct au_dpage *dpages;
  6175. +};
  6176. +
  6177. +/* ---------------------------------------------------------------------- */
  6178. +
  6179. +/* dcsub.c */
  6180. +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
  6181. +void au_dpages_free(struct au_dcsub_pages *dpages);
  6182. +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
  6183. +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
  6184. + au_dpages_test test, void *arg);
  6185. +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
  6186. + int do_include, au_dpages_test test, void *arg);
  6187. +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
  6188. + struct dentry *dentry, int do_include);
  6189. +int au_test_subdir(struct dentry *d1, struct dentry *d2);
  6190. +
  6191. +/* ---------------------------------------------------------------------- */
  6192. +
  6193. +/*
  6194. + * todo: in linux-3.13, several similar (but faster) helpers are added to
  6195. + * include/linux/dcache.h. Try them (in the future).
  6196. + */
  6197. +
  6198. +static inline int au_d_hashed_positive(struct dentry *d)
  6199. +{
  6200. + int err;
  6201. + struct inode *inode = d_inode(d);
  6202. +
  6203. + err = 0;
  6204. + if (unlikely(d_unhashed(d)
  6205. + || d_is_negative(d)
  6206. + || !inode->i_nlink))
  6207. + err = -ENOENT;
  6208. + return err;
  6209. +}
  6210. +
  6211. +static inline int au_d_linkable(struct dentry *d)
  6212. +{
  6213. + int err;
  6214. + struct inode *inode = d_inode(d);
  6215. +
  6216. + err = au_d_hashed_positive(d);
  6217. + if (err
  6218. + && d_is_positive(d)
  6219. + && (inode->i_state & I_LINKABLE))
  6220. + err = 0;
  6221. + return err;
  6222. +}
  6223. +
  6224. +static inline int au_d_alive(struct dentry *d)
  6225. +{
  6226. + int err;
  6227. + struct inode *inode;
  6228. +
  6229. + err = 0;
  6230. + if (!IS_ROOT(d))
  6231. + err = au_d_hashed_positive(d);
  6232. + else {
  6233. + inode = d_inode(d);
  6234. + if (unlikely(d_unlinked(d)
  6235. + || d_is_negative(d)
  6236. + || !inode->i_nlink))
  6237. + err = -ENOENT;
  6238. + }
  6239. + return err;
  6240. +}
  6241. +
  6242. +static inline int au_alive_dir(struct dentry *d)
  6243. +{
  6244. + int err;
  6245. +
  6246. + err = au_d_alive(d);
  6247. + if (unlikely(err || IS_DEADDIR(d_inode(d))))
  6248. + err = -ENOENT;
  6249. + return err;
  6250. +}
  6251. +
  6252. +static inline int au_qstreq(struct qstr *a, struct qstr *b)
  6253. +{
  6254. + return a->len == b->len
  6255. + && !memcmp(a->name, b->name, a->len);
  6256. +}
  6257. +
  6258. +/*
  6259. + * by the commit
  6260. + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
  6261. + * taking d_lock
  6262. + * the type of d_lockref.count became int, but the inlined function d_count()
  6263. + * still returns unsigned int.
  6264. + * I don't know why. Maybe it is for every d_count() users?
  6265. + * Anyway au_dcount() lives on.
  6266. + */
  6267. +static inline int au_dcount(struct dentry *d)
  6268. +{
  6269. + return (int)d_count(d);
  6270. +}
  6271. +
  6272. +#endif /* __KERNEL__ */
  6273. +#endif /* __AUFS_DCSUB_H__ */
  6274. --- /dev/null
  6275. +++ linux-4.19/fs/aufs/debug.c 2018-11-20 09:35:15.013195503 +0200
  6276. @@ -0,0 +1,440 @@
  6277. +// SPDX-License-Identifier: GPL-2.0
  6278. +/*
  6279. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  6280. + *
  6281. + * This program, aufs is free software; you can redistribute it and/or modify
  6282. + * it under the terms of the GNU General Public License as published by
  6283. + * the Free Software Foundation; either version 2 of the License, or
  6284. + * (at your option) any later version.
  6285. + *
  6286. + * This program is distributed in the hope that it will be useful,
  6287. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6288. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6289. + * GNU General Public License for more details.
  6290. + *
  6291. + * You should have received a copy of the GNU General Public License
  6292. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  6293. + */
  6294. +
  6295. +/*
  6296. + * debug print functions
  6297. + */
  6298. +
  6299. +#include "aufs.h"
  6300. +
  6301. +/* Returns 0, or -errno. arg is in kp->arg. */
  6302. +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
  6303. +{
  6304. + int err, n;
  6305. +
  6306. + err = kstrtoint(val, 0, &n);
  6307. + if (!err) {
  6308. + if (n > 0)
  6309. + au_debug_on();
  6310. + else
  6311. + au_debug_off();
  6312. + }
  6313. + return err;
  6314. +}
  6315. +
  6316. +/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
  6317. +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
  6318. +{
  6319. + atomic_t *a;
  6320. +
  6321. + a = kp->arg;
  6322. + return sprintf(buffer, "%d", atomic_read(a));
  6323. +}
  6324. +
  6325. +static struct kernel_param_ops param_ops_atomic_t = {
  6326. + .set = param_atomic_t_set,
  6327. + .get = param_atomic_t_get
  6328. + /* void (*free)(void *arg) */
  6329. +};
  6330. +
  6331. +atomic_t aufs_debug = ATOMIC_INIT(0);
  6332. +MODULE_PARM_DESC(debug, "debug print");
  6333. +module_param_named(debug, aufs_debug, atomic_t, 0664);
  6334. +
  6335. +DEFINE_MUTEX(au_dbg_mtx); /* just to serialize the dbg msgs */
  6336. +char *au_plevel = KERN_DEBUG;
  6337. +#define dpri(fmt, ...) do { \
  6338. + if ((au_plevel \
  6339. + && strcmp(au_plevel, KERN_DEBUG)) \
  6340. + || au_debug_test()) \
  6341. + printk("%s" fmt, au_plevel, ##__VA_ARGS__); \
  6342. +} while (0)
  6343. +
  6344. +/* ---------------------------------------------------------------------- */
  6345. +
  6346. +void au_dpri_whlist(struct au_nhash *whlist)
  6347. +{
  6348. + unsigned long ul, n;
  6349. + struct hlist_head *head;
  6350. + struct au_vdir_wh *pos;
  6351. +
  6352. + n = whlist->nh_num;
  6353. + head = whlist->nh_head;
  6354. + for (ul = 0; ul < n; ul++) {
  6355. + hlist_for_each_entry(pos, head, wh_hash)
  6356. + dpri("b%d, %.*s, %d\n",
  6357. + pos->wh_bindex,
  6358. + pos->wh_str.len, pos->wh_str.name,
  6359. + pos->wh_str.len);
  6360. + head++;
  6361. + }
  6362. +}
  6363. +
  6364. +void au_dpri_vdir(struct au_vdir *vdir)
  6365. +{
  6366. + unsigned long ul;
  6367. + union au_vdir_deblk_p p;
  6368. + unsigned char *o;
  6369. +
  6370. + if (!vdir || IS_ERR(vdir)) {
  6371. + dpri("err %ld\n", PTR_ERR(vdir));
  6372. + return;
  6373. + }
  6374. +
  6375. + dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
  6376. + vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
  6377. + vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
  6378. + for (ul = 0; ul < vdir->vd_nblk; ul++) {
  6379. + p.deblk = vdir->vd_deblk[ul];
  6380. + o = p.deblk;
  6381. + dpri("[%lu]: %p\n", ul, o);
  6382. + }
  6383. +}
  6384. +
  6385. +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
  6386. + struct dentry *wh)
  6387. +{
  6388. + char *n = NULL;
  6389. + int l = 0;
  6390. +
  6391. + if (!inode || IS_ERR(inode)) {
  6392. + dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
  6393. + return -1;
  6394. + }
  6395. +
  6396. + /* the type of i_blocks depends upon CONFIG_LBDAF */
  6397. + BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
  6398. + && sizeof(inode->i_blocks) != sizeof(u64));
  6399. + if (wh) {
  6400. + n = (void *)wh->d_name.name;
  6401. + l = wh->d_name.len;
  6402. + }
  6403. +
  6404. + dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
  6405. + " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
  6406. + bindex, inode,
  6407. + inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
  6408. + atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
  6409. + i_size_read(inode), (unsigned long long)inode->i_blocks,
  6410. + hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
  6411. + inode->i_mapping ? inode->i_mapping->nrpages : 0,
  6412. + inode->i_state, inode->i_flags, inode_peek_iversion(inode),
  6413. + inode->i_generation,
  6414. + l ? ", wh " : "", l, n);
  6415. + return 0;
  6416. +}
  6417. +
  6418. +void au_dpri_inode(struct inode *inode)
  6419. +{
  6420. + struct au_iinfo *iinfo;
  6421. + struct au_hinode *hi;
  6422. + aufs_bindex_t bindex;
  6423. + int err, hn;
  6424. +
  6425. + err = do_pri_inode(-1, inode, -1, NULL);
  6426. + if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
  6427. + return;
  6428. +
  6429. + iinfo = au_ii(inode);
  6430. + dpri("i-1: btop %d, bbot %d, gen %d\n",
  6431. + iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
  6432. + if (iinfo->ii_btop < 0)
  6433. + return;
  6434. + hn = 0;
  6435. + for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
  6436. + hi = au_hinode(iinfo, bindex);
  6437. + hn = !!au_hn(hi);
  6438. + do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
  6439. + }
  6440. +}
  6441. +
  6442. +void au_dpri_dalias(struct inode *inode)
  6443. +{
  6444. + struct dentry *d;
  6445. +
  6446. + spin_lock(&inode->i_lock);
  6447. + hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
  6448. + au_dpri_dentry(d);
  6449. + spin_unlock(&inode->i_lock);
  6450. +}
  6451. +
  6452. +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
  6453. +{
  6454. + struct dentry *wh = NULL;
  6455. + int hn;
  6456. + struct inode *inode;
  6457. + struct au_iinfo *iinfo;
  6458. + struct au_hinode *hi;
  6459. +
  6460. + if (!dentry || IS_ERR(dentry)) {
  6461. + dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
  6462. + return -1;
  6463. + }
  6464. + /* do not call dget_parent() here */
  6465. + /* note: access d_xxx without d_lock */
  6466. + dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
  6467. + bindex, dentry, dentry,
  6468. + dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
  6469. + au_dcount(dentry), dentry->d_flags,
  6470. + d_unhashed(dentry) ? "un" : "");
  6471. + hn = -1;
  6472. + inode = NULL;
  6473. + if (d_is_positive(dentry))
  6474. + inode = d_inode(dentry);
  6475. + if (inode
  6476. + && au_test_aufs(dentry->d_sb)
  6477. + && bindex >= 0
  6478. + && !au_is_bad_inode(inode)) {
  6479. + iinfo = au_ii(inode);
  6480. + hi = au_hinode(iinfo, bindex);
  6481. + hn = !!au_hn(hi);
  6482. + wh = hi->hi_whdentry;
  6483. + }
  6484. + do_pri_inode(bindex, inode, hn, wh);
  6485. + return 0;
  6486. +}
  6487. +
  6488. +void au_dpri_dentry(struct dentry *dentry)
  6489. +{
  6490. + struct au_dinfo *dinfo;
  6491. + aufs_bindex_t bindex;
  6492. + int err;
  6493. +
  6494. + err = do_pri_dentry(-1, dentry);
  6495. + if (err || !au_test_aufs(dentry->d_sb))
  6496. + return;
  6497. +
  6498. + dinfo = au_di(dentry);
  6499. + if (!dinfo)
  6500. + return;
  6501. + dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
  6502. + dinfo->di_btop, dinfo->di_bbot,
  6503. + dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
  6504. + dinfo->di_tmpfile);
  6505. + if (dinfo->di_btop < 0)
  6506. + return;
  6507. + for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
  6508. + do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
  6509. +}
  6510. +
  6511. +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
  6512. +{
  6513. + char a[32];
  6514. +
  6515. + if (!file || IS_ERR(file)) {
  6516. + dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
  6517. + return -1;
  6518. + }
  6519. + a[0] = 0;
  6520. + if (bindex < 0
  6521. + && !IS_ERR_OR_NULL(file->f_path.dentry)
  6522. + && au_test_aufs(file->f_path.dentry->d_sb)
  6523. + && au_fi(file))
  6524. + snprintf(a, sizeof(a), ", gen %d, mmapped %d",
  6525. + au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
  6526. + dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
  6527. + bindex, file->f_mode, file->f_flags, (long)file_count(file),
  6528. + file->f_version, file->f_pos, a);
  6529. + if (!IS_ERR_OR_NULL(file->f_path.dentry))
  6530. + do_pri_dentry(bindex, file->f_path.dentry);
  6531. + return 0;
  6532. +}
  6533. +
  6534. +void au_dpri_file(struct file *file)
  6535. +{
  6536. + struct au_finfo *finfo;
  6537. + struct au_fidir *fidir;
  6538. + struct au_hfile *hfile;
  6539. + aufs_bindex_t bindex;
  6540. + int err;
  6541. +
  6542. + err = do_pri_file(-1, file);
  6543. + if (err
  6544. + || IS_ERR_OR_NULL(file->f_path.dentry)
  6545. + || !au_test_aufs(file->f_path.dentry->d_sb))
  6546. + return;
  6547. +
  6548. + finfo = au_fi(file);
  6549. + if (!finfo)
  6550. + return;
  6551. + if (finfo->fi_btop < 0)
  6552. + return;
  6553. + fidir = finfo->fi_hdir;
  6554. + if (!fidir)
  6555. + do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
  6556. + else
  6557. + for (bindex = finfo->fi_btop;
  6558. + bindex >= 0 && bindex <= fidir->fd_bbot;
  6559. + bindex++) {
  6560. + hfile = fidir->fd_hfile + bindex;
  6561. + do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
  6562. + }
  6563. +}
  6564. +
  6565. +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
  6566. +{
  6567. + struct vfsmount *mnt;
  6568. + struct super_block *sb;
  6569. +
  6570. + if (!br || IS_ERR(br))
  6571. + goto out;
  6572. + mnt = au_br_mnt(br);
  6573. + if (!mnt || IS_ERR(mnt))
  6574. + goto out;
  6575. + sb = mnt->mnt_sb;
  6576. + if (!sb || IS_ERR(sb))
  6577. + goto out;
  6578. +
  6579. + dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
  6580. + "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
  6581. + "xino %d\n",
  6582. + bindex, br->br_perm, br->br_id, br->br_wbr,
  6583. + au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
  6584. + sb->s_flags, sb->s_count,
  6585. + atomic_read(&sb->s_active),
  6586. + !!au_xino_file(br->br_xino, /*idx*/-1));
  6587. + return 0;
  6588. +
  6589. +out:
  6590. + dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
  6591. + return -1;
  6592. +}
  6593. +
  6594. +void au_dpri_sb(struct super_block *sb)
  6595. +{
  6596. + struct au_sbinfo *sbinfo;
  6597. + aufs_bindex_t bindex;
  6598. + int err;
  6599. + /* to reduce stack size */
  6600. + struct {
  6601. + struct vfsmount mnt;
  6602. + struct au_branch fake;
  6603. + } *a;
  6604. +
  6605. + /* this function can be called from magic sysrq */
  6606. + a = kzalloc(sizeof(*a), GFP_ATOMIC);
  6607. + if (unlikely(!a)) {
  6608. + dpri("no memory\n");
  6609. + return;
  6610. + }
  6611. +
  6612. + a->mnt.mnt_sb = sb;
  6613. + a->fake.br_path.mnt = &a->mnt;
  6614. + err = do_pri_br(-1, &a->fake);
  6615. + kfree(a);
  6616. + dpri("dev 0x%x\n", sb->s_dev);
  6617. + if (err || !au_test_aufs(sb))
  6618. + return;
  6619. +
  6620. + sbinfo = au_sbi(sb);
  6621. + if (!sbinfo)
  6622. + return;
  6623. + dpri("nw %d, gen %u, kobj %d\n",
  6624. + atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
  6625. + kref_read(&sbinfo->si_kobj.kref));
  6626. + for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
  6627. + do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
  6628. +}
  6629. +
  6630. +/* ---------------------------------------------------------------------- */
  6631. +
  6632. +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
  6633. +{
  6634. + struct inode *h_inode, *inode = d_inode(dentry);
  6635. + struct dentry *h_dentry;
  6636. + aufs_bindex_t bindex, bbot, bi;
  6637. +
  6638. + if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
  6639. + return;
  6640. +
  6641. + bbot = au_dbbot(dentry);
  6642. + bi = au_ibbot(inode);
  6643. + if (bi < bbot)
  6644. + bbot = bi;
  6645. + bindex = au_dbtop(dentry);
  6646. + bi = au_ibtop(inode);
  6647. + if (bi > bindex)
  6648. + bindex = bi;
  6649. +
  6650. + for (; bindex <= bbot; bindex++) {
  6651. + h_dentry = au_h_dptr(dentry, bindex);
  6652. + if (!h_dentry)
  6653. + continue;
  6654. + h_inode = au_h_iptr(inode, bindex);
  6655. + if (unlikely(h_inode != d_inode(h_dentry))) {
  6656. + au_debug_on();
  6657. + AuDbg("b%d, %s:%d\n", bindex, func, line);
  6658. + AuDbgDentry(dentry);
  6659. + AuDbgInode(inode);
  6660. + au_debug_off();
  6661. + BUG();
  6662. + }
  6663. + }
  6664. +}
  6665. +
  6666. +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
  6667. +{
  6668. + int err, i, j;
  6669. + struct au_dcsub_pages dpages;
  6670. + struct au_dpage *dpage;
  6671. + struct dentry **dentries;
  6672. +
  6673. + err = au_dpages_init(&dpages, GFP_NOFS);
  6674. + AuDebugOn(err);
  6675. + err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
  6676. + AuDebugOn(err);
  6677. + for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
  6678. + dpage = dpages.dpages + i;
  6679. + dentries = dpage->dentries;
  6680. + for (j = dpage->ndentry - 1; !err && j >= 0; j--)
  6681. + AuDebugOn(au_digen_test(dentries[j], sigen));
  6682. + }
  6683. + au_dpages_free(&dpages);
  6684. +}
  6685. +
  6686. +void au_dbg_verify_kthread(void)
  6687. +{
  6688. + if (au_wkq_test()) {
  6689. + au_dbg_blocked();
  6690. + /*
  6691. + * It may be recursive, but udba=notify between two aufs mounts,
  6692. + * where a single ro branch is shared, is not a problem.
  6693. + */
  6694. + /* WARN_ON(1); */
  6695. + }
  6696. +}
  6697. +
  6698. +/* ---------------------------------------------------------------------- */
  6699. +
  6700. +int __init au_debug_init(void)
  6701. +{
  6702. + aufs_bindex_t bindex;
  6703. + struct au_vdir_destr destr;
  6704. +
  6705. + bindex = -1;
  6706. + AuDebugOn(bindex >= 0);
  6707. +
  6708. + destr.len = -1;
  6709. + AuDebugOn(destr.len < NAME_MAX);
  6710. +
  6711. +#ifdef CONFIG_4KSTACKS
  6712. + pr_warn("CONFIG_4KSTACKS is defined.\n");
  6713. +#endif
  6714. +
  6715. + return 0;
  6716. +}
  6717. --- /dev/null
  6718. +++ linux-4.19/fs/aufs/debug.h 2018-11-20 09:35:15.013195503 +0200
  6719. @@ -0,0 +1,226 @@
  6720. +/* SPDX-License-Identifier: GPL-2.0 */
  6721. +/*
  6722. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  6723. + *
  6724. + * This program, aufs is free software; you can redistribute it and/or modify
  6725. + * it under the terms of the GNU General Public License as published by
  6726. + * the Free Software Foundation; either version 2 of the License, or
  6727. + * (at your option) any later version.
  6728. + *
  6729. + * This program is distributed in the hope that it will be useful,
  6730. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6731. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6732. + * GNU General Public License for more details.
  6733. + *
  6734. + * You should have received a copy of the GNU General Public License
  6735. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  6736. + */
  6737. +
  6738. +/*
  6739. + * debug print functions
  6740. + */
  6741. +
  6742. +#ifndef __AUFS_DEBUG_H__
  6743. +#define __AUFS_DEBUG_H__
  6744. +
  6745. +#ifdef __KERNEL__
  6746. +
  6747. +#include <linux/atomic.h>
  6748. +#include <linux/module.h>
  6749. +#include <linux/kallsyms.h>
  6750. +#include <linux/sysrq.h>
  6751. +
  6752. +#ifdef CONFIG_AUFS_DEBUG
  6753. +#define AuDebugOn(a) BUG_ON(a)
  6754. +
  6755. +/* module parameter */
  6756. +extern atomic_t aufs_debug;
  6757. +static inline void au_debug_on(void)
  6758. +{
  6759. + atomic_inc(&aufs_debug);
  6760. +}
  6761. +static inline void au_debug_off(void)
  6762. +{
  6763. + atomic_dec_if_positive(&aufs_debug);
  6764. +}
  6765. +
  6766. +static inline int au_debug_test(void)
  6767. +{
  6768. + return atomic_read(&aufs_debug) > 0;
  6769. +}
  6770. +#else
  6771. +#define AuDebugOn(a) do {} while (0)
  6772. +AuStubVoid(au_debug_on, void)
  6773. +AuStubVoid(au_debug_off, void)
  6774. +AuStubInt0(au_debug_test, void)
  6775. +#endif /* CONFIG_AUFS_DEBUG */
  6776. +
  6777. +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
  6778. +
  6779. +/* ---------------------------------------------------------------------- */
  6780. +
  6781. +/* debug print */
  6782. +
  6783. +#define AuDbg(fmt, ...) do { \
  6784. + if (au_debug_test()) \
  6785. + pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
  6786. +} while (0)
  6787. +#define AuLabel(l) AuDbg(#l "\n")
  6788. +#define AuIOErr(fmt, ...) pr_err("I/O Error, " fmt, ##__VA_ARGS__)
  6789. +#define AuWarn1(fmt, ...) do { \
  6790. + static unsigned char _c; \
  6791. + if (!_c++) \
  6792. + pr_warn(fmt, ##__VA_ARGS__); \
  6793. +} while (0)
  6794. +
  6795. +#define AuErr1(fmt, ...) do { \
  6796. + static unsigned char _c; \
  6797. + if (!_c++) \
  6798. + pr_err(fmt, ##__VA_ARGS__); \
  6799. +} while (0)
  6800. +
  6801. +#define AuIOErr1(fmt, ...) do { \
  6802. + static unsigned char _c; \
  6803. + if (!_c++) \
  6804. + AuIOErr(fmt, ##__VA_ARGS__); \
  6805. +} while (0)
  6806. +
  6807. +#define AuUnsupportMsg "This operation is not supported." \
  6808. + " Please report this application to aufs-users ML."
  6809. +#define AuUnsupport(fmt, ...) do { \
  6810. + pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
  6811. + dump_stack(); \
  6812. +} while (0)
  6813. +
  6814. +#define AuTraceErr(e) do { \
  6815. + if (unlikely((e) < 0)) \
  6816. + AuDbg("err %d\n", (int)(e)); \
  6817. +} while (0)
  6818. +
  6819. +#define AuTraceErrPtr(p) do { \
  6820. + if (IS_ERR(p)) \
  6821. + AuDbg("err %ld\n", PTR_ERR(p)); \
  6822. +} while (0)
  6823. +
  6824. +/* dirty macros for debug print, use with "%.*s" and caution */
  6825. +#define AuLNPair(qstr) (qstr)->len, (qstr)->name
  6826. +
  6827. +/* ---------------------------------------------------------------------- */
  6828. +
  6829. +struct dentry;
  6830. +#ifdef CONFIG_AUFS_DEBUG
  6831. +extern struct mutex au_dbg_mtx;
  6832. +extern char *au_plevel;
  6833. +struct au_nhash;
  6834. +void au_dpri_whlist(struct au_nhash *whlist);
  6835. +struct au_vdir;
  6836. +void au_dpri_vdir(struct au_vdir *vdir);
  6837. +struct inode;
  6838. +void au_dpri_inode(struct inode *inode);
  6839. +void au_dpri_dalias(struct inode *inode);
  6840. +void au_dpri_dentry(struct dentry *dentry);
  6841. +struct file;
  6842. +void au_dpri_file(struct file *filp);
  6843. +struct super_block;
  6844. +void au_dpri_sb(struct super_block *sb);
  6845. +
  6846. +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
  6847. +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
  6848. +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
  6849. +void au_dbg_verify_kthread(void);
  6850. +
  6851. +int __init au_debug_init(void);
  6852. +
  6853. +#define AuDbgWhlist(w) do { \
  6854. + mutex_lock(&au_dbg_mtx); \
  6855. + AuDbg(#w "\n"); \
  6856. + au_dpri_whlist(w); \
  6857. + mutex_unlock(&au_dbg_mtx); \
  6858. +} while (0)
  6859. +
  6860. +#define AuDbgVdir(v) do { \
  6861. + mutex_lock(&au_dbg_mtx); \
  6862. + AuDbg(#v "\n"); \
  6863. + au_dpri_vdir(v); \
  6864. + mutex_unlock(&au_dbg_mtx); \
  6865. +} while (0)
  6866. +
  6867. +#define AuDbgInode(i) do { \
  6868. + mutex_lock(&au_dbg_mtx); \
  6869. + AuDbg(#i "\n"); \
  6870. + au_dpri_inode(i); \
  6871. + mutex_unlock(&au_dbg_mtx); \
  6872. +} while (0)
  6873. +
  6874. +#define AuDbgDAlias(i) do { \
  6875. + mutex_lock(&au_dbg_mtx); \
  6876. + AuDbg(#i "\n"); \
  6877. + au_dpri_dalias(i); \
  6878. + mutex_unlock(&au_dbg_mtx); \
  6879. +} while (0)
  6880. +
  6881. +#define AuDbgDentry(d) do { \
  6882. + mutex_lock(&au_dbg_mtx); \
  6883. + AuDbg(#d "\n"); \
  6884. + au_dpri_dentry(d); \
  6885. + mutex_unlock(&au_dbg_mtx); \
  6886. +} while (0)
  6887. +
  6888. +#define AuDbgFile(f) do { \
  6889. + mutex_lock(&au_dbg_mtx); \
  6890. + AuDbg(#f "\n"); \
  6891. + au_dpri_file(f); \
  6892. + mutex_unlock(&au_dbg_mtx); \
  6893. +} while (0)
  6894. +
  6895. +#define AuDbgSb(sb) do { \
  6896. + mutex_lock(&au_dbg_mtx); \
  6897. + AuDbg(#sb "\n"); \
  6898. + au_dpri_sb(sb); \
  6899. + mutex_unlock(&au_dbg_mtx); \
  6900. +} while (0)
  6901. +
  6902. +#define AuDbgSym(addr) do { \
  6903. + char sym[KSYM_SYMBOL_LEN]; \
  6904. + sprint_symbol(sym, (unsigned long)addr); \
  6905. + AuDbg("%s\n", sym); \
  6906. +} while (0)
  6907. +#else
  6908. +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
  6909. +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
  6910. +AuStubVoid(au_dbg_verify_kthread, void)
  6911. +AuStubInt0(__init au_debug_init, void)
  6912. +
  6913. +#define AuDbgWhlist(w) do {} while (0)
  6914. +#define AuDbgVdir(v) do {} while (0)
  6915. +#define AuDbgInode(i) do {} while (0)
  6916. +#define AuDbgDAlias(i) do {} while (0)
  6917. +#define AuDbgDentry(d) do {} while (0)
  6918. +#define AuDbgFile(f) do {} while (0)
  6919. +#define AuDbgSb(sb) do {} while (0)
  6920. +#define AuDbgSym(addr) do {} while (0)
  6921. +#endif /* CONFIG_AUFS_DEBUG */
  6922. +
  6923. +/* ---------------------------------------------------------------------- */
  6924. +
  6925. +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
  6926. +int __init au_sysrq_init(void);
  6927. +void au_sysrq_fin(void);
  6928. +
  6929. +#ifdef CONFIG_HW_CONSOLE
  6930. +#define au_dbg_blocked() do { \
  6931. + WARN_ON(1); \
  6932. + handle_sysrq('w'); \
  6933. +} while (0)
  6934. +#else
  6935. +AuStubVoid(au_dbg_blocked, void)
  6936. +#endif
  6937. +
  6938. +#else
  6939. +AuStubInt0(__init au_sysrq_init, void)
  6940. +AuStubVoid(au_sysrq_fin, void)
  6941. +AuStubVoid(au_dbg_blocked, void)
  6942. +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
  6943. +
  6944. +#endif /* __KERNEL__ */
  6945. +#endif /* __AUFS_DEBUG_H__ */
  6946. --- /dev/null
  6947. +++ linux-4.19/fs/aufs/dentry.c 2018-11-20 09:35:15.014195503 +0200
  6948. @@ -0,0 +1,1153 @@
  6949. +// SPDX-License-Identifier: GPL-2.0
  6950. +/*
  6951. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  6952. + *
  6953. + * This program, aufs is free software; you can redistribute it and/or modify
  6954. + * it under the terms of the GNU General Public License as published by
  6955. + * the Free Software Foundation; either version 2 of the License, or
  6956. + * (at your option) any later version.
  6957. + *
  6958. + * This program is distributed in the hope that it will be useful,
  6959. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6960. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6961. + * GNU General Public License for more details.
  6962. + *
  6963. + * You should have received a copy of the GNU General Public License
  6964. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  6965. + */
  6966. +
  6967. +/*
  6968. + * lookup and dentry operations
  6969. + */
  6970. +
  6971. +#include <linux/namei.h>
  6972. +#include "aufs.h"
  6973. +
  6974. +/*
  6975. + * returns positive/negative dentry, NULL or an error.
  6976. + * NULL means whiteout-ed or not-found.
  6977. + */
  6978. +static struct dentry*
  6979. +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
  6980. + aufs_bindex_t bindex, struct au_do_lookup_args *args)
  6981. +{
  6982. + struct dentry *h_dentry;
  6983. + struct inode *h_inode;
  6984. + struct au_branch *br;
  6985. + int wh_found, opq;
  6986. + unsigned char wh_able;
  6987. + const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
  6988. + const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
  6989. + IGNORE_PERM);
  6990. +
  6991. + wh_found = 0;
  6992. + br = au_sbr(dentry->d_sb, bindex);
  6993. + wh_able = !!au_br_whable(br->br_perm);
  6994. + if (wh_able)
  6995. + wh_found = au_wh_test(h_parent, &args->whname, ignore_perm);
  6996. + h_dentry = ERR_PTR(wh_found);
  6997. + if (!wh_found)
  6998. + goto real_lookup;
  6999. + if (unlikely(wh_found < 0))
  7000. + goto out;
  7001. +
  7002. + /* We found a whiteout */
  7003. + /* au_set_dbbot(dentry, bindex); */
  7004. + au_set_dbwh(dentry, bindex);
  7005. + if (!allow_neg)
  7006. + return NULL; /* success */
  7007. +
  7008. +real_lookup:
  7009. + if (!ignore_perm)
  7010. + h_dentry = vfsub_lkup_one(args->name, h_parent);
  7011. + else
  7012. + h_dentry = au_sio_lkup_one(args->name, h_parent);
  7013. + if (IS_ERR(h_dentry)) {
  7014. + if (PTR_ERR(h_dentry) == -ENAMETOOLONG
  7015. + && !allow_neg)
  7016. + h_dentry = NULL;
  7017. + goto out;
  7018. + }
  7019. +
  7020. + h_inode = d_inode(h_dentry);
  7021. + if (d_is_negative(h_dentry)) {
  7022. + if (!allow_neg)
  7023. + goto out_neg;
  7024. + } else if (wh_found
  7025. + || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
  7026. + goto out_neg;
  7027. + else if (au_ftest_lkup(args->flags, DIRREN)
  7028. + /* && h_inode */
  7029. + && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
  7030. + AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
  7031. + (unsigned long long)h_inode->i_ino);
  7032. + goto out_neg;
  7033. + }
  7034. +
  7035. + if (au_dbbot(dentry) <= bindex)
  7036. + au_set_dbbot(dentry, bindex);
  7037. + if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
  7038. + au_set_dbtop(dentry, bindex);
  7039. + au_set_h_dptr(dentry, bindex, h_dentry);
  7040. +
  7041. + if (!d_is_dir(h_dentry)
  7042. + || !wh_able
  7043. + || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
  7044. + goto out; /* success */
  7045. +
  7046. + inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
  7047. + opq = au_diropq_test(h_dentry);
  7048. + inode_unlock_shared(h_inode);
  7049. + if (opq > 0)
  7050. + au_set_dbdiropq(dentry, bindex);
  7051. + else if (unlikely(opq < 0)) {
  7052. + au_set_h_dptr(dentry, bindex, NULL);
  7053. + h_dentry = ERR_PTR(opq);
  7054. + }
  7055. + goto out;
  7056. +
  7057. +out_neg:
  7058. + dput(h_dentry);
  7059. + h_dentry = NULL;
  7060. +out:
  7061. + return h_dentry;
  7062. +}
  7063. +
  7064. +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
  7065. +{
  7066. + if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
  7067. + && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
  7068. + return -EPERM;
  7069. + return 0;
  7070. +}
  7071. +
  7072. +/*
  7073. + * returns the number of lower positive dentries,
  7074. + * otherwise an error.
  7075. + * can be called at unlinking with @type is zero.
  7076. + */
  7077. +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
  7078. + unsigned int flags)
  7079. +{
  7080. + int npositive, err;
  7081. + aufs_bindex_t bindex, btail, bdiropq;
  7082. + unsigned char isdir, dirperm1, dirren;
  7083. + struct au_do_lookup_args args = {
  7084. + .flags = flags,
  7085. + .name = &dentry->d_name
  7086. + };
  7087. + struct dentry *parent;
  7088. + struct super_block *sb;
  7089. +
  7090. + sb = dentry->d_sb;
  7091. + err = au_test_shwh(sb, args.name);
  7092. + if (unlikely(err))
  7093. + goto out;
  7094. +
  7095. + err = au_wh_name_alloc(&args.whname, args.name);
  7096. + if (unlikely(err))
  7097. + goto out;
  7098. +
  7099. + isdir = !!d_is_dir(dentry);
  7100. + dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
  7101. + dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
  7102. + if (dirren)
  7103. + au_fset_lkup(args.flags, DIRREN);
  7104. +
  7105. + npositive = 0;
  7106. + parent = dget_parent(dentry);
  7107. + btail = au_dbtaildir(parent);
  7108. + for (bindex = btop; bindex <= btail; bindex++) {
  7109. + struct dentry *h_parent, *h_dentry;
  7110. + struct inode *h_inode, *h_dir;
  7111. + struct au_branch *br;
  7112. +
  7113. + h_dentry = au_h_dptr(dentry, bindex);
  7114. + if (h_dentry) {
  7115. + if (d_is_positive(h_dentry))
  7116. + npositive++;
  7117. + break;
  7118. + }
  7119. + h_parent = au_h_dptr(parent, bindex);
  7120. + if (!h_parent || !d_is_dir(h_parent))
  7121. + continue;
  7122. +
  7123. + if (dirren) {
  7124. + /* if the inum matches, then use the prepared name */
  7125. + err = au_dr_lkup_name(&args, bindex);
  7126. + if (unlikely(err))
  7127. + goto out_parent;
  7128. + }
  7129. +
  7130. + h_dir = d_inode(h_parent);
  7131. + inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
  7132. + h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
  7133. + inode_unlock_shared(h_dir);
  7134. + err = PTR_ERR(h_dentry);
  7135. + if (IS_ERR(h_dentry))
  7136. + goto out_parent;
  7137. + if (h_dentry)
  7138. + au_fclr_lkup(args.flags, ALLOW_NEG);
  7139. + if (dirperm1)
  7140. + au_fset_lkup(args.flags, IGNORE_PERM);
  7141. +
  7142. + if (au_dbwh(dentry) == bindex)
  7143. + break;
  7144. + if (!h_dentry)
  7145. + continue;
  7146. + if (d_is_negative(h_dentry))
  7147. + continue;
  7148. + h_inode = d_inode(h_dentry);
  7149. + npositive++;
  7150. + if (!args.type)
  7151. + args.type = h_inode->i_mode & S_IFMT;
  7152. + if (args.type != S_IFDIR)
  7153. + break;
  7154. + else if (isdir) {
  7155. + /* the type of lower may be different */
  7156. + bdiropq = au_dbdiropq(dentry);
  7157. + if (bdiropq >= 0 && bdiropq <= bindex)
  7158. + break;
  7159. + }
  7160. + br = au_sbr(sb, bindex);
  7161. + if (dirren
  7162. + && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
  7163. + /*add_ent*/NULL)) {
  7164. + /* prepare next name to lookup */
  7165. + err = au_dr_lkup(&args, dentry, bindex);
  7166. + if (unlikely(err))
  7167. + goto out_parent;
  7168. + }
  7169. + }
  7170. +
  7171. + if (npositive) {
  7172. + AuLabel(positive);
  7173. + au_update_dbtop(dentry);
  7174. + }
  7175. + err = npositive;
  7176. + if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
  7177. + && au_dbtop(dentry) < 0)) {
  7178. + err = -EIO;
  7179. + AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
  7180. + dentry, err);
  7181. + }
  7182. +
  7183. +out_parent:
  7184. + dput(parent);
  7185. + kfree(args.whname.name);
  7186. + if (dirren)
  7187. + au_dr_lkup_fin(&args);
  7188. +out:
  7189. + return err;
  7190. +}
  7191. +
  7192. +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
  7193. +{
  7194. + struct dentry *dentry;
  7195. + int wkq_err;
  7196. +
  7197. + if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
  7198. + dentry = vfsub_lkup_one(name, parent);
  7199. + else {
  7200. + struct vfsub_lkup_one_args args = {
  7201. + .errp = &dentry,
  7202. + .name = name,
  7203. + .parent = parent
  7204. + };
  7205. +
  7206. + wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
  7207. + if (unlikely(wkq_err))
  7208. + dentry = ERR_PTR(wkq_err);
  7209. + }
  7210. +
  7211. + return dentry;
  7212. +}
  7213. +
  7214. +/*
  7215. + * lookup @dentry on @bindex which should be negative.
  7216. + */
  7217. +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
  7218. +{
  7219. + int err;
  7220. + struct dentry *parent, *h_parent, *h_dentry;
  7221. + struct au_branch *br;
  7222. +
  7223. + parent = dget_parent(dentry);
  7224. + h_parent = au_h_dptr(parent, bindex);
  7225. + br = au_sbr(dentry->d_sb, bindex);
  7226. + if (wh)
  7227. + h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
  7228. + else
  7229. + h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
  7230. + err = PTR_ERR(h_dentry);
  7231. + if (IS_ERR(h_dentry))
  7232. + goto out;
  7233. + if (unlikely(d_is_positive(h_dentry))) {
  7234. + err = -EIO;
  7235. + AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
  7236. + dput(h_dentry);
  7237. + goto out;
  7238. + }
  7239. +
  7240. + err = 0;
  7241. + if (bindex < au_dbtop(dentry))
  7242. + au_set_dbtop(dentry, bindex);
  7243. + if (au_dbbot(dentry) < bindex)
  7244. + au_set_dbbot(dentry, bindex);
  7245. + au_set_h_dptr(dentry, bindex, h_dentry);
  7246. +
  7247. +out:
  7248. + dput(parent);
  7249. + return err;
  7250. +}
  7251. +
  7252. +/* ---------------------------------------------------------------------- */
  7253. +
  7254. +/* subset of struct inode */
  7255. +struct au_iattr {
  7256. + unsigned long i_ino;
  7257. + /* unsigned int i_nlink; */
  7258. + kuid_t i_uid;
  7259. + kgid_t i_gid;
  7260. + u64 i_version;
  7261. +/*
  7262. + loff_t i_size;
  7263. + blkcnt_t i_blocks;
  7264. +*/
  7265. + umode_t i_mode;
  7266. +};
  7267. +
  7268. +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
  7269. +{
  7270. + ia->i_ino = h_inode->i_ino;
  7271. + /* ia->i_nlink = h_inode->i_nlink; */
  7272. + ia->i_uid = h_inode->i_uid;
  7273. + ia->i_gid = h_inode->i_gid;
  7274. + ia->i_version = inode_query_iversion(h_inode);
  7275. +/*
  7276. + ia->i_size = h_inode->i_size;
  7277. + ia->i_blocks = h_inode->i_blocks;
  7278. +*/
  7279. + ia->i_mode = (h_inode->i_mode & S_IFMT);
  7280. +}
  7281. +
  7282. +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
  7283. +{
  7284. + return ia->i_ino != h_inode->i_ino
  7285. + /* || ia->i_nlink != h_inode->i_nlink */
  7286. + || !uid_eq(ia->i_uid, h_inode->i_uid)
  7287. + || !gid_eq(ia->i_gid, h_inode->i_gid)
  7288. + || !inode_eq_iversion(h_inode, ia->i_version)
  7289. +/*
  7290. + || ia->i_size != h_inode->i_size
  7291. + || ia->i_blocks != h_inode->i_blocks
  7292. +*/
  7293. + || ia->i_mode != (h_inode->i_mode & S_IFMT);
  7294. +}
  7295. +
  7296. +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
  7297. + struct au_branch *br)
  7298. +{
  7299. + int err;
  7300. + struct au_iattr ia;
  7301. + struct inode *h_inode;
  7302. + struct dentry *h_d;
  7303. + struct super_block *h_sb;
  7304. +
  7305. + err = 0;
  7306. + memset(&ia, -1, sizeof(ia));
  7307. + h_sb = h_dentry->d_sb;
  7308. + h_inode = NULL;
  7309. + if (d_is_positive(h_dentry)) {
  7310. + h_inode = d_inode(h_dentry);
  7311. + au_iattr_save(&ia, h_inode);
  7312. + } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
  7313. + /* nfs d_revalidate may return 0 for negative dentry */
  7314. + /* fuse d_revalidate always return 0 for negative dentry */
  7315. + goto out;
  7316. +
  7317. + /* main purpose is namei.c:cached_lookup() and d_revalidate */
  7318. + h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
  7319. + err = PTR_ERR(h_d);
  7320. + if (IS_ERR(h_d))
  7321. + goto out;
  7322. +
  7323. + err = 0;
  7324. + if (unlikely(h_d != h_dentry
  7325. + || d_inode(h_d) != h_inode
  7326. + || (h_inode && au_iattr_test(&ia, h_inode))))
  7327. + err = au_busy_or_stale();
  7328. + dput(h_d);
  7329. +
  7330. +out:
  7331. + AuTraceErr(err);
  7332. + return err;
  7333. +}
  7334. +
  7335. +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
  7336. + struct dentry *h_parent, struct au_branch *br)
  7337. +{
  7338. + int err;
  7339. +
  7340. + err = 0;
  7341. + if (udba == AuOpt_UDBA_REVAL
  7342. + && !au_test_fs_remote(h_dentry->d_sb)) {
  7343. + IMustLock(h_dir);
  7344. + err = (d_inode(h_dentry->d_parent) != h_dir);
  7345. + } else if (udba != AuOpt_UDBA_NONE)
  7346. + err = au_h_verify_dentry(h_dentry, h_parent, br);
  7347. +
  7348. + return err;
  7349. +}
  7350. +
  7351. +/* ---------------------------------------------------------------------- */
  7352. +
  7353. +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
  7354. +{
  7355. + int err;
  7356. + aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
  7357. + struct au_hdentry tmp, *p, *q;
  7358. + struct au_dinfo *dinfo;
  7359. + struct super_block *sb;
  7360. +
  7361. + DiMustWriteLock(dentry);
  7362. +
  7363. + sb = dentry->d_sb;
  7364. + dinfo = au_di(dentry);
  7365. + bbot = dinfo->di_bbot;
  7366. + bwh = dinfo->di_bwh;
  7367. + bdiropq = dinfo->di_bdiropq;
  7368. + bindex = dinfo->di_btop;
  7369. + p = au_hdentry(dinfo, bindex);
  7370. + for (; bindex <= bbot; bindex++, p++) {
  7371. + if (!p->hd_dentry)
  7372. + continue;
  7373. +
  7374. + new_bindex = au_br_index(sb, p->hd_id);
  7375. + if (new_bindex == bindex)
  7376. + continue;
  7377. +
  7378. + if (dinfo->di_bwh == bindex)
  7379. + bwh = new_bindex;
  7380. + if (dinfo->di_bdiropq == bindex)
  7381. + bdiropq = new_bindex;
  7382. + if (new_bindex < 0) {
  7383. + au_hdput(p);
  7384. + p->hd_dentry = NULL;
  7385. + continue;
  7386. + }
  7387. +
  7388. + /* swap two lower dentries, and loop again */
  7389. + q = au_hdentry(dinfo, new_bindex);
  7390. + tmp = *q;
  7391. + *q = *p;
  7392. + *p = tmp;
  7393. + if (tmp.hd_dentry) {
  7394. + bindex--;
  7395. + p--;
  7396. + }
  7397. + }
  7398. +
  7399. + dinfo->di_bwh = -1;
  7400. + if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
  7401. + dinfo->di_bwh = bwh;
  7402. +
  7403. + dinfo->di_bdiropq = -1;
  7404. + if (bdiropq >= 0
  7405. + && bdiropq <= au_sbbot(sb)
  7406. + && au_sbr_whable(sb, bdiropq))
  7407. + dinfo->di_bdiropq = bdiropq;
  7408. +
  7409. + err = -EIO;
  7410. + dinfo->di_btop = -1;
  7411. + dinfo->di_bbot = -1;
  7412. + bbot = au_dbbot(parent);
  7413. + bindex = 0;
  7414. + p = au_hdentry(dinfo, bindex);
  7415. + for (; bindex <= bbot; bindex++, p++)
  7416. + if (p->hd_dentry) {
  7417. + dinfo->di_btop = bindex;
  7418. + break;
  7419. + }
  7420. +
  7421. + if (dinfo->di_btop >= 0) {
  7422. + bindex = bbot;
  7423. + p = au_hdentry(dinfo, bindex);
  7424. + for (; bindex >= 0; bindex--, p--)
  7425. + if (p->hd_dentry) {
  7426. + dinfo->di_bbot = bindex;
  7427. + err = 0;
  7428. + break;
  7429. + }
  7430. + }
  7431. +
  7432. + return err;
  7433. +}
  7434. +
  7435. +static void au_do_hide(struct dentry *dentry)
  7436. +{
  7437. + struct inode *inode;
  7438. +
  7439. + if (d_really_is_positive(dentry)) {
  7440. + inode = d_inode(dentry);
  7441. + if (!d_is_dir(dentry)) {
  7442. + if (inode->i_nlink && !d_unhashed(dentry))
  7443. + drop_nlink(inode);
  7444. + } else {
  7445. + clear_nlink(inode);
  7446. + /* stop next lookup */
  7447. + inode->i_flags |= S_DEAD;
  7448. + }
  7449. + smp_mb(); /* necessary? */
  7450. + }
  7451. + d_drop(dentry);
  7452. +}
  7453. +
  7454. +static int au_hide_children(struct dentry *parent)
  7455. +{
  7456. + int err, i, j, ndentry;
  7457. + struct au_dcsub_pages dpages;
  7458. + struct au_dpage *dpage;
  7459. + struct dentry *dentry;
  7460. +
  7461. + err = au_dpages_init(&dpages, GFP_NOFS);
  7462. + if (unlikely(err))
  7463. + goto out;
  7464. + err = au_dcsub_pages(&dpages, parent, NULL, NULL);
  7465. + if (unlikely(err))
  7466. + goto out_dpages;
  7467. +
  7468. + /* in reverse order */
  7469. + for (i = dpages.ndpage - 1; i >= 0; i--) {
  7470. + dpage = dpages.dpages + i;
  7471. + ndentry = dpage->ndentry;
  7472. + for (j = ndentry - 1; j >= 0; j--) {
  7473. + dentry = dpage->dentries[j];
  7474. + if (dentry != parent)
  7475. + au_do_hide(dentry);
  7476. + }
  7477. + }
  7478. +
  7479. +out_dpages:
  7480. + au_dpages_free(&dpages);
  7481. +out:
  7482. + return err;
  7483. +}
  7484. +
  7485. +static void au_hide(struct dentry *dentry)
  7486. +{
  7487. + int err;
  7488. +
  7489. + AuDbgDentry(dentry);
  7490. + if (d_is_dir(dentry)) {
  7491. + /* shrink_dcache_parent(dentry); */
  7492. + err = au_hide_children(dentry);
  7493. + if (unlikely(err))
  7494. + AuIOErr("%pd, failed hiding children, ignored %d\n",
  7495. + dentry, err);
  7496. + }
  7497. + au_do_hide(dentry);
  7498. +}
  7499. +
  7500. +/*
  7501. + * By adding a dirty branch, a cached dentry may be affected in various ways.
  7502. + *
  7503. + * a dirty branch is added
  7504. + * - on the top of layers
  7505. + * - in the middle of layers
  7506. + * - to the bottom of layers
  7507. + *
  7508. + * on the added branch there exists
  7509. + * - a whiteout
  7510. + * - a diropq
  7511. + * - a same named entry
  7512. + * + exist
  7513. + * * negative --> positive
  7514. + * * positive --> positive
  7515. + * - type is unchanged
  7516. + * - type is changed
  7517. + * + doesn't exist
  7518. + * * negative --> negative
  7519. + * * positive --> negative (rejected by au_br_del() for non-dir case)
  7520. + * - none
  7521. + */
  7522. +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
  7523. + struct au_dinfo *tmp)
  7524. +{
  7525. + int err;
  7526. + aufs_bindex_t bindex, bbot;
  7527. + struct {
  7528. + struct dentry *dentry;
  7529. + struct inode *inode;
  7530. + mode_t mode;
  7531. + } orig_h, tmp_h = {
  7532. + .dentry = NULL
  7533. + };
  7534. + struct au_hdentry *hd;
  7535. + struct inode *inode, *h_inode;
  7536. + struct dentry *h_dentry;
  7537. +
  7538. + err = 0;
  7539. + AuDebugOn(dinfo->di_btop < 0);
  7540. + orig_h.mode = 0;
  7541. + orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
  7542. + orig_h.inode = NULL;
  7543. + if (d_is_positive(orig_h.dentry)) {
  7544. + orig_h.inode = d_inode(orig_h.dentry);
  7545. + orig_h.mode = orig_h.inode->i_mode & S_IFMT;
  7546. + }
  7547. + if (tmp->di_btop >= 0) {
  7548. + tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
  7549. + if (d_is_positive(tmp_h.dentry)) {
  7550. + tmp_h.inode = d_inode(tmp_h.dentry);
  7551. + tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
  7552. + }
  7553. + }
  7554. +
  7555. + inode = NULL;
  7556. + if (d_really_is_positive(dentry))
  7557. + inode = d_inode(dentry);
  7558. + if (!orig_h.inode) {
  7559. + AuDbg("negative originally\n");
  7560. + if (inode) {
  7561. + au_hide(dentry);
  7562. + goto out;
  7563. + }
  7564. + AuDebugOn(inode);
  7565. + AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
  7566. + AuDebugOn(dinfo->di_bdiropq != -1);
  7567. +
  7568. + if (!tmp_h.inode) {
  7569. + AuDbg("negative --> negative\n");
  7570. + /* should have only one negative lower */
  7571. + if (tmp->di_btop >= 0
  7572. + && tmp->di_btop < dinfo->di_btop) {
  7573. + AuDebugOn(tmp->di_btop != tmp->di_bbot);
  7574. + AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
  7575. + au_set_h_dptr(dentry, dinfo->di_btop, NULL);
  7576. + au_di_cp(dinfo, tmp);
  7577. + hd = au_hdentry(tmp, tmp->di_btop);
  7578. + au_set_h_dptr(dentry, tmp->di_btop,
  7579. + dget(hd->hd_dentry));
  7580. + }
  7581. + au_dbg_verify_dinode(dentry);
  7582. + } else {
  7583. + AuDbg("negative --> positive\n");
  7584. + /*
  7585. + * similar to the behaviour of creating with bypassing
  7586. + * aufs.
  7587. + * unhash it in order to force an error in the
  7588. + * succeeding create operation.
  7589. + * we should not set S_DEAD here.
  7590. + */
  7591. + d_drop(dentry);
  7592. + /* au_di_swap(tmp, dinfo); */
  7593. + au_dbg_verify_dinode(dentry);
  7594. + }
  7595. + } else {
  7596. + AuDbg("positive originally\n");
  7597. + /* inode may be NULL */
  7598. + AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
  7599. + if (!tmp_h.inode) {
  7600. + AuDbg("positive --> negative\n");
  7601. + /* or bypassing aufs */
  7602. + au_hide(dentry);
  7603. + if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
  7604. + dinfo->di_bwh = tmp->di_bwh;
  7605. + if (inode)
  7606. + err = au_refresh_hinode_self(inode);
  7607. + au_dbg_verify_dinode(dentry);
  7608. + } else if (orig_h.mode == tmp_h.mode) {
  7609. + AuDbg("positive --> positive, same type\n");
  7610. + if (!S_ISDIR(orig_h.mode)
  7611. + && dinfo->di_btop > tmp->di_btop) {
  7612. + /*
  7613. + * similar to the behaviour of removing and
  7614. + * creating.
  7615. + */
  7616. + au_hide(dentry);
  7617. + if (inode)
  7618. + err = au_refresh_hinode_self(inode);
  7619. + au_dbg_verify_dinode(dentry);
  7620. + } else {
  7621. + /* fill empty slots */
  7622. + if (dinfo->di_btop > tmp->di_btop)
  7623. + dinfo->di_btop = tmp->di_btop;
  7624. + if (dinfo->di_bbot < tmp->di_bbot)
  7625. + dinfo->di_bbot = tmp->di_bbot;
  7626. + dinfo->di_bwh = tmp->di_bwh;
  7627. + dinfo->di_bdiropq = tmp->di_bdiropq;
  7628. + bbot = dinfo->di_bbot;
  7629. + bindex = tmp->di_btop;
  7630. + hd = au_hdentry(tmp, bindex);
  7631. + for (; bindex <= bbot; bindex++, hd++) {
  7632. + if (au_h_dptr(dentry, bindex))
  7633. + continue;
  7634. + h_dentry = hd->hd_dentry;
  7635. + if (!h_dentry)
  7636. + continue;
  7637. + AuDebugOn(d_is_negative(h_dentry));
  7638. + h_inode = d_inode(h_dentry);
  7639. + AuDebugOn(orig_h.mode
  7640. + != (h_inode->i_mode
  7641. + & S_IFMT));
  7642. + au_set_h_dptr(dentry, bindex,
  7643. + dget(h_dentry));
  7644. + }
  7645. + if (inode)
  7646. + err = au_refresh_hinode(inode, dentry);
  7647. + au_dbg_verify_dinode(dentry);
  7648. + }
  7649. + } else {
  7650. + AuDbg("positive --> positive, different type\n");
  7651. + /* similar to the behaviour of removing and creating */
  7652. + au_hide(dentry);
  7653. + if (inode)
  7654. + err = au_refresh_hinode_self(inode);
  7655. + au_dbg_verify_dinode(dentry);
  7656. + }
  7657. + }
  7658. +
  7659. +out:
  7660. + return err;
  7661. +}
  7662. +
  7663. +void au_refresh_dop(struct dentry *dentry, int force_reval)
  7664. +{
  7665. + const struct dentry_operations *dop
  7666. + = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
  7667. + static const unsigned int mask
  7668. + = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
  7669. +
  7670. + BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
  7671. +
  7672. + if (dentry->d_op == dop)
  7673. + return;
  7674. +
  7675. + AuDbg("%pd\n", dentry);
  7676. + spin_lock(&dentry->d_lock);
  7677. + if (dop == &aufs_dop)
  7678. + dentry->d_flags |= mask;
  7679. + else
  7680. + dentry->d_flags &= ~mask;
  7681. + dentry->d_op = dop;
  7682. + spin_unlock(&dentry->d_lock);
  7683. +}
  7684. +
  7685. +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
  7686. +{
  7687. + int err, ebrange, nbr;
  7688. + unsigned int sigen;
  7689. + struct au_dinfo *dinfo, *tmp;
  7690. + struct super_block *sb;
  7691. + struct inode *inode;
  7692. +
  7693. + DiMustWriteLock(dentry);
  7694. + AuDebugOn(IS_ROOT(dentry));
  7695. + AuDebugOn(d_really_is_negative(parent));
  7696. +
  7697. + sb = dentry->d_sb;
  7698. + sigen = au_sigen(sb);
  7699. + err = au_digen_test(parent, sigen);
  7700. + if (unlikely(err))
  7701. + goto out;
  7702. +
  7703. + nbr = au_sbbot(sb) + 1;
  7704. + dinfo = au_di(dentry);
  7705. + err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
  7706. + if (unlikely(err))
  7707. + goto out;
  7708. + ebrange = au_dbrange_test(dentry);
  7709. + if (!ebrange)
  7710. + ebrange = au_do_refresh_hdentry(dentry, parent);
  7711. +
  7712. + if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
  7713. + AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
  7714. + if (d_really_is_positive(dentry)) {
  7715. + inode = d_inode(dentry);
  7716. + err = au_refresh_hinode_self(inode);
  7717. + }
  7718. + au_dbg_verify_dinode(dentry);
  7719. + if (!err)
  7720. + goto out_dgen; /* success */
  7721. + goto out;
  7722. + }
  7723. +
  7724. + /* temporary dinfo */
  7725. + AuDbgDentry(dentry);
  7726. + err = -ENOMEM;
  7727. + tmp = au_di_alloc(sb, AuLsc_DI_TMP);
  7728. + if (unlikely(!tmp))
  7729. + goto out;
  7730. + au_di_swap(tmp, dinfo);
  7731. + /* returns the number of positive dentries */
  7732. + /*
  7733. + * if current working dir is removed, it returns an error.
  7734. + * but the dentry is legal.
  7735. + */
  7736. + err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
  7737. + AuDbgDentry(dentry);
  7738. + au_di_swap(tmp, dinfo);
  7739. + if (err == -ENOENT)
  7740. + err = 0;
  7741. + if (err >= 0) {
  7742. + /* compare/refresh by dinfo */
  7743. + AuDbgDentry(dentry);
  7744. + err = au_refresh_by_dinfo(dentry, dinfo, tmp);
  7745. + au_dbg_verify_dinode(dentry);
  7746. + AuTraceErr(err);
  7747. + }
  7748. + au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
  7749. + au_rw_write_unlock(&tmp->di_rwsem);
  7750. + au_di_free(tmp);
  7751. + if (unlikely(err))
  7752. + goto out;
  7753. +
  7754. +out_dgen:
  7755. + au_update_digen(dentry);
  7756. +out:
  7757. + if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
  7758. + AuIOErr("failed refreshing %pd, %d\n", dentry, err);
  7759. + AuDbgDentry(dentry);
  7760. + }
  7761. + AuTraceErr(err);
  7762. + return err;
  7763. +}
  7764. +
  7765. +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
  7766. + struct dentry *dentry, aufs_bindex_t bindex)
  7767. +{
  7768. + int err, valid;
  7769. +
  7770. + err = 0;
  7771. + if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
  7772. + goto out;
  7773. +
  7774. + AuDbg("b%d\n", bindex);
  7775. + /*
  7776. + * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
  7777. + * due to whiteout and branch permission.
  7778. + */
  7779. + flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
  7780. + | LOOKUP_FOLLOW | LOOKUP_EXCL);
  7781. + /* it may return tri-state */
  7782. + valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
  7783. +
  7784. + if (unlikely(valid < 0))
  7785. + err = valid;
  7786. + else if (!valid)
  7787. + err = -EINVAL;
  7788. +
  7789. +out:
  7790. + AuTraceErr(err);
  7791. + return err;
  7792. +}
  7793. +
  7794. +/* todo: remove this */
  7795. +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
  7796. + unsigned int flags, int do_udba, int dirren)
  7797. +{
  7798. + int err;
  7799. + umode_t mode, h_mode;
  7800. + aufs_bindex_t bindex, btail, btop, ibs, ibe;
  7801. + unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
  7802. + struct inode *h_inode, *h_cached_inode;
  7803. + struct dentry *h_dentry;
  7804. + struct qstr *name, *h_name;
  7805. +
  7806. + err = 0;
  7807. + plus = 0;
  7808. + mode = 0;
  7809. + ibs = -1;
  7810. + ibe = -1;
  7811. + unhashed = !!d_unhashed(dentry);
  7812. + is_root = !!IS_ROOT(dentry);
  7813. + name = &dentry->d_name;
  7814. + tmpfile = au_di(dentry)->di_tmpfile;
  7815. +
  7816. + /*
  7817. + * Theoretically, REVAL test should be unnecessary in case of
  7818. + * {FS,I}NOTIFY.
  7819. + * But {fs,i}notify doesn't fire some necessary events,
  7820. + * IN_ATTRIB for atime/nlink/pageio
  7821. + * Let's do REVAL test too.
  7822. + */
  7823. + if (do_udba && inode) {
  7824. + mode = (inode->i_mode & S_IFMT);
  7825. + plus = (inode->i_nlink > 0);
  7826. + ibs = au_ibtop(inode);
  7827. + ibe = au_ibbot(inode);
  7828. + }
  7829. +
  7830. + btop = au_dbtop(dentry);
  7831. + btail = btop;
  7832. + if (inode && S_ISDIR(inode->i_mode))
  7833. + btail = au_dbtaildir(dentry);
  7834. + for (bindex = btop; bindex <= btail; bindex++) {
  7835. + h_dentry = au_h_dptr(dentry, bindex);
  7836. + if (!h_dentry)
  7837. + continue;
  7838. +
  7839. + AuDbg("b%d, %pd\n", bindex, h_dentry);
  7840. + h_nfs = !!au_test_nfs(h_dentry->d_sb);
  7841. + spin_lock(&h_dentry->d_lock);
  7842. + h_name = &h_dentry->d_name;
  7843. + if (unlikely(do_udba
  7844. + && !is_root
  7845. + && ((!h_nfs
  7846. + && (unhashed != !!d_unhashed(h_dentry)
  7847. + || (!tmpfile && !dirren
  7848. + && !au_qstreq(name, h_name))
  7849. + ))
  7850. + || (h_nfs
  7851. + && !(flags & LOOKUP_OPEN)
  7852. + && (h_dentry->d_flags
  7853. + & DCACHE_NFSFS_RENAMED)))
  7854. + )) {
  7855. + int h_unhashed;
  7856. +
  7857. + h_unhashed = d_unhashed(h_dentry);
  7858. + spin_unlock(&h_dentry->d_lock);
  7859. + AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
  7860. + unhashed, h_unhashed, dentry, h_dentry);
  7861. + goto err;
  7862. + }
  7863. + spin_unlock(&h_dentry->d_lock);
  7864. +
  7865. + err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
  7866. + if (unlikely(err))
  7867. + /* do not goto err, to keep the errno */
  7868. + break;
  7869. +
  7870. + /* todo: plink too? */
  7871. + if (!do_udba)
  7872. + continue;
  7873. +
  7874. + /* UDBA tests */
  7875. + if (unlikely(!!inode != d_is_positive(h_dentry)))
  7876. + goto err;
  7877. +
  7878. + h_inode = NULL;
  7879. + if (d_is_positive(h_dentry))
  7880. + h_inode = d_inode(h_dentry);
  7881. + h_plus = plus;
  7882. + h_mode = mode;
  7883. + h_cached_inode = h_inode;
  7884. + if (h_inode) {
  7885. + h_mode = (h_inode->i_mode & S_IFMT);
  7886. + h_plus = (h_inode->i_nlink > 0);
  7887. + }
  7888. + if (inode && ibs <= bindex && bindex <= ibe)
  7889. + h_cached_inode = au_h_iptr(inode, bindex);
  7890. +
  7891. + if (!h_nfs) {
  7892. + if (unlikely(plus != h_plus && !tmpfile))
  7893. + goto err;
  7894. + } else {
  7895. + if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
  7896. + && !is_root
  7897. + && !IS_ROOT(h_dentry)
  7898. + && unhashed != d_unhashed(h_dentry)))
  7899. + goto err;
  7900. + }
  7901. + if (unlikely(mode != h_mode
  7902. + || h_cached_inode != h_inode))
  7903. + goto err;
  7904. + continue;
  7905. +
  7906. +err:
  7907. + err = -EINVAL;
  7908. + break;
  7909. + }
  7910. +
  7911. + AuTraceErr(err);
  7912. + return err;
  7913. +}
  7914. +
  7915. +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
  7916. +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
  7917. +{
  7918. + int err;
  7919. + struct dentry *parent;
  7920. +
  7921. + if (!au_digen_test(dentry, sigen))
  7922. + return 0;
  7923. +
  7924. + parent = dget_parent(dentry);
  7925. + di_read_lock_parent(parent, AuLock_IR);
  7926. + AuDebugOn(au_digen_test(parent, sigen));
  7927. + au_dbg_verify_gen(parent, sigen);
  7928. + err = au_refresh_dentry(dentry, parent);
  7929. + di_read_unlock(parent, AuLock_IR);
  7930. + dput(parent);
  7931. + AuTraceErr(err);
  7932. + return err;
  7933. +}
  7934. +
  7935. +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
  7936. +{
  7937. + int err;
  7938. + struct dentry *d, *parent;
  7939. +
  7940. + if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
  7941. + return simple_reval_dpath(dentry, sigen);
  7942. +
  7943. + /* slow loop, keep it simple and stupid */
  7944. + /* cf: au_cpup_dirs() */
  7945. + err = 0;
  7946. + parent = NULL;
  7947. + while (au_digen_test(dentry, sigen)) {
  7948. + d = dentry;
  7949. + while (1) {
  7950. + dput(parent);
  7951. + parent = dget_parent(d);
  7952. + if (!au_digen_test(parent, sigen))
  7953. + break;
  7954. + d = parent;
  7955. + }
  7956. +
  7957. + if (d != dentry)
  7958. + di_write_lock_child2(d);
  7959. +
  7960. + /* someone might update our dentry while we were sleeping */
  7961. + if (au_digen_test(d, sigen)) {
  7962. + /*
  7963. + * todo: consolidate with simple_reval_dpath(),
  7964. + * do_refresh() and au_reval_for_attr().
  7965. + */
  7966. + di_read_lock_parent(parent, AuLock_IR);
  7967. + err = au_refresh_dentry(d, parent);
  7968. + di_read_unlock(parent, AuLock_IR);
  7969. + }
  7970. +
  7971. + if (d != dentry)
  7972. + di_write_unlock(d);
  7973. + dput(parent);
  7974. + if (unlikely(err))
  7975. + break;
  7976. + }
  7977. +
  7978. + return err;
  7979. +}
  7980. +
  7981. +/*
  7982. + * if valid returns 1, otherwise 0.
  7983. + */
  7984. +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
  7985. +{
  7986. + int valid, err;
  7987. + unsigned int sigen;
  7988. + unsigned char do_udba, dirren;
  7989. + struct super_block *sb;
  7990. + struct inode *inode;
  7991. +
  7992. + /* todo: support rcu-walk? */
  7993. + if (flags & LOOKUP_RCU)
  7994. + return -ECHILD;
  7995. +
  7996. + valid = 0;
  7997. + if (unlikely(!au_di(dentry)))
  7998. + goto out;
  7999. +
  8000. + valid = 1;
  8001. + sb = dentry->d_sb;
  8002. + /*
  8003. + * todo: very ugly
  8004. + * i_mutex of parent dir may be held,
  8005. + * but we should not return 'invalid' due to busy.
  8006. + */
  8007. + err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
  8008. + if (unlikely(err)) {
  8009. + valid = err;
  8010. + AuTraceErr(err);
  8011. + goto out;
  8012. + }
  8013. + inode = NULL;
  8014. + if (d_really_is_positive(dentry))
  8015. + inode = d_inode(dentry);
  8016. + if (unlikely(inode && au_is_bad_inode(inode))) {
  8017. + err = -EINVAL;
  8018. + AuTraceErr(err);
  8019. + goto out_dgrade;
  8020. + }
  8021. + if (unlikely(au_dbrange_test(dentry))) {
  8022. + err = -EINVAL;
  8023. + AuTraceErr(err);
  8024. + goto out_dgrade;
  8025. + }
  8026. +
  8027. + sigen = au_sigen(sb);
  8028. + if (au_digen_test(dentry, sigen)) {
  8029. + AuDebugOn(IS_ROOT(dentry));
  8030. + err = au_reval_dpath(dentry, sigen);
  8031. + if (unlikely(err)) {
  8032. + AuTraceErr(err);
  8033. + goto out_dgrade;
  8034. + }
  8035. + }
  8036. + di_downgrade_lock(dentry, AuLock_IR);
  8037. +
  8038. + err = -EINVAL;
  8039. + if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
  8040. + && inode
  8041. + && !(inode->i_state && I_LINKABLE)
  8042. + && (IS_DEADDIR(inode) || !inode->i_nlink)) {
  8043. + AuTraceErr(err);
  8044. + goto out_inval;
  8045. + }
  8046. +
  8047. + do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
  8048. + if (do_udba && inode) {
  8049. + aufs_bindex_t btop = au_ibtop(inode);
  8050. + struct inode *h_inode;
  8051. +
  8052. + if (btop >= 0) {
  8053. + h_inode = au_h_iptr(inode, btop);
  8054. + if (h_inode && au_test_higen(inode, h_inode)) {
  8055. + AuTraceErr(err);
  8056. + goto out_inval;
  8057. + }
  8058. + }
  8059. + }
  8060. +
  8061. + dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
  8062. + err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
  8063. + if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
  8064. + err = -EIO;
  8065. + AuDbg("both of real entry and whiteout found, %p, err %d\n",
  8066. + dentry, err);
  8067. + }
  8068. + goto out_inval;
  8069. +
  8070. +out_dgrade:
  8071. + di_downgrade_lock(dentry, AuLock_IR);
  8072. +out_inval:
  8073. + aufs_read_unlock(dentry, AuLock_IR);
  8074. + AuTraceErr(err);
  8075. + valid = !err;
  8076. +out:
  8077. + if (!valid) {
  8078. + AuDbg("%pd invalid, %d\n", dentry, valid);
  8079. + d_drop(dentry);
  8080. + }
  8081. + return valid;
  8082. +}
  8083. +
  8084. +static void aufs_d_release(struct dentry *dentry)
  8085. +{
  8086. + if (au_di(dentry)) {
  8087. + au_di_fin(dentry);
  8088. + au_hn_di_reinit(dentry);
  8089. + }
  8090. +}
  8091. +
  8092. +const struct dentry_operations aufs_dop = {
  8093. + .d_revalidate = aufs_d_revalidate,
  8094. + .d_weak_revalidate = aufs_d_revalidate,
  8095. + .d_release = aufs_d_release
  8096. +};
  8097. +
  8098. +/* aufs_dop without d_revalidate */
  8099. +const struct dentry_operations aufs_dop_noreval = {
  8100. + .d_release = aufs_d_release
  8101. +};
  8102. --- /dev/null
  8103. +++ linux-4.19/fs/aufs/dentry.h 2018-11-20 09:35:15.015195503 +0200
  8104. @@ -0,0 +1,267 @@
  8105. +/* SPDX-License-Identifier: GPL-2.0 */
  8106. +/*
  8107. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  8108. + *
  8109. + * This program, aufs is free software; you can redistribute it and/or modify
  8110. + * it under the terms of the GNU General Public License as published by
  8111. + * the Free Software Foundation; either version 2 of the License, or
  8112. + * (at your option) any later version.
  8113. + *
  8114. + * This program is distributed in the hope that it will be useful,
  8115. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8116. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8117. + * GNU General Public License for more details.
  8118. + *
  8119. + * You should have received a copy of the GNU General Public License
  8120. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  8121. + */
  8122. +
  8123. +/*
  8124. + * lookup and dentry operations
  8125. + */
  8126. +
  8127. +#ifndef __AUFS_DENTRY_H__
  8128. +#define __AUFS_DENTRY_H__
  8129. +
  8130. +#ifdef __KERNEL__
  8131. +
  8132. +#include <linux/dcache.h>
  8133. +#include "dirren.h"
  8134. +#include "rwsem.h"
  8135. +
  8136. +struct au_hdentry {
  8137. + struct dentry *hd_dentry;
  8138. + aufs_bindex_t hd_id;
  8139. +};
  8140. +
  8141. +struct au_dinfo {
  8142. + atomic_t di_generation;
  8143. +
  8144. + struct au_rwsem di_rwsem;
  8145. + aufs_bindex_t di_btop, di_bbot, di_bwh, di_bdiropq;
  8146. + unsigned char di_tmpfile; /* to allow the different name */
  8147. + struct au_hdentry *di_hdentry;
  8148. +} ____cacheline_aligned_in_smp;
  8149. +
  8150. +/* ---------------------------------------------------------------------- */
  8151. +
  8152. +/* flags for au_lkup_dentry() */
  8153. +#define AuLkup_ALLOW_NEG 1
  8154. +#define AuLkup_IGNORE_PERM (1 << 1)
  8155. +#define AuLkup_DIRREN (1 << 2)
  8156. +#define au_ftest_lkup(flags, name) ((flags) & AuLkup_##name)
  8157. +#define au_fset_lkup(flags, name) \
  8158. + do { (flags) |= AuLkup_##name; } while (0)
  8159. +#define au_fclr_lkup(flags, name) \
  8160. + do { (flags) &= ~AuLkup_##name; } while (0)
  8161. +
  8162. +#ifndef CONFIG_AUFS_DIRREN
  8163. +#undef AuLkup_DIRREN
  8164. +#define AuLkup_DIRREN 0
  8165. +#endif
  8166. +
  8167. +struct au_do_lookup_args {
  8168. + unsigned int flags;
  8169. + mode_t type;
  8170. + struct qstr whname, *name;
  8171. + struct au_dr_lookup dirren;
  8172. +};
  8173. +
  8174. +/* ---------------------------------------------------------------------- */
  8175. +
  8176. +/* dentry.c */
  8177. +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
  8178. +struct au_branch;
  8179. +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
  8180. +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
  8181. + struct dentry *h_parent, struct au_branch *br);
  8182. +
  8183. +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
  8184. + unsigned int flags);
  8185. +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
  8186. +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
  8187. +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
  8188. +void au_refresh_dop(struct dentry *dentry, int force_reval);
  8189. +
  8190. +/* dinfo.c */
  8191. +void au_di_init_once(void *_di);
  8192. +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
  8193. +void au_di_free(struct au_dinfo *dinfo);
  8194. +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
  8195. +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
  8196. +int au_di_init(struct dentry *dentry);
  8197. +void au_di_fin(struct dentry *dentry);
  8198. +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
  8199. +
  8200. +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
  8201. +void di_read_unlock(struct dentry *d, int flags);
  8202. +void di_downgrade_lock(struct dentry *d, int flags);
  8203. +void di_write_lock(struct dentry *d, unsigned int lsc);
  8204. +void di_write_unlock(struct dentry *d);
  8205. +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
  8206. +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
  8207. +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
  8208. +
  8209. +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
  8210. +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
  8211. +aufs_bindex_t au_dbtail(struct dentry *dentry);
  8212. +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
  8213. +
  8214. +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
  8215. + struct dentry *h_dentry);
  8216. +int au_digen_test(struct dentry *dentry, unsigned int sigen);
  8217. +int au_dbrange_test(struct dentry *dentry);
  8218. +void au_update_digen(struct dentry *dentry);
  8219. +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
  8220. +void au_update_dbtop(struct dentry *dentry);
  8221. +void au_update_dbbot(struct dentry *dentry);
  8222. +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
  8223. +
  8224. +/* ---------------------------------------------------------------------- */
  8225. +
  8226. +static inline struct au_dinfo *au_di(struct dentry *dentry)
  8227. +{
  8228. + return dentry->d_fsdata;
  8229. +}
  8230. +
  8231. +/* ---------------------------------------------------------------------- */
  8232. +
  8233. +/* lock subclass for dinfo */
  8234. +enum {
  8235. + AuLsc_DI_CHILD, /* child first */
  8236. + AuLsc_DI_CHILD2, /* rename(2), link(2), and cpup at hnotify */
  8237. + AuLsc_DI_CHILD3, /* copyup dirs */
  8238. + AuLsc_DI_PARENT,
  8239. + AuLsc_DI_PARENT2,
  8240. + AuLsc_DI_PARENT3,
  8241. + AuLsc_DI_TMP /* temp for replacing dinfo */
  8242. +};
  8243. +
  8244. +/*
  8245. + * di_read_lock_child, di_write_lock_child,
  8246. + * di_read_lock_child2, di_write_lock_child2,
  8247. + * di_read_lock_child3, di_write_lock_child3,
  8248. + * di_read_lock_parent, di_write_lock_parent,
  8249. + * di_read_lock_parent2, di_write_lock_parent2,
  8250. + * di_read_lock_parent3, di_write_lock_parent3,
  8251. + */
  8252. +#define AuReadLockFunc(name, lsc) \
  8253. +static inline void di_read_lock_##name(struct dentry *d, int flags) \
  8254. +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
  8255. +
  8256. +#define AuWriteLockFunc(name, lsc) \
  8257. +static inline void di_write_lock_##name(struct dentry *d) \
  8258. +{ di_write_lock(d, AuLsc_DI_##lsc); }
  8259. +
  8260. +#define AuRWLockFuncs(name, lsc) \
  8261. + AuReadLockFunc(name, lsc) \
  8262. + AuWriteLockFunc(name, lsc)
  8263. +
  8264. +AuRWLockFuncs(child, CHILD);
  8265. +AuRWLockFuncs(child2, CHILD2);
  8266. +AuRWLockFuncs(child3, CHILD3);
  8267. +AuRWLockFuncs(parent, PARENT);
  8268. +AuRWLockFuncs(parent2, PARENT2);
  8269. +AuRWLockFuncs(parent3, PARENT3);
  8270. +
  8271. +#undef AuReadLockFunc
  8272. +#undef AuWriteLockFunc
  8273. +#undef AuRWLockFuncs
  8274. +
  8275. +#define DiMustNoWaiters(d) AuRwMustNoWaiters(&au_di(d)->di_rwsem)
  8276. +#define DiMustAnyLock(d) AuRwMustAnyLock(&au_di(d)->di_rwsem)
  8277. +#define DiMustWriteLock(d) AuRwMustWriteLock(&au_di(d)->di_rwsem)
  8278. +
  8279. +/* ---------------------------------------------------------------------- */
  8280. +
  8281. +/* todo: memory barrier? */
  8282. +static inline unsigned int au_digen(struct dentry *d)
  8283. +{
  8284. + return atomic_read(&au_di(d)->di_generation);
  8285. +}
  8286. +
  8287. +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
  8288. +{
  8289. + hdentry->hd_dentry = NULL;
  8290. +}
  8291. +
  8292. +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
  8293. + aufs_bindex_t bindex)
  8294. +{
  8295. + return di->di_hdentry + bindex;
  8296. +}
  8297. +
  8298. +static inline void au_hdput(struct au_hdentry *hd)
  8299. +{
  8300. + if (hd)
  8301. + dput(hd->hd_dentry);
  8302. +}
  8303. +
  8304. +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
  8305. +{
  8306. + DiMustAnyLock(dentry);
  8307. + return au_di(dentry)->di_btop;
  8308. +}
  8309. +
  8310. +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
  8311. +{
  8312. + DiMustAnyLock(dentry);
  8313. + return au_di(dentry)->di_bbot;
  8314. +}
  8315. +
  8316. +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
  8317. +{
  8318. + DiMustAnyLock(dentry);
  8319. + return au_di(dentry)->di_bwh;
  8320. +}
  8321. +
  8322. +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
  8323. +{
  8324. + DiMustAnyLock(dentry);
  8325. + return au_di(dentry)->di_bdiropq;
  8326. +}
  8327. +
  8328. +/* todo: hard/soft set? */
  8329. +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
  8330. +{
  8331. + DiMustWriteLock(dentry);
  8332. + au_di(dentry)->di_btop = bindex;
  8333. +}
  8334. +
  8335. +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
  8336. +{
  8337. + DiMustWriteLock(dentry);
  8338. + au_di(dentry)->di_bbot = bindex;
  8339. +}
  8340. +
  8341. +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
  8342. +{
  8343. + DiMustWriteLock(dentry);
  8344. + /* dbwh can be outside of btop - bbot range */
  8345. + au_di(dentry)->di_bwh = bindex;
  8346. +}
  8347. +
  8348. +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
  8349. +{
  8350. + DiMustWriteLock(dentry);
  8351. + au_di(dentry)->di_bdiropq = bindex;
  8352. +}
  8353. +
  8354. +/* ---------------------------------------------------------------------- */
  8355. +
  8356. +#ifdef CONFIG_AUFS_HNOTIFY
  8357. +static inline void au_digen_dec(struct dentry *d)
  8358. +{
  8359. + atomic_dec(&au_di(d)->di_generation);
  8360. +}
  8361. +
  8362. +static inline void au_hn_di_reinit(struct dentry *dentry)
  8363. +{
  8364. + dentry->d_fsdata = NULL;
  8365. +}
  8366. +#else
  8367. +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
  8368. +#endif /* CONFIG_AUFS_HNOTIFY */
  8369. +
  8370. +#endif /* __KERNEL__ */
  8371. +#endif /* __AUFS_DENTRY_H__ */
  8372. --- /dev/null
  8373. +++ linux-4.19/fs/aufs/dinfo.c 2018-11-20 09:35:15.016195503 +0200
  8374. @@ -0,0 +1,554 @@
  8375. +// SPDX-License-Identifier: GPL-2.0
  8376. +/*
  8377. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  8378. + *
  8379. + * This program, aufs is free software; you can redistribute it and/or modify
  8380. + * it under the terms of the GNU General Public License as published by
  8381. + * the Free Software Foundation; either version 2 of the License, or
  8382. + * (at your option) any later version.
  8383. + *
  8384. + * This program is distributed in the hope that it will be useful,
  8385. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8386. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8387. + * GNU General Public License for more details.
  8388. + *
  8389. + * You should have received a copy of the GNU General Public License
  8390. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  8391. + */
  8392. +
  8393. +/*
  8394. + * dentry private data
  8395. + */
  8396. +
  8397. +#include "aufs.h"
  8398. +
  8399. +void au_di_init_once(void *_dinfo)
  8400. +{
  8401. + struct au_dinfo *dinfo = _dinfo;
  8402. +
  8403. + au_rw_init(&dinfo->di_rwsem);
  8404. +}
  8405. +
  8406. +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
  8407. +{
  8408. + struct au_dinfo *dinfo;
  8409. + int nbr, i;
  8410. +
  8411. + dinfo = au_cache_alloc_dinfo();
  8412. + if (unlikely(!dinfo))
  8413. + goto out;
  8414. +
  8415. + nbr = au_sbbot(sb) + 1;
  8416. + if (nbr <= 0)
  8417. + nbr = 1;
  8418. + dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
  8419. + if (dinfo->di_hdentry) {
  8420. + au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
  8421. + dinfo->di_btop = -1;
  8422. + dinfo->di_bbot = -1;
  8423. + dinfo->di_bwh = -1;
  8424. + dinfo->di_bdiropq = -1;
  8425. + dinfo->di_tmpfile = 0;
  8426. + for (i = 0; i < nbr; i++)
  8427. + dinfo->di_hdentry[i].hd_id = -1;
  8428. + goto out;
  8429. + }
  8430. +
  8431. + au_cache_free_dinfo(dinfo);
  8432. + dinfo = NULL;
  8433. +
  8434. +out:
  8435. + return dinfo;
  8436. +}
  8437. +
  8438. +void au_di_free(struct au_dinfo *dinfo)
  8439. +{
  8440. + struct au_hdentry *p;
  8441. + aufs_bindex_t bbot, bindex;
  8442. +
  8443. + /* dentry may not be revalidated */
  8444. + bindex = dinfo->di_btop;
  8445. + if (bindex >= 0) {
  8446. + bbot = dinfo->di_bbot;
  8447. + p = au_hdentry(dinfo, bindex);
  8448. + while (bindex++ <= bbot)
  8449. + au_hdput(p++);
  8450. + }
  8451. + kfree(dinfo->di_hdentry);
  8452. + au_cache_free_dinfo(dinfo);
  8453. +}
  8454. +
  8455. +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
  8456. +{
  8457. + struct au_hdentry *p;
  8458. + aufs_bindex_t bi;
  8459. +
  8460. + AuRwMustWriteLock(&a->di_rwsem);
  8461. + AuRwMustWriteLock(&b->di_rwsem);
  8462. +
  8463. +#define DiSwap(v, name) \
  8464. + do { \
  8465. + v = a->di_##name; \
  8466. + a->di_##name = b->di_##name; \
  8467. + b->di_##name = v; \
  8468. + } while (0)
  8469. +
  8470. + DiSwap(p, hdentry);
  8471. + DiSwap(bi, btop);
  8472. + DiSwap(bi, bbot);
  8473. + DiSwap(bi, bwh);
  8474. + DiSwap(bi, bdiropq);
  8475. + /* smp_mb(); */
  8476. +
  8477. +#undef DiSwap
  8478. +}
  8479. +
  8480. +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
  8481. +{
  8482. + AuRwMustWriteLock(&dst->di_rwsem);
  8483. + AuRwMustWriteLock(&src->di_rwsem);
  8484. +
  8485. + dst->di_btop = src->di_btop;
  8486. + dst->di_bbot = src->di_bbot;
  8487. + dst->di_bwh = src->di_bwh;
  8488. + dst->di_bdiropq = src->di_bdiropq;
  8489. + /* smp_mb(); */
  8490. +}
  8491. +
  8492. +int au_di_init(struct dentry *dentry)
  8493. +{
  8494. + int err;
  8495. + struct super_block *sb;
  8496. + struct au_dinfo *dinfo;
  8497. +
  8498. + err = 0;
  8499. + sb = dentry->d_sb;
  8500. + dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
  8501. + if (dinfo) {
  8502. + atomic_set(&dinfo->di_generation, au_sigen(sb));
  8503. + /* smp_mb(); */ /* atomic_set */
  8504. + dentry->d_fsdata = dinfo;
  8505. + } else
  8506. + err = -ENOMEM;
  8507. +
  8508. + return err;
  8509. +}
  8510. +
  8511. +void au_di_fin(struct dentry *dentry)
  8512. +{
  8513. + struct au_dinfo *dinfo;
  8514. +
  8515. + dinfo = au_di(dentry);
  8516. + AuRwDestroy(&dinfo->di_rwsem);
  8517. + au_di_free(dinfo);
  8518. +}
  8519. +
  8520. +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
  8521. +{
  8522. + int err, sz;
  8523. + struct au_hdentry *hdp;
  8524. +
  8525. + AuRwMustWriteLock(&dinfo->di_rwsem);
  8526. +
  8527. + err = -ENOMEM;
  8528. + sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
  8529. + if (!sz)
  8530. + sz = sizeof(*hdp);
  8531. + hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
  8532. + may_shrink);
  8533. + if (hdp) {
  8534. + dinfo->di_hdentry = hdp;
  8535. + err = 0;
  8536. + }
  8537. +
  8538. + return err;
  8539. +}
  8540. +
  8541. +/* ---------------------------------------------------------------------- */
  8542. +
  8543. +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
  8544. +{
  8545. + switch (lsc) {
  8546. + case AuLsc_DI_CHILD:
  8547. + ii_write_lock_child(inode);
  8548. + break;
  8549. + case AuLsc_DI_CHILD2:
  8550. + ii_write_lock_child2(inode);
  8551. + break;
  8552. + case AuLsc_DI_CHILD3:
  8553. + ii_write_lock_child3(inode);
  8554. + break;
  8555. + case AuLsc_DI_PARENT:
  8556. + ii_write_lock_parent(inode);
  8557. + break;
  8558. + case AuLsc_DI_PARENT2:
  8559. + ii_write_lock_parent2(inode);
  8560. + break;
  8561. + case AuLsc_DI_PARENT3:
  8562. + ii_write_lock_parent3(inode);
  8563. + break;
  8564. + default:
  8565. + BUG();
  8566. + }
  8567. +}
  8568. +
  8569. +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
  8570. +{
  8571. + switch (lsc) {
  8572. + case AuLsc_DI_CHILD:
  8573. + ii_read_lock_child(inode);
  8574. + break;
  8575. + case AuLsc_DI_CHILD2:
  8576. + ii_read_lock_child2(inode);
  8577. + break;
  8578. + case AuLsc_DI_CHILD3:
  8579. + ii_read_lock_child3(inode);
  8580. + break;
  8581. + case AuLsc_DI_PARENT:
  8582. + ii_read_lock_parent(inode);
  8583. + break;
  8584. + case AuLsc_DI_PARENT2:
  8585. + ii_read_lock_parent2(inode);
  8586. + break;
  8587. + case AuLsc_DI_PARENT3:
  8588. + ii_read_lock_parent3(inode);
  8589. + break;
  8590. + default:
  8591. + BUG();
  8592. + }
  8593. +}
  8594. +
  8595. +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
  8596. +{
  8597. + struct inode *inode;
  8598. +
  8599. + au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
  8600. + if (d_really_is_positive(d)) {
  8601. + inode = d_inode(d);
  8602. + if (au_ftest_lock(flags, IW))
  8603. + do_ii_write_lock(inode, lsc);
  8604. + else if (au_ftest_lock(flags, IR))
  8605. + do_ii_read_lock(inode, lsc);
  8606. + }
  8607. +}
  8608. +
  8609. +void di_read_unlock(struct dentry *d, int flags)
  8610. +{
  8611. + struct inode *inode;
  8612. +
  8613. + if (d_really_is_positive(d)) {
  8614. + inode = d_inode(d);
  8615. + if (au_ftest_lock(flags, IW)) {
  8616. + au_dbg_verify_dinode(d);
  8617. + ii_write_unlock(inode);
  8618. + } else if (au_ftest_lock(flags, IR)) {
  8619. + au_dbg_verify_dinode(d);
  8620. + ii_read_unlock(inode);
  8621. + }
  8622. + }
  8623. + au_rw_read_unlock(&au_di(d)->di_rwsem);
  8624. +}
  8625. +
  8626. +void di_downgrade_lock(struct dentry *d, int flags)
  8627. +{
  8628. + if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
  8629. + ii_downgrade_lock(d_inode(d));
  8630. + au_rw_dgrade_lock(&au_di(d)->di_rwsem);
  8631. +}
  8632. +
  8633. +void di_write_lock(struct dentry *d, unsigned int lsc)
  8634. +{
  8635. + au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
  8636. + if (d_really_is_positive(d))
  8637. + do_ii_write_lock(d_inode(d), lsc);
  8638. +}
  8639. +
  8640. +void di_write_unlock(struct dentry *d)
  8641. +{
  8642. + au_dbg_verify_dinode(d);
  8643. + if (d_really_is_positive(d))
  8644. + ii_write_unlock(d_inode(d));
  8645. + au_rw_write_unlock(&au_di(d)->di_rwsem);
  8646. +}
  8647. +
  8648. +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
  8649. +{
  8650. + AuDebugOn(d1 == d2
  8651. + || d_inode(d1) == d_inode(d2)
  8652. + || d1->d_sb != d2->d_sb);
  8653. +
  8654. + if ((isdir && au_test_subdir(d1, d2))
  8655. + || d1 < d2) {
  8656. + di_write_lock_child(d1);
  8657. + di_write_lock_child2(d2);
  8658. + } else {
  8659. + di_write_lock_child(d2);
  8660. + di_write_lock_child2(d1);
  8661. + }
  8662. +}
  8663. +
  8664. +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
  8665. +{
  8666. + AuDebugOn(d1 == d2
  8667. + || d_inode(d1) == d_inode(d2)
  8668. + || d1->d_sb != d2->d_sb);
  8669. +
  8670. + if ((isdir && au_test_subdir(d1, d2))
  8671. + || d1 < d2) {
  8672. + di_write_lock_parent(d1);
  8673. + di_write_lock_parent2(d2);
  8674. + } else {
  8675. + di_write_lock_parent(d2);
  8676. + di_write_lock_parent2(d1);
  8677. + }
  8678. +}
  8679. +
  8680. +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
  8681. +{
  8682. + di_write_unlock(d1);
  8683. + if (d_inode(d1) == d_inode(d2))
  8684. + au_rw_write_unlock(&au_di(d2)->di_rwsem);
  8685. + else
  8686. + di_write_unlock(d2);
  8687. +}
  8688. +
  8689. +/* ---------------------------------------------------------------------- */
  8690. +
  8691. +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
  8692. +{
  8693. + struct dentry *d;
  8694. +
  8695. + DiMustAnyLock(dentry);
  8696. +
  8697. + if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
  8698. + return NULL;
  8699. + AuDebugOn(bindex < 0);
  8700. + d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
  8701. + AuDebugOn(d && au_dcount(d) <= 0);
  8702. + return d;
  8703. +}
  8704. +
  8705. +/*
  8706. + * extended version of au_h_dptr().
  8707. + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
  8708. + * error.
  8709. + */
  8710. +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
  8711. +{
  8712. + struct dentry *h_dentry;
  8713. + struct inode *inode, *h_inode;
  8714. +
  8715. + AuDebugOn(d_really_is_negative(dentry));
  8716. +
  8717. + h_dentry = NULL;
  8718. + if (au_dbtop(dentry) <= bindex
  8719. + && bindex <= au_dbbot(dentry))
  8720. + h_dentry = au_h_dptr(dentry, bindex);
  8721. + if (h_dentry && !au_d_linkable(h_dentry)) {
  8722. + dget(h_dentry);
  8723. + goto out; /* success */
  8724. + }
  8725. +
  8726. + inode = d_inode(dentry);
  8727. + AuDebugOn(bindex < au_ibtop(inode));
  8728. + AuDebugOn(au_ibbot(inode) < bindex);
  8729. + h_inode = au_h_iptr(inode, bindex);
  8730. + h_dentry = d_find_alias(h_inode);
  8731. + if (h_dentry) {
  8732. + if (!IS_ERR(h_dentry)) {
  8733. + if (!au_d_linkable(h_dentry))
  8734. + goto out; /* success */
  8735. + dput(h_dentry);
  8736. + } else
  8737. + goto out;
  8738. + }
  8739. +
  8740. + if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
  8741. + h_dentry = au_plink_lkup(inode, bindex);
  8742. + AuDebugOn(!h_dentry);
  8743. + if (!IS_ERR(h_dentry)) {
  8744. + if (!au_d_hashed_positive(h_dentry))
  8745. + goto out; /* success */
  8746. + dput(h_dentry);
  8747. + h_dentry = NULL;
  8748. + }
  8749. + }
  8750. +
  8751. +out:
  8752. + AuDbgDentry(h_dentry);
  8753. + return h_dentry;
  8754. +}
  8755. +
  8756. +aufs_bindex_t au_dbtail(struct dentry *dentry)
  8757. +{
  8758. + aufs_bindex_t bbot, bwh;
  8759. +
  8760. + bbot = au_dbbot(dentry);
  8761. + if (0 <= bbot) {
  8762. + bwh = au_dbwh(dentry);
  8763. + if (!bwh)
  8764. + return bwh;
  8765. + if (0 < bwh && bwh < bbot)
  8766. + return bwh - 1;
  8767. + }
  8768. + return bbot;
  8769. +}
  8770. +
  8771. +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
  8772. +{
  8773. + aufs_bindex_t bbot, bopq;
  8774. +
  8775. + bbot = au_dbtail(dentry);
  8776. + if (0 <= bbot) {
  8777. + bopq = au_dbdiropq(dentry);
  8778. + if (0 <= bopq && bopq < bbot)
  8779. + bbot = bopq;
  8780. + }
  8781. + return bbot;
  8782. +}
  8783. +
  8784. +/* ---------------------------------------------------------------------- */
  8785. +
  8786. +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
  8787. + struct dentry *h_dentry)
  8788. +{
  8789. + struct au_dinfo *dinfo;
  8790. + struct au_hdentry *hd;
  8791. + struct au_branch *br;
  8792. +
  8793. + DiMustWriteLock(dentry);
  8794. +
  8795. + dinfo = au_di(dentry);
  8796. + hd = au_hdentry(dinfo, bindex);
  8797. + au_hdput(hd);
  8798. + hd->hd_dentry = h_dentry;
  8799. + if (h_dentry) {
  8800. + br = au_sbr(dentry->d_sb, bindex);
  8801. + hd->hd_id = br->br_id;
  8802. + }
  8803. +}
  8804. +
  8805. +int au_dbrange_test(struct dentry *dentry)
  8806. +{
  8807. + int err;
  8808. + aufs_bindex_t btop, bbot;
  8809. +
  8810. + err = 0;
  8811. + btop = au_dbtop(dentry);
  8812. + bbot = au_dbbot(dentry);
  8813. + if (btop >= 0)
  8814. + AuDebugOn(bbot < 0 && btop > bbot);
  8815. + else {
  8816. + err = -EIO;
  8817. + AuDebugOn(bbot >= 0);
  8818. + }
  8819. +
  8820. + return err;
  8821. +}
  8822. +
  8823. +int au_digen_test(struct dentry *dentry, unsigned int sigen)
  8824. +{
  8825. + int err;
  8826. +
  8827. + err = 0;
  8828. + if (unlikely(au_digen(dentry) != sigen
  8829. + || au_iigen_test(d_inode(dentry), sigen)))
  8830. + err = -EIO;
  8831. +
  8832. + return err;
  8833. +}
  8834. +
  8835. +void au_update_digen(struct dentry *dentry)
  8836. +{
  8837. + atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
  8838. + /* smp_mb(); */ /* atomic_set */
  8839. +}
  8840. +
  8841. +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
  8842. +{
  8843. + struct au_dinfo *dinfo;
  8844. + struct dentry *h_d;
  8845. + struct au_hdentry *hdp;
  8846. + aufs_bindex_t bindex, bbot;
  8847. +
  8848. + DiMustWriteLock(dentry);
  8849. +
  8850. + dinfo = au_di(dentry);
  8851. + if (!dinfo || dinfo->di_btop < 0)
  8852. + return;
  8853. +
  8854. + if (do_put_zero) {
  8855. + bbot = dinfo->di_bbot;
  8856. + bindex = dinfo->di_btop;
  8857. + hdp = au_hdentry(dinfo, bindex);
  8858. + for (; bindex <= bbot; bindex++, hdp++) {
  8859. + h_d = hdp->hd_dentry;
  8860. + if (h_d && d_is_negative(h_d))
  8861. + au_set_h_dptr(dentry, bindex, NULL);
  8862. + }
  8863. + }
  8864. +
  8865. + dinfo->di_btop = 0;
  8866. + hdp = au_hdentry(dinfo, dinfo->di_btop);
  8867. + for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
  8868. + if (hdp->hd_dentry)
  8869. + break;
  8870. + if (dinfo->di_btop > dinfo->di_bbot) {
  8871. + dinfo->di_btop = -1;
  8872. + dinfo->di_bbot = -1;
  8873. + return;
  8874. + }
  8875. +
  8876. + hdp = au_hdentry(dinfo, dinfo->di_bbot);
  8877. + for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
  8878. + if (hdp->hd_dentry)
  8879. + break;
  8880. + AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
  8881. +}
  8882. +
  8883. +void au_update_dbtop(struct dentry *dentry)
  8884. +{
  8885. + aufs_bindex_t bindex, bbot;
  8886. + struct dentry *h_dentry;
  8887. +
  8888. + bbot = au_dbbot(dentry);
  8889. + for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
  8890. + h_dentry = au_h_dptr(dentry, bindex);
  8891. + if (!h_dentry)
  8892. + continue;
  8893. + if (d_is_positive(h_dentry)) {
  8894. + au_set_dbtop(dentry, bindex);
  8895. + return;
  8896. + }
  8897. + au_set_h_dptr(dentry, bindex, NULL);
  8898. + }
  8899. +}
  8900. +
  8901. +void au_update_dbbot(struct dentry *dentry)
  8902. +{
  8903. + aufs_bindex_t bindex, btop;
  8904. + struct dentry *h_dentry;
  8905. +
  8906. + btop = au_dbtop(dentry);
  8907. + for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
  8908. + h_dentry = au_h_dptr(dentry, bindex);
  8909. + if (!h_dentry)
  8910. + continue;
  8911. + if (d_is_positive(h_dentry)) {
  8912. + au_set_dbbot(dentry, bindex);
  8913. + return;
  8914. + }
  8915. + au_set_h_dptr(dentry, bindex, NULL);
  8916. + }
  8917. +}
  8918. +
  8919. +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
  8920. +{
  8921. + aufs_bindex_t bindex, bbot;
  8922. +
  8923. + bbot = au_dbbot(dentry);
  8924. + for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
  8925. + if (au_h_dptr(dentry, bindex) == h_dentry)
  8926. + return bindex;
  8927. + return -1;
  8928. +}
  8929. --- /dev/null
  8930. +++ linux-4.19/fs/aufs/dir.c 2018-11-20 09:35:15.016195503 +0200
  8931. @@ -0,0 +1,762 @@
  8932. +// SPDX-License-Identifier: GPL-2.0
  8933. +/*
  8934. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  8935. + *
  8936. + * This program, aufs is free software; you can redistribute it and/or modify
  8937. + * it under the terms of the GNU General Public License as published by
  8938. + * the Free Software Foundation; either version 2 of the License, or
  8939. + * (at your option) any later version.
  8940. + *
  8941. + * This program is distributed in the hope that it will be useful,
  8942. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8943. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8944. + * GNU General Public License for more details.
  8945. + *
  8946. + * You should have received a copy of the GNU General Public License
  8947. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  8948. + */
  8949. +
  8950. +/*
  8951. + * directory operations
  8952. + */
  8953. +
  8954. +#include <linux/fs_stack.h>
  8955. +#include "aufs.h"
  8956. +
  8957. +void au_add_nlink(struct inode *dir, struct inode *h_dir)
  8958. +{
  8959. + unsigned int nlink;
  8960. +
  8961. + AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
  8962. +
  8963. + nlink = dir->i_nlink;
  8964. + nlink += h_dir->i_nlink - 2;
  8965. + if (h_dir->i_nlink < 2)
  8966. + nlink += 2;
  8967. + smp_mb(); /* for i_nlink */
  8968. + /* 0 can happen in revaliding */
  8969. + set_nlink(dir, nlink);
  8970. +}
  8971. +
  8972. +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
  8973. +{
  8974. + unsigned int nlink;
  8975. +
  8976. + AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
  8977. +
  8978. + nlink = dir->i_nlink;
  8979. + nlink -= h_dir->i_nlink - 2;
  8980. + if (h_dir->i_nlink < 2)
  8981. + nlink -= 2;
  8982. + smp_mb(); /* for i_nlink */
  8983. + /* nlink == 0 means the branch-fs is broken */
  8984. + set_nlink(dir, nlink);
  8985. +}
  8986. +
  8987. +loff_t au_dir_size(struct file *file, struct dentry *dentry)
  8988. +{
  8989. + loff_t sz;
  8990. + aufs_bindex_t bindex, bbot;
  8991. + struct file *h_file;
  8992. + struct dentry *h_dentry;
  8993. +
  8994. + sz = 0;
  8995. + if (file) {
  8996. + AuDebugOn(!d_is_dir(file->f_path.dentry));
  8997. +
  8998. + bbot = au_fbbot_dir(file);
  8999. + for (bindex = au_fbtop(file);
  9000. + bindex <= bbot && sz < KMALLOC_MAX_SIZE;
  9001. + bindex++) {
  9002. + h_file = au_hf_dir(file, bindex);
  9003. + if (h_file && file_inode(h_file))
  9004. + sz += vfsub_f_size_read(h_file);
  9005. + }
  9006. + } else {
  9007. + AuDebugOn(!dentry);
  9008. + AuDebugOn(!d_is_dir(dentry));
  9009. +
  9010. + bbot = au_dbtaildir(dentry);
  9011. + for (bindex = au_dbtop(dentry);
  9012. + bindex <= bbot && sz < KMALLOC_MAX_SIZE;
  9013. + bindex++) {
  9014. + h_dentry = au_h_dptr(dentry, bindex);
  9015. + if (h_dentry && d_is_positive(h_dentry))
  9016. + sz += i_size_read(d_inode(h_dentry));
  9017. + }
  9018. + }
  9019. + if (sz < KMALLOC_MAX_SIZE)
  9020. + sz = roundup_pow_of_two(sz);
  9021. + if (sz > KMALLOC_MAX_SIZE)
  9022. + sz = KMALLOC_MAX_SIZE;
  9023. + else if (sz < NAME_MAX) {
  9024. + BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
  9025. + sz = AUFS_RDBLK_DEF;
  9026. + }
  9027. + return sz;
  9028. +}
  9029. +
  9030. +struct au_dir_ts_arg {
  9031. + struct dentry *dentry;
  9032. + aufs_bindex_t brid;
  9033. +};
  9034. +
  9035. +static void au_do_dir_ts(void *arg)
  9036. +{
  9037. + struct au_dir_ts_arg *a = arg;
  9038. + struct au_dtime dt;
  9039. + struct path h_path;
  9040. + struct inode *dir, *h_dir;
  9041. + struct super_block *sb;
  9042. + struct au_branch *br;
  9043. + struct au_hinode *hdir;
  9044. + int err;
  9045. + aufs_bindex_t btop, bindex;
  9046. +
  9047. + sb = a->dentry->d_sb;
  9048. + if (d_really_is_negative(a->dentry))
  9049. + goto out;
  9050. + /* no dir->i_mutex lock */
  9051. + aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
  9052. +
  9053. + dir = d_inode(a->dentry);
  9054. + btop = au_ibtop(dir);
  9055. + bindex = au_br_index(sb, a->brid);
  9056. + if (bindex < btop)
  9057. + goto out_unlock;
  9058. +
  9059. + br = au_sbr(sb, bindex);
  9060. + h_path.dentry = au_h_dptr(a->dentry, bindex);
  9061. + if (!h_path.dentry)
  9062. + goto out_unlock;
  9063. + h_path.mnt = au_br_mnt(br);
  9064. + au_dtime_store(&dt, a->dentry, &h_path);
  9065. +
  9066. + br = au_sbr(sb, btop);
  9067. + if (!au_br_writable(br->br_perm))
  9068. + goto out_unlock;
  9069. + h_path.dentry = au_h_dptr(a->dentry, btop);
  9070. + h_path.mnt = au_br_mnt(br);
  9071. + err = vfsub_mnt_want_write(h_path.mnt);
  9072. + if (err)
  9073. + goto out_unlock;
  9074. + hdir = au_hi(dir, btop);
  9075. + au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
  9076. + h_dir = au_h_iptr(dir, btop);
  9077. + if (h_dir->i_nlink
  9078. + && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
  9079. + dt.dt_h_path = h_path;
  9080. + au_dtime_revert(&dt);
  9081. + }
  9082. + au_hn_inode_unlock(hdir);
  9083. + vfsub_mnt_drop_write(h_path.mnt);
  9084. + au_cpup_attr_timesizes(dir);
  9085. +
  9086. +out_unlock:
  9087. + aufs_read_unlock(a->dentry, AuLock_DW);
  9088. +out:
  9089. + dput(a->dentry);
  9090. + au_nwt_done(&au_sbi(sb)->si_nowait);
  9091. + kfree(arg);
  9092. +}
  9093. +
  9094. +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
  9095. +{
  9096. + int perm, wkq_err;
  9097. + aufs_bindex_t btop;
  9098. + struct au_dir_ts_arg *arg;
  9099. + struct dentry *dentry;
  9100. + struct super_block *sb;
  9101. +
  9102. + IMustLock(dir);
  9103. +
  9104. + dentry = d_find_any_alias(dir);
  9105. + AuDebugOn(!dentry);
  9106. + sb = dentry->d_sb;
  9107. + btop = au_ibtop(dir);
  9108. + if (btop == bindex) {
  9109. + au_cpup_attr_timesizes(dir);
  9110. + goto out;
  9111. + }
  9112. +
  9113. + perm = au_sbr_perm(sb, btop);
  9114. + if (!au_br_writable(perm))
  9115. + goto out;
  9116. +
  9117. + arg = kmalloc(sizeof(*arg), GFP_NOFS);
  9118. + if (!arg)
  9119. + goto out;
  9120. +
  9121. + arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
  9122. + arg->brid = au_sbr_id(sb, bindex);
  9123. + wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
  9124. + if (unlikely(wkq_err)) {
  9125. + pr_err("wkq %d\n", wkq_err);
  9126. + dput(dentry);
  9127. + kfree(arg);
  9128. + }
  9129. +
  9130. +out:
  9131. + dput(dentry);
  9132. +}
  9133. +
  9134. +/* ---------------------------------------------------------------------- */
  9135. +
  9136. +static int reopen_dir(struct file *file)
  9137. +{
  9138. + int err;
  9139. + unsigned int flags;
  9140. + aufs_bindex_t bindex, btail, btop;
  9141. + struct dentry *dentry, *h_dentry;
  9142. + struct file *h_file;
  9143. +
  9144. + /* open all lower dirs */
  9145. + dentry = file->f_path.dentry;
  9146. + btop = au_dbtop(dentry);
  9147. + for (bindex = au_fbtop(file); bindex < btop; bindex++)
  9148. + au_set_h_fptr(file, bindex, NULL);
  9149. + au_set_fbtop(file, btop);
  9150. +
  9151. + btail = au_dbtaildir(dentry);
  9152. + for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
  9153. + au_set_h_fptr(file, bindex, NULL);
  9154. + au_set_fbbot_dir(file, btail);
  9155. +
  9156. + flags = vfsub_file_flags(file);
  9157. + for (bindex = btop; bindex <= btail; bindex++) {
  9158. + h_dentry = au_h_dptr(dentry, bindex);
  9159. + if (!h_dentry)
  9160. + continue;
  9161. + h_file = au_hf_dir(file, bindex);
  9162. + if (h_file)
  9163. + continue;
  9164. +
  9165. + h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
  9166. + err = PTR_ERR(h_file);
  9167. + if (IS_ERR(h_file))
  9168. + goto out; /* close all? */
  9169. + au_set_h_fptr(file, bindex, h_file);
  9170. + }
  9171. + au_update_figen(file);
  9172. + /* todo: necessary? */
  9173. + /* file->f_ra = h_file->f_ra; */
  9174. + err = 0;
  9175. +
  9176. +out:
  9177. + return err;
  9178. +}
  9179. +
  9180. +static int do_open_dir(struct file *file, int flags, struct file *h_file)
  9181. +{
  9182. + int err;
  9183. + aufs_bindex_t bindex, btail;
  9184. + struct dentry *dentry, *h_dentry;
  9185. + struct vfsmount *mnt;
  9186. +
  9187. + FiMustWriteLock(file);
  9188. + AuDebugOn(h_file);
  9189. +
  9190. + err = 0;
  9191. + mnt = file->f_path.mnt;
  9192. + dentry = file->f_path.dentry;
  9193. + file->f_version = inode_query_iversion(d_inode(dentry));
  9194. + bindex = au_dbtop(dentry);
  9195. + au_set_fbtop(file, bindex);
  9196. + btail = au_dbtaildir(dentry);
  9197. + au_set_fbbot_dir(file, btail);
  9198. + for (; !err && bindex <= btail; bindex++) {
  9199. + h_dentry = au_h_dptr(dentry, bindex);
  9200. + if (!h_dentry)
  9201. + continue;
  9202. +
  9203. + err = vfsub_test_mntns(mnt, h_dentry->d_sb);
  9204. + if (unlikely(err))
  9205. + break;
  9206. + h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
  9207. + if (IS_ERR(h_file)) {
  9208. + err = PTR_ERR(h_file);
  9209. + break;
  9210. + }
  9211. + au_set_h_fptr(file, bindex, h_file);
  9212. + }
  9213. + au_update_figen(file);
  9214. + /* todo: necessary? */
  9215. + /* file->f_ra = h_file->f_ra; */
  9216. + if (!err)
  9217. + return 0; /* success */
  9218. +
  9219. + /* close all */
  9220. + for (bindex = au_fbtop(file); bindex <= btail; bindex++)
  9221. + au_set_h_fptr(file, bindex, NULL);
  9222. + au_set_fbtop(file, -1);
  9223. + au_set_fbbot_dir(file, -1);
  9224. +
  9225. + return err;
  9226. +}
  9227. +
  9228. +static int aufs_open_dir(struct inode *inode __maybe_unused,
  9229. + struct file *file)
  9230. +{
  9231. + int err;
  9232. + struct super_block *sb;
  9233. + struct au_fidir *fidir;
  9234. +
  9235. + err = -ENOMEM;
  9236. + sb = file->f_path.dentry->d_sb;
  9237. + si_read_lock(sb, AuLock_FLUSH);
  9238. + fidir = au_fidir_alloc(sb);
  9239. + if (fidir) {
  9240. + struct au_do_open_args args = {
  9241. + .open = do_open_dir,
  9242. + .fidir = fidir
  9243. + };
  9244. + err = au_do_open(file, &args);
  9245. + if (unlikely(err))
  9246. + kfree(fidir);
  9247. + }
  9248. + si_read_unlock(sb);
  9249. + return err;
  9250. +}
  9251. +
  9252. +static int aufs_release_dir(struct inode *inode __maybe_unused,
  9253. + struct file *file)
  9254. +{
  9255. + struct au_vdir *vdir_cache;
  9256. + struct au_finfo *finfo;
  9257. + struct au_fidir *fidir;
  9258. + struct au_hfile *hf;
  9259. + aufs_bindex_t bindex, bbot;
  9260. +
  9261. + finfo = au_fi(file);
  9262. + fidir = finfo->fi_hdir;
  9263. + if (fidir) {
  9264. + au_hbl_del(&finfo->fi_hlist,
  9265. + &au_sbi(file->f_path.dentry->d_sb)->si_files);
  9266. + vdir_cache = fidir->fd_vdir_cache; /* lock-free */
  9267. + if (vdir_cache)
  9268. + au_vdir_free(vdir_cache);
  9269. +
  9270. + bindex = finfo->fi_btop;
  9271. + if (bindex >= 0) {
  9272. + hf = fidir->fd_hfile + bindex;
  9273. + /*
  9274. + * calls fput() instead of filp_close(),
  9275. + * since no dnotify or lock for the lower file.
  9276. + */
  9277. + bbot = fidir->fd_bbot;
  9278. + for (; bindex <= bbot; bindex++, hf++)
  9279. + if (hf->hf_file)
  9280. + au_hfput(hf, /*execed*/0);
  9281. + }
  9282. + kfree(fidir);
  9283. + finfo->fi_hdir = NULL;
  9284. + }
  9285. + au_finfo_fin(file);
  9286. + return 0;
  9287. +}
  9288. +
  9289. +/* ---------------------------------------------------------------------- */
  9290. +
  9291. +static int au_do_flush_dir(struct file *file, fl_owner_t id)
  9292. +{
  9293. + int err;
  9294. + aufs_bindex_t bindex, bbot;
  9295. + struct file *h_file;
  9296. +
  9297. + err = 0;
  9298. + bbot = au_fbbot_dir(file);
  9299. + for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
  9300. + h_file = au_hf_dir(file, bindex);
  9301. + if (h_file)
  9302. + err = vfsub_flush(h_file, id);
  9303. + }
  9304. + return err;
  9305. +}
  9306. +
  9307. +static int aufs_flush_dir(struct file *file, fl_owner_t id)
  9308. +{
  9309. + return au_do_flush(file, id, au_do_flush_dir);
  9310. +}
  9311. +
  9312. +/* ---------------------------------------------------------------------- */
  9313. +
  9314. +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
  9315. +{
  9316. + int err;
  9317. + aufs_bindex_t bbot, bindex;
  9318. + struct inode *inode;
  9319. + struct super_block *sb;
  9320. +
  9321. + err = 0;
  9322. + sb = dentry->d_sb;
  9323. + inode = d_inode(dentry);
  9324. + IMustLock(inode);
  9325. + bbot = au_dbbot(dentry);
  9326. + for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
  9327. + struct path h_path;
  9328. +
  9329. + if (au_test_ro(sb, bindex, inode))
  9330. + continue;
  9331. + h_path.dentry = au_h_dptr(dentry, bindex);
  9332. + if (!h_path.dentry)
  9333. + continue;
  9334. +
  9335. + h_path.mnt = au_sbr_mnt(sb, bindex);
  9336. + err = vfsub_fsync(NULL, &h_path, datasync);
  9337. + }
  9338. +
  9339. + return err;
  9340. +}
  9341. +
  9342. +static int au_do_fsync_dir(struct file *file, int datasync)
  9343. +{
  9344. + int err;
  9345. + aufs_bindex_t bbot, bindex;
  9346. + struct file *h_file;
  9347. + struct super_block *sb;
  9348. + struct inode *inode;
  9349. +
  9350. + err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
  9351. + if (unlikely(err))
  9352. + goto out;
  9353. +
  9354. + inode = file_inode(file);
  9355. + sb = inode->i_sb;
  9356. + bbot = au_fbbot_dir(file);
  9357. + for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
  9358. + h_file = au_hf_dir(file, bindex);
  9359. + if (!h_file || au_test_ro(sb, bindex, inode))
  9360. + continue;
  9361. +
  9362. + err = vfsub_fsync(h_file, &h_file->f_path, datasync);
  9363. + }
  9364. +
  9365. +out:
  9366. + return err;
  9367. +}
  9368. +
  9369. +/*
  9370. + * @file may be NULL
  9371. + */
  9372. +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
  9373. + int datasync)
  9374. +{
  9375. + int err;
  9376. + struct dentry *dentry;
  9377. + struct inode *inode;
  9378. + struct super_block *sb;
  9379. +
  9380. + err = 0;
  9381. + dentry = file->f_path.dentry;
  9382. + inode = d_inode(dentry);
  9383. + inode_lock(inode);
  9384. + sb = dentry->d_sb;
  9385. + si_noflush_read_lock(sb);
  9386. + if (file)
  9387. + err = au_do_fsync_dir(file, datasync);
  9388. + else {
  9389. + di_write_lock_child(dentry);
  9390. + err = au_do_fsync_dir_no_file(dentry, datasync);
  9391. + }
  9392. + au_cpup_attr_timesizes(inode);
  9393. + di_write_unlock(dentry);
  9394. + if (file)
  9395. + fi_write_unlock(file);
  9396. +
  9397. + si_read_unlock(sb);
  9398. + inode_unlock(inode);
  9399. + return err;
  9400. +}
  9401. +
  9402. +/* ---------------------------------------------------------------------- */
  9403. +
  9404. +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
  9405. +{
  9406. + int err;
  9407. + struct dentry *dentry;
  9408. + struct inode *inode, *h_inode;
  9409. + struct super_block *sb;
  9410. +
  9411. + AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
  9412. +
  9413. + dentry = file->f_path.dentry;
  9414. + inode = d_inode(dentry);
  9415. + IMustLock(inode);
  9416. +
  9417. + sb = dentry->d_sb;
  9418. + si_read_lock(sb, AuLock_FLUSH);
  9419. + err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
  9420. + if (unlikely(err))
  9421. + goto out;
  9422. + err = au_alive_dir(dentry);
  9423. + if (!err)
  9424. + err = au_vdir_init(file);
  9425. + di_downgrade_lock(dentry, AuLock_IR);
  9426. + if (unlikely(err))
  9427. + goto out_unlock;
  9428. +
  9429. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  9430. + if (!au_test_nfsd()) {
  9431. + err = au_vdir_fill_de(file, ctx);
  9432. + fsstack_copy_attr_atime(inode, h_inode);
  9433. + } else {
  9434. + /*
  9435. + * nfsd filldir may call lookup_one_len(), vfs_getattr(),
  9436. + * encode_fh() and others.
  9437. + */
  9438. + atomic_inc(&h_inode->i_count);
  9439. + di_read_unlock(dentry, AuLock_IR);
  9440. + si_read_unlock(sb);
  9441. + err = au_vdir_fill_de(file, ctx);
  9442. + fsstack_copy_attr_atime(inode, h_inode);
  9443. + fi_write_unlock(file);
  9444. + iput(h_inode);
  9445. +
  9446. + AuTraceErr(err);
  9447. + return err;
  9448. + }
  9449. +
  9450. +out_unlock:
  9451. + di_read_unlock(dentry, AuLock_IR);
  9452. + fi_write_unlock(file);
  9453. +out:
  9454. + si_read_unlock(sb);
  9455. + return err;
  9456. +}
  9457. +
  9458. +/* ---------------------------------------------------------------------- */
  9459. +
  9460. +#define AuTestEmpty_WHONLY 1
  9461. +#define AuTestEmpty_CALLED (1 << 1)
  9462. +#define AuTestEmpty_SHWH (1 << 2)
  9463. +#define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
  9464. +#define au_fset_testempty(flags, name) \
  9465. + do { (flags) |= AuTestEmpty_##name; } while (0)
  9466. +#define au_fclr_testempty(flags, name) \
  9467. + do { (flags) &= ~AuTestEmpty_##name; } while (0)
  9468. +
  9469. +#ifndef CONFIG_AUFS_SHWH
  9470. +#undef AuTestEmpty_SHWH
  9471. +#define AuTestEmpty_SHWH 0
  9472. +#endif
  9473. +
  9474. +struct test_empty_arg {
  9475. + struct dir_context ctx;
  9476. + struct au_nhash *whlist;
  9477. + unsigned int flags;
  9478. + int err;
  9479. + aufs_bindex_t bindex;
  9480. +};
  9481. +
  9482. +static int test_empty_cb(struct dir_context *ctx, const char *__name,
  9483. + int namelen, loff_t offset __maybe_unused, u64 ino,
  9484. + unsigned int d_type)
  9485. +{
  9486. + struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
  9487. + ctx);
  9488. + char *name = (void *)__name;
  9489. +
  9490. + arg->err = 0;
  9491. + au_fset_testempty(arg->flags, CALLED);
  9492. + /* smp_mb(); */
  9493. + if (name[0] == '.'
  9494. + && (namelen == 1 || (name[1] == '.' && namelen == 2)))
  9495. + goto out; /* success */
  9496. +
  9497. + if (namelen <= AUFS_WH_PFX_LEN
  9498. + || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
  9499. + if (au_ftest_testempty(arg->flags, WHONLY)
  9500. + && !au_nhash_test_known_wh(arg->whlist, name, namelen))
  9501. + arg->err = -ENOTEMPTY;
  9502. + goto out;
  9503. + }
  9504. +
  9505. + name += AUFS_WH_PFX_LEN;
  9506. + namelen -= AUFS_WH_PFX_LEN;
  9507. + if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
  9508. + arg->err = au_nhash_append_wh
  9509. + (arg->whlist, name, namelen, ino, d_type, arg->bindex,
  9510. + au_ftest_testempty(arg->flags, SHWH));
  9511. +
  9512. +out:
  9513. + /* smp_mb(); */
  9514. + AuTraceErr(arg->err);
  9515. + return arg->err;
  9516. +}
  9517. +
  9518. +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
  9519. +{
  9520. + int err;
  9521. + struct file *h_file;
  9522. + struct au_branch *br;
  9523. +
  9524. + h_file = au_h_open(dentry, arg->bindex,
  9525. + O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
  9526. + /*file*/NULL, /*force_wr*/0);
  9527. + err = PTR_ERR(h_file);
  9528. + if (IS_ERR(h_file))
  9529. + goto out;
  9530. +
  9531. + err = 0;
  9532. + if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
  9533. + && !file_inode(h_file)->i_nlink)
  9534. + goto out_put;
  9535. +
  9536. + do {
  9537. + arg->err = 0;
  9538. + au_fclr_testempty(arg->flags, CALLED);
  9539. + /* smp_mb(); */
  9540. + err = vfsub_iterate_dir(h_file, &arg->ctx);
  9541. + if (err >= 0)
  9542. + err = arg->err;
  9543. + } while (!err && au_ftest_testempty(arg->flags, CALLED));
  9544. +
  9545. +out_put:
  9546. + fput(h_file);
  9547. + br = au_sbr(dentry->d_sb, arg->bindex);
  9548. + au_lcnt_dec(&br->br_nfiles);
  9549. +out:
  9550. + return err;
  9551. +}
  9552. +
  9553. +struct do_test_empty_args {
  9554. + int *errp;
  9555. + struct dentry *dentry;
  9556. + struct test_empty_arg *arg;
  9557. +};
  9558. +
  9559. +static void call_do_test_empty(void *args)
  9560. +{
  9561. + struct do_test_empty_args *a = args;
  9562. + *a->errp = do_test_empty(a->dentry, a->arg);
  9563. +}
  9564. +
  9565. +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
  9566. +{
  9567. + int err, wkq_err;
  9568. + struct dentry *h_dentry;
  9569. + struct inode *h_inode;
  9570. +
  9571. + h_dentry = au_h_dptr(dentry, arg->bindex);
  9572. + h_inode = d_inode(h_dentry);
  9573. + /* todo: i_mode changes anytime? */
  9574. + inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
  9575. + err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
  9576. + inode_unlock_shared(h_inode);
  9577. + if (!err)
  9578. + err = do_test_empty(dentry, arg);
  9579. + else {
  9580. + struct do_test_empty_args args = {
  9581. + .errp = &err,
  9582. + .dentry = dentry,
  9583. + .arg = arg
  9584. + };
  9585. + unsigned int flags = arg->flags;
  9586. +
  9587. + wkq_err = au_wkq_wait(call_do_test_empty, &args);
  9588. + if (unlikely(wkq_err))
  9589. + err = wkq_err;
  9590. + arg->flags = flags;
  9591. + }
  9592. +
  9593. + return err;
  9594. +}
  9595. +
  9596. +int au_test_empty_lower(struct dentry *dentry)
  9597. +{
  9598. + int err;
  9599. + unsigned int rdhash;
  9600. + aufs_bindex_t bindex, btop, btail;
  9601. + struct au_nhash whlist;
  9602. + struct test_empty_arg arg = {
  9603. + .ctx = {
  9604. + .actor = test_empty_cb
  9605. + }
  9606. + };
  9607. + int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
  9608. +
  9609. + SiMustAnyLock(dentry->d_sb);
  9610. +
  9611. + rdhash = au_sbi(dentry->d_sb)->si_rdhash;
  9612. + if (!rdhash)
  9613. + rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
  9614. + err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
  9615. + if (unlikely(err))
  9616. + goto out;
  9617. +
  9618. + arg.flags = 0;
  9619. + arg.whlist = &whlist;
  9620. + btop = au_dbtop(dentry);
  9621. + if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
  9622. + au_fset_testempty(arg.flags, SHWH);
  9623. + test_empty = do_test_empty;
  9624. + if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
  9625. + test_empty = sio_test_empty;
  9626. + arg.bindex = btop;
  9627. + err = test_empty(dentry, &arg);
  9628. + if (unlikely(err))
  9629. + goto out_whlist;
  9630. +
  9631. + au_fset_testempty(arg.flags, WHONLY);
  9632. + btail = au_dbtaildir(dentry);
  9633. + for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
  9634. + struct dentry *h_dentry;
  9635. +
  9636. + h_dentry = au_h_dptr(dentry, bindex);
  9637. + if (h_dentry && d_is_positive(h_dentry)) {
  9638. + arg.bindex = bindex;
  9639. + err = test_empty(dentry, &arg);
  9640. + }
  9641. + }
  9642. +
  9643. +out_whlist:
  9644. + au_nhash_wh_free(&whlist);
  9645. +out:
  9646. + return err;
  9647. +}
  9648. +
  9649. +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
  9650. +{
  9651. + int err;
  9652. + struct test_empty_arg arg = {
  9653. + .ctx = {
  9654. + .actor = test_empty_cb
  9655. + }
  9656. + };
  9657. + aufs_bindex_t bindex, btail;
  9658. +
  9659. + err = 0;
  9660. + arg.whlist = whlist;
  9661. + arg.flags = AuTestEmpty_WHONLY;
  9662. + if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
  9663. + au_fset_testempty(arg.flags, SHWH);
  9664. + btail = au_dbtaildir(dentry);
  9665. + for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
  9666. + struct dentry *h_dentry;
  9667. +
  9668. + h_dentry = au_h_dptr(dentry, bindex);
  9669. + if (h_dentry && d_is_positive(h_dentry)) {
  9670. + arg.bindex = bindex;
  9671. + err = sio_test_empty(dentry, &arg);
  9672. + }
  9673. + }
  9674. +
  9675. + return err;
  9676. +}
  9677. +
  9678. +/* ---------------------------------------------------------------------- */
  9679. +
  9680. +const struct file_operations aufs_dir_fop = {
  9681. + .owner = THIS_MODULE,
  9682. + .llseek = default_llseek,
  9683. + .read = generic_read_dir,
  9684. + .iterate_shared = aufs_iterate_shared,
  9685. + .unlocked_ioctl = aufs_ioctl_dir,
  9686. +#ifdef CONFIG_COMPAT
  9687. + .compat_ioctl = aufs_compat_ioctl_dir,
  9688. +#endif
  9689. + .open = aufs_open_dir,
  9690. + .release = aufs_release_dir,
  9691. + .flush = aufs_flush_dir,
  9692. + .fsync = aufs_fsync_dir
  9693. +};
  9694. --- /dev/null
  9695. +++ linux-4.19/fs/aufs/dir.h 2018-11-20 09:35:15.017195504 +0200
  9696. @@ -0,0 +1,132 @@
  9697. +/* SPDX-License-Identifier: GPL-2.0 */
  9698. +/*
  9699. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  9700. + *
  9701. + * This program, aufs is free software; you can redistribute it and/or modify
  9702. + * it under the terms of the GNU General Public License as published by
  9703. + * the Free Software Foundation; either version 2 of the License, or
  9704. + * (at your option) any later version.
  9705. + *
  9706. + * This program is distributed in the hope that it will be useful,
  9707. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9708. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9709. + * GNU General Public License for more details.
  9710. + *
  9711. + * You should have received a copy of the GNU General Public License
  9712. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  9713. + */
  9714. +
  9715. +/*
  9716. + * directory operations
  9717. + */
  9718. +
  9719. +#ifndef __AUFS_DIR_H__
  9720. +#define __AUFS_DIR_H__
  9721. +
  9722. +#ifdef __KERNEL__
  9723. +
  9724. +#include <linux/fs.h>
  9725. +
  9726. +/* ---------------------------------------------------------------------- */
  9727. +
  9728. +/* need to be faster and smaller */
  9729. +
  9730. +struct au_nhash {
  9731. + unsigned int nh_num;
  9732. + struct hlist_head *nh_head;
  9733. +};
  9734. +
  9735. +struct au_vdir_destr {
  9736. + unsigned char len;
  9737. + unsigned char name[0];
  9738. +} __packed;
  9739. +
  9740. +struct au_vdir_dehstr {
  9741. + struct hlist_node hash;
  9742. + struct au_vdir_destr *str;
  9743. +} ____cacheline_aligned_in_smp;
  9744. +
  9745. +struct au_vdir_de {
  9746. + ino_t de_ino;
  9747. + unsigned char de_type;
  9748. + /* caution: packed */
  9749. + struct au_vdir_destr de_str;
  9750. +} __packed;
  9751. +
  9752. +struct au_vdir_wh {
  9753. + struct hlist_node wh_hash;
  9754. +#ifdef CONFIG_AUFS_SHWH
  9755. + ino_t wh_ino;
  9756. + aufs_bindex_t wh_bindex;
  9757. + unsigned char wh_type;
  9758. +#else
  9759. + aufs_bindex_t wh_bindex;
  9760. +#endif
  9761. + /* caution: packed */
  9762. + struct au_vdir_destr wh_str;
  9763. +} __packed;
  9764. +
  9765. +union au_vdir_deblk_p {
  9766. + unsigned char *deblk;
  9767. + struct au_vdir_de *de;
  9768. +};
  9769. +
  9770. +struct au_vdir {
  9771. + unsigned char **vd_deblk;
  9772. + unsigned long vd_nblk;
  9773. + struct {
  9774. + unsigned long ul;
  9775. + union au_vdir_deblk_p p;
  9776. + } vd_last;
  9777. +
  9778. + u64 vd_version;
  9779. + unsigned int vd_deblk_sz;
  9780. + unsigned long vd_jiffy;
  9781. +} ____cacheline_aligned_in_smp;
  9782. +
  9783. +/* ---------------------------------------------------------------------- */
  9784. +
  9785. +/* dir.c */
  9786. +extern const struct file_operations aufs_dir_fop;
  9787. +void au_add_nlink(struct inode *dir, struct inode *h_dir);
  9788. +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
  9789. +loff_t au_dir_size(struct file *file, struct dentry *dentry);
  9790. +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
  9791. +int au_test_empty_lower(struct dentry *dentry);
  9792. +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
  9793. +
  9794. +/* vdir.c */
  9795. +unsigned int au_rdhash_est(loff_t sz);
  9796. +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
  9797. +void au_nhash_wh_free(struct au_nhash *whlist);
  9798. +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
  9799. + int limit);
  9800. +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
  9801. +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
  9802. + unsigned int d_type, aufs_bindex_t bindex,
  9803. + unsigned char shwh);
  9804. +void au_vdir_free(struct au_vdir *vdir);
  9805. +int au_vdir_init(struct file *file);
  9806. +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
  9807. +
  9808. +/* ioctl.c */
  9809. +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
  9810. +
  9811. +#ifdef CONFIG_AUFS_RDU
  9812. +/* rdu.c */
  9813. +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  9814. +#ifdef CONFIG_COMPAT
  9815. +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
  9816. + unsigned long arg);
  9817. +#endif
  9818. +#else
  9819. +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
  9820. + unsigned int cmd, unsigned long arg)
  9821. +#ifdef CONFIG_COMPAT
  9822. +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
  9823. + unsigned int cmd, unsigned long arg)
  9824. +#endif
  9825. +#endif
  9826. +
  9827. +#endif /* __KERNEL__ */
  9828. +#endif /* __AUFS_DIR_H__ */
  9829. --- /dev/null
  9830. +++ linux-4.19/fs/aufs/dirren.c 2018-11-20 09:35:15.017195504 +0200
  9831. @@ -0,0 +1,1316 @@
  9832. +// SPDX-License-Identifier: GPL-2.0
  9833. +/*
  9834. + * Copyright (C) 2017-2018 Junjiro R. Okajima
  9835. + *
  9836. + * This program, aufs is free software; you can redistribute it and/or modify
  9837. + * it under the terms of the GNU General Public License as published by
  9838. + * the Free Software Foundation; either version 2 of the License, or
  9839. + * (at your option) any later version.
  9840. + *
  9841. + * This program is distributed in the hope that it will be useful,
  9842. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9843. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9844. + * GNU General Public License for more details.
  9845. + *
  9846. + * You should have received a copy of the GNU General Public License
  9847. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  9848. + */
  9849. +
  9850. +/*
  9851. + * special handling in renaming a directory
  9852. + * in order to support looking-up the before-renamed name on the lower readonly
  9853. + * branches
  9854. + */
  9855. +
  9856. +#include <linux/byteorder/generic.h>
  9857. +#include "aufs.h"
  9858. +
  9859. +static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
  9860. +{
  9861. + int idx;
  9862. +
  9863. + idx = au_dr_ihash(ent->dr_h_ino);
  9864. + au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
  9865. +}
  9866. +
  9867. +static int au_dr_hino_test_empty(struct au_dr_br *dr)
  9868. +{
  9869. + int ret, i;
  9870. + struct hlist_bl_head *hbl;
  9871. +
  9872. + ret = 1;
  9873. + for (i = 0; ret && i < AuDirren_NHASH; i++) {
  9874. + hbl = dr->dr_h_ino + i;
  9875. + hlist_bl_lock(hbl);
  9876. + ret &= hlist_bl_empty(hbl);
  9877. + hlist_bl_unlock(hbl);
  9878. + }
  9879. +
  9880. + return ret;
  9881. +}
  9882. +
  9883. +static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
  9884. +{
  9885. + struct au_dr_hino *found, *ent;
  9886. + struct hlist_bl_head *hbl;
  9887. + struct hlist_bl_node *pos;
  9888. + int idx;
  9889. +
  9890. + found = NULL;
  9891. + idx = au_dr_ihash(ino);
  9892. + hbl = dr->dr_h_ino + idx;
  9893. + hlist_bl_lock(hbl);
  9894. + hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
  9895. + if (ent->dr_h_ino == ino) {
  9896. + found = ent;
  9897. + break;
  9898. + }
  9899. + hlist_bl_unlock(hbl);
  9900. +
  9901. + return found;
  9902. +}
  9903. +
  9904. +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
  9905. + struct au_dr_hino *add_ent)
  9906. +{
  9907. + int found, idx;
  9908. + struct hlist_bl_head *hbl;
  9909. + struct hlist_bl_node *pos;
  9910. + struct au_dr_hino *ent;
  9911. +
  9912. + found = 0;
  9913. + idx = au_dr_ihash(ino);
  9914. + hbl = dr->dr_h_ino + idx;
  9915. +#if 0
  9916. + {
  9917. + struct hlist_bl_node *tmp;
  9918. +
  9919. + hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
  9920. + AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
  9921. + }
  9922. +#endif
  9923. + hlist_bl_lock(hbl);
  9924. + hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
  9925. + if (ent->dr_h_ino == ino) {
  9926. + found = 1;
  9927. + break;
  9928. + }
  9929. + if (!found && add_ent)
  9930. + hlist_bl_add_head(&add_ent->dr_hnode, hbl);
  9931. + hlist_bl_unlock(hbl);
  9932. +
  9933. + if (!found && add_ent)
  9934. + AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
  9935. +
  9936. + return found;
  9937. +}
  9938. +
  9939. +void au_dr_hino_free(struct au_dr_br *dr)
  9940. +{
  9941. + int i;
  9942. + struct hlist_bl_head *hbl;
  9943. + struct hlist_bl_node *pos, *tmp;
  9944. + struct au_dr_hino *ent;
  9945. +
  9946. + /* SiMustWriteLock(sb); */
  9947. +
  9948. + for (i = 0; i < AuDirren_NHASH; i++) {
  9949. + hbl = dr->dr_h_ino + i;
  9950. + /* no spinlock since sbinfo must be write-locked */
  9951. + hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
  9952. + kfree(ent);
  9953. + INIT_HLIST_BL_HEAD(hbl);
  9954. + }
  9955. +}
  9956. +
  9957. +/* returns the number of inodes or an error */
  9958. +static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
  9959. + struct file *hinofile)
  9960. +{
  9961. + int err, i;
  9962. + ssize_t ssz;
  9963. + loff_t pos, oldsize;
  9964. + __be64 u64;
  9965. + struct inode *hinoinode;
  9966. + struct hlist_bl_head *hbl;
  9967. + struct hlist_bl_node *n1, *n2;
  9968. + struct au_dr_hino *ent;
  9969. +
  9970. + SiMustWriteLock(sb);
  9971. + AuDebugOn(!au_br_writable(br->br_perm));
  9972. +
  9973. + hinoinode = file_inode(hinofile);
  9974. + oldsize = i_size_read(hinoinode);
  9975. +
  9976. + err = 0;
  9977. + pos = 0;
  9978. + hbl = br->br_dirren.dr_h_ino;
  9979. + for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
  9980. + /* no bit-lock since sbinfo must be write-locked */
  9981. + hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
  9982. + AuDbg("hi%llu, %pD2\n",
  9983. + (unsigned long long)ent->dr_h_ino, hinofile);
  9984. + u64 = cpu_to_be64(ent->dr_h_ino);
  9985. + ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
  9986. + if (ssz == sizeof(u64))
  9987. + continue;
  9988. +
  9989. + /* write error */
  9990. + pr_err("ssz %zd, %pD2\n", ssz, hinofile);
  9991. + err = -ENOSPC;
  9992. + if (ssz < 0)
  9993. + err = ssz;
  9994. + break;
  9995. + }
  9996. + }
  9997. + /* regardless the error */
  9998. + if (pos < oldsize) {
  9999. + err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
  10000. + AuTraceErr(err);
  10001. + }
  10002. +
  10003. + AuTraceErr(err);
  10004. + return err;
  10005. +}
  10006. +
  10007. +static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
  10008. +{
  10009. + int err, hidx;
  10010. + ssize_t ssz;
  10011. + size_t sz, n;
  10012. + loff_t pos;
  10013. + uint64_t u64;
  10014. + struct au_dr_hino *ent;
  10015. + struct inode *hinoinode;
  10016. + struct hlist_bl_head *hbl;
  10017. +
  10018. + err = 0;
  10019. + pos = 0;
  10020. + hbl = dr->dr_h_ino;
  10021. + hinoinode = file_inode(hinofile);
  10022. + sz = i_size_read(hinoinode);
  10023. + AuDebugOn(sz % sizeof(u64));
  10024. + n = sz / sizeof(u64);
  10025. + while (n--) {
  10026. + ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
  10027. + if (unlikely(ssz != sizeof(u64))) {
  10028. + pr_err("ssz %zd, %pD2\n", ssz, hinofile);
  10029. + err = -EINVAL;
  10030. + if (ssz < 0)
  10031. + err = ssz;
  10032. + goto out_free;
  10033. + }
  10034. +
  10035. + ent = kmalloc(sizeof(*ent), GFP_NOFS);
  10036. + if (!ent) {
  10037. + err = -ENOMEM;
  10038. + AuTraceErr(err);
  10039. + goto out_free;
  10040. + }
  10041. + ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
  10042. + AuDbg("hi%llu, %pD2\n",
  10043. + (unsigned long long)ent->dr_h_ino, hinofile);
  10044. + hidx = au_dr_ihash(ent->dr_h_ino);
  10045. + au_hbl_add(&ent->dr_hnode, hbl + hidx);
  10046. + }
  10047. + goto out; /* success */
  10048. +
  10049. +out_free:
  10050. + au_dr_hino_free(dr);
  10051. +out:
  10052. + AuTraceErr(err);
  10053. + return err;
  10054. +}
  10055. +
  10056. +/*
  10057. + * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
  10058. + * @path is a switch to distinguish load and store.
  10059. + */
  10060. +static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
  10061. + struct au_branch *br, const struct path *path)
  10062. +{
  10063. + int err, flags;
  10064. + unsigned char load, suspend;
  10065. + struct file *hinofile;
  10066. + struct au_hinode *hdir;
  10067. + struct inode *dir, *delegated;
  10068. + struct path hinopath;
  10069. + struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
  10070. + sizeof(AUFS_WH_DR_BRHINO) - 1);
  10071. +
  10072. + AuDebugOn(bindex < 0 && !br);
  10073. + AuDebugOn(bindex >= 0 && br);
  10074. +
  10075. + err = -EINVAL;
  10076. + suspend = !br;
  10077. + if (suspend)
  10078. + br = au_sbr(sb, bindex);
  10079. + load = !!path;
  10080. + if (!load) {
  10081. + path = &br->br_path;
  10082. + AuDebugOn(!au_br_writable(br->br_perm));
  10083. + if (unlikely(!au_br_writable(br->br_perm)))
  10084. + goto out;
  10085. + }
  10086. +
  10087. + hdir = NULL;
  10088. + if (suspend) {
  10089. + dir = d_inode(sb->s_root);
  10090. + hdir = au_hinode(au_ii(dir), bindex);
  10091. + dir = hdir->hi_inode;
  10092. + au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
  10093. + } else {
  10094. + dir = d_inode(path->dentry);
  10095. + inode_lock_nested(dir, AuLsc_I_CHILD);
  10096. + }
  10097. + hinopath.dentry = vfsub_lkup_one(&hinoname, path->dentry);
  10098. + err = PTR_ERR(hinopath.dentry);
  10099. + if (IS_ERR(hinopath.dentry))
  10100. + goto out_unlock;
  10101. +
  10102. + err = 0;
  10103. + flags = O_RDONLY;
  10104. + if (load) {
  10105. + if (d_is_negative(hinopath.dentry))
  10106. + goto out_dput; /* success */
  10107. + } else {
  10108. + if (au_dr_hino_test_empty(&br->br_dirren)) {
  10109. + if (d_is_positive(hinopath.dentry)) {
  10110. + delegated = NULL;
  10111. + err = vfsub_unlink(dir, &hinopath, &delegated,
  10112. + /*force*/0);
  10113. + AuTraceErr(err);
  10114. + if (unlikely(err))
  10115. + pr_err("ignored err %d, %pd2\n",
  10116. + err, hinopath.dentry);
  10117. + if (unlikely(err == -EWOULDBLOCK))
  10118. + iput(delegated);
  10119. + err = 0;
  10120. + }
  10121. + goto out_dput;
  10122. + } else if (!d_is_positive(hinopath.dentry)) {
  10123. + err = vfsub_create(dir, &hinopath, 0600,
  10124. + /*want_excl*/false);
  10125. + AuTraceErr(err);
  10126. + if (unlikely(err))
  10127. + goto out_dput;
  10128. + }
  10129. + flags = O_WRONLY;
  10130. + }
  10131. + hinopath.mnt = path->mnt;
  10132. + hinofile = vfsub_dentry_open(&hinopath, flags);
  10133. + if (suspend)
  10134. + au_hn_inode_unlock(hdir);
  10135. + else
  10136. + inode_unlock(dir);
  10137. + dput(hinopath.dentry);
  10138. + AuTraceErrPtr(hinofile);
  10139. + if (IS_ERR(hinofile)) {
  10140. + err = PTR_ERR(hinofile);
  10141. + goto out;
  10142. + }
  10143. +
  10144. + if (load)
  10145. + err = au_dr_hino_load(&br->br_dirren, hinofile);
  10146. + else
  10147. + err = au_dr_hino_store(sb, br, hinofile);
  10148. + fput(hinofile);
  10149. + goto out;
  10150. +
  10151. +out_dput:
  10152. + dput(hinopath.dentry);
  10153. +out_unlock:
  10154. + if (suspend)
  10155. + au_hn_inode_unlock(hdir);
  10156. + else
  10157. + inode_unlock(dir);
  10158. +out:
  10159. + AuTraceErr(err);
  10160. + return err;
  10161. +}
  10162. +
  10163. +/* ---------------------------------------------------------------------- */
  10164. +
  10165. +static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
  10166. +{
  10167. + int err;
  10168. + struct kstatfs kstfs;
  10169. + dev_t dev;
  10170. + struct dentry *dentry;
  10171. + struct super_block *sb;
  10172. +
  10173. + err = vfs_statfs((void *)path, &kstfs);
  10174. + AuTraceErr(err);
  10175. + if (unlikely(err))
  10176. + goto out;
  10177. +
  10178. + /* todo: support for UUID */
  10179. +
  10180. + if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
  10181. + brid->type = AuBrid_FSID;
  10182. + brid->fsid = kstfs.f_fsid;
  10183. + } else {
  10184. + dentry = path->dentry;
  10185. + sb = dentry->d_sb;
  10186. + dev = sb->s_dev;
  10187. + if (dev) {
  10188. + brid->type = AuBrid_DEV;
  10189. + brid->dev = dev;
  10190. + }
  10191. + }
  10192. +
  10193. +out:
  10194. + return err;
  10195. +}
  10196. +
  10197. +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
  10198. + const struct path *path)
  10199. +{
  10200. + int err, i;
  10201. + struct au_dr_br *dr;
  10202. + struct hlist_bl_head *hbl;
  10203. +
  10204. + dr = &br->br_dirren;
  10205. + hbl = dr->dr_h_ino;
  10206. + for (i = 0; i < AuDirren_NHASH; i++, hbl++)
  10207. + INIT_HLIST_BL_HEAD(hbl);
  10208. +
  10209. + err = au_dr_brid_init(&dr->dr_brid, path);
  10210. + if (unlikely(err))
  10211. + goto out;
  10212. +
  10213. + if (au_opt_test(au_mntflags(sb), DIRREN))
  10214. + err = au_dr_hino(sb, /*bindex*/-1, br, path);
  10215. +
  10216. +out:
  10217. + AuTraceErr(err);
  10218. + return err;
  10219. +}
  10220. +
  10221. +int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
  10222. +{
  10223. + int err;
  10224. +
  10225. + err = 0;
  10226. + if (au_br_writable(br->br_perm))
  10227. + err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
  10228. + if (!err)
  10229. + au_dr_hino_free(&br->br_dirren);
  10230. +
  10231. + return err;
  10232. +}
  10233. +
  10234. +/* ---------------------------------------------------------------------- */
  10235. +
  10236. +static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
  10237. + char *buf, size_t sz)
  10238. +{
  10239. + int err;
  10240. + unsigned int major, minor;
  10241. + char *p;
  10242. +
  10243. + p = buf;
  10244. + err = snprintf(p, sz, "%d_", brid->type);
  10245. + AuDebugOn(err > sz);
  10246. + p += err;
  10247. + sz -= err;
  10248. + switch (brid->type) {
  10249. + case AuBrid_Unset:
  10250. + return -EINVAL;
  10251. + case AuBrid_UUID:
  10252. + err = snprintf(p, sz, "%pU", brid->uuid.b);
  10253. + break;
  10254. + case AuBrid_FSID:
  10255. + err = snprintf(p, sz, "%08x-%08x",
  10256. + brid->fsid.val[0], brid->fsid.val[1]);
  10257. + break;
  10258. + case AuBrid_DEV:
  10259. + major = MAJOR(brid->dev);
  10260. + minor = MINOR(brid->dev);
  10261. + if (major <= 0xff && minor <= 0xff)
  10262. + err = snprintf(p, sz, "%02x%02x", major, minor);
  10263. + else
  10264. + err = snprintf(p, sz, "%03x:%05x", major, minor);
  10265. + break;
  10266. + }
  10267. + AuDebugOn(err > sz);
  10268. + p += err;
  10269. + sz -= err;
  10270. + err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
  10271. + AuDebugOn(err > sz);
  10272. + p += err;
  10273. + sz -= err;
  10274. +
  10275. + return p - buf;
  10276. +}
  10277. +
  10278. +static int au_drinfo_name(struct au_branch *br, char *name, int len)
  10279. +{
  10280. + int rlen;
  10281. + struct dentry *br_dentry;
  10282. + struct inode *br_inode;
  10283. +
  10284. + br_dentry = au_br_dentry(br);
  10285. + br_inode = d_inode(br_dentry);
  10286. + rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
  10287. + AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
  10288. + AuDebugOn(rlen > len);
  10289. +
  10290. + return rlen;
  10291. +}
  10292. +
  10293. +/* ---------------------------------------------------------------------- */
  10294. +
  10295. +/*
  10296. + * from the given @h_dentry, construct drinfo at @*fdata.
  10297. + * when the size of @*fdata is not enough, reallocate and return new @fdata and
  10298. + * @allocated.
  10299. + */
  10300. +static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
  10301. + struct dentry *h_dentry,
  10302. + unsigned char *allocated)
  10303. +{
  10304. + int err, v;
  10305. + struct au_drinfo_fdata *f, *p;
  10306. + struct au_drinfo *drinfo;
  10307. + struct inode *h_inode;
  10308. + struct qstr *qname;
  10309. +
  10310. + err = 0;
  10311. + f = *fdata;
  10312. + h_inode = d_inode(h_dentry);
  10313. + qname = &h_dentry->d_name;
  10314. + drinfo = &f->drinfo;
  10315. + drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
  10316. + drinfo->oldnamelen = qname->len;
  10317. + if (*allocated < sizeof(*f) + qname->len) {
  10318. + v = roundup_pow_of_two(*allocated + qname->len);
  10319. + p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
  10320. + if (unlikely(!p)) {
  10321. + err = -ENOMEM;
  10322. + AuTraceErr(err);
  10323. + goto out;
  10324. + }
  10325. + f = p;
  10326. + *fdata = f;
  10327. + *allocated = v;
  10328. + drinfo = &f->drinfo;
  10329. + }
  10330. + memcpy(drinfo->oldname, qname->name, qname->len);
  10331. + AuDbg("i%llu, %.*s\n",
  10332. + be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
  10333. + drinfo->oldname);
  10334. +
  10335. +out:
  10336. + AuTraceErr(err);
  10337. + return err;
  10338. +}
  10339. +
  10340. +/* callers have to free the return value */
  10341. +static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
  10342. +{
  10343. + struct au_drinfo *ret, *drinfo;
  10344. + struct au_drinfo_fdata fdata;
  10345. + int len;
  10346. + loff_t pos;
  10347. + ssize_t ssz;
  10348. +
  10349. + ret = ERR_PTR(-EIO);
  10350. + pos = 0;
  10351. + ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
  10352. + if (unlikely(ssz != sizeof(fdata))) {
  10353. + AuIOErr("ssz %zd, %u, %pD2\n",
  10354. + ssz, (unsigned int)sizeof(fdata), file);
  10355. + goto out;
  10356. + }
  10357. +
  10358. + fdata.magic = ntohl((__force __be32)fdata.magic);
  10359. + switch (fdata.magic) {
  10360. + case AUFS_DRINFO_MAGIC_V1:
  10361. + break;
  10362. + default:
  10363. + AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
  10364. + fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
  10365. + goto out;
  10366. + }
  10367. +
  10368. + drinfo = &fdata.drinfo;
  10369. + len = drinfo->oldnamelen;
  10370. + if (!len) {
  10371. + AuIOErr("broken drinfo %pD2\n", file);
  10372. + goto out;
  10373. + }
  10374. +
  10375. + ret = NULL;
  10376. + drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
  10377. + if (unlikely(h_ino && drinfo->ino != h_ino)) {
  10378. + AuDbg("ignored i%llu, i%llu, %pD2\n",
  10379. + (unsigned long long)drinfo->ino,
  10380. + (unsigned long long)h_ino, file);
  10381. + goto out; /* success */
  10382. + }
  10383. +
  10384. + ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
  10385. + if (unlikely(!ret)) {
  10386. + ret = ERR_PTR(-ENOMEM);
  10387. + AuTraceErrPtr(ret);
  10388. + goto out;
  10389. + }
  10390. +
  10391. + *ret = *drinfo;
  10392. + ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
  10393. + if (unlikely(ssz != len)) {
  10394. + kfree(ret);
  10395. + ret = ERR_PTR(-EIO);
  10396. + AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
  10397. + goto out;
  10398. + }
  10399. +
  10400. + AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
  10401. +
  10402. +out:
  10403. + return ret;
  10404. +}
  10405. +
  10406. +/* ---------------------------------------------------------------------- */
  10407. +
  10408. +/* in order to be revertible */
  10409. +struct au_drinfo_rev_elm {
  10410. + int created;
  10411. + struct dentry *info_dentry;
  10412. + struct au_drinfo *info_last;
  10413. +};
  10414. +
  10415. +struct au_drinfo_rev {
  10416. + unsigned char already;
  10417. + aufs_bindex_t nelm;
  10418. + struct au_drinfo_rev_elm elm[0];
  10419. +};
  10420. +
  10421. +/* todo: isn't it too large? */
  10422. +struct au_drinfo_store {
  10423. + struct path h_ppath;
  10424. + struct dentry *h_dentry;
  10425. + struct au_drinfo_fdata *fdata;
  10426. + char *infoname; /* inside of whname, just after PFX */
  10427. + char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
  10428. + aufs_bindex_t btgt, btail;
  10429. + unsigned char no_sio,
  10430. + allocated, /* current size of *fdata */
  10431. + infonamelen, /* room size for p */
  10432. + whnamelen, /* length of the generated name */
  10433. + renameback; /* renamed back */
  10434. +};
  10435. +
  10436. +/* on rename(2) error, the caller should revert it using @elm */
  10437. +static int au_drinfo_do_store(struct au_drinfo_store *w,
  10438. + struct au_drinfo_rev_elm *elm)
  10439. +{
  10440. + int err, len;
  10441. + ssize_t ssz;
  10442. + loff_t pos;
  10443. + struct path infopath = {
  10444. + .mnt = w->h_ppath.mnt
  10445. + };
  10446. + struct inode *h_dir, *h_inode, *delegated;
  10447. + struct file *infofile;
  10448. + struct qstr *qname;
  10449. +
  10450. + AuDebugOn(elm
  10451. + && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
  10452. +
  10453. + infopath.dentry = vfsub_lookup_one_len(w->whname, w->h_ppath.dentry,
  10454. + w->whnamelen);
  10455. + AuTraceErrPtr(infopath.dentry);
  10456. + if (IS_ERR(infopath.dentry)) {
  10457. + err = PTR_ERR(infopath.dentry);
  10458. + goto out;
  10459. + }
  10460. +
  10461. + err = 0;
  10462. + h_dir = d_inode(w->h_ppath.dentry);
  10463. + if (elm && d_is_negative(infopath.dentry)) {
  10464. + err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
  10465. + AuTraceErr(err);
  10466. + if (unlikely(err))
  10467. + goto out_dput;
  10468. + elm->created = 1;
  10469. + elm->info_dentry = dget(infopath.dentry);
  10470. + }
  10471. +
  10472. + infofile = vfsub_dentry_open(&infopath, O_RDWR);
  10473. + AuTraceErrPtr(infofile);
  10474. + if (IS_ERR(infofile)) {
  10475. + err = PTR_ERR(infofile);
  10476. + goto out_dput;
  10477. + }
  10478. +
  10479. + h_inode = d_inode(infopath.dentry);
  10480. + if (elm && i_size_read(h_inode)) {
  10481. + h_inode = d_inode(w->h_dentry);
  10482. + elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
  10483. + AuTraceErrPtr(elm->info_last);
  10484. + if (IS_ERR(elm->info_last)) {
  10485. + err = PTR_ERR(elm->info_last);
  10486. + elm->info_last = NULL;
  10487. + AuDebugOn(elm->info_dentry);
  10488. + goto out_fput;
  10489. + }
  10490. + }
  10491. +
  10492. + if (elm && w->renameback) {
  10493. + delegated = NULL;
  10494. + err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
  10495. + AuTraceErr(err);
  10496. + if (unlikely(err == -EWOULDBLOCK))
  10497. + iput(delegated);
  10498. + goto out_fput;
  10499. + }
  10500. +
  10501. + pos = 0;
  10502. + qname = &w->h_dentry->d_name;
  10503. + len = sizeof(*w->fdata) + qname->len;
  10504. + if (!elm)
  10505. + len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
  10506. + ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
  10507. + if (ssz == len) {
  10508. + AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
  10509. + w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
  10510. + goto out_fput; /* success */
  10511. + } else {
  10512. + err = -EIO;
  10513. + if (ssz < 0)
  10514. + err = ssz;
  10515. + /* the caller should revert it using @elm */
  10516. + }
  10517. +
  10518. +out_fput:
  10519. + fput(infofile);
  10520. +out_dput:
  10521. + dput(infopath.dentry);
  10522. +out:
  10523. + AuTraceErr(err);
  10524. + return err;
  10525. +}
  10526. +
  10527. +struct au_call_drinfo_do_store_args {
  10528. + int *errp;
  10529. + struct au_drinfo_store *w;
  10530. + struct au_drinfo_rev_elm *elm;
  10531. +};
  10532. +
  10533. +static void au_call_drinfo_do_store(void *args)
  10534. +{
  10535. + struct au_call_drinfo_do_store_args *a = args;
  10536. +
  10537. + *a->errp = au_drinfo_do_store(a->w, a->elm);
  10538. +}
  10539. +
  10540. +static int au_drinfo_store_sio(struct au_drinfo_store *w,
  10541. + struct au_drinfo_rev_elm *elm)
  10542. +{
  10543. + int err, wkq_err;
  10544. +
  10545. + if (w->no_sio)
  10546. + err = au_drinfo_do_store(w, elm);
  10547. + else {
  10548. + struct au_call_drinfo_do_store_args a = {
  10549. + .errp = &err,
  10550. + .w = w,
  10551. + .elm = elm
  10552. + };
  10553. + wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
  10554. + if (unlikely(wkq_err))
  10555. + err = wkq_err;
  10556. + }
  10557. + AuTraceErr(err);
  10558. +
  10559. + return err;
  10560. +}
  10561. +
  10562. +static int au_drinfo_store_work_init(struct au_drinfo_store *w,
  10563. + aufs_bindex_t btgt)
  10564. +{
  10565. + int err;
  10566. +
  10567. + memset(w, 0, sizeof(*w));
  10568. + w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
  10569. + strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
  10570. + w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
  10571. + w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
  10572. + w->btgt = btgt;
  10573. + w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
  10574. +
  10575. + err = -ENOMEM;
  10576. + w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
  10577. + if (unlikely(!w->fdata)) {
  10578. + AuTraceErr(err);
  10579. + goto out;
  10580. + }
  10581. + w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
  10582. + err = 0;
  10583. +
  10584. +out:
  10585. + return err;
  10586. +}
  10587. +
  10588. +static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
  10589. +{
  10590. + kfree(w->fdata);
  10591. +}
  10592. +
  10593. +static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
  10594. + struct au_drinfo_store *w)
  10595. +{
  10596. + struct au_drinfo_rev_elm *elm;
  10597. + struct inode *h_dir, *delegated;
  10598. + int err, nelm;
  10599. + struct path infopath = {
  10600. + .mnt = w->h_ppath.mnt
  10601. + };
  10602. +
  10603. + h_dir = d_inode(w->h_ppath.dentry);
  10604. + IMustLock(h_dir);
  10605. +
  10606. + err = 0;
  10607. + elm = rev->elm;
  10608. + for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
  10609. + AuDebugOn(elm->created && elm->info_last);
  10610. + if (elm->created) {
  10611. + AuDbg("here\n");
  10612. + delegated = NULL;
  10613. + infopath.dentry = elm->info_dentry;
  10614. + err = vfsub_unlink(h_dir, &infopath, &delegated,
  10615. + !w->no_sio);
  10616. + AuTraceErr(err);
  10617. + if (unlikely(err == -EWOULDBLOCK))
  10618. + iput(delegated);
  10619. + dput(elm->info_dentry);
  10620. + } else if (elm->info_last) {
  10621. + AuDbg("here\n");
  10622. + w->fdata->drinfo = *elm->info_last;
  10623. + memcpy(w->fdata->drinfo.oldname,
  10624. + elm->info_last->oldname,
  10625. + elm->info_last->oldnamelen);
  10626. + err = au_drinfo_store_sio(w, /*elm*/NULL);
  10627. + kfree(elm->info_last);
  10628. + }
  10629. + if (unlikely(err))
  10630. + AuIOErr("%d, %s\n", err, w->whname);
  10631. + /* go on even if err */
  10632. + }
  10633. +}
  10634. +
  10635. +/* caller has to call au_dr_rename_fin() later */
  10636. +static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
  10637. + struct qstr *dst_name, void *_rev)
  10638. +{
  10639. + int err, sz, nelm;
  10640. + aufs_bindex_t bindex, btail;
  10641. + struct au_drinfo_store work;
  10642. + struct au_drinfo_rev *rev, **p;
  10643. + struct au_drinfo_rev_elm *elm;
  10644. + struct super_block *sb;
  10645. + struct au_branch *br;
  10646. + struct au_hinode *hdir;
  10647. +
  10648. + err = au_drinfo_store_work_init(&work, btgt);
  10649. + AuTraceErr(err);
  10650. + if (unlikely(err))
  10651. + goto out;
  10652. +
  10653. + err = -ENOMEM;
  10654. + btail = au_dbtaildir(dentry);
  10655. + nelm = btail - btgt;
  10656. + sz = sizeof(*rev) + sizeof(*elm) * nelm;
  10657. + rev = kcalloc(1, sz, GFP_NOFS);
  10658. + if (unlikely(!rev)) {
  10659. + AuTraceErr(err);
  10660. + goto out_args;
  10661. + }
  10662. + rev->nelm = nelm;
  10663. + elm = rev->elm;
  10664. + p = _rev;
  10665. + *p = rev;
  10666. +
  10667. + err = 0;
  10668. + sb = dentry->d_sb;
  10669. + work.h_ppath.dentry = au_h_dptr(dentry, btgt);
  10670. + work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
  10671. + hdir = au_hi(d_inode(dentry), btgt);
  10672. + au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
  10673. + for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
  10674. + work.h_dentry = au_h_dptr(dentry, bindex);
  10675. + if (!work.h_dentry)
  10676. + continue;
  10677. +
  10678. + err = au_drinfo_construct(&work.fdata, work.h_dentry,
  10679. + &work.allocated);
  10680. + AuTraceErr(err);
  10681. + if (unlikely(err))
  10682. + break;
  10683. +
  10684. + work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
  10685. + br = au_sbr(sb, bindex);
  10686. + work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
  10687. + work.whnamelen += au_drinfo_name(br, work.infoname,
  10688. + work.infonamelen);
  10689. + AuDbg("whname %.*s, i%llu, %.*s\n",
  10690. + work.whnamelen, work.whname,
  10691. + be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
  10692. + work.fdata->drinfo.oldnamelen,
  10693. + work.fdata->drinfo.oldname);
  10694. +
  10695. + err = au_drinfo_store_sio(&work, elm);
  10696. + AuTraceErr(err);
  10697. + if (unlikely(err))
  10698. + break;
  10699. + }
  10700. + if (unlikely(err)) {
  10701. + /* revert all drinfo */
  10702. + au_drinfo_store_rev(rev, &work);
  10703. + kfree(rev);
  10704. + *p = NULL;
  10705. + }
  10706. + au_hn_inode_unlock(hdir);
  10707. +
  10708. +out_args:
  10709. + au_drinfo_store_work_fin(&work);
  10710. +out:
  10711. + return err;
  10712. +}
  10713. +
  10714. +/* ---------------------------------------------------------------------- */
  10715. +
  10716. +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
  10717. + struct qstr *dst_name, void *_rev)
  10718. +{
  10719. + int err, already;
  10720. + ino_t ino;
  10721. + struct super_block *sb;
  10722. + struct au_branch *br;
  10723. + struct au_dr_br *dr;
  10724. + struct dentry *h_dentry;
  10725. + struct inode *h_inode;
  10726. + struct au_dr_hino *ent;
  10727. + struct au_drinfo_rev *rev, **p;
  10728. +
  10729. + AuDbg("bindex %d\n", bindex);
  10730. +
  10731. + err = -ENOMEM;
  10732. + ent = kmalloc(sizeof(*ent), GFP_NOFS);
  10733. + if (unlikely(!ent))
  10734. + goto out;
  10735. +
  10736. + sb = src->d_sb;
  10737. + br = au_sbr(sb, bindex);
  10738. + dr = &br->br_dirren;
  10739. + h_dentry = au_h_dptr(src, bindex);
  10740. + h_inode = d_inode(h_dentry);
  10741. + ino = h_inode->i_ino;
  10742. + ent->dr_h_ino = ino;
  10743. + already = au_dr_hino_test_add(dr, ino, ent);
  10744. + AuDbg("b%d, hi%llu, already %d\n",
  10745. + bindex, (unsigned long long)ino, already);
  10746. +
  10747. + err = au_drinfo_store(src, bindex, dst_name, _rev);
  10748. + AuTraceErr(err);
  10749. + if (!err) {
  10750. + p = _rev;
  10751. + rev = *p;
  10752. + rev->already = already;
  10753. + goto out; /* success */
  10754. + }
  10755. +
  10756. + /* revert */
  10757. + if (!already)
  10758. + au_dr_hino_del(dr, ent);
  10759. + kfree(ent);
  10760. +
  10761. +out:
  10762. + AuTraceErr(err);
  10763. + return err;
  10764. +}
  10765. +
  10766. +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
  10767. +{
  10768. + struct au_drinfo_rev *rev;
  10769. + struct au_drinfo_rev_elm *elm;
  10770. + int nelm;
  10771. +
  10772. + rev = _rev;
  10773. + elm = rev->elm;
  10774. + for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
  10775. + dput(elm->info_dentry);
  10776. + kfree(elm->info_last);
  10777. + }
  10778. + kfree(rev);
  10779. +}
  10780. +
  10781. +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
  10782. +{
  10783. + int err;
  10784. + struct au_drinfo_store work;
  10785. + struct au_drinfo_rev *rev = _rev;
  10786. + struct super_block *sb;
  10787. + struct au_branch *br;
  10788. + struct inode *h_inode;
  10789. + struct au_dr_br *dr;
  10790. + struct au_dr_hino *ent;
  10791. +
  10792. + err = au_drinfo_store_work_init(&work, btgt);
  10793. + if (unlikely(err))
  10794. + goto out;
  10795. +
  10796. + sb = src->d_sb;
  10797. + br = au_sbr(sb, btgt);
  10798. + work.h_ppath.dentry = au_h_dptr(src, btgt);
  10799. + work.h_ppath.mnt = au_br_mnt(br);
  10800. + au_drinfo_store_rev(rev, &work);
  10801. + au_drinfo_store_work_fin(&work);
  10802. + if (rev->already)
  10803. + goto out;
  10804. +
  10805. + dr = &br->br_dirren;
  10806. + h_inode = d_inode(work.h_ppath.dentry);
  10807. + ent = au_dr_hino_find(dr, h_inode->i_ino);
  10808. + BUG_ON(!ent);
  10809. + au_dr_hino_del(dr, ent);
  10810. + kfree(ent);
  10811. +
  10812. +out:
  10813. + kfree(rev);
  10814. + if (unlikely(err))
  10815. + pr_err("failed to remove dirren info\n");
  10816. +}
  10817. +
  10818. +/* ---------------------------------------------------------------------- */
  10819. +
  10820. +static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
  10821. + char *whname, int whnamelen,
  10822. + struct dentry **info_dentry)
  10823. +{
  10824. + struct au_drinfo *drinfo;
  10825. + struct file *f;
  10826. + struct inode *h_dir;
  10827. + struct path infopath;
  10828. + int unlocked;
  10829. +
  10830. + AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
  10831. +
  10832. + *info_dentry = NULL;
  10833. + drinfo = NULL;
  10834. + unlocked = 0;
  10835. + h_dir = d_inode(h_ppath->dentry);
  10836. + inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
  10837. + infopath.dentry = vfsub_lookup_one_len(whname, h_ppath->dentry,
  10838. + whnamelen);
  10839. + if (IS_ERR(infopath.dentry)) {
  10840. + drinfo = (void *)infopath.dentry;
  10841. + goto out;
  10842. + }
  10843. +
  10844. + if (d_is_negative(infopath.dentry))
  10845. + goto out_dput; /* success */
  10846. +
  10847. + infopath.mnt = h_ppath->mnt;
  10848. + f = vfsub_dentry_open(&infopath, O_RDONLY);
  10849. + inode_unlock_shared(h_dir);
  10850. + unlocked = 1;
  10851. + if (IS_ERR(f)) {
  10852. + drinfo = (void *)f;
  10853. + goto out_dput;
  10854. + }
  10855. +
  10856. + drinfo = au_drinfo_read_k(f, /*h_ino*/0);
  10857. + if (IS_ERR_OR_NULL(drinfo))
  10858. + goto out_fput;
  10859. +
  10860. + AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
  10861. + *info_dentry = dget(infopath.dentry); /* keep it alive */
  10862. +
  10863. +out_fput:
  10864. + fput(f);
  10865. +out_dput:
  10866. + dput(infopath.dentry);
  10867. +out:
  10868. + if (!unlocked)
  10869. + inode_unlock_shared(h_dir);
  10870. + AuTraceErrPtr(drinfo);
  10871. + return drinfo;
  10872. +}
  10873. +
  10874. +struct au_drinfo_do_load_args {
  10875. + struct au_drinfo **drinfop;
  10876. + struct path *h_ppath;
  10877. + char *whname;
  10878. + int whnamelen;
  10879. + struct dentry **info_dentry;
  10880. +};
  10881. +
  10882. +static void au_call_drinfo_do_load(void *args)
  10883. +{
  10884. + struct au_drinfo_do_load_args *a = args;
  10885. +
  10886. + *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
  10887. + a->info_dentry);
  10888. +}
  10889. +
  10890. +struct au_drinfo_load {
  10891. + struct path h_ppath;
  10892. + struct qstr *qname;
  10893. + unsigned char no_sio;
  10894. +
  10895. + aufs_bindex_t ninfo;
  10896. + struct au_drinfo **drinfo;
  10897. +};
  10898. +
  10899. +static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
  10900. + struct au_branch *br)
  10901. +{
  10902. + int err, wkq_err, whnamelen, e;
  10903. + char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
  10904. + = AUFS_WH_DR_INFO_PFX;
  10905. + struct au_drinfo *drinfo;
  10906. + struct qstr oldname;
  10907. + struct inode *h_dir, *delegated;
  10908. + struct dentry *info_dentry;
  10909. + struct path infopath;
  10910. +
  10911. + whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
  10912. + whnamelen += au_drinfo_name(br, whname + whnamelen,
  10913. + sizeof(whname) - whnamelen);
  10914. + if (w->no_sio)
  10915. + drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
  10916. + &info_dentry);
  10917. + else {
  10918. + struct au_drinfo_do_load_args args = {
  10919. + .drinfop = &drinfo,
  10920. + .h_ppath = &w->h_ppath,
  10921. + .whname = whname,
  10922. + .whnamelen = whnamelen,
  10923. + .info_dentry = &info_dentry
  10924. + };
  10925. + wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
  10926. + if (unlikely(wkq_err))
  10927. + drinfo = ERR_PTR(wkq_err);
  10928. + }
  10929. + err = PTR_ERR(drinfo);
  10930. + if (IS_ERR_OR_NULL(drinfo))
  10931. + goto out;
  10932. +
  10933. + err = 0;
  10934. + oldname.len = drinfo->oldnamelen;
  10935. + oldname.name = drinfo->oldname;
  10936. + if (au_qstreq(w->qname, &oldname)) {
  10937. + /* the name is renamed back */
  10938. + kfree(drinfo);
  10939. + drinfo = NULL;
  10940. +
  10941. + infopath.dentry = info_dentry;
  10942. + infopath.mnt = w->h_ppath.mnt;
  10943. + h_dir = d_inode(w->h_ppath.dentry);
  10944. + delegated = NULL;
  10945. + inode_lock_nested(h_dir, AuLsc_I_PARENT);
  10946. + e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
  10947. + inode_unlock(h_dir);
  10948. + if (unlikely(e))
  10949. + AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
  10950. + if (unlikely(e == -EWOULDBLOCK))
  10951. + iput(delegated);
  10952. + }
  10953. + kfree(w->drinfo[bindex]);
  10954. + w->drinfo[bindex] = drinfo;
  10955. + dput(info_dentry);
  10956. +
  10957. +out:
  10958. + AuTraceErr(err);
  10959. + return err;
  10960. +}
  10961. +
  10962. +/* ---------------------------------------------------------------------- */
  10963. +
  10964. +static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
  10965. +{
  10966. + struct au_drinfo **p = drinfo;
  10967. +
  10968. + while (n-- > 0)
  10969. + kfree(*drinfo++);
  10970. + kfree(p);
  10971. +}
  10972. +
  10973. +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
  10974. + aufs_bindex_t btgt)
  10975. +{
  10976. + int err, ninfo;
  10977. + struct au_drinfo_load w;
  10978. + aufs_bindex_t bindex, bbot;
  10979. + struct au_branch *br;
  10980. + struct inode *h_dir;
  10981. + struct au_dr_hino *ent;
  10982. + struct super_block *sb;
  10983. +
  10984. + AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
  10985. + AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
  10986. + AuLNPair(&lkup->whname), btgt);
  10987. +
  10988. + sb = dentry->d_sb;
  10989. + bbot = au_sbbot(sb);
  10990. + w.ninfo = bbot + 1;
  10991. + if (!lkup->dirren.drinfo) {
  10992. + lkup->dirren.drinfo = kcalloc(w.ninfo,
  10993. + sizeof(*lkup->dirren.drinfo),
  10994. + GFP_NOFS);
  10995. + if (unlikely(!lkup->dirren.drinfo)) {
  10996. + err = -ENOMEM;
  10997. + goto out;
  10998. + }
  10999. + lkup->dirren.ninfo = w.ninfo;
  11000. + }
  11001. + w.drinfo = lkup->dirren.drinfo;
  11002. + w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
  11003. + w.h_ppath.dentry = au_h_dptr(dentry, btgt);
  11004. + AuDebugOn(!w.h_ppath.dentry);
  11005. + w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
  11006. + w.qname = &dentry->d_name;
  11007. +
  11008. + ninfo = 0;
  11009. + for (bindex = btgt + 1; bindex <= bbot; bindex++) {
  11010. + br = au_sbr(sb, bindex);
  11011. + err = au_drinfo_load(&w, bindex, br);
  11012. + if (unlikely(err))
  11013. + goto out_free;
  11014. + if (w.drinfo[bindex])
  11015. + ninfo++;
  11016. + }
  11017. + if (!ninfo) {
  11018. + br = au_sbr(sb, btgt);
  11019. + h_dir = d_inode(w.h_ppath.dentry);
  11020. + ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
  11021. + AuDebugOn(!ent);
  11022. + au_dr_hino_del(&br->br_dirren, ent);
  11023. + kfree(ent);
  11024. + }
  11025. + goto out; /* success */
  11026. +
  11027. +out_free:
  11028. + au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
  11029. + lkup->dirren.ninfo = 0;
  11030. + lkup->dirren.drinfo = NULL;
  11031. +out:
  11032. + AuTraceErr(err);
  11033. + return err;
  11034. +}
  11035. +
  11036. +void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
  11037. +{
  11038. + au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
  11039. +}
  11040. +
  11041. +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
  11042. +{
  11043. + int err;
  11044. + struct au_drinfo *drinfo;
  11045. +
  11046. + err = 0;
  11047. + if (!lkup->dirren.drinfo)
  11048. + goto out;
  11049. + AuDebugOn(lkup->dirren.ninfo < btgt + 1);
  11050. + drinfo = lkup->dirren.drinfo[btgt + 1];
  11051. + if (!drinfo)
  11052. + goto out;
  11053. +
  11054. + kfree(lkup->whname.name);
  11055. + lkup->whname.name = NULL;
  11056. + lkup->dirren.dr_name.len = drinfo->oldnamelen;
  11057. + lkup->dirren.dr_name.name = drinfo->oldname;
  11058. + lkup->name = &lkup->dirren.dr_name;
  11059. + err = au_wh_name_alloc(&lkup->whname, lkup->name);
  11060. + if (!err)
  11061. + AuDbg("name %.*s, whname %.*s, b%d\n",
  11062. + AuLNPair(lkup->name), AuLNPair(&lkup->whname),
  11063. + btgt);
  11064. +
  11065. +out:
  11066. + AuTraceErr(err);
  11067. + return err;
  11068. +}
  11069. +
  11070. +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
  11071. + ino_t h_ino)
  11072. +{
  11073. + int match;
  11074. + struct au_drinfo *drinfo;
  11075. +
  11076. + match = 1;
  11077. + if (!lkup->dirren.drinfo)
  11078. + goto out;
  11079. + AuDebugOn(lkup->dirren.ninfo < bindex + 1);
  11080. + drinfo = lkup->dirren.drinfo[bindex + 1];
  11081. + if (!drinfo)
  11082. + goto out;
  11083. +
  11084. + match = (drinfo->ino == h_ino);
  11085. + AuDbg("match %d\n", match);
  11086. +
  11087. +out:
  11088. + return match;
  11089. +}
  11090. +
  11091. +/* ---------------------------------------------------------------------- */
  11092. +
  11093. +int au_dr_opt_set(struct super_block *sb)
  11094. +{
  11095. + int err;
  11096. + aufs_bindex_t bindex, bbot;
  11097. + struct au_branch *br;
  11098. +
  11099. + err = 0;
  11100. + bbot = au_sbbot(sb);
  11101. + for (bindex = 0; !err && bindex <= bbot; bindex++) {
  11102. + br = au_sbr(sb, bindex);
  11103. + err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
  11104. + }
  11105. +
  11106. + return err;
  11107. +}
  11108. +
  11109. +int au_dr_opt_flush(struct super_block *sb)
  11110. +{
  11111. + int err;
  11112. + aufs_bindex_t bindex, bbot;
  11113. + struct au_branch *br;
  11114. +
  11115. + err = 0;
  11116. + bbot = au_sbbot(sb);
  11117. + for (bindex = 0; !err && bindex <= bbot; bindex++) {
  11118. + br = au_sbr(sb, bindex);
  11119. + if (au_br_writable(br->br_perm))
  11120. + err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
  11121. + }
  11122. +
  11123. + return err;
  11124. +}
  11125. +
  11126. +int au_dr_opt_clr(struct super_block *sb, int no_flush)
  11127. +{
  11128. + int err;
  11129. + aufs_bindex_t bindex, bbot;
  11130. + struct au_branch *br;
  11131. +
  11132. + err = 0;
  11133. + if (!no_flush) {
  11134. + err = au_dr_opt_flush(sb);
  11135. + if (unlikely(err))
  11136. + goto out;
  11137. + }
  11138. +
  11139. + bbot = au_sbbot(sb);
  11140. + for (bindex = 0; bindex <= bbot; bindex++) {
  11141. + br = au_sbr(sb, bindex);
  11142. + au_dr_hino_free(&br->br_dirren);
  11143. + }
  11144. +
  11145. +out:
  11146. + return err;
  11147. +}
  11148. --- /dev/null
  11149. +++ linux-4.19/fs/aufs/dirren.h 2018-11-20 09:35:15.018195504 +0200
  11150. @@ -0,0 +1,140 @@
  11151. +/* SPDX-License-Identifier: GPL-2.0 */
  11152. +/*
  11153. + * Copyright (C) 2017-2018 Junjiro R. Okajima
  11154. + *
  11155. + * This program, aufs is free software; you can redistribute it and/or modify
  11156. + * it under the terms of the GNU General Public License as published by
  11157. + * the Free Software Foundation; either version 2 of the License, or
  11158. + * (at your option) any later version.
  11159. + *
  11160. + * This program is distributed in the hope that it will be useful,
  11161. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11162. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11163. + * GNU General Public License for more details.
  11164. + *
  11165. + * You should have received a copy of the GNU General Public License
  11166. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  11167. + */
  11168. +
  11169. +/*
  11170. + * renamed dir info
  11171. + */
  11172. +
  11173. +#ifndef __AUFS_DIRREN_H__
  11174. +#define __AUFS_DIRREN_H__
  11175. +
  11176. +#ifdef __KERNEL__
  11177. +
  11178. +#include <linux/dcache.h>
  11179. +#include <linux/statfs.h>
  11180. +#include <linux/uuid.h>
  11181. +#include "hbl.h"
  11182. +
  11183. +#define AuDirren_NHASH 100
  11184. +
  11185. +#ifdef CONFIG_AUFS_DIRREN
  11186. +enum au_brid_type {
  11187. + AuBrid_Unset,
  11188. + AuBrid_UUID,
  11189. + AuBrid_FSID,
  11190. + AuBrid_DEV
  11191. +};
  11192. +
  11193. +struct au_dr_brid {
  11194. + enum au_brid_type type;
  11195. + union {
  11196. + uuid_t uuid; /* unimplemented yet */
  11197. + fsid_t fsid;
  11198. + dev_t dev;
  11199. + };
  11200. +};
  11201. +
  11202. +/* 20 is the max digits length of ulong 64 */
  11203. +/* brid-type "_" uuid "_" inum */
  11204. +#define AUFS_DIRREN_FNAME_SZ (1 + 1 + UUID_STRING_LEN + 20)
  11205. +#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
  11206. +
  11207. +struct au_dr_hino {
  11208. + struct hlist_bl_node dr_hnode;
  11209. + ino_t dr_h_ino;
  11210. +};
  11211. +
  11212. +struct au_dr_br {
  11213. + struct hlist_bl_head dr_h_ino[AuDirren_NHASH];
  11214. + struct au_dr_brid dr_brid;
  11215. +};
  11216. +
  11217. +struct au_dr_lookup {
  11218. + /* dr_name is pointed by struct au_do_lookup_args.name */
  11219. + struct qstr dr_name; /* subset of dr_info */
  11220. + aufs_bindex_t ninfo;
  11221. + struct au_drinfo **drinfo;
  11222. +};
  11223. +#else
  11224. +struct au_dr_hino;
  11225. +/* empty */
  11226. +struct au_dr_br { };
  11227. +struct au_dr_lookup { };
  11228. +#endif
  11229. +
  11230. +/* ---------------------------------------------------------------------- */
  11231. +
  11232. +struct au_branch;
  11233. +struct au_do_lookup_args;
  11234. +struct au_hinode;
  11235. +#ifdef CONFIG_AUFS_DIRREN
  11236. +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
  11237. + struct au_dr_hino *add_ent);
  11238. +void au_dr_hino_free(struct au_dr_br *dr);
  11239. +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
  11240. + const struct path *path);
  11241. +int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
  11242. +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
  11243. + struct qstr *dst_name, void *_rev);
  11244. +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
  11245. +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
  11246. +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
  11247. + aufs_bindex_t bindex);
  11248. +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
  11249. +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
  11250. + ino_t h_ino);
  11251. +void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
  11252. +int au_dr_opt_set(struct super_block *sb);
  11253. +int au_dr_opt_flush(struct super_block *sb);
  11254. +int au_dr_opt_clr(struct super_block *sb, int no_flush);
  11255. +#else
  11256. +AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
  11257. + struct au_dr_hino *add_ent);
  11258. +AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
  11259. +AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
  11260. + const struct path *path);
  11261. +AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
  11262. +AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
  11263. + struct qstr *dst_name, void *_rev);
  11264. +AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
  11265. +AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
  11266. + void *rev);
  11267. +AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
  11268. + aufs_bindex_t bindex);
  11269. +AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
  11270. +AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
  11271. + aufs_bindex_t bindex, ino_t h_ino);
  11272. +AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
  11273. +AuStubInt0(au_dr_opt_set, struct super_block *sb);
  11274. +AuStubInt0(au_dr_opt_flush, struct super_block *sb);
  11275. +AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
  11276. +#endif
  11277. +
  11278. +/* ---------------------------------------------------------------------- */
  11279. +
  11280. +#ifdef CONFIG_AUFS_DIRREN
  11281. +static inline int au_dr_ihash(ino_t h_ino)
  11282. +{
  11283. + return h_ino % AuDirren_NHASH;
  11284. +}
  11285. +#else
  11286. +AuStubInt0(au_dr_ihash, ino_t h_ino);
  11287. +#endif
  11288. +
  11289. +#endif /* __KERNEL__ */
  11290. +#endif /* __AUFS_DIRREN_H__ */
  11291. --- /dev/null
  11292. +++ linux-4.19/fs/aufs/dynop.c 2018-11-20 09:35:15.018195504 +0200
  11293. @@ -0,0 +1,370 @@
  11294. +// SPDX-License-Identifier: GPL-2.0
  11295. +/*
  11296. + * Copyright (C) 2010-2018 Junjiro R. Okajima
  11297. + *
  11298. + * This program, aufs is free software; you can redistribute it and/or modify
  11299. + * it under the terms of the GNU General Public License as published by
  11300. + * the Free Software Foundation; either version 2 of the License, or
  11301. + * (at your option) any later version.
  11302. + *
  11303. + * This program is distributed in the hope that it will be useful,
  11304. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11305. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11306. + * GNU General Public License for more details.
  11307. + *
  11308. + * You should have received a copy of the GNU General Public License
  11309. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  11310. + */
  11311. +
  11312. +/*
  11313. + * dynamically customizable operations for regular files
  11314. + */
  11315. +
  11316. +#include "aufs.h"
  11317. +
  11318. +#define DyPrSym(key) AuDbgSym(key->dk_op.dy_hop)
  11319. +
  11320. +/*
  11321. + * How large will these lists be?
  11322. + * Usually just a few elements, 20-30 at most for each, I guess.
  11323. + */
  11324. +static struct hlist_bl_head dynop[AuDyLast];
  11325. +
  11326. +static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
  11327. + const void *h_op)
  11328. +{
  11329. + struct au_dykey *key, *tmp;
  11330. + struct hlist_bl_node *pos;
  11331. +
  11332. + key = NULL;
  11333. + hlist_bl_lock(hbl);
  11334. + hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
  11335. + if (tmp->dk_op.dy_hop == h_op) {
  11336. + key = tmp;
  11337. + kref_get(&key->dk_kref);
  11338. + break;
  11339. + }
  11340. + hlist_bl_unlock(hbl);
  11341. +
  11342. + return key;
  11343. +}
  11344. +
  11345. +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
  11346. +{
  11347. + struct au_dykey **k, *found;
  11348. + const void *h_op = key->dk_op.dy_hop;
  11349. + int i;
  11350. +
  11351. + found = NULL;
  11352. + k = br->br_dykey;
  11353. + for (i = 0; i < AuBrDynOp; i++)
  11354. + if (k[i]) {
  11355. + if (k[i]->dk_op.dy_hop == h_op) {
  11356. + found = k[i];
  11357. + break;
  11358. + }
  11359. + } else
  11360. + break;
  11361. + if (!found) {
  11362. + spin_lock(&br->br_dykey_lock);
  11363. + for (; i < AuBrDynOp; i++)
  11364. + if (k[i]) {
  11365. + if (k[i]->dk_op.dy_hop == h_op) {
  11366. + found = k[i];
  11367. + break;
  11368. + }
  11369. + } else {
  11370. + k[i] = key;
  11371. + break;
  11372. + }
  11373. + spin_unlock(&br->br_dykey_lock);
  11374. + BUG_ON(i == AuBrDynOp); /* expand the array */
  11375. + }
  11376. +
  11377. + return found;
  11378. +}
  11379. +
  11380. +/* kref_get() if @key is already added */
  11381. +static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
  11382. +{
  11383. + struct au_dykey *tmp, *found;
  11384. + struct hlist_bl_node *pos;
  11385. + const void *h_op = key->dk_op.dy_hop;
  11386. +
  11387. + found = NULL;
  11388. + hlist_bl_lock(hbl);
  11389. + hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
  11390. + if (tmp->dk_op.dy_hop == h_op) {
  11391. + kref_get(&tmp->dk_kref);
  11392. + found = tmp;
  11393. + break;
  11394. + }
  11395. + if (!found)
  11396. + hlist_bl_add_head(&key->dk_hnode, hbl);
  11397. + hlist_bl_unlock(hbl);
  11398. +
  11399. + if (!found)
  11400. + DyPrSym(key);
  11401. + return found;
  11402. +}
  11403. +
  11404. +static void dy_free_rcu(struct rcu_head *rcu)
  11405. +{
  11406. + struct au_dykey *key;
  11407. +
  11408. + key = container_of(rcu, struct au_dykey, dk_rcu);
  11409. + DyPrSym(key);
  11410. + kfree(key);
  11411. +}
  11412. +
  11413. +static void dy_free(struct kref *kref)
  11414. +{
  11415. + struct au_dykey *key;
  11416. + struct hlist_bl_head *hbl;
  11417. +
  11418. + key = container_of(kref, struct au_dykey, dk_kref);
  11419. + hbl = dynop + key->dk_op.dy_type;
  11420. + au_hbl_del(&key->dk_hnode, hbl);
  11421. + call_rcu(&key->dk_rcu, dy_free_rcu);
  11422. +}
  11423. +
  11424. +void au_dy_put(struct au_dykey *key)
  11425. +{
  11426. + kref_put(&key->dk_kref, dy_free);
  11427. +}
  11428. +
  11429. +/* ---------------------------------------------------------------------- */
  11430. +
  11431. +#define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
  11432. +
  11433. +#ifdef CONFIG_AUFS_DEBUG
  11434. +#define DyDbgDeclare(cnt) unsigned int cnt = 0
  11435. +#define DyDbgInc(cnt) do { cnt++; } while (0)
  11436. +#else
  11437. +#define DyDbgDeclare(cnt) do {} while (0)
  11438. +#define DyDbgInc(cnt) do {} while (0)
  11439. +#endif
  11440. +
  11441. +#define DySet(func, dst, src, h_op, h_sb) do { \
  11442. + DyDbgInc(cnt); \
  11443. + if (h_op->func) { \
  11444. + if (src.func) \
  11445. + dst.func = src.func; \
  11446. + else \
  11447. + AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
  11448. + } \
  11449. +} while (0)
  11450. +
  11451. +#define DySetForce(func, dst, src) do { \
  11452. + AuDebugOn(!src.func); \
  11453. + DyDbgInc(cnt); \
  11454. + dst.func = src.func; \
  11455. +} while (0)
  11456. +
  11457. +#define DySetAop(func) \
  11458. + DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
  11459. +#define DySetAopForce(func) \
  11460. + DySetForce(func, dyaop->da_op, aufs_aop)
  11461. +
  11462. +static void dy_aop(struct au_dykey *key, const void *h_op,
  11463. + struct super_block *h_sb __maybe_unused)
  11464. +{
  11465. + struct au_dyaop *dyaop = (void *)key;
  11466. + const struct address_space_operations *h_aop = h_op;
  11467. + DyDbgDeclare(cnt);
  11468. +
  11469. + AuDbg("%s\n", au_sbtype(h_sb));
  11470. +
  11471. + DySetAop(writepage);
  11472. + DySetAopForce(readpage); /* force */
  11473. + DySetAop(writepages);
  11474. + DySetAop(set_page_dirty);
  11475. + DySetAop(readpages);
  11476. + DySetAop(write_begin);
  11477. + DySetAop(write_end);
  11478. + DySetAop(bmap);
  11479. + DySetAop(invalidatepage);
  11480. + DySetAop(releasepage);
  11481. + DySetAop(freepage);
  11482. + /* this one will be changed according to an aufs mount option */
  11483. + DySetAop(direct_IO);
  11484. + DySetAop(migratepage);
  11485. + DySetAop(isolate_page);
  11486. + DySetAop(putback_page);
  11487. + DySetAop(launder_page);
  11488. + DySetAop(is_partially_uptodate);
  11489. + DySetAop(is_dirty_writeback);
  11490. + DySetAop(error_remove_page);
  11491. + DySetAop(swap_activate);
  11492. + DySetAop(swap_deactivate);
  11493. +
  11494. + DyDbgSize(cnt, *h_aop);
  11495. +}
  11496. +
  11497. +/* ---------------------------------------------------------------------- */
  11498. +
  11499. +static void dy_bug(struct kref *kref)
  11500. +{
  11501. + BUG();
  11502. +}
  11503. +
  11504. +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
  11505. +{
  11506. + struct au_dykey *key, *old;
  11507. + struct hlist_bl_head *hbl;
  11508. + struct op {
  11509. + unsigned int sz;
  11510. + void (*set)(struct au_dykey *key, const void *h_op,
  11511. + struct super_block *h_sb __maybe_unused);
  11512. + };
  11513. + static const struct op a[] = {
  11514. + [AuDy_AOP] = {
  11515. + .sz = sizeof(struct au_dyaop),
  11516. + .set = dy_aop
  11517. + }
  11518. + };
  11519. + const struct op *p;
  11520. +
  11521. + hbl = dynop + op->dy_type;
  11522. + key = dy_gfind_get(hbl, op->dy_hop);
  11523. + if (key)
  11524. + goto out_add; /* success */
  11525. +
  11526. + p = a + op->dy_type;
  11527. + key = kzalloc(p->sz, GFP_NOFS);
  11528. + if (unlikely(!key)) {
  11529. + key = ERR_PTR(-ENOMEM);
  11530. + goto out;
  11531. + }
  11532. +
  11533. + key->dk_op.dy_hop = op->dy_hop;
  11534. + kref_init(&key->dk_kref);
  11535. + p->set(key, op->dy_hop, au_br_sb(br));
  11536. + old = dy_gadd(hbl, key);
  11537. + if (old) {
  11538. + kfree(key);
  11539. + key = old;
  11540. + }
  11541. +
  11542. +out_add:
  11543. + old = dy_bradd(br, key);
  11544. + if (old)
  11545. + /* its ref-count should never be zero here */
  11546. + kref_put(&key->dk_kref, dy_bug);
  11547. +out:
  11548. + return key;
  11549. +}
  11550. +
  11551. +/* ---------------------------------------------------------------------- */
  11552. +/*
  11553. + * Aufs prohibits O_DIRECT by default even if the branch supports it.
  11554. + * This behaviour is necessary to return an error from open(O_DIRECT) instead
  11555. + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
  11556. + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
  11557. + * See the aufs manual in detail.
  11558. + */
  11559. +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
  11560. +{
  11561. + if (!do_dx)
  11562. + dyaop->da_op.direct_IO = NULL;
  11563. + else
  11564. + dyaop->da_op.direct_IO = aufs_aop.direct_IO;
  11565. +}
  11566. +
  11567. +static struct au_dyaop *dy_aget(struct au_branch *br,
  11568. + const struct address_space_operations *h_aop,
  11569. + int do_dx)
  11570. +{
  11571. + struct au_dyaop *dyaop;
  11572. + struct au_dynop op;
  11573. +
  11574. + op.dy_type = AuDy_AOP;
  11575. + op.dy_haop = h_aop;
  11576. + dyaop = (void *)dy_get(&op, br);
  11577. + if (IS_ERR(dyaop))
  11578. + goto out;
  11579. + dy_adx(dyaop, do_dx);
  11580. +
  11581. +out:
  11582. + return dyaop;
  11583. +}
  11584. +
  11585. +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
  11586. + struct inode *h_inode)
  11587. +{
  11588. + int err, do_dx;
  11589. + struct super_block *sb;
  11590. + struct au_branch *br;
  11591. + struct au_dyaop *dyaop;
  11592. +
  11593. + AuDebugOn(!S_ISREG(h_inode->i_mode));
  11594. + IiMustWriteLock(inode);
  11595. +
  11596. + sb = inode->i_sb;
  11597. + br = au_sbr(sb, bindex);
  11598. + do_dx = !!au_opt_test(au_mntflags(sb), DIO);
  11599. + dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
  11600. + err = PTR_ERR(dyaop);
  11601. + if (IS_ERR(dyaop))
  11602. + /* unnecessary to call dy_fput() */
  11603. + goto out;
  11604. +
  11605. + err = 0;
  11606. + inode->i_mapping->a_ops = &dyaop->da_op;
  11607. +
  11608. +out:
  11609. + return err;
  11610. +}
  11611. +
  11612. +/*
  11613. + * Is it safe to replace a_ops during the inode/file is in operation?
  11614. + * Yes, I hope so.
  11615. + */
  11616. +int au_dy_irefresh(struct inode *inode)
  11617. +{
  11618. + int err;
  11619. + aufs_bindex_t btop;
  11620. + struct inode *h_inode;
  11621. +
  11622. + err = 0;
  11623. + if (S_ISREG(inode->i_mode)) {
  11624. + btop = au_ibtop(inode);
  11625. + h_inode = au_h_iptr(inode, btop);
  11626. + err = au_dy_iaop(inode, btop, h_inode);
  11627. + }
  11628. + return err;
  11629. +}
  11630. +
  11631. +void au_dy_arefresh(int do_dx)
  11632. +{
  11633. + struct hlist_bl_head *hbl;
  11634. + struct hlist_bl_node *pos;
  11635. + struct au_dykey *key;
  11636. +
  11637. + hbl = dynop + AuDy_AOP;
  11638. + hlist_bl_lock(hbl);
  11639. + hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
  11640. + dy_adx((void *)key, do_dx);
  11641. + hlist_bl_unlock(hbl);
  11642. +}
  11643. +
  11644. +/* ---------------------------------------------------------------------- */
  11645. +
  11646. +void __init au_dy_init(void)
  11647. +{
  11648. + int i;
  11649. +
  11650. + /* make sure that 'struct au_dykey *' can be any type */
  11651. + BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
  11652. +
  11653. + for (i = 0; i < AuDyLast; i++)
  11654. + INIT_HLIST_BL_HEAD(dynop + i);
  11655. +}
  11656. +
  11657. +void au_dy_fin(void)
  11658. +{
  11659. + int i;
  11660. +
  11661. + for (i = 0; i < AuDyLast; i++)
  11662. + WARN_ON(!hlist_bl_empty(dynop + i));
  11663. +}
  11664. --- /dev/null
  11665. +++ linux-4.19/fs/aufs/dynop.h 2018-11-20 09:35:15.018195504 +0200
  11666. @@ -0,0 +1,75 @@
  11667. +/* SPDX-License-Identifier: GPL-2.0 */
  11668. +/*
  11669. + * Copyright (C) 2010-2018 Junjiro R. Okajima
  11670. + *
  11671. + * This program, aufs is free software; you can redistribute it and/or modify
  11672. + * it under the terms of the GNU General Public License as published by
  11673. + * the Free Software Foundation; either version 2 of the License, or
  11674. + * (at your option) any later version.
  11675. + *
  11676. + * This program is distributed in the hope that it will be useful,
  11677. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11678. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11679. + * GNU General Public License for more details.
  11680. + *
  11681. + * You should have received a copy of the GNU General Public License
  11682. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  11683. + */
  11684. +
  11685. +/*
  11686. + * dynamically customizable operations (for regular files only)
  11687. + */
  11688. +
  11689. +#ifndef __AUFS_DYNOP_H__
  11690. +#define __AUFS_DYNOP_H__
  11691. +
  11692. +#ifdef __KERNEL__
  11693. +
  11694. +#include <linux/fs.h>
  11695. +#include <linux/kref.h>
  11696. +
  11697. +enum {AuDy_AOP, AuDyLast};
  11698. +
  11699. +struct au_dynop {
  11700. + int dy_type;
  11701. + union {
  11702. + const void *dy_hop;
  11703. + const struct address_space_operations *dy_haop;
  11704. + };
  11705. +};
  11706. +
  11707. +struct au_dykey {
  11708. + union {
  11709. + struct hlist_bl_node dk_hnode;
  11710. + struct rcu_head dk_rcu;
  11711. + };
  11712. + struct au_dynop dk_op;
  11713. +
  11714. + /*
  11715. + * during I am in the branch local array, kref is gotten. when the
  11716. + * branch is removed, kref is put.
  11717. + */
  11718. + struct kref dk_kref;
  11719. +};
  11720. +
  11721. +/* stop unioning since their sizes are very different from each other */
  11722. +struct au_dyaop {
  11723. + struct au_dykey da_key;
  11724. + struct address_space_operations da_op; /* not const */
  11725. +};
  11726. +
  11727. +/* ---------------------------------------------------------------------- */
  11728. +
  11729. +/* dynop.c */
  11730. +struct au_branch;
  11731. +void au_dy_put(struct au_dykey *key);
  11732. +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
  11733. + struct inode *h_inode);
  11734. +int au_dy_irefresh(struct inode *inode);
  11735. +void au_dy_arefresh(int do_dio);
  11736. +
  11737. +void __init au_dy_init(void);
  11738. +void au_dy_fin(void);
  11739. +
  11740. +#endif /* __KERNEL__ */
  11741. +#endif /* __AUFS_DYNOP_H__ */
  11742. --- /dev/null
  11743. +++ linux-4.19/fs/aufs/export.c 2018-11-20 09:35:15.019195504 +0200
  11744. @@ -0,0 +1,838 @@
  11745. +// SPDX-License-Identifier: GPL-2.0
  11746. +/*
  11747. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  11748. + *
  11749. + * This program, aufs is free software; you can redistribute it and/or modify
  11750. + * it under the terms of the GNU General Public License as published by
  11751. + * the Free Software Foundation; either version 2 of the License, or
  11752. + * (at your option) any later version.
  11753. + *
  11754. + * This program is distributed in the hope that it will be useful,
  11755. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11756. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11757. + * GNU General Public License for more details.
  11758. + *
  11759. + * You should have received a copy of the GNU General Public License
  11760. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  11761. + */
  11762. +
  11763. +/*
  11764. + * export via nfs
  11765. + */
  11766. +
  11767. +#include <linux/exportfs.h>
  11768. +#include <linux/fs_struct.h>
  11769. +#include <linux/namei.h>
  11770. +#include <linux/nsproxy.h>
  11771. +#include <linux/random.h>
  11772. +#include <linux/writeback.h>
  11773. +#include "aufs.h"
  11774. +
  11775. +union conv {
  11776. +#ifdef CONFIG_AUFS_INO_T_64
  11777. + __u32 a[2];
  11778. +#else
  11779. + __u32 a[1];
  11780. +#endif
  11781. + ino_t ino;
  11782. +};
  11783. +
  11784. +static ino_t decode_ino(__u32 *a)
  11785. +{
  11786. + union conv u;
  11787. +
  11788. + BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
  11789. + u.a[0] = a[0];
  11790. +#ifdef CONFIG_AUFS_INO_T_64
  11791. + u.a[1] = a[1];
  11792. +#endif
  11793. + return u.ino;
  11794. +}
  11795. +
  11796. +static void encode_ino(__u32 *a, ino_t ino)
  11797. +{
  11798. + union conv u;
  11799. +
  11800. + u.ino = ino;
  11801. + a[0] = u.a[0];
  11802. +#ifdef CONFIG_AUFS_INO_T_64
  11803. + a[1] = u.a[1];
  11804. +#endif
  11805. +}
  11806. +
  11807. +/* NFS file handle */
  11808. +enum {
  11809. + Fh_br_id,
  11810. + Fh_sigen,
  11811. +#ifdef CONFIG_AUFS_INO_T_64
  11812. + /* support 64bit inode number */
  11813. + Fh_ino1,
  11814. + Fh_ino2,
  11815. + Fh_dir_ino1,
  11816. + Fh_dir_ino2,
  11817. +#else
  11818. + Fh_ino1,
  11819. + Fh_dir_ino1,
  11820. +#endif
  11821. + Fh_igen,
  11822. + Fh_h_type,
  11823. + Fh_tail,
  11824. +
  11825. + Fh_ino = Fh_ino1,
  11826. + Fh_dir_ino = Fh_dir_ino1
  11827. +};
  11828. +
  11829. +static int au_test_anon(struct dentry *dentry)
  11830. +{
  11831. + /* note: read d_flags without d_lock */
  11832. + return !!(dentry->d_flags & DCACHE_DISCONNECTED);
  11833. +}
  11834. +
  11835. +int au_test_nfsd(void)
  11836. +{
  11837. + int ret;
  11838. + struct task_struct *tsk = current;
  11839. + char comm[sizeof(tsk->comm)];
  11840. +
  11841. + ret = 0;
  11842. + if (tsk->flags & PF_KTHREAD) {
  11843. + get_task_comm(comm, tsk);
  11844. + ret = !strcmp(comm, "nfsd");
  11845. + }
  11846. +
  11847. + return ret;
  11848. +}
  11849. +
  11850. +/* ---------------------------------------------------------------------- */
  11851. +/* inode generation external table */
  11852. +
  11853. +void au_xigen_inc(struct inode *inode)
  11854. +{
  11855. + loff_t pos;
  11856. + ssize_t sz;
  11857. + __u32 igen;
  11858. + struct super_block *sb;
  11859. + struct au_sbinfo *sbinfo;
  11860. +
  11861. + sb = inode->i_sb;
  11862. + AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
  11863. +
  11864. + sbinfo = au_sbi(sb);
  11865. + pos = inode->i_ino;
  11866. + pos *= sizeof(igen);
  11867. + igen = inode->i_generation + 1;
  11868. + sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
  11869. + sizeof(igen), &pos);
  11870. + if (sz == sizeof(igen))
  11871. + return; /* success */
  11872. +
  11873. + if (unlikely(sz >= 0))
  11874. + AuIOErr("xigen error (%zd)\n", sz);
  11875. +}
  11876. +
  11877. +int au_xigen_new(struct inode *inode)
  11878. +{
  11879. + int err;
  11880. + loff_t pos;
  11881. + ssize_t sz;
  11882. + struct super_block *sb;
  11883. + struct au_sbinfo *sbinfo;
  11884. + struct file *file;
  11885. +
  11886. + err = 0;
  11887. + /* todo: dirty, at mount time */
  11888. + if (inode->i_ino == AUFS_ROOT_INO)
  11889. + goto out;
  11890. + sb = inode->i_sb;
  11891. + SiMustAnyLock(sb);
  11892. + if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
  11893. + goto out;
  11894. +
  11895. + err = -EFBIG;
  11896. + pos = inode->i_ino;
  11897. + if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
  11898. + AuIOErr1("too large i%lld\n", pos);
  11899. + goto out;
  11900. + }
  11901. + pos *= sizeof(inode->i_generation);
  11902. +
  11903. + err = 0;
  11904. + sbinfo = au_sbi(sb);
  11905. + file = sbinfo->si_xigen;
  11906. + BUG_ON(!file);
  11907. +
  11908. + if (vfsub_f_size_read(file)
  11909. + < pos + sizeof(inode->i_generation)) {
  11910. + inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
  11911. + sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
  11912. + sizeof(inode->i_generation), &pos);
  11913. + } else
  11914. + sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
  11915. + sizeof(inode->i_generation), &pos);
  11916. + if (sz == sizeof(inode->i_generation))
  11917. + goto out; /* success */
  11918. +
  11919. + err = sz;
  11920. + if (unlikely(sz >= 0)) {
  11921. + err = -EIO;
  11922. + AuIOErr("xigen error (%zd)\n", sz);
  11923. + }
  11924. +
  11925. +out:
  11926. + return err;
  11927. +}
  11928. +
  11929. +int au_xigen_set(struct super_block *sb, struct path *path)
  11930. +{
  11931. + int err;
  11932. + struct au_sbinfo *sbinfo;
  11933. + struct file *file;
  11934. +
  11935. + SiMustWriteLock(sb);
  11936. +
  11937. + sbinfo = au_sbi(sb);
  11938. + file = au_xino_create2(sb, path, sbinfo->si_xigen);
  11939. + err = PTR_ERR(file);
  11940. + if (IS_ERR(file))
  11941. + goto out;
  11942. + err = 0;
  11943. + if (sbinfo->si_xigen)
  11944. + fput(sbinfo->si_xigen);
  11945. + sbinfo->si_xigen = file;
  11946. +
  11947. +out:
  11948. + AuTraceErr(err);
  11949. + return err;
  11950. +}
  11951. +
  11952. +void au_xigen_clr(struct super_block *sb)
  11953. +{
  11954. + struct au_sbinfo *sbinfo;
  11955. +
  11956. + SiMustWriteLock(sb);
  11957. +
  11958. + sbinfo = au_sbi(sb);
  11959. + if (sbinfo->si_xigen) {
  11960. + fput(sbinfo->si_xigen);
  11961. + sbinfo->si_xigen = NULL;
  11962. + }
  11963. +}
  11964. +
  11965. +/* ---------------------------------------------------------------------- */
  11966. +
  11967. +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
  11968. + ino_t dir_ino)
  11969. +{
  11970. + struct dentry *dentry, *d;
  11971. + struct inode *inode;
  11972. + unsigned int sigen;
  11973. +
  11974. + dentry = NULL;
  11975. + inode = ilookup(sb, ino);
  11976. + if (!inode)
  11977. + goto out;
  11978. +
  11979. + dentry = ERR_PTR(-ESTALE);
  11980. + sigen = au_sigen(sb);
  11981. + if (unlikely(au_is_bad_inode(inode)
  11982. + || IS_DEADDIR(inode)
  11983. + || sigen != au_iigen(inode, NULL)))
  11984. + goto out_iput;
  11985. +
  11986. + dentry = NULL;
  11987. + if (!dir_ino || S_ISDIR(inode->i_mode))
  11988. + dentry = d_find_alias(inode);
  11989. + else {
  11990. + spin_lock(&inode->i_lock);
  11991. + hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
  11992. + spin_lock(&d->d_lock);
  11993. + if (!au_test_anon(d)
  11994. + && d_inode(d->d_parent)->i_ino == dir_ino) {
  11995. + dentry = dget_dlock(d);
  11996. + spin_unlock(&d->d_lock);
  11997. + break;
  11998. + }
  11999. + spin_unlock(&d->d_lock);
  12000. + }
  12001. + spin_unlock(&inode->i_lock);
  12002. + }
  12003. + if (unlikely(dentry && au_digen_test(dentry, sigen))) {
  12004. + /* need to refresh */
  12005. + dput(dentry);
  12006. + dentry = NULL;
  12007. + }
  12008. +
  12009. +out_iput:
  12010. + iput(inode);
  12011. +out:
  12012. + AuTraceErrPtr(dentry);
  12013. + return dentry;
  12014. +}
  12015. +
  12016. +/* ---------------------------------------------------------------------- */
  12017. +
  12018. +/* todo: dirty? */
  12019. +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
  12020. +
  12021. +struct au_compare_mnt_args {
  12022. + /* input */
  12023. + struct super_block *sb;
  12024. +
  12025. + /* output */
  12026. + struct vfsmount *mnt;
  12027. +};
  12028. +
  12029. +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
  12030. +{
  12031. + struct au_compare_mnt_args *a = arg;
  12032. +
  12033. + if (mnt->mnt_sb != a->sb)
  12034. + return 0;
  12035. + a->mnt = mntget(mnt);
  12036. + return 1;
  12037. +}
  12038. +
  12039. +static struct vfsmount *au_mnt_get(struct super_block *sb)
  12040. +{
  12041. + int err;
  12042. + struct path root;
  12043. + struct au_compare_mnt_args args = {
  12044. + .sb = sb
  12045. + };
  12046. +
  12047. + get_fs_root(current->fs, &root);
  12048. + rcu_read_lock();
  12049. + err = iterate_mounts(au_compare_mnt, &args, root.mnt);
  12050. + rcu_read_unlock();
  12051. + path_put(&root);
  12052. + AuDebugOn(!err);
  12053. + AuDebugOn(!args.mnt);
  12054. + return args.mnt;
  12055. +}
  12056. +
  12057. +struct au_nfsd_si_lock {
  12058. + unsigned int sigen;
  12059. + aufs_bindex_t bindex, br_id;
  12060. + unsigned char force_lock;
  12061. +};
  12062. +
  12063. +static int si_nfsd_read_lock(struct super_block *sb,
  12064. + struct au_nfsd_si_lock *nsi_lock)
  12065. +{
  12066. + int err;
  12067. + aufs_bindex_t bindex;
  12068. +
  12069. + si_read_lock(sb, AuLock_FLUSH);
  12070. +
  12071. + /* branch id may be wrapped around */
  12072. + err = 0;
  12073. + bindex = au_br_index(sb, nsi_lock->br_id);
  12074. + if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
  12075. + goto out; /* success */
  12076. +
  12077. + err = -ESTALE;
  12078. + bindex = -1;
  12079. + if (!nsi_lock->force_lock)
  12080. + si_read_unlock(sb);
  12081. +
  12082. +out:
  12083. + nsi_lock->bindex = bindex;
  12084. + return err;
  12085. +}
  12086. +
  12087. +struct find_name_by_ino {
  12088. + struct dir_context ctx;
  12089. + int called, found;
  12090. + ino_t ino;
  12091. + char *name;
  12092. + int namelen;
  12093. +};
  12094. +
  12095. +static int
  12096. +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
  12097. + loff_t offset, u64 ino, unsigned int d_type)
  12098. +{
  12099. + struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
  12100. + ctx);
  12101. +
  12102. + a->called++;
  12103. + if (a->ino != ino)
  12104. + return 0;
  12105. +
  12106. + memcpy(a->name, name, namelen);
  12107. + a->namelen = namelen;
  12108. + a->found = 1;
  12109. + return 1;
  12110. +}
  12111. +
  12112. +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
  12113. + struct au_nfsd_si_lock *nsi_lock)
  12114. +{
  12115. + struct dentry *dentry, *parent;
  12116. + struct file *file;
  12117. + struct inode *dir;
  12118. + struct find_name_by_ino arg = {
  12119. + .ctx = {
  12120. + .actor = find_name_by_ino
  12121. + }
  12122. + };
  12123. + int err;
  12124. +
  12125. + parent = path->dentry;
  12126. + if (nsi_lock)
  12127. + si_read_unlock(parent->d_sb);
  12128. + file = vfsub_dentry_open(path, au_dir_roflags);
  12129. + dentry = (void *)file;
  12130. + if (IS_ERR(file))
  12131. + goto out;
  12132. +
  12133. + dentry = ERR_PTR(-ENOMEM);
  12134. + arg.name = (void *)__get_free_page(GFP_NOFS);
  12135. + if (unlikely(!arg.name))
  12136. + goto out_file;
  12137. + arg.ino = ino;
  12138. + arg.found = 0;
  12139. + do {
  12140. + arg.called = 0;
  12141. + /* smp_mb(); */
  12142. + err = vfsub_iterate_dir(file, &arg.ctx);
  12143. + } while (!err && !arg.found && arg.called);
  12144. + dentry = ERR_PTR(err);
  12145. + if (unlikely(err))
  12146. + goto out_name;
  12147. + /* instead of ENOENT */
  12148. + dentry = ERR_PTR(-ESTALE);
  12149. + if (!arg.found)
  12150. + goto out_name;
  12151. +
  12152. + /* do not call vfsub_lkup_one() */
  12153. + dir = d_inode(parent);
  12154. + dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
  12155. + AuTraceErrPtr(dentry);
  12156. + if (IS_ERR(dentry))
  12157. + goto out_name;
  12158. + AuDebugOn(au_test_anon(dentry));
  12159. + if (unlikely(d_really_is_negative(dentry))) {
  12160. + dput(dentry);
  12161. + dentry = ERR_PTR(-ENOENT);
  12162. + }
  12163. +
  12164. +out_name:
  12165. + free_page((unsigned long)arg.name);
  12166. +out_file:
  12167. + fput(file);
  12168. +out:
  12169. + if (unlikely(nsi_lock
  12170. + && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
  12171. + if (!IS_ERR(dentry)) {
  12172. + dput(dentry);
  12173. + dentry = ERR_PTR(-ESTALE);
  12174. + }
  12175. + AuTraceErrPtr(dentry);
  12176. + return dentry;
  12177. +}
  12178. +
  12179. +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
  12180. + ino_t dir_ino,
  12181. + struct au_nfsd_si_lock *nsi_lock)
  12182. +{
  12183. + struct dentry *dentry;
  12184. + struct path path;
  12185. +
  12186. + if (dir_ino != AUFS_ROOT_INO) {
  12187. + path.dentry = decode_by_ino(sb, dir_ino, 0);
  12188. + dentry = path.dentry;
  12189. + if (!path.dentry || IS_ERR(path.dentry))
  12190. + goto out;
  12191. + AuDebugOn(au_test_anon(path.dentry));
  12192. + } else
  12193. + path.dentry = dget(sb->s_root);
  12194. +
  12195. + path.mnt = au_mnt_get(sb);
  12196. + dentry = au_lkup_by_ino(&path, ino, nsi_lock);
  12197. + path_put(&path);
  12198. +
  12199. +out:
  12200. + AuTraceErrPtr(dentry);
  12201. + return dentry;
  12202. +}
  12203. +
  12204. +/* ---------------------------------------------------------------------- */
  12205. +
  12206. +static int h_acceptable(void *expv, struct dentry *dentry)
  12207. +{
  12208. + return 1;
  12209. +}
  12210. +
  12211. +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
  12212. + char *buf, int len, struct super_block *sb)
  12213. +{
  12214. + char *p;
  12215. + int n;
  12216. + struct path path;
  12217. +
  12218. + p = d_path(h_rootpath, buf, len);
  12219. + if (IS_ERR(p))
  12220. + goto out;
  12221. + n = strlen(p);
  12222. +
  12223. + path.mnt = h_rootpath->mnt;
  12224. + path.dentry = h_parent;
  12225. + p = d_path(&path, buf, len);
  12226. + if (IS_ERR(p))
  12227. + goto out;
  12228. + if (n != 1)
  12229. + p += n;
  12230. +
  12231. + path.mnt = au_mnt_get(sb);
  12232. + path.dentry = sb->s_root;
  12233. + p = d_path(&path, buf, len - strlen(p));
  12234. + mntput(path.mnt);
  12235. + if (IS_ERR(p))
  12236. + goto out;
  12237. + if (n != 1)
  12238. + p[strlen(p)] = '/';
  12239. +
  12240. +out:
  12241. + AuTraceErrPtr(p);
  12242. + return p;
  12243. +}
  12244. +
  12245. +static
  12246. +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
  12247. + int fh_len, struct au_nfsd_si_lock *nsi_lock)
  12248. +{
  12249. + struct dentry *dentry, *h_parent, *root;
  12250. + struct super_block *h_sb;
  12251. + char *pathname, *p;
  12252. + struct vfsmount *h_mnt;
  12253. + struct au_branch *br;
  12254. + int err;
  12255. + struct path path;
  12256. +
  12257. + br = au_sbr(sb, nsi_lock->bindex);
  12258. + h_mnt = au_br_mnt(br);
  12259. + h_sb = h_mnt->mnt_sb;
  12260. + /* todo: call lower fh_to_dentry()? fh_to_parent()? */
  12261. + lockdep_off();
  12262. + h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
  12263. + fh_len - Fh_tail, fh[Fh_h_type],
  12264. + h_acceptable, /*context*/NULL);
  12265. + lockdep_on();
  12266. + dentry = h_parent;
  12267. + if (unlikely(!h_parent || IS_ERR(h_parent))) {
  12268. + AuWarn1("%s decode_fh failed, %ld\n",
  12269. + au_sbtype(h_sb), PTR_ERR(h_parent));
  12270. + goto out;
  12271. + }
  12272. + dentry = NULL;
  12273. + if (unlikely(au_test_anon(h_parent))) {
  12274. + AuWarn1("%s decode_fh returned a disconnected dentry\n",
  12275. + au_sbtype(h_sb));
  12276. + goto out_h_parent;
  12277. + }
  12278. +
  12279. + dentry = ERR_PTR(-ENOMEM);
  12280. + pathname = (void *)__get_free_page(GFP_NOFS);
  12281. + if (unlikely(!pathname))
  12282. + goto out_h_parent;
  12283. +
  12284. + root = sb->s_root;
  12285. + path.mnt = h_mnt;
  12286. + di_read_lock_parent(root, !AuLock_IR);
  12287. + path.dentry = au_h_dptr(root, nsi_lock->bindex);
  12288. + di_read_unlock(root, !AuLock_IR);
  12289. + p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
  12290. + dentry = (void *)p;
  12291. + if (IS_ERR(p))
  12292. + goto out_pathname;
  12293. +
  12294. + si_read_unlock(sb);
  12295. + err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
  12296. + dentry = ERR_PTR(err);
  12297. + if (unlikely(err))
  12298. + goto out_relock;
  12299. +
  12300. + dentry = ERR_PTR(-ENOENT);
  12301. + AuDebugOn(au_test_anon(path.dentry));
  12302. + if (unlikely(d_really_is_negative(path.dentry)))
  12303. + goto out_path;
  12304. +
  12305. + if (ino != d_inode(path.dentry)->i_ino)
  12306. + dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
  12307. + else
  12308. + dentry = dget(path.dentry);
  12309. +
  12310. +out_path:
  12311. + path_put(&path);
  12312. +out_relock:
  12313. + if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
  12314. + if (!IS_ERR(dentry)) {
  12315. + dput(dentry);
  12316. + dentry = ERR_PTR(-ESTALE);
  12317. + }
  12318. +out_pathname:
  12319. + free_page((unsigned long)pathname);
  12320. +out_h_parent:
  12321. + dput(h_parent);
  12322. +out:
  12323. + AuTraceErrPtr(dentry);
  12324. + return dentry;
  12325. +}
  12326. +
  12327. +/* ---------------------------------------------------------------------- */
  12328. +
  12329. +static struct dentry *
  12330. +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
  12331. + int fh_type)
  12332. +{
  12333. + struct dentry *dentry;
  12334. + __u32 *fh = fid->raw;
  12335. + struct au_branch *br;
  12336. + ino_t ino, dir_ino;
  12337. + struct au_nfsd_si_lock nsi_lock = {
  12338. + .force_lock = 0
  12339. + };
  12340. +
  12341. + dentry = ERR_PTR(-ESTALE);
  12342. + /* it should never happen, but the file handle is unreliable */
  12343. + if (unlikely(fh_len < Fh_tail))
  12344. + goto out;
  12345. + nsi_lock.sigen = fh[Fh_sigen];
  12346. + nsi_lock.br_id = fh[Fh_br_id];
  12347. +
  12348. + /* branch id may be wrapped around */
  12349. + br = NULL;
  12350. + if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
  12351. + goto out;
  12352. + nsi_lock.force_lock = 1;
  12353. +
  12354. + /* is this inode still cached? */
  12355. + ino = decode_ino(fh + Fh_ino);
  12356. + /* it should never happen */
  12357. + if (unlikely(ino == AUFS_ROOT_INO))
  12358. + goto out_unlock;
  12359. +
  12360. + dir_ino = decode_ino(fh + Fh_dir_ino);
  12361. + dentry = decode_by_ino(sb, ino, dir_ino);
  12362. + if (IS_ERR(dentry))
  12363. + goto out_unlock;
  12364. + if (dentry)
  12365. + goto accept;
  12366. +
  12367. + /* is the parent dir cached? */
  12368. + br = au_sbr(sb, nsi_lock.bindex);
  12369. + au_lcnt_inc(&br->br_nfiles);
  12370. + dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
  12371. + if (IS_ERR(dentry))
  12372. + goto out_unlock;
  12373. + if (dentry)
  12374. + goto accept;
  12375. +
  12376. + /* lookup path */
  12377. + dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
  12378. + if (IS_ERR(dentry))
  12379. + goto out_unlock;
  12380. + if (unlikely(!dentry))
  12381. + /* todo?: make it ESTALE */
  12382. + goto out_unlock;
  12383. +
  12384. +accept:
  12385. + if (!au_digen_test(dentry, au_sigen(sb))
  12386. + && d_inode(dentry)->i_generation == fh[Fh_igen])
  12387. + goto out_unlock; /* success */
  12388. +
  12389. + dput(dentry);
  12390. + dentry = ERR_PTR(-ESTALE);
  12391. +out_unlock:
  12392. + if (br)
  12393. + au_lcnt_dec(&br->br_nfiles);
  12394. + si_read_unlock(sb);
  12395. +out:
  12396. + AuTraceErrPtr(dentry);
  12397. + return dentry;
  12398. +}
  12399. +
  12400. +#if 0 /* reserved for future use */
  12401. +/* support subtreecheck option */
  12402. +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
  12403. + int fh_len, int fh_type)
  12404. +{
  12405. + struct dentry *parent;
  12406. + __u32 *fh = fid->raw;
  12407. + ino_t dir_ino;
  12408. +
  12409. + dir_ino = decode_ino(fh + Fh_dir_ino);
  12410. + parent = decode_by_ino(sb, dir_ino, 0);
  12411. + if (IS_ERR(parent))
  12412. + goto out;
  12413. + if (!parent)
  12414. + parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
  12415. + dir_ino, fh, fh_len);
  12416. +
  12417. +out:
  12418. + AuTraceErrPtr(parent);
  12419. + return parent;
  12420. +}
  12421. +#endif
  12422. +
  12423. +/* ---------------------------------------------------------------------- */
  12424. +
  12425. +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
  12426. + struct inode *dir)
  12427. +{
  12428. + int err;
  12429. + aufs_bindex_t bindex;
  12430. + struct super_block *sb, *h_sb;
  12431. + struct dentry *dentry, *parent, *h_parent;
  12432. + struct inode *h_dir;
  12433. + struct au_branch *br;
  12434. +
  12435. + err = -ENOSPC;
  12436. + if (unlikely(*max_len <= Fh_tail)) {
  12437. + AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
  12438. + goto out;
  12439. + }
  12440. +
  12441. + err = FILEID_ROOT;
  12442. + if (inode->i_ino == AUFS_ROOT_INO) {
  12443. + AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
  12444. + goto out;
  12445. + }
  12446. +
  12447. + h_parent = NULL;
  12448. + sb = inode->i_sb;
  12449. + err = si_read_lock(sb, AuLock_FLUSH);
  12450. + if (unlikely(err))
  12451. + goto out;
  12452. +
  12453. +#ifdef CONFIG_AUFS_DEBUG
  12454. + if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
  12455. + AuWarn1("NFS-exporting requires xino\n");
  12456. +#endif
  12457. + err = -EIO;
  12458. + parent = NULL;
  12459. + ii_read_lock_child(inode);
  12460. + bindex = au_ibtop(inode);
  12461. + if (!dir) {
  12462. + dentry = d_find_any_alias(inode);
  12463. + if (unlikely(!dentry))
  12464. + goto out_unlock;
  12465. + AuDebugOn(au_test_anon(dentry));
  12466. + parent = dget_parent(dentry);
  12467. + dput(dentry);
  12468. + if (unlikely(!parent))
  12469. + goto out_unlock;
  12470. + if (d_really_is_positive(parent))
  12471. + dir = d_inode(parent);
  12472. + }
  12473. +
  12474. + ii_read_lock_parent(dir);
  12475. + h_dir = au_h_iptr(dir, bindex);
  12476. + ii_read_unlock(dir);
  12477. + if (unlikely(!h_dir))
  12478. + goto out_parent;
  12479. + h_parent = d_find_any_alias(h_dir);
  12480. + if (unlikely(!h_parent))
  12481. + goto out_hparent;
  12482. +
  12483. + err = -EPERM;
  12484. + br = au_sbr(sb, bindex);
  12485. + h_sb = au_br_sb(br);
  12486. + if (unlikely(!h_sb->s_export_op)) {
  12487. + AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
  12488. + goto out_hparent;
  12489. + }
  12490. +
  12491. + fh[Fh_br_id] = br->br_id;
  12492. + fh[Fh_sigen] = au_sigen(sb);
  12493. + encode_ino(fh + Fh_ino, inode->i_ino);
  12494. + encode_ino(fh + Fh_dir_ino, dir->i_ino);
  12495. + fh[Fh_igen] = inode->i_generation;
  12496. +
  12497. + *max_len -= Fh_tail;
  12498. + fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
  12499. + max_len,
  12500. + /*connectable or subtreecheck*/0);
  12501. + err = fh[Fh_h_type];
  12502. + *max_len += Fh_tail;
  12503. + /* todo: macros? */
  12504. + if (err != FILEID_INVALID)
  12505. + err = 99;
  12506. + else
  12507. + AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
  12508. +
  12509. +out_hparent:
  12510. + dput(h_parent);
  12511. +out_parent:
  12512. + dput(parent);
  12513. +out_unlock:
  12514. + ii_read_unlock(inode);
  12515. + si_read_unlock(sb);
  12516. +out:
  12517. + if (unlikely(err < 0))
  12518. + err = FILEID_INVALID;
  12519. + return err;
  12520. +}
  12521. +
  12522. +/* ---------------------------------------------------------------------- */
  12523. +
  12524. +static int aufs_commit_metadata(struct inode *inode)
  12525. +{
  12526. + int err;
  12527. + aufs_bindex_t bindex;
  12528. + struct super_block *sb;
  12529. + struct inode *h_inode;
  12530. + int (*f)(struct inode *inode);
  12531. +
  12532. + sb = inode->i_sb;
  12533. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  12534. + ii_write_lock_child(inode);
  12535. + bindex = au_ibtop(inode);
  12536. + AuDebugOn(bindex < 0);
  12537. + h_inode = au_h_iptr(inode, bindex);
  12538. +
  12539. + f = h_inode->i_sb->s_export_op->commit_metadata;
  12540. + if (f)
  12541. + err = f(h_inode);
  12542. + else {
  12543. + struct writeback_control wbc = {
  12544. + .sync_mode = WB_SYNC_ALL,
  12545. + .nr_to_write = 0 /* metadata only */
  12546. + };
  12547. +
  12548. + err = sync_inode(h_inode, &wbc);
  12549. + }
  12550. +
  12551. + au_cpup_attr_timesizes(inode);
  12552. + ii_write_unlock(inode);
  12553. + si_read_unlock(sb);
  12554. + return err;
  12555. +}
  12556. +
  12557. +/* ---------------------------------------------------------------------- */
  12558. +
  12559. +static struct export_operations aufs_export_op = {
  12560. + .fh_to_dentry = aufs_fh_to_dentry,
  12561. + /* .fh_to_parent = aufs_fh_to_parent, */
  12562. + .encode_fh = aufs_encode_fh,
  12563. + .commit_metadata = aufs_commit_metadata
  12564. +};
  12565. +
  12566. +void au_export_init(struct super_block *sb)
  12567. +{
  12568. + struct au_sbinfo *sbinfo;
  12569. + __u32 u;
  12570. +
  12571. + BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
  12572. + && IS_MODULE(CONFIG_EXPORTFS),
  12573. + AUFS_NAME ": unsupported configuration "
  12574. + "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
  12575. +
  12576. + sb->s_export_op = &aufs_export_op;
  12577. + sbinfo = au_sbi(sb);
  12578. + sbinfo->si_xigen = NULL;
  12579. + get_random_bytes(&u, sizeof(u));
  12580. + BUILD_BUG_ON(sizeof(u) != sizeof(int));
  12581. + atomic_set(&sbinfo->si_xigen_next, u);
  12582. +}
  12583. --- /dev/null
  12584. +++ linux-4.19/fs/aufs/fhsm.c 2018-11-20 09:35:15.020195504 +0200
  12585. @@ -0,0 +1,427 @@
  12586. +// SPDX-License-Identifier: GPL-2.0
  12587. +/*
  12588. + * Copyright (C) 2011-2018 Junjiro R. Okajima
  12589. + *
  12590. + * This program, aufs is free software; you can redistribute it and/or modify
  12591. + * it under the terms of the GNU General Public License as published by
  12592. + * the Free Software Foundation; either version 2 of the License, or
  12593. + * (at your option) any later version.
  12594. + *
  12595. + * This program is distributed in the hope that it will be useful,
  12596. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12597. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12598. + * GNU General Public License for more details.
  12599. + *
  12600. + * You should have received a copy of the GNU General Public License
  12601. + * along with this program; if not, write to the Free Software
  12602. + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  12603. + */
  12604. +
  12605. +/*
  12606. + * File-based Hierarchy Storage Management
  12607. + */
  12608. +
  12609. +#include <linux/anon_inodes.h>
  12610. +#include <linux/poll.h>
  12611. +#include <linux/seq_file.h>
  12612. +#include <linux/statfs.h>
  12613. +#include "aufs.h"
  12614. +
  12615. +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
  12616. +{
  12617. + struct au_sbinfo *sbinfo;
  12618. + struct au_fhsm *fhsm;
  12619. +
  12620. + SiMustAnyLock(sb);
  12621. +
  12622. + sbinfo = au_sbi(sb);
  12623. + fhsm = &sbinfo->si_fhsm;
  12624. + AuDebugOn(!fhsm);
  12625. + return fhsm->fhsm_bottom;
  12626. +}
  12627. +
  12628. +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
  12629. +{
  12630. + struct au_sbinfo *sbinfo;
  12631. + struct au_fhsm *fhsm;
  12632. +
  12633. + SiMustWriteLock(sb);
  12634. +
  12635. + sbinfo = au_sbi(sb);
  12636. + fhsm = &sbinfo->si_fhsm;
  12637. + AuDebugOn(!fhsm);
  12638. + fhsm->fhsm_bottom = bindex;
  12639. +}
  12640. +
  12641. +/* ---------------------------------------------------------------------- */
  12642. +
  12643. +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
  12644. +{
  12645. + struct au_br_fhsm *bf;
  12646. +
  12647. + bf = br->br_fhsm;
  12648. + MtxMustLock(&bf->bf_lock);
  12649. +
  12650. + return !bf->bf_readable
  12651. + || time_after(jiffies,
  12652. + bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
  12653. +}
  12654. +
  12655. +/* ---------------------------------------------------------------------- */
  12656. +
  12657. +static void au_fhsm_notify(struct super_block *sb, int val)
  12658. +{
  12659. + struct au_sbinfo *sbinfo;
  12660. + struct au_fhsm *fhsm;
  12661. +
  12662. + SiMustAnyLock(sb);
  12663. +
  12664. + sbinfo = au_sbi(sb);
  12665. + fhsm = &sbinfo->si_fhsm;
  12666. + if (au_fhsm_pid(fhsm)
  12667. + && atomic_read(&fhsm->fhsm_readable) != -1) {
  12668. + atomic_set(&fhsm->fhsm_readable, val);
  12669. + if (val)
  12670. + wake_up(&fhsm->fhsm_wqh);
  12671. + }
  12672. +}
  12673. +
  12674. +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
  12675. + struct aufs_stfs *rstfs, int do_lock, int do_notify)
  12676. +{
  12677. + int err;
  12678. + struct au_branch *br;
  12679. + struct au_br_fhsm *bf;
  12680. +
  12681. + br = au_sbr(sb, bindex);
  12682. + AuDebugOn(au_br_rdonly(br));
  12683. + bf = br->br_fhsm;
  12684. + AuDebugOn(!bf);
  12685. +
  12686. + if (do_lock)
  12687. + mutex_lock(&bf->bf_lock);
  12688. + else
  12689. + MtxMustLock(&bf->bf_lock);
  12690. +
  12691. + /* sb->s_root for NFS is unreliable */
  12692. + err = au_br_stfs(br, &bf->bf_stfs);
  12693. + if (unlikely(err)) {
  12694. + AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
  12695. + goto out;
  12696. + }
  12697. +
  12698. + bf->bf_jiffy = jiffies;
  12699. + bf->bf_readable = 1;
  12700. + if (do_notify)
  12701. + au_fhsm_notify(sb, /*val*/1);
  12702. + if (rstfs)
  12703. + *rstfs = bf->bf_stfs;
  12704. +
  12705. +out:
  12706. + if (do_lock)
  12707. + mutex_unlock(&bf->bf_lock);
  12708. + au_fhsm_notify(sb, /*val*/1);
  12709. +
  12710. + return err;
  12711. +}
  12712. +
  12713. +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
  12714. +{
  12715. + int err;
  12716. + struct au_sbinfo *sbinfo;
  12717. + struct au_fhsm *fhsm;
  12718. + struct au_branch *br;
  12719. + struct au_br_fhsm *bf;
  12720. +
  12721. + AuDbg("b%d, force %d\n", bindex, force);
  12722. + SiMustAnyLock(sb);
  12723. +
  12724. + sbinfo = au_sbi(sb);
  12725. + fhsm = &sbinfo->si_fhsm;
  12726. + if (!au_ftest_si(sbinfo, FHSM)
  12727. + || fhsm->fhsm_bottom == bindex)
  12728. + return;
  12729. +
  12730. + br = au_sbr(sb, bindex);
  12731. + bf = br->br_fhsm;
  12732. + AuDebugOn(!bf);
  12733. + mutex_lock(&bf->bf_lock);
  12734. + if (force
  12735. + || au_fhsm_pid(fhsm)
  12736. + || au_fhsm_test_jiffy(sbinfo, br))
  12737. + err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
  12738. + /*do_notify*/1);
  12739. + mutex_unlock(&bf->bf_lock);
  12740. +}
  12741. +
  12742. +void au_fhsm_wrote_all(struct super_block *sb, int force)
  12743. +{
  12744. + aufs_bindex_t bindex, bbot;
  12745. + struct au_branch *br;
  12746. +
  12747. + /* exclude the bottom */
  12748. + bbot = au_fhsm_bottom(sb);
  12749. + for (bindex = 0; bindex < bbot; bindex++) {
  12750. + br = au_sbr(sb, bindex);
  12751. + if (au_br_fhsm(br->br_perm))
  12752. + au_fhsm_wrote(sb, bindex, force);
  12753. + }
  12754. +}
  12755. +
  12756. +/* ---------------------------------------------------------------------- */
  12757. +
  12758. +static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
  12759. +{
  12760. + __poll_t mask;
  12761. + struct au_sbinfo *sbinfo;
  12762. + struct au_fhsm *fhsm;
  12763. +
  12764. + mask = 0;
  12765. + sbinfo = file->private_data;
  12766. + fhsm = &sbinfo->si_fhsm;
  12767. + poll_wait(file, &fhsm->fhsm_wqh, wait);
  12768. + if (atomic_read(&fhsm->fhsm_readable))
  12769. + mask = EPOLLIN /* | EPOLLRDNORM */;
  12770. +
  12771. + if (!mask)
  12772. + AuDbg("mask 0x%x\n", mask);
  12773. + return mask;
  12774. +}
  12775. +
  12776. +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
  12777. + struct aufs_stfs *stfs, __s16 brid)
  12778. +{
  12779. + int err;
  12780. +
  12781. + err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
  12782. + if (!err)
  12783. + err = __put_user(brid, &stbr->brid);
  12784. + if (unlikely(err))
  12785. + err = -EFAULT;
  12786. +
  12787. + return err;
  12788. +}
  12789. +
  12790. +static ssize_t au_fhsm_do_read(struct super_block *sb,
  12791. + struct aufs_stbr __user *stbr, size_t count)
  12792. +{
  12793. + ssize_t err;
  12794. + int nstbr;
  12795. + aufs_bindex_t bindex, bbot;
  12796. + struct au_branch *br;
  12797. + struct au_br_fhsm *bf;
  12798. +
  12799. + /* except the bottom branch */
  12800. + err = 0;
  12801. + nstbr = 0;
  12802. + bbot = au_fhsm_bottom(sb);
  12803. + for (bindex = 0; !err && bindex < bbot; bindex++) {
  12804. + br = au_sbr(sb, bindex);
  12805. + if (!au_br_fhsm(br->br_perm))
  12806. + continue;
  12807. +
  12808. + bf = br->br_fhsm;
  12809. + mutex_lock(&bf->bf_lock);
  12810. + if (bf->bf_readable) {
  12811. + err = -EFAULT;
  12812. + if (count >= sizeof(*stbr))
  12813. + err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
  12814. + br->br_id);
  12815. + if (!err) {
  12816. + bf->bf_readable = 0;
  12817. + count -= sizeof(*stbr);
  12818. + nstbr++;
  12819. + }
  12820. + }
  12821. + mutex_unlock(&bf->bf_lock);
  12822. + }
  12823. + if (!err)
  12824. + err = sizeof(*stbr) * nstbr;
  12825. +
  12826. + return err;
  12827. +}
  12828. +
  12829. +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
  12830. + loff_t *pos)
  12831. +{
  12832. + ssize_t err;
  12833. + int readable;
  12834. + aufs_bindex_t nfhsm, bindex, bbot;
  12835. + struct au_sbinfo *sbinfo;
  12836. + struct au_fhsm *fhsm;
  12837. + struct au_branch *br;
  12838. + struct super_block *sb;
  12839. +
  12840. + err = 0;
  12841. + sbinfo = file->private_data;
  12842. + fhsm = &sbinfo->si_fhsm;
  12843. +need_data:
  12844. + spin_lock_irq(&fhsm->fhsm_wqh.lock);
  12845. + if (!atomic_read(&fhsm->fhsm_readable)) {
  12846. + if (vfsub_file_flags(file) & O_NONBLOCK)
  12847. + err = -EAGAIN;
  12848. + else
  12849. + err = wait_event_interruptible_locked_irq
  12850. + (fhsm->fhsm_wqh,
  12851. + atomic_read(&fhsm->fhsm_readable));
  12852. + }
  12853. + spin_unlock_irq(&fhsm->fhsm_wqh.lock);
  12854. + if (unlikely(err))
  12855. + goto out;
  12856. +
  12857. + /* sb may already be dead */
  12858. + au_rw_read_lock(&sbinfo->si_rwsem);
  12859. + readable = atomic_read(&fhsm->fhsm_readable);
  12860. + if (readable > 0) {
  12861. + sb = sbinfo->si_sb;
  12862. + AuDebugOn(!sb);
  12863. + /* exclude the bottom branch */
  12864. + nfhsm = 0;
  12865. + bbot = au_fhsm_bottom(sb);
  12866. + for (bindex = 0; bindex < bbot; bindex++) {
  12867. + br = au_sbr(sb, bindex);
  12868. + if (au_br_fhsm(br->br_perm))
  12869. + nfhsm++;
  12870. + }
  12871. + err = -EMSGSIZE;
  12872. + if (nfhsm * sizeof(struct aufs_stbr) <= count) {
  12873. + atomic_set(&fhsm->fhsm_readable, 0);
  12874. + err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
  12875. + count);
  12876. + }
  12877. + }
  12878. + au_rw_read_unlock(&sbinfo->si_rwsem);
  12879. + if (!readable)
  12880. + goto need_data;
  12881. +
  12882. +out:
  12883. + return err;
  12884. +}
  12885. +
  12886. +static int au_fhsm_release(struct inode *inode, struct file *file)
  12887. +{
  12888. + struct au_sbinfo *sbinfo;
  12889. + struct au_fhsm *fhsm;
  12890. +
  12891. + /* sb may already be dead */
  12892. + sbinfo = file->private_data;
  12893. + fhsm = &sbinfo->si_fhsm;
  12894. + spin_lock(&fhsm->fhsm_spin);
  12895. + fhsm->fhsm_pid = 0;
  12896. + spin_unlock(&fhsm->fhsm_spin);
  12897. + kobject_put(&sbinfo->si_kobj);
  12898. +
  12899. + return 0;
  12900. +}
  12901. +
  12902. +static const struct file_operations au_fhsm_fops = {
  12903. + .owner = THIS_MODULE,
  12904. + .llseek = noop_llseek,
  12905. + .read = au_fhsm_read,
  12906. + .poll = au_fhsm_poll,
  12907. + .release = au_fhsm_release
  12908. +};
  12909. +
  12910. +int au_fhsm_fd(struct super_block *sb, int oflags)
  12911. +{
  12912. + int err, fd;
  12913. + struct au_sbinfo *sbinfo;
  12914. + struct au_fhsm *fhsm;
  12915. +
  12916. + err = -EPERM;
  12917. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  12918. + goto out;
  12919. +
  12920. + err = -EINVAL;
  12921. + if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
  12922. + goto out;
  12923. +
  12924. + err = 0;
  12925. + sbinfo = au_sbi(sb);
  12926. + fhsm = &sbinfo->si_fhsm;
  12927. + spin_lock(&fhsm->fhsm_spin);
  12928. + if (!fhsm->fhsm_pid)
  12929. + fhsm->fhsm_pid = current->pid;
  12930. + else
  12931. + err = -EBUSY;
  12932. + spin_unlock(&fhsm->fhsm_spin);
  12933. + if (unlikely(err))
  12934. + goto out;
  12935. +
  12936. + oflags |= O_RDONLY;
  12937. + /* oflags |= FMODE_NONOTIFY; */
  12938. + fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
  12939. + err = fd;
  12940. + if (unlikely(fd < 0))
  12941. + goto out_pid;
  12942. +
  12943. + /* succeed regardless 'fhsm' status */
  12944. + kobject_get(&sbinfo->si_kobj);
  12945. + si_noflush_read_lock(sb);
  12946. + if (au_ftest_si(sbinfo, FHSM))
  12947. + au_fhsm_wrote_all(sb, /*force*/0);
  12948. + si_read_unlock(sb);
  12949. + goto out; /* success */
  12950. +
  12951. +out_pid:
  12952. + spin_lock(&fhsm->fhsm_spin);
  12953. + fhsm->fhsm_pid = 0;
  12954. + spin_unlock(&fhsm->fhsm_spin);
  12955. +out:
  12956. + AuTraceErr(err);
  12957. + return err;
  12958. +}
  12959. +
  12960. +/* ---------------------------------------------------------------------- */
  12961. +
  12962. +int au_fhsm_br_alloc(struct au_branch *br)
  12963. +{
  12964. + int err;
  12965. +
  12966. + err = 0;
  12967. + br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
  12968. + if (br->br_fhsm)
  12969. + au_br_fhsm_init(br->br_fhsm);
  12970. + else
  12971. + err = -ENOMEM;
  12972. +
  12973. + return err;
  12974. +}
  12975. +
  12976. +/* ---------------------------------------------------------------------- */
  12977. +
  12978. +void au_fhsm_fin(struct super_block *sb)
  12979. +{
  12980. + au_fhsm_notify(sb, /*val*/-1);
  12981. +}
  12982. +
  12983. +void au_fhsm_init(struct au_sbinfo *sbinfo)
  12984. +{
  12985. + struct au_fhsm *fhsm;
  12986. +
  12987. + fhsm = &sbinfo->si_fhsm;
  12988. + spin_lock_init(&fhsm->fhsm_spin);
  12989. + init_waitqueue_head(&fhsm->fhsm_wqh);
  12990. + atomic_set(&fhsm->fhsm_readable, 0);
  12991. + fhsm->fhsm_expire
  12992. + = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
  12993. + fhsm->fhsm_bottom = -1;
  12994. +}
  12995. +
  12996. +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
  12997. +{
  12998. + sbinfo->si_fhsm.fhsm_expire
  12999. + = msecs_to_jiffies(sec * MSEC_PER_SEC);
  13000. +}
  13001. +
  13002. +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
  13003. +{
  13004. + unsigned int u;
  13005. +
  13006. + if (!au_ftest_si(sbinfo, FHSM))
  13007. + return;
  13008. +
  13009. + u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
  13010. + if (u != AUFS_FHSM_CACHE_DEF_SEC)
  13011. + seq_printf(seq, ",fhsm_sec=%u", u);
  13012. +}
  13013. --- /dev/null
  13014. +++ linux-4.19/fs/aufs/file.c 2018-11-20 09:35:15.021195504 +0200
  13015. @@ -0,0 +1,863 @@
  13016. +// SPDX-License-Identifier: GPL-2.0
  13017. +/*
  13018. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  13019. + *
  13020. + * This program, aufs is free software; you can redistribute it and/or modify
  13021. + * it under the terms of the GNU General Public License as published by
  13022. + * the Free Software Foundation; either version 2 of the License, or
  13023. + * (at your option) any later version.
  13024. + *
  13025. + * This program is distributed in the hope that it will be useful,
  13026. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13027. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13028. + * GNU General Public License for more details.
  13029. + *
  13030. + * You should have received a copy of the GNU General Public License
  13031. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  13032. + */
  13033. +
  13034. +/*
  13035. + * handling file/dir, and address_space operation
  13036. + */
  13037. +
  13038. +#ifdef CONFIG_AUFS_DEBUG
  13039. +#include <linux/migrate.h>
  13040. +#endif
  13041. +#include <linux/pagemap.h>
  13042. +#include "aufs.h"
  13043. +
  13044. +/* drop flags for writing */
  13045. +unsigned int au_file_roflags(unsigned int flags)
  13046. +{
  13047. + flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
  13048. + flags |= O_RDONLY | O_NOATIME;
  13049. + return flags;
  13050. +}
  13051. +
  13052. +/* common functions to regular file and dir */
  13053. +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
  13054. + struct file *file, int force_wr)
  13055. +{
  13056. + struct file *h_file;
  13057. + struct dentry *h_dentry;
  13058. + struct inode *h_inode;
  13059. + struct super_block *sb;
  13060. + struct au_branch *br;
  13061. + struct path h_path;
  13062. + int err;
  13063. +
  13064. + /* a race condition can happen between open and unlink/rmdir */
  13065. + h_file = ERR_PTR(-ENOENT);
  13066. + h_dentry = au_h_dptr(dentry, bindex);
  13067. + if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
  13068. + goto out;
  13069. + h_inode = d_inode(h_dentry);
  13070. + spin_lock(&h_dentry->d_lock);
  13071. + err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
  13072. + /* || !d_inode(dentry)->i_nlink */
  13073. + ;
  13074. + spin_unlock(&h_dentry->d_lock);
  13075. + if (unlikely(err))
  13076. + goto out;
  13077. +
  13078. + sb = dentry->d_sb;
  13079. + br = au_sbr(sb, bindex);
  13080. + err = au_br_test_oflag(flags, br);
  13081. + h_file = ERR_PTR(err);
  13082. + if (unlikely(err))
  13083. + goto out;
  13084. +
  13085. + /* drop flags for writing */
  13086. + if (au_test_ro(sb, bindex, d_inode(dentry))) {
  13087. + if (force_wr && !(flags & O_WRONLY))
  13088. + force_wr = 0;
  13089. + flags = au_file_roflags(flags);
  13090. + if (force_wr) {
  13091. + h_file = ERR_PTR(-EROFS);
  13092. + flags = au_file_roflags(flags);
  13093. + if (unlikely(vfsub_native_ro(h_inode)
  13094. + || IS_APPEND(h_inode)))
  13095. + goto out;
  13096. + flags &= ~O_ACCMODE;
  13097. + flags |= O_WRONLY;
  13098. + }
  13099. + }
  13100. + flags &= ~O_CREAT;
  13101. + au_lcnt_inc(&br->br_nfiles);
  13102. + h_path.dentry = h_dentry;
  13103. + h_path.mnt = au_br_mnt(br);
  13104. + h_file = vfsub_dentry_open(&h_path, flags);
  13105. + if (IS_ERR(h_file))
  13106. + goto out_br;
  13107. +
  13108. + if (flags & __FMODE_EXEC) {
  13109. + err = deny_write_access(h_file);
  13110. + if (unlikely(err)) {
  13111. + fput(h_file);
  13112. + h_file = ERR_PTR(err);
  13113. + goto out_br;
  13114. + }
  13115. + }
  13116. + fsnotify_open(h_file);
  13117. + goto out; /* success */
  13118. +
  13119. +out_br:
  13120. + au_lcnt_dec(&br->br_nfiles);
  13121. +out:
  13122. + return h_file;
  13123. +}
  13124. +
  13125. +static int au_cmoo(struct dentry *dentry)
  13126. +{
  13127. + int err, cmoo, matched;
  13128. + unsigned int udba;
  13129. + struct path h_path;
  13130. + struct au_pin pin;
  13131. + struct au_cp_generic cpg = {
  13132. + .dentry = dentry,
  13133. + .bdst = -1,
  13134. + .bsrc = -1,
  13135. + .len = -1,
  13136. + .pin = &pin,
  13137. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  13138. + };
  13139. + struct inode *delegated;
  13140. + struct super_block *sb;
  13141. + struct au_sbinfo *sbinfo;
  13142. + struct au_fhsm *fhsm;
  13143. + pid_t pid;
  13144. + struct au_branch *br;
  13145. + struct dentry *parent;
  13146. + struct au_hinode *hdir;
  13147. +
  13148. + DiMustWriteLock(dentry);
  13149. + IiMustWriteLock(d_inode(dentry));
  13150. +
  13151. + err = 0;
  13152. + if (IS_ROOT(dentry))
  13153. + goto out;
  13154. + cpg.bsrc = au_dbtop(dentry);
  13155. + if (!cpg.bsrc)
  13156. + goto out;
  13157. +
  13158. + sb = dentry->d_sb;
  13159. + sbinfo = au_sbi(sb);
  13160. + fhsm = &sbinfo->si_fhsm;
  13161. + pid = au_fhsm_pid(fhsm);
  13162. + rcu_read_lock();
  13163. + matched = (pid
  13164. + && (current->pid == pid
  13165. + || rcu_dereference(current->real_parent)->pid == pid));
  13166. + rcu_read_unlock();
  13167. + if (matched)
  13168. + goto out;
  13169. +
  13170. + br = au_sbr(sb, cpg.bsrc);
  13171. + cmoo = au_br_cmoo(br->br_perm);
  13172. + if (!cmoo)
  13173. + goto out;
  13174. + if (!d_is_reg(dentry))
  13175. + cmoo &= AuBrAttr_COO_ALL;
  13176. + if (!cmoo)
  13177. + goto out;
  13178. +
  13179. + parent = dget_parent(dentry);
  13180. + di_write_lock_parent(parent);
  13181. + err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
  13182. + cpg.bdst = err;
  13183. + if (unlikely(err < 0)) {
  13184. + err = 0; /* there is no upper writable branch */
  13185. + goto out_dgrade;
  13186. + }
  13187. + AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
  13188. +
  13189. + /* do not respect the coo attrib for the target branch */
  13190. + err = au_cpup_dirs(dentry, cpg.bdst);
  13191. + if (unlikely(err))
  13192. + goto out_dgrade;
  13193. +
  13194. + di_downgrade_lock(parent, AuLock_IR);
  13195. + udba = au_opt_udba(sb);
  13196. + err = au_pin(&pin, dentry, cpg.bdst, udba,
  13197. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  13198. + if (unlikely(err))
  13199. + goto out_parent;
  13200. +
  13201. + err = au_sio_cpup_simple(&cpg);
  13202. + au_unpin(&pin);
  13203. + if (unlikely(err))
  13204. + goto out_parent;
  13205. + if (!(cmoo & AuBrWAttr_MOO))
  13206. + goto out_parent; /* success */
  13207. +
  13208. + err = au_pin(&pin, dentry, cpg.bsrc, udba,
  13209. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  13210. + if (unlikely(err))
  13211. + goto out_parent;
  13212. +
  13213. + h_path.mnt = au_br_mnt(br);
  13214. + h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
  13215. + hdir = au_hi(d_inode(parent), cpg.bsrc);
  13216. + delegated = NULL;
  13217. + err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
  13218. + au_unpin(&pin);
  13219. + /* todo: keep h_dentry or not? */
  13220. + if (unlikely(err == -EWOULDBLOCK)) {
  13221. + pr_warn("cannot retry for NFSv4 delegation"
  13222. + " for an internal unlink\n");
  13223. + iput(delegated);
  13224. + }
  13225. + if (unlikely(err)) {
  13226. + pr_err("unlink %pd after coo failed (%d), ignored\n",
  13227. + dentry, err);
  13228. + err = 0;
  13229. + }
  13230. + goto out_parent; /* success */
  13231. +
  13232. +out_dgrade:
  13233. + di_downgrade_lock(parent, AuLock_IR);
  13234. +out_parent:
  13235. + di_read_unlock(parent, AuLock_IR);
  13236. + dput(parent);
  13237. +out:
  13238. + AuTraceErr(err);
  13239. + return err;
  13240. +}
  13241. +
  13242. +int au_do_open(struct file *file, struct au_do_open_args *args)
  13243. +{
  13244. + int err, aopen = args->aopen;
  13245. + struct dentry *dentry;
  13246. + struct au_finfo *finfo;
  13247. +
  13248. + if (!aopen)
  13249. + err = au_finfo_init(file, args->fidir);
  13250. + else {
  13251. + lockdep_off();
  13252. + err = au_finfo_init(file, args->fidir);
  13253. + lockdep_on();
  13254. + }
  13255. + if (unlikely(err))
  13256. + goto out;
  13257. +
  13258. + dentry = file->f_path.dentry;
  13259. + AuDebugOn(IS_ERR_OR_NULL(dentry));
  13260. + di_write_lock_child(dentry);
  13261. + err = au_cmoo(dentry);
  13262. + di_downgrade_lock(dentry, AuLock_IR);
  13263. + if (!err) {
  13264. + if (!aopen)
  13265. + err = args->open(file, vfsub_file_flags(file), NULL);
  13266. + else {
  13267. + lockdep_off();
  13268. + err = args->open(file, vfsub_file_flags(file),
  13269. + args->h_file);
  13270. + lockdep_on();
  13271. + }
  13272. + }
  13273. + di_read_unlock(dentry, AuLock_IR);
  13274. +
  13275. + finfo = au_fi(file);
  13276. + if (!err) {
  13277. + finfo->fi_file = file;
  13278. + au_hbl_add(&finfo->fi_hlist,
  13279. + &au_sbi(file->f_path.dentry->d_sb)->si_files);
  13280. + }
  13281. + if (!aopen)
  13282. + fi_write_unlock(file);
  13283. + else {
  13284. + lockdep_off();
  13285. + fi_write_unlock(file);
  13286. + lockdep_on();
  13287. + }
  13288. + if (unlikely(err)) {
  13289. + finfo->fi_hdir = NULL;
  13290. + au_finfo_fin(file);
  13291. + }
  13292. +
  13293. +out:
  13294. + AuTraceErr(err);
  13295. + return err;
  13296. +}
  13297. +
  13298. +int au_reopen_nondir(struct file *file)
  13299. +{
  13300. + int err;
  13301. + aufs_bindex_t btop;
  13302. + struct dentry *dentry;
  13303. + struct au_branch *br;
  13304. + struct file *h_file, *h_file_tmp;
  13305. +
  13306. + dentry = file->f_path.dentry;
  13307. + btop = au_dbtop(dentry);
  13308. + br = au_sbr(dentry->d_sb, btop);
  13309. + h_file_tmp = NULL;
  13310. + if (au_fbtop(file) == btop) {
  13311. + h_file = au_hf_top(file);
  13312. + if (file->f_mode == h_file->f_mode)
  13313. + return 0; /* success */
  13314. + h_file_tmp = h_file;
  13315. + get_file(h_file_tmp);
  13316. + au_lcnt_inc(&br->br_nfiles);
  13317. + au_set_h_fptr(file, btop, NULL);
  13318. + }
  13319. + AuDebugOn(au_fi(file)->fi_hdir);
  13320. + /*
  13321. + * it can happen
  13322. + * file exists on both of rw and ro
  13323. + * open --> dbtop and fbtop are both 0
  13324. + * prepend a branch as rw, "rw" become ro
  13325. + * remove rw/file
  13326. + * delete the top branch, "rw" becomes rw again
  13327. + * --> dbtop is 1, fbtop is still 0
  13328. + * write --> fbtop is 0 but dbtop is 1
  13329. + */
  13330. + /* AuDebugOn(au_fbtop(file) < btop); */
  13331. +
  13332. + h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
  13333. + file, /*force_wr*/0);
  13334. + err = PTR_ERR(h_file);
  13335. + if (IS_ERR(h_file)) {
  13336. + if (h_file_tmp) {
  13337. + /* revert */
  13338. + au_set_h_fptr(file, btop, h_file_tmp);
  13339. + h_file_tmp = NULL;
  13340. + }
  13341. + goto out; /* todo: close all? */
  13342. + }
  13343. +
  13344. + err = 0;
  13345. + au_set_fbtop(file, btop);
  13346. + au_set_h_fptr(file, btop, h_file);
  13347. + au_update_figen(file);
  13348. + /* todo: necessary? */
  13349. + /* file->f_ra = h_file->f_ra; */
  13350. +
  13351. +out:
  13352. + if (h_file_tmp) {
  13353. + fput(h_file_tmp);
  13354. + au_lcnt_dec(&br->br_nfiles);
  13355. + }
  13356. + return err;
  13357. +}
  13358. +
  13359. +/* ---------------------------------------------------------------------- */
  13360. +
  13361. +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
  13362. + struct dentry *hi_wh)
  13363. +{
  13364. + int err;
  13365. + aufs_bindex_t btop;
  13366. + struct au_dinfo *dinfo;
  13367. + struct dentry *h_dentry;
  13368. + struct au_hdentry *hdp;
  13369. +
  13370. + dinfo = au_di(file->f_path.dentry);
  13371. + AuRwMustWriteLock(&dinfo->di_rwsem);
  13372. +
  13373. + btop = dinfo->di_btop;
  13374. + dinfo->di_btop = btgt;
  13375. + hdp = au_hdentry(dinfo, btgt);
  13376. + h_dentry = hdp->hd_dentry;
  13377. + hdp->hd_dentry = hi_wh;
  13378. + err = au_reopen_nondir(file);
  13379. + hdp->hd_dentry = h_dentry;
  13380. + dinfo->di_btop = btop;
  13381. +
  13382. + return err;
  13383. +}
  13384. +
  13385. +static int au_ready_to_write_wh(struct file *file, loff_t len,
  13386. + aufs_bindex_t bcpup, struct au_pin *pin)
  13387. +{
  13388. + int err;
  13389. + struct inode *inode, *h_inode;
  13390. + struct dentry *h_dentry, *hi_wh;
  13391. + struct au_cp_generic cpg = {
  13392. + .dentry = file->f_path.dentry,
  13393. + .bdst = bcpup,
  13394. + .bsrc = -1,
  13395. + .len = len,
  13396. + .pin = pin
  13397. + };
  13398. +
  13399. + au_update_dbtop(cpg.dentry);
  13400. + inode = d_inode(cpg.dentry);
  13401. + h_inode = NULL;
  13402. + if (au_dbtop(cpg.dentry) <= bcpup
  13403. + && au_dbbot(cpg.dentry) >= bcpup) {
  13404. + h_dentry = au_h_dptr(cpg.dentry, bcpup);
  13405. + if (h_dentry && d_is_positive(h_dentry))
  13406. + h_inode = d_inode(h_dentry);
  13407. + }
  13408. + hi_wh = au_hi_wh(inode, bcpup);
  13409. + if (!hi_wh && !h_inode)
  13410. + err = au_sio_cpup_wh(&cpg, file);
  13411. + else
  13412. + /* already copied-up after unlink */
  13413. + err = au_reopen_wh(file, bcpup, hi_wh);
  13414. +
  13415. + if (!err
  13416. + && (inode->i_nlink > 1
  13417. + || (inode->i_state & I_LINKABLE))
  13418. + && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
  13419. + au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
  13420. +
  13421. + return err;
  13422. +}
  13423. +
  13424. +/*
  13425. + * prepare the @file for writing.
  13426. + */
  13427. +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
  13428. +{
  13429. + int err;
  13430. + aufs_bindex_t dbtop;
  13431. + struct dentry *parent;
  13432. + struct inode *inode;
  13433. + struct super_block *sb;
  13434. + struct file *h_file;
  13435. + struct au_cp_generic cpg = {
  13436. + .dentry = file->f_path.dentry,
  13437. + .bdst = -1,
  13438. + .bsrc = -1,
  13439. + .len = len,
  13440. + .pin = pin,
  13441. + .flags = AuCpup_DTIME
  13442. + };
  13443. +
  13444. + sb = cpg.dentry->d_sb;
  13445. + inode = d_inode(cpg.dentry);
  13446. + cpg.bsrc = au_fbtop(file);
  13447. + err = au_test_ro(sb, cpg.bsrc, inode);
  13448. + if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
  13449. + err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
  13450. + /*flags*/0);
  13451. + goto out;
  13452. + }
  13453. +
  13454. + /* need to cpup or reopen */
  13455. + parent = dget_parent(cpg.dentry);
  13456. + di_write_lock_parent(parent);
  13457. + err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
  13458. + cpg.bdst = err;
  13459. + if (unlikely(err < 0))
  13460. + goto out_dgrade;
  13461. + err = 0;
  13462. +
  13463. + if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
  13464. + err = au_cpup_dirs(cpg.dentry, cpg.bdst);
  13465. + if (unlikely(err))
  13466. + goto out_dgrade;
  13467. + }
  13468. +
  13469. + err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
  13470. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  13471. + if (unlikely(err))
  13472. + goto out_dgrade;
  13473. +
  13474. + dbtop = au_dbtop(cpg.dentry);
  13475. + if (dbtop <= cpg.bdst)
  13476. + cpg.bsrc = cpg.bdst;
  13477. +
  13478. + if (dbtop <= cpg.bdst /* just reopen */
  13479. + || !d_unhashed(cpg.dentry) /* copyup and reopen */
  13480. + ) {
  13481. + h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
  13482. + if (IS_ERR(h_file))
  13483. + err = PTR_ERR(h_file);
  13484. + else {
  13485. + di_downgrade_lock(parent, AuLock_IR);
  13486. + if (dbtop > cpg.bdst)
  13487. + err = au_sio_cpup_simple(&cpg);
  13488. + if (!err)
  13489. + err = au_reopen_nondir(file);
  13490. + au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
  13491. + }
  13492. + } else { /* copyup as wh and reopen */
  13493. + /*
  13494. + * since writable hfsplus branch is not supported,
  13495. + * h_open_pre/post() are unnecessary.
  13496. + */
  13497. + err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
  13498. + di_downgrade_lock(parent, AuLock_IR);
  13499. + }
  13500. +
  13501. + if (!err) {
  13502. + au_pin_set_parent_lflag(pin, /*lflag*/0);
  13503. + goto out_dput; /* success */
  13504. + }
  13505. + au_unpin(pin);
  13506. + goto out_unlock;
  13507. +
  13508. +out_dgrade:
  13509. + di_downgrade_lock(parent, AuLock_IR);
  13510. +out_unlock:
  13511. + di_read_unlock(parent, AuLock_IR);
  13512. +out_dput:
  13513. + dput(parent);
  13514. +out:
  13515. + return err;
  13516. +}
  13517. +
  13518. +/* ---------------------------------------------------------------------- */
  13519. +
  13520. +int au_do_flush(struct file *file, fl_owner_t id,
  13521. + int (*flush)(struct file *file, fl_owner_t id))
  13522. +{
  13523. + int err;
  13524. + struct super_block *sb;
  13525. + struct inode *inode;
  13526. +
  13527. + inode = file_inode(file);
  13528. + sb = inode->i_sb;
  13529. + si_noflush_read_lock(sb);
  13530. + fi_read_lock(file);
  13531. + ii_read_lock_child(inode);
  13532. +
  13533. + err = flush(file, id);
  13534. + au_cpup_attr_timesizes(inode);
  13535. +
  13536. + ii_read_unlock(inode);
  13537. + fi_read_unlock(file);
  13538. + si_read_unlock(sb);
  13539. + return err;
  13540. +}
  13541. +
  13542. +/* ---------------------------------------------------------------------- */
  13543. +
  13544. +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
  13545. +{
  13546. + int err;
  13547. + struct au_pin pin;
  13548. + struct au_finfo *finfo;
  13549. + struct dentry *parent, *hi_wh;
  13550. + struct inode *inode;
  13551. + struct super_block *sb;
  13552. + struct au_cp_generic cpg = {
  13553. + .dentry = file->f_path.dentry,
  13554. + .bdst = -1,
  13555. + .bsrc = -1,
  13556. + .len = -1,
  13557. + .pin = &pin,
  13558. + .flags = AuCpup_DTIME
  13559. + };
  13560. +
  13561. + FiMustWriteLock(file);
  13562. +
  13563. + err = 0;
  13564. + finfo = au_fi(file);
  13565. + sb = cpg.dentry->d_sb;
  13566. + inode = d_inode(cpg.dentry);
  13567. + cpg.bdst = au_ibtop(inode);
  13568. + if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
  13569. + goto out;
  13570. +
  13571. + parent = dget_parent(cpg.dentry);
  13572. + if (au_test_ro(sb, cpg.bdst, inode)) {
  13573. + di_read_lock_parent(parent, !AuLock_IR);
  13574. + err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
  13575. + cpg.bdst = err;
  13576. + di_read_unlock(parent, !AuLock_IR);
  13577. + if (unlikely(err < 0))
  13578. + goto out_parent;
  13579. + err = 0;
  13580. + }
  13581. +
  13582. + di_read_lock_parent(parent, AuLock_IR);
  13583. + hi_wh = au_hi_wh(inode, cpg.bdst);
  13584. + if (!S_ISDIR(inode->i_mode)
  13585. + && au_opt_test(au_mntflags(sb), PLINK)
  13586. + && au_plink_test(inode)
  13587. + && !d_unhashed(cpg.dentry)
  13588. + && cpg.bdst < au_dbtop(cpg.dentry)) {
  13589. + err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
  13590. + if (unlikely(err))
  13591. + goto out_unlock;
  13592. +
  13593. + /* always superio. */
  13594. + err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
  13595. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  13596. + if (!err) {
  13597. + err = au_sio_cpup_simple(&cpg);
  13598. + au_unpin(&pin);
  13599. + }
  13600. + } else if (hi_wh) {
  13601. + /* already copied-up after unlink */
  13602. + err = au_reopen_wh(file, cpg.bdst, hi_wh);
  13603. + *need_reopen = 0;
  13604. + }
  13605. +
  13606. +out_unlock:
  13607. + di_read_unlock(parent, AuLock_IR);
  13608. +out_parent:
  13609. + dput(parent);
  13610. +out:
  13611. + return err;
  13612. +}
  13613. +
  13614. +static void au_do_refresh_dir(struct file *file)
  13615. +{
  13616. + aufs_bindex_t bindex, bbot, new_bindex, brid;
  13617. + struct au_hfile *p, tmp, *q;
  13618. + struct au_finfo *finfo;
  13619. + struct super_block *sb;
  13620. + struct au_fidir *fidir;
  13621. +
  13622. + FiMustWriteLock(file);
  13623. +
  13624. + sb = file->f_path.dentry->d_sb;
  13625. + finfo = au_fi(file);
  13626. + fidir = finfo->fi_hdir;
  13627. + AuDebugOn(!fidir);
  13628. + p = fidir->fd_hfile + finfo->fi_btop;
  13629. + brid = p->hf_br->br_id;
  13630. + bbot = fidir->fd_bbot;
  13631. + for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
  13632. + if (!p->hf_file)
  13633. + continue;
  13634. +
  13635. + new_bindex = au_br_index(sb, p->hf_br->br_id);
  13636. + if (new_bindex == bindex)
  13637. + continue;
  13638. + if (new_bindex < 0) {
  13639. + au_set_h_fptr(file, bindex, NULL);
  13640. + continue;
  13641. + }
  13642. +
  13643. + /* swap two lower inode, and loop again */
  13644. + q = fidir->fd_hfile + new_bindex;
  13645. + tmp = *q;
  13646. + *q = *p;
  13647. + *p = tmp;
  13648. + if (tmp.hf_file) {
  13649. + bindex--;
  13650. + p--;
  13651. + }
  13652. + }
  13653. +
  13654. + p = fidir->fd_hfile;
  13655. + if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
  13656. + bbot = au_sbbot(sb);
  13657. + for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
  13658. + finfo->fi_btop++, p++)
  13659. + if (p->hf_file) {
  13660. + if (file_inode(p->hf_file))
  13661. + break;
  13662. + au_hfput(p, /*execed*/0);
  13663. + }
  13664. + } else {
  13665. + bbot = au_br_index(sb, brid);
  13666. + for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
  13667. + finfo->fi_btop++, p++)
  13668. + if (p->hf_file)
  13669. + au_hfput(p, /*execed*/0);
  13670. + bbot = au_sbbot(sb);
  13671. + }
  13672. +
  13673. + p = fidir->fd_hfile + bbot;
  13674. + for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
  13675. + fidir->fd_bbot--, p--)
  13676. + if (p->hf_file) {
  13677. + if (file_inode(p->hf_file))
  13678. + break;
  13679. + au_hfput(p, /*execed*/0);
  13680. + }
  13681. + AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
  13682. +}
  13683. +
  13684. +/*
  13685. + * after branch manipulating, refresh the file.
  13686. + */
  13687. +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
  13688. +{
  13689. + int err, need_reopen, nbr;
  13690. + aufs_bindex_t bbot, bindex;
  13691. + struct dentry *dentry;
  13692. + struct super_block *sb;
  13693. + struct au_finfo *finfo;
  13694. + struct au_hfile *hfile;
  13695. +
  13696. + dentry = file->f_path.dentry;
  13697. + sb = dentry->d_sb;
  13698. + nbr = au_sbbot(sb) + 1;
  13699. + finfo = au_fi(file);
  13700. + if (!finfo->fi_hdir) {
  13701. + hfile = &finfo->fi_htop;
  13702. + AuDebugOn(!hfile->hf_file);
  13703. + bindex = au_br_index(sb, hfile->hf_br->br_id);
  13704. + AuDebugOn(bindex < 0);
  13705. + if (bindex != finfo->fi_btop)
  13706. + au_set_fbtop(file, bindex);
  13707. + } else {
  13708. + err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
  13709. + if (unlikely(err))
  13710. + goto out;
  13711. + au_do_refresh_dir(file);
  13712. + }
  13713. +
  13714. + err = 0;
  13715. + need_reopen = 1;
  13716. + if (!au_test_mmapped(file))
  13717. + err = au_file_refresh_by_inode(file, &need_reopen);
  13718. + if (finfo->fi_hdir)
  13719. + /* harmless if err */
  13720. + au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
  13721. + if (!err && need_reopen && !d_unlinked(dentry))
  13722. + err = reopen(file);
  13723. + if (!err) {
  13724. + au_update_figen(file);
  13725. + goto out; /* success */
  13726. + }
  13727. +
  13728. + /* error, close all lower files */
  13729. + if (finfo->fi_hdir) {
  13730. + bbot = au_fbbot_dir(file);
  13731. + for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
  13732. + au_set_h_fptr(file, bindex, NULL);
  13733. + }
  13734. +
  13735. +out:
  13736. + return err;
  13737. +}
  13738. +
  13739. +/* common function to regular file and dir */
  13740. +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
  13741. + int wlock, unsigned int fi_lsc)
  13742. +{
  13743. + int err;
  13744. + unsigned int sigen, figen;
  13745. + aufs_bindex_t btop;
  13746. + unsigned char pseudo_link;
  13747. + struct dentry *dentry;
  13748. + struct inode *inode;
  13749. +
  13750. + err = 0;
  13751. + dentry = file->f_path.dentry;
  13752. + inode = d_inode(dentry);
  13753. + sigen = au_sigen(dentry->d_sb);
  13754. + fi_write_lock_nested(file, fi_lsc);
  13755. + figen = au_figen(file);
  13756. + if (!fi_lsc)
  13757. + di_write_lock_child(dentry);
  13758. + else
  13759. + di_write_lock_child2(dentry);
  13760. + btop = au_dbtop(dentry);
  13761. + pseudo_link = (btop != au_ibtop(inode));
  13762. + if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
  13763. + if (!wlock) {
  13764. + di_downgrade_lock(dentry, AuLock_IR);
  13765. + fi_downgrade_lock(file);
  13766. + }
  13767. + goto out; /* success */
  13768. + }
  13769. +
  13770. + AuDbg("sigen %d, figen %d\n", sigen, figen);
  13771. + if (au_digen_test(dentry, sigen)) {
  13772. + err = au_reval_dpath(dentry, sigen);
  13773. + AuDebugOn(!err && au_digen_test(dentry, sigen));
  13774. + }
  13775. +
  13776. + if (!err)
  13777. + err = refresh_file(file, reopen);
  13778. + if (!err) {
  13779. + if (!wlock) {
  13780. + di_downgrade_lock(dentry, AuLock_IR);
  13781. + fi_downgrade_lock(file);
  13782. + }
  13783. + } else {
  13784. + di_write_unlock(dentry);
  13785. + fi_write_unlock(file);
  13786. + }
  13787. +
  13788. +out:
  13789. + return err;
  13790. +}
  13791. +
  13792. +/* ---------------------------------------------------------------------- */
  13793. +
  13794. +/* cf. aufs_nopage() */
  13795. +/* for madvise(2) */
  13796. +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
  13797. +{
  13798. + unlock_page(page);
  13799. + return 0;
  13800. +}
  13801. +
  13802. +/* it will never be called, but necessary to support O_DIRECT */
  13803. +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  13804. +{ BUG(); return 0; }
  13805. +
  13806. +/* they will never be called. */
  13807. +#ifdef CONFIG_AUFS_DEBUG
  13808. +static int aufs_write_begin(struct file *file, struct address_space *mapping,
  13809. + loff_t pos, unsigned len, unsigned flags,
  13810. + struct page **pagep, void **fsdata)
  13811. +{ AuUnsupport(); return 0; }
  13812. +static int aufs_write_end(struct file *file, struct address_space *mapping,
  13813. + loff_t pos, unsigned len, unsigned copied,
  13814. + struct page *page, void *fsdata)
  13815. +{ AuUnsupport(); return 0; }
  13816. +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
  13817. +{ AuUnsupport(); return 0; }
  13818. +
  13819. +static int aufs_set_page_dirty(struct page *page)
  13820. +{ AuUnsupport(); return 0; }
  13821. +static void aufs_invalidatepage(struct page *page, unsigned int offset,
  13822. + unsigned int length)
  13823. +{ AuUnsupport(); }
  13824. +static int aufs_releasepage(struct page *page, gfp_t gfp)
  13825. +{ AuUnsupport(); return 0; }
  13826. +#if 0 /* called by memory compaction regardless file */
  13827. +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
  13828. + struct page *page, enum migrate_mode mode)
  13829. +{ AuUnsupport(); return 0; }
  13830. +#endif
  13831. +static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
  13832. +{ AuUnsupport(); return true; }
  13833. +static void aufs_putback_page(struct page *page)
  13834. +{ AuUnsupport(); }
  13835. +static int aufs_launder_page(struct page *page)
  13836. +{ AuUnsupport(); return 0; }
  13837. +static int aufs_is_partially_uptodate(struct page *page,
  13838. + unsigned long from,
  13839. + unsigned long count)
  13840. +{ AuUnsupport(); return 0; }
  13841. +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
  13842. + bool *writeback)
  13843. +{ AuUnsupport(); }
  13844. +static int aufs_error_remove_page(struct address_space *mapping,
  13845. + struct page *page)
  13846. +{ AuUnsupport(); return 0; }
  13847. +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
  13848. + sector_t *span)
  13849. +{ AuUnsupport(); return 0; }
  13850. +static void aufs_swap_deactivate(struct file *file)
  13851. +{ AuUnsupport(); }
  13852. +#endif /* CONFIG_AUFS_DEBUG */
  13853. +
  13854. +const struct address_space_operations aufs_aop = {
  13855. + .readpage = aufs_readpage,
  13856. + .direct_IO = aufs_direct_IO,
  13857. +#ifdef CONFIG_AUFS_DEBUG
  13858. + .writepage = aufs_writepage,
  13859. + /* no writepages, because of writepage */
  13860. + .set_page_dirty = aufs_set_page_dirty,
  13861. + /* no readpages, because of readpage */
  13862. + .write_begin = aufs_write_begin,
  13863. + .write_end = aufs_write_end,
  13864. + /* no bmap, no block device */
  13865. + .invalidatepage = aufs_invalidatepage,
  13866. + .releasepage = aufs_releasepage,
  13867. + /* is fallback_migrate_page ok? */
  13868. + /* .migratepage = aufs_migratepage, */
  13869. + .isolate_page = aufs_isolate_page,
  13870. + .putback_page = aufs_putback_page,
  13871. + .launder_page = aufs_launder_page,
  13872. + .is_partially_uptodate = aufs_is_partially_uptodate,
  13873. + .is_dirty_writeback = aufs_is_dirty_writeback,
  13874. + .error_remove_page = aufs_error_remove_page,
  13875. + .swap_activate = aufs_swap_activate,
  13876. + .swap_deactivate = aufs_swap_deactivate
  13877. +#endif /* CONFIG_AUFS_DEBUG */
  13878. +};
  13879. --- /dev/null
  13880. +++ linux-4.19/fs/aufs/file.h 2018-11-20 09:35:15.021195504 +0200
  13881. @@ -0,0 +1,341 @@
  13882. +/* SPDX-License-Identifier: GPL-2.0 */
  13883. +/*
  13884. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  13885. + *
  13886. + * This program, aufs is free software; you can redistribute it and/or modify
  13887. + * it under the terms of the GNU General Public License as published by
  13888. + * the Free Software Foundation; either version 2 of the License, or
  13889. + * (at your option) any later version.
  13890. + *
  13891. + * This program is distributed in the hope that it will be useful,
  13892. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13893. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13894. + * GNU General Public License for more details.
  13895. + *
  13896. + * You should have received a copy of the GNU General Public License
  13897. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  13898. + */
  13899. +
  13900. +/*
  13901. + * file operations
  13902. + */
  13903. +
  13904. +#ifndef __AUFS_FILE_H__
  13905. +#define __AUFS_FILE_H__
  13906. +
  13907. +#ifdef __KERNEL__
  13908. +
  13909. +#include <linux/file.h>
  13910. +#include <linux/fs.h>
  13911. +#include <linux/mm_types.h>
  13912. +#include <linux/poll.h>
  13913. +#include "rwsem.h"
  13914. +
  13915. +struct au_branch;
  13916. +struct au_hfile {
  13917. + struct file *hf_file;
  13918. + struct au_branch *hf_br;
  13919. +};
  13920. +
  13921. +struct au_vdir;
  13922. +struct au_fidir {
  13923. + aufs_bindex_t fd_bbot;
  13924. + aufs_bindex_t fd_nent;
  13925. + struct au_vdir *fd_vdir_cache;
  13926. + struct au_hfile fd_hfile[];
  13927. +};
  13928. +
  13929. +static inline int au_fidir_sz(int nent)
  13930. +{
  13931. + AuDebugOn(nent < 0);
  13932. + return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
  13933. +}
  13934. +
  13935. +struct au_finfo {
  13936. + atomic_t fi_generation;
  13937. +
  13938. + struct au_rwsem fi_rwsem;
  13939. + aufs_bindex_t fi_btop;
  13940. +
  13941. + /* do not union them */
  13942. + struct { /* for non-dir */
  13943. + struct au_hfile fi_htop;
  13944. + atomic_t fi_mmapped;
  13945. + };
  13946. + struct au_fidir *fi_hdir; /* for dir only */
  13947. +
  13948. + struct hlist_bl_node fi_hlist;
  13949. + struct file *fi_file; /* very ugly */
  13950. +} ____cacheline_aligned_in_smp;
  13951. +
  13952. +/* ---------------------------------------------------------------------- */
  13953. +
  13954. +/* file.c */
  13955. +extern const struct address_space_operations aufs_aop;
  13956. +unsigned int au_file_roflags(unsigned int flags);
  13957. +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
  13958. + struct file *file, int force_wr);
  13959. +struct au_do_open_args {
  13960. + int aopen;
  13961. + int (*open)(struct file *file, int flags,
  13962. + struct file *h_file);
  13963. + struct au_fidir *fidir;
  13964. + struct file *h_file;
  13965. +};
  13966. +int au_do_open(struct file *file, struct au_do_open_args *args);
  13967. +int au_reopen_nondir(struct file *file);
  13968. +struct au_pin;
  13969. +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
  13970. +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
  13971. + int wlock, unsigned int fi_lsc);
  13972. +int au_do_flush(struct file *file, fl_owner_t id,
  13973. + int (*flush)(struct file *file, fl_owner_t id));
  13974. +
  13975. +/* poll.c */
  13976. +#ifdef CONFIG_AUFS_POLL
  13977. +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
  13978. +#endif
  13979. +
  13980. +#ifdef CONFIG_AUFS_BR_HFSPLUS
  13981. +/* hfsplus.c */
  13982. +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
  13983. + int force_wr);
  13984. +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
  13985. + struct file *h_file);
  13986. +#else
  13987. +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
  13988. + aufs_bindex_t bindex, int force_wr)
  13989. +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
  13990. + struct file *h_file);
  13991. +#endif
  13992. +
  13993. +/* f_op.c */
  13994. +extern const struct file_operations aufs_file_fop;
  13995. +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
  13996. +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
  13997. +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
  13998. +
  13999. +/* finfo.c */
  14000. +void au_hfput(struct au_hfile *hf, int execed);
  14001. +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
  14002. + struct file *h_file);
  14003. +
  14004. +void au_update_figen(struct file *file);
  14005. +struct au_fidir *au_fidir_alloc(struct super_block *sb);
  14006. +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
  14007. +
  14008. +void au_fi_init_once(void *_fi);
  14009. +void au_finfo_fin(struct file *file);
  14010. +int au_finfo_init(struct file *file, struct au_fidir *fidir);
  14011. +
  14012. +/* ioctl.c */
  14013. +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
  14014. +#ifdef CONFIG_COMPAT
  14015. +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
  14016. + unsigned long arg);
  14017. +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
  14018. + unsigned long arg);
  14019. +#endif
  14020. +
  14021. +/* ---------------------------------------------------------------------- */
  14022. +
  14023. +static inline struct au_finfo *au_fi(struct file *file)
  14024. +{
  14025. + return file->private_data;
  14026. +}
  14027. +
  14028. +/* ---------------------------------------------------------------------- */
  14029. +
  14030. +#define fi_read_lock(f) au_rw_read_lock(&au_fi(f)->fi_rwsem)
  14031. +#define fi_write_lock(f) au_rw_write_lock(&au_fi(f)->fi_rwsem)
  14032. +#define fi_read_trylock(f) au_rw_read_trylock(&au_fi(f)->fi_rwsem)
  14033. +#define fi_write_trylock(f) au_rw_write_trylock(&au_fi(f)->fi_rwsem)
  14034. +/*
  14035. +#define fi_read_trylock_nested(f) \
  14036. + au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
  14037. +#define fi_write_trylock_nested(f) \
  14038. + au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
  14039. +*/
  14040. +
  14041. +#define fi_read_unlock(f) au_rw_read_unlock(&au_fi(f)->fi_rwsem)
  14042. +#define fi_write_unlock(f) au_rw_write_unlock(&au_fi(f)->fi_rwsem)
  14043. +#define fi_downgrade_lock(f) au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
  14044. +
  14045. +/* lock subclass for finfo */
  14046. +enum {
  14047. + AuLsc_FI_1,
  14048. + AuLsc_FI_2
  14049. +};
  14050. +
  14051. +static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
  14052. +{
  14053. + au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
  14054. +}
  14055. +
  14056. +static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
  14057. +{
  14058. + au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
  14059. +}
  14060. +
  14061. +/*
  14062. + * fi_read_lock_1, fi_write_lock_1,
  14063. + * fi_read_lock_2, fi_write_lock_2
  14064. + */
  14065. +#define AuReadLockFunc(name) \
  14066. +static inline void fi_read_lock_##name(struct file *f) \
  14067. +{ fi_read_lock_nested(f, AuLsc_FI_##name); }
  14068. +
  14069. +#define AuWriteLockFunc(name) \
  14070. +static inline void fi_write_lock_##name(struct file *f) \
  14071. +{ fi_write_lock_nested(f, AuLsc_FI_##name); }
  14072. +
  14073. +#define AuRWLockFuncs(name) \
  14074. + AuReadLockFunc(name) \
  14075. + AuWriteLockFunc(name)
  14076. +
  14077. +AuRWLockFuncs(1);
  14078. +AuRWLockFuncs(2);
  14079. +
  14080. +#undef AuReadLockFunc
  14081. +#undef AuWriteLockFunc
  14082. +#undef AuRWLockFuncs
  14083. +
  14084. +#define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
  14085. +#define FiMustAnyLock(f) AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
  14086. +#define FiMustWriteLock(f) AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
  14087. +
  14088. +/* ---------------------------------------------------------------------- */
  14089. +
  14090. +/* todo: hard/soft set? */
  14091. +static inline aufs_bindex_t au_fbtop(struct file *file)
  14092. +{
  14093. + FiMustAnyLock(file);
  14094. + return au_fi(file)->fi_btop;
  14095. +}
  14096. +
  14097. +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
  14098. +{
  14099. + FiMustAnyLock(file);
  14100. + AuDebugOn(!au_fi(file)->fi_hdir);
  14101. + return au_fi(file)->fi_hdir->fd_bbot;
  14102. +}
  14103. +
  14104. +static inline struct au_vdir *au_fvdir_cache(struct file *file)
  14105. +{
  14106. + FiMustAnyLock(file);
  14107. + AuDebugOn(!au_fi(file)->fi_hdir);
  14108. + return au_fi(file)->fi_hdir->fd_vdir_cache;
  14109. +}
  14110. +
  14111. +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
  14112. +{
  14113. + FiMustWriteLock(file);
  14114. + au_fi(file)->fi_btop = bindex;
  14115. +}
  14116. +
  14117. +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
  14118. +{
  14119. + FiMustWriteLock(file);
  14120. + AuDebugOn(!au_fi(file)->fi_hdir);
  14121. + au_fi(file)->fi_hdir->fd_bbot = bindex;
  14122. +}
  14123. +
  14124. +static inline void au_set_fvdir_cache(struct file *file,
  14125. + struct au_vdir *vdir_cache)
  14126. +{
  14127. + FiMustWriteLock(file);
  14128. + AuDebugOn(!au_fi(file)->fi_hdir);
  14129. + au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
  14130. +}
  14131. +
  14132. +static inline struct file *au_hf_top(struct file *file)
  14133. +{
  14134. + FiMustAnyLock(file);
  14135. + AuDebugOn(au_fi(file)->fi_hdir);
  14136. + return au_fi(file)->fi_htop.hf_file;
  14137. +}
  14138. +
  14139. +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
  14140. +{
  14141. + FiMustAnyLock(file);
  14142. + AuDebugOn(!au_fi(file)->fi_hdir);
  14143. + return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
  14144. +}
  14145. +
  14146. +/* todo: memory barrier? */
  14147. +static inline unsigned int au_figen(struct file *f)
  14148. +{
  14149. + return atomic_read(&au_fi(f)->fi_generation);
  14150. +}
  14151. +
  14152. +static inline void au_set_mmapped(struct file *f)
  14153. +{
  14154. + if (atomic_inc_return(&au_fi(f)->fi_mmapped))
  14155. + return;
  14156. + pr_warn("fi_mmapped wrapped around\n");
  14157. + while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
  14158. + ;
  14159. +}
  14160. +
  14161. +static inline void au_unset_mmapped(struct file *f)
  14162. +{
  14163. + atomic_dec(&au_fi(f)->fi_mmapped);
  14164. +}
  14165. +
  14166. +static inline int au_test_mmapped(struct file *f)
  14167. +{
  14168. + return atomic_read(&au_fi(f)->fi_mmapped);
  14169. +}
  14170. +
  14171. +/* customize vma->vm_file */
  14172. +
  14173. +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
  14174. + struct file *file)
  14175. +{
  14176. + struct file *f;
  14177. +
  14178. + f = vma->vm_file;
  14179. + get_file(file);
  14180. + vma->vm_file = file;
  14181. + fput(f);
  14182. +}
  14183. +
  14184. +#ifdef CONFIG_MMU
  14185. +#define AuDbgVmRegion(file, vma) do {} while (0)
  14186. +
  14187. +static inline void au_vm_file_reset(struct vm_area_struct *vma,
  14188. + struct file *file)
  14189. +{
  14190. + au_do_vm_file_reset(vma, file);
  14191. +}
  14192. +#else
  14193. +#define AuDbgVmRegion(file, vma) \
  14194. + AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
  14195. +
  14196. +static inline void au_vm_file_reset(struct vm_area_struct *vma,
  14197. + struct file *file)
  14198. +{
  14199. + struct file *f;
  14200. +
  14201. + au_do_vm_file_reset(vma, file);
  14202. + f = vma->vm_region->vm_file;
  14203. + get_file(file);
  14204. + vma->vm_region->vm_file = file;
  14205. + fput(f);
  14206. +}
  14207. +#endif /* CONFIG_MMU */
  14208. +
  14209. +/* handle vma->vm_prfile */
  14210. +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
  14211. + struct file *file)
  14212. +{
  14213. + get_file(file);
  14214. + vma->vm_prfile = file;
  14215. +#ifndef CONFIG_MMU
  14216. + get_file(file);
  14217. + vma->vm_region->vm_prfile = file;
  14218. +#endif
  14219. +}
  14220. +
  14221. +#endif /* __KERNEL__ */
  14222. +#endif /* __AUFS_FILE_H__ */
  14223. --- /dev/null
  14224. +++ linux-4.19/fs/aufs/finfo.c 2018-11-20 09:35:15.022195504 +0200
  14225. @@ -0,0 +1,149 @@
  14226. +// SPDX-License-Identifier: GPL-2.0
  14227. +/*
  14228. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  14229. + *
  14230. + * This program, aufs is free software; you can redistribute it and/or modify
  14231. + * it under the terms of the GNU General Public License as published by
  14232. + * the Free Software Foundation; either version 2 of the License, or
  14233. + * (at your option) any later version.
  14234. + *
  14235. + * This program is distributed in the hope that it will be useful,
  14236. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14237. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14238. + * GNU General Public License for more details.
  14239. + *
  14240. + * You should have received a copy of the GNU General Public License
  14241. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14242. + */
  14243. +
  14244. +/*
  14245. + * file private data
  14246. + */
  14247. +
  14248. +#include "aufs.h"
  14249. +
  14250. +void au_hfput(struct au_hfile *hf, int execed)
  14251. +{
  14252. + if (execed)
  14253. + allow_write_access(hf->hf_file);
  14254. + fput(hf->hf_file);
  14255. + hf->hf_file = NULL;
  14256. + au_lcnt_dec(&hf->hf_br->br_nfiles);
  14257. + hf->hf_br = NULL;
  14258. +}
  14259. +
  14260. +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
  14261. +{
  14262. + struct au_finfo *finfo = au_fi(file);
  14263. + struct au_hfile *hf;
  14264. + struct au_fidir *fidir;
  14265. +
  14266. + fidir = finfo->fi_hdir;
  14267. + if (!fidir) {
  14268. + AuDebugOn(finfo->fi_btop != bindex);
  14269. + hf = &finfo->fi_htop;
  14270. + } else
  14271. + hf = fidir->fd_hfile + bindex;
  14272. +
  14273. + if (hf && hf->hf_file)
  14274. + au_hfput(hf, vfsub_file_execed(file));
  14275. + if (val) {
  14276. + FiMustWriteLock(file);
  14277. + AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
  14278. + hf->hf_file = val;
  14279. + hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
  14280. + }
  14281. +}
  14282. +
  14283. +void au_update_figen(struct file *file)
  14284. +{
  14285. + atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
  14286. + /* smp_mb(); */ /* atomic_set */
  14287. +}
  14288. +
  14289. +/* ---------------------------------------------------------------------- */
  14290. +
  14291. +struct au_fidir *au_fidir_alloc(struct super_block *sb)
  14292. +{
  14293. + struct au_fidir *fidir;
  14294. + int nbr;
  14295. +
  14296. + nbr = au_sbbot(sb) + 1;
  14297. + if (nbr < 2)
  14298. + nbr = 2; /* initial allocate for 2 branches */
  14299. + fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
  14300. + if (fidir) {
  14301. + fidir->fd_bbot = -1;
  14302. + fidir->fd_nent = nbr;
  14303. + }
  14304. +
  14305. + return fidir;
  14306. +}
  14307. +
  14308. +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
  14309. +{
  14310. + int err;
  14311. + struct au_fidir *fidir, *p;
  14312. +
  14313. + AuRwMustWriteLock(&finfo->fi_rwsem);
  14314. + fidir = finfo->fi_hdir;
  14315. + AuDebugOn(!fidir);
  14316. +
  14317. + err = -ENOMEM;
  14318. + p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
  14319. + GFP_NOFS, may_shrink);
  14320. + if (p) {
  14321. + p->fd_nent = nbr;
  14322. + finfo->fi_hdir = p;
  14323. + err = 0;
  14324. + }
  14325. +
  14326. + return err;
  14327. +}
  14328. +
  14329. +/* ---------------------------------------------------------------------- */
  14330. +
  14331. +void au_finfo_fin(struct file *file)
  14332. +{
  14333. + struct au_finfo *finfo;
  14334. +
  14335. + au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
  14336. +
  14337. + finfo = au_fi(file);
  14338. + AuDebugOn(finfo->fi_hdir);
  14339. + AuRwDestroy(&finfo->fi_rwsem);
  14340. + au_cache_free_finfo(finfo);
  14341. +}
  14342. +
  14343. +void au_fi_init_once(void *_finfo)
  14344. +{
  14345. + struct au_finfo *finfo = _finfo;
  14346. +
  14347. + au_rw_init(&finfo->fi_rwsem);
  14348. +}
  14349. +
  14350. +int au_finfo_init(struct file *file, struct au_fidir *fidir)
  14351. +{
  14352. + int err;
  14353. + struct au_finfo *finfo;
  14354. + struct dentry *dentry;
  14355. +
  14356. + err = -ENOMEM;
  14357. + dentry = file->f_path.dentry;
  14358. + finfo = au_cache_alloc_finfo();
  14359. + if (unlikely(!finfo))
  14360. + goto out;
  14361. +
  14362. + err = 0;
  14363. + au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
  14364. + au_rw_write_lock(&finfo->fi_rwsem);
  14365. + finfo->fi_btop = -1;
  14366. + finfo->fi_hdir = fidir;
  14367. + atomic_set(&finfo->fi_generation, au_digen(dentry));
  14368. + /* smp_mb(); */ /* atomic_set */
  14369. +
  14370. + file->private_data = finfo;
  14371. +
  14372. +out:
  14373. + return err;
  14374. +}
  14375. --- /dev/null
  14376. +++ linux-4.19/fs/aufs/f_op.c 2018-11-20 09:35:15.020195504 +0200
  14377. @@ -0,0 +1,819 @@
  14378. +// SPDX-License-Identifier: GPL-2.0
  14379. +/*
  14380. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  14381. + *
  14382. + * This program, aufs is free software; you can redistribute it and/or modify
  14383. + * it under the terms of the GNU General Public License as published by
  14384. + * the Free Software Foundation; either version 2 of the License, or
  14385. + * (at your option) any later version.
  14386. + *
  14387. + * This program is distributed in the hope that it will be useful,
  14388. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14389. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14390. + * GNU General Public License for more details.
  14391. + *
  14392. + * You should have received a copy of the GNU General Public License
  14393. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14394. + */
  14395. +
  14396. +/*
  14397. + * file and vm operations
  14398. + */
  14399. +
  14400. +#include <linux/aio.h>
  14401. +#include <linux/fs_stack.h>
  14402. +#include <linux/mman.h>
  14403. +#include <linux/security.h>
  14404. +#include "aufs.h"
  14405. +
  14406. +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
  14407. +{
  14408. + int err;
  14409. + aufs_bindex_t bindex;
  14410. + struct dentry *dentry, *h_dentry;
  14411. + struct au_finfo *finfo;
  14412. + struct inode *h_inode;
  14413. +
  14414. + FiMustWriteLock(file);
  14415. +
  14416. + err = 0;
  14417. + dentry = file->f_path.dentry;
  14418. + AuDebugOn(IS_ERR_OR_NULL(dentry));
  14419. + finfo = au_fi(file);
  14420. + memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
  14421. + atomic_set(&finfo->fi_mmapped, 0);
  14422. + bindex = au_dbtop(dentry);
  14423. + if (!h_file) {
  14424. + h_dentry = au_h_dptr(dentry, bindex);
  14425. + err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
  14426. + if (unlikely(err))
  14427. + goto out;
  14428. + h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
  14429. + if (IS_ERR(h_file)) {
  14430. + err = PTR_ERR(h_file);
  14431. + goto out;
  14432. + }
  14433. + } else {
  14434. + h_dentry = h_file->f_path.dentry;
  14435. + err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
  14436. + if (unlikely(err))
  14437. + goto out;
  14438. + /* br ref is already inc-ed */
  14439. + }
  14440. +
  14441. + if ((flags & __O_TMPFILE)
  14442. + && !(flags & O_EXCL)) {
  14443. + h_inode = file_inode(h_file);
  14444. + spin_lock(&h_inode->i_lock);
  14445. + h_inode->i_state |= I_LINKABLE;
  14446. + spin_unlock(&h_inode->i_lock);
  14447. + }
  14448. + au_set_fbtop(file, bindex);
  14449. + au_set_h_fptr(file, bindex, h_file);
  14450. + au_update_figen(file);
  14451. + /* todo: necessary? */
  14452. + /* file->f_ra = h_file->f_ra; */
  14453. +
  14454. +out:
  14455. + return err;
  14456. +}
  14457. +
  14458. +static int aufs_open_nondir(struct inode *inode __maybe_unused,
  14459. + struct file *file)
  14460. +{
  14461. + int err;
  14462. + struct super_block *sb;
  14463. + struct au_do_open_args args = {
  14464. + .open = au_do_open_nondir
  14465. + };
  14466. +
  14467. + AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
  14468. + file, vfsub_file_flags(file), file->f_mode);
  14469. +
  14470. + sb = file->f_path.dentry->d_sb;
  14471. + si_read_lock(sb, AuLock_FLUSH);
  14472. + err = au_do_open(file, &args);
  14473. + si_read_unlock(sb);
  14474. + return err;
  14475. +}
  14476. +
  14477. +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
  14478. +{
  14479. + struct au_finfo *finfo;
  14480. + aufs_bindex_t bindex;
  14481. +
  14482. + finfo = au_fi(file);
  14483. + au_hbl_del(&finfo->fi_hlist,
  14484. + &au_sbi(file->f_path.dentry->d_sb)->si_files);
  14485. + bindex = finfo->fi_btop;
  14486. + if (bindex >= 0)
  14487. + au_set_h_fptr(file, bindex, NULL);
  14488. +
  14489. + au_finfo_fin(file);
  14490. + return 0;
  14491. +}
  14492. +
  14493. +/* ---------------------------------------------------------------------- */
  14494. +
  14495. +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
  14496. +{
  14497. + int err;
  14498. + struct file *h_file;
  14499. +
  14500. + err = 0;
  14501. + h_file = au_hf_top(file);
  14502. + if (h_file)
  14503. + err = vfsub_flush(h_file, id);
  14504. + return err;
  14505. +}
  14506. +
  14507. +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
  14508. +{
  14509. + return au_do_flush(file, id, au_do_flush_nondir);
  14510. +}
  14511. +
  14512. +/* ---------------------------------------------------------------------- */
  14513. +/*
  14514. + * read and write functions acquire [fdi]_rwsem once, but release before
  14515. + * mmap_sem. This is because to stop a race condition between mmap(2).
  14516. + * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
  14517. + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
  14518. + * read functions after [fdi]_rwsem are released, but it should be harmless.
  14519. + */
  14520. +
  14521. +/* Callers should call au_read_post() or fput() in the end */
  14522. +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
  14523. +{
  14524. + struct file *h_file;
  14525. + int err;
  14526. +
  14527. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
  14528. + if (!err) {
  14529. + di_read_unlock(file->f_path.dentry, AuLock_IR);
  14530. + h_file = au_hf_top(file);
  14531. + get_file(h_file);
  14532. + if (!keep_fi)
  14533. + fi_read_unlock(file);
  14534. + } else
  14535. + h_file = ERR_PTR(err);
  14536. +
  14537. + return h_file;
  14538. +}
  14539. +
  14540. +static void au_read_post(struct inode *inode, struct file *h_file)
  14541. +{
  14542. + /* update without lock, I don't think it a problem */
  14543. + fsstack_copy_attr_atime(inode, file_inode(h_file));
  14544. + fput(h_file);
  14545. +}
  14546. +
  14547. +struct au_write_pre {
  14548. + /* input */
  14549. + unsigned int lsc;
  14550. +
  14551. + /* output */
  14552. + blkcnt_t blks;
  14553. + aufs_bindex_t btop;
  14554. +};
  14555. +
  14556. +/*
  14557. + * return with iinfo is write-locked
  14558. + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
  14559. + * end
  14560. + */
  14561. +static struct file *au_write_pre(struct file *file, int do_ready,
  14562. + struct au_write_pre *wpre)
  14563. +{
  14564. + struct file *h_file;
  14565. + struct dentry *dentry;
  14566. + int err;
  14567. + unsigned int lsc;
  14568. + struct au_pin pin;
  14569. +
  14570. + lsc = 0;
  14571. + if (wpre)
  14572. + lsc = wpre->lsc;
  14573. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
  14574. + h_file = ERR_PTR(err);
  14575. + if (unlikely(err))
  14576. + goto out;
  14577. +
  14578. + dentry = file->f_path.dentry;
  14579. + if (do_ready) {
  14580. + err = au_ready_to_write(file, -1, &pin);
  14581. + if (unlikely(err)) {
  14582. + h_file = ERR_PTR(err);
  14583. + di_write_unlock(dentry);
  14584. + goto out_fi;
  14585. + }
  14586. + }
  14587. +
  14588. + di_downgrade_lock(dentry, /*flags*/0);
  14589. + if (wpre)
  14590. + wpre->btop = au_fbtop(file);
  14591. + h_file = au_hf_top(file);
  14592. + get_file(h_file);
  14593. + if (wpre)
  14594. + wpre->blks = file_inode(h_file)->i_blocks;
  14595. + if (do_ready)
  14596. + au_unpin(&pin);
  14597. + di_read_unlock(dentry, /*flags*/0);
  14598. +
  14599. +out_fi:
  14600. + fi_write_unlock(file);
  14601. +out:
  14602. + return h_file;
  14603. +}
  14604. +
  14605. +static void au_write_post(struct inode *inode, struct file *h_file,
  14606. + struct au_write_pre *wpre, ssize_t written)
  14607. +{
  14608. + struct inode *h_inode;
  14609. +
  14610. + au_cpup_attr_timesizes(inode);
  14611. + AuDebugOn(au_ibtop(inode) != wpre->btop);
  14612. + h_inode = file_inode(h_file);
  14613. + inode->i_mode = h_inode->i_mode;
  14614. + ii_write_unlock(inode);
  14615. + /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
  14616. + if (written > 0)
  14617. + au_fhsm_wrote(inode->i_sb, wpre->btop,
  14618. + /*force*/h_inode->i_blocks > wpre->blks);
  14619. + fput(h_file);
  14620. +}
  14621. +
  14622. +static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
  14623. + loff_t *ppos)
  14624. +{
  14625. + ssize_t err;
  14626. + struct inode *inode;
  14627. + struct file *h_file;
  14628. + struct super_block *sb;
  14629. +
  14630. + inode = file_inode(file);
  14631. + sb = inode->i_sb;
  14632. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  14633. +
  14634. + h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
  14635. + err = PTR_ERR(h_file);
  14636. + if (IS_ERR(h_file))
  14637. + goto out;
  14638. +
  14639. + /* filedata may be obsoleted by concurrent copyup, but no problem */
  14640. + err = vfsub_read_u(h_file, buf, count, ppos);
  14641. + /* todo: necessary? */
  14642. + /* file->f_ra = h_file->f_ra; */
  14643. + au_read_post(inode, h_file);
  14644. +
  14645. +out:
  14646. + si_read_unlock(sb);
  14647. + return err;
  14648. +}
  14649. +
  14650. +/*
  14651. + * todo: very ugly
  14652. + * it locks both of i_mutex and si_rwsem for read in safe.
  14653. + * if the plink maintenance mode continues forever (that is the problem),
  14654. + * may loop forever.
  14655. + */
  14656. +static void au_mtx_and_read_lock(struct inode *inode)
  14657. +{
  14658. + int err;
  14659. + struct super_block *sb = inode->i_sb;
  14660. +
  14661. + while (1) {
  14662. + inode_lock(inode);
  14663. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  14664. + if (!err)
  14665. + break;
  14666. + inode_unlock(inode);
  14667. + si_read_lock(sb, AuLock_NOPLMW);
  14668. + si_read_unlock(sb);
  14669. + }
  14670. +}
  14671. +
  14672. +static ssize_t aufs_write(struct file *file, const char __user *ubuf,
  14673. + size_t count, loff_t *ppos)
  14674. +{
  14675. + ssize_t err;
  14676. + struct au_write_pre wpre;
  14677. + struct inode *inode;
  14678. + struct file *h_file;
  14679. + char __user *buf = (char __user *)ubuf;
  14680. +
  14681. + inode = file_inode(file);
  14682. + au_mtx_and_read_lock(inode);
  14683. +
  14684. + wpre.lsc = 0;
  14685. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  14686. + err = PTR_ERR(h_file);
  14687. + if (IS_ERR(h_file))
  14688. + goto out;
  14689. +
  14690. + err = vfsub_write_u(h_file, buf, count, ppos);
  14691. + au_write_post(inode, h_file, &wpre, err);
  14692. +
  14693. +out:
  14694. + si_read_unlock(inode->i_sb);
  14695. + inode_unlock(inode);
  14696. + return err;
  14697. +}
  14698. +
  14699. +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
  14700. + struct iov_iter *iov_iter)
  14701. +{
  14702. + ssize_t err;
  14703. + struct file *file;
  14704. + ssize_t (*iter)(struct kiocb *, struct iov_iter *);
  14705. +
  14706. + err = security_file_permission(h_file, rw);
  14707. + if (unlikely(err))
  14708. + goto out;
  14709. +
  14710. + err = -ENOSYS;
  14711. + iter = NULL;
  14712. + if (rw == MAY_READ)
  14713. + iter = h_file->f_op->read_iter;
  14714. + else if (rw == MAY_WRITE)
  14715. + iter = h_file->f_op->write_iter;
  14716. +
  14717. + file = kio->ki_filp;
  14718. + kio->ki_filp = h_file;
  14719. + if (iter) {
  14720. + lockdep_off();
  14721. + err = iter(kio, iov_iter);
  14722. + lockdep_on();
  14723. + } else
  14724. + /* currently there is no such fs */
  14725. + WARN_ON_ONCE(1);
  14726. + kio->ki_filp = file;
  14727. +
  14728. +out:
  14729. + return err;
  14730. +}
  14731. +
  14732. +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
  14733. +{
  14734. + ssize_t err;
  14735. + struct file *file, *h_file;
  14736. + struct inode *inode;
  14737. + struct super_block *sb;
  14738. +
  14739. + file = kio->ki_filp;
  14740. + inode = file_inode(file);
  14741. + sb = inode->i_sb;
  14742. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  14743. +
  14744. + h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
  14745. + err = PTR_ERR(h_file);
  14746. + if (IS_ERR(h_file))
  14747. + goto out;
  14748. +
  14749. + if (au_test_loopback_kthread()) {
  14750. + au_warn_loopback(h_file->f_path.dentry->d_sb);
  14751. + if (file->f_mapping != h_file->f_mapping) {
  14752. + file->f_mapping = h_file->f_mapping;
  14753. + smp_mb(); /* unnecessary? */
  14754. + }
  14755. + }
  14756. + fi_read_unlock(file);
  14757. +
  14758. + err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
  14759. + /* todo: necessary? */
  14760. + /* file->f_ra = h_file->f_ra; */
  14761. + au_read_post(inode, h_file);
  14762. +
  14763. +out:
  14764. + si_read_unlock(sb);
  14765. + return err;
  14766. +}
  14767. +
  14768. +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
  14769. +{
  14770. + ssize_t err;
  14771. + struct au_write_pre wpre;
  14772. + struct inode *inode;
  14773. + struct file *file, *h_file;
  14774. +
  14775. + file = kio->ki_filp;
  14776. + inode = file_inode(file);
  14777. + au_mtx_and_read_lock(inode);
  14778. +
  14779. + wpre.lsc = 0;
  14780. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  14781. + err = PTR_ERR(h_file);
  14782. + if (IS_ERR(h_file))
  14783. + goto out;
  14784. +
  14785. + err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
  14786. + au_write_post(inode, h_file, &wpre, err);
  14787. +
  14788. +out:
  14789. + si_read_unlock(inode->i_sb);
  14790. + inode_unlock(inode);
  14791. + return err;
  14792. +}
  14793. +
  14794. +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
  14795. + struct pipe_inode_info *pipe, size_t len,
  14796. + unsigned int flags)
  14797. +{
  14798. + ssize_t err;
  14799. + struct file *h_file;
  14800. + struct inode *inode;
  14801. + struct super_block *sb;
  14802. +
  14803. + inode = file_inode(file);
  14804. + sb = inode->i_sb;
  14805. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  14806. +
  14807. + h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
  14808. + err = PTR_ERR(h_file);
  14809. + if (IS_ERR(h_file))
  14810. + goto out;
  14811. +
  14812. + err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
  14813. + /* todo: necessary? */
  14814. + /* file->f_ra = h_file->f_ra; */
  14815. + au_read_post(inode, h_file);
  14816. +
  14817. +out:
  14818. + si_read_unlock(sb);
  14819. + return err;
  14820. +}
  14821. +
  14822. +static ssize_t
  14823. +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
  14824. + size_t len, unsigned int flags)
  14825. +{
  14826. + ssize_t err;
  14827. + struct au_write_pre wpre;
  14828. + struct inode *inode;
  14829. + struct file *h_file;
  14830. +
  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 = vfsub_splice_from(pipe, h_file, ppos, len, flags);
  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. +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
  14850. + loff_t len)
  14851. +{
  14852. + long err;
  14853. + struct au_write_pre wpre;
  14854. + struct inode *inode;
  14855. + struct file *h_file;
  14856. +
  14857. + inode = file_inode(file);
  14858. + au_mtx_and_read_lock(inode);
  14859. +
  14860. + wpre.lsc = 0;
  14861. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  14862. + err = PTR_ERR(h_file);
  14863. + if (IS_ERR(h_file))
  14864. + goto out;
  14865. +
  14866. + lockdep_off();
  14867. + err = vfs_fallocate(h_file, mode, offset, len);
  14868. + lockdep_on();
  14869. + au_write_post(inode, h_file, &wpre, /*written*/1);
  14870. +
  14871. +out:
  14872. + si_read_unlock(inode->i_sb);
  14873. + inode_unlock(inode);
  14874. + return err;
  14875. +}
  14876. +
  14877. +static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
  14878. + struct file *dst, loff_t dst_pos,
  14879. + size_t len, unsigned int flags)
  14880. +{
  14881. + ssize_t err;
  14882. + struct au_write_pre wpre;
  14883. + enum { SRC, DST };
  14884. + struct {
  14885. + struct inode *inode;
  14886. + struct file *h_file;
  14887. + struct super_block *h_sb;
  14888. + } a[2];
  14889. +#define a_src a[SRC]
  14890. +#define a_dst a[DST]
  14891. +
  14892. + err = -EINVAL;
  14893. + a_src.inode = file_inode(src);
  14894. + if (unlikely(!S_ISREG(a_src.inode->i_mode)))
  14895. + goto out;
  14896. + a_dst.inode = file_inode(dst);
  14897. + if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
  14898. + goto out;
  14899. +
  14900. + au_mtx_and_read_lock(a_dst.inode);
  14901. + /*
  14902. + * in order to match the order in di_write_lock2_{child,parent}(),
  14903. + * use f_path.dentry for this comparison.
  14904. + */
  14905. + if (src->f_path.dentry < dst->f_path.dentry) {
  14906. + a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
  14907. + err = PTR_ERR(a_src.h_file);
  14908. + if (IS_ERR(a_src.h_file))
  14909. + goto out_si;
  14910. +
  14911. + wpre.lsc = AuLsc_FI_2;
  14912. + a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
  14913. + err = PTR_ERR(a_dst.h_file);
  14914. + if (IS_ERR(a_dst.h_file)) {
  14915. + au_read_post(a_src.inode, a_src.h_file);
  14916. + goto out_si;
  14917. + }
  14918. + } else {
  14919. + wpre.lsc = AuLsc_FI_1;
  14920. + a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
  14921. + err = PTR_ERR(a_dst.h_file);
  14922. + if (IS_ERR(a_dst.h_file))
  14923. + goto out_si;
  14924. +
  14925. + a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
  14926. + err = PTR_ERR(a_src.h_file);
  14927. + if (IS_ERR(a_src.h_file)) {
  14928. + au_write_post(a_dst.inode, a_dst.h_file, &wpre,
  14929. + /*written*/0);
  14930. + goto out_si;
  14931. + }
  14932. + }
  14933. +
  14934. + err = -EXDEV;
  14935. + a_src.h_sb = file_inode(a_src.h_file)->i_sb;
  14936. + a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
  14937. + if (unlikely(a_src.h_sb != a_dst.h_sb)) {
  14938. + AuDbgFile(src);
  14939. + AuDbgFile(dst);
  14940. + goto out_file;
  14941. + }
  14942. +
  14943. + err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
  14944. + dst_pos, len, flags);
  14945. +
  14946. +out_file:
  14947. + au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
  14948. + fi_read_unlock(src);
  14949. + au_read_post(a_src.inode, a_src.h_file);
  14950. +out_si:
  14951. + si_read_unlock(a_dst.inode->i_sb);
  14952. + inode_unlock(a_dst.inode);
  14953. +out:
  14954. + return err;
  14955. +#undef a_src
  14956. +#undef a_dst
  14957. +}
  14958. +
  14959. +/* ---------------------------------------------------------------------- */
  14960. +
  14961. +/*
  14962. + * The locking order around current->mmap_sem.
  14963. + * - in most and regular cases
  14964. + * file I/O syscall -- aufs_read() or something
  14965. + * -- si_rwsem for read -- mmap_sem
  14966. + * (Note that [fdi]i_rwsem are released before mmap_sem).
  14967. + * - in mmap case
  14968. + * mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
  14969. + * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
  14970. + * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
  14971. + * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
  14972. + * It means that when aufs acquires si_rwsem for write, the process should never
  14973. + * acquire mmap_sem.
  14974. + *
  14975. + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
  14976. + * problem either since any directory is not able to be mmap-ed.
  14977. + * The similar scenario is applied to aufs_readlink() too.
  14978. + */
  14979. +
  14980. +#if 0 /* stop calling security_file_mmap() */
  14981. +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
  14982. +#define AuConv_VM_PROT(f, b) _calc_vm_trans(f, VM_##b, PROT_##b)
  14983. +
  14984. +static unsigned long au_arch_prot_conv(unsigned long flags)
  14985. +{
  14986. + /* currently ppc64 only */
  14987. +#ifdef CONFIG_PPC64
  14988. + /* cf. linux/arch/powerpc/include/asm/mman.h */
  14989. + AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
  14990. + return AuConv_VM_PROT(flags, SAO);
  14991. +#else
  14992. + AuDebugOn(arch_calc_vm_prot_bits(-1));
  14993. + return 0;
  14994. +#endif
  14995. +}
  14996. +
  14997. +static unsigned long au_prot_conv(unsigned long flags)
  14998. +{
  14999. + return AuConv_VM_PROT(flags, READ)
  15000. + | AuConv_VM_PROT(flags, WRITE)
  15001. + | AuConv_VM_PROT(flags, EXEC)
  15002. + | au_arch_prot_conv(flags);
  15003. +}
  15004. +
  15005. +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
  15006. +#define AuConv_VM_MAP(f, b) _calc_vm_trans(f, VM_##b, MAP_##b)
  15007. +
  15008. +static unsigned long au_flag_conv(unsigned long flags)
  15009. +{
  15010. + return AuConv_VM_MAP(flags, GROWSDOWN)
  15011. + | AuConv_VM_MAP(flags, DENYWRITE)
  15012. + | AuConv_VM_MAP(flags, LOCKED);
  15013. +}
  15014. +#endif
  15015. +
  15016. +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
  15017. +{
  15018. + int err;
  15019. + const unsigned char wlock
  15020. + = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
  15021. + struct super_block *sb;
  15022. + struct file *h_file;
  15023. + struct inode *inode;
  15024. +
  15025. + AuDbgVmRegion(file, vma);
  15026. +
  15027. + inode = file_inode(file);
  15028. + sb = inode->i_sb;
  15029. + lockdep_off();
  15030. + si_read_lock(sb, AuLock_NOPLMW);
  15031. +
  15032. + h_file = au_write_pre(file, wlock, /*wpre*/NULL);
  15033. + lockdep_on();
  15034. + err = PTR_ERR(h_file);
  15035. + if (IS_ERR(h_file))
  15036. + goto out;
  15037. +
  15038. + err = 0;
  15039. + au_set_mmapped(file);
  15040. + au_vm_file_reset(vma, h_file);
  15041. + /*
  15042. + * we cannot call security_mmap_file() here since it may acquire
  15043. + * mmap_sem or i_mutex.
  15044. + *
  15045. + * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
  15046. + * au_flag_conv(vma->vm_flags));
  15047. + */
  15048. + if (!err)
  15049. + err = call_mmap(h_file, vma);
  15050. + if (!err) {
  15051. + au_vm_prfile_set(vma, file);
  15052. + fsstack_copy_attr_atime(inode, file_inode(h_file));
  15053. + goto out_fput; /* success */
  15054. + }
  15055. + au_unset_mmapped(file);
  15056. + au_vm_file_reset(vma, file);
  15057. +
  15058. +out_fput:
  15059. + lockdep_off();
  15060. + ii_write_unlock(inode);
  15061. + lockdep_on();
  15062. + fput(h_file);
  15063. +out:
  15064. + lockdep_off();
  15065. + si_read_unlock(sb);
  15066. + lockdep_on();
  15067. + AuTraceErr(err);
  15068. + return err;
  15069. +}
  15070. +
  15071. +/* ---------------------------------------------------------------------- */
  15072. +
  15073. +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
  15074. + int datasync)
  15075. +{
  15076. + int err;
  15077. + struct au_write_pre wpre;
  15078. + struct inode *inode;
  15079. + struct file *h_file;
  15080. +
  15081. + err = 0; /* -EBADF; */ /* posix? */
  15082. + if (unlikely(!(file->f_mode & FMODE_WRITE)))
  15083. + goto out;
  15084. +
  15085. + inode = file_inode(file);
  15086. + au_mtx_and_read_lock(inode);
  15087. +
  15088. + wpre.lsc = 0;
  15089. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  15090. + err = PTR_ERR(h_file);
  15091. + if (IS_ERR(h_file))
  15092. + goto out_unlock;
  15093. +
  15094. + err = vfsub_fsync(h_file, &h_file->f_path, datasync);
  15095. + au_write_post(inode, h_file, &wpre, /*written*/0);
  15096. +
  15097. +out_unlock:
  15098. + si_read_unlock(inode->i_sb);
  15099. + inode_unlock(inode);
  15100. +out:
  15101. + return err;
  15102. +}
  15103. +
  15104. +static int aufs_fasync(int fd, struct file *file, int flag)
  15105. +{
  15106. + int err;
  15107. + struct file *h_file;
  15108. + struct super_block *sb;
  15109. +
  15110. + sb = file->f_path.dentry->d_sb;
  15111. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  15112. +
  15113. + h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
  15114. + err = PTR_ERR(h_file);
  15115. + if (IS_ERR(h_file))
  15116. + goto out;
  15117. +
  15118. + if (h_file->f_op->fasync)
  15119. + err = h_file->f_op->fasync(fd, h_file, flag);
  15120. + fput(h_file); /* instead of au_read_post() */
  15121. +
  15122. +out:
  15123. + si_read_unlock(sb);
  15124. + return err;
  15125. +}
  15126. +
  15127. +static int aufs_setfl(struct file *file, unsigned long arg)
  15128. +{
  15129. + int err;
  15130. + struct file *h_file;
  15131. + struct super_block *sb;
  15132. +
  15133. + sb = file->f_path.dentry->d_sb;
  15134. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  15135. +
  15136. + h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
  15137. + err = PTR_ERR(h_file);
  15138. + if (IS_ERR(h_file))
  15139. + goto out;
  15140. +
  15141. + /* stop calling h_file->fasync */
  15142. + arg |= vfsub_file_flags(file) & FASYNC;
  15143. + err = setfl(/*unused fd*/-1, h_file, arg);
  15144. + fput(h_file); /* instead of au_read_post() */
  15145. +
  15146. +out:
  15147. + si_read_unlock(sb);
  15148. + return err;
  15149. +}
  15150. +
  15151. +/* ---------------------------------------------------------------------- */
  15152. +
  15153. +/* no one supports this operation, currently */
  15154. +#if 0
  15155. +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
  15156. + size_t len, loff_t *pos, int more)
  15157. +{
  15158. +}
  15159. +#endif
  15160. +
  15161. +/* ---------------------------------------------------------------------- */
  15162. +
  15163. +const struct file_operations aufs_file_fop = {
  15164. + .owner = THIS_MODULE,
  15165. +
  15166. + .llseek = default_llseek,
  15167. +
  15168. + .read = aufs_read,
  15169. + .write = aufs_write,
  15170. + .read_iter = aufs_read_iter,
  15171. + .write_iter = aufs_write_iter,
  15172. +
  15173. +#ifdef CONFIG_AUFS_POLL
  15174. + .poll = aufs_poll,
  15175. +#endif
  15176. + .unlocked_ioctl = aufs_ioctl_nondir,
  15177. +#ifdef CONFIG_COMPAT
  15178. + .compat_ioctl = aufs_compat_ioctl_nondir,
  15179. +#endif
  15180. + .mmap = aufs_mmap,
  15181. + .open = aufs_open_nondir,
  15182. + .flush = aufs_flush_nondir,
  15183. + .release = aufs_release_nondir,
  15184. + .fsync = aufs_fsync_nondir,
  15185. + .fasync = aufs_fasync,
  15186. + /* .sendpage = aufs_sendpage, */
  15187. + .setfl = aufs_setfl,
  15188. + .splice_write = aufs_splice_write,
  15189. + .splice_read = aufs_splice_read,
  15190. +#if 0
  15191. + .aio_splice_write = aufs_aio_splice_write,
  15192. + .aio_splice_read = aufs_aio_splice_read,
  15193. +#endif
  15194. + .fallocate = aufs_fallocate,
  15195. + .copy_file_range = aufs_copy_file_range
  15196. +};
  15197. --- /dev/null
  15198. +++ linux-4.19/fs/aufs/fstype.h 2018-11-20 09:35:15.022195504 +0200
  15199. @@ -0,0 +1,401 @@
  15200. +/* SPDX-License-Identifier: GPL-2.0 */
  15201. +/*
  15202. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  15203. + *
  15204. + * This program, aufs is free software; you can redistribute it and/or modify
  15205. + * it under the terms of the GNU General Public License as published by
  15206. + * the Free Software Foundation; either version 2 of the License, or
  15207. + * (at your option) any later version.
  15208. + *
  15209. + * This program is distributed in the hope that it will be useful,
  15210. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15211. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15212. + * GNU General Public License for more details.
  15213. + *
  15214. + * You should have received a copy of the GNU General Public License
  15215. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15216. + */
  15217. +
  15218. +/*
  15219. + * judging filesystem type
  15220. + */
  15221. +
  15222. +#ifndef __AUFS_FSTYPE_H__
  15223. +#define __AUFS_FSTYPE_H__
  15224. +
  15225. +#ifdef __KERNEL__
  15226. +
  15227. +#include <linux/fs.h>
  15228. +#include <linux/magic.h>
  15229. +#include <linux/nfs_fs.h>
  15230. +#include <linux/romfs_fs.h>
  15231. +
  15232. +static inline int au_test_aufs(struct super_block *sb)
  15233. +{
  15234. + return sb->s_magic == AUFS_SUPER_MAGIC;
  15235. +}
  15236. +
  15237. +static inline const char *au_sbtype(struct super_block *sb)
  15238. +{
  15239. + return sb->s_type->name;
  15240. +}
  15241. +
  15242. +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
  15243. +{
  15244. +#if IS_ENABLED(CONFIG_ISO9660_FS)
  15245. + return sb->s_magic == ISOFS_SUPER_MAGIC;
  15246. +#else
  15247. + return 0;
  15248. +#endif
  15249. +}
  15250. +
  15251. +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
  15252. +{
  15253. +#if IS_ENABLED(CONFIG_ROMFS_FS)
  15254. + return sb->s_magic == ROMFS_MAGIC;
  15255. +#else
  15256. + return 0;
  15257. +#endif
  15258. +}
  15259. +
  15260. +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
  15261. +{
  15262. +#if IS_ENABLED(CONFIG_CRAMFS)
  15263. + return sb->s_magic == CRAMFS_MAGIC;
  15264. +#endif
  15265. + return 0;
  15266. +}
  15267. +
  15268. +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
  15269. +{
  15270. +#if IS_ENABLED(CONFIG_NFS_FS)
  15271. + return sb->s_magic == NFS_SUPER_MAGIC;
  15272. +#else
  15273. + return 0;
  15274. +#endif
  15275. +}
  15276. +
  15277. +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
  15278. +{
  15279. +#if IS_ENABLED(CONFIG_FUSE_FS)
  15280. + return sb->s_magic == FUSE_SUPER_MAGIC;
  15281. +#else
  15282. + return 0;
  15283. +#endif
  15284. +}
  15285. +
  15286. +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
  15287. +{
  15288. +#if IS_ENABLED(CONFIG_XFS_FS)
  15289. + return sb->s_magic == XFS_SB_MAGIC;
  15290. +#else
  15291. + return 0;
  15292. +#endif
  15293. +}
  15294. +
  15295. +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
  15296. +{
  15297. +#ifdef CONFIG_TMPFS
  15298. + return sb->s_magic == TMPFS_MAGIC;
  15299. +#else
  15300. + return 0;
  15301. +#endif
  15302. +}
  15303. +
  15304. +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
  15305. +{
  15306. +#if IS_ENABLED(CONFIG_ECRYPT_FS)
  15307. + return !strcmp(au_sbtype(sb), "ecryptfs");
  15308. +#else
  15309. + return 0;
  15310. +#endif
  15311. +}
  15312. +
  15313. +static inline int au_test_ramfs(struct super_block *sb)
  15314. +{
  15315. + return sb->s_magic == RAMFS_MAGIC;
  15316. +}
  15317. +
  15318. +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
  15319. +{
  15320. +#if IS_ENABLED(CONFIG_UBIFS_FS)
  15321. + return sb->s_magic == UBIFS_SUPER_MAGIC;
  15322. +#else
  15323. + return 0;
  15324. +#endif
  15325. +}
  15326. +
  15327. +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
  15328. +{
  15329. +#ifdef CONFIG_PROC_FS
  15330. + return sb->s_magic == PROC_SUPER_MAGIC;
  15331. +#else
  15332. + return 0;
  15333. +#endif
  15334. +}
  15335. +
  15336. +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
  15337. +{
  15338. +#ifdef CONFIG_SYSFS
  15339. + return sb->s_magic == SYSFS_MAGIC;
  15340. +#else
  15341. + return 0;
  15342. +#endif
  15343. +}
  15344. +
  15345. +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
  15346. +{
  15347. +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
  15348. + return sb->s_magic == CONFIGFS_MAGIC;
  15349. +#else
  15350. + return 0;
  15351. +#endif
  15352. +}
  15353. +
  15354. +static inline int au_test_minix(struct super_block *sb __maybe_unused)
  15355. +{
  15356. +#if IS_ENABLED(CONFIG_MINIX_FS)
  15357. + return sb->s_magic == MINIX3_SUPER_MAGIC
  15358. + || sb->s_magic == MINIX2_SUPER_MAGIC
  15359. + || sb->s_magic == MINIX2_SUPER_MAGIC2
  15360. + || sb->s_magic == MINIX_SUPER_MAGIC
  15361. + || sb->s_magic == MINIX_SUPER_MAGIC2;
  15362. +#else
  15363. + return 0;
  15364. +#endif
  15365. +}
  15366. +
  15367. +static inline int au_test_fat(struct super_block *sb __maybe_unused)
  15368. +{
  15369. +#if IS_ENABLED(CONFIG_FAT_FS)
  15370. + return sb->s_magic == MSDOS_SUPER_MAGIC;
  15371. +#else
  15372. + return 0;
  15373. +#endif
  15374. +}
  15375. +
  15376. +static inline int au_test_msdos(struct super_block *sb)
  15377. +{
  15378. + return au_test_fat(sb);
  15379. +}
  15380. +
  15381. +static inline int au_test_vfat(struct super_block *sb)
  15382. +{
  15383. + return au_test_fat(sb);
  15384. +}
  15385. +
  15386. +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
  15387. +{
  15388. +#ifdef CONFIG_SECURITYFS
  15389. + return sb->s_magic == SECURITYFS_MAGIC;
  15390. +#else
  15391. + return 0;
  15392. +#endif
  15393. +}
  15394. +
  15395. +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
  15396. +{
  15397. +#if IS_ENABLED(CONFIG_SQUASHFS)
  15398. + return sb->s_magic == SQUASHFS_MAGIC;
  15399. +#else
  15400. + return 0;
  15401. +#endif
  15402. +}
  15403. +
  15404. +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
  15405. +{
  15406. +#if IS_ENABLED(CONFIG_BTRFS_FS)
  15407. + return sb->s_magic == BTRFS_SUPER_MAGIC;
  15408. +#else
  15409. + return 0;
  15410. +#endif
  15411. +}
  15412. +
  15413. +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
  15414. +{
  15415. +#if IS_ENABLED(CONFIG_XENFS)
  15416. + return sb->s_magic == XENFS_SUPER_MAGIC;
  15417. +#else
  15418. + return 0;
  15419. +#endif
  15420. +}
  15421. +
  15422. +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
  15423. +{
  15424. +#ifdef CONFIG_DEBUG_FS
  15425. + return sb->s_magic == DEBUGFS_MAGIC;
  15426. +#else
  15427. + return 0;
  15428. +#endif
  15429. +}
  15430. +
  15431. +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
  15432. +{
  15433. +#if IS_ENABLED(CONFIG_NILFS)
  15434. + return sb->s_magic == NILFS_SUPER_MAGIC;
  15435. +#else
  15436. + return 0;
  15437. +#endif
  15438. +}
  15439. +
  15440. +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
  15441. +{
  15442. +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
  15443. + return sb->s_magic == HFSPLUS_SUPER_MAGIC;
  15444. +#else
  15445. + return 0;
  15446. +#endif
  15447. +}
  15448. +
  15449. +/* ---------------------------------------------------------------------- */
  15450. +/*
  15451. + * they can't be an aufs branch.
  15452. + */
  15453. +static inline int au_test_fs_unsuppoted(struct super_block *sb)
  15454. +{
  15455. + return
  15456. +#ifndef CONFIG_AUFS_BR_RAMFS
  15457. + au_test_ramfs(sb) ||
  15458. +#endif
  15459. + au_test_procfs(sb)
  15460. + || au_test_sysfs(sb)
  15461. + || au_test_configfs(sb)
  15462. + || au_test_debugfs(sb)
  15463. + || au_test_securityfs(sb)
  15464. + || au_test_xenfs(sb)
  15465. + || au_test_ecryptfs(sb)
  15466. + /* || !strcmp(au_sbtype(sb), "unionfs") */
  15467. + || au_test_aufs(sb); /* will be supported in next version */
  15468. +}
  15469. +
  15470. +static inline int au_test_fs_remote(struct super_block *sb)
  15471. +{
  15472. + return !au_test_tmpfs(sb)
  15473. +#ifdef CONFIG_AUFS_BR_RAMFS
  15474. + && !au_test_ramfs(sb)
  15475. +#endif
  15476. + && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
  15477. +}
  15478. +
  15479. +/* ---------------------------------------------------------------------- */
  15480. +
  15481. +/*
  15482. + * Note: these functions (below) are created after reading ->getattr() in all
  15483. + * filesystems under linux/fs. it means we have to do so in every update...
  15484. + */
  15485. +
  15486. +/*
  15487. + * some filesystems require getattr to refresh the inode attributes before
  15488. + * referencing.
  15489. + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
  15490. + * and leave the work for d_revalidate()
  15491. + */
  15492. +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
  15493. +{
  15494. + return au_test_nfs(sb)
  15495. + || au_test_fuse(sb)
  15496. + /* || au_test_btrfs(sb) */ /* untested */
  15497. + ;
  15498. +}
  15499. +
  15500. +/*
  15501. + * filesystems which don't maintain i_size or i_blocks.
  15502. + */
  15503. +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
  15504. +{
  15505. + return au_test_xfs(sb)
  15506. + || au_test_btrfs(sb)
  15507. + || au_test_ubifs(sb)
  15508. + || au_test_hfsplus(sb) /* maintained, but incorrect */
  15509. + /* || au_test_minix(sb) */ /* untested */
  15510. + ;
  15511. +}
  15512. +
  15513. +/*
  15514. + * filesystems which don't store the correct value in some of their inode
  15515. + * attributes.
  15516. + */
  15517. +static inline int au_test_fs_bad_iattr(struct super_block *sb)
  15518. +{
  15519. + return au_test_fs_bad_iattr_size(sb)
  15520. + || au_test_fat(sb)
  15521. + || au_test_msdos(sb)
  15522. + || au_test_vfat(sb);
  15523. +}
  15524. +
  15525. +/* they don't check i_nlink in link(2) */
  15526. +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
  15527. +{
  15528. + return au_test_tmpfs(sb)
  15529. +#ifdef CONFIG_AUFS_BR_RAMFS
  15530. + || au_test_ramfs(sb)
  15531. +#endif
  15532. + || au_test_ubifs(sb)
  15533. + || au_test_hfsplus(sb);
  15534. +}
  15535. +
  15536. +/*
  15537. + * filesystems which sets S_NOATIME and S_NOCMTIME.
  15538. + */
  15539. +static inline int au_test_fs_notime(struct super_block *sb)
  15540. +{
  15541. + return au_test_nfs(sb)
  15542. + || au_test_fuse(sb)
  15543. + || au_test_ubifs(sb)
  15544. + ;
  15545. +}
  15546. +
  15547. +/* temporary support for i#1 in cramfs */
  15548. +static inline int au_test_fs_unique_ino(struct inode *inode)
  15549. +{
  15550. + if (au_test_cramfs(inode->i_sb))
  15551. + return inode->i_ino != 1;
  15552. + return 1;
  15553. +}
  15554. +
  15555. +/* ---------------------------------------------------------------------- */
  15556. +
  15557. +/*
  15558. + * the filesystem where the xino files placed must support i/o after unlink and
  15559. + * maintain i_size and i_blocks.
  15560. + */
  15561. +static inline int au_test_fs_bad_xino(struct super_block *sb)
  15562. +{
  15563. + return au_test_fs_remote(sb)
  15564. + || au_test_fs_bad_iattr_size(sb)
  15565. + /* don't want unnecessary work for xino */
  15566. + || au_test_aufs(sb)
  15567. + || au_test_ecryptfs(sb)
  15568. + || au_test_nilfs(sb);
  15569. +}
  15570. +
  15571. +static inline int au_test_fs_trunc_xino(struct super_block *sb)
  15572. +{
  15573. + return au_test_tmpfs(sb)
  15574. + || au_test_ramfs(sb);
  15575. +}
  15576. +
  15577. +/*
  15578. + * test if the @sb is real-readonly.
  15579. + */
  15580. +static inline int au_test_fs_rr(struct super_block *sb)
  15581. +{
  15582. + return au_test_squashfs(sb)
  15583. + || au_test_iso9660(sb)
  15584. + || au_test_cramfs(sb)
  15585. + || au_test_romfs(sb);
  15586. +}
  15587. +
  15588. +/*
  15589. + * test if the @inode is nfs with 'noacl' option
  15590. + * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
  15591. + */
  15592. +static inline int au_test_nfs_noacl(struct inode *inode)
  15593. +{
  15594. + return au_test_nfs(inode->i_sb)
  15595. + /* && IS_POSIXACL(inode) */
  15596. + && !nfs_server_capable(inode, NFS_CAP_ACLS);
  15597. +}
  15598. +
  15599. +#endif /* __KERNEL__ */
  15600. +#endif /* __AUFS_FSTYPE_H__ */
  15601. --- /dev/null
  15602. +++ linux-4.19/fs/aufs/hbl.h 2018-11-20 09:35:15.023195504 +0200
  15603. @@ -0,0 +1,65 @@
  15604. +/* SPDX-License-Identifier: GPL-2.0 */
  15605. +/*
  15606. + * Copyright (C) 2017-2018 Junjiro R. Okajima
  15607. + *
  15608. + * This program, aufs is free software; you can redistribute it and/or modify
  15609. + * it under the terms of the GNU General Public License as published by
  15610. + * the Free Software Foundation; either version 2 of the License, or
  15611. + * (at your option) any later version.
  15612. + *
  15613. + * This program is distributed in the hope that it will be useful,
  15614. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15615. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15616. + * GNU General Public License for more details.
  15617. + *
  15618. + * You should have received a copy of the GNU General Public License
  15619. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15620. + */
  15621. +
  15622. +/*
  15623. + * helpers for hlist_bl.h
  15624. + */
  15625. +
  15626. +#ifndef __AUFS_HBL_H__
  15627. +#define __AUFS_HBL_H__
  15628. +
  15629. +#ifdef __KERNEL__
  15630. +
  15631. +#include <linux/list_bl.h>
  15632. +
  15633. +static inline void au_hbl_add(struct hlist_bl_node *node,
  15634. + struct hlist_bl_head *hbl)
  15635. +{
  15636. + hlist_bl_lock(hbl);
  15637. + hlist_bl_add_head(node, hbl);
  15638. + hlist_bl_unlock(hbl);
  15639. +}
  15640. +
  15641. +static inline void au_hbl_del(struct hlist_bl_node *node,
  15642. + struct hlist_bl_head *hbl)
  15643. +{
  15644. + hlist_bl_lock(hbl);
  15645. + hlist_bl_del(node);
  15646. + hlist_bl_unlock(hbl);
  15647. +}
  15648. +
  15649. +#define au_hbl_for_each(pos, head) \
  15650. + for (pos = hlist_bl_first(head); \
  15651. + pos; \
  15652. + pos = pos->next)
  15653. +
  15654. +static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
  15655. +{
  15656. + unsigned long cnt;
  15657. + struct hlist_bl_node *pos;
  15658. +
  15659. + cnt = 0;
  15660. + hlist_bl_lock(hbl);
  15661. + au_hbl_for_each(pos, hbl)
  15662. + cnt++;
  15663. + hlist_bl_unlock(hbl);
  15664. + return cnt;
  15665. +}
  15666. +
  15667. +#endif /* __KERNEL__ */
  15668. +#endif /* __AUFS_HBL_H__ */
  15669. --- /dev/null
  15670. +++ linux-4.19/fs/aufs/hfsnotify.c 2018-11-20 09:35:15.023195504 +0200
  15671. @@ -0,0 +1,289 @@
  15672. +// SPDX-License-Identifier: GPL-2.0
  15673. +/*
  15674. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  15675. + *
  15676. + * This program, aufs is free software; you can redistribute it and/or modify
  15677. + * it under the terms of the GNU General Public License as published by
  15678. + * the Free Software Foundation; either version 2 of the License, or
  15679. + * (at your option) any later version.
  15680. + *
  15681. + * This program is distributed in the hope that it will be useful,
  15682. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15683. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15684. + * GNU General Public License for more details.
  15685. + *
  15686. + * You should have received a copy of the GNU General Public License
  15687. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15688. + */
  15689. +
  15690. +/*
  15691. + * fsnotify for the lower directories
  15692. + */
  15693. +
  15694. +#include "aufs.h"
  15695. +
  15696. +/* FS_IN_IGNORED is unnecessary */
  15697. +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
  15698. + | FS_CREATE | FS_EVENT_ON_CHILD);
  15699. +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
  15700. +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
  15701. +
  15702. +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
  15703. +{
  15704. + struct au_hnotify *hn = container_of(mark, struct au_hnotify,
  15705. + hn_mark);
  15706. + /* AuDbg("here\n"); */
  15707. + au_cache_free_hnotify(hn);
  15708. + smp_mb__before_atomic(); /* for atomic64_dec */
  15709. + if (atomic64_dec_and_test(&au_hfsn_ifree))
  15710. + wake_up(&au_hfsn_wq);
  15711. +}
  15712. +
  15713. +static int au_hfsn_alloc(struct au_hinode *hinode)
  15714. +{
  15715. + int err;
  15716. + struct au_hnotify *hn;
  15717. + struct super_block *sb;
  15718. + struct au_branch *br;
  15719. + struct fsnotify_mark *mark;
  15720. + aufs_bindex_t bindex;
  15721. +
  15722. + hn = hinode->hi_notify;
  15723. + sb = hn->hn_aufs_inode->i_sb;
  15724. + bindex = au_br_index(sb, hinode->hi_id);
  15725. + br = au_sbr(sb, bindex);
  15726. + AuDebugOn(!br->br_hfsn);
  15727. +
  15728. + mark = &hn->hn_mark;
  15729. + fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
  15730. + mark->mask = AuHfsnMask;
  15731. + /*
  15732. + * by udba rename or rmdir, aufs assign a new inode to the known
  15733. + * h_inode, so specify 1 to allow dups.
  15734. + */
  15735. + lockdep_off();
  15736. + err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
  15737. + lockdep_on();
  15738. +
  15739. + return err;
  15740. +}
  15741. +
  15742. +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
  15743. +{
  15744. + struct fsnotify_mark *mark;
  15745. + unsigned long long ull;
  15746. + struct fsnotify_group *group;
  15747. +
  15748. + ull = atomic64_inc_return(&au_hfsn_ifree);
  15749. + BUG_ON(!ull);
  15750. +
  15751. + mark = &hn->hn_mark;
  15752. + spin_lock(&mark->lock);
  15753. + group = mark->group;
  15754. + fsnotify_get_group(group);
  15755. + spin_unlock(&mark->lock);
  15756. + lockdep_off();
  15757. + fsnotify_destroy_mark(mark, group);
  15758. + fsnotify_put_mark(mark);
  15759. + fsnotify_put_group(group);
  15760. + lockdep_on();
  15761. +
  15762. + /* free hn by myself */
  15763. + return 0;
  15764. +}
  15765. +
  15766. +/* ---------------------------------------------------------------------- */
  15767. +
  15768. +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
  15769. +{
  15770. + struct fsnotify_mark *mark;
  15771. +
  15772. + mark = &hinode->hi_notify->hn_mark;
  15773. + spin_lock(&mark->lock);
  15774. + if (do_set) {
  15775. + AuDebugOn(mark->mask & AuHfsnMask);
  15776. + mark->mask |= AuHfsnMask;
  15777. + } else {
  15778. + AuDebugOn(!(mark->mask & AuHfsnMask));
  15779. + mark->mask &= ~AuHfsnMask;
  15780. + }
  15781. + spin_unlock(&mark->lock);
  15782. + /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
  15783. +}
  15784. +
  15785. +/* ---------------------------------------------------------------------- */
  15786. +
  15787. +/* #define AuDbgHnotify */
  15788. +#ifdef AuDbgHnotify
  15789. +static char *au_hfsn_name(u32 mask)
  15790. +{
  15791. +#ifdef CONFIG_AUFS_DEBUG
  15792. +#define test_ret(flag) \
  15793. + do { \
  15794. + if (mask & flag) \
  15795. + return #flag; \
  15796. + } while (0)
  15797. + test_ret(FS_ACCESS);
  15798. + test_ret(FS_MODIFY);
  15799. + test_ret(FS_ATTRIB);
  15800. + test_ret(FS_CLOSE_WRITE);
  15801. + test_ret(FS_CLOSE_NOWRITE);
  15802. + test_ret(FS_OPEN);
  15803. + test_ret(FS_MOVED_FROM);
  15804. + test_ret(FS_MOVED_TO);
  15805. + test_ret(FS_CREATE);
  15806. + test_ret(FS_DELETE);
  15807. + test_ret(FS_DELETE_SELF);
  15808. + test_ret(FS_MOVE_SELF);
  15809. + test_ret(FS_UNMOUNT);
  15810. + test_ret(FS_Q_OVERFLOW);
  15811. + test_ret(FS_IN_IGNORED);
  15812. + test_ret(FS_ISDIR);
  15813. + test_ret(FS_IN_ONESHOT);
  15814. + test_ret(FS_EVENT_ON_CHILD);
  15815. + return "";
  15816. +#undef test_ret
  15817. +#else
  15818. + return "??";
  15819. +#endif
  15820. +}
  15821. +#endif
  15822. +
  15823. +/* ---------------------------------------------------------------------- */
  15824. +
  15825. +static void au_hfsn_free_group(struct fsnotify_group *group)
  15826. +{
  15827. + struct au_br_hfsnotify *hfsn = group->private;
  15828. +
  15829. + /* AuDbg("here\n"); */
  15830. + kfree(hfsn);
  15831. +}
  15832. +
  15833. +static int au_hfsn_handle_event(struct fsnotify_group *group,
  15834. + struct inode *inode,
  15835. + u32 mask, const void *data, int data_type,
  15836. + const unsigned char *file_name, u32 cookie,
  15837. + struct fsnotify_iter_info *iter_info)
  15838. +{
  15839. + int err;
  15840. + struct au_hnotify *hnotify;
  15841. + struct inode *h_dir, *h_inode;
  15842. + struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
  15843. + struct fsnotify_mark *inode_mark;
  15844. +
  15845. + AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
  15846. +
  15847. + err = 0;
  15848. + /* if FS_UNMOUNT happens, there must be another bug */
  15849. + AuDebugOn(mask & FS_UNMOUNT);
  15850. + if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
  15851. + goto out;
  15852. +
  15853. + h_dir = inode;
  15854. + h_inode = NULL;
  15855. +#ifdef AuDbgHnotify
  15856. + au_debug_on();
  15857. + if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
  15858. + || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
  15859. + AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
  15860. + h_dir->i_ino, mask, au_hfsn_name(mask),
  15861. + AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
  15862. + /* WARN_ON(1); */
  15863. + }
  15864. + au_debug_off();
  15865. +#endif
  15866. +
  15867. + inode_mark = fsnotify_iter_inode_mark(iter_info);
  15868. + AuDebugOn(!inode_mark);
  15869. + hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
  15870. + err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
  15871. +
  15872. +out:
  15873. + return err;
  15874. +}
  15875. +
  15876. +static struct fsnotify_ops au_hfsn_ops = {
  15877. + .handle_event = au_hfsn_handle_event,
  15878. + .free_group_priv = au_hfsn_free_group,
  15879. + .free_mark = au_hfsn_free_mark
  15880. +};
  15881. +
  15882. +/* ---------------------------------------------------------------------- */
  15883. +
  15884. +static void au_hfsn_fin_br(struct au_branch *br)
  15885. +{
  15886. + struct au_br_hfsnotify *hfsn;
  15887. +
  15888. + hfsn = br->br_hfsn;
  15889. + if (hfsn) {
  15890. + lockdep_off();
  15891. + fsnotify_put_group(hfsn->hfsn_group);
  15892. + lockdep_on();
  15893. + }
  15894. +}
  15895. +
  15896. +static int au_hfsn_init_br(struct au_branch *br, int perm)
  15897. +{
  15898. + int err;
  15899. + struct fsnotify_group *group;
  15900. + struct au_br_hfsnotify *hfsn;
  15901. +
  15902. + err = 0;
  15903. + br->br_hfsn = NULL;
  15904. + if (!au_br_hnotifyable(perm))
  15905. + goto out;
  15906. +
  15907. + err = -ENOMEM;
  15908. + hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
  15909. + if (unlikely(!hfsn))
  15910. + goto out;
  15911. +
  15912. + err = 0;
  15913. + group = fsnotify_alloc_group(&au_hfsn_ops);
  15914. + if (IS_ERR(group)) {
  15915. + err = PTR_ERR(group);
  15916. + pr_err("fsnotify_alloc_group() failed, %d\n", err);
  15917. + goto out_hfsn;
  15918. + }
  15919. +
  15920. + group->private = hfsn;
  15921. + hfsn->hfsn_group = group;
  15922. + br->br_hfsn = hfsn;
  15923. + goto out; /* success */
  15924. +
  15925. +out_hfsn:
  15926. + kfree(hfsn);
  15927. +out:
  15928. + return err;
  15929. +}
  15930. +
  15931. +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
  15932. +{
  15933. + int err;
  15934. +
  15935. + err = 0;
  15936. + if (!br->br_hfsn)
  15937. + err = au_hfsn_init_br(br, perm);
  15938. +
  15939. + return err;
  15940. +}
  15941. +
  15942. +/* ---------------------------------------------------------------------- */
  15943. +
  15944. +static void au_hfsn_fin(void)
  15945. +{
  15946. + AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
  15947. + wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
  15948. +}
  15949. +
  15950. +const struct au_hnotify_op au_hnotify_op = {
  15951. + .ctl = au_hfsn_ctl,
  15952. + .alloc = au_hfsn_alloc,
  15953. + .free = au_hfsn_free,
  15954. +
  15955. + .fin = au_hfsn_fin,
  15956. +
  15957. + .reset_br = au_hfsn_reset_br,
  15958. + .fin_br = au_hfsn_fin_br,
  15959. + .init_br = au_hfsn_init_br
  15960. +};
  15961. --- /dev/null
  15962. +++ linux-4.19/fs/aufs/hfsplus.c 2018-11-20 09:35:15.023195504 +0200
  15963. @@ -0,0 +1,60 @@
  15964. +// SPDX-License-Identifier: GPL-2.0
  15965. +/*
  15966. + * Copyright (C) 2010-2018 Junjiro R. Okajima
  15967. + *
  15968. + * This program, aufs is free software; you can redistribute it and/or modify
  15969. + * it under the terms of the GNU General Public License as published by
  15970. + * the Free Software Foundation; either version 2 of the License, or
  15971. + * (at your option) any later version.
  15972. + *
  15973. + * This program is distributed in the hope that it will be useful,
  15974. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15975. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15976. + * GNU General Public License for more details.
  15977. + *
  15978. + * You should have received a copy of the GNU General Public License
  15979. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15980. + */
  15981. +
  15982. +/*
  15983. + * special support for filesystems which acquires an inode mutex
  15984. + * at final closing a file, eg, hfsplus.
  15985. + *
  15986. + * This trick is very simple and stupid, just to open the file before really
  15987. + * necessary open to tell hfsplus that this is not the final closing.
  15988. + * The caller should call au_h_open_pre() after acquiring the inode mutex,
  15989. + * and au_h_open_post() after releasing it.
  15990. + */
  15991. +
  15992. +#include "aufs.h"
  15993. +
  15994. +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
  15995. + int force_wr)
  15996. +{
  15997. + struct file *h_file;
  15998. + struct dentry *h_dentry;
  15999. +
  16000. + h_dentry = au_h_dptr(dentry, bindex);
  16001. + AuDebugOn(!h_dentry);
  16002. + AuDebugOn(d_is_negative(h_dentry));
  16003. +
  16004. + h_file = NULL;
  16005. + if (au_test_hfsplus(h_dentry->d_sb)
  16006. + && d_is_reg(h_dentry))
  16007. + h_file = au_h_open(dentry, bindex,
  16008. + O_RDONLY | O_NOATIME | O_LARGEFILE,
  16009. + /*file*/NULL, force_wr);
  16010. + return h_file;
  16011. +}
  16012. +
  16013. +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
  16014. + struct file *h_file)
  16015. +{
  16016. + struct au_branch *br;
  16017. +
  16018. + if (h_file) {
  16019. + fput(h_file);
  16020. + br = au_sbr(dentry->d_sb, bindex);
  16021. + au_lcnt_dec(&br->br_nfiles);
  16022. + }
  16023. +}
  16024. --- /dev/null
  16025. +++ linux-4.19/fs/aufs/hnotify.c 2018-11-20 09:35:15.024195504 +0200
  16026. @@ -0,0 +1,720 @@
  16027. +// SPDX-License-Identifier: GPL-2.0
  16028. +/*
  16029. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  16030. + *
  16031. + * This program, aufs is free software; you can redistribute it and/or modify
  16032. + * it under the terms of the GNU General Public License as published by
  16033. + * the Free Software Foundation; either version 2 of the License, or
  16034. + * (at your option) any later version.
  16035. + *
  16036. + * This program is distributed in the hope that it will be useful,
  16037. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16038. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16039. + * GNU General Public License for more details.
  16040. + *
  16041. + * You should have received a copy of the GNU General Public License
  16042. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16043. + */
  16044. +
  16045. +/*
  16046. + * abstraction to notify the direct changes on lower directories
  16047. + */
  16048. +
  16049. +#include "aufs.h"
  16050. +
  16051. +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
  16052. +{
  16053. + int err;
  16054. + struct au_hnotify *hn;
  16055. +
  16056. + err = -ENOMEM;
  16057. + hn = au_cache_alloc_hnotify();
  16058. + if (hn) {
  16059. + hn->hn_aufs_inode = inode;
  16060. + hinode->hi_notify = hn;
  16061. + err = au_hnotify_op.alloc(hinode);
  16062. + AuTraceErr(err);
  16063. + if (unlikely(err)) {
  16064. + hinode->hi_notify = NULL;
  16065. + au_cache_free_hnotify(hn);
  16066. + /*
  16067. + * The upper dir was removed by udba, but the same named
  16068. + * dir left. In this case, aufs assigns a new inode
  16069. + * number and set the monitor again.
  16070. + * For the lower dir, the old monitor is still left.
  16071. + */
  16072. + if (err == -EEXIST)
  16073. + err = 0;
  16074. + }
  16075. + }
  16076. +
  16077. + AuTraceErr(err);
  16078. + return err;
  16079. +}
  16080. +
  16081. +void au_hn_free(struct au_hinode *hinode)
  16082. +{
  16083. + struct au_hnotify *hn;
  16084. +
  16085. + hn = hinode->hi_notify;
  16086. + if (hn) {
  16087. + hinode->hi_notify = NULL;
  16088. + if (au_hnotify_op.free(hinode, hn))
  16089. + au_cache_free_hnotify(hn);
  16090. + }
  16091. +}
  16092. +
  16093. +/* ---------------------------------------------------------------------- */
  16094. +
  16095. +void au_hn_ctl(struct au_hinode *hinode, int do_set)
  16096. +{
  16097. + if (hinode->hi_notify)
  16098. + au_hnotify_op.ctl(hinode, do_set);
  16099. +}
  16100. +
  16101. +void au_hn_reset(struct inode *inode, unsigned int flags)
  16102. +{
  16103. + aufs_bindex_t bindex, bbot;
  16104. + struct inode *hi;
  16105. + struct dentry *iwhdentry;
  16106. +
  16107. + bbot = au_ibbot(inode);
  16108. + for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
  16109. + hi = au_h_iptr(inode, bindex);
  16110. + if (!hi)
  16111. + continue;
  16112. +
  16113. + /* inode_lock_nested(hi, AuLsc_I_CHILD); */
  16114. + iwhdentry = au_hi_wh(inode, bindex);
  16115. + if (iwhdentry)
  16116. + dget(iwhdentry);
  16117. + au_igrab(hi);
  16118. + au_set_h_iptr(inode, bindex, NULL, 0);
  16119. + au_set_h_iptr(inode, bindex, au_igrab(hi),
  16120. + flags & ~AuHi_XINO);
  16121. + iput(hi);
  16122. + dput(iwhdentry);
  16123. + /* inode_unlock(hi); */
  16124. + }
  16125. +}
  16126. +
  16127. +/* ---------------------------------------------------------------------- */
  16128. +
  16129. +static int hn_xino(struct inode *inode, struct inode *h_inode)
  16130. +{
  16131. + int err;
  16132. + aufs_bindex_t bindex, bbot, bfound, btop;
  16133. + struct inode *h_i;
  16134. +
  16135. + err = 0;
  16136. + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
  16137. + pr_warn("branch root dir was changed\n");
  16138. + goto out;
  16139. + }
  16140. +
  16141. + bfound = -1;
  16142. + bbot = au_ibbot(inode);
  16143. + btop = au_ibtop(inode);
  16144. +#if 0 /* reserved for future use */
  16145. + if (bindex == bbot) {
  16146. + /* keep this ino in rename case */
  16147. + goto out;
  16148. + }
  16149. +#endif
  16150. + for (bindex = btop; bindex <= bbot; bindex++)
  16151. + if (au_h_iptr(inode, bindex) == h_inode) {
  16152. + bfound = bindex;
  16153. + break;
  16154. + }
  16155. + if (bfound < 0)
  16156. + goto out;
  16157. +
  16158. + for (bindex = btop; bindex <= bbot; bindex++) {
  16159. + h_i = au_h_iptr(inode, bindex);
  16160. + if (!h_i)
  16161. + continue;
  16162. +
  16163. + err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
  16164. + /* ignore this error */
  16165. + /* bad action? */
  16166. + }
  16167. +
  16168. + /* children inode number will be broken */
  16169. +
  16170. +out:
  16171. + AuTraceErr(err);
  16172. + return err;
  16173. +}
  16174. +
  16175. +static int hn_gen_tree(struct dentry *dentry)
  16176. +{
  16177. + int err, i, j, ndentry;
  16178. + struct au_dcsub_pages dpages;
  16179. + struct au_dpage *dpage;
  16180. + struct dentry **dentries;
  16181. +
  16182. + err = au_dpages_init(&dpages, GFP_NOFS);
  16183. + if (unlikely(err))
  16184. + goto out;
  16185. + err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
  16186. + if (unlikely(err))
  16187. + goto out_dpages;
  16188. +
  16189. + for (i = 0; i < dpages.ndpage; i++) {
  16190. + dpage = dpages.dpages + i;
  16191. + dentries = dpage->dentries;
  16192. + ndentry = dpage->ndentry;
  16193. + for (j = 0; j < ndentry; j++) {
  16194. + struct dentry *d;
  16195. +
  16196. + d = dentries[j];
  16197. + if (IS_ROOT(d))
  16198. + continue;
  16199. +
  16200. + au_digen_dec(d);
  16201. + if (d_really_is_positive(d))
  16202. + /* todo: reset children xino?
  16203. + cached children only? */
  16204. + au_iigen_dec(d_inode(d));
  16205. + }
  16206. + }
  16207. +
  16208. +out_dpages:
  16209. + au_dpages_free(&dpages);
  16210. +
  16211. +#if 0
  16212. + /* discard children */
  16213. + dentry_unhash(dentry);
  16214. + dput(dentry);
  16215. +#endif
  16216. +out:
  16217. + return err;
  16218. +}
  16219. +
  16220. +/*
  16221. + * return 0 if processed.
  16222. + */
  16223. +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
  16224. + const unsigned int isdir)
  16225. +{
  16226. + int err;
  16227. + struct dentry *d;
  16228. + struct qstr *dname;
  16229. +
  16230. + err = 1;
  16231. + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
  16232. + pr_warn("branch root dir was changed\n");
  16233. + err = 0;
  16234. + goto out;
  16235. + }
  16236. +
  16237. + if (!isdir) {
  16238. + AuDebugOn(!name);
  16239. + au_iigen_dec(inode);
  16240. + spin_lock(&inode->i_lock);
  16241. + hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
  16242. + spin_lock(&d->d_lock);
  16243. + dname = &d->d_name;
  16244. + if (dname->len != nlen
  16245. + && memcmp(dname->name, name, nlen)) {
  16246. + spin_unlock(&d->d_lock);
  16247. + continue;
  16248. + }
  16249. + err = 0;
  16250. + au_digen_dec(d);
  16251. + spin_unlock(&d->d_lock);
  16252. + break;
  16253. + }
  16254. + spin_unlock(&inode->i_lock);
  16255. + } else {
  16256. + au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
  16257. + d = d_find_any_alias(inode);
  16258. + if (!d) {
  16259. + au_iigen_dec(inode);
  16260. + goto out;
  16261. + }
  16262. +
  16263. + spin_lock(&d->d_lock);
  16264. + dname = &d->d_name;
  16265. + if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
  16266. + spin_unlock(&d->d_lock);
  16267. + err = hn_gen_tree(d);
  16268. + spin_lock(&d->d_lock);
  16269. + }
  16270. + spin_unlock(&d->d_lock);
  16271. + dput(d);
  16272. + }
  16273. +
  16274. +out:
  16275. + AuTraceErr(err);
  16276. + return err;
  16277. +}
  16278. +
  16279. +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
  16280. +{
  16281. + int err;
  16282. +
  16283. + if (IS_ROOT(dentry)) {
  16284. + pr_warn("branch root dir was changed\n");
  16285. + return 0;
  16286. + }
  16287. +
  16288. + err = 0;
  16289. + if (!isdir) {
  16290. + au_digen_dec(dentry);
  16291. + if (d_really_is_positive(dentry))
  16292. + au_iigen_dec(d_inode(dentry));
  16293. + } else {
  16294. + au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
  16295. + if (d_really_is_positive(dentry))
  16296. + err = hn_gen_tree(dentry);
  16297. + }
  16298. +
  16299. + AuTraceErr(err);
  16300. + return err;
  16301. +}
  16302. +
  16303. +/* ---------------------------------------------------------------------- */
  16304. +
  16305. +/* hnotify job flags */
  16306. +#define AuHnJob_XINO0 1
  16307. +#define AuHnJob_GEN (1 << 1)
  16308. +#define AuHnJob_DIRENT (1 << 2)
  16309. +#define AuHnJob_ISDIR (1 << 3)
  16310. +#define AuHnJob_TRYXINO0 (1 << 4)
  16311. +#define AuHnJob_MNTPNT (1 << 5)
  16312. +#define au_ftest_hnjob(flags, name) ((flags) & AuHnJob_##name)
  16313. +#define au_fset_hnjob(flags, name) \
  16314. + do { (flags) |= AuHnJob_##name; } while (0)
  16315. +#define au_fclr_hnjob(flags, name) \
  16316. + do { (flags) &= ~AuHnJob_##name; } while (0)
  16317. +
  16318. +enum {
  16319. + AuHn_CHILD,
  16320. + AuHn_PARENT,
  16321. + AuHnLast
  16322. +};
  16323. +
  16324. +struct au_hnotify_args {
  16325. + struct inode *h_dir, *dir, *h_child_inode;
  16326. + u32 mask;
  16327. + unsigned int flags[AuHnLast];
  16328. + unsigned int h_child_nlen;
  16329. + char h_child_name[];
  16330. +};
  16331. +
  16332. +struct hn_job_args {
  16333. + unsigned int flags;
  16334. + struct inode *inode, *h_inode, *dir, *h_dir;
  16335. + struct dentry *dentry;
  16336. + char *h_name;
  16337. + int h_nlen;
  16338. +};
  16339. +
  16340. +static int hn_job(struct hn_job_args *a)
  16341. +{
  16342. + const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
  16343. + int e;
  16344. +
  16345. + /* reset xino */
  16346. + if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
  16347. + hn_xino(a->inode, a->h_inode); /* ignore this error */
  16348. +
  16349. + if (au_ftest_hnjob(a->flags, TRYXINO0)
  16350. + && a->inode
  16351. + && a->h_inode) {
  16352. + inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
  16353. + if (!a->h_inode->i_nlink
  16354. + && !(a->h_inode->i_state & I_LINKABLE))
  16355. + hn_xino(a->inode, a->h_inode); /* ignore this error */
  16356. + inode_unlock_shared(a->h_inode);
  16357. + }
  16358. +
  16359. + /* make the generation obsolete */
  16360. + if (au_ftest_hnjob(a->flags, GEN)) {
  16361. + e = -1;
  16362. + if (a->inode)
  16363. + e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
  16364. + isdir);
  16365. + if (e && a->dentry)
  16366. + hn_gen_by_name(a->dentry, isdir);
  16367. + /* ignore this error */
  16368. + }
  16369. +
  16370. + /* make dir entries obsolete */
  16371. + if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
  16372. + struct au_vdir *vdir;
  16373. +
  16374. + vdir = au_ivdir(a->inode);
  16375. + if (vdir)
  16376. + vdir->vd_jiffy = 0;
  16377. + /* IMustLock(a->inode); */
  16378. + /* inode_inc_iversion(a->inode); */
  16379. + }
  16380. +
  16381. + /* can do nothing but warn */
  16382. + if (au_ftest_hnjob(a->flags, MNTPNT)
  16383. + && a->dentry
  16384. + && d_mountpoint(a->dentry))
  16385. + pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
  16386. +
  16387. + return 0;
  16388. +}
  16389. +
  16390. +/* ---------------------------------------------------------------------- */
  16391. +
  16392. +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
  16393. + struct inode *dir)
  16394. +{
  16395. + struct dentry *dentry, *d, *parent;
  16396. + struct qstr *dname;
  16397. +
  16398. + parent = d_find_any_alias(dir);
  16399. + if (!parent)
  16400. + return NULL;
  16401. +
  16402. + dentry = NULL;
  16403. + spin_lock(&parent->d_lock);
  16404. + list_for_each_entry(d, &parent->d_subdirs, d_child) {
  16405. + /* AuDbg("%pd\n", d); */
  16406. + spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
  16407. + dname = &d->d_name;
  16408. + if (dname->len != nlen || memcmp(dname->name, name, nlen))
  16409. + goto cont_unlock;
  16410. + if (au_di(d))
  16411. + au_digen_dec(d);
  16412. + else
  16413. + goto cont_unlock;
  16414. + if (au_dcount(d) > 0) {
  16415. + dentry = dget_dlock(d);
  16416. + spin_unlock(&d->d_lock);
  16417. + break;
  16418. + }
  16419. +
  16420. +cont_unlock:
  16421. + spin_unlock(&d->d_lock);
  16422. + }
  16423. + spin_unlock(&parent->d_lock);
  16424. + dput(parent);
  16425. +
  16426. + if (dentry)
  16427. + di_write_lock_child(dentry);
  16428. +
  16429. + return dentry;
  16430. +}
  16431. +
  16432. +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
  16433. + aufs_bindex_t bindex, ino_t h_ino)
  16434. +{
  16435. + struct inode *inode;
  16436. + ino_t ino;
  16437. + int err;
  16438. +
  16439. + inode = NULL;
  16440. + err = au_xino_read(sb, bindex, h_ino, &ino);
  16441. + if (!err && ino)
  16442. + inode = ilookup(sb, ino);
  16443. + if (!inode)
  16444. + goto out;
  16445. +
  16446. + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
  16447. + pr_warn("wrong root branch\n");
  16448. + iput(inode);
  16449. + inode = NULL;
  16450. + goto out;
  16451. + }
  16452. +
  16453. + ii_write_lock_child(inode);
  16454. +
  16455. +out:
  16456. + return inode;
  16457. +}
  16458. +
  16459. +static void au_hn_bh(void *_args)
  16460. +{
  16461. + struct au_hnotify_args *a = _args;
  16462. + struct super_block *sb;
  16463. + aufs_bindex_t bindex, bbot, bfound;
  16464. + unsigned char xino, try_iput;
  16465. + int err;
  16466. + struct inode *inode;
  16467. + ino_t h_ino;
  16468. + struct hn_job_args args;
  16469. + struct dentry *dentry;
  16470. + struct au_sbinfo *sbinfo;
  16471. +
  16472. + AuDebugOn(!_args);
  16473. + AuDebugOn(!a->h_dir);
  16474. + AuDebugOn(!a->dir);
  16475. + AuDebugOn(!a->mask);
  16476. + AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
  16477. + a->mask, a->dir->i_ino, a->h_dir->i_ino,
  16478. + a->h_child_inode ? a->h_child_inode->i_ino : 0);
  16479. +
  16480. + inode = NULL;
  16481. + dentry = NULL;
  16482. + /*
  16483. + * do not lock a->dir->i_mutex here
  16484. + * because of d_revalidate() may cause a deadlock.
  16485. + */
  16486. + sb = a->dir->i_sb;
  16487. + AuDebugOn(!sb);
  16488. + sbinfo = au_sbi(sb);
  16489. + AuDebugOn(!sbinfo);
  16490. + si_write_lock(sb, AuLock_NOPLMW);
  16491. +
  16492. + if (au_opt_test(sbinfo->si_mntflags, DIRREN))
  16493. + switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
  16494. + case FS_MOVED_FROM:
  16495. + case FS_MOVED_TO:
  16496. + AuWarn1("DIRREN with UDBA may not work correctly "
  16497. + "for the direct rename(2)\n");
  16498. + }
  16499. +
  16500. + ii_read_lock_parent(a->dir);
  16501. + bfound = -1;
  16502. + bbot = au_ibbot(a->dir);
  16503. + for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
  16504. + if (au_h_iptr(a->dir, bindex) == a->h_dir) {
  16505. + bfound = bindex;
  16506. + break;
  16507. + }
  16508. + ii_read_unlock(a->dir);
  16509. + if (unlikely(bfound < 0))
  16510. + goto out;
  16511. +
  16512. + xino = !!au_opt_test(au_mntflags(sb), XINO);
  16513. + h_ino = 0;
  16514. + if (a->h_child_inode)
  16515. + h_ino = a->h_child_inode->i_ino;
  16516. +
  16517. + if (a->h_child_nlen
  16518. + && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
  16519. + || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
  16520. + dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
  16521. + a->dir);
  16522. + try_iput = 0;
  16523. + if (dentry && d_really_is_positive(dentry))
  16524. + inode = d_inode(dentry);
  16525. + if (xino && !inode && h_ino
  16526. + && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
  16527. + || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
  16528. + || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
  16529. + inode = lookup_wlock_by_ino(sb, bfound, h_ino);
  16530. + try_iput = 1;
  16531. + }
  16532. +
  16533. + args.flags = a->flags[AuHn_CHILD];
  16534. + args.dentry = dentry;
  16535. + args.inode = inode;
  16536. + args.h_inode = a->h_child_inode;
  16537. + args.dir = a->dir;
  16538. + args.h_dir = a->h_dir;
  16539. + args.h_name = a->h_child_name;
  16540. + args.h_nlen = a->h_child_nlen;
  16541. + err = hn_job(&args);
  16542. + if (dentry) {
  16543. + if (au_di(dentry))
  16544. + di_write_unlock(dentry);
  16545. + dput(dentry);
  16546. + }
  16547. + if (inode && try_iput) {
  16548. + ii_write_unlock(inode);
  16549. + iput(inode);
  16550. + }
  16551. +
  16552. + ii_write_lock_parent(a->dir);
  16553. + args.flags = a->flags[AuHn_PARENT];
  16554. + args.dentry = NULL;
  16555. + args.inode = a->dir;
  16556. + args.h_inode = a->h_dir;
  16557. + args.dir = NULL;
  16558. + args.h_dir = NULL;
  16559. + args.h_name = NULL;
  16560. + args.h_nlen = 0;
  16561. + err = hn_job(&args);
  16562. + ii_write_unlock(a->dir);
  16563. +
  16564. +out:
  16565. + iput(a->h_child_inode);
  16566. + iput(a->h_dir);
  16567. + iput(a->dir);
  16568. + si_write_unlock(sb);
  16569. + au_nwt_done(&sbinfo->si_nowait);
  16570. + kfree(a);
  16571. +}
  16572. +
  16573. +/* ---------------------------------------------------------------------- */
  16574. +
  16575. +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
  16576. + struct qstr *h_child_qstr, struct inode *h_child_inode)
  16577. +{
  16578. + int err, len;
  16579. + unsigned int flags[AuHnLast], f;
  16580. + unsigned char isdir, isroot, wh;
  16581. + struct inode *dir;
  16582. + struct au_hnotify_args *args;
  16583. + char *p, *h_child_name;
  16584. +
  16585. + err = 0;
  16586. + AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
  16587. + dir = igrab(hnotify->hn_aufs_inode);
  16588. + if (!dir)
  16589. + goto out;
  16590. +
  16591. + isroot = (dir->i_ino == AUFS_ROOT_INO);
  16592. + wh = 0;
  16593. + h_child_name = (void *)h_child_qstr->name;
  16594. + len = h_child_qstr->len;
  16595. + if (h_child_name) {
  16596. + if (len > AUFS_WH_PFX_LEN
  16597. + && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
  16598. + h_child_name += AUFS_WH_PFX_LEN;
  16599. + len -= AUFS_WH_PFX_LEN;
  16600. + wh = 1;
  16601. + }
  16602. + }
  16603. +
  16604. + isdir = 0;
  16605. + if (h_child_inode)
  16606. + isdir = !!S_ISDIR(h_child_inode->i_mode);
  16607. + flags[AuHn_PARENT] = AuHnJob_ISDIR;
  16608. + flags[AuHn_CHILD] = 0;
  16609. + if (isdir)
  16610. + flags[AuHn_CHILD] = AuHnJob_ISDIR;
  16611. + au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
  16612. + au_fset_hnjob(flags[AuHn_CHILD], GEN);
  16613. + switch (mask & FS_EVENTS_POSS_ON_CHILD) {
  16614. + case FS_MOVED_FROM:
  16615. + case FS_MOVED_TO:
  16616. + au_fset_hnjob(flags[AuHn_CHILD], XINO0);
  16617. + au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
  16618. + /*FALLTHROUGH*/
  16619. + case FS_CREATE:
  16620. + AuDebugOn(!h_child_name);
  16621. + break;
  16622. +
  16623. + case FS_DELETE:
  16624. + /*
  16625. + * aufs never be able to get this child inode.
  16626. + * revalidation should be in d_revalidate()
  16627. + * by checking i_nlink, i_generation or d_unhashed().
  16628. + */
  16629. + AuDebugOn(!h_child_name);
  16630. + au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
  16631. + au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
  16632. + break;
  16633. +
  16634. + default:
  16635. + AuDebugOn(1);
  16636. + }
  16637. +
  16638. + if (wh)
  16639. + h_child_inode = NULL;
  16640. +
  16641. + err = -ENOMEM;
  16642. + /* iput() and kfree() will be called in au_hnotify() */
  16643. + args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
  16644. + if (unlikely(!args)) {
  16645. + AuErr1("no memory\n");
  16646. + iput(dir);
  16647. + goto out;
  16648. + }
  16649. + args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
  16650. + args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
  16651. + args->mask = mask;
  16652. + args->dir = dir;
  16653. + args->h_dir = igrab(h_dir);
  16654. + if (h_child_inode)
  16655. + h_child_inode = igrab(h_child_inode); /* can be NULL */
  16656. + args->h_child_inode = h_child_inode;
  16657. + args->h_child_nlen = len;
  16658. + if (len) {
  16659. + p = (void *)args;
  16660. + p += sizeof(*args);
  16661. + memcpy(p, h_child_name, len);
  16662. + p[len] = 0;
  16663. + }
  16664. +
  16665. + /* NFS fires the event for silly-renamed one from kworker */
  16666. + f = 0;
  16667. + if (!dir->i_nlink
  16668. + || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
  16669. + f = AuWkq_NEST;
  16670. + err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
  16671. + if (unlikely(err)) {
  16672. + pr_err("wkq %d\n", err);
  16673. + iput(args->h_child_inode);
  16674. + iput(args->h_dir);
  16675. + iput(args->dir);
  16676. + kfree(args);
  16677. + }
  16678. +
  16679. +out:
  16680. + return err;
  16681. +}
  16682. +
  16683. +/* ---------------------------------------------------------------------- */
  16684. +
  16685. +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
  16686. +{
  16687. + int err;
  16688. +
  16689. + AuDebugOn(!(udba & AuOptMask_UDBA));
  16690. +
  16691. + err = 0;
  16692. + if (au_hnotify_op.reset_br)
  16693. + err = au_hnotify_op.reset_br(udba, br, perm);
  16694. +
  16695. + return err;
  16696. +}
  16697. +
  16698. +int au_hnotify_init_br(struct au_branch *br, int perm)
  16699. +{
  16700. + int err;
  16701. +
  16702. + err = 0;
  16703. + if (au_hnotify_op.init_br)
  16704. + err = au_hnotify_op.init_br(br, perm);
  16705. +
  16706. + return err;
  16707. +}
  16708. +
  16709. +void au_hnotify_fin_br(struct au_branch *br)
  16710. +{
  16711. + if (au_hnotify_op.fin_br)
  16712. + au_hnotify_op.fin_br(br);
  16713. +}
  16714. +
  16715. +static void au_hn_destroy_cache(void)
  16716. +{
  16717. + kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
  16718. + au_cache[AuCache_HNOTIFY] = NULL;
  16719. +}
  16720. +
  16721. +int __init au_hnotify_init(void)
  16722. +{
  16723. + int err;
  16724. +
  16725. + err = -ENOMEM;
  16726. + au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
  16727. + if (au_cache[AuCache_HNOTIFY]) {
  16728. + err = 0;
  16729. + if (au_hnotify_op.init)
  16730. + err = au_hnotify_op.init();
  16731. + if (unlikely(err))
  16732. + au_hn_destroy_cache();
  16733. + }
  16734. + AuTraceErr(err);
  16735. + return err;
  16736. +}
  16737. +
  16738. +void au_hnotify_fin(void)
  16739. +{
  16740. + if (au_hnotify_op.fin)
  16741. + au_hnotify_op.fin();
  16742. +
  16743. + /* cf. au_cache_fin() */
  16744. + if (au_cache[AuCache_HNOTIFY])
  16745. + au_hn_destroy_cache();
  16746. +}
  16747. --- /dev/null
  16748. +++ linux-4.19/fs/aufs/iinfo.c 2018-11-20 09:35:15.029195504 +0200
  16749. @@ -0,0 +1,286 @@
  16750. +// SPDX-License-Identifier: GPL-2.0
  16751. +/*
  16752. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  16753. + *
  16754. + * This program, aufs is free software; you can redistribute it and/or modify
  16755. + * it under the terms of the GNU General Public License as published by
  16756. + * the Free Software Foundation; either version 2 of the License, or
  16757. + * (at your option) any later version.
  16758. + *
  16759. + * This program is distributed in the hope that it will be useful,
  16760. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16761. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16762. + * GNU General Public License for more details.
  16763. + *
  16764. + * You should have received a copy of the GNU General Public License
  16765. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16766. + */
  16767. +
  16768. +/*
  16769. + * inode private data
  16770. + */
  16771. +
  16772. +#include "aufs.h"
  16773. +
  16774. +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
  16775. +{
  16776. + struct inode *h_inode;
  16777. + struct au_hinode *hinode;
  16778. +
  16779. + IiMustAnyLock(inode);
  16780. +
  16781. + hinode = au_hinode(au_ii(inode), bindex);
  16782. + h_inode = hinode->hi_inode;
  16783. + AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
  16784. + return h_inode;
  16785. +}
  16786. +
  16787. +/* todo: hard/soft set? */
  16788. +void au_hiput(struct au_hinode *hinode)
  16789. +{
  16790. + au_hn_free(hinode);
  16791. + dput(hinode->hi_whdentry);
  16792. + iput(hinode->hi_inode);
  16793. +}
  16794. +
  16795. +unsigned int au_hi_flags(struct inode *inode, int isdir)
  16796. +{
  16797. + unsigned int flags;
  16798. + const unsigned int mnt_flags = au_mntflags(inode->i_sb);
  16799. +
  16800. + flags = 0;
  16801. + if (au_opt_test(mnt_flags, XINO))
  16802. + au_fset_hi(flags, XINO);
  16803. + if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
  16804. + au_fset_hi(flags, HNOTIFY);
  16805. + return flags;
  16806. +}
  16807. +
  16808. +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
  16809. + struct inode *h_inode, unsigned int flags)
  16810. +{
  16811. + struct au_hinode *hinode;
  16812. + struct inode *hi;
  16813. + struct au_iinfo *iinfo = au_ii(inode);
  16814. +
  16815. + IiMustWriteLock(inode);
  16816. +
  16817. + hinode = au_hinode(iinfo, bindex);
  16818. + hi = hinode->hi_inode;
  16819. + AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
  16820. +
  16821. + if (hi)
  16822. + au_hiput(hinode);
  16823. + hinode->hi_inode = h_inode;
  16824. + if (h_inode) {
  16825. + int err;
  16826. + struct super_block *sb = inode->i_sb;
  16827. + struct au_branch *br;
  16828. +
  16829. + AuDebugOn(inode->i_mode
  16830. + && (h_inode->i_mode & S_IFMT)
  16831. + != (inode->i_mode & S_IFMT));
  16832. + if (bindex == iinfo->ii_btop)
  16833. + au_cpup_igen(inode, h_inode);
  16834. + br = au_sbr(sb, bindex);
  16835. + hinode->hi_id = br->br_id;
  16836. + if (au_ftest_hi(flags, XINO)) {
  16837. + err = au_xino_write(sb, bindex, h_inode->i_ino,
  16838. + inode->i_ino);
  16839. + if (unlikely(err))
  16840. + AuIOErr1("failed au_xino_write() %d\n", err);
  16841. + }
  16842. +
  16843. + if (au_ftest_hi(flags, HNOTIFY)
  16844. + && au_br_hnotifyable(br->br_perm)) {
  16845. + err = au_hn_alloc(hinode, inode);
  16846. + if (unlikely(err))
  16847. + AuIOErr1("au_hn_alloc() %d\n", err);
  16848. + }
  16849. + }
  16850. +}
  16851. +
  16852. +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
  16853. + struct dentry *h_wh)
  16854. +{
  16855. + struct au_hinode *hinode;
  16856. +
  16857. + IiMustWriteLock(inode);
  16858. +
  16859. + hinode = au_hinode(au_ii(inode), bindex);
  16860. + AuDebugOn(hinode->hi_whdentry);
  16861. + hinode->hi_whdentry = h_wh;
  16862. +}
  16863. +
  16864. +void au_update_iigen(struct inode *inode, int half)
  16865. +{
  16866. + struct au_iinfo *iinfo;
  16867. + struct au_iigen *iigen;
  16868. + unsigned int sigen;
  16869. +
  16870. + sigen = au_sigen(inode->i_sb);
  16871. + iinfo = au_ii(inode);
  16872. + iigen = &iinfo->ii_generation;
  16873. + spin_lock(&iigen->ig_spin);
  16874. + iigen->ig_generation = sigen;
  16875. + if (half)
  16876. + au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
  16877. + else
  16878. + au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
  16879. + spin_unlock(&iigen->ig_spin);
  16880. +}
  16881. +
  16882. +/* it may be called at remount time, too */
  16883. +void au_update_ibrange(struct inode *inode, int do_put_zero)
  16884. +{
  16885. + struct au_iinfo *iinfo;
  16886. + aufs_bindex_t bindex, bbot;
  16887. +
  16888. + AuDebugOn(au_is_bad_inode(inode));
  16889. + IiMustWriteLock(inode);
  16890. +
  16891. + iinfo = au_ii(inode);
  16892. + if (do_put_zero && iinfo->ii_btop >= 0) {
  16893. + for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
  16894. + bindex++) {
  16895. + struct inode *h_i;
  16896. +
  16897. + h_i = au_hinode(iinfo, bindex)->hi_inode;
  16898. + if (h_i
  16899. + && !h_i->i_nlink
  16900. + && !(h_i->i_state & I_LINKABLE))
  16901. + au_set_h_iptr(inode, bindex, NULL, 0);
  16902. + }
  16903. + }
  16904. +
  16905. + iinfo->ii_btop = -1;
  16906. + iinfo->ii_bbot = -1;
  16907. + bbot = au_sbbot(inode->i_sb);
  16908. + for (bindex = 0; bindex <= bbot; bindex++)
  16909. + if (au_hinode(iinfo, bindex)->hi_inode) {
  16910. + iinfo->ii_btop = bindex;
  16911. + break;
  16912. + }
  16913. + if (iinfo->ii_btop >= 0)
  16914. + for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
  16915. + if (au_hinode(iinfo, bindex)->hi_inode) {
  16916. + iinfo->ii_bbot = bindex;
  16917. + break;
  16918. + }
  16919. + AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
  16920. +}
  16921. +
  16922. +/* ---------------------------------------------------------------------- */
  16923. +
  16924. +void au_icntnr_init_once(void *_c)
  16925. +{
  16926. + struct au_icntnr *c = _c;
  16927. + struct au_iinfo *iinfo = &c->iinfo;
  16928. +
  16929. + spin_lock_init(&iinfo->ii_generation.ig_spin);
  16930. + au_rw_init(&iinfo->ii_rwsem);
  16931. + inode_init_once(&c->vfs_inode);
  16932. +}
  16933. +
  16934. +void au_hinode_init(struct au_hinode *hinode)
  16935. +{
  16936. + hinode->hi_inode = NULL;
  16937. + hinode->hi_id = -1;
  16938. + au_hn_init(hinode);
  16939. + hinode->hi_whdentry = NULL;
  16940. +}
  16941. +
  16942. +int au_iinfo_init(struct inode *inode)
  16943. +{
  16944. + struct au_iinfo *iinfo;
  16945. + struct super_block *sb;
  16946. + struct au_hinode *hi;
  16947. + int nbr, i;
  16948. +
  16949. + sb = inode->i_sb;
  16950. + iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
  16951. + nbr = au_sbbot(sb) + 1;
  16952. + if (unlikely(nbr <= 0))
  16953. + nbr = 1;
  16954. + hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
  16955. + if (hi) {
  16956. + au_lcnt_inc(&au_sbi(sb)->si_ninodes);
  16957. +
  16958. + iinfo->ii_hinode = hi;
  16959. + for (i = 0; i < nbr; i++, hi++)
  16960. + au_hinode_init(hi);
  16961. +
  16962. + iinfo->ii_generation.ig_generation = au_sigen(sb);
  16963. + iinfo->ii_btop = -1;
  16964. + iinfo->ii_bbot = -1;
  16965. + iinfo->ii_vdir = NULL;
  16966. + return 0;
  16967. + }
  16968. + return -ENOMEM;
  16969. +}
  16970. +
  16971. +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
  16972. +{
  16973. + int err, i;
  16974. + struct au_hinode *hip;
  16975. +
  16976. + AuRwMustWriteLock(&iinfo->ii_rwsem);
  16977. +
  16978. + err = -ENOMEM;
  16979. + hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
  16980. + may_shrink);
  16981. + if (hip) {
  16982. + iinfo->ii_hinode = hip;
  16983. + i = iinfo->ii_bbot + 1;
  16984. + hip += i;
  16985. + for (; i < nbr; i++, hip++)
  16986. + au_hinode_init(hip);
  16987. + err = 0;
  16988. + }
  16989. +
  16990. + return err;
  16991. +}
  16992. +
  16993. +void au_iinfo_fin(struct inode *inode)
  16994. +{
  16995. + struct au_iinfo *iinfo;
  16996. + struct au_hinode *hi;
  16997. + struct super_block *sb;
  16998. + aufs_bindex_t bindex, bbot;
  16999. + const unsigned char unlinked = !inode->i_nlink;
  17000. +
  17001. + AuDebugOn(au_is_bad_inode(inode));
  17002. +
  17003. + sb = inode->i_sb;
  17004. + au_lcnt_dec(&au_sbi(sb)->si_ninodes);
  17005. + if (si_pid_test(sb))
  17006. + au_xino_delete_inode(inode, unlinked);
  17007. + else {
  17008. + /*
  17009. + * it is safe to hide the dependency between sbinfo and
  17010. + * sb->s_umount.
  17011. + */
  17012. + lockdep_off();
  17013. + si_noflush_read_lock(sb);
  17014. + au_xino_delete_inode(inode, unlinked);
  17015. + si_read_unlock(sb);
  17016. + lockdep_on();
  17017. + }
  17018. +
  17019. + iinfo = au_ii(inode);
  17020. + if (iinfo->ii_vdir)
  17021. + au_vdir_free(iinfo->ii_vdir);
  17022. +
  17023. + bindex = iinfo->ii_btop;
  17024. + if (bindex >= 0) {
  17025. + hi = au_hinode(iinfo, bindex);
  17026. + bbot = iinfo->ii_bbot;
  17027. + while (bindex++ <= bbot) {
  17028. + if (hi->hi_inode)
  17029. + au_hiput(hi);
  17030. + hi++;
  17031. + }
  17032. + }
  17033. + kfree(iinfo->ii_hinode);
  17034. + AuRwDestroy(&iinfo->ii_rwsem);
  17035. +}
  17036. --- /dev/null
  17037. +++ linux-4.19/fs/aufs/inode.c 2018-11-20 09:35:15.030195504 +0200
  17038. @@ -0,0 +1,528 @@
  17039. +// SPDX-License-Identifier: GPL-2.0
  17040. +/*
  17041. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  17042. + *
  17043. + * This program, aufs is free software; you can redistribute it and/or modify
  17044. + * it under the terms of the GNU General Public License as published by
  17045. + * the Free Software Foundation; either version 2 of the License, or
  17046. + * (at your option) any later version.
  17047. + *
  17048. + * This program is distributed in the hope that it will be useful,
  17049. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17050. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17051. + * GNU General Public License for more details.
  17052. + *
  17053. + * You should have received a copy of the GNU General Public License
  17054. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17055. + */
  17056. +
  17057. +/*
  17058. + * inode functions
  17059. + */
  17060. +
  17061. +#include "aufs.h"
  17062. +
  17063. +struct inode *au_igrab(struct inode *inode)
  17064. +{
  17065. + if (inode) {
  17066. + AuDebugOn(!atomic_read(&inode->i_count));
  17067. + ihold(inode);
  17068. + }
  17069. + return inode;
  17070. +}
  17071. +
  17072. +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
  17073. +{
  17074. + au_cpup_attr_all(inode, /*force*/0);
  17075. + au_update_iigen(inode, /*half*/1);
  17076. + if (do_version)
  17077. + inode_inc_iversion(inode);
  17078. +}
  17079. +
  17080. +static int au_ii_refresh(struct inode *inode, int *update)
  17081. +{
  17082. + int err, e, nbr;
  17083. + umode_t type;
  17084. + aufs_bindex_t bindex, new_bindex;
  17085. + struct super_block *sb;
  17086. + struct au_iinfo *iinfo;
  17087. + struct au_hinode *p, *q, tmp;
  17088. +
  17089. + AuDebugOn(au_is_bad_inode(inode));
  17090. + IiMustWriteLock(inode);
  17091. +
  17092. + *update = 0;
  17093. + sb = inode->i_sb;
  17094. + nbr = au_sbbot(sb) + 1;
  17095. + type = inode->i_mode & S_IFMT;
  17096. + iinfo = au_ii(inode);
  17097. + err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
  17098. + if (unlikely(err))
  17099. + goto out;
  17100. +
  17101. + AuDebugOn(iinfo->ii_btop < 0);
  17102. + p = au_hinode(iinfo, iinfo->ii_btop);
  17103. + for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
  17104. + bindex++, p++) {
  17105. + if (!p->hi_inode)
  17106. + continue;
  17107. +
  17108. + AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
  17109. + new_bindex = au_br_index(sb, p->hi_id);
  17110. + if (new_bindex == bindex)
  17111. + continue;
  17112. +
  17113. + if (new_bindex < 0) {
  17114. + *update = 1;
  17115. + au_hiput(p);
  17116. + p->hi_inode = NULL;
  17117. + continue;
  17118. + }
  17119. +
  17120. + if (new_bindex < iinfo->ii_btop)
  17121. + iinfo->ii_btop = new_bindex;
  17122. + if (iinfo->ii_bbot < new_bindex)
  17123. + iinfo->ii_bbot = new_bindex;
  17124. + /* swap two lower inode, and loop again */
  17125. + q = au_hinode(iinfo, new_bindex);
  17126. + tmp = *q;
  17127. + *q = *p;
  17128. + *p = tmp;
  17129. + if (tmp.hi_inode) {
  17130. + bindex--;
  17131. + p--;
  17132. + }
  17133. + }
  17134. + au_update_ibrange(inode, /*do_put_zero*/0);
  17135. + au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
  17136. + e = au_dy_irefresh(inode);
  17137. + if (unlikely(e && !err))
  17138. + err = e;
  17139. +
  17140. +out:
  17141. + AuTraceErr(err);
  17142. + return err;
  17143. +}
  17144. +
  17145. +void au_refresh_iop(struct inode *inode, int force_getattr)
  17146. +{
  17147. + int type;
  17148. + struct au_sbinfo *sbi = au_sbi(inode->i_sb);
  17149. + const struct inode_operations *iop
  17150. + = force_getattr ? aufs_iop : sbi->si_iop_array;
  17151. +
  17152. + if (inode->i_op == iop)
  17153. + return;
  17154. +
  17155. + switch (inode->i_mode & S_IFMT) {
  17156. + case S_IFDIR:
  17157. + type = AuIop_DIR;
  17158. + break;
  17159. + case S_IFLNK:
  17160. + type = AuIop_SYMLINK;
  17161. + break;
  17162. + default:
  17163. + type = AuIop_OTHER;
  17164. + break;
  17165. + }
  17166. +
  17167. + inode->i_op = iop + type;
  17168. + /* unnecessary smp_wmb() */
  17169. +}
  17170. +
  17171. +int au_refresh_hinode_self(struct inode *inode)
  17172. +{
  17173. + int err, update;
  17174. +
  17175. + err = au_ii_refresh(inode, &update);
  17176. + if (!err)
  17177. + au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
  17178. +
  17179. + AuTraceErr(err);
  17180. + return err;
  17181. +}
  17182. +
  17183. +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
  17184. +{
  17185. + int err, e, update;
  17186. + unsigned int flags;
  17187. + umode_t mode;
  17188. + aufs_bindex_t bindex, bbot;
  17189. + unsigned char isdir;
  17190. + struct au_hinode *p;
  17191. + struct au_iinfo *iinfo;
  17192. +
  17193. + err = au_ii_refresh(inode, &update);
  17194. + if (unlikely(err))
  17195. + goto out;
  17196. +
  17197. + update = 0;
  17198. + iinfo = au_ii(inode);
  17199. + p = au_hinode(iinfo, iinfo->ii_btop);
  17200. + mode = (inode->i_mode & S_IFMT);
  17201. + isdir = S_ISDIR(mode);
  17202. + flags = au_hi_flags(inode, isdir);
  17203. + bbot = au_dbbot(dentry);
  17204. + for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
  17205. + struct inode *h_i, *h_inode;
  17206. + struct dentry *h_d;
  17207. +
  17208. + h_d = au_h_dptr(dentry, bindex);
  17209. + if (!h_d || d_is_negative(h_d))
  17210. + continue;
  17211. +
  17212. + h_inode = d_inode(h_d);
  17213. + AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
  17214. + if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
  17215. + h_i = au_h_iptr(inode, bindex);
  17216. + if (h_i) {
  17217. + if (h_i == h_inode)
  17218. + continue;
  17219. + err = -EIO;
  17220. + break;
  17221. + }
  17222. + }
  17223. + if (bindex < iinfo->ii_btop)
  17224. + iinfo->ii_btop = bindex;
  17225. + if (iinfo->ii_bbot < bindex)
  17226. + iinfo->ii_bbot = bindex;
  17227. + au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
  17228. + update = 1;
  17229. + }
  17230. + au_update_ibrange(inode, /*do_put_zero*/0);
  17231. + e = au_dy_irefresh(inode);
  17232. + if (unlikely(e && !err))
  17233. + err = e;
  17234. + if (!err)
  17235. + au_refresh_hinode_attr(inode, update && isdir);
  17236. +
  17237. +out:
  17238. + AuTraceErr(err);
  17239. + return err;
  17240. +}
  17241. +
  17242. +static int set_inode(struct inode *inode, struct dentry *dentry)
  17243. +{
  17244. + int err;
  17245. + unsigned int flags;
  17246. + umode_t mode;
  17247. + aufs_bindex_t bindex, btop, btail;
  17248. + unsigned char isdir;
  17249. + struct dentry *h_dentry;
  17250. + struct inode *h_inode;
  17251. + struct au_iinfo *iinfo;
  17252. + struct inode_operations *iop;
  17253. +
  17254. + IiMustWriteLock(inode);
  17255. +
  17256. + err = 0;
  17257. + isdir = 0;
  17258. + iop = au_sbi(inode->i_sb)->si_iop_array;
  17259. + btop = au_dbtop(dentry);
  17260. + h_dentry = au_h_dptr(dentry, btop);
  17261. + h_inode = d_inode(h_dentry);
  17262. + mode = h_inode->i_mode;
  17263. + switch (mode & S_IFMT) {
  17264. + case S_IFREG:
  17265. + btail = au_dbtail(dentry);
  17266. + inode->i_op = iop + AuIop_OTHER;
  17267. + inode->i_fop = &aufs_file_fop;
  17268. + err = au_dy_iaop(inode, btop, h_inode);
  17269. + if (unlikely(err))
  17270. + goto out;
  17271. + break;
  17272. + case S_IFDIR:
  17273. + isdir = 1;
  17274. + btail = au_dbtaildir(dentry);
  17275. + inode->i_op = iop + AuIop_DIR;
  17276. + inode->i_fop = &aufs_dir_fop;
  17277. + break;
  17278. + case S_IFLNK:
  17279. + btail = au_dbtail(dentry);
  17280. + inode->i_op = iop + AuIop_SYMLINK;
  17281. + break;
  17282. + case S_IFBLK:
  17283. + case S_IFCHR:
  17284. + case S_IFIFO:
  17285. + case S_IFSOCK:
  17286. + btail = au_dbtail(dentry);
  17287. + inode->i_op = iop + AuIop_OTHER;
  17288. + init_special_inode(inode, mode, h_inode->i_rdev);
  17289. + break;
  17290. + default:
  17291. + AuIOErr("Unknown file type 0%o\n", mode);
  17292. + err = -EIO;
  17293. + goto out;
  17294. + }
  17295. +
  17296. + /* do not set hnotify for whiteouted dirs (SHWH mode) */
  17297. + flags = au_hi_flags(inode, isdir);
  17298. + if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
  17299. + && au_ftest_hi(flags, HNOTIFY)
  17300. + && dentry->d_name.len > AUFS_WH_PFX_LEN
  17301. + && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
  17302. + au_fclr_hi(flags, HNOTIFY);
  17303. + iinfo = au_ii(inode);
  17304. + iinfo->ii_btop = btop;
  17305. + iinfo->ii_bbot = btail;
  17306. + for (bindex = btop; bindex <= btail; bindex++) {
  17307. + h_dentry = au_h_dptr(dentry, bindex);
  17308. + if (h_dentry)
  17309. + au_set_h_iptr(inode, bindex,
  17310. + au_igrab(d_inode(h_dentry)), flags);
  17311. + }
  17312. + au_cpup_attr_all(inode, /*force*/1);
  17313. + /*
  17314. + * to force calling aufs_get_acl() every time,
  17315. + * do not call cache_no_acl() for aufs inode.
  17316. + */
  17317. +
  17318. +out:
  17319. + return err;
  17320. +}
  17321. +
  17322. +/*
  17323. + * successful returns with iinfo write_locked
  17324. + * minus: errno
  17325. + * zero: success, matched
  17326. + * plus: no error, but unmatched
  17327. + */
  17328. +static int reval_inode(struct inode *inode, struct dentry *dentry)
  17329. +{
  17330. + int err;
  17331. + unsigned int gen, igflags;
  17332. + aufs_bindex_t bindex, bbot;
  17333. + struct inode *h_inode, *h_dinode;
  17334. + struct dentry *h_dentry;
  17335. +
  17336. + /*
  17337. + * before this function, if aufs got any iinfo lock, it must be only
  17338. + * one, the parent dir.
  17339. + * it can happen by UDBA and the obsoleted inode number.
  17340. + */
  17341. + err = -EIO;
  17342. + if (unlikely(inode->i_ino == parent_ino(dentry)))
  17343. + goto out;
  17344. +
  17345. + err = 1;
  17346. + ii_write_lock_new_child(inode);
  17347. + h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
  17348. + h_dinode = d_inode(h_dentry);
  17349. + bbot = au_ibbot(inode);
  17350. + for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
  17351. + h_inode = au_h_iptr(inode, bindex);
  17352. + if (!h_inode || h_inode != h_dinode)
  17353. + continue;
  17354. +
  17355. + err = 0;
  17356. + gen = au_iigen(inode, &igflags);
  17357. + if (gen == au_digen(dentry)
  17358. + && !au_ig_ftest(igflags, HALF_REFRESHED))
  17359. + break;
  17360. +
  17361. + /* fully refresh inode using dentry */
  17362. + err = au_refresh_hinode(inode, dentry);
  17363. + if (!err)
  17364. + au_update_iigen(inode, /*half*/0);
  17365. + break;
  17366. + }
  17367. +
  17368. + if (unlikely(err))
  17369. + ii_write_unlock(inode);
  17370. +out:
  17371. + return err;
  17372. +}
  17373. +
  17374. +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  17375. + unsigned int d_type, ino_t *ino)
  17376. +{
  17377. + int err, idx;
  17378. + const int isnondir = d_type != DT_DIR;
  17379. +
  17380. + /* prevent hardlinked inode number from race condition */
  17381. + if (isnondir) {
  17382. + err = au_xinondir_enter(sb, bindex, h_ino, &idx);
  17383. + if (unlikely(err))
  17384. + goto out;
  17385. + }
  17386. +
  17387. + err = au_xino_read(sb, bindex, h_ino, ino);
  17388. + if (unlikely(err))
  17389. + goto out_xinondir;
  17390. +
  17391. + if (!*ino) {
  17392. + err = -EIO;
  17393. + *ino = au_xino_new_ino(sb);
  17394. + if (unlikely(!*ino))
  17395. + goto out_xinondir;
  17396. + err = au_xino_write(sb, bindex, h_ino, *ino);
  17397. + if (unlikely(err))
  17398. + goto out_xinondir;
  17399. + }
  17400. +
  17401. +out_xinondir:
  17402. + if (isnondir && idx >= 0)
  17403. + au_xinondir_leave(sb, bindex, h_ino, idx);
  17404. +out:
  17405. + return err;
  17406. +}
  17407. +
  17408. +/* successful returns with iinfo write_locked */
  17409. +/* todo: return with unlocked? */
  17410. +struct inode *au_new_inode(struct dentry *dentry, int must_new)
  17411. +{
  17412. + struct inode *inode, *h_inode;
  17413. + struct dentry *h_dentry;
  17414. + struct super_block *sb;
  17415. + ino_t h_ino, ino;
  17416. + int err, idx, hlinked;
  17417. + aufs_bindex_t btop;
  17418. +
  17419. + sb = dentry->d_sb;
  17420. + btop = au_dbtop(dentry);
  17421. + h_dentry = au_h_dptr(dentry, btop);
  17422. + h_inode = d_inode(h_dentry);
  17423. + h_ino = h_inode->i_ino;
  17424. + hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
  17425. +
  17426. +new_ino:
  17427. + /*
  17428. + * stop 'race'-ing between hardlinks under different
  17429. + * parents.
  17430. + */
  17431. + if (hlinked) {
  17432. + err = au_xinondir_enter(sb, btop, h_ino, &idx);
  17433. + inode = ERR_PTR(err);
  17434. + if (unlikely(err))
  17435. + goto out;
  17436. + }
  17437. +
  17438. + err = au_xino_read(sb, btop, h_ino, &ino);
  17439. + inode = ERR_PTR(err);
  17440. + if (unlikely(err))
  17441. + goto out_xinondir;
  17442. +
  17443. + if (!ino) {
  17444. + ino = au_xino_new_ino(sb);
  17445. + if (unlikely(!ino)) {
  17446. + inode = ERR_PTR(-EIO);
  17447. + goto out_xinondir;
  17448. + }
  17449. + }
  17450. +
  17451. + AuDbg("i%lu\n", (unsigned long)ino);
  17452. + inode = au_iget_locked(sb, ino);
  17453. + err = PTR_ERR(inode);
  17454. + if (IS_ERR(inode))
  17455. + goto out_xinondir;
  17456. +
  17457. + AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
  17458. + if (inode->i_state & I_NEW) {
  17459. + ii_write_lock_new_child(inode);
  17460. + err = set_inode(inode, dentry);
  17461. + if (!err) {
  17462. + unlock_new_inode(inode);
  17463. + goto out_xinondir; /* success */
  17464. + }
  17465. +
  17466. + /*
  17467. + * iget_failed() calls iput(), but we need to call
  17468. + * ii_write_unlock() after iget_failed(). so dirty hack for
  17469. + * i_count.
  17470. + */
  17471. + atomic_inc(&inode->i_count);
  17472. + iget_failed(inode);
  17473. + ii_write_unlock(inode);
  17474. + au_xino_write(sb, btop, h_ino, /*ino*/0);
  17475. + /* ignore this error */
  17476. + goto out_iput;
  17477. + } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
  17478. + /*
  17479. + * horrible race condition between lookup, readdir and copyup
  17480. + * (or something).
  17481. + */
  17482. + if (hlinked && idx >= 0)
  17483. + au_xinondir_leave(sb, btop, h_ino, idx);
  17484. + err = reval_inode(inode, dentry);
  17485. + if (unlikely(err < 0)) {
  17486. + hlinked = 0;
  17487. + goto out_iput;
  17488. + }
  17489. + if (!err)
  17490. + goto out; /* success */
  17491. + else if (hlinked && idx >= 0) {
  17492. + err = au_xinondir_enter(sb, btop, h_ino, &idx);
  17493. + if (unlikely(err)) {
  17494. + iput(inode);
  17495. + inode = ERR_PTR(err);
  17496. + goto out;
  17497. + }
  17498. + }
  17499. + }
  17500. +
  17501. + if (unlikely(au_test_fs_unique_ino(h_inode)))
  17502. + AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
  17503. + " b%d, %s, %pd, hi%lu, i%lu.\n",
  17504. + btop, au_sbtype(h_dentry->d_sb), dentry,
  17505. + (unsigned long)h_ino, (unsigned long)ino);
  17506. + ino = 0;
  17507. + err = au_xino_write(sb, btop, h_ino, /*ino*/0);
  17508. + if (!err) {
  17509. + iput(inode);
  17510. + if (hlinked && idx >= 0)
  17511. + au_xinondir_leave(sb, btop, h_ino, idx);
  17512. + goto new_ino;
  17513. + }
  17514. +
  17515. +out_iput:
  17516. + iput(inode);
  17517. + inode = ERR_PTR(err);
  17518. +out_xinondir:
  17519. + if (hlinked && idx >= 0)
  17520. + au_xinondir_leave(sb, btop, h_ino, idx);
  17521. +out:
  17522. + return inode;
  17523. +}
  17524. +
  17525. +/* ---------------------------------------------------------------------- */
  17526. +
  17527. +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
  17528. + struct inode *inode)
  17529. +{
  17530. + int err;
  17531. + struct inode *hi;
  17532. +
  17533. + err = au_br_rdonly(au_sbr(sb, bindex));
  17534. +
  17535. + /* pseudo-link after flushed may happen out of bounds */
  17536. + if (!err
  17537. + && inode
  17538. + && au_ibtop(inode) <= bindex
  17539. + && bindex <= au_ibbot(inode)) {
  17540. + /*
  17541. + * permission check is unnecessary since vfsub routine
  17542. + * will be called later
  17543. + */
  17544. + hi = au_h_iptr(inode, bindex);
  17545. + if (hi)
  17546. + err = IS_IMMUTABLE(hi) ? -EROFS : 0;
  17547. + }
  17548. +
  17549. + return err;
  17550. +}
  17551. +
  17552. +int au_test_h_perm(struct inode *h_inode, int mask)
  17553. +{
  17554. + if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
  17555. + return 0;
  17556. + return inode_permission(h_inode, mask);
  17557. +}
  17558. +
  17559. +int au_test_h_perm_sio(struct inode *h_inode, int mask)
  17560. +{
  17561. + if (au_test_nfs(h_inode->i_sb)
  17562. + && (mask & MAY_WRITE)
  17563. + && S_ISDIR(h_inode->i_mode))
  17564. + mask |= MAY_READ; /* force permission check */
  17565. + return au_test_h_perm(h_inode, mask);
  17566. +}
  17567. --- /dev/null
  17568. +++ linux-4.19/fs/aufs/inode.h 2018-11-20 09:35:15.031195504 +0200
  17569. @@ -0,0 +1,696 @@
  17570. +/* SPDX-License-Identifier: GPL-2.0 */
  17571. +/*
  17572. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  17573. + *
  17574. + * This program, aufs is free software; you can redistribute it and/or modify
  17575. + * it under the terms of the GNU General Public License as published by
  17576. + * the Free Software Foundation; either version 2 of the License, or
  17577. + * (at your option) any later version.
  17578. + *
  17579. + * This program is distributed in the hope that it will be useful,
  17580. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17581. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17582. + * GNU General Public License for more details.
  17583. + *
  17584. + * You should have received a copy of the GNU General Public License
  17585. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17586. + */
  17587. +
  17588. +/*
  17589. + * inode operations
  17590. + */
  17591. +
  17592. +#ifndef __AUFS_INODE_H__
  17593. +#define __AUFS_INODE_H__
  17594. +
  17595. +#ifdef __KERNEL__
  17596. +
  17597. +#include <linux/fsnotify.h>
  17598. +#include "rwsem.h"
  17599. +
  17600. +struct vfsmount;
  17601. +
  17602. +struct au_hnotify {
  17603. +#ifdef CONFIG_AUFS_HNOTIFY
  17604. +#ifdef CONFIG_AUFS_HFSNOTIFY
  17605. + /* never use fsnotify_add_vfsmount_mark() */
  17606. + struct fsnotify_mark hn_mark;
  17607. +#endif
  17608. + struct inode *hn_aufs_inode; /* no get/put */
  17609. +#endif
  17610. +} ____cacheline_aligned_in_smp;
  17611. +
  17612. +struct au_hinode {
  17613. + struct inode *hi_inode;
  17614. + aufs_bindex_t hi_id;
  17615. +#ifdef CONFIG_AUFS_HNOTIFY
  17616. + struct au_hnotify *hi_notify;
  17617. +#endif
  17618. +
  17619. + /* reference to the copied-up whiteout with get/put */
  17620. + struct dentry *hi_whdentry;
  17621. +};
  17622. +
  17623. +/* ig_flags */
  17624. +#define AuIG_HALF_REFRESHED 1
  17625. +#define au_ig_ftest(flags, name) ((flags) & AuIG_##name)
  17626. +#define au_ig_fset(flags, name) \
  17627. + do { (flags) |= AuIG_##name; } while (0)
  17628. +#define au_ig_fclr(flags, name) \
  17629. + do { (flags) &= ~AuIG_##name; } while (0)
  17630. +
  17631. +struct au_iigen {
  17632. + spinlock_t ig_spin;
  17633. + __u32 ig_generation, ig_flags;
  17634. +};
  17635. +
  17636. +struct au_vdir;
  17637. +struct au_iinfo {
  17638. + struct au_iigen ii_generation;
  17639. + struct super_block *ii_hsb1; /* no get/put */
  17640. +
  17641. + struct au_rwsem ii_rwsem;
  17642. + aufs_bindex_t ii_btop, ii_bbot;
  17643. + __u32 ii_higen;
  17644. + struct au_hinode *ii_hinode;
  17645. + struct au_vdir *ii_vdir;
  17646. +};
  17647. +
  17648. +struct au_icntnr {
  17649. + struct au_iinfo iinfo;
  17650. + struct inode vfs_inode;
  17651. + struct hlist_bl_node plink;
  17652. +} ____cacheline_aligned_in_smp;
  17653. +
  17654. +/* au_pin flags */
  17655. +#define AuPin_DI_LOCKED 1
  17656. +#define AuPin_MNT_WRITE (1 << 1)
  17657. +#define au_ftest_pin(flags, name) ((flags) & AuPin_##name)
  17658. +#define au_fset_pin(flags, name) \
  17659. + do { (flags) |= AuPin_##name; } while (0)
  17660. +#define au_fclr_pin(flags, name) \
  17661. + do { (flags) &= ~AuPin_##name; } while (0)
  17662. +
  17663. +struct au_pin {
  17664. + /* input */
  17665. + struct dentry *dentry;
  17666. + unsigned int udba;
  17667. + unsigned char lsc_di, lsc_hi, flags;
  17668. + aufs_bindex_t bindex;
  17669. +
  17670. + /* output */
  17671. + struct dentry *parent;
  17672. + struct au_hinode *hdir;
  17673. + struct vfsmount *h_mnt;
  17674. +
  17675. + /* temporary unlock/relock for copyup */
  17676. + struct dentry *h_dentry, *h_parent;
  17677. + struct au_branch *br;
  17678. + struct task_struct *task;
  17679. +};
  17680. +
  17681. +void au_pin_hdir_unlock(struct au_pin *p);
  17682. +int au_pin_hdir_lock(struct au_pin *p);
  17683. +int au_pin_hdir_relock(struct au_pin *p);
  17684. +void au_pin_hdir_acquire_nest(struct au_pin *p);
  17685. +void au_pin_hdir_release(struct au_pin *p);
  17686. +
  17687. +/* ---------------------------------------------------------------------- */
  17688. +
  17689. +static inline struct au_iinfo *au_ii(struct inode *inode)
  17690. +{
  17691. + BUG_ON(is_bad_inode(inode));
  17692. + return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
  17693. +}
  17694. +
  17695. +/* ---------------------------------------------------------------------- */
  17696. +
  17697. +/* inode.c */
  17698. +struct inode *au_igrab(struct inode *inode);
  17699. +void au_refresh_iop(struct inode *inode, int force_getattr);
  17700. +int au_refresh_hinode_self(struct inode *inode);
  17701. +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
  17702. +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  17703. + unsigned int d_type, ino_t *ino);
  17704. +struct inode *au_new_inode(struct dentry *dentry, int must_new);
  17705. +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
  17706. + struct inode *inode);
  17707. +int au_test_h_perm(struct inode *h_inode, int mask);
  17708. +int au_test_h_perm_sio(struct inode *h_inode, int mask);
  17709. +
  17710. +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
  17711. + ino_t h_ino, unsigned int d_type, ino_t *ino)
  17712. +{
  17713. +#ifdef CONFIG_AUFS_SHWH
  17714. + return au_ino(sb, bindex, h_ino, d_type, ino);
  17715. +#else
  17716. + return 0;
  17717. +#endif
  17718. +}
  17719. +
  17720. +/* i_op.c */
  17721. +enum {
  17722. + AuIop_SYMLINK,
  17723. + AuIop_DIR,
  17724. + AuIop_OTHER,
  17725. + AuIop_Last
  17726. +};
  17727. +extern struct inode_operations aufs_iop[AuIop_Last],
  17728. + aufs_iop_nogetattr[AuIop_Last];
  17729. +
  17730. +/* au_wr_dir flags */
  17731. +#define AuWrDir_ADD_ENTRY 1
  17732. +#define AuWrDir_ISDIR (1 << 1)
  17733. +#define AuWrDir_TMPFILE (1 << 2)
  17734. +#define au_ftest_wrdir(flags, name) ((flags) & AuWrDir_##name)
  17735. +#define au_fset_wrdir(flags, name) \
  17736. + do { (flags) |= AuWrDir_##name; } while (0)
  17737. +#define au_fclr_wrdir(flags, name) \
  17738. + do { (flags) &= ~AuWrDir_##name; } while (0)
  17739. +
  17740. +struct au_wr_dir_args {
  17741. + aufs_bindex_t force_btgt;
  17742. + unsigned char flags;
  17743. +};
  17744. +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
  17745. + struct au_wr_dir_args *args);
  17746. +
  17747. +struct dentry *au_pinned_h_parent(struct au_pin *pin);
  17748. +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
  17749. + aufs_bindex_t bindex, int lsc_di, int lsc_hi,
  17750. + unsigned int udba, unsigned char flags);
  17751. +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
  17752. + unsigned int udba, unsigned char flags) __must_check;
  17753. +int au_do_pin(struct au_pin *pin) __must_check;
  17754. +void au_unpin(struct au_pin *pin);
  17755. +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
  17756. +
  17757. +#define AuIcpup_DID_CPUP 1
  17758. +#define au_ftest_icpup(flags, name) ((flags) & AuIcpup_##name)
  17759. +#define au_fset_icpup(flags, name) \
  17760. + do { (flags) |= AuIcpup_##name; } while (0)
  17761. +#define au_fclr_icpup(flags, name) \
  17762. + do { (flags) &= ~AuIcpup_##name; } while (0)
  17763. +
  17764. +struct au_icpup_args {
  17765. + unsigned char flags;
  17766. + unsigned char pin_flags;
  17767. + aufs_bindex_t btgt;
  17768. + unsigned int udba;
  17769. + struct au_pin pin;
  17770. + struct path h_path;
  17771. + struct inode *h_inode;
  17772. +};
  17773. +
  17774. +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
  17775. + struct au_icpup_args *a);
  17776. +
  17777. +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
  17778. + int locked);
  17779. +
  17780. +/* i_op_add.c */
  17781. +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
  17782. + struct dentry *h_parent, int isdir);
  17783. +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  17784. + dev_t dev);
  17785. +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
  17786. +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  17787. + bool want_excl);
  17788. +struct vfsub_aopen_args;
  17789. +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
  17790. + struct vfsub_aopen_args *args);
  17791. +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
  17792. +int aufs_link(struct dentry *src_dentry, struct inode *dir,
  17793. + struct dentry *dentry);
  17794. +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
  17795. +
  17796. +/* i_op_del.c */
  17797. +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
  17798. +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
  17799. + struct dentry *h_parent, int isdir);
  17800. +int aufs_unlink(struct inode *dir, struct dentry *dentry);
  17801. +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
  17802. +
  17803. +/* i_op_ren.c */
  17804. +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
  17805. +int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
  17806. + struct inode *dir, struct dentry *dentry,
  17807. + unsigned int flags);
  17808. +
  17809. +/* iinfo.c */
  17810. +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
  17811. +void au_hiput(struct au_hinode *hinode);
  17812. +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
  17813. + struct dentry *h_wh);
  17814. +unsigned int au_hi_flags(struct inode *inode, int isdir);
  17815. +
  17816. +/* hinode flags */
  17817. +#define AuHi_XINO 1
  17818. +#define AuHi_HNOTIFY (1 << 1)
  17819. +#define au_ftest_hi(flags, name) ((flags) & AuHi_##name)
  17820. +#define au_fset_hi(flags, name) \
  17821. + do { (flags) |= AuHi_##name; } while (0)
  17822. +#define au_fclr_hi(flags, name) \
  17823. + do { (flags) &= ~AuHi_##name; } while (0)
  17824. +
  17825. +#ifndef CONFIG_AUFS_HNOTIFY
  17826. +#undef AuHi_HNOTIFY
  17827. +#define AuHi_HNOTIFY 0
  17828. +#endif
  17829. +
  17830. +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
  17831. + struct inode *h_inode, unsigned int flags);
  17832. +
  17833. +void au_update_iigen(struct inode *inode, int half);
  17834. +void au_update_ibrange(struct inode *inode, int do_put_zero);
  17835. +
  17836. +void au_icntnr_init_once(void *_c);
  17837. +void au_hinode_init(struct au_hinode *hinode);
  17838. +int au_iinfo_init(struct inode *inode);
  17839. +void au_iinfo_fin(struct inode *inode);
  17840. +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
  17841. +
  17842. +#ifdef CONFIG_PROC_FS
  17843. +/* plink.c */
  17844. +int au_plink_maint(struct super_block *sb, int flags);
  17845. +struct au_sbinfo;
  17846. +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
  17847. +int au_plink_maint_enter(struct super_block *sb);
  17848. +#ifdef CONFIG_AUFS_DEBUG
  17849. +void au_plink_list(struct super_block *sb);
  17850. +#else
  17851. +AuStubVoid(au_plink_list, struct super_block *sb)
  17852. +#endif
  17853. +int au_plink_test(struct inode *inode);
  17854. +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
  17855. +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
  17856. + struct dentry *h_dentry);
  17857. +void au_plink_put(struct super_block *sb, int verbose);
  17858. +void au_plink_clean(struct super_block *sb, int verbose);
  17859. +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
  17860. +#else
  17861. +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
  17862. +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
  17863. +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
  17864. +AuStubVoid(au_plink_list, struct super_block *sb);
  17865. +AuStubInt0(au_plink_test, struct inode *inode);
  17866. +AuStub(struct dentry *, au_plink_lkup, return NULL,
  17867. + struct inode *inode, aufs_bindex_t bindex);
  17868. +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
  17869. + struct dentry *h_dentry);
  17870. +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
  17871. +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
  17872. +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
  17873. +#endif /* CONFIG_PROC_FS */
  17874. +
  17875. +#ifdef CONFIG_AUFS_XATTR
  17876. +/* xattr.c */
  17877. +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
  17878. + unsigned int verbose);
  17879. +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
  17880. +void au_xattr_init(struct super_block *sb);
  17881. +#else
  17882. +AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
  17883. + int ignore_flags, unsigned int verbose);
  17884. +AuStubVoid(au_xattr_init, struct super_block *sb);
  17885. +#endif
  17886. +
  17887. +#ifdef CONFIG_FS_POSIX_ACL
  17888. +struct posix_acl *aufs_get_acl(struct inode *inode, int type);
  17889. +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
  17890. +#endif
  17891. +
  17892. +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
  17893. +enum {
  17894. + AU_XATTR_SET,
  17895. + AU_ACL_SET
  17896. +};
  17897. +
  17898. +struct au_sxattr {
  17899. + int type;
  17900. + union {
  17901. + struct {
  17902. + const char *name;
  17903. + const void *value;
  17904. + size_t size;
  17905. + int flags;
  17906. + } set;
  17907. + struct {
  17908. + struct posix_acl *acl;
  17909. + int type;
  17910. + } acl_set;
  17911. + } u;
  17912. +};
  17913. +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
  17914. + struct au_sxattr *arg);
  17915. +#endif
  17916. +
  17917. +/* ---------------------------------------------------------------------- */
  17918. +
  17919. +/* lock subclass for iinfo */
  17920. +enum {
  17921. + AuLsc_II_CHILD, /* child first */
  17922. + AuLsc_II_CHILD2, /* rename(2), link(2), and cpup at hnotify */
  17923. + AuLsc_II_CHILD3, /* copyup dirs */
  17924. + AuLsc_II_PARENT, /* see AuLsc_I_PARENT in vfsub.h */
  17925. + AuLsc_II_PARENT2,
  17926. + AuLsc_II_PARENT3, /* copyup dirs */
  17927. + AuLsc_II_NEW_CHILD
  17928. +};
  17929. +
  17930. +/*
  17931. + * ii_read_lock_child, ii_write_lock_child,
  17932. + * ii_read_lock_child2, ii_write_lock_child2,
  17933. + * ii_read_lock_child3, ii_write_lock_child3,
  17934. + * ii_read_lock_parent, ii_write_lock_parent,
  17935. + * ii_read_lock_parent2, ii_write_lock_parent2,
  17936. + * ii_read_lock_parent3, ii_write_lock_parent3,
  17937. + * ii_read_lock_new_child, ii_write_lock_new_child,
  17938. + */
  17939. +#define AuReadLockFunc(name, lsc) \
  17940. +static inline void ii_read_lock_##name(struct inode *i) \
  17941. +{ \
  17942. + au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
  17943. +}
  17944. +
  17945. +#define AuWriteLockFunc(name, lsc) \
  17946. +static inline void ii_write_lock_##name(struct inode *i) \
  17947. +{ \
  17948. + au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
  17949. +}
  17950. +
  17951. +#define AuRWLockFuncs(name, lsc) \
  17952. + AuReadLockFunc(name, lsc) \
  17953. + AuWriteLockFunc(name, lsc)
  17954. +
  17955. +AuRWLockFuncs(child, CHILD);
  17956. +AuRWLockFuncs(child2, CHILD2);
  17957. +AuRWLockFuncs(child3, CHILD3);
  17958. +AuRWLockFuncs(parent, PARENT);
  17959. +AuRWLockFuncs(parent2, PARENT2);
  17960. +AuRWLockFuncs(parent3, PARENT3);
  17961. +AuRWLockFuncs(new_child, NEW_CHILD);
  17962. +
  17963. +#undef AuReadLockFunc
  17964. +#undef AuWriteLockFunc
  17965. +#undef AuRWLockFuncs
  17966. +
  17967. +#define ii_read_unlock(i) au_rw_read_unlock(&au_ii(i)->ii_rwsem)
  17968. +#define ii_write_unlock(i) au_rw_write_unlock(&au_ii(i)->ii_rwsem)
  17969. +#define ii_downgrade_lock(i) au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
  17970. +
  17971. +#define IiMustNoWaiters(i) AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
  17972. +#define IiMustAnyLock(i) AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
  17973. +#define IiMustWriteLock(i) AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
  17974. +
  17975. +/* ---------------------------------------------------------------------- */
  17976. +
  17977. +static inline void au_icntnr_init(struct au_icntnr *c)
  17978. +{
  17979. +#ifdef CONFIG_AUFS_DEBUG
  17980. + c->vfs_inode.i_mode = 0;
  17981. +#endif
  17982. +}
  17983. +
  17984. +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
  17985. +{
  17986. + unsigned int gen;
  17987. + struct au_iinfo *iinfo;
  17988. + struct au_iigen *iigen;
  17989. +
  17990. + iinfo = au_ii(inode);
  17991. + iigen = &iinfo->ii_generation;
  17992. + spin_lock(&iigen->ig_spin);
  17993. + if (igflags)
  17994. + *igflags = iigen->ig_flags;
  17995. + gen = iigen->ig_generation;
  17996. + spin_unlock(&iigen->ig_spin);
  17997. +
  17998. + return gen;
  17999. +}
  18000. +
  18001. +/* tiny test for inode number */
  18002. +/* tmpfs generation is too rough */
  18003. +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
  18004. +{
  18005. + struct au_iinfo *iinfo;
  18006. +
  18007. + iinfo = au_ii(inode);
  18008. + AuRwMustAnyLock(&iinfo->ii_rwsem);
  18009. + return !(iinfo->ii_hsb1 == h_inode->i_sb
  18010. + && iinfo->ii_higen == h_inode->i_generation);
  18011. +}
  18012. +
  18013. +static inline void au_iigen_dec(struct inode *inode)
  18014. +{
  18015. + struct au_iinfo *iinfo;
  18016. + struct au_iigen *iigen;
  18017. +
  18018. + iinfo = au_ii(inode);
  18019. + iigen = &iinfo->ii_generation;
  18020. + spin_lock(&iigen->ig_spin);
  18021. + iigen->ig_generation--;
  18022. + spin_unlock(&iigen->ig_spin);
  18023. +}
  18024. +
  18025. +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
  18026. +{
  18027. + int err;
  18028. +
  18029. + err = 0;
  18030. + if (unlikely(inode && au_iigen(inode, NULL) != sigen))
  18031. + err = -EIO;
  18032. +
  18033. + return err;
  18034. +}
  18035. +
  18036. +/* ---------------------------------------------------------------------- */
  18037. +
  18038. +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
  18039. + aufs_bindex_t bindex)
  18040. +{
  18041. + return iinfo->ii_hinode + bindex;
  18042. +}
  18043. +
  18044. +static inline int au_is_bad_inode(struct inode *inode)
  18045. +{
  18046. + return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
  18047. +}
  18048. +
  18049. +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
  18050. + aufs_bindex_t bindex)
  18051. +{
  18052. + IiMustAnyLock(inode);
  18053. + return au_hinode(au_ii(inode), bindex)->hi_id;
  18054. +}
  18055. +
  18056. +static inline aufs_bindex_t au_ibtop(struct inode *inode)
  18057. +{
  18058. + IiMustAnyLock(inode);
  18059. + return au_ii(inode)->ii_btop;
  18060. +}
  18061. +
  18062. +static inline aufs_bindex_t au_ibbot(struct inode *inode)
  18063. +{
  18064. + IiMustAnyLock(inode);
  18065. + return au_ii(inode)->ii_bbot;
  18066. +}
  18067. +
  18068. +static inline struct au_vdir *au_ivdir(struct inode *inode)
  18069. +{
  18070. + IiMustAnyLock(inode);
  18071. + return au_ii(inode)->ii_vdir;
  18072. +}
  18073. +
  18074. +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
  18075. +{
  18076. + IiMustAnyLock(inode);
  18077. + return au_hinode(au_ii(inode), bindex)->hi_whdentry;
  18078. +}
  18079. +
  18080. +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
  18081. +{
  18082. + IiMustWriteLock(inode);
  18083. + au_ii(inode)->ii_btop = bindex;
  18084. +}
  18085. +
  18086. +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
  18087. +{
  18088. + IiMustWriteLock(inode);
  18089. + au_ii(inode)->ii_bbot = bindex;
  18090. +}
  18091. +
  18092. +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
  18093. +{
  18094. + IiMustWriteLock(inode);
  18095. + au_ii(inode)->ii_vdir = vdir;
  18096. +}
  18097. +
  18098. +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
  18099. +{
  18100. + IiMustAnyLock(inode);
  18101. + return au_hinode(au_ii(inode), bindex);
  18102. +}
  18103. +
  18104. +/* ---------------------------------------------------------------------- */
  18105. +
  18106. +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
  18107. +{
  18108. + if (pin)
  18109. + return pin->parent;
  18110. + return NULL;
  18111. +}
  18112. +
  18113. +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
  18114. +{
  18115. + if (pin && pin->hdir)
  18116. + return pin->hdir->hi_inode;
  18117. + return NULL;
  18118. +}
  18119. +
  18120. +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
  18121. +{
  18122. + if (pin)
  18123. + return pin->hdir;
  18124. + return NULL;
  18125. +}
  18126. +
  18127. +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
  18128. +{
  18129. + if (pin)
  18130. + pin->dentry = dentry;
  18131. +}
  18132. +
  18133. +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
  18134. + unsigned char lflag)
  18135. +{
  18136. + if (pin) {
  18137. + if (lflag)
  18138. + au_fset_pin(pin->flags, DI_LOCKED);
  18139. + else
  18140. + au_fclr_pin(pin->flags, DI_LOCKED);
  18141. + }
  18142. +}
  18143. +
  18144. +#if 0 /* reserved */
  18145. +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
  18146. +{
  18147. + if (pin) {
  18148. + dput(pin->parent);
  18149. + pin->parent = dget(parent);
  18150. + }
  18151. +}
  18152. +#endif
  18153. +
  18154. +/* ---------------------------------------------------------------------- */
  18155. +
  18156. +struct au_branch;
  18157. +#ifdef CONFIG_AUFS_HNOTIFY
  18158. +struct au_hnotify_op {
  18159. + void (*ctl)(struct au_hinode *hinode, int do_set);
  18160. + int (*alloc)(struct au_hinode *hinode);
  18161. +
  18162. + /*
  18163. + * if it returns true, the the caller should free hinode->hi_notify,
  18164. + * otherwise ->free() frees it.
  18165. + */
  18166. + int (*free)(struct au_hinode *hinode,
  18167. + struct au_hnotify *hn) __must_check;
  18168. +
  18169. + void (*fin)(void);
  18170. + int (*init)(void);
  18171. +
  18172. + int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
  18173. + void (*fin_br)(struct au_branch *br);
  18174. + int (*init_br)(struct au_branch *br, int perm);
  18175. +};
  18176. +
  18177. +/* hnotify.c */
  18178. +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
  18179. +void au_hn_free(struct au_hinode *hinode);
  18180. +void au_hn_ctl(struct au_hinode *hinode, int do_set);
  18181. +void au_hn_reset(struct inode *inode, unsigned int flags);
  18182. +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
  18183. + struct qstr *h_child_qstr, struct inode *h_child_inode);
  18184. +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
  18185. +int au_hnotify_init_br(struct au_branch *br, int perm);
  18186. +void au_hnotify_fin_br(struct au_branch *br);
  18187. +int __init au_hnotify_init(void);
  18188. +void au_hnotify_fin(void);
  18189. +
  18190. +/* hfsnotify.c */
  18191. +extern const struct au_hnotify_op au_hnotify_op;
  18192. +
  18193. +static inline
  18194. +void au_hn_init(struct au_hinode *hinode)
  18195. +{
  18196. + hinode->hi_notify = NULL;
  18197. +}
  18198. +
  18199. +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
  18200. +{
  18201. + return hinode->hi_notify;
  18202. +}
  18203. +
  18204. +#else
  18205. +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
  18206. + struct au_hinode *hinode __maybe_unused,
  18207. + struct inode *inode __maybe_unused)
  18208. +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
  18209. +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
  18210. +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
  18211. + int do_set __maybe_unused)
  18212. +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
  18213. + unsigned int flags __maybe_unused)
  18214. +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
  18215. + struct au_branch *br __maybe_unused,
  18216. + int perm __maybe_unused)
  18217. +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
  18218. + int perm __maybe_unused)
  18219. +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
  18220. +AuStubInt0(__init au_hnotify_init, void)
  18221. +AuStubVoid(au_hnotify_fin, void)
  18222. +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
  18223. +#endif /* CONFIG_AUFS_HNOTIFY */
  18224. +
  18225. +static inline void au_hn_suspend(struct au_hinode *hdir)
  18226. +{
  18227. + au_hn_ctl(hdir, /*do_set*/0);
  18228. +}
  18229. +
  18230. +static inline void au_hn_resume(struct au_hinode *hdir)
  18231. +{
  18232. + au_hn_ctl(hdir, /*do_set*/1);
  18233. +}
  18234. +
  18235. +static inline void au_hn_inode_lock(struct au_hinode *hdir)
  18236. +{
  18237. + inode_lock(hdir->hi_inode);
  18238. + au_hn_suspend(hdir);
  18239. +}
  18240. +
  18241. +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
  18242. + unsigned int sc __maybe_unused)
  18243. +{
  18244. + inode_lock_nested(hdir->hi_inode, sc);
  18245. + au_hn_suspend(hdir);
  18246. +}
  18247. +
  18248. +#if 0 /* unused */
  18249. +#include "vfsub.h"
  18250. +static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
  18251. + unsigned int sc)
  18252. +{
  18253. + inode_lock_shared_nested(hdir->hi_inode, sc);
  18254. + au_hn_suspend(hdir);
  18255. +}
  18256. +#endif
  18257. +
  18258. +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
  18259. +{
  18260. + au_hn_resume(hdir);
  18261. + inode_unlock(hdir->hi_inode);
  18262. +}
  18263. +
  18264. +#endif /* __KERNEL__ */
  18265. +#endif /* __AUFS_INODE_H__ */
  18266. --- /dev/null
  18267. +++ linux-4.19/fs/aufs/ioctl.c 2018-11-20 09:35:15.032195504 +0200
  18268. @@ -0,0 +1,220 @@
  18269. +// SPDX-License-Identifier: GPL-2.0
  18270. +/*
  18271. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  18272. + *
  18273. + * This program, aufs is free software; you can redistribute it and/or modify
  18274. + * it under the terms of the GNU General Public License as published by
  18275. + * the Free Software Foundation; either version 2 of the License, or
  18276. + * (at your option) any later version.
  18277. + *
  18278. + * This program is distributed in the hope that it will be useful,
  18279. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18280. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18281. + * GNU General Public License for more details.
  18282. + *
  18283. + * You should have received a copy of the GNU General Public License
  18284. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18285. + */
  18286. +
  18287. +/*
  18288. + * ioctl
  18289. + * plink-management and readdir in userspace.
  18290. + * assist the pathconf(3) wrapper library.
  18291. + * move-down
  18292. + * File-based Hierarchical Storage Management.
  18293. + */
  18294. +
  18295. +#include <linux/compat.h>
  18296. +#include <linux/file.h>
  18297. +#include "aufs.h"
  18298. +
  18299. +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
  18300. +{
  18301. + int err, fd;
  18302. + aufs_bindex_t wbi, bindex, bbot;
  18303. + struct file *h_file;
  18304. + struct super_block *sb;
  18305. + struct dentry *root;
  18306. + struct au_branch *br;
  18307. + struct aufs_wbr_fd wbrfd = {
  18308. + .oflags = au_dir_roflags,
  18309. + .brid = -1
  18310. + };
  18311. + const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
  18312. + | O_NOATIME | O_CLOEXEC;
  18313. +
  18314. + AuDebugOn(wbrfd.oflags & ~valid);
  18315. +
  18316. + if (arg) {
  18317. + err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
  18318. + if (unlikely(err)) {
  18319. + err = -EFAULT;
  18320. + goto out;
  18321. + }
  18322. +
  18323. + err = -EINVAL;
  18324. + AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
  18325. + wbrfd.oflags |= au_dir_roflags;
  18326. + AuDbg("0%o\n", wbrfd.oflags);
  18327. + if (unlikely(wbrfd.oflags & ~valid))
  18328. + goto out;
  18329. + }
  18330. +
  18331. + fd = get_unused_fd_flags(0);
  18332. + err = fd;
  18333. + if (unlikely(fd < 0))
  18334. + goto out;
  18335. +
  18336. + h_file = ERR_PTR(-EINVAL);
  18337. + wbi = 0;
  18338. + br = NULL;
  18339. + sb = path->dentry->d_sb;
  18340. + root = sb->s_root;
  18341. + aufs_read_lock(root, AuLock_IR);
  18342. + bbot = au_sbbot(sb);
  18343. + if (wbrfd.brid >= 0) {
  18344. + wbi = au_br_index(sb, wbrfd.brid);
  18345. + if (unlikely(wbi < 0 || wbi > bbot))
  18346. + goto out_unlock;
  18347. + }
  18348. +
  18349. + h_file = ERR_PTR(-ENOENT);
  18350. + br = au_sbr(sb, wbi);
  18351. + if (!au_br_writable(br->br_perm)) {
  18352. + if (arg)
  18353. + goto out_unlock;
  18354. +
  18355. + bindex = wbi + 1;
  18356. + wbi = -1;
  18357. + for (; bindex <= bbot; bindex++) {
  18358. + br = au_sbr(sb, bindex);
  18359. + if (au_br_writable(br->br_perm)) {
  18360. + wbi = bindex;
  18361. + br = au_sbr(sb, wbi);
  18362. + break;
  18363. + }
  18364. + }
  18365. + }
  18366. + AuDbg("wbi %d\n", wbi);
  18367. + if (wbi >= 0)
  18368. + h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
  18369. + /*force_wr*/0);
  18370. +
  18371. +out_unlock:
  18372. + aufs_read_unlock(root, AuLock_IR);
  18373. + err = PTR_ERR(h_file);
  18374. + if (IS_ERR(h_file))
  18375. + goto out_fd;
  18376. +
  18377. + au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
  18378. + fd_install(fd, h_file);
  18379. + err = fd;
  18380. + goto out; /* success */
  18381. +
  18382. +out_fd:
  18383. + put_unused_fd(fd);
  18384. +out:
  18385. + AuTraceErr(err);
  18386. + return err;
  18387. +}
  18388. +
  18389. +/* ---------------------------------------------------------------------- */
  18390. +
  18391. +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
  18392. +{
  18393. + long err;
  18394. + struct dentry *dentry;
  18395. +
  18396. + switch (cmd) {
  18397. + case AUFS_CTL_RDU:
  18398. + case AUFS_CTL_RDU_INO:
  18399. + err = au_rdu_ioctl(file, cmd, arg);
  18400. + break;
  18401. +
  18402. + case AUFS_CTL_WBR_FD:
  18403. + err = au_wbr_fd(&file->f_path, (void __user *)arg);
  18404. + break;
  18405. +
  18406. + case AUFS_CTL_IBUSY:
  18407. + err = au_ibusy_ioctl(file, arg);
  18408. + break;
  18409. +
  18410. + case AUFS_CTL_BRINFO:
  18411. + err = au_brinfo_ioctl(file, arg);
  18412. + break;
  18413. +
  18414. + case AUFS_CTL_FHSM_FD:
  18415. + dentry = file->f_path.dentry;
  18416. + if (IS_ROOT(dentry))
  18417. + err = au_fhsm_fd(dentry->d_sb, arg);
  18418. + else
  18419. + err = -ENOTTY;
  18420. + break;
  18421. +
  18422. + default:
  18423. + /* do not call the lower */
  18424. + AuDbg("0x%x\n", cmd);
  18425. + err = -ENOTTY;
  18426. + }
  18427. +
  18428. + AuTraceErr(err);
  18429. + return err;
  18430. +}
  18431. +
  18432. +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
  18433. +{
  18434. + long err;
  18435. +
  18436. + switch (cmd) {
  18437. + case AUFS_CTL_MVDOWN:
  18438. + err = au_mvdown(file->f_path.dentry, (void __user *)arg);
  18439. + break;
  18440. +
  18441. + case AUFS_CTL_WBR_FD:
  18442. + err = au_wbr_fd(&file->f_path, (void __user *)arg);
  18443. + break;
  18444. +
  18445. + default:
  18446. + /* do not call the lower */
  18447. + AuDbg("0x%x\n", cmd);
  18448. + err = -ENOTTY;
  18449. + }
  18450. +
  18451. + AuTraceErr(err);
  18452. + return err;
  18453. +}
  18454. +
  18455. +#ifdef CONFIG_COMPAT
  18456. +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
  18457. + unsigned long arg)
  18458. +{
  18459. + long err;
  18460. +
  18461. + switch (cmd) {
  18462. + case AUFS_CTL_RDU:
  18463. + case AUFS_CTL_RDU_INO:
  18464. + err = au_rdu_compat_ioctl(file, cmd, arg);
  18465. + break;
  18466. +
  18467. + case AUFS_CTL_IBUSY:
  18468. + err = au_ibusy_compat_ioctl(file, arg);
  18469. + break;
  18470. +
  18471. + case AUFS_CTL_BRINFO:
  18472. + err = au_brinfo_compat_ioctl(file, arg);
  18473. + break;
  18474. +
  18475. + default:
  18476. + err = aufs_ioctl_dir(file, cmd, arg);
  18477. + }
  18478. +
  18479. + AuTraceErr(err);
  18480. + return err;
  18481. +}
  18482. +
  18483. +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
  18484. + unsigned long arg)
  18485. +{
  18486. + return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
  18487. +}
  18488. +#endif
  18489. --- /dev/null
  18490. +++ linux-4.19/fs/aufs/i_op_add.c 2018-11-20 09:35:15.027195504 +0200
  18491. @@ -0,0 +1,935 @@
  18492. +// SPDX-License-Identifier: GPL-2.0
  18493. +/*
  18494. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  18495. + *
  18496. + * This program, aufs is free software; you can redistribute it and/or modify
  18497. + * it under the terms of the GNU General Public License as published by
  18498. + * the Free Software Foundation; either version 2 of the License, or
  18499. + * (at your option) any later version.
  18500. + *
  18501. + * This program is distributed in the hope that it will be useful,
  18502. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18503. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18504. + * GNU General Public License for more details.
  18505. + *
  18506. + * You should have received a copy of the GNU General Public License
  18507. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18508. + */
  18509. +
  18510. +/*
  18511. + * inode operations (add entry)
  18512. + */
  18513. +
  18514. +#include "aufs.h"
  18515. +
  18516. +/*
  18517. + * final procedure of adding a new entry, except link(2).
  18518. + * remove whiteout, instantiate, copyup the parent dir's times and size
  18519. + * and update version.
  18520. + * if it failed, re-create the removed whiteout.
  18521. + */
  18522. +static int epilog(struct inode *dir, aufs_bindex_t bindex,
  18523. + struct dentry *wh_dentry, struct dentry *dentry)
  18524. +{
  18525. + int err, rerr;
  18526. + aufs_bindex_t bwh;
  18527. + struct path h_path;
  18528. + struct super_block *sb;
  18529. + struct inode *inode, *h_dir;
  18530. + struct dentry *wh;
  18531. +
  18532. + bwh = -1;
  18533. + sb = dir->i_sb;
  18534. + if (wh_dentry) {
  18535. + h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
  18536. + IMustLock(h_dir);
  18537. + AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
  18538. + bwh = au_dbwh(dentry);
  18539. + h_path.dentry = wh_dentry;
  18540. + h_path.mnt = au_sbr_mnt(sb, bindex);
  18541. + err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
  18542. + dentry);
  18543. + if (unlikely(err))
  18544. + goto out;
  18545. + }
  18546. +
  18547. + inode = au_new_inode(dentry, /*must_new*/1);
  18548. + if (!IS_ERR(inode)) {
  18549. + d_instantiate(dentry, inode);
  18550. + dir = d_inode(dentry->d_parent); /* dir inode is locked */
  18551. + IMustLock(dir);
  18552. + au_dir_ts(dir, bindex);
  18553. + inode_inc_iversion(dir);
  18554. + au_fhsm_wrote(sb, bindex, /*force*/0);
  18555. + return 0; /* success */
  18556. + }
  18557. +
  18558. + err = PTR_ERR(inode);
  18559. + if (!wh_dentry)
  18560. + goto out;
  18561. +
  18562. + /* revert */
  18563. + /* dir inode is locked */
  18564. + wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
  18565. + rerr = PTR_ERR(wh);
  18566. + if (IS_ERR(wh)) {
  18567. + AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
  18568. + dentry, err, rerr);
  18569. + err = -EIO;
  18570. + } else
  18571. + dput(wh);
  18572. +
  18573. +out:
  18574. + return err;
  18575. +}
  18576. +
  18577. +static int au_d_may_add(struct dentry *dentry)
  18578. +{
  18579. + int err;
  18580. +
  18581. + err = 0;
  18582. + if (unlikely(d_unhashed(dentry)))
  18583. + err = -ENOENT;
  18584. + if (unlikely(d_really_is_positive(dentry)))
  18585. + err = -EEXIST;
  18586. + return err;
  18587. +}
  18588. +
  18589. +/*
  18590. + * simple tests for the adding inode operations.
  18591. + * following the checks in vfs, plus the parent-child relationship.
  18592. + */
  18593. +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
  18594. + struct dentry *h_parent, int isdir)
  18595. +{
  18596. + int err;
  18597. + umode_t h_mode;
  18598. + struct dentry *h_dentry;
  18599. + struct inode *h_inode;
  18600. +
  18601. + err = -ENAMETOOLONG;
  18602. + if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
  18603. + goto out;
  18604. +
  18605. + h_dentry = au_h_dptr(dentry, bindex);
  18606. + if (d_really_is_negative(dentry)) {
  18607. + err = -EEXIST;
  18608. + if (unlikely(d_is_positive(h_dentry)))
  18609. + goto out;
  18610. + } else {
  18611. + /* rename(2) case */
  18612. + err = -EIO;
  18613. + if (unlikely(d_is_negative(h_dentry)))
  18614. + goto out;
  18615. + h_inode = d_inode(h_dentry);
  18616. + if (unlikely(!h_inode->i_nlink))
  18617. + goto out;
  18618. +
  18619. + h_mode = h_inode->i_mode;
  18620. + if (!isdir) {
  18621. + err = -EISDIR;
  18622. + if (unlikely(S_ISDIR(h_mode)))
  18623. + goto out;
  18624. + } else if (unlikely(!S_ISDIR(h_mode))) {
  18625. + err = -ENOTDIR;
  18626. + goto out;
  18627. + }
  18628. + }
  18629. +
  18630. + err = 0;
  18631. + /* expected parent dir is locked */
  18632. + if (unlikely(h_parent != h_dentry->d_parent))
  18633. + err = -EIO;
  18634. +
  18635. +out:
  18636. + AuTraceErr(err);
  18637. + return err;
  18638. +}
  18639. +
  18640. +/*
  18641. + * initial procedure of adding a new entry.
  18642. + * prepare writable branch and the parent dir, lock it,
  18643. + * and lookup whiteout for the new entry.
  18644. + */
  18645. +static struct dentry*
  18646. +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
  18647. + struct dentry *src_dentry, struct au_pin *pin,
  18648. + struct au_wr_dir_args *wr_dir_args)
  18649. +{
  18650. + struct dentry *wh_dentry, *h_parent;
  18651. + struct super_block *sb;
  18652. + struct au_branch *br;
  18653. + int err;
  18654. + unsigned int udba;
  18655. + aufs_bindex_t bcpup;
  18656. +
  18657. + AuDbg("%pd\n", dentry);
  18658. +
  18659. + err = au_wr_dir(dentry, src_dentry, wr_dir_args);
  18660. + bcpup = err;
  18661. + wh_dentry = ERR_PTR(err);
  18662. + if (unlikely(err < 0))
  18663. + goto out;
  18664. +
  18665. + sb = dentry->d_sb;
  18666. + udba = au_opt_udba(sb);
  18667. + err = au_pin(pin, dentry, bcpup, udba,
  18668. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  18669. + wh_dentry = ERR_PTR(err);
  18670. + if (unlikely(err))
  18671. + goto out;
  18672. +
  18673. + h_parent = au_pinned_h_parent(pin);
  18674. + if (udba != AuOpt_UDBA_NONE
  18675. + && au_dbtop(dentry) == bcpup)
  18676. + err = au_may_add(dentry, bcpup, h_parent,
  18677. + au_ftest_wrdir(wr_dir_args->flags, ISDIR));
  18678. + else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
  18679. + err = -ENAMETOOLONG;
  18680. + wh_dentry = ERR_PTR(err);
  18681. + if (unlikely(err))
  18682. + goto out_unpin;
  18683. +
  18684. + br = au_sbr(sb, bcpup);
  18685. + if (dt) {
  18686. + struct path tmp = {
  18687. + .dentry = h_parent,
  18688. + .mnt = au_br_mnt(br)
  18689. + };
  18690. + au_dtime_store(dt, au_pinned_parent(pin), &tmp);
  18691. + }
  18692. +
  18693. + wh_dentry = NULL;
  18694. + if (bcpup != au_dbwh(dentry))
  18695. + goto out; /* success */
  18696. +
  18697. + /*
  18698. + * ENAMETOOLONG here means that if we allowed create such name, then it
  18699. + * would not be able to removed in the future. So we don't allow such
  18700. + * name here and we don't handle ENAMETOOLONG differently here.
  18701. + */
  18702. + wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
  18703. +
  18704. +out_unpin:
  18705. + if (IS_ERR(wh_dentry))
  18706. + au_unpin(pin);
  18707. +out:
  18708. + return wh_dentry;
  18709. +}
  18710. +
  18711. +/* ---------------------------------------------------------------------- */
  18712. +
  18713. +enum { Mknod, Symlink, Creat };
  18714. +struct simple_arg {
  18715. + int type;
  18716. + union {
  18717. + struct {
  18718. + umode_t mode;
  18719. + bool want_excl;
  18720. + bool try_aopen;
  18721. + struct vfsub_aopen_args *aopen;
  18722. + } c;
  18723. + struct {
  18724. + const char *symname;
  18725. + } s;
  18726. + struct {
  18727. + umode_t mode;
  18728. + dev_t dev;
  18729. + } m;
  18730. + } u;
  18731. +};
  18732. +
  18733. +static int add_simple(struct inode *dir, struct dentry *dentry,
  18734. + struct simple_arg *arg)
  18735. +{
  18736. + int err, rerr;
  18737. + aufs_bindex_t btop;
  18738. + unsigned char created;
  18739. + const unsigned char try_aopen
  18740. + = (arg->type == Creat && arg->u.c.try_aopen);
  18741. + struct vfsub_aopen_args *aopen = arg->u.c.aopen;
  18742. + struct dentry *wh_dentry, *parent;
  18743. + struct inode *h_dir;
  18744. + struct super_block *sb;
  18745. + struct au_branch *br;
  18746. + /* to reduce stack size */
  18747. + struct {
  18748. + struct au_dtime dt;
  18749. + struct au_pin pin;
  18750. + struct path h_path;
  18751. + struct au_wr_dir_args wr_dir_args;
  18752. + } *a;
  18753. +
  18754. + AuDbg("%pd\n", dentry);
  18755. + IMustLock(dir);
  18756. +
  18757. + err = -ENOMEM;
  18758. + a = kmalloc(sizeof(*a), GFP_NOFS);
  18759. + if (unlikely(!a))
  18760. + goto out;
  18761. + a->wr_dir_args.force_btgt = -1;
  18762. + a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
  18763. +
  18764. + parent = dentry->d_parent; /* dir inode is locked */
  18765. + if (!try_aopen) {
  18766. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
  18767. + if (unlikely(err))
  18768. + goto out_free;
  18769. + }
  18770. + err = au_d_may_add(dentry);
  18771. + if (unlikely(err))
  18772. + goto out_unlock;
  18773. + if (!try_aopen)
  18774. + di_write_lock_parent(parent);
  18775. + wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
  18776. + &a->pin, &a->wr_dir_args);
  18777. + err = PTR_ERR(wh_dentry);
  18778. + if (IS_ERR(wh_dentry))
  18779. + goto out_parent;
  18780. +
  18781. + btop = au_dbtop(dentry);
  18782. + sb = dentry->d_sb;
  18783. + br = au_sbr(sb, btop);
  18784. + a->h_path.dentry = au_h_dptr(dentry, btop);
  18785. + a->h_path.mnt = au_br_mnt(br);
  18786. + h_dir = au_pinned_h_dir(&a->pin);
  18787. + switch (arg->type) {
  18788. + case Creat:
  18789. + if (!try_aopen || !h_dir->i_op->atomic_open) {
  18790. + err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
  18791. + arg->u.c.want_excl);
  18792. + created = !err;
  18793. + if (!err && try_aopen)
  18794. + aopen->file->f_mode |= FMODE_CREATED;
  18795. + } else {
  18796. + aopen->br = br;
  18797. + err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
  18798. + AuDbg("err %d\n", err);
  18799. + AuDbgFile(aopen->file);
  18800. + created = err >= 0
  18801. + && !!(aopen->file->f_mode & FMODE_CREATED);
  18802. + }
  18803. + break;
  18804. + case Symlink:
  18805. + err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
  18806. + created = !err;
  18807. + break;
  18808. + case Mknod:
  18809. + err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
  18810. + arg->u.m.dev);
  18811. + created = !err;
  18812. + break;
  18813. + default:
  18814. + BUG();
  18815. + }
  18816. + if (unlikely(err < 0))
  18817. + goto out_unpin;
  18818. +
  18819. + err = epilog(dir, btop, wh_dentry, dentry);
  18820. + if (!err)
  18821. + goto out_unpin; /* success */
  18822. +
  18823. + /* revert */
  18824. + if (created /* && d_is_positive(a->h_path.dentry) */) {
  18825. + /* no delegation since it is just created */
  18826. + rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
  18827. + /*force*/0);
  18828. + if (rerr) {
  18829. + AuIOErr("%pd revert failure(%d, %d)\n",
  18830. + dentry, err, rerr);
  18831. + err = -EIO;
  18832. + }
  18833. + au_dtime_revert(&a->dt);
  18834. + }
  18835. + if (try_aopen && h_dir->i_op->atomic_open
  18836. + && (aopen->file->f_mode & FMODE_OPENED))
  18837. + /* aopen->file is still opened */
  18838. + au_lcnt_dec(&aopen->br->br_nfiles);
  18839. +
  18840. +out_unpin:
  18841. + au_unpin(&a->pin);
  18842. + dput(wh_dentry);
  18843. +out_parent:
  18844. + if (!try_aopen)
  18845. + di_write_unlock(parent);
  18846. +out_unlock:
  18847. + if (unlikely(err)) {
  18848. + au_update_dbtop(dentry);
  18849. + d_drop(dentry);
  18850. + }
  18851. + if (!try_aopen)
  18852. + aufs_read_unlock(dentry, AuLock_DW);
  18853. +out_free:
  18854. + kfree(a);
  18855. +out:
  18856. + return err;
  18857. +}
  18858. +
  18859. +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  18860. + dev_t dev)
  18861. +{
  18862. + struct simple_arg arg = {
  18863. + .type = Mknod,
  18864. + .u.m = {
  18865. + .mode = mode,
  18866. + .dev = dev
  18867. + }
  18868. + };
  18869. + return add_simple(dir, dentry, &arg);
  18870. +}
  18871. +
  18872. +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  18873. +{
  18874. + struct simple_arg arg = {
  18875. + .type = Symlink,
  18876. + .u.s.symname = symname
  18877. + };
  18878. + return add_simple(dir, dentry, &arg);
  18879. +}
  18880. +
  18881. +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  18882. + bool want_excl)
  18883. +{
  18884. + struct simple_arg arg = {
  18885. + .type = Creat,
  18886. + .u.c = {
  18887. + .mode = mode,
  18888. + .want_excl = want_excl
  18889. + }
  18890. + };
  18891. + return add_simple(dir, dentry, &arg);
  18892. +}
  18893. +
  18894. +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
  18895. + struct vfsub_aopen_args *aopen_args)
  18896. +{
  18897. + struct simple_arg arg = {
  18898. + .type = Creat,
  18899. + .u.c = {
  18900. + .mode = aopen_args->create_mode,
  18901. + .want_excl = aopen_args->open_flag & O_EXCL,
  18902. + .try_aopen = true,
  18903. + .aopen = aopen_args
  18904. + }
  18905. + };
  18906. + return add_simple(dir, dentry, &arg);
  18907. +}
  18908. +
  18909. +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
  18910. +{
  18911. + int err;
  18912. + aufs_bindex_t bindex;
  18913. + struct super_block *sb;
  18914. + struct dentry *parent, *h_parent, *h_dentry;
  18915. + struct inode *h_dir, *inode;
  18916. + struct vfsmount *h_mnt;
  18917. + struct au_wr_dir_args wr_dir_args = {
  18918. + .force_btgt = -1,
  18919. + .flags = AuWrDir_TMPFILE
  18920. + };
  18921. +
  18922. + /* copy-up may happen */
  18923. + inode_lock(dir);
  18924. +
  18925. + sb = dir->i_sb;
  18926. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  18927. + if (unlikely(err))
  18928. + goto out;
  18929. +
  18930. + err = au_di_init(dentry);
  18931. + if (unlikely(err))
  18932. + goto out_si;
  18933. +
  18934. + err = -EBUSY;
  18935. + parent = d_find_any_alias(dir);
  18936. + AuDebugOn(!parent);
  18937. + di_write_lock_parent(parent);
  18938. + if (unlikely(d_inode(parent) != dir))
  18939. + goto out_parent;
  18940. +
  18941. + err = au_digen_test(parent, au_sigen(sb));
  18942. + if (unlikely(err))
  18943. + goto out_parent;
  18944. +
  18945. + bindex = au_dbtop(parent);
  18946. + au_set_dbtop(dentry, bindex);
  18947. + au_set_dbbot(dentry, bindex);
  18948. + err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
  18949. + bindex = err;
  18950. + if (unlikely(err < 0))
  18951. + goto out_parent;
  18952. +
  18953. + err = -EOPNOTSUPP;
  18954. + h_dir = au_h_iptr(dir, bindex);
  18955. + if (unlikely(!h_dir->i_op->tmpfile))
  18956. + goto out_parent;
  18957. +
  18958. + h_mnt = au_sbr_mnt(sb, bindex);
  18959. + err = vfsub_mnt_want_write(h_mnt);
  18960. + if (unlikely(err))
  18961. + goto out_parent;
  18962. +
  18963. + h_parent = au_h_dptr(parent, bindex);
  18964. + h_dentry = vfs_tmpfile(h_parent, mode, /*open_flag*/0);
  18965. + if (IS_ERR(h_dentry)) {
  18966. + err = PTR_ERR(h_dentry);
  18967. + goto out_mnt;
  18968. + }
  18969. +
  18970. + au_set_dbtop(dentry, bindex);
  18971. + au_set_dbbot(dentry, bindex);
  18972. + au_set_h_dptr(dentry, bindex, dget(h_dentry));
  18973. + inode = au_new_inode(dentry, /*must_new*/1);
  18974. + if (IS_ERR(inode)) {
  18975. + err = PTR_ERR(inode);
  18976. + au_set_h_dptr(dentry, bindex, NULL);
  18977. + au_set_dbtop(dentry, -1);
  18978. + au_set_dbbot(dentry, -1);
  18979. + } else {
  18980. + if (!inode->i_nlink)
  18981. + set_nlink(inode, 1);
  18982. + d_tmpfile(dentry, inode);
  18983. + au_di(dentry)->di_tmpfile = 1;
  18984. +
  18985. + /* update without i_mutex */
  18986. + if (au_ibtop(dir) == au_dbtop(dentry))
  18987. + au_cpup_attr_timesizes(dir);
  18988. + }
  18989. + dput(h_dentry);
  18990. +
  18991. +out_mnt:
  18992. + vfsub_mnt_drop_write(h_mnt);
  18993. +out_parent:
  18994. + di_write_unlock(parent);
  18995. + dput(parent);
  18996. + di_write_unlock(dentry);
  18997. + if (unlikely(err)) {
  18998. + au_di_fin(dentry);
  18999. + dentry->d_fsdata = NULL;
  19000. + }
  19001. +out_si:
  19002. + si_read_unlock(sb);
  19003. +out:
  19004. + inode_unlock(dir);
  19005. + return err;
  19006. +}
  19007. +
  19008. +/* ---------------------------------------------------------------------- */
  19009. +
  19010. +struct au_link_args {
  19011. + aufs_bindex_t bdst, bsrc;
  19012. + struct au_pin pin;
  19013. + struct path h_path;
  19014. + struct dentry *src_parent, *parent;
  19015. +};
  19016. +
  19017. +static int au_cpup_before_link(struct dentry *src_dentry,
  19018. + struct au_link_args *a)
  19019. +{
  19020. + int err;
  19021. + struct dentry *h_src_dentry;
  19022. + struct au_cp_generic cpg = {
  19023. + .dentry = src_dentry,
  19024. + .bdst = a->bdst,
  19025. + .bsrc = a->bsrc,
  19026. + .len = -1,
  19027. + .pin = &a->pin,
  19028. + .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
  19029. + };
  19030. +
  19031. + di_read_lock_parent(a->src_parent, AuLock_IR);
  19032. + err = au_test_and_cpup_dirs(src_dentry, a->bdst);
  19033. + if (unlikely(err))
  19034. + goto out;
  19035. +
  19036. + h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
  19037. + err = au_pin(&a->pin, src_dentry, a->bdst,
  19038. + au_opt_udba(src_dentry->d_sb),
  19039. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  19040. + if (unlikely(err))
  19041. + goto out;
  19042. +
  19043. + err = au_sio_cpup_simple(&cpg);
  19044. + au_unpin(&a->pin);
  19045. +
  19046. +out:
  19047. + di_read_unlock(a->src_parent, AuLock_IR);
  19048. + return err;
  19049. +}
  19050. +
  19051. +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
  19052. + struct au_link_args *a)
  19053. +{
  19054. + int err;
  19055. + unsigned char plink;
  19056. + aufs_bindex_t bbot;
  19057. + struct dentry *h_src_dentry;
  19058. + struct inode *h_inode, *inode, *delegated;
  19059. + struct super_block *sb;
  19060. + struct file *h_file;
  19061. +
  19062. + plink = 0;
  19063. + h_inode = NULL;
  19064. + sb = src_dentry->d_sb;
  19065. + inode = d_inode(src_dentry);
  19066. + if (au_ibtop(inode) <= a->bdst)
  19067. + h_inode = au_h_iptr(inode, a->bdst);
  19068. + if (!h_inode || !h_inode->i_nlink) {
  19069. + /* copyup src_dentry as the name of dentry. */
  19070. + bbot = au_dbbot(dentry);
  19071. + if (bbot < a->bsrc)
  19072. + au_set_dbbot(dentry, a->bsrc);
  19073. + au_set_h_dptr(dentry, a->bsrc,
  19074. + dget(au_h_dptr(src_dentry, a->bsrc)));
  19075. + dget(a->h_path.dentry);
  19076. + au_set_h_dptr(dentry, a->bdst, NULL);
  19077. + AuDbg("temporary d_inode...\n");
  19078. + spin_lock(&dentry->d_lock);
  19079. + dentry->d_inode = d_inode(src_dentry); /* tmp */
  19080. + spin_unlock(&dentry->d_lock);
  19081. + h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
  19082. + if (IS_ERR(h_file))
  19083. + err = PTR_ERR(h_file);
  19084. + else {
  19085. + struct au_cp_generic cpg = {
  19086. + .dentry = dentry,
  19087. + .bdst = a->bdst,
  19088. + .bsrc = -1,
  19089. + .len = -1,
  19090. + .pin = &a->pin,
  19091. + .flags = AuCpup_KEEPLINO
  19092. + };
  19093. + err = au_sio_cpup_simple(&cpg);
  19094. + au_h_open_post(dentry, a->bsrc, h_file);
  19095. + if (!err) {
  19096. + dput(a->h_path.dentry);
  19097. + a->h_path.dentry = au_h_dptr(dentry, a->bdst);
  19098. + } else
  19099. + au_set_h_dptr(dentry, a->bdst,
  19100. + a->h_path.dentry);
  19101. + }
  19102. + spin_lock(&dentry->d_lock);
  19103. + dentry->d_inode = NULL; /* restore */
  19104. + spin_unlock(&dentry->d_lock);
  19105. + AuDbg("temporary d_inode...done\n");
  19106. + au_set_h_dptr(dentry, a->bsrc, NULL);
  19107. + au_set_dbbot(dentry, bbot);
  19108. + } else {
  19109. + /* the inode of src_dentry already exists on a.bdst branch */
  19110. + h_src_dentry = d_find_alias(h_inode);
  19111. + if (!h_src_dentry && au_plink_test(inode)) {
  19112. + plink = 1;
  19113. + h_src_dentry = au_plink_lkup(inode, a->bdst);
  19114. + err = PTR_ERR(h_src_dentry);
  19115. + if (IS_ERR(h_src_dentry))
  19116. + goto out;
  19117. +
  19118. + if (unlikely(d_is_negative(h_src_dentry))) {
  19119. + dput(h_src_dentry);
  19120. + h_src_dentry = NULL;
  19121. + }
  19122. +
  19123. + }
  19124. + if (h_src_dentry) {
  19125. + delegated = NULL;
  19126. + err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
  19127. + &a->h_path, &delegated);
  19128. + if (unlikely(err == -EWOULDBLOCK)) {
  19129. + pr_warn("cannot retry for NFSv4 delegation"
  19130. + " for an internal link\n");
  19131. + iput(delegated);
  19132. + }
  19133. + dput(h_src_dentry);
  19134. + } else {
  19135. + AuIOErr("no dentry found for hi%lu on b%d\n",
  19136. + h_inode->i_ino, a->bdst);
  19137. + err = -EIO;
  19138. + }
  19139. + }
  19140. +
  19141. + if (!err && !plink)
  19142. + au_plink_append(inode, a->bdst, a->h_path.dentry);
  19143. +
  19144. +out:
  19145. + AuTraceErr(err);
  19146. + return err;
  19147. +}
  19148. +
  19149. +int aufs_link(struct dentry *src_dentry, struct inode *dir,
  19150. + struct dentry *dentry)
  19151. +{
  19152. + int err, rerr;
  19153. + struct au_dtime dt;
  19154. + struct au_link_args *a;
  19155. + struct dentry *wh_dentry, *h_src_dentry;
  19156. + struct inode *inode, *delegated;
  19157. + struct super_block *sb;
  19158. + struct au_wr_dir_args wr_dir_args = {
  19159. + /* .force_btgt = -1, */
  19160. + .flags = AuWrDir_ADD_ENTRY
  19161. + };
  19162. +
  19163. + IMustLock(dir);
  19164. + inode = d_inode(src_dentry);
  19165. + IMustLock(inode);
  19166. +
  19167. + err = -ENOMEM;
  19168. + a = kzalloc(sizeof(*a), GFP_NOFS);
  19169. + if (unlikely(!a))
  19170. + goto out;
  19171. +
  19172. + a->parent = dentry->d_parent; /* dir inode is locked */
  19173. + err = aufs_read_and_write_lock2(dentry, src_dentry,
  19174. + AuLock_NOPLM | AuLock_GEN);
  19175. + if (unlikely(err))
  19176. + goto out_kfree;
  19177. + err = au_d_linkable(src_dentry);
  19178. + if (unlikely(err))
  19179. + goto out_unlock;
  19180. + err = au_d_may_add(dentry);
  19181. + if (unlikely(err))
  19182. + goto out_unlock;
  19183. +
  19184. + a->src_parent = dget_parent(src_dentry);
  19185. + wr_dir_args.force_btgt = au_ibtop(inode);
  19186. +
  19187. + di_write_lock_parent(a->parent);
  19188. + wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
  19189. + wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
  19190. + &wr_dir_args);
  19191. + err = PTR_ERR(wh_dentry);
  19192. + if (IS_ERR(wh_dentry))
  19193. + goto out_parent;
  19194. +
  19195. + err = 0;
  19196. + sb = dentry->d_sb;
  19197. + a->bdst = au_dbtop(dentry);
  19198. + a->h_path.dentry = au_h_dptr(dentry, a->bdst);
  19199. + a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
  19200. + a->bsrc = au_ibtop(inode);
  19201. + h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
  19202. + if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
  19203. + h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
  19204. + if (!h_src_dentry) {
  19205. + a->bsrc = au_dbtop(src_dentry);
  19206. + h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
  19207. + AuDebugOn(!h_src_dentry);
  19208. + } else if (IS_ERR(h_src_dentry)) {
  19209. + err = PTR_ERR(h_src_dentry);
  19210. + goto out_parent;
  19211. + }
  19212. +
  19213. + /*
  19214. + * aufs doesn't touch the credential so
  19215. + * security_dentry_create_files_as() is unnecessary.
  19216. + */
  19217. + if (au_opt_test(au_mntflags(sb), PLINK)) {
  19218. + if (a->bdst < a->bsrc
  19219. + /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
  19220. + err = au_cpup_or_link(src_dentry, dentry, a);
  19221. + else {
  19222. + delegated = NULL;
  19223. + err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
  19224. + &a->h_path, &delegated);
  19225. + if (unlikely(err == -EWOULDBLOCK)) {
  19226. + pr_warn("cannot retry for NFSv4 delegation"
  19227. + " for an internal link\n");
  19228. + iput(delegated);
  19229. + }
  19230. + }
  19231. + dput(h_src_dentry);
  19232. + } else {
  19233. + /*
  19234. + * copyup src_dentry to the branch we process,
  19235. + * and then link(2) to it.
  19236. + */
  19237. + dput(h_src_dentry);
  19238. + if (a->bdst < a->bsrc
  19239. + /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
  19240. + au_unpin(&a->pin);
  19241. + di_write_unlock(a->parent);
  19242. + err = au_cpup_before_link(src_dentry, a);
  19243. + di_write_lock_parent(a->parent);
  19244. + if (!err)
  19245. + err = au_pin(&a->pin, dentry, a->bdst,
  19246. + au_opt_udba(sb),
  19247. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  19248. + if (unlikely(err))
  19249. + goto out_wh;
  19250. + }
  19251. + if (!err) {
  19252. + h_src_dentry = au_h_dptr(src_dentry, a->bdst);
  19253. + err = -ENOENT;
  19254. + if (h_src_dentry && d_is_positive(h_src_dentry)) {
  19255. + delegated = NULL;
  19256. + err = vfsub_link(h_src_dentry,
  19257. + au_pinned_h_dir(&a->pin),
  19258. + &a->h_path, &delegated);
  19259. + if (unlikely(err == -EWOULDBLOCK)) {
  19260. + pr_warn("cannot retry"
  19261. + " for NFSv4 delegation"
  19262. + " for an internal link\n");
  19263. + iput(delegated);
  19264. + }
  19265. + }
  19266. + }
  19267. + }
  19268. + if (unlikely(err))
  19269. + goto out_unpin;
  19270. +
  19271. + if (wh_dentry) {
  19272. + a->h_path.dentry = wh_dentry;
  19273. + err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
  19274. + dentry);
  19275. + if (unlikely(err))
  19276. + goto out_revert;
  19277. + }
  19278. +
  19279. + au_dir_ts(dir, a->bdst);
  19280. + inode_inc_iversion(dir);
  19281. + inc_nlink(inode);
  19282. + inode->i_ctime = dir->i_ctime;
  19283. + d_instantiate(dentry, au_igrab(inode));
  19284. + if (d_unhashed(a->h_path.dentry))
  19285. + /* some filesystem calls d_drop() */
  19286. + d_drop(dentry);
  19287. + /* some filesystems consume an inode even hardlink */
  19288. + au_fhsm_wrote(sb, a->bdst, /*force*/0);
  19289. + goto out_unpin; /* success */
  19290. +
  19291. +out_revert:
  19292. + /* no delegation since it is just created */
  19293. + rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
  19294. + /*delegated*/NULL, /*force*/0);
  19295. + if (unlikely(rerr)) {
  19296. + AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
  19297. + err = -EIO;
  19298. + }
  19299. + au_dtime_revert(&dt);
  19300. +out_unpin:
  19301. + au_unpin(&a->pin);
  19302. +out_wh:
  19303. + dput(wh_dentry);
  19304. +out_parent:
  19305. + di_write_unlock(a->parent);
  19306. + dput(a->src_parent);
  19307. +out_unlock:
  19308. + if (unlikely(err)) {
  19309. + au_update_dbtop(dentry);
  19310. + d_drop(dentry);
  19311. + }
  19312. + aufs_read_and_write_unlock2(dentry, src_dentry);
  19313. +out_kfree:
  19314. + kfree(a);
  19315. +out:
  19316. + AuTraceErr(err);
  19317. + return err;
  19318. +}
  19319. +
  19320. +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  19321. +{
  19322. + int err, rerr;
  19323. + aufs_bindex_t bindex;
  19324. + unsigned char diropq;
  19325. + struct path h_path;
  19326. + struct dentry *wh_dentry, *parent, *opq_dentry;
  19327. + struct inode *h_inode;
  19328. + struct super_block *sb;
  19329. + struct {
  19330. + struct au_pin pin;
  19331. + struct au_dtime dt;
  19332. + } *a; /* reduce the stack usage */
  19333. + struct au_wr_dir_args wr_dir_args = {
  19334. + .force_btgt = -1,
  19335. + .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
  19336. + };
  19337. +
  19338. + IMustLock(dir);
  19339. +
  19340. + err = -ENOMEM;
  19341. + a = kmalloc(sizeof(*a), GFP_NOFS);
  19342. + if (unlikely(!a))
  19343. + goto out;
  19344. +
  19345. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
  19346. + if (unlikely(err))
  19347. + goto out_free;
  19348. + err = au_d_may_add(dentry);
  19349. + if (unlikely(err))
  19350. + goto out_unlock;
  19351. +
  19352. + parent = dentry->d_parent; /* dir inode is locked */
  19353. + di_write_lock_parent(parent);
  19354. + wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
  19355. + &a->pin, &wr_dir_args);
  19356. + err = PTR_ERR(wh_dentry);
  19357. + if (IS_ERR(wh_dentry))
  19358. + goto out_parent;
  19359. +
  19360. + sb = dentry->d_sb;
  19361. + bindex = au_dbtop(dentry);
  19362. + h_path.dentry = au_h_dptr(dentry, bindex);
  19363. + h_path.mnt = au_sbr_mnt(sb, bindex);
  19364. + err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
  19365. + if (unlikely(err))
  19366. + goto out_unpin;
  19367. +
  19368. + /* make the dir opaque */
  19369. + diropq = 0;
  19370. + h_inode = d_inode(h_path.dentry);
  19371. + if (wh_dentry
  19372. + || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
  19373. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  19374. + opq_dentry = au_diropq_create(dentry, bindex);
  19375. + inode_unlock(h_inode);
  19376. + err = PTR_ERR(opq_dentry);
  19377. + if (IS_ERR(opq_dentry))
  19378. + goto out_dir;
  19379. + dput(opq_dentry);
  19380. + diropq = 1;
  19381. + }
  19382. +
  19383. + err = epilog(dir, bindex, wh_dentry, dentry);
  19384. + if (!err) {
  19385. + inc_nlink(dir);
  19386. + goto out_unpin; /* success */
  19387. + }
  19388. +
  19389. + /* revert */
  19390. + if (diropq) {
  19391. + AuLabel(revert opq);
  19392. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  19393. + rerr = au_diropq_remove(dentry, bindex);
  19394. + inode_unlock(h_inode);
  19395. + if (rerr) {
  19396. + AuIOErr("%pd reverting diropq failed(%d, %d)\n",
  19397. + dentry, err, rerr);
  19398. + err = -EIO;
  19399. + }
  19400. + }
  19401. +
  19402. +out_dir:
  19403. + AuLabel(revert dir);
  19404. + rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
  19405. + if (rerr) {
  19406. + AuIOErr("%pd reverting dir failed(%d, %d)\n",
  19407. + dentry, err, rerr);
  19408. + err = -EIO;
  19409. + }
  19410. + au_dtime_revert(&a->dt);
  19411. +out_unpin:
  19412. + au_unpin(&a->pin);
  19413. + dput(wh_dentry);
  19414. +out_parent:
  19415. + di_write_unlock(parent);
  19416. +out_unlock:
  19417. + if (unlikely(err)) {
  19418. + au_update_dbtop(dentry);
  19419. + d_drop(dentry);
  19420. + }
  19421. + aufs_read_unlock(dentry, AuLock_DW);
  19422. +out_free:
  19423. + kfree(a);
  19424. +out:
  19425. + return err;
  19426. +}
  19427. --- /dev/null
  19428. +++ linux-4.19/fs/aufs/i_op.c 2018-11-20 09:35:15.025195504 +0200
  19429. @@ -0,0 +1,1506 @@
  19430. +// SPDX-License-Identifier: GPL-2.0
  19431. +/*
  19432. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  19433. + *
  19434. + * This program, aufs is free software; you can redistribute it and/or modify
  19435. + * it under the terms of the GNU General Public License as published by
  19436. + * the Free Software Foundation; either version 2 of the License, or
  19437. + * (at your option) any later version.
  19438. + *
  19439. + * This program is distributed in the hope that it will be useful,
  19440. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19441. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19442. + * GNU General Public License for more details.
  19443. + *
  19444. + * You should have received a copy of the GNU General Public License
  19445. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19446. + */
  19447. +
  19448. +/*
  19449. + * inode operations (except add/del/rename)
  19450. + */
  19451. +
  19452. +#include <linux/device_cgroup.h>
  19453. +#include <linux/fs_stack.h>
  19454. +#include <linux/namei.h>
  19455. +#include <linux/security.h>
  19456. +#include "aufs.h"
  19457. +
  19458. +static int h_permission(struct inode *h_inode, int mask,
  19459. + struct path *h_path, int brperm)
  19460. +{
  19461. + int err;
  19462. + const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
  19463. +
  19464. + err = -EPERM;
  19465. + if (write_mask && IS_IMMUTABLE(h_inode))
  19466. + goto out;
  19467. +
  19468. + err = -EACCES;
  19469. + if (((mask & MAY_EXEC)
  19470. + && S_ISREG(h_inode->i_mode)
  19471. + && (path_noexec(h_path)
  19472. + || !(h_inode->i_mode & 0111))))
  19473. + goto out;
  19474. +
  19475. + /*
  19476. + * - skip the lower fs test in the case of write to ro branch.
  19477. + * - nfs dir permission write check is optimized, but a policy for
  19478. + * link/rename requires a real check.
  19479. + * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
  19480. + * in this case, generic_permission() returns -EOPNOTSUPP.
  19481. + */
  19482. + if ((write_mask && !au_br_writable(brperm))
  19483. + || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
  19484. + && write_mask && !(mask & MAY_READ))
  19485. + || !h_inode->i_op->permission) {
  19486. + /* AuLabel(generic_permission); */
  19487. + /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
  19488. + err = generic_permission(h_inode, mask);
  19489. + if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
  19490. + err = h_inode->i_op->permission(h_inode, mask);
  19491. + AuTraceErr(err);
  19492. + } else {
  19493. + /* AuLabel(h_inode->permission); */
  19494. + err = h_inode->i_op->permission(h_inode, mask);
  19495. + AuTraceErr(err);
  19496. + }
  19497. +
  19498. + if (!err)
  19499. + err = devcgroup_inode_permission(h_inode, mask);
  19500. + if (!err)
  19501. + err = security_inode_permission(h_inode, mask);
  19502. +
  19503. +#if 0
  19504. + if (!err) {
  19505. + /* todo: do we need to call ima_path_check()? */
  19506. + struct path h_path = {
  19507. + .dentry =
  19508. + .mnt = h_mnt
  19509. + };
  19510. + err = ima_path_check(&h_path,
  19511. + mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
  19512. + IMA_COUNT_LEAVE);
  19513. + }
  19514. +#endif
  19515. +
  19516. +out:
  19517. + return err;
  19518. +}
  19519. +
  19520. +static int aufs_permission(struct inode *inode, int mask)
  19521. +{
  19522. + int err;
  19523. + aufs_bindex_t bindex, bbot;
  19524. + const unsigned char isdir = !!S_ISDIR(inode->i_mode),
  19525. + write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
  19526. + struct inode *h_inode;
  19527. + struct super_block *sb;
  19528. + struct au_branch *br;
  19529. +
  19530. + /* todo: support rcu-walk? */
  19531. + if (mask & MAY_NOT_BLOCK)
  19532. + return -ECHILD;
  19533. +
  19534. + sb = inode->i_sb;
  19535. + si_read_lock(sb, AuLock_FLUSH);
  19536. + ii_read_lock_child(inode);
  19537. +#if 0
  19538. + err = au_iigen_test(inode, au_sigen(sb));
  19539. + if (unlikely(err))
  19540. + goto out;
  19541. +#endif
  19542. +
  19543. + if (!isdir
  19544. + || write_mask
  19545. + || au_opt_test(au_mntflags(sb), DIRPERM1)) {
  19546. + err = au_busy_or_stale();
  19547. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  19548. + if (unlikely(!h_inode
  19549. + || (h_inode->i_mode & S_IFMT)
  19550. + != (inode->i_mode & S_IFMT)))
  19551. + goto out;
  19552. +
  19553. + err = 0;
  19554. + bindex = au_ibtop(inode);
  19555. + br = au_sbr(sb, bindex);
  19556. + err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
  19557. + if (write_mask
  19558. + && !err
  19559. + && !special_file(h_inode->i_mode)) {
  19560. + /* test whether the upper writable branch exists */
  19561. + err = -EROFS;
  19562. + for (; bindex >= 0; bindex--)
  19563. + if (!au_br_rdonly(au_sbr(sb, bindex))) {
  19564. + err = 0;
  19565. + break;
  19566. + }
  19567. + }
  19568. + goto out;
  19569. + }
  19570. +
  19571. + /* non-write to dir */
  19572. + err = 0;
  19573. + bbot = au_ibbot(inode);
  19574. + for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
  19575. + h_inode = au_h_iptr(inode, bindex);
  19576. + if (h_inode) {
  19577. + err = au_busy_or_stale();
  19578. + if (unlikely(!S_ISDIR(h_inode->i_mode)))
  19579. + break;
  19580. +
  19581. + br = au_sbr(sb, bindex);
  19582. + err = h_permission(h_inode, mask, &br->br_path,
  19583. + br->br_perm);
  19584. + }
  19585. + }
  19586. +
  19587. +out:
  19588. + ii_read_unlock(inode);
  19589. + si_read_unlock(sb);
  19590. + return err;
  19591. +}
  19592. +
  19593. +/* ---------------------------------------------------------------------- */
  19594. +
  19595. +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
  19596. + unsigned int flags)
  19597. +{
  19598. + struct dentry *ret, *parent;
  19599. + struct inode *inode;
  19600. + struct super_block *sb;
  19601. + int err, npositive;
  19602. +
  19603. + IMustLock(dir);
  19604. +
  19605. + /* todo: support rcu-walk? */
  19606. + ret = ERR_PTR(-ECHILD);
  19607. + if (flags & LOOKUP_RCU)
  19608. + goto out;
  19609. +
  19610. + ret = ERR_PTR(-ENAMETOOLONG);
  19611. + if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
  19612. + goto out;
  19613. +
  19614. + sb = dir->i_sb;
  19615. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  19616. + ret = ERR_PTR(err);
  19617. + if (unlikely(err))
  19618. + goto out;
  19619. +
  19620. + err = au_di_init(dentry);
  19621. + ret = ERR_PTR(err);
  19622. + if (unlikely(err))
  19623. + goto out_si;
  19624. +
  19625. + inode = NULL;
  19626. + npositive = 0; /* suppress a warning */
  19627. + parent = dentry->d_parent; /* dir inode is locked */
  19628. + di_read_lock_parent(parent, AuLock_IR);
  19629. + err = au_alive_dir(parent);
  19630. + if (!err)
  19631. + err = au_digen_test(parent, au_sigen(sb));
  19632. + if (!err) {
  19633. + /* regardless LOOKUP_CREATE, always ALLOW_NEG */
  19634. + npositive = au_lkup_dentry(dentry, au_dbtop(parent),
  19635. + AuLkup_ALLOW_NEG);
  19636. + err = npositive;
  19637. + }
  19638. + di_read_unlock(parent, AuLock_IR);
  19639. + ret = ERR_PTR(err);
  19640. + if (unlikely(err < 0))
  19641. + goto out_unlock;
  19642. +
  19643. + if (npositive) {
  19644. + inode = au_new_inode(dentry, /*must_new*/0);
  19645. + if (IS_ERR(inode)) {
  19646. + ret = (void *)inode;
  19647. + inode = NULL;
  19648. + goto out_unlock;
  19649. + }
  19650. + }
  19651. +
  19652. + if (inode)
  19653. + atomic_inc(&inode->i_count);
  19654. + ret = d_splice_alias(inode, dentry);
  19655. +#if 0
  19656. + if (unlikely(d_need_lookup(dentry))) {
  19657. + spin_lock(&dentry->d_lock);
  19658. + dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
  19659. + spin_unlock(&dentry->d_lock);
  19660. + } else
  19661. +#endif
  19662. + if (inode) {
  19663. + if (!IS_ERR(ret)) {
  19664. + iput(inode);
  19665. + if (ret && ret != dentry)
  19666. + ii_write_unlock(inode);
  19667. + } else {
  19668. + ii_write_unlock(inode);
  19669. + iput(inode);
  19670. + inode = NULL;
  19671. + }
  19672. + }
  19673. +
  19674. +out_unlock:
  19675. + di_write_unlock(dentry);
  19676. +out_si:
  19677. + si_read_unlock(sb);
  19678. +out:
  19679. + return ret;
  19680. +}
  19681. +
  19682. +/* ---------------------------------------------------------------------- */
  19683. +
  19684. +/*
  19685. + * very dirty and complicated aufs ->atomic_open().
  19686. + * aufs_atomic_open()
  19687. + * + au_aopen_or_create()
  19688. + * + add_simple()
  19689. + * + vfsub_atomic_open()
  19690. + * + branch fs ->atomic_open()
  19691. + * may call the actual 'open' for h_file
  19692. + * + inc br_nfiles only if opened
  19693. + * + au_aopen_no_open() or au_aopen_do_open()
  19694. + *
  19695. + * au_aopen_do_open()
  19696. + * + finish_open()
  19697. + * + au_do_aopen()
  19698. + * + au_do_open() the body of all 'open'
  19699. + * + au_do_open_nondir()
  19700. + * set the passed h_file
  19701. + *
  19702. + * au_aopen_no_open()
  19703. + * + finish_no_open()
  19704. + */
  19705. +
  19706. +struct aopen_node {
  19707. + struct hlist_bl_node hblist;
  19708. + struct file *file, *h_file;
  19709. +};
  19710. +
  19711. +static int au_do_aopen(struct inode *inode, struct file *file)
  19712. +{
  19713. + struct hlist_bl_head *aopen;
  19714. + struct hlist_bl_node *pos;
  19715. + struct aopen_node *node;
  19716. + struct au_do_open_args args = {
  19717. + .aopen = 1,
  19718. + .open = au_do_open_nondir
  19719. + };
  19720. +
  19721. + aopen = &au_sbi(inode->i_sb)->si_aopen;
  19722. + hlist_bl_lock(aopen);
  19723. + hlist_bl_for_each_entry(node, pos, aopen, hblist)
  19724. + if (node->file == file) {
  19725. + args.h_file = node->h_file;
  19726. + break;
  19727. + }
  19728. + hlist_bl_unlock(aopen);
  19729. + /* AuDebugOn(!args.h_file); */
  19730. +
  19731. + return au_do_open(file, &args);
  19732. +}
  19733. +
  19734. +static int au_aopen_do_open(struct file *file, struct dentry *dentry,
  19735. + struct aopen_node *aopen_node)
  19736. +{
  19737. + int err;
  19738. + struct hlist_bl_head *aopen;
  19739. +
  19740. + AuLabel(here);
  19741. + aopen = &au_sbi(dentry->d_sb)->si_aopen;
  19742. + au_hbl_add(&aopen_node->hblist, aopen);
  19743. + err = finish_open(file, dentry, au_do_aopen);
  19744. + au_hbl_del(&aopen_node->hblist, aopen);
  19745. + /* AuDbgFile(file); */
  19746. + AuDbg("%pd%s%s\n", dentry,
  19747. + (file->f_mode & FMODE_CREATED) ? " created" : "",
  19748. + (file->f_mode & FMODE_OPENED) ? " opened" : "");
  19749. +
  19750. + AuTraceErr(err);
  19751. + return err;
  19752. +}
  19753. +
  19754. +static int au_aopen_no_open(struct file *file, struct dentry *dentry)
  19755. +{
  19756. + int err;
  19757. +
  19758. + AuLabel(here);
  19759. + dget(dentry);
  19760. + err = finish_no_open(file, dentry);
  19761. +
  19762. + AuTraceErr(err);
  19763. + return err;
  19764. +}
  19765. +
  19766. +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
  19767. + struct file *file, unsigned int open_flag,
  19768. + umode_t create_mode)
  19769. +{
  19770. + int err, did_open;
  19771. + unsigned int lkup_flags;
  19772. + aufs_bindex_t bindex;
  19773. + struct super_block *sb;
  19774. + struct dentry *parent, *d;
  19775. + struct vfsub_aopen_args args = {
  19776. + .open_flag = open_flag,
  19777. + .create_mode = create_mode
  19778. + };
  19779. + struct aopen_node aopen_node = {
  19780. + .file = file
  19781. + };
  19782. +
  19783. + IMustLock(dir);
  19784. + AuDbg("open_flag 0%o\n", open_flag);
  19785. + AuDbgDentry(dentry);
  19786. +
  19787. + err = 0;
  19788. + if (!au_di(dentry)) {
  19789. + lkup_flags = LOOKUP_OPEN;
  19790. + if (open_flag & O_CREAT)
  19791. + lkup_flags |= LOOKUP_CREATE;
  19792. + d = aufs_lookup(dir, dentry, lkup_flags);
  19793. + if (IS_ERR(d)) {
  19794. + err = PTR_ERR(d);
  19795. + AuTraceErr(err);
  19796. + goto out;
  19797. + } else if (d) {
  19798. + /*
  19799. + * obsoleted dentry found.
  19800. + * another error will be returned later.
  19801. + */
  19802. + d_drop(d);
  19803. + AuDbgDentry(d);
  19804. + dput(d);
  19805. + }
  19806. + AuDbgDentry(dentry);
  19807. + }
  19808. +
  19809. + if (d_is_positive(dentry)
  19810. + || d_unhashed(dentry)
  19811. + || d_unlinked(dentry)
  19812. + || !(open_flag & O_CREAT)) {
  19813. + err = au_aopen_no_open(file, dentry);
  19814. + goto out; /* success */
  19815. + }
  19816. +
  19817. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
  19818. + if (unlikely(err))
  19819. + goto out;
  19820. +
  19821. + sb = dentry->d_sb;
  19822. + parent = dentry->d_parent; /* dir is locked */
  19823. + di_write_lock_parent(parent);
  19824. + err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
  19825. + if (unlikely(err < 0))
  19826. + goto out_parent;
  19827. +
  19828. + AuDbgDentry(dentry);
  19829. + if (d_is_positive(dentry)) {
  19830. + err = au_aopen_no_open(file, dentry);
  19831. + goto out_parent; /* success */
  19832. + }
  19833. +
  19834. + args.file = alloc_empty_file(file->f_flags, current_cred());
  19835. + err = PTR_ERR(args.file);
  19836. + if (IS_ERR(args.file))
  19837. + goto out_parent;
  19838. +
  19839. + bindex = au_dbtop(dentry);
  19840. + err = au_aopen_or_create(dir, dentry, &args);
  19841. + AuTraceErr(err);
  19842. + AuDbgFile(args.file);
  19843. + file->f_mode = args.file->f_mode & ~FMODE_OPENED;
  19844. + did_open = !!(args.file->f_mode & FMODE_OPENED);
  19845. + if (!did_open) {
  19846. + fput(args.file);
  19847. + args.file = NULL;
  19848. + }
  19849. + di_write_unlock(parent);
  19850. + di_write_unlock(dentry);
  19851. + if (unlikely(err < 0)) {
  19852. + if (args.file)
  19853. + fput(args.file);
  19854. + goto out_sb;
  19855. + }
  19856. +
  19857. + if (!did_open)
  19858. + err = au_aopen_no_open(file, dentry);
  19859. + else {
  19860. + aopen_node.h_file = args.file;
  19861. + err = au_aopen_do_open(file, dentry, &aopen_node);
  19862. + }
  19863. + if (unlikely(err < 0)) {
  19864. + if (args.file)
  19865. + fput(args.file);
  19866. + if (did_open)
  19867. + au_lcnt_dec(&args.br->br_nfiles);
  19868. + }
  19869. + goto out_sb; /* success */
  19870. +
  19871. +out_parent:
  19872. + di_write_unlock(parent);
  19873. + di_write_unlock(dentry);
  19874. +out_sb:
  19875. + si_read_unlock(sb);
  19876. +out:
  19877. + AuTraceErr(err);
  19878. + AuDbgFile(file);
  19879. + return err;
  19880. +}
  19881. +
  19882. +
  19883. +/* ---------------------------------------------------------------------- */
  19884. +
  19885. +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
  19886. + const unsigned char add_entry, aufs_bindex_t bcpup,
  19887. + aufs_bindex_t btop)
  19888. +{
  19889. + int err;
  19890. + struct dentry *h_parent;
  19891. + struct inode *h_dir;
  19892. +
  19893. + if (add_entry)
  19894. + IMustLock(d_inode(parent));
  19895. + else
  19896. + di_write_lock_parent(parent);
  19897. +
  19898. + err = 0;
  19899. + if (!au_h_dptr(parent, bcpup)) {
  19900. + if (btop > bcpup)
  19901. + err = au_cpup_dirs(dentry, bcpup);
  19902. + else if (btop < bcpup)
  19903. + err = au_cpdown_dirs(dentry, bcpup);
  19904. + else
  19905. + BUG();
  19906. + }
  19907. + if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
  19908. + h_parent = au_h_dptr(parent, bcpup);
  19909. + h_dir = d_inode(h_parent);
  19910. + inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
  19911. + err = au_lkup_neg(dentry, bcpup, /*wh*/0);
  19912. + /* todo: no unlock here */
  19913. + inode_unlock_shared(h_dir);
  19914. +
  19915. + AuDbg("bcpup %d\n", bcpup);
  19916. + if (!err) {
  19917. + if (d_really_is_negative(dentry))
  19918. + au_set_h_dptr(dentry, btop, NULL);
  19919. + au_update_dbrange(dentry, /*do_put_zero*/0);
  19920. + }
  19921. + }
  19922. +
  19923. + if (!add_entry)
  19924. + di_write_unlock(parent);
  19925. + if (!err)
  19926. + err = bcpup; /* success */
  19927. +
  19928. + AuTraceErr(err);
  19929. + return err;
  19930. +}
  19931. +
  19932. +/*
  19933. + * decide the branch and the parent dir where we will create a new entry.
  19934. + * returns new bindex or an error.
  19935. + * copyup the parent dir if needed.
  19936. + */
  19937. +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
  19938. + struct au_wr_dir_args *args)
  19939. +{
  19940. + int err;
  19941. + unsigned int flags;
  19942. + aufs_bindex_t bcpup, btop, src_btop;
  19943. + const unsigned char add_entry
  19944. + = au_ftest_wrdir(args->flags, ADD_ENTRY)
  19945. + | au_ftest_wrdir(args->flags, TMPFILE);
  19946. + struct super_block *sb;
  19947. + struct dentry *parent;
  19948. + struct au_sbinfo *sbinfo;
  19949. +
  19950. + sb = dentry->d_sb;
  19951. + sbinfo = au_sbi(sb);
  19952. + parent = dget_parent(dentry);
  19953. + btop = au_dbtop(dentry);
  19954. + bcpup = btop;
  19955. + if (args->force_btgt < 0) {
  19956. + if (src_dentry) {
  19957. + src_btop = au_dbtop(src_dentry);
  19958. + if (src_btop < btop)
  19959. + bcpup = src_btop;
  19960. + } else if (add_entry) {
  19961. + flags = 0;
  19962. + if (au_ftest_wrdir(args->flags, ISDIR))
  19963. + au_fset_wbr(flags, DIR);
  19964. + err = AuWbrCreate(sbinfo, dentry, flags);
  19965. + bcpup = err;
  19966. + }
  19967. +
  19968. + if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
  19969. + if (add_entry)
  19970. + err = AuWbrCopyup(sbinfo, dentry);
  19971. + else {
  19972. + if (!IS_ROOT(dentry)) {
  19973. + di_read_lock_parent(parent, !AuLock_IR);
  19974. + err = AuWbrCopyup(sbinfo, dentry);
  19975. + di_read_unlock(parent, !AuLock_IR);
  19976. + } else
  19977. + err = AuWbrCopyup(sbinfo, dentry);
  19978. + }
  19979. + bcpup = err;
  19980. + if (unlikely(err < 0))
  19981. + goto out;
  19982. + }
  19983. + } else {
  19984. + bcpup = args->force_btgt;
  19985. + AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
  19986. + }
  19987. +
  19988. + AuDbg("btop %d, bcpup %d\n", btop, bcpup);
  19989. + err = bcpup;
  19990. + if (bcpup == btop)
  19991. + goto out; /* success */
  19992. +
  19993. + /* copyup the new parent into the branch we process */
  19994. + err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
  19995. + if (err >= 0) {
  19996. + if (d_really_is_negative(dentry)) {
  19997. + au_set_h_dptr(dentry, btop, NULL);
  19998. + au_set_dbtop(dentry, bcpup);
  19999. + au_set_dbbot(dentry, bcpup);
  20000. + }
  20001. + AuDebugOn(add_entry
  20002. + && !au_ftest_wrdir(args->flags, TMPFILE)
  20003. + && !au_h_dptr(dentry, bcpup));
  20004. + }
  20005. +
  20006. +out:
  20007. + dput(parent);
  20008. + return err;
  20009. +}
  20010. +
  20011. +/* ---------------------------------------------------------------------- */
  20012. +
  20013. +void au_pin_hdir_unlock(struct au_pin *p)
  20014. +{
  20015. + if (p->hdir)
  20016. + au_hn_inode_unlock(p->hdir);
  20017. +}
  20018. +
  20019. +int au_pin_hdir_lock(struct au_pin *p)
  20020. +{
  20021. + int err;
  20022. +
  20023. + err = 0;
  20024. + if (!p->hdir)
  20025. + goto out;
  20026. +
  20027. + /* even if an error happens later, keep this lock */
  20028. + au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
  20029. +
  20030. + err = -EBUSY;
  20031. + if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
  20032. + goto out;
  20033. +
  20034. + err = 0;
  20035. + if (p->h_dentry)
  20036. + err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
  20037. + p->h_parent, p->br);
  20038. +
  20039. +out:
  20040. + return err;
  20041. +}
  20042. +
  20043. +int au_pin_hdir_relock(struct au_pin *p)
  20044. +{
  20045. + int err, i;
  20046. + struct inode *h_i;
  20047. + struct dentry *h_d[] = {
  20048. + p->h_dentry,
  20049. + p->h_parent
  20050. + };
  20051. +
  20052. + err = au_pin_hdir_lock(p);
  20053. + if (unlikely(err))
  20054. + goto out;
  20055. +
  20056. + for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
  20057. + if (!h_d[i])
  20058. + continue;
  20059. + if (d_is_positive(h_d[i])) {
  20060. + h_i = d_inode(h_d[i]);
  20061. + err = !h_i->i_nlink;
  20062. + }
  20063. + }
  20064. +
  20065. +out:
  20066. + return err;
  20067. +}
  20068. +
  20069. +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
  20070. +{
  20071. +#if !defined(CONFIG_RWSEM_GENERIC_SPINLOCK) && defined(CONFIG_RWSEM_SPIN_ON_OWNER)
  20072. + p->hdir->hi_inode->i_rwsem.owner = task;
  20073. +#endif
  20074. +}
  20075. +
  20076. +void au_pin_hdir_acquire_nest(struct au_pin *p)
  20077. +{
  20078. + if (p->hdir) {
  20079. + rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
  20080. + p->lsc_hi, 0, NULL, _RET_IP_);
  20081. + au_pin_hdir_set_owner(p, current);
  20082. + }
  20083. +}
  20084. +
  20085. +void au_pin_hdir_release(struct au_pin *p)
  20086. +{
  20087. + if (p->hdir) {
  20088. + au_pin_hdir_set_owner(p, p->task);
  20089. + rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, 1, _RET_IP_);
  20090. + }
  20091. +}
  20092. +
  20093. +struct dentry *au_pinned_h_parent(struct au_pin *pin)
  20094. +{
  20095. + if (pin && pin->parent)
  20096. + return au_h_dptr(pin->parent, pin->bindex);
  20097. + return NULL;
  20098. +}
  20099. +
  20100. +void au_unpin(struct au_pin *p)
  20101. +{
  20102. + if (p->hdir)
  20103. + au_pin_hdir_unlock(p);
  20104. + if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
  20105. + vfsub_mnt_drop_write(p->h_mnt);
  20106. + if (!p->hdir)
  20107. + return;
  20108. +
  20109. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  20110. + di_read_unlock(p->parent, AuLock_IR);
  20111. + iput(p->hdir->hi_inode);
  20112. + dput(p->parent);
  20113. + p->parent = NULL;
  20114. + p->hdir = NULL;
  20115. + p->h_mnt = NULL;
  20116. + /* do not clear p->task */
  20117. +}
  20118. +
  20119. +int au_do_pin(struct au_pin *p)
  20120. +{
  20121. + int err;
  20122. + struct super_block *sb;
  20123. + struct inode *h_dir;
  20124. +
  20125. + err = 0;
  20126. + sb = p->dentry->d_sb;
  20127. + p->br = au_sbr(sb, p->bindex);
  20128. + if (IS_ROOT(p->dentry)) {
  20129. + if (au_ftest_pin(p->flags, MNT_WRITE)) {
  20130. + p->h_mnt = au_br_mnt(p->br);
  20131. + err = vfsub_mnt_want_write(p->h_mnt);
  20132. + if (unlikely(err)) {
  20133. + au_fclr_pin(p->flags, MNT_WRITE);
  20134. + goto out_err;
  20135. + }
  20136. + }
  20137. + goto out;
  20138. + }
  20139. +
  20140. + p->h_dentry = NULL;
  20141. + if (p->bindex <= au_dbbot(p->dentry))
  20142. + p->h_dentry = au_h_dptr(p->dentry, p->bindex);
  20143. +
  20144. + p->parent = dget_parent(p->dentry);
  20145. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  20146. + di_read_lock(p->parent, AuLock_IR, p->lsc_di);
  20147. +
  20148. + h_dir = NULL;
  20149. + p->h_parent = au_h_dptr(p->parent, p->bindex);
  20150. + p->hdir = au_hi(d_inode(p->parent), p->bindex);
  20151. + if (p->hdir)
  20152. + h_dir = p->hdir->hi_inode;
  20153. +
  20154. + /*
  20155. + * udba case, or
  20156. + * if DI_LOCKED is not set, then p->parent may be different
  20157. + * and h_parent can be NULL.
  20158. + */
  20159. + if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
  20160. + err = -EBUSY;
  20161. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  20162. + di_read_unlock(p->parent, AuLock_IR);
  20163. + dput(p->parent);
  20164. + p->parent = NULL;
  20165. + goto out_err;
  20166. + }
  20167. +
  20168. + if (au_ftest_pin(p->flags, MNT_WRITE)) {
  20169. + p->h_mnt = au_br_mnt(p->br);
  20170. + err = vfsub_mnt_want_write(p->h_mnt);
  20171. + if (unlikely(err)) {
  20172. + au_fclr_pin(p->flags, MNT_WRITE);
  20173. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  20174. + di_read_unlock(p->parent, AuLock_IR);
  20175. + dput(p->parent);
  20176. + p->parent = NULL;
  20177. + goto out_err;
  20178. + }
  20179. + }
  20180. +
  20181. + au_igrab(h_dir);
  20182. + err = au_pin_hdir_lock(p);
  20183. + if (!err)
  20184. + goto out; /* success */
  20185. +
  20186. + au_unpin(p);
  20187. +
  20188. +out_err:
  20189. + pr_err("err %d\n", err);
  20190. + err = au_busy_or_stale();
  20191. +out:
  20192. + return err;
  20193. +}
  20194. +
  20195. +void au_pin_init(struct au_pin *p, struct dentry *dentry,
  20196. + aufs_bindex_t bindex, int lsc_di, int lsc_hi,
  20197. + unsigned int udba, unsigned char flags)
  20198. +{
  20199. + p->dentry = dentry;
  20200. + p->udba = udba;
  20201. + p->lsc_di = lsc_di;
  20202. + p->lsc_hi = lsc_hi;
  20203. + p->flags = flags;
  20204. + p->bindex = bindex;
  20205. +
  20206. + p->parent = NULL;
  20207. + p->hdir = NULL;
  20208. + p->h_mnt = NULL;
  20209. +
  20210. + p->h_dentry = NULL;
  20211. + p->h_parent = NULL;
  20212. + p->br = NULL;
  20213. + p->task = current;
  20214. +}
  20215. +
  20216. +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
  20217. + unsigned int udba, unsigned char flags)
  20218. +{
  20219. + au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
  20220. + udba, flags);
  20221. + return au_do_pin(pin);
  20222. +}
  20223. +
  20224. +/* ---------------------------------------------------------------------- */
  20225. +
  20226. +/*
  20227. + * ->setattr() and ->getattr() are called in various cases.
  20228. + * chmod, stat: dentry is revalidated.
  20229. + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
  20230. + * unhashed.
  20231. + * for ->setattr(), ia->ia_file is passed from ftruncate only.
  20232. + */
  20233. +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
  20234. +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
  20235. +{
  20236. + int err;
  20237. + struct dentry *parent;
  20238. +
  20239. + err = 0;
  20240. + if (au_digen_test(dentry, sigen)) {
  20241. + parent = dget_parent(dentry);
  20242. + di_read_lock_parent(parent, AuLock_IR);
  20243. + err = au_refresh_dentry(dentry, parent);
  20244. + di_read_unlock(parent, AuLock_IR);
  20245. + dput(parent);
  20246. + }
  20247. +
  20248. + AuTraceErr(err);
  20249. + return err;
  20250. +}
  20251. +
  20252. +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
  20253. + struct au_icpup_args *a)
  20254. +{
  20255. + int err;
  20256. + loff_t sz;
  20257. + aufs_bindex_t btop, ibtop;
  20258. + struct dentry *hi_wh, *parent;
  20259. + struct inode *inode;
  20260. + struct au_wr_dir_args wr_dir_args = {
  20261. + .force_btgt = -1,
  20262. + .flags = 0
  20263. + };
  20264. +
  20265. + if (d_is_dir(dentry))
  20266. + au_fset_wrdir(wr_dir_args.flags, ISDIR);
  20267. + /* plink or hi_wh() case */
  20268. + btop = au_dbtop(dentry);
  20269. + inode = d_inode(dentry);
  20270. + ibtop = au_ibtop(inode);
  20271. + if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
  20272. + wr_dir_args.force_btgt = ibtop;
  20273. + err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
  20274. + if (unlikely(err < 0))
  20275. + goto out;
  20276. + a->btgt = err;
  20277. + if (err != btop)
  20278. + au_fset_icpup(a->flags, DID_CPUP);
  20279. +
  20280. + err = 0;
  20281. + a->pin_flags = AuPin_MNT_WRITE;
  20282. + parent = NULL;
  20283. + if (!IS_ROOT(dentry)) {
  20284. + au_fset_pin(a->pin_flags, DI_LOCKED);
  20285. + parent = dget_parent(dentry);
  20286. + di_write_lock_parent(parent);
  20287. + }
  20288. +
  20289. + err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
  20290. + if (unlikely(err))
  20291. + goto out_parent;
  20292. +
  20293. + sz = -1;
  20294. + a->h_path.dentry = au_h_dptr(dentry, btop);
  20295. + a->h_inode = d_inode(a->h_path.dentry);
  20296. + if (ia && (ia->ia_valid & ATTR_SIZE)) {
  20297. + inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
  20298. + if (ia->ia_size < i_size_read(a->h_inode))
  20299. + sz = ia->ia_size;
  20300. + inode_unlock_shared(a->h_inode);
  20301. + }
  20302. +
  20303. + hi_wh = NULL;
  20304. + if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
  20305. + hi_wh = au_hi_wh(inode, a->btgt);
  20306. + if (!hi_wh) {
  20307. + struct au_cp_generic cpg = {
  20308. + .dentry = dentry,
  20309. + .bdst = a->btgt,
  20310. + .bsrc = -1,
  20311. + .len = sz,
  20312. + .pin = &a->pin
  20313. + };
  20314. + err = au_sio_cpup_wh(&cpg, /*file*/NULL);
  20315. + if (unlikely(err))
  20316. + goto out_unlock;
  20317. + hi_wh = au_hi_wh(inode, a->btgt);
  20318. + /* todo: revalidate hi_wh? */
  20319. + }
  20320. + }
  20321. +
  20322. + if (parent) {
  20323. + au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
  20324. + di_downgrade_lock(parent, AuLock_IR);
  20325. + dput(parent);
  20326. + parent = NULL;
  20327. + }
  20328. + if (!au_ftest_icpup(a->flags, DID_CPUP))
  20329. + goto out; /* success */
  20330. +
  20331. + if (!d_unhashed(dentry)) {
  20332. + struct au_cp_generic cpg = {
  20333. + .dentry = dentry,
  20334. + .bdst = a->btgt,
  20335. + .bsrc = btop,
  20336. + .len = sz,
  20337. + .pin = &a->pin,
  20338. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  20339. + };
  20340. + err = au_sio_cpup_simple(&cpg);
  20341. + if (!err)
  20342. + a->h_path.dentry = au_h_dptr(dentry, a->btgt);
  20343. + } else if (!hi_wh)
  20344. + a->h_path.dentry = au_h_dptr(dentry, a->btgt);
  20345. + else
  20346. + a->h_path.dentry = hi_wh; /* do not dget here */
  20347. +
  20348. +out_unlock:
  20349. + a->h_inode = d_inode(a->h_path.dentry);
  20350. + if (!err)
  20351. + goto out; /* success */
  20352. + au_unpin(&a->pin);
  20353. +out_parent:
  20354. + if (parent) {
  20355. + di_write_unlock(parent);
  20356. + dput(parent);
  20357. + }
  20358. +out:
  20359. + if (!err)
  20360. + inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
  20361. + return err;
  20362. +}
  20363. +
  20364. +static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
  20365. +{
  20366. + int err;
  20367. + struct inode *inode, *delegated;
  20368. + struct super_block *sb;
  20369. + struct file *file;
  20370. + struct au_icpup_args *a;
  20371. +
  20372. + inode = d_inode(dentry);
  20373. + IMustLock(inode);
  20374. +
  20375. + err = setattr_prepare(dentry, ia);
  20376. + if (unlikely(err))
  20377. + goto out;
  20378. +
  20379. + err = -ENOMEM;
  20380. + a = kzalloc(sizeof(*a), GFP_NOFS);
  20381. + if (unlikely(!a))
  20382. + goto out;
  20383. +
  20384. + if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
  20385. + ia->ia_valid &= ~ATTR_MODE;
  20386. +
  20387. + file = NULL;
  20388. + sb = dentry->d_sb;
  20389. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  20390. + if (unlikely(err))
  20391. + goto out_kfree;
  20392. +
  20393. + if (ia->ia_valid & ATTR_FILE) {
  20394. + /* currently ftruncate(2) only */
  20395. + AuDebugOn(!d_is_reg(dentry));
  20396. + file = ia->ia_file;
  20397. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
  20398. + /*fi_lsc*/0);
  20399. + if (unlikely(err))
  20400. + goto out_si;
  20401. + ia->ia_file = au_hf_top(file);
  20402. + a->udba = AuOpt_UDBA_NONE;
  20403. + } else {
  20404. + /* fchmod() doesn't pass ia_file */
  20405. + a->udba = au_opt_udba(sb);
  20406. + di_write_lock_child(dentry);
  20407. + /* no d_unlinked(), to set UDBA_NONE for root */
  20408. + if (d_unhashed(dentry))
  20409. + a->udba = AuOpt_UDBA_NONE;
  20410. + if (a->udba != AuOpt_UDBA_NONE) {
  20411. + AuDebugOn(IS_ROOT(dentry));
  20412. + err = au_reval_for_attr(dentry, au_sigen(sb));
  20413. + if (unlikely(err))
  20414. + goto out_dentry;
  20415. + }
  20416. + }
  20417. +
  20418. + err = au_pin_and_icpup(dentry, ia, a);
  20419. + if (unlikely(err < 0))
  20420. + goto out_dentry;
  20421. + if (au_ftest_icpup(a->flags, DID_CPUP)) {
  20422. + ia->ia_file = NULL;
  20423. + ia->ia_valid &= ~ATTR_FILE;
  20424. + }
  20425. +
  20426. + a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
  20427. + if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
  20428. + == (ATTR_MODE | ATTR_CTIME)) {
  20429. + err = security_path_chmod(&a->h_path, ia->ia_mode);
  20430. + if (unlikely(err))
  20431. + goto out_unlock;
  20432. + } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
  20433. + && (ia->ia_valid & ATTR_CTIME)) {
  20434. + err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
  20435. + if (unlikely(err))
  20436. + goto out_unlock;
  20437. + }
  20438. +
  20439. + if (ia->ia_valid & ATTR_SIZE) {
  20440. + struct file *f;
  20441. +
  20442. + if (ia->ia_size < i_size_read(inode))
  20443. + /* unmap only */
  20444. + truncate_setsize(inode, ia->ia_size);
  20445. +
  20446. + f = NULL;
  20447. + if (ia->ia_valid & ATTR_FILE)
  20448. + f = ia->ia_file;
  20449. + inode_unlock(a->h_inode);
  20450. + err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
  20451. + inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
  20452. + } else {
  20453. + delegated = NULL;
  20454. + while (1) {
  20455. + err = vfsub_notify_change(&a->h_path, ia, &delegated);
  20456. + if (delegated) {
  20457. + err = break_deleg_wait(&delegated);
  20458. + if (!err)
  20459. + continue;
  20460. + }
  20461. + break;
  20462. + }
  20463. + }
  20464. + /*
  20465. + * regardless aufs 'acl' option setting.
  20466. + * why don't all acl-aware fs call this func from their ->setattr()?
  20467. + */
  20468. + if (!err && (ia->ia_valid & ATTR_MODE))
  20469. + err = vfsub_acl_chmod(a->h_inode, ia->ia_mode);
  20470. + if (!err)
  20471. + au_cpup_attr_changeable(inode);
  20472. +
  20473. +out_unlock:
  20474. + inode_unlock(a->h_inode);
  20475. + au_unpin(&a->pin);
  20476. + if (unlikely(err))
  20477. + au_update_dbtop(dentry);
  20478. +out_dentry:
  20479. + di_write_unlock(dentry);
  20480. + if (file) {
  20481. + fi_write_unlock(file);
  20482. + ia->ia_file = file;
  20483. + ia->ia_valid |= ATTR_FILE;
  20484. + }
  20485. +out_si:
  20486. + si_read_unlock(sb);
  20487. +out_kfree:
  20488. + kfree(a);
  20489. +out:
  20490. + AuTraceErr(err);
  20491. + return err;
  20492. +}
  20493. +
  20494. +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
  20495. +static int au_h_path_to_set_attr(struct dentry *dentry,
  20496. + struct au_icpup_args *a, struct path *h_path)
  20497. +{
  20498. + int err;
  20499. + struct super_block *sb;
  20500. +
  20501. + sb = dentry->d_sb;
  20502. + a->udba = au_opt_udba(sb);
  20503. + /* no d_unlinked(), to set UDBA_NONE for root */
  20504. + if (d_unhashed(dentry))
  20505. + a->udba = AuOpt_UDBA_NONE;
  20506. + if (a->udba != AuOpt_UDBA_NONE) {
  20507. + AuDebugOn(IS_ROOT(dentry));
  20508. + err = au_reval_for_attr(dentry, au_sigen(sb));
  20509. + if (unlikely(err))
  20510. + goto out;
  20511. + }
  20512. + err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
  20513. + if (unlikely(err < 0))
  20514. + goto out;
  20515. +
  20516. + h_path->dentry = a->h_path.dentry;
  20517. + h_path->mnt = au_sbr_mnt(sb, a->btgt);
  20518. +
  20519. +out:
  20520. + return err;
  20521. +}
  20522. +
  20523. +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
  20524. + struct au_sxattr *arg)
  20525. +{
  20526. + int err;
  20527. + struct path h_path;
  20528. + struct super_block *sb;
  20529. + struct au_icpup_args *a;
  20530. + struct inode *h_inode;
  20531. +
  20532. + IMustLock(inode);
  20533. +
  20534. + err = -ENOMEM;
  20535. + a = kzalloc(sizeof(*a), GFP_NOFS);
  20536. + if (unlikely(!a))
  20537. + goto out;
  20538. +
  20539. + sb = dentry->d_sb;
  20540. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  20541. + if (unlikely(err))
  20542. + goto out_kfree;
  20543. +
  20544. + h_path.dentry = NULL; /* silence gcc */
  20545. + di_write_lock_child(dentry);
  20546. + err = au_h_path_to_set_attr(dentry, a, &h_path);
  20547. + if (unlikely(err))
  20548. + goto out_di;
  20549. +
  20550. + inode_unlock(a->h_inode);
  20551. + switch (arg->type) {
  20552. + case AU_XATTR_SET:
  20553. + AuDebugOn(d_is_negative(h_path.dentry));
  20554. + err = vfsub_setxattr(h_path.dentry,
  20555. + arg->u.set.name, arg->u.set.value,
  20556. + arg->u.set.size, arg->u.set.flags);
  20557. + break;
  20558. + case AU_ACL_SET:
  20559. + err = -EOPNOTSUPP;
  20560. + h_inode = d_inode(h_path.dentry);
  20561. + if (h_inode->i_op->set_acl)
  20562. + /* this will call posix_acl_update_mode */
  20563. + err = h_inode->i_op->set_acl(h_inode,
  20564. + arg->u.acl_set.acl,
  20565. + arg->u.acl_set.type);
  20566. + break;
  20567. + }
  20568. + if (!err)
  20569. + au_cpup_attr_timesizes(inode);
  20570. +
  20571. + au_unpin(&a->pin);
  20572. + if (unlikely(err))
  20573. + au_update_dbtop(dentry);
  20574. +
  20575. +out_di:
  20576. + di_write_unlock(dentry);
  20577. + si_read_unlock(sb);
  20578. +out_kfree:
  20579. + kfree(a);
  20580. +out:
  20581. + AuTraceErr(err);
  20582. + return err;
  20583. +}
  20584. +#endif
  20585. +
  20586. +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
  20587. + unsigned int nlink)
  20588. +{
  20589. + unsigned int n;
  20590. +
  20591. + inode->i_mode = st->mode;
  20592. + /* don't i_[ug]id_write() here */
  20593. + inode->i_uid = st->uid;
  20594. + inode->i_gid = st->gid;
  20595. + inode->i_atime = st->atime;
  20596. + inode->i_mtime = st->mtime;
  20597. + inode->i_ctime = st->ctime;
  20598. +
  20599. + au_cpup_attr_nlink(inode, /*force*/0);
  20600. + if (S_ISDIR(inode->i_mode)) {
  20601. + n = inode->i_nlink;
  20602. + n -= nlink;
  20603. + n += st->nlink;
  20604. + smp_mb(); /* for i_nlink */
  20605. + /* 0 can happen */
  20606. + set_nlink(inode, n);
  20607. + }
  20608. +
  20609. + spin_lock(&inode->i_lock);
  20610. + inode->i_blocks = st->blocks;
  20611. + i_size_write(inode, st->size);
  20612. + spin_unlock(&inode->i_lock);
  20613. +}
  20614. +
  20615. +/*
  20616. + * common routine for aufs_getattr() and au_getxattr().
  20617. + * returns zero or negative (an error).
  20618. + * @dentry will be read-locked in success.
  20619. + */
  20620. +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
  20621. + int locked)
  20622. +{
  20623. + int err;
  20624. + unsigned int mnt_flags, sigen;
  20625. + unsigned char udba_none;
  20626. + aufs_bindex_t bindex;
  20627. + struct super_block *sb, *h_sb;
  20628. + struct inode *inode;
  20629. +
  20630. + h_path->mnt = NULL;
  20631. + h_path->dentry = NULL;
  20632. +
  20633. + err = 0;
  20634. + sb = dentry->d_sb;
  20635. + mnt_flags = au_mntflags(sb);
  20636. + udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
  20637. +
  20638. + if (unlikely(locked))
  20639. + goto body; /* skip locking dinfo */
  20640. +
  20641. + /* support fstat(2) */
  20642. + if (!d_unlinked(dentry) && !udba_none) {
  20643. + sigen = au_sigen(sb);
  20644. + err = au_digen_test(dentry, sigen);
  20645. + if (!err) {
  20646. + di_read_lock_child(dentry, AuLock_IR);
  20647. + err = au_dbrange_test(dentry);
  20648. + if (unlikely(err)) {
  20649. + di_read_unlock(dentry, AuLock_IR);
  20650. + goto out;
  20651. + }
  20652. + } else {
  20653. + AuDebugOn(IS_ROOT(dentry));
  20654. + di_write_lock_child(dentry);
  20655. + err = au_dbrange_test(dentry);
  20656. + if (!err)
  20657. + err = au_reval_for_attr(dentry, sigen);
  20658. + if (!err)
  20659. + di_downgrade_lock(dentry, AuLock_IR);
  20660. + else {
  20661. + di_write_unlock(dentry);
  20662. + goto out;
  20663. + }
  20664. + }
  20665. + } else
  20666. + di_read_lock_child(dentry, AuLock_IR);
  20667. +
  20668. +body:
  20669. + inode = d_inode(dentry);
  20670. + bindex = au_ibtop(inode);
  20671. + h_path->mnt = au_sbr_mnt(sb, bindex);
  20672. + h_sb = h_path->mnt->mnt_sb;
  20673. + if (!force
  20674. + && !au_test_fs_bad_iattr(h_sb)
  20675. + && udba_none)
  20676. + goto out; /* success */
  20677. +
  20678. + if (au_dbtop(dentry) == bindex)
  20679. + h_path->dentry = au_h_dptr(dentry, bindex);
  20680. + else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
  20681. + h_path->dentry = au_plink_lkup(inode, bindex);
  20682. + if (IS_ERR(h_path->dentry))
  20683. + /* pretending success */
  20684. + h_path->dentry = NULL;
  20685. + else
  20686. + dput(h_path->dentry);
  20687. + }
  20688. +
  20689. +out:
  20690. + return err;
  20691. +}
  20692. +
  20693. +static int aufs_getattr(const struct path *path, struct kstat *st,
  20694. + u32 request, unsigned int query)
  20695. +{
  20696. + int err;
  20697. + unsigned char positive;
  20698. + struct path h_path;
  20699. + struct dentry *dentry;
  20700. + struct inode *inode;
  20701. + struct super_block *sb;
  20702. +
  20703. + dentry = path->dentry;
  20704. + inode = d_inode(dentry);
  20705. + sb = dentry->d_sb;
  20706. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  20707. + if (unlikely(err))
  20708. + goto out;
  20709. + err = au_h_path_getattr(dentry, /*force*/0, &h_path, /*locked*/0);
  20710. + if (unlikely(err))
  20711. + goto out_si;
  20712. + if (unlikely(!h_path.dentry))
  20713. + /* illegally overlapped or something */
  20714. + goto out_fill; /* pretending success */
  20715. +
  20716. + positive = d_is_positive(h_path.dentry);
  20717. + if (positive)
  20718. + /* no vfsub version */
  20719. + err = vfs_getattr(&h_path, st, request, query);
  20720. + if (!err) {
  20721. + if (positive)
  20722. + au_refresh_iattr(inode, st,
  20723. + d_inode(h_path.dentry)->i_nlink);
  20724. + goto out_fill; /* success */
  20725. + }
  20726. + AuTraceErr(err);
  20727. + goto out_di;
  20728. +
  20729. +out_fill:
  20730. + generic_fillattr(inode, st);
  20731. +out_di:
  20732. + di_read_unlock(dentry, AuLock_IR);
  20733. +out_si:
  20734. + si_read_unlock(sb);
  20735. +out:
  20736. + AuTraceErr(err);
  20737. + return err;
  20738. +}
  20739. +
  20740. +/* ---------------------------------------------------------------------- */
  20741. +
  20742. +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
  20743. + struct delayed_call *done)
  20744. +{
  20745. + const char *ret;
  20746. + struct dentry *h_dentry;
  20747. + struct inode *h_inode;
  20748. + int err;
  20749. + aufs_bindex_t bindex;
  20750. +
  20751. + ret = NULL; /* suppress a warning */
  20752. + err = -ECHILD;
  20753. + if (!dentry)
  20754. + goto out;
  20755. +
  20756. + err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
  20757. + if (unlikely(err))
  20758. + goto out;
  20759. +
  20760. + err = au_d_hashed_positive(dentry);
  20761. + if (unlikely(err))
  20762. + goto out_unlock;
  20763. +
  20764. + err = -EINVAL;
  20765. + inode = d_inode(dentry);
  20766. + bindex = au_ibtop(inode);
  20767. + h_inode = au_h_iptr(inode, bindex);
  20768. + if (unlikely(!h_inode->i_op->get_link))
  20769. + goto out_unlock;
  20770. +
  20771. + err = -EBUSY;
  20772. + h_dentry = NULL;
  20773. + if (au_dbtop(dentry) <= bindex) {
  20774. + h_dentry = au_h_dptr(dentry, bindex);
  20775. + if (h_dentry)
  20776. + dget(h_dentry);
  20777. + }
  20778. + if (!h_dentry) {
  20779. + h_dentry = d_find_any_alias(h_inode);
  20780. + if (IS_ERR(h_dentry)) {
  20781. + err = PTR_ERR(h_dentry);
  20782. + goto out_unlock;
  20783. + }
  20784. + }
  20785. + if (unlikely(!h_dentry))
  20786. + goto out_unlock;
  20787. +
  20788. + err = 0;
  20789. + AuDbg("%ps\n", h_inode->i_op->get_link);
  20790. + AuDbgDentry(h_dentry);
  20791. + ret = vfs_get_link(h_dentry, done);
  20792. + dput(h_dentry);
  20793. + if (IS_ERR(ret))
  20794. + err = PTR_ERR(ret);
  20795. +
  20796. +out_unlock:
  20797. + aufs_read_unlock(dentry, AuLock_IR);
  20798. +out:
  20799. + if (unlikely(err))
  20800. + ret = ERR_PTR(err);
  20801. + AuTraceErrPtr(ret);
  20802. + return ret;
  20803. +}
  20804. +
  20805. +/* ---------------------------------------------------------------------- */
  20806. +
  20807. +static int au_is_special(struct inode *inode)
  20808. +{
  20809. + return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
  20810. +}
  20811. +
  20812. +static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
  20813. + int flags)
  20814. +{
  20815. + int err;
  20816. + aufs_bindex_t bindex;
  20817. + struct super_block *sb;
  20818. + struct inode *h_inode;
  20819. + struct vfsmount *h_mnt;
  20820. +
  20821. + sb = inode->i_sb;
  20822. + WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
  20823. + "unexpected s_flags 0x%lx", sb->s_flags);
  20824. +
  20825. + /* mmap_sem might be acquired already, cf. aufs_mmap() */
  20826. + lockdep_off();
  20827. + si_read_lock(sb, AuLock_FLUSH);
  20828. + ii_write_lock_child(inode);
  20829. +
  20830. + err = 0;
  20831. + bindex = au_ibtop(inode);
  20832. + h_inode = au_h_iptr(inode, bindex);
  20833. + if (!au_test_ro(sb, bindex, inode)) {
  20834. + h_mnt = au_sbr_mnt(sb, bindex);
  20835. + err = vfsub_mnt_want_write(h_mnt);
  20836. + if (!err) {
  20837. + err = vfsub_update_time(h_inode, ts, flags);
  20838. + vfsub_mnt_drop_write(h_mnt);
  20839. + }
  20840. + } else if (au_is_special(h_inode)) {
  20841. + /*
  20842. + * Never copy-up here.
  20843. + * These special files may already be opened and used for
  20844. + * communicating. If we copied it up, then the communication
  20845. + * would be corrupted.
  20846. + */
  20847. + AuWarn1("timestamps for i%lu are ignored "
  20848. + "since it is on readonly branch (hi%lu).\n",
  20849. + inode->i_ino, h_inode->i_ino);
  20850. + } else if (flags & ~S_ATIME) {
  20851. + err = -EIO;
  20852. + AuIOErr1("unexpected flags 0x%x\n", flags);
  20853. + AuDebugOn(1);
  20854. + }
  20855. +
  20856. + if (!err)
  20857. + au_cpup_attr_timesizes(inode);
  20858. + ii_write_unlock(inode);
  20859. + si_read_unlock(sb);
  20860. + lockdep_on();
  20861. +
  20862. + if (!err && (flags & S_VERSION))
  20863. + inode_inc_iversion(inode);
  20864. +
  20865. + return err;
  20866. +}
  20867. +
  20868. +/* ---------------------------------------------------------------------- */
  20869. +
  20870. +/* no getattr version will be set by module.c:aufs_init() */
  20871. +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
  20872. + aufs_iop[] = {
  20873. + [AuIop_SYMLINK] = {
  20874. + .permission = aufs_permission,
  20875. +#ifdef CONFIG_FS_POSIX_ACL
  20876. + .get_acl = aufs_get_acl,
  20877. + .set_acl = aufs_set_acl, /* unsupport for symlink? */
  20878. +#endif
  20879. +
  20880. + .setattr = aufs_setattr,
  20881. + .getattr = aufs_getattr,
  20882. +
  20883. +#ifdef CONFIG_AUFS_XATTR
  20884. + .listxattr = aufs_listxattr,
  20885. +#endif
  20886. +
  20887. + .get_link = aufs_get_link,
  20888. +
  20889. + /* .update_time = aufs_update_time */
  20890. + },
  20891. + [AuIop_DIR] = {
  20892. + .create = aufs_create,
  20893. + .lookup = aufs_lookup,
  20894. + .link = aufs_link,
  20895. + .unlink = aufs_unlink,
  20896. + .symlink = aufs_symlink,
  20897. + .mkdir = aufs_mkdir,
  20898. + .rmdir = aufs_rmdir,
  20899. + .mknod = aufs_mknod,
  20900. + .rename = aufs_rename,
  20901. +
  20902. + .permission = aufs_permission,
  20903. +#ifdef CONFIG_FS_POSIX_ACL
  20904. + .get_acl = aufs_get_acl,
  20905. + .set_acl = aufs_set_acl,
  20906. +#endif
  20907. +
  20908. + .setattr = aufs_setattr,
  20909. + .getattr = aufs_getattr,
  20910. +
  20911. +#ifdef CONFIG_AUFS_XATTR
  20912. + .listxattr = aufs_listxattr,
  20913. +#endif
  20914. +
  20915. + .update_time = aufs_update_time,
  20916. + .atomic_open = aufs_atomic_open,
  20917. + .tmpfile = aufs_tmpfile
  20918. + },
  20919. + [AuIop_OTHER] = {
  20920. + .permission = aufs_permission,
  20921. +#ifdef CONFIG_FS_POSIX_ACL
  20922. + .get_acl = aufs_get_acl,
  20923. + .set_acl = aufs_set_acl,
  20924. +#endif
  20925. +
  20926. + .setattr = aufs_setattr,
  20927. + .getattr = aufs_getattr,
  20928. +
  20929. +#ifdef CONFIG_AUFS_XATTR
  20930. + .listxattr = aufs_listxattr,
  20931. +#endif
  20932. +
  20933. + .update_time = aufs_update_time
  20934. + }
  20935. +};
  20936. --- /dev/null
  20937. +++ linux-4.19/fs/aufs/i_op_del.c 2018-11-20 09:35:15.027195504 +0200
  20938. @@ -0,0 +1,512 @@
  20939. +// SPDX-License-Identifier: GPL-2.0
  20940. +/*
  20941. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  20942. + *
  20943. + * This program, aufs is free software; you can redistribute it and/or modify
  20944. + * it under the terms of the GNU General Public License as published by
  20945. + * the Free Software Foundation; either version 2 of the License, or
  20946. + * (at your option) any later version.
  20947. + *
  20948. + * This program is distributed in the hope that it will be useful,
  20949. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20950. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20951. + * GNU General Public License for more details.
  20952. + *
  20953. + * You should have received a copy of the GNU General Public License
  20954. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20955. + */
  20956. +
  20957. +/*
  20958. + * inode operations (del entry)
  20959. + */
  20960. +
  20961. +#include "aufs.h"
  20962. +
  20963. +/*
  20964. + * decide if a new whiteout for @dentry is necessary or not.
  20965. + * when it is necessary, prepare the parent dir for the upper branch whose
  20966. + * branch index is @bcpup for creation. the actual creation of the whiteout will
  20967. + * be done by caller.
  20968. + * return value:
  20969. + * 0: wh is unnecessary
  20970. + * plus: wh is necessary
  20971. + * minus: error
  20972. + */
  20973. +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
  20974. +{
  20975. + int need_wh, err;
  20976. + aufs_bindex_t btop;
  20977. + struct super_block *sb;
  20978. +
  20979. + sb = dentry->d_sb;
  20980. + btop = au_dbtop(dentry);
  20981. + if (*bcpup < 0) {
  20982. + *bcpup = btop;
  20983. + if (au_test_ro(sb, btop, d_inode(dentry))) {
  20984. + err = AuWbrCopyup(au_sbi(sb), dentry);
  20985. + *bcpup = err;
  20986. + if (unlikely(err < 0))
  20987. + goto out;
  20988. + }
  20989. + } else
  20990. + AuDebugOn(btop < *bcpup
  20991. + || au_test_ro(sb, *bcpup, d_inode(dentry)));
  20992. + AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
  20993. +
  20994. + if (*bcpup != btop) {
  20995. + err = au_cpup_dirs(dentry, *bcpup);
  20996. + if (unlikely(err))
  20997. + goto out;
  20998. + need_wh = 1;
  20999. + } else {
  21000. + struct au_dinfo *dinfo, *tmp;
  21001. +
  21002. + need_wh = -ENOMEM;
  21003. + dinfo = au_di(dentry);
  21004. + tmp = au_di_alloc(sb, AuLsc_DI_TMP);
  21005. + if (tmp) {
  21006. + au_di_cp(tmp, dinfo);
  21007. + au_di_swap(tmp, dinfo);
  21008. + /* returns the number of positive dentries */
  21009. + need_wh = au_lkup_dentry(dentry, btop + 1,
  21010. + /* AuLkup_IGNORE_PERM */ 0);
  21011. + au_di_swap(tmp, dinfo);
  21012. + au_rw_write_unlock(&tmp->di_rwsem);
  21013. + au_di_free(tmp);
  21014. + }
  21015. + }
  21016. + AuDbg("need_wh %d\n", need_wh);
  21017. + err = need_wh;
  21018. +
  21019. +out:
  21020. + return err;
  21021. +}
  21022. +
  21023. +/*
  21024. + * simple tests for the del-entry operations.
  21025. + * following the checks in vfs, plus the parent-child relationship.
  21026. + */
  21027. +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
  21028. + struct dentry *h_parent, int isdir)
  21029. +{
  21030. + int err;
  21031. + umode_t h_mode;
  21032. + struct dentry *h_dentry, *h_latest;
  21033. + struct inode *h_inode;
  21034. +
  21035. + h_dentry = au_h_dptr(dentry, bindex);
  21036. + if (d_really_is_positive(dentry)) {
  21037. + err = -ENOENT;
  21038. + if (unlikely(d_is_negative(h_dentry)))
  21039. + goto out;
  21040. + h_inode = d_inode(h_dentry);
  21041. + if (unlikely(!h_inode->i_nlink))
  21042. + goto out;
  21043. +
  21044. + h_mode = h_inode->i_mode;
  21045. + if (!isdir) {
  21046. + err = -EISDIR;
  21047. + if (unlikely(S_ISDIR(h_mode)))
  21048. + goto out;
  21049. + } else if (unlikely(!S_ISDIR(h_mode))) {
  21050. + err = -ENOTDIR;
  21051. + goto out;
  21052. + }
  21053. + } else {
  21054. + /* rename(2) case */
  21055. + err = -EIO;
  21056. + if (unlikely(d_is_positive(h_dentry)))
  21057. + goto out;
  21058. + }
  21059. +
  21060. + err = -ENOENT;
  21061. + /* expected parent dir is locked */
  21062. + if (unlikely(h_parent != h_dentry->d_parent))
  21063. + goto out;
  21064. + err = 0;
  21065. +
  21066. + /*
  21067. + * rmdir a dir may break the consistency on some filesystem.
  21068. + * let's try heavy test.
  21069. + */
  21070. + err = -EACCES;
  21071. + if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
  21072. + && au_test_h_perm(d_inode(h_parent),
  21073. + MAY_EXEC | MAY_WRITE)))
  21074. + goto out;
  21075. +
  21076. + h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
  21077. + err = -EIO;
  21078. + if (IS_ERR(h_latest))
  21079. + goto out;
  21080. + if (h_latest == h_dentry)
  21081. + err = 0;
  21082. + dput(h_latest);
  21083. +
  21084. +out:
  21085. + return err;
  21086. +}
  21087. +
  21088. +/*
  21089. + * decide the branch where we operate for @dentry. the branch index will be set
  21090. + * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
  21091. + * dir for reverting.
  21092. + * when a new whiteout is necessary, create it.
  21093. + */
  21094. +static struct dentry*
  21095. +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
  21096. + struct au_dtime *dt, struct au_pin *pin)
  21097. +{
  21098. + struct dentry *wh_dentry;
  21099. + struct super_block *sb;
  21100. + struct path h_path;
  21101. + int err, need_wh;
  21102. + unsigned int udba;
  21103. + aufs_bindex_t bcpup;
  21104. +
  21105. + need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
  21106. + wh_dentry = ERR_PTR(need_wh);
  21107. + if (unlikely(need_wh < 0))
  21108. + goto out;
  21109. +
  21110. + sb = dentry->d_sb;
  21111. + udba = au_opt_udba(sb);
  21112. + bcpup = *rbcpup;
  21113. + err = au_pin(pin, dentry, bcpup, udba,
  21114. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  21115. + wh_dentry = ERR_PTR(err);
  21116. + if (unlikely(err))
  21117. + goto out;
  21118. +
  21119. + h_path.dentry = au_pinned_h_parent(pin);
  21120. + if (udba != AuOpt_UDBA_NONE
  21121. + && au_dbtop(dentry) == bcpup) {
  21122. + err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
  21123. + wh_dentry = ERR_PTR(err);
  21124. + if (unlikely(err))
  21125. + goto out_unpin;
  21126. + }
  21127. +
  21128. + h_path.mnt = au_sbr_mnt(sb, bcpup);
  21129. + au_dtime_store(dt, au_pinned_parent(pin), &h_path);
  21130. + wh_dentry = NULL;
  21131. + if (!need_wh)
  21132. + goto out; /* success, no need to create whiteout */
  21133. +
  21134. + wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
  21135. + if (IS_ERR(wh_dentry))
  21136. + goto out_unpin;
  21137. +
  21138. + /* returns with the parent is locked and wh_dentry is dget-ed */
  21139. + goto out; /* success */
  21140. +
  21141. +out_unpin:
  21142. + au_unpin(pin);
  21143. +out:
  21144. + return wh_dentry;
  21145. +}
  21146. +
  21147. +/*
  21148. + * when removing a dir, rename it to a unique temporary whiteout-ed name first
  21149. + * in order to be revertible and save time for removing many child whiteouts
  21150. + * under the dir.
  21151. + * returns 1 when there are too many child whiteout and caller should remove
  21152. + * them asynchronously. returns 0 when the number of children is enough small to
  21153. + * remove now or the branch fs is a remote fs.
  21154. + * otherwise return an error.
  21155. + */
  21156. +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
  21157. + struct au_nhash *whlist, struct inode *dir)
  21158. +{
  21159. + int rmdir_later, err, dirwh;
  21160. + struct dentry *h_dentry;
  21161. + struct super_block *sb;
  21162. + struct inode *inode;
  21163. +
  21164. + sb = dentry->d_sb;
  21165. + SiMustAnyLock(sb);
  21166. + h_dentry = au_h_dptr(dentry, bindex);
  21167. + err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
  21168. + if (unlikely(err))
  21169. + goto out;
  21170. +
  21171. + /* stop monitoring */
  21172. + inode = d_inode(dentry);
  21173. + au_hn_free(au_hi(inode, bindex));
  21174. +
  21175. + if (!au_test_fs_remote(h_dentry->d_sb)) {
  21176. + dirwh = au_sbi(sb)->si_dirwh;
  21177. + rmdir_later = (dirwh <= 1);
  21178. + if (!rmdir_later)
  21179. + rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
  21180. + dirwh);
  21181. + if (rmdir_later)
  21182. + return rmdir_later;
  21183. + }
  21184. +
  21185. + err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
  21186. + if (unlikely(err)) {
  21187. + AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
  21188. + h_dentry, bindex, err);
  21189. + err = 0;
  21190. + }
  21191. +
  21192. +out:
  21193. + AuTraceErr(err);
  21194. + return err;
  21195. +}
  21196. +
  21197. +/*
  21198. + * final procedure for deleting a entry.
  21199. + * maintain dentry and iattr.
  21200. + */
  21201. +static void epilog(struct inode *dir, struct dentry *dentry,
  21202. + aufs_bindex_t bindex)
  21203. +{
  21204. + struct inode *inode;
  21205. +
  21206. + inode = d_inode(dentry);
  21207. + d_drop(dentry);
  21208. + inode->i_ctime = dir->i_ctime;
  21209. +
  21210. + au_dir_ts(dir, bindex);
  21211. + inode_inc_iversion(dir);
  21212. +}
  21213. +
  21214. +/*
  21215. + * when an error happened, remove the created whiteout and revert everything.
  21216. + */
  21217. +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
  21218. + aufs_bindex_t bwh, struct dentry *wh_dentry,
  21219. + struct dentry *dentry, struct au_dtime *dt)
  21220. +{
  21221. + int rerr;
  21222. + struct path h_path = {
  21223. + .dentry = wh_dentry,
  21224. + .mnt = au_sbr_mnt(dir->i_sb, bindex)
  21225. + };
  21226. +
  21227. + rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
  21228. + if (!rerr) {
  21229. + au_set_dbwh(dentry, bwh);
  21230. + au_dtime_revert(dt);
  21231. + return 0;
  21232. + }
  21233. +
  21234. + AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
  21235. + return -EIO;
  21236. +}
  21237. +
  21238. +/* ---------------------------------------------------------------------- */
  21239. +
  21240. +int aufs_unlink(struct inode *dir, struct dentry *dentry)
  21241. +{
  21242. + int err;
  21243. + aufs_bindex_t bwh, bindex, btop;
  21244. + struct inode *inode, *h_dir, *delegated;
  21245. + struct dentry *parent, *wh_dentry;
  21246. + /* to reduce stack size */
  21247. + struct {
  21248. + struct au_dtime dt;
  21249. + struct au_pin pin;
  21250. + struct path h_path;
  21251. + } *a;
  21252. +
  21253. + IMustLock(dir);
  21254. +
  21255. + err = -ENOMEM;
  21256. + a = kmalloc(sizeof(*a), GFP_NOFS);
  21257. + if (unlikely(!a))
  21258. + goto out;
  21259. +
  21260. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
  21261. + if (unlikely(err))
  21262. + goto out_free;
  21263. + err = au_d_hashed_positive(dentry);
  21264. + if (unlikely(err))
  21265. + goto out_unlock;
  21266. + inode = d_inode(dentry);
  21267. + IMustLock(inode);
  21268. + err = -EISDIR;
  21269. + if (unlikely(d_is_dir(dentry)))
  21270. + goto out_unlock; /* possible? */
  21271. +
  21272. + btop = au_dbtop(dentry);
  21273. + bwh = au_dbwh(dentry);
  21274. + bindex = -1;
  21275. + parent = dentry->d_parent; /* dir inode is locked */
  21276. + di_write_lock_parent(parent);
  21277. + wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
  21278. + &a->pin);
  21279. + err = PTR_ERR(wh_dentry);
  21280. + if (IS_ERR(wh_dentry))
  21281. + goto out_parent;
  21282. +
  21283. + a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
  21284. + a->h_path.dentry = au_h_dptr(dentry, btop);
  21285. + dget(a->h_path.dentry);
  21286. + if (bindex == btop) {
  21287. + h_dir = au_pinned_h_dir(&a->pin);
  21288. + delegated = NULL;
  21289. + err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
  21290. + if (unlikely(err == -EWOULDBLOCK)) {
  21291. + pr_warn("cannot retry for NFSv4 delegation"
  21292. + " for an internal unlink\n");
  21293. + iput(delegated);
  21294. + }
  21295. + } else {
  21296. + /* dir inode is locked */
  21297. + h_dir = d_inode(wh_dentry->d_parent);
  21298. + IMustLock(h_dir);
  21299. + err = 0;
  21300. + }
  21301. +
  21302. + if (!err) {
  21303. + vfsub_drop_nlink(inode);
  21304. + epilog(dir, dentry, bindex);
  21305. +
  21306. + /* update target timestamps */
  21307. + if (bindex == btop) {
  21308. + vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
  21309. + /*ignore*/
  21310. + inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
  21311. + } else
  21312. + /* todo: this timestamp may be reverted later */
  21313. + inode->i_ctime = h_dir->i_ctime;
  21314. + goto out_unpin; /* success */
  21315. + }
  21316. +
  21317. + /* revert */
  21318. + if (wh_dentry) {
  21319. + int rerr;
  21320. +
  21321. + rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
  21322. + &a->dt);
  21323. + if (rerr)
  21324. + err = rerr;
  21325. + }
  21326. +
  21327. +out_unpin:
  21328. + au_unpin(&a->pin);
  21329. + dput(wh_dentry);
  21330. + dput(a->h_path.dentry);
  21331. +out_parent:
  21332. + di_write_unlock(parent);
  21333. +out_unlock:
  21334. + aufs_read_unlock(dentry, AuLock_DW);
  21335. +out_free:
  21336. + kfree(a);
  21337. +out:
  21338. + return err;
  21339. +}
  21340. +
  21341. +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
  21342. +{
  21343. + int err, rmdir_later;
  21344. + aufs_bindex_t bwh, bindex, btop;
  21345. + struct inode *inode;
  21346. + struct dentry *parent, *wh_dentry, *h_dentry;
  21347. + struct au_whtmp_rmdir *args;
  21348. + /* to reduce stack size */
  21349. + struct {
  21350. + struct au_dtime dt;
  21351. + struct au_pin pin;
  21352. + } *a;
  21353. +
  21354. + IMustLock(dir);
  21355. +
  21356. + err = -ENOMEM;
  21357. + a = kmalloc(sizeof(*a), GFP_NOFS);
  21358. + if (unlikely(!a))
  21359. + goto out;
  21360. +
  21361. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
  21362. + if (unlikely(err))
  21363. + goto out_free;
  21364. + err = au_alive_dir(dentry);
  21365. + if (unlikely(err))
  21366. + goto out_unlock;
  21367. + inode = d_inode(dentry);
  21368. + IMustLock(inode);
  21369. + err = -ENOTDIR;
  21370. + if (unlikely(!d_is_dir(dentry)))
  21371. + goto out_unlock; /* possible? */
  21372. +
  21373. + err = -ENOMEM;
  21374. + args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
  21375. + if (unlikely(!args))
  21376. + goto out_unlock;
  21377. +
  21378. + parent = dentry->d_parent; /* dir inode is locked */
  21379. + di_write_lock_parent(parent);
  21380. + err = au_test_empty(dentry, &args->whlist);
  21381. + if (unlikely(err))
  21382. + goto out_parent;
  21383. +
  21384. + btop = au_dbtop(dentry);
  21385. + bwh = au_dbwh(dentry);
  21386. + bindex = -1;
  21387. + wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
  21388. + &a->pin);
  21389. + err = PTR_ERR(wh_dentry);
  21390. + if (IS_ERR(wh_dentry))
  21391. + goto out_parent;
  21392. +
  21393. + h_dentry = au_h_dptr(dentry, btop);
  21394. + dget(h_dentry);
  21395. + rmdir_later = 0;
  21396. + if (bindex == btop) {
  21397. + err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
  21398. + if (err > 0) {
  21399. + rmdir_later = err;
  21400. + err = 0;
  21401. + }
  21402. + } else {
  21403. + /* stop monitoring */
  21404. + au_hn_free(au_hi(inode, btop));
  21405. +
  21406. + /* dir inode is locked */
  21407. + IMustLock(d_inode(wh_dentry->d_parent));
  21408. + err = 0;
  21409. + }
  21410. +
  21411. + if (!err) {
  21412. + vfsub_dead_dir(inode);
  21413. + au_set_dbdiropq(dentry, -1);
  21414. + epilog(dir, dentry, bindex);
  21415. +
  21416. + if (rmdir_later) {
  21417. + au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
  21418. + args = NULL;
  21419. + }
  21420. +
  21421. + goto out_unpin; /* success */
  21422. + }
  21423. +
  21424. + /* revert */
  21425. + AuLabel(revert);
  21426. + if (wh_dentry) {
  21427. + int rerr;
  21428. +
  21429. + rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
  21430. + &a->dt);
  21431. + if (rerr)
  21432. + err = rerr;
  21433. + }
  21434. +
  21435. +out_unpin:
  21436. + au_unpin(&a->pin);
  21437. + dput(wh_dentry);
  21438. + dput(h_dentry);
  21439. +out_parent:
  21440. + di_write_unlock(parent);
  21441. + if (args)
  21442. + au_whtmp_rmdir_free(args);
  21443. +out_unlock:
  21444. + aufs_read_unlock(dentry, AuLock_DW);
  21445. +out_free:
  21446. + kfree(a);
  21447. +out:
  21448. + AuTraceErr(err);
  21449. + return err;
  21450. +}
  21451. --- /dev/null
  21452. +++ linux-4.19/fs/aufs/i_op_ren.c 2018-11-20 09:35:15.028195504 +0200
  21453. @@ -0,0 +1,1249 @@
  21454. +// SPDX-License-Identifier: GPL-2.0
  21455. +/*
  21456. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  21457. + *
  21458. + * This program, aufs is free software; you can redistribute it and/or modify
  21459. + * it under the terms of the GNU General Public License as published by
  21460. + * the Free Software Foundation; either version 2 of the License, or
  21461. + * (at your option) any later version.
  21462. + *
  21463. + * This program is distributed in the hope that it will be useful,
  21464. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21465. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21466. + * GNU General Public License for more details.
  21467. + *
  21468. + * You should have received a copy of the GNU General Public License
  21469. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21470. + */
  21471. +
  21472. +/*
  21473. + * inode operation (rename entry)
  21474. + * todo: this is crazy monster
  21475. + */
  21476. +
  21477. +#include "aufs.h"
  21478. +
  21479. +enum { AuSRC, AuDST, AuSrcDst };
  21480. +enum { AuPARENT, AuCHILD, AuParentChild };
  21481. +
  21482. +#define AuRen_ISDIR_SRC 1
  21483. +#define AuRen_ISDIR_DST (1 << 1)
  21484. +#define AuRen_ISSAMEDIR (1 << 2)
  21485. +#define AuRen_WHSRC (1 << 3)
  21486. +#define AuRen_WHDST (1 << 4)
  21487. +#define AuRen_MNT_WRITE (1 << 5)
  21488. +#define AuRen_DT_DSTDIR (1 << 6)
  21489. +#define AuRen_DIROPQ_SRC (1 << 7)
  21490. +#define AuRen_DIROPQ_DST (1 << 8)
  21491. +#define AuRen_DIRREN (1 << 9)
  21492. +#define AuRen_DROPPED_SRC (1 << 10)
  21493. +#define AuRen_DROPPED_DST (1 << 11)
  21494. +#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
  21495. +#define au_fset_ren(flags, name) \
  21496. + do { (flags) |= AuRen_##name; } while (0)
  21497. +#define au_fclr_ren(flags, name) \
  21498. + do { (flags) &= ~AuRen_##name; } while (0)
  21499. +
  21500. +#ifndef CONFIG_AUFS_DIRREN
  21501. +#undef AuRen_DIRREN
  21502. +#define AuRen_DIRREN 0
  21503. +#endif
  21504. +
  21505. +struct au_ren_args {
  21506. + struct {
  21507. + struct dentry *dentry, *h_dentry, *parent, *h_parent,
  21508. + *wh_dentry;
  21509. + struct inode *dir, *inode;
  21510. + struct au_hinode *hdir, *hinode;
  21511. + struct au_dtime dt[AuParentChild];
  21512. + aufs_bindex_t btop, bdiropq;
  21513. + } sd[AuSrcDst];
  21514. +
  21515. +#define src_dentry sd[AuSRC].dentry
  21516. +#define src_dir sd[AuSRC].dir
  21517. +#define src_inode sd[AuSRC].inode
  21518. +#define src_h_dentry sd[AuSRC].h_dentry
  21519. +#define src_parent sd[AuSRC].parent
  21520. +#define src_h_parent sd[AuSRC].h_parent
  21521. +#define src_wh_dentry sd[AuSRC].wh_dentry
  21522. +#define src_hdir sd[AuSRC].hdir
  21523. +#define src_hinode sd[AuSRC].hinode
  21524. +#define src_h_dir sd[AuSRC].hdir->hi_inode
  21525. +#define src_dt sd[AuSRC].dt
  21526. +#define src_btop sd[AuSRC].btop
  21527. +#define src_bdiropq sd[AuSRC].bdiropq
  21528. +
  21529. +#define dst_dentry sd[AuDST].dentry
  21530. +#define dst_dir sd[AuDST].dir
  21531. +#define dst_inode sd[AuDST].inode
  21532. +#define dst_h_dentry sd[AuDST].h_dentry
  21533. +#define dst_parent sd[AuDST].parent
  21534. +#define dst_h_parent sd[AuDST].h_parent
  21535. +#define dst_wh_dentry sd[AuDST].wh_dentry
  21536. +#define dst_hdir sd[AuDST].hdir
  21537. +#define dst_hinode sd[AuDST].hinode
  21538. +#define dst_h_dir sd[AuDST].hdir->hi_inode
  21539. +#define dst_dt sd[AuDST].dt
  21540. +#define dst_btop sd[AuDST].btop
  21541. +#define dst_bdiropq sd[AuDST].bdiropq
  21542. +
  21543. + struct dentry *h_trap;
  21544. + struct au_branch *br;
  21545. + struct path h_path;
  21546. + struct au_nhash whlist;
  21547. + aufs_bindex_t btgt, src_bwh;
  21548. +
  21549. + struct {
  21550. + unsigned short auren_flags;
  21551. + unsigned char flags; /* syscall parameter */
  21552. + unsigned char exchange;
  21553. + } __packed;
  21554. +
  21555. + struct au_whtmp_rmdir *thargs;
  21556. + struct dentry *h_dst;
  21557. + struct au_hinode *h_root;
  21558. +};
  21559. +
  21560. +/* ---------------------------------------------------------------------- */
  21561. +
  21562. +/*
  21563. + * functions for reverting.
  21564. + * when an error happened in a single rename systemcall, we should revert
  21565. + * everything as if nothing happened.
  21566. + * we don't need to revert the copied-up/down the parent dir since they are
  21567. + * harmless.
  21568. + */
  21569. +
  21570. +#define RevertFailure(fmt, ...) do { \
  21571. + AuIOErr("revert failure: " fmt " (%d, %d)\n", \
  21572. + ##__VA_ARGS__, err, rerr); \
  21573. + err = -EIO; \
  21574. +} while (0)
  21575. +
  21576. +static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
  21577. +{
  21578. + int rerr;
  21579. + struct dentry *d;
  21580. +#define src_or_dst(member) a->sd[idx].member
  21581. +
  21582. + d = src_or_dst(dentry); /* {src,dst}_dentry */
  21583. + au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
  21584. + rerr = au_diropq_remove(d, a->btgt);
  21585. + au_hn_inode_unlock(src_or_dst(hinode));
  21586. + au_set_dbdiropq(d, src_or_dst(bdiropq));
  21587. + if (rerr)
  21588. + RevertFailure("remove diropq %pd", d);
  21589. +
  21590. +#undef src_or_dst_
  21591. +}
  21592. +
  21593. +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
  21594. +{
  21595. + if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
  21596. + au_ren_do_rev_diropq(err, a, AuSRC);
  21597. + if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
  21598. + au_ren_do_rev_diropq(err, a, AuDST);
  21599. +}
  21600. +
  21601. +static void au_ren_rev_rename(int err, struct au_ren_args *a)
  21602. +{
  21603. + int rerr;
  21604. + struct inode *delegated;
  21605. +
  21606. + a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
  21607. + a->src_h_parent);
  21608. + rerr = PTR_ERR(a->h_path.dentry);
  21609. + if (IS_ERR(a->h_path.dentry)) {
  21610. + RevertFailure("lkup one %pd", a->src_dentry);
  21611. + return;
  21612. + }
  21613. +
  21614. + delegated = NULL;
  21615. + rerr = vfsub_rename(a->dst_h_dir,
  21616. + au_h_dptr(a->src_dentry, a->btgt),
  21617. + a->src_h_dir, &a->h_path, &delegated, a->flags);
  21618. + if (unlikely(rerr == -EWOULDBLOCK)) {
  21619. + pr_warn("cannot retry for NFSv4 delegation"
  21620. + " for an internal rename\n");
  21621. + iput(delegated);
  21622. + }
  21623. + d_drop(a->h_path.dentry);
  21624. + dput(a->h_path.dentry);
  21625. + /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
  21626. + if (rerr)
  21627. + RevertFailure("rename %pd", a->src_dentry);
  21628. +}
  21629. +
  21630. +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
  21631. +{
  21632. + int rerr;
  21633. + struct inode *delegated;
  21634. +
  21635. + a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
  21636. + a->dst_h_parent);
  21637. + rerr = PTR_ERR(a->h_path.dentry);
  21638. + if (IS_ERR(a->h_path.dentry)) {
  21639. + RevertFailure("lkup one %pd", a->dst_dentry);
  21640. + return;
  21641. + }
  21642. + if (d_is_positive(a->h_path.dentry)) {
  21643. + d_drop(a->h_path.dentry);
  21644. + dput(a->h_path.dentry);
  21645. + return;
  21646. + }
  21647. +
  21648. + delegated = NULL;
  21649. + rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
  21650. + &delegated, a->flags);
  21651. + if (unlikely(rerr == -EWOULDBLOCK)) {
  21652. + pr_warn("cannot retry for NFSv4 delegation"
  21653. + " for an internal rename\n");
  21654. + iput(delegated);
  21655. + }
  21656. + d_drop(a->h_path.dentry);
  21657. + dput(a->h_path.dentry);
  21658. + if (!rerr)
  21659. + au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
  21660. + else
  21661. + RevertFailure("rename %pd", a->h_dst);
  21662. +}
  21663. +
  21664. +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
  21665. +{
  21666. + int rerr;
  21667. +
  21668. + a->h_path.dentry = a->src_wh_dentry;
  21669. + rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
  21670. + au_set_dbwh(a->src_dentry, a->src_bwh);
  21671. + if (rerr)
  21672. + RevertFailure("unlink %pd", a->src_wh_dentry);
  21673. +}
  21674. +#undef RevertFailure
  21675. +
  21676. +/* ---------------------------------------------------------------------- */
  21677. +
  21678. +/*
  21679. + * when we have to copyup the renaming entry, do it with the rename-target name
  21680. + * in order to minimize the cost (the later actual rename is unnecessary).
  21681. + * otherwise rename it on the target branch.
  21682. + */
  21683. +static int au_ren_or_cpup(struct au_ren_args *a)
  21684. +{
  21685. + int err;
  21686. + struct dentry *d;
  21687. + struct inode *delegated;
  21688. +
  21689. + d = a->src_dentry;
  21690. + if (au_dbtop(d) == a->btgt) {
  21691. + a->h_path.dentry = a->dst_h_dentry;
  21692. + AuDebugOn(au_dbtop(d) != a->btgt);
  21693. + delegated = NULL;
  21694. + err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
  21695. + a->dst_h_dir, &a->h_path, &delegated,
  21696. + a->flags);
  21697. + if (unlikely(err == -EWOULDBLOCK)) {
  21698. + pr_warn("cannot retry for NFSv4 delegation"
  21699. + " for an internal rename\n");
  21700. + iput(delegated);
  21701. + }
  21702. + } else
  21703. + BUG();
  21704. +
  21705. + if (!err && a->h_dst)
  21706. + /* it will be set to dinfo later */
  21707. + dget(a->h_dst);
  21708. +
  21709. + return err;
  21710. +}
  21711. +
  21712. +/* cf. aufs_rmdir() */
  21713. +static int au_ren_del_whtmp(struct au_ren_args *a)
  21714. +{
  21715. + int err;
  21716. + struct inode *dir;
  21717. +
  21718. + dir = a->dst_dir;
  21719. + SiMustAnyLock(dir->i_sb);
  21720. + if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
  21721. + au_sbi(dir->i_sb)->si_dirwh)
  21722. + || au_test_fs_remote(a->h_dst->d_sb)) {
  21723. + err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
  21724. + if (unlikely(err))
  21725. + pr_warn("failed removing whtmp dir %pd (%d), "
  21726. + "ignored.\n", a->h_dst, err);
  21727. + } else {
  21728. + au_nhash_wh_free(&a->thargs->whlist);
  21729. + a->thargs->whlist = a->whlist;
  21730. + a->whlist.nh_num = 0;
  21731. + au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
  21732. + dput(a->h_dst);
  21733. + a->thargs = NULL;
  21734. + }
  21735. +
  21736. + return 0;
  21737. +}
  21738. +
  21739. +/* make it 'opaque' dir. */
  21740. +static int au_ren_do_diropq(struct au_ren_args *a, int idx)
  21741. +{
  21742. + int err;
  21743. + struct dentry *d, *diropq;
  21744. +#define src_or_dst(member) a->sd[idx].member
  21745. +
  21746. + err = 0;
  21747. + d = src_or_dst(dentry); /* {src,dst}_dentry */
  21748. + src_or_dst(bdiropq) = au_dbdiropq(d);
  21749. + src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
  21750. + au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
  21751. + diropq = au_diropq_create(d, a->btgt);
  21752. + au_hn_inode_unlock(src_or_dst(hinode));
  21753. + if (IS_ERR(diropq))
  21754. + err = PTR_ERR(diropq);
  21755. + else
  21756. + dput(diropq);
  21757. +
  21758. +#undef src_or_dst_
  21759. + return err;
  21760. +}
  21761. +
  21762. +static int au_ren_diropq(struct au_ren_args *a)
  21763. +{
  21764. + int err;
  21765. + unsigned char always;
  21766. + struct dentry *d;
  21767. +
  21768. + err = 0;
  21769. + d = a->dst_dentry; /* already renamed on the branch */
  21770. + always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
  21771. + if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
  21772. + && !au_ftest_ren(a->auren_flags, DIRREN)
  21773. + && a->btgt != au_dbdiropq(a->src_dentry)
  21774. + && (a->dst_wh_dentry
  21775. + || a->btgt <= au_dbdiropq(d)
  21776. + /* hide the lower to keep xino */
  21777. + /* the lowers may not be a dir, but we hide them anyway */
  21778. + || a->btgt < au_dbbot(d)
  21779. + || always)) {
  21780. + AuDbg("here\n");
  21781. + err = au_ren_do_diropq(a, AuSRC);
  21782. + if (unlikely(err))
  21783. + goto out;
  21784. + au_fset_ren(a->auren_flags, DIROPQ_SRC);
  21785. + }
  21786. + if (!a->exchange)
  21787. + goto out; /* success */
  21788. +
  21789. + d = a->src_dentry; /* already renamed on the branch */
  21790. + if (au_ftest_ren(a->auren_flags, ISDIR_DST)
  21791. + && a->btgt != au_dbdiropq(a->dst_dentry)
  21792. + && (a->btgt < au_dbdiropq(d)
  21793. + || a->btgt < au_dbbot(d)
  21794. + || always)) {
  21795. + AuDbgDentry(a->src_dentry);
  21796. + AuDbgDentry(a->dst_dentry);
  21797. + err = au_ren_do_diropq(a, AuDST);
  21798. + if (unlikely(err))
  21799. + goto out_rev_src;
  21800. + au_fset_ren(a->auren_flags, DIROPQ_DST);
  21801. + }
  21802. + goto out; /* success */
  21803. +
  21804. +out_rev_src:
  21805. + AuDbg("err %d, reverting src\n", err);
  21806. + au_ren_rev_diropq(err, a);
  21807. +out:
  21808. + return err;
  21809. +}
  21810. +
  21811. +static int do_rename(struct au_ren_args *a)
  21812. +{
  21813. + int err;
  21814. + struct dentry *d, *h_d;
  21815. +
  21816. + if (!a->exchange) {
  21817. + /* prepare workqueue args for asynchronous rmdir */
  21818. + h_d = a->dst_h_dentry;
  21819. + if (au_ftest_ren(a->auren_flags, ISDIR_DST)
  21820. + /* && !au_ftest_ren(a->auren_flags, DIRREN) */
  21821. + && d_is_positive(h_d)) {
  21822. + err = -ENOMEM;
  21823. + a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
  21824. + GFP_NOFS);
  21825. + if (unlikely(!a->thargs))
  21826. + goto out;
  21827. + a->h_dst = dget(h_d);
  21828. + }
  21829. +
  21830. + /* create whiteout for src_dentry */
  21831. + if (au_ftest_ren(a->auren_flags, WHSRC)) {
  21832. + a->src_bwh = au_dbwh(a->src_dentry);
  21833. + AuDebugOn(a->src_bwh >= 0);
  21834. + a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
  21835. + a->src_h_parent);
  21836. + err = PTR_ERR(a->src_wh_dentry);
  21837. + if (IS_ERR(a->src_wh_dentry))
  21838. + goto out_thargs;
  21839. + }
  21840. +
  21841. + /* lookup whiteout for dentry */
  21842. + if (au_ftest_ren(a->auren_flags, WHDST)) {
  21843. + h_d = au_wh_lkup(a->dst_h_parent,
  21844. + &a->dst_dentry->d_name, a->br);
  21845. + err = PTR_ERR(h_d);
  21846. + if (IS_ERR(h_d))
  21847. + goto out_whsrc;
  21848. + if (d_is_negative(h_d))
  21849. + dput(h_d);
  21850. + else
  21851. + a->dst_wh_dentry = h_d;
  21852. + }
  21853. +
  21854. + /* rename dentry to tmpwh */
  21855. + if (a->thargs) {
  21856. + err = au_whtmp_ren(a->dst_h_dentry, a->br);
  21857. + if (unlikely(err))
  21858. + goto out_whdst;
  21859. +
  21860. + d = a->dst_dentry;
  21861. + au_set_h_dptr(d, a->btgt, NULL);
  21862. + err = au_lkup_neg(d, a->btgt, /*wh*/0);
  21863. + if (unlikely(err))
  21864. + goto out_whtmp;
  21865. + a->dst_h_dentry = au_h_dptr(d, a->btgt);
  21866. + }
  21867. + }
  21868. +
  21869. + BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
  21870. +#if 0
  21871. + BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
  21872. + && d_is_positive(a->dst_h_dentry)
  21873. + && a->src_btop != a->btgt);
  21874. +#endif
  21875. +
  21876. + /* rename by vfs_rename or cpup */
  21877. + err = au_ren_or_cpup(a);
  21878. + if (unlikely(err))
  21879. + /* leave the copied-up one */
  21880. + goto out_whtmp;
  21881. +
  21882. + /* make dir opaque */
  21883. + err = au_ren_diropq(a);
  21884. + if (unlikely(err))
  21885. + goto out_rename;
  21886. +
  21887. + /* update target timestamps */
  21888. + if (a->exchange) {
  21889. + AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
  21890. + a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
  21891. + vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
  21892. + a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
  21893. + }
  21894. + AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
  21895. + a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
  21896. + vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
  21897. + a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
  21898. +
  21899. + if (!a->exchange) {
  21900. + /* remove whiteout for dentry */
  21901. + if (a->dst_wh_dentry) {
  21902. + a->h_path.dentry = a->dst_wh_dentry;
  21903. + err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
  21904. + a->dst_dentry);
  21905. + if (unlikely(err))
  21906. + goto out_diropq;
  21907. + }
  21908. +
  21909. + /* remove whtmp */
  21910. + if (a->thargs)
  21911. + au_ren_del_whtmp(a); /* ignore this error */
  21912. +
  21913. + au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
  21914. + }
  21915. + err = 0;
  21916. + goto out_success;
  21917. +
  21918. +out_diropq:
  21919. + au_ren_rev_diropq(err, a);
  21920. +out_rename:
  21921. + au_ren_rev_rename(err, a);
  21922. + dput(a->h_dst);
  21923. +out_whtmp:
  21924. + if (a->thargs)
  21925. + au_ren_rev_whtmp(err, a);
  21926. +out_whdst:
  21927. + dput(a->dst_wh_dentry);
  21928. + a->dst_wh_dentry = NULL;
  21929. +out_whsrc:
  21930. + if (a->src_wh_dentry)
  21931. + au_ren_rev_whsrc(err, a);
  21932. +out_success:
  21933. + dput(a->src_wh_dentry);
  21934. + dput(a->dst_wh_dentry);
  21935. +out_thargs:
  21936. + if (a->thargs) {
  21937. + dput(a->h_dst);
  21938. + au_whtmp_rmdir_free(a->thargs);
  21939. + a->thargs = NULL;
  21940. + }
  21941. +out:
  21942. + return err;
  21943. +}
  21944. +
  21945. +/* ---------------------------------------------------------------------- */
  21946. +
  21947. +/*
  21948. + * test if @dentry dir can be rename destination or not.
  21949. + * success means, it is a logically empty dir.
  21950. + */
  21951. +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
  21952. +{
  21953. + return au_test_empty(dentry, whlist);
  21954. +}
  21955. +
  21956. +/*
  21957. + * test if @a->src_dentry dir can be rename source or not.
  21958. + * if it can, return 0.
  21959. + * success means,
  21960. + * - it is a logically empty dir.
  21961. + * - or, it exists on writable branch and has no children including whiteouts
  21962. + * on the lower branch unless DIRREN is on.
  21963. + */
  21964. +static int may_rename_srcdir(struct au_ren_args *a)
  21965. +{
  21966. + int err;
  21967. + unsigned int rdhash;
  21968. + aufs_bindex_t btop, btgt;
  21969. + struct dentry *dentry;
  21970. + struct super_block *sb;
  21971. + struct au_sbinfo *sbinfo;
  21972. +
  21973. + dentry = a->src_dentry;
  21974. + sb = dentry->d_sb;
  21975. + sbinfo = au_sbi(sb);
  21976. + if (au_opt_test(sbinfo->si_mntflags, DIRREN))
  21977. + au_fset_ren(a->auren_flags, DIRREN);
  21978. +
  21979. + btgt = a->btgt;
  21980. + btop = au_dbtop(dentry);
  21981. + if (btop != btgt) {
  21982. + struct au_nhash whlist;
  21983. +
  21984. + SiMustAnyLock(sb);
  21985. + rdhash = sbinfo->si_rdhash;
  21986. + if (!rdhash)
  21987. + rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
  21988. + dentry));
  21989. + err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
  21990. + if (unlikely(err))
  21991. + goto out;
  21992. + err = au_test_empty(dentry, &whlist);
  21993. + au_nhash_wh_free(&whlist);
  21994. + goto out;
  21995. + }
  21996. +
  21997. + if (btop == au_dbtaildir(dentry))
  21998. + return 0; /* success */
  21999. +
  22000. + err = au_test_empty_lower(dentry);
  22001. +
  22002. +out:
  22003. + if (err == -ENOTEMPTY) {
  22004. + if (au_ftest_ren(a->auren_flags, DIRREN)) {
  22005. + err = 0;
  22006. + } else {
  22007. + AuWarn1("renaming dir who has child(ren) on multiple "
  22008. + "branches, is not supported\n");
  22009. + err = -EXDEV;
  22010. + }
  22011. + }
  22012. + return err;
  22013. +}
  22014. +
  22015. +/* side effect: sets whlist and h_dentry */
  22016. +static int au_ren_may_dir(struct au_ren_args *a)
  22017. +{
  22018. + int err;
  22019. + unsigned int rdhash;
  22020. + struct dentry *d;
  22021. +
  22022. + d = a->dst_dentry;
  22023. + SiMustAnyLock(d->d_sb);
  22024. +
  22025. + err = 0;
  22026. + if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
  22027. + rdhash = au_sbi(d->d_sb)->si_rdhash;
  22028. + if (!rdhash)
  22029. + rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
  22030. + err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
  22031. + if (unlikely(err))
  22032. + goto out;
  22033. +
  22034. + if (!a->exchange) {
  22035. + au_set_dbtop(d, a->dst_btop);
  22036. + err = may_rename_dstdir(d, &a->whlist);
  22037. + au_set_dbtop(d, a->btgt);
  22038. + } else
  22039. + err = may_rename_srcdir(a);
  22040. + }
  22041. + a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
  22042. + if (unlikely(err))
  22043. + goto out;
  22044. +
  22045. + d = a->src_dentry;
  22046. + a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
  22047. + if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
  22048. + err = may_rename_srcdir(a);
  22049. + if (unlikely(err)) {
  22050. + au_nhash_wh_free(&a->whlist);
  22051. + a->whlist.nh_num = 0;
  22052. + }
  22053. + }
  22054. +out:
  22055. + return err;
  22056. +}
  22057. +
  22058. +/* ---------------------------------------------------------------------- */
  22059. +
  22060. +/*
  22061. + * simple tests for rename.
  22062. + * following the checks in vfs, plus the parent-child relationship.
  22063. + */
  22064. +static int au_may_ren(struct au_ren_args *a)
  22065. +{
  22066. + int err, isdir;
  22067. + struct inode *h_inode;
  22068. +
  22069. + if (a->src_btop == a->btgt) {
  22070. + err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
  22071. + au_ftest_ren(a->auren_flags, ISDIR_SRC));
  22072. + if (unlikely(err))
  22073. + goto out;
  22074. + err = -EINVAL;
  22075. + if (unlikely(a->src_h_dentry == a->h_trap))
  22076. + goto out;
  22077. + }
  22078. +
  22079. + err = 0;
  22080. + if (a->dst_btop != a->btgt)
  22081. + goto out;
  22082. +
  22083. + err = -ENOTEMPTY;
  22084. + if (unlikely(a->dst_h_dentry == a->h_trap))
  22085. + goto out;
  22086. +
  22087. + err = -EIO;
  22088. + isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
  22089. + if (d_really_is_negative(a->dst_dentry)) {
  22090. + if (d_is_negative(a->dst_h_dentry))
  22091. + err = au_may_add(a->dst_dentry, a->btgt,
  22092. + a->dst_h_parent, isdir);
  22093. + } else {
  22094. + if (unlikely(d_is_negative(a->dst_h_dentry)))
  22095. + goto out;
  22096. + h_inode = d_inode(a->dst_h_dentry);
  22097. + if (h_inode->i_nlink)
  22098. + err = au_may_del(a->dst_dentry, a->btgt,
  22099. + a->dst_h_parent, isdir);
  22100. + }
  22101. +
  22102. +out:
  22103. + if (unlikely(err == -ENOENT || err == -EEXIST))
  22104. + err = -EIO;
  22105. + AuTraceErr(err);
  22106. + return err;
  22107. +}
  22108. +
  22109. +/* ---------------------------------------------------------------------- */
  22110. +
  22111. +/*
  22112. + * locking order
  22113. + * (VFS)
  22114. + * - src_dir and dir by lock_rename()
  22115. + * - inode if exists
  22116. + * (aufs)
  22117. + * - lock all
  22118. + * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
  22119. + * + si_read_lock
  22120. + * + di_write_lock2_child()
  22121. + * + di_write_lock_child()
  22122. + * + ii_write_lock_child()
  22123. + * + di_write_lock_child2()
  22124. + * + ii_write_lock_child2()
  22125. + * + src_parent and parent
  22126. + * + di_write_lock_parent()
  22127. + * + ii_write_lock_parent()
  22128. + * + di_write_lock_parent2()
  22129. + * + ii_write_lock_parent2()
  22130. + * + lower src_dir and dir by vfsub_lock_rename()
  22131. + * + verify the every relationships between child and parent. if any
  22132. + * of them failed, unlock all and return -EBUSY.
  22133. + */
  22134. +static void au_ren_unlock(struct au_ren_args *a)
  22135. +{
  22136. + vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
  22137. + a->dst_h_parent, a->dst_hdir);
  22138. + if (au_ftest_ren(a->auren_flags, DIRREN)
  22139. + && a->h_root)
  22140. + au_hn_inode_unlock(a->h_root);
  22141. + if (au_ftest_ren(a->auren_flags, MNT_WRITE))
  22142. + vfsub_mnt_drop_write(au_br_mnt(a->br));
  22143. +}
  22144. +
  22145. +static int au_ren_lock(struct au_ren_args *a)
  22146. +{
  22147. + int err;
  22148. + unsigned int udba;
  22149. +
  22150. + err = 0;
  22151. + a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
  22152. + a->src_hdir = au_hi(a->src_dir, a->btgt);
  22153. + a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
  22154. + a->dst_hdir = au_hi(a->dst_dir, a->btgt);
  22155. +
  22156. + err = vfsub_mnt_want_write(au_br_mnt(a->br));
  22157. + if (unlikely(err))
  22158. + goto out;
  22159. + au_fset_ren(a->auren_flags, MNT_WRITE);
  22160. + if (au_ftest_ren(a->auren_flags, DIRREN)) {
  22161. + struct dentry *root;
  22162. + struct inode *dir;
  22163. +
  22164. + /*
  22165. + * sbinfo is already locked, so this ii_read_lock is
  22166. + * unnecessary. but our debugging feature checks it.
  22167. + */
  22168. + root = a->src_inode->i_sb->s_root;
  22169. + if (root != a->src_parent && root != a->dst_parent) {
  22170. + dir = d_inode(root);
  22171. + ii_read_lock_parent3(dir);
  22172. + a->h_root = au_hi(dir, a->btgt);
  22173. + ii_read_unlock(dir);
  22174. + au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
  22175. + }
  22176. + }
  22177. + a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
  22178. + a->dst_h_parent, a->dst_hdir);
  22179. + udba = au_opt_udba(a->src_dentry->d_sb);
  22180. + if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
  22181. + || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
  22182. + err = au_busy_or_stale();
  22183. + if (!err && au_dbtop(a->src_dentry) == a->btgt)
  22184. + err = au_h_verify(a->src_h_dentry, udba,
  22185. + d_inode(a->src_h_parent), a->src_h_parent,
  22186. + a->br);
  22187. + if (!err && au_dbtop(a->dst_dentry) == a->btgt)
  22188. + err = au_h_verify(a->dst_h_dentry, udba,
  22189. + d_inode(a->dst_h_parent), a->dst_h_parent,
  22190. + a->br);
  22191. + if (!err)
  22192. + goto out; /* success */
  22193. +
  22194. + err = au_busy_or_stale();
  22195. + au_ren_unlock(a);
  22196. +
  22197. +out:
  22198. + return err;
  22199. +}
  22200. +
  22201. +/* ---------------------------------------------------------------------- */
  22202. +
  22203. +static void au_ren_refresh_dir(struct au_ren_args *a)
  22204. +{
  22205. + struct inode *dir;
  22206. +
  22207. + dir = a->dst_dir;
  22208. + inode_inc_iversion(dir);
  22209. + if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
  22210. + /* is this updating defined in POSIX? */
  22211. + au_cpup_attr_timesizes(a->src_inode);
  22212. + au_cpup_attr_nlink(dir, /*force*/1);
  22213. + }
  22214. + au_dir_ts(dir, a->btgt);
  22215. +
  22216. + if (a->exchange) {
  22217. + dir = a->src_dir;
  22218. + inode_inc_iversion(dir);
  22219. + if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
  22220. + /* is this updating defined in POSIX? */
  22221. + au_cpup_attr_timesizes(a->dst_inode);
  22222. + au_cpup_attr_nlink(dir, /*force*/1);
  22223. + }
  22224. + au_dir_ts(dir, a->btgt);
  22225. + }
  22226. +
  22227. + if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
  22228. + return;
  22229. +
  22230. + dir = a->src_dir;
  22231. + inode_inc_iversion(dir);
  22232. + if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
  22233. + au_cpup_attr_nlink(dir, /*force*/1);
  22234. + au_dir_ts(dir, a->btgt);
  22235. +}
  22236. +
  22237. +static void au_ren_refresh(struct au_ren_args *a)
  22238. +{
  22239. + aufs_bindex_t bbot, bindex;
  22240. + struct dentry *d, *h_d;
  22241. + struct inode *i, *h_i;
  22242. + struct super_block *sb;
  22243. +
  22244. + d = a->dst_dentry;
  22245. + d_drop(d);
  22246. + if (a->h_dst)
  22247. + /* already dget-ed by au_ren_or_cpup() */
  22248. + au_set_h_dptr(d, a->btgt, a->h_dst);
  22249. +
  22250. + i = a->dst_inode;
  22251. + if (i) {
  22252. + if (!a->exchange) {
  22253. + if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
  22254. + vfsub_drop_nlink(i);
  22255. + else {
  22256. + vfsub_dead_dir(i);
  22257. + au_cpup_attr_timesizes(i);
  22258. + }
  22259. + au_update_dbrange(d, /*do_put_zero*/1);
  22260. + } else
  22261. + au_cpup_attr_nlink(i, /*force*/1);
  22262. + } else {
  22263. + bbot = a->btgt;
  22264. + for (bindex = au_dbtop(d); bindex < bbot; bindex++)
  22265. + au_set_h_dptr(d, bindex, NULL);
  22266. + bbot = au_dbbot(d);
  22267. + for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
  22268. + au_set_h_dptr(d, bindex, NULL);
  22269. + au_update_dbrange(d, /*do_put_zero*/0);
  22270. + }
  22271. +
  22272. + if (a->exchange
  22273. + || au_ftest_ren(a->auren_flags, DIRREN)) {
  22274. + d_drop(a->src_dentry);
  22275. + if (au_ftest_ren(a->auren_flags, DIRREN))
  22276. + au_set_dbwh(a->src_dentry, -1);
  22277. + return;
  22278. + }
  22279. +
  22280. + d = a->src_dentry;
  22281. + au_set_dbwh(d, -1);
  22282. + bbot = au_dbbot(d);
  22283. + for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
  22284. + h_d = au_h_dptr(d, bindex);
  22285. + if (h_d)
  22286. + au_set_h_dptr(d, bindex, NULL);
  22287. + }
  22288. + au_set_dbbot(d, a->btgt);
  22289. +
  22290. + sb = d->d_sb;
  22291. + i = a->src_inode;
  22292. + if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
  22293. + return; /* success */
  22294. +
  22295. + bbot = au_ibbot(i);
  22296. + for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
  22297. + h_i = au_h_iptr(i, bindex);
  22298. + if (h_i) {
  22299. + au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
  22300. + /* ignore this error */
  22301. + au_set_h_iptr(i, bindex, NULL, 0);
  22302. + }
  22303. + }
  22304. + au_set_ibbot(i, a->btgt);
  22305. +}
  22306. +
  22307. +/* ---------------------------------------------------------------------- */
  22308. +
  22309. +/* mainly for link(2) and rename(2) */
  22310. +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
  22311. +{
  22312. + aufs_bindex_t bdiropq, bwh;
  22313. + struct dentry *parent;
  22314. + struct au_branch *br;
  22315. +
  22316. + parent = dentry->d_parent;
  22317. + IMustLock(d_inode(parent)); /* dir is locked */
  22318. +
  22319. + bdiropq = au_dbdiropq(parent);
  22320. + bwh = au_dbwh(dentry);
  22321. + br = au_sbr(dentry->d_sb, btgt);
  22322. + if (au_br_rdonly(br)
  22323. + || (0 <= bdiropq && bdiropq < btgt)
  22324. + || (0 <= bwh && bwh < btgt))
  22325. + btgt = -1;
  22326. +
  22327. + AuDbg("btgt %d\n", btgt);
  22328. + return btgt;
  22329. +}
  22330. +
  22331. +/* sets src_btop, dst_btop and btgt */
  22332. +static int au_ren_wbr(struct au_ren_args *a)
  22333. +{
  22334. + int err;
  22335. + struct au_wr_dir_args wr_dir_args = {
  22336. + /* .force_btgt = -1, */
  22337. + .flags = AuWrDir_ADD_ENTRY
  22338. + };
  22339. +
  22340. + a->src_btop = au_dbtop(a->src_dentry);
  22341. + a->dst_btop = au_dbtop(a->dst_dentry);
  22342. + if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
  22343. + || au_ftest_ren(a->auren_flags, ISDIR_DST))
  22344. + au_fset_wrdir(wr_dir_args.flags, ISDIR);
  22345. + wr_dir_args.force_btgt = a->src_btop;
  22346. + if (a->dst_inode && a->dst_btop < a->src_btop)
  22347. + wr_dir_args.force_btgt = a->dst_btop;
  22348. + wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
  22349. + err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
  22350. + a->btgt = err;
  22351. + if (a->exchange)
  22352. + au_update_dbtop(a->dst_dentry);
  22353. +
  22354. + return err;
  22355. +}
  22356. +
  22357. +static void au_ren_dt(struct au_ren_args *a)
  22358. +{
  22359. + a->h_path.dentry = a->src_h_parent;
  22360. + au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
  22361. + if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
  22362. + a->h_path.dentry = a->dst_h_parent;
  22363. + au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
  22364. + }
  22365. +
  22366. + au_fclr_ren(a->auren_flags, DT_DSTDIR);
  22367. + if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
  22368. + && !a->exchange)
  22369. + return;
  22370. +
  22371. + a->h_path.dentry = a->src_h_dentry;
  22372. + au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
  22373. + if (d_is_positive(a->dst_h_dentry)) {
  22374. + au_fset_ren(a->auren_flags, DT_DSTDIR);
  22375. + a->h_path.dentry = a->dst_h_dentry;
  22376. + au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
  22377. + }
  22378. +}
  22379. +
  22380. +static void au_ren_rev_dt(int err, struct au_ren_args *a)
  22381. +{
  22382. + struct dentry *h_d;
  22383. + struct inode *h_inode;
  22384. +
  22385. + au_dtime_revert(a->src_dt + AuPARENT);
  22386. + if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
  22387. + au_dtime_revert(a->dst_dt + AuPARENT);
  22388. +
  22389. + if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
  22390. + h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
  22391. + h_inode = d_inode(h_d);
  22392. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  22393. + au_dtime_revert(a->src_dt + AuCHILD);
  22394. + inode_unlock(h_inode);
  22395. +
  22396. + if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
  22397. + h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
  22398. + h_inode = d_inode(h_d);
  22399. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  22400. + au_dtime_revert(a->dst_dt + AuCHILD);
  22401. + inode_unlock(h_inode);
  22402. + }
  22403. + }
  22404. +}
  22405. +
  22406. +/* ---------------------------------------------------------------------- */
  22407. +
  22408. +int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
  22409. + struct inode *_dst_dir, struct dentry *_dst_dentry,
  22410. + unsigned int _flags)
  22411. +{
  22412. + int err, lock_flags;
  22413. + void *rev;
  22414. + /* reduce stack space */
  22415. + struct au_ren_args *a;
  22416. + struct au_pin pin;
  22417. +
  22418. + AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
  22419. + IMustLock(_src_dir);
  22420. + IMustLock(_dst_dir);
  22421. +
  22422. + err = -EINVAL;
  22423. + if (unlikely(_flags & RENAME_WHITEOUT))
  22424. + goto out;
  22425. +
  22426. + err = -ENOMEM;
  22427. + BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
  22428. + a = kzalloc(sizeof(*a), GFP_NOFS);
  22429. + if (unlikely(!a))
  22430. + goto out;
  22431. +
  22432. + a->flags = _flags;
  22433. + BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
  22434. + && RENAME_EXCHANGE > U8_MAX);
  22435. + a->exchange = _flags & RENAME_EXCHANGE;
  22436. + a->src_dir = _src_dir;
  22437. + a->src_dentry = _src_dentry;
  22438. + a->src_inode = NULL;
  22439. + if (d_really_is_positive(a->src_dentry))
  22440. + a->src_inode = d_inode(a->src_dentry);
  22441. + a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
  22442. + a->dst_dir = _dst_dir;
  22443. + a->dst_dentry = _dst_dentry;
  22444. + a->dst_inode = NULL;
  22445. + if (d_really_is_positive(a->dst_dentry))
  22446. + a->dst_inode = d_inode(a->dst_dentry);
  22447. + a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
  22448. + if (a->dst_inode) {
  22449. + /*
  22450. + * if EXCHANGE && src is non-dir && dst is dir,
  22451. + * dst is not locked.
  22452. + */
  22453. + /* IMustLock(a->dst_inode); */
  22454. + au_igrab(a->dst_inode);
  22455. + }
  22456. +
  22457. + err = -ENOTDIR;
  22458. + lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
  22459. + if (d_is_dir(a->src_dentry)) {
  22460. + au_fset_ren(a->auren_flags, ISDIR_SRC);
  22461. + if (unlikely(!a->exchange
  22462. + && d_really_is_positive(a->dst_dentry)
  22463. + && !d_is_dir(a->dst_dentry)))
  22464. + goto out_free;
  22465. + lock_flags |= AuLock_DIRS;
  22466. + }
  22467. + if (a->dst_inode && d_is_dir(a->dst_dentry)) {
  22468. + au_fset_ren(a->auren_flags, ISDIR_DST);
  22469. + if (unlikely(!a->exchange
  22470. + && d_really_is_positive(a->src_dentry)
  22471. + && !d_is_dir(a->src_dentry)))
  22472. + goto out_free;
  22473. + lock_flags |= AuLock_DIRS;
  22474. + }
  22475. + err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
  22476. + lock_flags);
  22477. + if (unlikely(err))
  22478. + goto out_free;
  22479. +
  22480. + err = au_d_hashed_positive(a->src_dentry);
  22481. + if (unlikely(err))
  22482. + goto out_unlock;
  22483. + err = -ENOENT;
  22484. + if (a->dst_inode) {
  22485. + /*
  22486. + * If it is a dir, VFS unhash it before this
  22487. + * function. It means we cannot rely upon d_unhashed().
  22488. + */
  22489. + if (unlikely(!a->dst_inode->i_nlink))
  22490. + goto out_unlock;
  22491. + if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
  22492. + err = au_d_hashed_positive(a->dst_dentry);
  22493. + if (unlikely(err && !a->exchange))
  22494. + goto out_unlock;
  22495. + } else if (unlikely(IS_DEADDIR(a->dst_inode)))
  22496. + goto out_unlock;
  22497. + } else if (unlikely(d_unhashed(a->dst_dentry)))
  22498. + goto out_unlock;
  22499. +
  22500. + /*
  22501. + * is it possible?
  22502. + * yes, it happened (in linux-3.3-rcN) but I don't know why.
  22503. + * there may exist a problem somewhere else.
  22504. + */
  22505. + err = -EINVAL;
  22506. + if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
  22507. + goto out_unlock;
  22508. +
  22509. + au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
  22510. + di_write_lock_parent(a->dst_parent);
  22511. +
  22512. + /* which branch we process */
  22513. + err = au_ren_wbr(a);
  22514. + if (unlikely(err < 0))
  22515. + goto out_parent;
  22516. + a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
  22517. + a->h_path.mnt = au_br_mnt(a->br);
  22518. +
  22519. + /* are they available to be renamed */
  22520. + err = au_ren_may_dir(a);
  22521. + if (unlikely(err))
  22522. + goto out_children;
  22523. +
  22524. + /* prepare the writable parent dir on the same branch */
  22525. + if (a->dst_btop == a->btgt) {
  22526. + au_fset_ren(a->auren_flags, WHDST);
  22527. + } else {
  22528. + err = au_cpup_dirs(a->dst_dentry, a->btgt);
  22529. + if (unlikely(err))
  22530. + goto out_children;
  22531. + }
  22532. +
  22533. + err = 0;
  22534. + if (!a->exchange) {
  22535. + if (a->src_dir != a->dst_dir) {
  22536. + /*
  22537. + * this temporary unlock is safe,
  22538. + * because both dir->i_mutex are locked.
  22539. + */
  22540. + di_write_unlock(a->dst_parent);
  22541. + di_write_lock_parent(a->src_parent);
  22542. + err = au_wr_dir_need_wh(a->src_dentry,
  22543. + au_ftest_ren(a->auren_flags,
  22544. + ISDIR_SRC),
  22545. + &a->btgt);
  22546. + di_write_unlock(a->src_parent);
  22547. + di_write_lock2_parent(a->src_parent, a->dst_parent,
  22548. + /*isdir*/1);
  22549. + au_fclr_ren(a->auren_flags, ISSAMEDIR);
  22550. + } else
  22551. + err = au_wr_dir_need_wh(a->src_dentry,
  22552. + au_ftest_ren(a->auren_flags,
  22553. + ISDIR_SRC),
  22554. + &a->btgt);
  22555. + }
  22556. + if (unlikely(err < 0))
  22557. + goto out_children;
  22558. + if (err)
  22559. + au_fset_ren(a->auren_flags, WHSRC);
  22560. +
  22561. + /* cpup src */
  22562. + if (a->src_btop != a->btgt) {
  22563. + err = au_pin(&pin, a->src_dentry, a->btgt,
  22564. + au_opt_udba(a->src_dentry->d_sb),
  22565. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  22566. + if (!err) {
  22567. + struct au_cp_generic cpg = {
  22568. + .dentry = a->src_dentry,
  22569. + .bdst = a->btgt,
  22570. + .bsrc = a->src_btop,
  22571. + .len = -1,
  22572. + .pin = &pin,
  22573. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  22574. + };
  22575. + AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
  22576. + err = au_sio_cpup_simple(&cpg);
  22577. + au_unpin(&pin);
  22578. + }
  22579. + if (unlikely(err))
  22580. + goto out_children;
  22581. + a->src_btop = a->btgt;
  22582. + a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
  22583. + if (!a->exchange)
  22584. + au_fset_ren(a->auren_flags, WHSRC);
  22585. + }
  22586. +
  22587. + /* cpup dst */
  22588. + if (a->exchange && a->dst_inode
  22589. + && a->dst_btop != a->btgt) {
  22590. + err = au_pin(&pin, a->dst_dentry, a->btgt,
  22591. + au_opt_udba(a->dst_dentry->d_sb),
  22592. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  22593. + if (!err) {
  22594. + struct au_cp_generic cpg = {
  22595. + .dentry = a->dst_dentry,
  22596. + .bdst = a->btgt,
  22597. + .bsrc = a->dst_btop,
  22598. + .len = -1,
  22599. + .pin = &pin,
  22600. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  22601. + };
  22602. + err = au_sio_cpup_simple(&cpg);
  22603. + au_unpin(&pin);
  22604. + }
  22605. + if (unlikely(err))
  22606. + goto out_children;
  22607. + a->dst_btop = a->btgt;
  22608. + a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
  22609. + }
  22610. +
  22611. + /* lock them all */
  22612. + err = au_ren_lock(a);
  22613. + if (unlikely(err))
  22614. + /* leave the copied-up one */
  22615. + goto out_children;
  22616. +
  22617. + if (!a->exchange) {
  22618. + if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
  22619. + err = au_may_ren(a);
  22620. + else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
  22621. + err = -ENAMETOOLONG;
  22622. + if (unlikely(err))
  22623. + goto out_hdir;
  22624. + }
  22625. +
  22626. + /* store timestamps to be revertible */
  22627. + au_ren_dt(a);
  22628. +
  22629. + /* store dirren info */
  22630. + if (au_ftest_ren(a->auren_flags, DIRREN)) {
  22631. + err = au_dr_rename(a->src_dentry, a->btgt,
  22632. + &a->dst_dentry->d_name, &rev);
  22633. + AuTraceErr(err);
  22634. + if (unlikely(err))
  22635. + goto out_dt;
  22636. + }
  22637. +
  22638. + /* here we go */
  22639. + err = do_rename(a);
  22640. + if (unlikely(err))
  22641. + goto out_dirren;
  22642. +
  22643. + if (au_ftest_ren(a->auren_flags, DIRREN))
  22644. + au_dr_rename_fin(a->src_dentry, a->btgt, rev);
  22645. +
  22646. + /* update dir attributes */
  22647. + au_ren_refresh_dir(a);
  22648. +
  22649. + /* dput/iput all lower dentries */
  22650. + au_ren_refresh(a);
  22651. +
  22652. + goto out_hdir; /* success */
  22653. +
  22654. +out_dirren:
  22655. + if (au_ftest_ren(a->auren_flags, DIRREN))
  22656. + au_dr_rename_rev(a->src_dentry, a->btgt, rev);
  22657. +out_dt:
  22658. + au_ren_rev_dt(err, a);
  22659. +out_hdir:
  22660. + au_ren_unlock(a);
  22661. +out_children:
  22662. + au_nhash_wh_free(&a->whlist);
  22663. + if (err && a->dst_inode && a->dst_btop != a->btgt) {
  22664. + AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
  22665. + au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
  22666. + au_set_dbtop(a->dst_dentry, a->dst_btop);
  22667. + }
  22668. +out_parent:
  22669. + if (!err) {
  22670. + if (d_unhashed(a->src_dentry))
  22671. + au_fset_ren(a->auren_flags, DROPPED_SRC);
  22672. + if (d_unhashed(a->dst_dentry))
  22673. + au_fset_ren(a->auren_flags, DROPPED_DST);
  22674. + if (!a->exchange)
  22675. + d_move(a->src_dentry, a->dst_dentry);
  22676. + else {
  22677. + d_exchange(a->src_dentry, a->dst_dentry);
  22678. + if (au_ftest_ren(a->auren_flags, DROPPED_DST))
  22679. + d_drop(a->dst_dentry);
  22680. + }
  22681. + if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
  22682. + d_drop(a->src_dentry);
  22683. + } else {
  22684. + au_update_dbtop(a->dst_dentry);
  22685. + if (!a->dst_inode)
  22686. + d_drop(a->dst_dentry);
  22687. + }
  22688. + if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
  22689. + di_write_unlock(a->dst_parent);
  22690. + else
  22691. + di_write_unlock2(a->src_parent, a->dst_parent);
  22692. +out_unlock:
  22693. + aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
  22694. +out_free:
  22695. + iput(a->dst_inode);
  22696. + if (a->thargs)
  22697. + au_whtmp_rmdir_free(a->thargs);
  22698. + kfree(a);
  22699. +out:
  22700. + AuTraceErr(err);
  22701. + return err;
  22702. +}
  22703. --- /dev/null
  22704. +++ linux-4.19/fs/aufs/Kconfig 2018-11-20 09:35:15.007195503 +0200
  22705. @@ -0,0 +1,199 @@
  22706. +# SPDX-License-Identifier: GPL-2.0
  22707. +config AUFS_FS
  22708. + tristate "Aufs (Advanced multi layered unification filesystem) support"
  22709. + help
  22710. + Aufs is a stackable unification filesystem such as Unionfs,
  22711. + which unifies several directories and provides a merged single
  22712. + directory.
  22713. + In the early days, aufs was entirely re-designed and
  22714. + re-implemented Unionfs Version 1.x series. Introducing many
  22715. + original ideas, approaches and improvements, it becomes totally
  22716. + different from Unionfs while keeping the basic features.
  22717. +
  22718. +if AUFS_FS
  22719. +choice
  22720. + prompt "Maximum number of branches"
  22721. + default AUFS_BRANCH_MAX_127
  22722. + help
  22723. + Specifies the maximum number of branches (or member directories)
  22724. + in a single aufs. The larger value consumes more system
  22725. + resources and has a minor impact to performance.
  22726. +config AUFS_BRANCH_MAX_127
  22727. + bool "127"
  22728. + help
  22729. + Specifies the maximum number of branches (or member directories)
  22730. + in a single aufs. The larger value consumes more system
  22731. + resources and has a minor impact to performance.
  22732. +config AUFS_BRANCH_MAX_511
  22733. + bool "511"
  22734. + help
  22735. + Specifies the maximum number of branches (or member directories)
  22736. + in a single aufs. The larger value consumes more system
  22737. + resources and has a minor impact to performance.
  22738. +config AUFS_BRANCH_MAX_1023
  22739. + bool "1023"
  22740. + help
  22741. + Specifies the maximum number of branches (or member directories)
  22742. + in a single aufs. The larger value consumes more system
  22743. + resources and has a minor impact to performance.
  22744. +config AUFS_BRANCH_MAX_32767
  22745. + bool "32767"
  22746. + help
  22747. + Specifies the maximum number of branches (or member directories)
  22748. + in a single aufs. The larger value consumes more system
  22749. + resources and has a minor impact to performance.
  22750. +endchoice
  22751. +
  22752. +config AUFS_SBILIST
  22753. + bool
  22754. + depends on AUFS_MAGIC_SYSRQ || PROC_FS
  22755. + default y
  22756. + help
  22757. + Automatic configuration for internal use.
  22758. + When aufs supports Magic SysRq or /proc, enabled automatically.
  22759. +
  22760. +config AUFS_HNOTIFY
  22761. + bool "Detect direct branch access (bypassing aufs)"
  22762. + help
  22763. + If you want to modify files on branches directly, eg. bypassing aufs,
  22764. + and want aufs to detect the changes of them fully, then enable this
  22765. + option and use 'udba=notify' mount option.
  22766. + Currently there is only one available configuration, "fsnotify".
  22767. + It will have a negative impact to the performance.
  22768. + See detail in aufs.5.
  22769. +
  22770. +choice
  22771. + prompt "method" if AUFS_HNOTIFY
  22772. + default AUFS_HFSNOTIFY
  22773. +config AUFS_HFSNOTIFY
  22774. + bool "fsnotify"
  22775. + select FSNOTIFY
  22776. +endchoice
  22777. +
  22778. +config AUFS_EXPORT
  22779. + bool "NFS-exportable aufs"
  22780. + depends on EXPORTFS
  22781. + help
  22782. + If you want to export your mounted aufs via NFS, then enable this
  22783. + option. There are several requirements for this configuration.
  22784. + See detail in aufs.5.
  22785. +
  22786. +config AUFS_INO_T_64
  22787. + bool
  22788. + depends on AUFS_EXPORT
  22789. + depends on 64BIT && !(ALPHA || S390)
  22790. + default y
  22791. + help
  22792. + Automatic configuration for internal use.
  22793. + /* typedef unsigned long/int __kernel_ino_t */
  22794. + /* alpha and s390x are int */
  22795. +
  22796. +config AUFS_XATTR
  22797. + bool "support for XATTR/EA (including Security Labels)"
  22798. + help
  22799. + If your branch fs supports XATTR/EA and you want to make them
  22800. + available in aufs too, then enable this opsion and specify the
  22801. + branch attributes for EA.
  22802. + See detail in aufs.5.
  22803. +
  22804. +config AUFS_FHSM
  22805. + bool "File-based Hierarchical Storage Management"
  22806. + help
  22807. + Hierarchical Storage Management (or HSM) is a well-known feature
  22808. + in the storage world. Aufs provides this feature as file-based.
  22809. + with multiple branches.
  22810. + These multiple branches are prioritized, ie. the topmost one
  22811. + should be the fastest drive and be used heavily.
  22812. +
  22813. +config AUFS_RDU
  22814. + bool "Readdir in userspace"
  22815. + help
  22816. + Aufs has two methods to provide a merged view for a directory,
  22817. + by a user-space library and by kernel-space natively. The latter
  22818. + is always enabled but sometimes large and slow.
  22819. + If you enable this option, install the library in aufs2-util
  22820. + package, and set some environment variables for your readdir(3),
  22821. + then the work will be handled in user-space which generally
  22822. + shows better performance in most cases.
  22823. + See detail in aufs.5.
  22824. +
  22825. +config AUFS_DIRREN
  22826. + bool "Workaround for rename(2)-ing a directory"
  22827. + help
  22828. + By default, aufs returns EXDEV error in renameing a dir who has
  22829. + his child on the lower branch, since it is a bad idea to issue
  22830. + rename(2) internally for every lower branch. But user may not
  22831. + accept this behaviour. So here is a workaround to allow such
  22832. + rename(2) and store some extra infromation on the writable
  22833. + branch. Obviously this costs high (and I don't like it).
  22834. + To use this feature, you need to enable this configuration AND
  22835. + to specify the mount option `dirren.'
  22836. + See details in aufs.5 and the design documents.
  22837. +
  22838. +config AUFS_SHWH
  22839. + bool "Show whiteouts"
  22840. + help
  22841. + If you want to make the whiteouts in aufs visible, then enable
  22842. + this option and specify 'shwh' mount option. Although it may
  22843. + sounds like philosophy or something, but in technically it
  22844. + simply shows the name of whiteout with keeping its behaviour.
  22845. +
  22846. +config AUFS_BR_RAMFS
  22847. + bool "Ramfs (initramfs/rootfs) as an aufs branch"
  22848. + help
  22849. + If you want to use ramfs as an aufs branch fs, then enable this
  22850. + option. Generally tmpfs is recommended.
  22851. + Aufs prohibited them to be a branch fs by default, because
  22852. + initramfs becomes unusable after switch_root or something
  22853. + generally. If you sets initramfs as an aufs branch and boot your
  22854. + system by switch_root, you will meet a problem easily since the
  22855. + files in initramfs may be inaccessible.
  22856. + Unless you are going to use ramfs as an aufs branch fs without
  22857. + switch_root or something, leave it N.
  22858. +
  22859. +config AUFS_BR_FUSE
  22860. + bool "Fuse fs as an aufs branch"
  22861. + depends on FUSE_FS
  22862. + select AUFS_POLL
  22863. + help
  22864. + If you want to use fuse-based userspace filesystem as an aufs
  22865. + branch fs, then enable this option.
  22866. + It implements the internal poll(2) operation which is
  22867. + implemented by fuse only (curretnly).
  22868. +
  22869. +config AUFS_POLL
  22870. + bool
  22871. + help
  22872. + Automatic configuration for internal use.
  22873. +
  22874. +config AUFS_BR_HFSPLUS
  22875. + bool "Hfsplus as an aufs branch"
  22876. + depends on HFSPLUS_FS
  22877. + default y
  22878. + help
  22879. + If you want to use hfsplus fs as an aufs branch fs, then enable
  22880. + this option. This option introduces a small overhead at
  22881. + copying-up a file on hfsplus.
  22882. +
  22883. +config AUFS_BDEV_LOOP
  22884. + bool
  22885. + depends on BLK_DEV_LOOP
  22886. + default y
  22887. + help
  22888. + Automatic configuration for internal use.
  22889. + Convert =[ym] into =y.
  22890. +
  22891. +config AUFS_DEBUG
  22892. + bool "Debug aufs"
  22893. + help
  22894. + Enable this to compile aufs internal debug code.
  22895. + It will have a negative impact to the performance.
  22896. +
  22897. +config AUFS_MAGIC_SYSRQ
  22898. + bool
  22899. + depends on AUFS_DEBUG && MAGIC_SYSRQ
  22900. + default y
  22901. + help
  22902. + Automatic configuration for internal use.
  22903. + When aufs supports Magic SysRq, enabled automatically.
  22904. +endif
  22905. --- /dev/null
  22906. +++ linux-4.19/fs/aufs/lcnt.h 2018-11-20 09:35:15.032195504 +0200
  22907. @@ -0,0 +1,186 @@
  22908. +/* SPDX-License-Identifier: GPL-2.0 */
  22909. +/*
  22910. + * Copyright (C) 2018 Junjiro R. Okajima
  22911. + *
  22912. + * This program, aufs is free software; you can redistribute it and/or modify
  22913. + * it under the terms of the GNU General Public License as published by
  22914. + * the Free Software Foundation; either version 2 of the License, or
  22915. + * (at your option) any later version.
  22916. + *
  22917. + * This program is distributed in the hope that it will be useful,
  22918. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22919. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22920. + * GNU General Public License for more details.
  22921. + *
  22922. + * You should have received a copy of the GNU General Public License
  22923. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22924. + */
  22925. +
  22926. +/*
  22927. + * simple long counter wrapper
  22928. + */
  22929. +
  22930. +#ifndef __AUFS_LCNT_H__
  22931. +#define __AUFS_LCNT_H__
  22932. +
  22933. +#ifdef __KERNEL__
  22934. +
  22935. +#include "debug.h"
  22936. +
  22937. +#define AuLCntATOMIC 1
  22938. +#define AuLCntPCPUCNT 2
  22939. +/*
  22940. + * why does percpu_refcount require extra synchronize_rcu()s in
  22941. + * au_br_do_free()
  22942. + */
  22943. +#define AuLCntPCPUREF 3
  22944. +
  22945. +/* #define AuLCntChosen AuLCntATOMIC */
  22946. +#define AuLCntChosen AuLCntPCPUCNT
  22947. +/* #define AuLCntChosen AuLCntPCPUREF */
  22948. +
  22949. +#if AuLCntChosen == AuLCntATOMIC
  22950. +#include <linux/atomic.h>
  22951. +
  22952. +typedef atomic_long_t au_lcnt_t;
  22953. +
  22954. +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
  22955. +{
  22956. + atomic_long_set(cnt, 0);
  22957. + return 0;
  22958. +}
  22959. +
  22960. +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
  22961. +{
  22962. + /* empty */
  22963. +}
  22964. +
  22965. +static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
  22966. + int do_sync __maybe_unused)
  22967. +{
  22968. + /* empty */
  22969. +}
  22970. +
  22971. +static inline void au_lcnt_inc(au_lcnt_t *cnt)
  22972. +{
  22973. + atomic_long_inc(cnt);
  22974. +}
  22975. +
  22976. +static inline void au_lcnt_dec(au_lcnt_t *cnt)
  22977. +{
  22978. + atomic_long_dec(cnt);
  22979. +}
  22980. +
  22981. +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
  22982. +{
  22983. + return atomic_long_read(cnt);
  22984. +}
  22985. +#endif
  22986. +
  22987. +#if AuLCntChosen == AuLCntPCPUCNT
  22988. +#include <linux/percpu_counter.h>
  22989. +
  22990. +typedef struct percpu_counter au_lcnt_t;
  22991. +
  22992. +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
  22993. +{
  22994. + return percpu_counter_init(cnt, 0, GFP_NOFS);
  22995. +}
  22996. +
  22997. +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
  22998. +{
  22999. + /* empty */
  23000. +}
  23001. +
  23002. +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
  23003. +{
  23004. + percpu_counter_destroy(cnt);
  23005. +}
  23006. +
  23007. +static inline void au_lcnt_inc(au_lcnt_t *cnt)
  23008. +{
  23009. + percpu_counter_inc(cnt);
  23010. +}
  23011. +
  23012. +static inline void au_lcnt_dec(au_lcnt_t *cnt)
  23013. +{
  23014. + percpu_counter_dec(cnt);
  23015. +}
  23016. +
  23017. +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
  23018. +{
  23019. + s64 n;
  23020. +
  23021. + n = percpu_counter_sum(cnt);
  23022. + BUG_ON(n < 0);
  23023. + if (LONG_MAX != LLONG_MAX
  23024. + && n > LONG_MAX)
  23025. + AuWarn1("%s\n", "wrap-around");
  23026. +
  23027. + return n;
  23028. +}
  23029. +#endif
  23030. +
  23031. +#if AuLCntChosen == AuLCntPCPUREF
  23032. +#include <linux/percpu-refcount.h>
  23033. +
  23034. +typedef struct percpu_ref au_lcnt_t;
  23035. +
  23036. +static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
  23037. +{
  23038. + if (!release)
  23039. + release = percpu_ref_exit;
  23040. + return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
  23041. +}
  23042. +
  23043. +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
  23044. +{
  23045. + synchronize_rcu();
  23046. +}
  23047. +
  23048. +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
  23049. +{
  23050. + percpu_ref_kill(cnt);
  23051. + if (do_sync)
  23052. + au_lcnt_wait_for_fin(cnt);
  23053. +}
  23054. +
  23055. +static inline void au_lcnt_inc(au_lcnt_t *cnt)
  23056. +{
  23057. + percpu_ref_get(cnt);
  23058. +}
  23059. +
  23060. +static inline void au_lcnt_dec(au_lcnt_t *cnt)
  23061. +{
  23062. + percpu_ref_put(cnt);
  23063. +}
  23064. +
  23065. +/*
  23066. + * avoid calling this func as possible.
  23067. + */
  23068. +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
  23069. +{
  23070. + long l;
  23071. +
  23072. + percpu_ref_switch_to_atomic_sync(cnt);
  23073. + l = atomic_long_read(&cnt->count);
  23074. + if (do_rev)
  23075. + percpu_ref_switch_to_percpu(cnt);
  23076. +
  23077. + /* percpu_ref is initialized by 1 instead of 0 */
  23078. + return l - 1;
  23079. +}
  23080. +#endif
  23081. +
  23082. +#ifdef CONFIG_AUFS_DEBUG
  23083. +#define AuLCntZero(val) do { \
  23084. + long l = val; \
  23085. + if (l) \
  23086. + AuDbg("%s = %ld\n", #val, l); \
  23087. +} while (0)
  23088. +#else
  23089. +#define AuLCntZero(val) do {} while (0)
  23090. +#endif
  23091. +
  23092. +#endif /* __KERNEL__ */
  23093. +#endif /* __AUFS_LCNT_H__ */
  23094. --- /dev/null
  23095. +++ linux-4.19/fs/aufs/loop.c 2018-11-20 09:35:15.032195504 +0200
  23096. @@ -0,0 +1,148 @@
  23097. +// SPDX-License-Identifier: GPL-2.0
  23098. +/*
  23099. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  23100. + *
  23101. + * This program, aufs is free software; you can redistribute it and/or modify
  23102. + * it under the terms of the GNU General Public License as published by
  23103. + * the Free Software Foundation; either version 2 of the License, or
  23104. + * (at your option) any later version.
  23105. + *
  23106. + * This program is distributed in the hope that it will be useful,
  23107. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23108. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23109. + * GNU General Public License for more details.
  23110. + *
  23111. + * You should have received a copy of the GNU General Public License
  23112. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23113. + */
  23114. +
  23115. +/*
  23116. + * support for loopback block device as a branch
  23117. + */
  23118. +
  23119. +#include "aufs.h"
  23120. +
  23121. +/* added into drivers/block/loop.c */
  23122. +static struct file *(*backing_file_func)(struct super_block *sb);
  23123. +
  23124. +/*
  23125. + * test if two lower dentries have overlapping branches.
  23126. + */
  23127. +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
  23128. +{
  23129. + struct super_block *h_sb;
  23130. + struct file *backing_file;
  23131. +
  23132. + if (unlikely(!backing_file_func)) {
  23133. + /* don't load "loop" module here */
  23134. + backing_file_func = symbol_get(loop_backing_file);
  23135. + if (unlikely(!backing_file_func))
  23136. + /* "loop" module is not loaded */
  23137. + return 0;
  23138. + }
  23139. +
  23140. + h_sb = h_adding->d_sb;
  23141. + backing_file = backing_file_func(h_sb);
  23142. + if (!backing_file)
  23143. + return 0;
  23144. +
  23145. + h_adding = backing_file->f_path.dentry;
  23146. + /*
  23147. + * h_adding can be local NFS.
  23148. + * in this case aufs cannot detect the loop.
  23149. + */
  23150. + if (unlikely(h_adding->d_sb == sb))
  23151. + return 1;
  23152. + return !!au_test_subdir(h_adding, sb->s_root);
  23153. +}
  23154. +
  23155. +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
  23156. +int au_test_loopback_kthread(void)
  23157. +{
  23158. + int ret;
  23159. + struct task_struct *tsk = current;
  23160. + char c, comm[sizeof(tsk->comm)];
  23161. +
  23162. + ret = 0;
  23163. + if (tsk->flags & PF_KTHREAD) {
  23164. + get_task_comm(comm, tsk);
  23165. + c = comm[4];
  23166. + ret = ('0' <= c && c <= '9'
  23167. + && !strncmp(comm, "loop", 4));
  23168. + }
  23169. +
  23170. + return ret;
  23171. +}
  23172. +
  23173. +/* ---------------------------------------------------------------------- */
  23174. +
  23175. +#define au_warn_loopback_step 16
  23176. +static int au_warn_loopback_nelem = au_warn_loopback_step;
  23177. +static unsigned long *au_warn_loopback_array;
  23178. +
  23179. +void au_warn_loopback(struct super_block *h_sb)
  23180. +{
  23181. + int i, new_nelem;
  23182. + unsigned long *a, magic;
  23183. + static DEFINE_SPINLOCK(spin);
  23184. +
  23185. + magic = h_sb->s_magic;
  23186. + spin_lock(&spin);
  23187. + a = au_warn_loopback_array;
  23188. + for (i = 0; i < au_warn_loopback_nelem && *a; i++)
  23189. + if (a[i] == magic) {
  23190. + spin_unlock(&spin);
  23191. + return;
  23192. + }
  23193. +
  23194. + /* h_sb is new to us, print it */
  23195. + if (i < au_warn_loopback_nelem) {
  23196. + a[i] = magic;
  23197. + goto pr;
  23198. + }
  23199. +
  23200. + /* expand the array */
  23201. + new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
  23202. + a = au_kzrealloc(au_warn_loopback_array,
  23203. + au_warn_loopback_nelem * sizeof(unsigned long),
  23204. + new_nelem * sizeof(unsigned long), GFP_ATOMIC,
  23205. + /*may_shrink*/0);
  23206. + if (a) {
  23207. + au_warn_loopback_nelem = new_nelem;
  23208. + au_warn_loopback_array = a;
  23209. + a[i] = magic;
  23210. + goto pr;
  23211. + }
  23212. +
  23213. + spin_unlock(&spin);
  23214. + AuWarn1("realloc failed, ignored\n");
  23215. + return;
  23216. +
  23217. +pr:
  23218. + spin_unlock(&spin);
  23219. + pr_warn("you may want to try another patch for loopback file "
  23220. + "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
  23221. +}
  23222. +
  23223. +int au_loopback_init(void)
  23224. +{
  23225. + int err;
  23226. + struct super_block *sb __maybe_unused;
  23227. +
  23228. + BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(unsigned long));
  23229. +
  23230. + err = 0;
  23231. + au_warn_loopback_array = kcalloc(au_warn_loopback_step,
  23232. + sizeof(unsigned long), GFP_NOFS);
  23233. + if (unlikely(!au_warn_loopback_array))
  23234. + err = -ENOMEM;
  23235. +
  23236. + return err;
  23237. +}
  23238. +
  23239. +void au_loopback_fin(void)
  23240. +{
  23241. + if (backing_file_func)
  23242. + symbol_put(loop_backing_file);
  23243. + kfree(au_warn_loopback_array);
  23244. +}
  23245. --- /dev/null
  23246. +++ linux-4.19/fs/aufs/loop.h 2018-11-20 09:35:15.033195504 +0200
  23247. @@ -0,0 +1,53 @@
  23248. +/* SPDX-License-Identifier: GPL-2.0 */
  23249. +/*
  23250. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  23251. + *
  23252. + * This program, aufs is free software; you can redistribute it and/or modify
  23253. + * it under the terms of the GNU General Public License as published by
  23254. + * the Free Software Foundation; either version 2 of the License, or
  23255. + * (at your option) any later version.
  23256. + *
  23257. + * This program is distributed in the hope that it will be useful,
  23258. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23259. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23260. + * GNU General Public License for more details.
  23261. + *
  23262. + * You should have received a copy of the GNU General Public License
  23263. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23264. + */
  23265. +
  23266. +/*
  23267. + * support for loopback mount as a branch
  23268. + */
  23269. +
  23270. +#ifndef __AUFS_LOOP_H__
  23271. +#define __AUFS_LOOP_H__
  23272. +
  23273. +#ifdef __KERNEL__
  23274. +
  23275. +struct dentry;
  23276. +struct super_block;
  23277. +
  23278. +#ifdef CONFIG_AUFS_BDEV_LOOP
  23279. +/* drivers/block/loop.c */
  23280. +struct file *loop_backing_file(struct super_block *sb);
  23281. +
  23282. +/* loop.c */
  23283. +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
  23284. +int au_test_loopback_kthread(void);
  23285. +void au_warn_loopback(struct super_block *h_sb);
  23286. +
  23287. +int au_loopback_init(void);
  23288. +void au_loopback_fin(void);
  23289. +#else
  23290. +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
  23291. + struct dentry *h_adding)
  23292. +AuStubInt0(au_test_loopback_kthread, void)
  23293. +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
  23294. +
  23295. +AuStubInt0(au_loopback_init, void)
  23296. +AuStubVoid(au_loopback_fin, void)
  23297. +#endif /* BLK_DEV_LOOP */
  23298. +
  23299. +#endif /* __KERNEL__ */
  23300. +#endif /* __AUFS_LOOP_H__ */
  23301. --- /dev/null
  23302. +++ linux-4.19/fs/aufs/magic.mk 2018-11-20 09:35:15.033195504 +0200
  23303. @@ -0,0 +1,31 @@
  23304. +# SPDX-License-Identifier: GPL-2.0
  23305. +
  23306. +# defined in ${srctree}/fs/fuse/inode.c
  23307. +# tristate
  23308. +ifdef CONFIG_FUSE_FS
  23309. +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
  23310. +endif
  23311. +
  23312. +# defined in ${srctree}/fs/xfs/xfs_sb.h
  23313. +# tristate
  23314. +ifdef CONFIG_XFS_FS
  23315. +ccflags-y += -DXFS_SB_MAGIC=0x58465342
  23316. +endif
  23317. +
  23318. +# defined in ${srctree}/fs/configfs/mount.c
  23319. +# tristate
  23320. +ifdef CONFIG_CONFIGFS_FS
  23321. +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
  23322. +endif
  23323. +
  23324. +# defined in ${srctree}/fs/ubifs/ubifs.h
  23325. +# tristate
  23326. +ifdef CONFIG_UBIFS_FS
  23327. +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
  23328. +endif
  23329. +
  23330. +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
  23331. +# tristate
  23332. +ifdef CONFIG_HFSPLUS_FS
  23333. +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
  23334. +endif
  23335. --- /dev/null
  23336. +++ linux-4.19/fs/aufs/Makefile 2018-11-20 09:35:15.007195503 +0200
  23337. @@ -0,0 +1,46 @@
  23338. +# SPDX-License-Identifier: GPL-2.0
  23339. +
  23340. +include ${src}/magic.mk
  23341. +ifeq (${CONFIG_AUFS_FS},m)
  23342. +include ${src}/conf.mk
  23343. +endif
  23344. +-include ${src}/priv_def.mk
  23345. +
  23346. +# cf. include/linux/kernel.h
  23347. +# enable pr_debug
  23348. +ccflags-y += -DDEBUG
  23349. +# sparse requires the full pathname
  23350. +ifdef M
  23351. +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
  23352. +else
  23353. +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
  23354. +endif
  23355. +
  23356. +obj-$(CONFIG_AUFS_FS) += aufs.o
  23357. +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
  23358. + wkq.o vfsub.o dcsub.o \
  23359. + cpup.o whout.o wbr_policy.o \
  23360. + dinfo.o dentry.o \
  23361. + dynop.o \
  23362. + finfo.o file.o f_op.o \
  23363. + dir.o vdir.o \
  23364. + iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
  23365. + mvdown.o ioctl.o
  23366. +
  23367. +# all are boolean
  23368. +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
  23369. +aufs-$(CONFIG_SYSFS) += sysfs.o
  23370. +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
  23371. +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
  23372. +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
  23373. +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
  23374. +aufs-$(CONFIG_AUFS_EXPORT) += export.o
  23375. +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
  23376. +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
  23377. +aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
  23378. +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
  23379. +aufs-$(CONFIG_AUFS_POLL) += poll.o
  23380. +aufs-$(CONFIG_AUFS_RDU) += rdu.o
  23381. +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
  23382. +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
  23383. +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
  23384. --- /dev/null
  23385. +++ linux-4.19/fs/aufs/module.c 2018-11-20 09:35:15.034195504 +0200
  23386. @@ -0,0 +1,273 @@
  23387. +// SPDX-License-Identifier: GPL-2.0
  23388. +/*
  23389. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  23390. + *
  23391. + * This program, aufs is free software; you can redistribute it and/or modify
  23392. + * it under the terms of the GNU General Public License as published by
  23393. + * the Free Software Foundation; either version 2 of the License, or
  23394. + * (at your option) any later version.
  23395. + *
  23396. + * This program is distributed in the hope that it will be useful,
  23397. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23398. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23399. + * GNU General Public License for more details.
  23400. + *
  23401. + * You should have received a copy of the GNU General Public License
  23402. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23403. + */
  23404. +
  23405. +/*
  23406. + * module global variables and operations
  23407. + */
  23408. +
  23409. +#include <linux/module.h>
  23410. +#include <linux/seq_file.h>
  23411. +#include "aufs.h"
  23412. +
  23413. +/* shrinkable realloc */
  23414. +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
  23415. +{
  23416. + size_t sz;
  23417. + int diff;
  23418. +
  23419. + sz = 0;
  23420. + diff = -1;
  23421. + if (p) {
  23422. +#if 0 /* unused */
  23423. + if (!new_sz) {
  23424. + kfree(p);
  23425. + p = NULL;
  23426. + goto out;
  23427. + }
  23428. +#else
  23429. + AuDebugOn(!new_sz);
  23430. +#endif
  23431. + sz = ksize(p);
  23432. + diff = au_kmidx_sub(sz, new_sz);
  23433. + }
  23434. + if (sz && !diff)
  23435. + goto out;
  23436. +
  23437. + if (sz < new_sz)
  23438. + /* expand or SLOB */
  23439. + p = krealloc(p, new_sz, gfp);
  23440. + else if (new_sz < sz && may_shrink) {
  23441. + /* shrink */
  23442. + void *q;
  23443. +
  23444. + q = kmalloc(new_sz, gfp);
  23445. + if (q) {
  23446. + if (p) {
  23447. + memcpy(q, p, new_sz);
  23448. + kfree(p);
  23449. + }
  23450. + p = q;
  23451. + } else
  23452. + p = NULL;
  23453. + }
  23454. +
  23455. +out:
  23456. + return p;
  23457. +}
  23458. +
  23459. +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
  23460. + int may_shrink)
  23461. +{
  23462. + p = au_krealloc(p, new_sz, gfp, may_shrink);
  23463. + if (p && new_sz > nused)
  23464. + memset(p + nused, 0, new_sz - nused);
  23465. + return p;
  23466. +}
  23467. +
  23468. +/* ---------------------------------------------------------------------- */
  23469. +/*
  23470. + * aufs caches
  23471. + */
  23472. +struct kmem_cache *au_cache[AuCache_Last];
  23473. +
  23474. +static void au_cache_fin(void)
  23475. +{
  23476. + int i;
  23477. +
  23478. + /*
  23479. + * Make sure all delayed rcu free inodes are flushed before we
  23480. + * destroy cache.
  23481. + */
  23482. + rcu_barrier();
  23483. +
  23484. + /* excluding AuCache_HNOTIFY */
  23485. + BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
  23486. + for (i = 0; i < AuCache_HNOTIFY; i++) {
  23487. + kmem_cache_destroy(au_cache[i]);
  23488. + au_cache[i] = NULL;
  23489. + }
  23490. +}
  23491. +
  23492. +static int __init au_cache_init(void)
  23493. +{
  23494. + au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
  23495. + if (au_cache[AuCache_DINFO])
  23496. + /* SLAB_DESTROY_BY_RCU */
  23497. + au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
  23498. + au_icntnr_init_once);
  23499. + if (au_cache[AuCache_ICNTNR])
  23500. + au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
  23501. + au_fi_init_once);
  23502. + if (au_cache[AuCache_FINFO])
  23503. + au_cache[AuCache_VDIR] = AuCache(au_vdir);
  23504. + if (au_cache[AuCache_VDIR])
  23505. + au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
  23506. + if (au_cache[AuCache_DEHSTR])
  23507. + return 0;
  23508. +
  23509. + au_cache_fin();
  23510. + return -ENOMEM;
  23511. +}
  23512. +
  23513. +/* ---------------------------------------------------------------------- */
  23514. +
  23515. +int au_dir_roflags;
  23516. +
  23517. +#ifdef CONFIG_AUFS_SBILIST
  23518. +/*
  23519. + * iterate_supers_type() doesn't protect us from
  23520. + * remounting (branch management)
  23521. + */
  23522. +struct hlist_bl_head au_sbilist;
  23523. +#endif
  23524. +
  23525. +/*
  23526. + * functions for module interface.
  23527. + */
  23528. +MODULE_LICENSE("GPL");
  23529. +/* MODULE_LICENSE("GPL v2"); */
  23530. +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
  23531. +MODULE_DESCRIPTION(AUFS_NAME
  23532. + " -- Advanced multi layered unification filesystem");
  23533. +MODULE_VERSION(AUFS_VERSION);
  23534. +MODULE_ALIAS_FS(AUFS_NAME);
  23535. +
  23536. +/* this module parameter has no meaning when SYSFS is disabled */
  23537. +int sysaufs_brs = 1;
  23538. +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
  23539. +module_param_named(brs, sysaufs_brs, int, 0444);
  23540. +
  23541. +/* this module parameter has no meaning when USER_NS is disabled */
  23542. +bool au_userns;
  23543. +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
  23544. +module_param_named(allow_userns, au_userns, bool, 0444);
  23545. +
  23546. +/* ---------------------------------------------------------------------- */
  23547. +
  23548. +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
  23549. +
  23550. +int au_seq_path(struct seq_file *seq, struct path *path)
  23551. +{
  23552. + int err;
  23553. +
  23554. + err = seq_path(seq, path, au_esc_chars);
  23555. + if (err >= 0)
  23556. + err = 0;
  23557. + else
  23558. + err = -ENOMEM;
  23559. +
  23560. + return err;
  23561. +}
  23562. +
  23563. +/* ---------------------------------------------------------------------- */
  23564. +
  23565. +static int __init aufs_init(void)
  23566. +{
  23567. + int err, i;
  23568. + char *p;
  23569. +
  23570. + p = au_esc_chars;
  23571. + for (i = 1; i <= ' '; i++)
  23572. + *p++ = i;
  23573. + *p++ = '\\';
  23574. + *p++ = '\x7f';
  23575. + *p = 0;
  23576. +
  23577. + au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
  23578. +
  23579. + memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
  23580. + for (i = 0; i < AuIop_Last; i++)
  23581. + aufs_iop_nogetattr[i].getattr = NULL;
  23582. +
  23583. + memset(au_cache, 0, sizeof(au_cache)); /* including hnotify */
  23584. +
  23585. + au_sbilist_init();
  23586. + sysaufs_brs_init();
  23587. + au_debug_init();
  23588. + au_dy_init();
  23589. + err = sysaufs_init();
  23590. + if (unlikely(err))
  23591. + goto out;
  23592. + err = dbgaufs_init();
  23593. + if (unlikely(err))
  23594. + goto out_sysaufs;
  23595. + err = au_procfs_init();
  23596. + if (unlikely(err))
  23597. + goto out_dbgaufs;
  23598. + err = au_wkq_init();
  23599. + if (unlikely(err))
  23600. + goto out_procfs;
  23601. + err = au_loopback_init();
  23602. + if (unlikely(err))
  23603. + goto out_wkq;
  23604. + err = au_hnotify_init();
  23605. + if (unlikely(err))
  23606. + goto out_loopback;
  23607. + err = au_sysrq_init();
  23608. + if (unlikely(err))
  23609. + goto out_hin;
  23610. + err = au_cache_init();
  23611. + if (unlikely(err))
  23612. + goto out_sysrq;
  23613. +
  23614. + aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
  23615. + err = register_filesystem(&aufs_fs_type);
  23616. + if (unlikely(err))
  23617. + goto out_cache;
  23618. +
  23619. + /* since we define pr_fmt, call printk directly */
  23620. + printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
  23621. + goto out; /* success */
  23622. +
  23623. +out_cache:
  23624. + au_cache_fin();
  23625. +out_sysrq:
  23626. + au_sysrq_fin();
  23627. +out_hin:
  23628. + au_hnotify_fin();
  23629. +out_loopback:
  23630. + au_loopback_fin();
  23631. +out_wkq:
  23632. + au_wkq_fin();
  23633. +out_procfs:
  23634. + au_procfs_fin();
  23635. +out_dbgaufs:
  23636. + dbgaufs_fin();
  23637. +out_sysaufs:
  23638. + sysaufs_fin();
  23639. + au_dy_fin();
  23640. +out:
  23641. + return err;
  23642. +}
  23643. +
  23644. +static void __exit aufs_exit(void)
  23645. +{
  23646. + unregister_filesystem(&aufs_fs_type);
  23647. + au_cache_fin();
  23648. + au_sysrq_fin();
  23649. + au_hnotify_fin();
  23650. + au_loopback_fin();
  23651. + au_wkq_fin();
  23652. + au_procfs_fin();
  23653. + dbgaufs_fin();
  23654. + sysaufs_fin();
  23655. + au_dy_fin();
  23656. +}
  23657. +
  23658. +module_init(aufs_init);
  23659. +module_exit(aufs_exit);
  23660. --- /dev/null
  23661. +++ linux-4.19/fs/aufs/module.h 2018-11-20 09:35:15.034195504 +0200
  23662. @@ -0,0 +1,102 @@
  23663. +/* SPDX-License-Identifier: GPL-2.0 */
  23664. +/*
  23665. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  23666. + *
  23667. + * This program, aufs is free software; you can redistribute it and/or modify
  23668. + * it under the terms of the GNU General Public License as published by
  23669. + * the Free Software Foundation; either version 2 of the License, or
  23670. + * (at your option) any later version.
  23671. + *
  23672. + * This program is distributed in the hope that it will be useful,
  23673. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23674. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23675. + * GNU General Public License for more details.
  23676. + *
  23677. + * You should have received a copy of the GNU General Public License
  23678. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23679. + */
  23680. +
  23681. +/*
  23682. + * module initialization and module-global
  23683. + */
  23684. +
  23685. +#ifndef __AUFS_MODULE_H__
  23686. +#define __AUFS_MODULE_H__
  23687. +
  23688. +#ifdef __KERNEL__
  23689. +
  23690. +#include <linux/slab.h>
  23691. +
  23692. +struct path;
  23693. +struct seq_file;
  23694. +
  23695. +/* module parameters */
  23696. +extern int sysaufs_brs;
  23697. +extern bool au_userns;
  23698. +
  23699. +/* ---------------------------------------------------------------------- */
  23700. +
  23701. +extern int au_dir_roflags;
  23702. +
  23703. +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
  23704. +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
  23705. + int may_shrink);
  23706. +
  23707. +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
  23708. +{
  23709. +#ifndef CONFIG_SLOB
  23710. + return kmalloc_index(sz) - kmalloc_index(new_sz);
  23711. +#else
  23712. + return -1; /* SLOB is untested */
  23713. +#endif
  23714. +}
  23715. +
  23716. +int au_seq_path(struct seq_file *seq, struct path *path);
  23717. +
  23718. +#ifdef CONFIG_PROC_FS
  23719. +/* procfs.c */
  23720. +int __init au_procfs_init(void);
  23721. +void au_procfs_fin(void);
  23722. +#else
  23723. +AuStubInt0(au_procfs_init, void);
  23724. +AuStubVoid(au_procfs_fin, void);
  23725. +#endif
  23726. +
  23727. +/* ---------------------------------------------------------------------- */
  23728. +
  23729. +/* kmem cache */
  23730. +enum {
  23731. + AuCache_DINFO,
  23732. + AuCache_ICNTNR,
  23733. + AuCache_FINFO,
  23734. + AuCache_VDIR,
  23735. + AuCache_DEHSTR,
  23736. + AuCache_HNOTIFY, /* must be last */
  23737. + AuCache_Last
  23738. +};
  23739. +
  23740. +extern struct kmem_cache *au_cache[AuCache_Last];
  23741. +
  23742. +#define AuCacheFlags (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
  23743. +#define AuCache(type) KMEM_CACHE(type, AuCacheFlags)
  23744. +#define AuCacheCtor(type, ctor) \
  23745. + kmem_cache_create(#type, sizeof(struct type), \
  23746. + __alignof__(struct type), AuCacheFlags, ctor)
  23747. +
  23748. +#define AuCacheFuncs(name, index) \
  23749. +static inline struct au_##name *au_cache_alloc_##name(void) \
  23750. +{ return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
  23751. +static inline void au_cache_free_##name(struct au_##name *p) \
  23752. +{ kmem_cache_free(au_cache[AuCache_##index], p); }
  23753. +
  23754. +AuCacheFuncs(dinfo, DINFO);
  23755. +AuCacheFuncs(icntnr, ICNTNR);
  23756. +AuCacheFuncs(finfo, FINFO);
  23757. +AuCacheFuncs(vdir, VDIR);
  23758. +AuCacheFuncs(vdir_dehstr, DEHSTR);
  23759. +#ifdef CONFIG_AUFS_HNOTIFY
  23760. +AuCacheFuncs(hnotify, HNOTIFY);
  23761. +#endif
  23762. +
  23763. +#endif /* __KERNEL__ */
  23764. +#endif /* __AUFS_MODULE_H__ */
  23765. --- /dev/null
  23766. +++ linux-4.19/fs/aufs/mvdown.c 2018-11-20 09:35:15.035195504 +0200
  23767. @@ -0,0 +1,705 @@
  23768. +// SPDX-License-Identifier: GPL-2.0
  23769. +/*
  23770. + * Copyright (C) 2011-2018 Junjiro R. Okajima
  23771. + *
  23772. + * This program, aufs is free software; you can redistribute it and/or modify
  23773. + * it under the terms of the GNU General Public License as published by
  23774. + * the Free Software Foundation; either version 2 of the License, or
  23775. + * (at your option) any later version.
  23776. + *
  23777. + * This program is distributed in the hope that it will be useful,
  23778. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23779. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23780. + * GNU General Public License for more details.
  23781. + *
  23782. + * You should have received a copy of the GNU General Public License
  23783. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23784. + */
  23785. +
  23786. +/*
  23787. + * move-down, opposite of copy-up
  23788. + */
  23789. +
  23790. +#include "aufs.h"
  23791. +
  23792. +struct au_mvd_args {
  23793. + struct {
  23794. + struct super_block *h_sb;
  23795. + struct dentry *h_parent;
  23796. + struct au_hinode *hdir;
  23797. + struct inode *h_dir, *h_inode;
  23798. + struct au_pin pin;
  23799. + } info[AUFS_MVDOWN_NARRAY];
  23800. +
  23801. + struct aufs_mvdown mvdown;
  23802. + struct dentry *dentry, *parent;
  23803. + struct inode *inode, *dir;
  23804. + struct super_block *sb;
  23805. + aufs_bindex_t bopq, bwh, bfound;
  23806. + unsigned char rename_lock;
  23807. +};
  23808. +
  23809. +#define mvd_errno mvdown.au_errno
  23810. +#define mvd_bsrc mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
  23811. +#define mvd_src_brid mvdown.stbr[AUFS_MVDOWN_UPPER].brid
  23812. +#define mvd_bdst mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
  23813. +#define mvd_dst_brid mvdown.stbr[AUFS_MVDOWN_LOWER].brid
  23814. +
  23815. +#define mvd_h_src_sb info[AUFS_MVDOWN_UPPER].h_sb
  23816. +#define mvd_h_src_parent info[AUFS_MVDOWN_UPPER].h_parent
  23817. +#define mvd_hdir_src info[AUFS_MVDOWN_UPPER].hdir
  23818. +#define mvd_h_src_dir info[AUFS_MVDOWN_UPPER].h_dir
  23819. +#define mvd_h_src_inode info[AUFS_MVDOWN_UPPER].h_inode
  23820. +#define mvd_pin_src info[AUFS_MVDOWN_UPPER].pin
  23821. +
  23822. +#define mvd_h_dst_sb info[AUFS_MVDOWN_LOWER].h_sb
  23823. +#define mvd_h_dst_parent info[AUFS_MVDOWN_LOWER].h_parent
  23824. +#define mvd_hdir_dst info[AUFS_MVDOWN_LOWER].hdir
  23825. +#define mvd_h_dst_dir info[AUFS_MVDOWN_LOWER].h_dir
  23826. +#define mvd_h_dst_inode info[AUFS_MVDOWN_LOWER].h_inode
  23827. +#define mvd_pin_dst info[AUFS_MVDOWN_LOWER].pin
  23828. +
  23829. +#define AU_MVD_PR(flag, ...) do { \
  23830. + if (flag) \
  23831. + pr_err(__VA_ARGS__); \
  23832. + } while (0)
  23833. +
  23834. +static int find_lower_writable(struct au_mvd_args *a)
  23835. +{
  23836. + struct super_block *sb;
  23837. + aufs_bindex_t bindex, bbot;
  23838. + struct au_branch *br;
  23839. +
  23840. + sb = a->sb;
  23841. + bindex = a->mvd_bsrc;
  23842. + bbot = au_sbbot(sb);
  23843. + if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
  23844. + for (bindex++; bindex <= bbot; bindex++) {
  23845. + br = au_sbr(sb, bindex);
  23846. + if (au_br_fhsm(br->br_perm)
  23847. + && !sb_rdonly(au_br_sb(br)))
  23848. + return bindex;
  23849. + }
  23850. + else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
  23851. + for (bindex++; bindex <= bbot; bindex++) {
  23852. + br = au_sbr(sb, bindex);
  23853. + if (!au_br_rdonly(br))
  23854. + return bindex;
  23855. + }
  23856. + else
  23857. + for (bindex++; bindex <= bbot; bindex++) {
  23858. + br = au_sbr(sb, bindex);
  23859. + if (!sb_rdonly(au_br_sb(br))) {
  23860. + if (au_br_rdonly(br))
  23861. + a->mvdown.flags
  23862. + |= AUFS_MVDOWN_ROLOWER_R;
  23863. + return bindex;
  23864. + }
  23865. + }
  23866. +
  23867. + return -1;
  23868. +}
  23869. +
  23870. +/* make the parent dir on bdst */
  23871. +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
  23872. +{
  23873. + int err;
  23874. +
  23875. + err = 0;
  23876. + a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
  23877. + a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
  23878. + a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
  23879. + a->mvd_h_dst_parent = NULL;
  23880. + if (au_dbbot(a->parent) >= a->mvd_bdst)
  23881. + a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
  23882. + if (!a->mvd_h_dst_parent) {
  23883. + err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
  23884. + if (unlikely(err)) {
  23885. + AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
  23886. + goto out;
  23887. + }
  23888. + a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
  23889. + }
  23890. +
  23891. +out:
  23892. + AuTraceErr(err);
  23893. + return err;
  23894. +}
  23895. +
  23896. +/* lock them all */
  23897. +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
  23898. +{
  23899. + int err;
  23900. + struct dentry *h_trap;
  23901. +
  23902. + a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
  23903. + a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
  23904. + err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
  23905. + au_opt_udba(a->sb),
  23906. + AuPin_MNT_WRITE | AuPin_DI_LOCKED);
  23907. + AuTraceErr(err);
  23908. + if (unlikely(err)) {
  23909. + AU_MVD_PR(dmsg, "pin_dst failed\n");
  23910. + goto out;
  23911. + }
  23912. +
  23913. + if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
  23914. + a->rename_lock = 0;
  23915. + au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
  23916. + AuLsc_DI_PARENT, AuLsc_I_PARENT3,
  23917. + au_opt_udba(a->sb),
  23918. + AuPin_MNT_WRITE | AuPin_DI_LOCKED);
  23919. + err = au_do_pin(&a->mvd_pin_src);
  23920. + AuTraceErr(err);
  23921. + a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
  23922. + if (unlikely(err)) {
  23923. + AU_MVD_PR(dmsg, "pin_src failed\n");
  23924. + goto out_dst;
  23925. + }
  23926. + goto out; /* success */
  23927. + }
  23928. +
  23929. + a->rename_lock = 1;
  23930. + au_pin_hdir_unlock(&a->mvd_pin_dst);
  23931. + err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
  23932. + au_opt_udba(a->sb),
  23933. + AuPin_MNT_WRITE | AuPin_DI_LOCKED);
  23934. + AuTraceErr(err);
  23935. + a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
  23936. + if (unlikely(err)) {
  23937. + AU_MVD_PR(dmsg, "pin_src failed\n");
  23938. + au_pin_hdir_lock(&a->mvd_pin_dst);
  23939. + goto out_dst;
  23940. + }
  23941. + au_pin_hdir_unlock(&a->mvd_pin_src);
  23942. + h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
  23943. + a->mvd_h_dst_parent, a->mvd_hdir_dst);
  23944. + if (h_trap) {
  23945. + err = (h_trap != a->mvd_h_src_parent);
  23946. + if (err)
  23947. + err = (h_trap != a->mvd_h_dst_parent);
  23948. + }
  23949. + BUG_ON(err); /* it should never happen */
  23950. + if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
  23951. + err = -EBUSY;
  23952. + AuTraceErr(err);
  23953. + vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
  23954. + a->mvd_h_dst_parent, a->mvd_hdir_dst);
  23955. + au_pin_hdir_lock(&a->mvd_pin_src);
  23956. + au_unpin(&a->mvd_pin_src);
  23957. + au_pin_hdir_lock(&a->mvd_pin_dst);
  23958. + goto out_dst;
  23959. + }
  23960. + goto out; /* success */
  23961. +
  23962. +out_dst:
  23963. + au_unpin(&a->mvd_pin_dst);
  23964. +out:
  23965. + AuTraceErr(err);
  23966. + return err;
  23967. +}
  23968. +
  23969. +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
  23970. +{
  23971. + if (!a->rename_lock)
  23972. + au_unpin(&a->mvd_pin_src);
  23973. + else {
  23974. + vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
  23975. + a->mvd_h_dst_parent, a->mvd_hdir_dst);
  23976. + au_pin_hdir_lock(&a->mvd_pin_src);
  23977. + au_unpin(&a->mvd_pin_src);
  23978. + au_pin_hdir_lock(&a->mvd_pin_dst);
  23979. + }
  23980. + au_unpin(&a->mvd_pin_dst);
  23981. +}
  23982. +
  23983. +/* copy-down the file */
  23984. +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
  23985. +{
  23986. + int err;
  23987. + struct au_cp_generic cpg = {
  23988. + .dentry = a->dentry,
  23989. + .bdst = a->mvd_bdst,
  23990. + .bsrc = a->mvd_bsrc,
  23991. + .len = -1,
  23992. + .pin = &a->mvd_pin_dst,
  23993. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  23994. + };
  23995. +
  23996. + AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
  23997. + if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
  23998. + au_fset_cpup(cpg.flags, OVERWRITE);
  23999. + if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
  24000. + au_fset_cpup(cpg.flags, RWDST);
  24001. + err = au_sio_cpdown_simple(&cpg);
  24002. + if (unlikely(err))
  24003. + AU_MVD_PR(dmsg, "cpdown failed\n");
  24004. +
  24005. + AuTraceErr(err);
  24006. + return err;
  24007. +}
  24008. +
  24009. +/*
  24010. + * unlink the whiteout on bdst if exist which may be created by UDBA while we
  24011. + * were sleeping
  24012. + */
  24013. +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
  24014. +{
  24015. + int err;
  24016. + struct path h_path;
  24017. + struct au_branch *br;
  24018. + struct inode *delegated;
  24019. +
  24020. + br = au_sbr(a->sb, a->mvd_bdst);
  24021. + h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
  24022. + err = PTR_ERR(h_path.dentry);
  24023. + if (IS_ERR(h_path.dentry)) {
  24024. + AU_MVD_PR(dmsg, "wh_lkup failed\n");
  24025. + goto out;
  24026. + }
  24027. +
  24028. + err = 0;
  24029. + if (d_is_positive(h_path.dentry)) {
  24030. + h_path.mnt = au_br_mnt(br);
  24031. + delegated = NULL;
  24032. + err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
  24033. + &delegated, /*force*/0);
  24034. + if (unlikely(err == -EWOULDBLOCK)) {
  24035. + pr_warn("cannot retry for NFSv4 delegation"
  24036. + " for an internal unlink\n");
  24037. + iput(delegated);
  24038. + }
  24039. + if (unlikely(err))
  24040. + AU_MVD_PR(dmsg, "wh_unlink failed\n");
  24041. + }
  24042. + dput(h_path.dentry);
  24043. +
  24044. +out:
  24045. + AuTraceErr(err);
  24046. + return err;
  24047. +}
  24048. +
  24049. +/*
  24050. + * unlink the topmost h_dentry
  24051. + */
  24052. +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
  24053. +{
  24054. + int err;
  24055. + struct path h_path;
  24056. + struct inode *delegated;
  24057. +
  24058. + h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
  24059. + h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
  24060. + delegated = NULL;
  24061. + err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
  24062. + if (unlikely(err == -EWOULDBLOCK)) {
  24063. + pr_warn("cannot retry for NFSv4 delegation"
  24064. + " for an internal unlink\n");
  24065. + iput(delegated);
  24066. + }
  24067. + if (unlikely(err))
  24068. + AU_MVD_PR(dmsg, "unlink failed\n");
  24069. +
  24070. + AuTraceErr(err);
  24071. + return err;
  24072. +}
  24073. +
  24074. +/* Since mvdown succeeded, we ignore an error of this function */
  24075. +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
  24076. +{
  24077. + int err;
  24078. + struct au_branch *br;
  24079. +
  24080. + a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
  24081. + br = au_sbr(a->sb, a->mvd_bsrc);
  24082. + err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
  24083. + if (!err) {
  24084. + br = au_sbr(a->sb, a->mvd_bdst);
  24085. + a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
  24086. + err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
  24087. + }
  24088. + if (!err)
  24089. + a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
  24090. + else
  24091. + AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
  24092. +}
  24093. +
  24094. +/*
  24095. + * copy-down the file and unlink the bsrc file.
  24096. + * - unlink the bdst whout if exist
  24097. + * - copy-down the file (with whtmp name and rename)
  24098. + * - unlink the bsrc file
  24099. + */
  24100. +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
  24101. +{
  24102. + int err;
  24103. +
  24104. + err = au_do_mkdir(dmsg, a);
  24105. + if (!err)
  24106. + err = au_do_lock(dmsg, a);
  24107. + if (unlikely(err))
  24108. + goto out;
  24109. +
  24110. + /*
  24111. + * do not revert the activities we made on bdst since they should be
  24112. + * harmless in aufs.
  24113. + */
  24114. +
  24115. + err = au_do_cpdown(dmsg, a);
  24116. + if (!err)
  24117. + err = au_do_unlink_wh(dmsg, a);
  24118. + if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
  24119. + err = au_do_unlink(dmsg, a);
  24120. + if (unlikely(err))
  24121. + goto out_unlock;
  24122. +
  24123. + AuDbg("%pd2, 0x%x, %d --> %d\n",
  24124. + a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
  24125. + if (find_lower_writable(a) < 0)
  24126. + a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
  24127. +
  24128. + if (a->mvdown.flags & AUFS_MVDOWN_STFS)
  24129. + au_do_stfs(dmsg, a);
  24130. +
  24131. + /* maintain internal array */
  24132. + if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
  24133. + au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
  24134. + au_set_dbtop(a->dentry, a->mvd_bdst);
  24135. + au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
  24136. + au_set_ibtop(a->inode, a->mvd_bdst);
  24137. + } else {
  24138. + /* hide the lower */
  24139. + au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
  24140. + au_set_dbbot(a->dentry, a->mvd_bsrc);
  24141. + au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
  24142. + au_set_ibbot(a->inode, a->mvd_bsrc);
  24143. + }
  24144. + if (au_dbbot(a->dentry) < a->mvd_bdst)
  24145. + au_set_dbbot(a->dentry, a->mvd_bdst);
  24146. + if (au_ibbot(a->inode) < a->mvd_bdst)
  24147. + au_set_ibbot(a->inode, a->mvd_bdst);
  24148. +
  24149. +out_unlock:
  24150. + au_do_unlock(dmsg, a);
  24151. +out:
  24152. + AuTraceErr(err);
  24153. + return err;
  24154. +}
  24155. +
  24156. +/* ---------------------------------------------------------------------- */
  24157. +
  24158. +/* make sure the file is idle */
  24159. +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
  24160. +{
  24161. + int err, plinked;
  24162. +
  24163. + err = 0;
  24164. + plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
  24165. + if (au_dbtop(a->dentry) == a->mvd_bsrc
  24166. + && au_dcount(a->dentry) == 1
  24167. + && atomic_read(&a->inode->i_count) == 1
  24168. + /* && a->mvd_h_src_inode->i_nlink == 1 */
  24169. + && (!plinked || !au_plink_test(a->inode))
  24170. + && a->inode->i_nlink == 1)
  24171. + goto out;
  24172. +
  24173. + err = -EBUSY;
  24174. + AU_MVD_PR(dmsg,
  24175. + "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
  24176. + a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
  24177. + atomic_read(&a->inode->i_count), a->inode->i_nlink,
  24178. + a->mvd_h_src_inode->i_nlink,
  24179. + plinked, plinked ? au_plink_test(a->inode) : 0);
  24180. +
  24181. +out:
  24182. + AuTraceErr(err);
  24183. + return err;
  24184. +}
  24185. +
  24186. +/* make sure the parent dir is fine */
  24187. +static int au_mvd_args_parent(const unsigned char dmsg,
  24188. + struct au_mvd_args *a)
  24189. +{
  24190. + int err;
  24191. + aufs_bindex_t bindex;
  24192. +
  24193. + err = 0;
  24194. + if (unlikely(au_alive_dir(a->parent))) {
  24195. + err = -ENOENT;
  24196. + AU_MVD_PR(dmsg, "parent dir is dead\n");
  24197. + goto out;
  24198. + }
  24199. +
  24200. + a->bopq = au_dbdiropq(a->parent);
  24201. + bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
  24202. + AuDbg("b%d\n", bindex);
  24203. + if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
  24204. + || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
  24205. + err = -EINVAL;
  24206. + a->mvd_errno = EAU_MVDOWN_OPAQUE;
  24207. + AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
  24208. + a->bopq, a->mvd_bdst);
  24209. + }
  24210. +
  24211. +out:
  24212. + AuTraceErr(err);
  24213. + return err;
  24214. +}
  24215. +
  24216. +static int au_mvd_args_intermediate(const unsigned char dmsg,
  24217. + struct au_mvd_args *a)
  24218. +{
  24219. + int err;
  24220. + struct au_dinfo *dinfo, *tmp;
  24221. +
  24222. + /* lookup the next lower positive entry */
  24223. + err = -ENOMEM;
  24224. + tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
  24225. + if (unlikely(!tmp))
  24226. + goto out;
  24227. +
  24228. + a->bfound = -1;
  24229. + a->bwh = -1;
  24230. + dinfo = au_di(a->dentry);
  24231. + au_di_cp(tmp, dinfo);
  24232. + au_di_swap(tmp, dinfo);
  24233. +
  24234. + /* returns the number of positive dentries */
  24235. + err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
  24236. + /* AuLkup_IGNORE_PERM */ 0);
  24237. + if (!err)
  24238. + a->bwh = au_dbwh(a->dentry);
  24239. + else if (err > 0)
  24240. + a->bfound = au_dbtop(a->dentry);
  24241. +
  24242. + au_di_swap(tmp, dinfo);
  24243. + au_rw_write_unlock(&tmp->di_rwsem);
  24244. + au_di_free(tmp);
  24245. + if (unlikely(err < 0))
  24246. + AU_MVD_PR(dmsg, "failed look-up lower\n");
  24247. +
  24248. + /*
  24249. + * here, we have these cases.
  24250. + * bfound == -1
  24251. + * no positive dentry under bsrc. there are more sub-cases.
  24252. + * bwh < 0
  24253. + * there no whiteout, we can safely move-down.
  24254. + * bwh <= bsrc
  24255. + * impossible
  24256. + * bsrc < bwh && bwh < bdst
  24257. + * there is a whiteout on RO branch. cannot proceed.
  24258. + * bwh == bdst
  24259. + * there is a whiteout on the RW target branch. it should
  24260. + * be removed.
  24261. + * bdst < bwh
  24262. + * there is a whiteout somewhere unrelated branch.
  24263. + * -1 < bfound && bfound <= bsrc
  24264. + * impossible.
  24265. + * bfound < bdst
  24266. + * found, but it is on RO branch between bsrc and bdst. cannot
  24267. + * proceed.
  24268. + * bfound == bdst
  24269. + * found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
  24270. + * error.
  24271. + * bdst < bfound
  24272. + * found, after we create the file on bdst, it will be hidden.
  24273. + */
  24274. +
  24275. + AuDebugOn(a->bfound == -1
  24276. + && a->bwh != -1
  24277. + && a->bwh <= a->mvd_bsrc);
  24278. + AuDebugOn(-1 < a->bfound
  24279. + && a->bfound <= a->mvd_bsrc);
  24280. +
  24281. + err = -EINVAL;
  24282. + if (a->bfound == -1
  24283. + && a->mvd_bsrc < a->bwh
  24284. + && a->bwh != -1
  24285. + && a->bwh < a->mvd_bdst) {
  24286. + a->mvd_errno = EAU_MVDOWN_WHITEOUT;
  24287. + AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
  24288. + a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
  24289. + goto out;
  24290. + } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
  24291. + a->mvd_errno = EAU_MVDOWN_UPPER;
  24292. + AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
  24293. + a->mvd_bdst, a->bfound);
  24294. + goto out;
  24295. + }
  24296. +
  24297. + err = 0; /* success */
  24298. +
  24299. +out:
  24300. + AuTraceErr(err);
  24301. + return err;
  24302. +}
  24303. +
  24304. +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
  24305. +{
  24306. + int err;
  24307. +
  24308. + err = 0;
  24309. + if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
  24310. + && a->bfound == a->mvd_bdst)
  24311. + err = -EEXIST;
  24312. + AuTraceErr(err);
  24313. + return err;
  24314. +}
  24315. +
  24316. +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
  24317. +{
  24318. + int err;
  24319. + struct au_branch *br;
  24320. +
  24321. + err = -EISDIR;
  24322. + if (unlikely(S_ISDIR(a->inode->i_mode)))
  24323. + goto out;
  24324. +
  24325. + err = -EINVAL;
  24326. + if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
  24327. + a->mvd_bsrc = au_ibtop(a->inode);
  24328. + else {
  24329. + a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
  24330. + if (unlikely(a->mvd_bsrc < 0
  24331. + || (a->mvd_bsrc < au_dbtop(a->dentry)
  24332. + || au_dbbot(a->dentry) < a->mvd_bsrc
  24333. + || !au_h_dptr(a->dentry, a->mvd_bsrc))
  24334. + || (a->mvd_bsrc < au_ibtop(a->inode)
  24335. + || au_ibbot(a->inode) < a->mvd_bsrc
  24336. + || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
  24337. + a->mvd_errno = EAU_MVDOWN_NOUPPER;
  24338. + AU_MVD_PR(dmsg, "no upper\n");
  24339. + goto out;
  24340. + }
  24341. + }
  24342. + if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
  24343. + a->mvd_errno = EAU_MVDOWN_BOTTOM;
  24344. + AU_MVD_PR(dmsg, "on the bottom\n");
  24345. + goto out;
  24346. + }
  24347. + a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
  24348. + br = au_sbr(a->sb, a->mvd_bsrc);
  24349. + err = au_br_rdonly(br);
  24350. + if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
  24351. + if (unlikely(err))
  24352. + goto out;
  24353. + } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
  24354. + || IS_APPEND(a->mvd_h_src_inode))) {
  24355. + if (err)
  24356. + a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
  24357. + /* go on */
  24358. + } else
  24359. + goto out;
  24360. +
  24361. + err = -EINVAL;
  24362. + if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
  24363. + a->mvd_bdst = find_lower_writable(a);
  24364. + if (unlikely(a->mvd_bdst < 0)) {
  24365. + a->mvd_errno = EAU_MVDOWN_BOTTOM;
  24366. + AU_MVD_PR(dmsg, "no writable lower branch\n");
  24367. + goto out;
  24368. + }
  24369. + } else {
  24370. + a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
  24371. + if (unlikely(a->mvd_bdst < 0
  24372. + || au_sbbot(a->sb) < a->mvd_bdst)) {
  24373. + a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
  24374. + AU_MVD_PR(dmsg, "no lower brid\n");
  24375. + goto out;
  24376. + }
  24377. + }
  24378. +
  24379. + err = au_mvd_args_busy(dmsg, a);
  24380. + if (!err)
  24381. + err = au_mvd_args_parent(dmsg, a);
  24382. + if (!err)
  24383. + err = au_mvd_args_intermediate(dmsg, a);
  24384. + if (!err)
  24385. + err = au_mvd_args_exist(dmsg, a);
  24386. + if (!err)
  24387. + AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
  24388. +
  24389. +out:
  24390. + AuTraceErr(err);
  24391. + return err;
  24392. +}
  24393. +
  24394. +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
  24395. +{
  24396. + int err, e;
  24397. + unsigned char dmsg;
  24398. + struct au_mvd_args *args;
  24399. + struct inode *inode;
  24400. +
  24401. + inode = d_inode(dentry);
  24402. + err = -EPERM;
  24403. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  24404. + goto out;
  24405. +
  24406. + err = -ENOMEM;
  24407. + args = kmalloc(sizeof(*args), GFP_NOFS);
  24408. + if (unlikely(!args))
  24409. + goto out;
  24410. +
  24411. + err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
  24412. + if (!err)
  24413. + err = !access_ok(VERIFY_WRITE, uarg, sizeof(*uarg));
  24414. + if (unlikely(err)) {
  24415. + err = -EFAULT;
  24416. + AuTraceErr(err);
  24417. + goto out_free;
  24418. + }
  24419. + AuDbg("flags 0x%x\n", args->mvdown.flags);
  24420. + args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
  24421. + args->mvdown.au_errno = 0;
  24422. + args->dentry = dentry;
  24423. + args->inode = inode;
  24424. + args->sb = dentry->d_sb;
  24425. +
  24426. + err = -ENOENT;
  24427. + dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
  24428. + args->parent = dget_parent(dentry);
  24429. + args->dir = d_inode(args->parent);
  24430. + inode_lock_nested(args->dir, I_MUTEX_PARENT);
  24431. + dput(args->parent);
  24432. + if (unlikely(args->parent != dentry->d_parent)) {
  24433. + AU_MVD_PR(dmsg, "parent dir is moved\n");
  24434. + goto out_dir;
  24435. + }
  24436. +
  24437. + inode_lock_nested(inode, I_MUTEX_CHILD);
  24438. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
  24439. + if (unlikely(err))
  24440. + goto out_inode;
  24441. +
  24442. + di_write_lock_parent(args->parent);
  24443. + err = au_mvd_args(dmsg, args);
  24444. + if (unlikely(err))
  24445. + goto out_parent;
  24446. +
  24447. + err = au_do_mvdown(dmsg, args);
  24448. + if (unlikely(err))
  24449. + goto out_parent;
  24450. +
  24451. + au_cpup_attr_timesizes(args->dir);
  24452. + au_cpup_attr_timesizes(inode);
  24453. + if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
  24454. + au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
  24455. + /* au_digen_dec(dentry); */
  24456. +
  24457. +out_parent:
  24458. + di_write_unlock(args->parent);
  24459. + aufs_read_unlock(dentry, AuLock_DW);
  24460. +out_inode:
  24461. + inode_unlock(inode);
  24462. +out_dir:
  24463. + inode_unlock(args->dir);
  24464. +out_free:
  24465. + e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
  24466. + if (unlikely(e))
  24467. + err = -EFAULT;
  24468. + kfree(args);
  24469. +out:
  24470. + AuTraceErr(err);
  24471. + return err;
  24472. +}
  24473. --- /dev/null
  24474. +++ linux-4.19/fs/aufs/opts.c 2018-11-20 09:35:15.037195504 +0200
  24475. @@ -0,0 +1,1877 @@
  24476. +// SPDX-License-Identifier: GPL-2.0
  24477. +/*
  24478. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  24479. + *
  24480. + * This program, aufs is free software; you can redistribute it and/or modify
  24481. + * it under the terms of the GNU General Public License as published by
  24482. + * the Free Software Foundation; either version 2 of the License, or
  24483. + * (at your option) any later version.
  24484. + *
  24485. + * This program is distributed in the hope that it will be useful,
  24486. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24487. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24488. + * GNU General Public License for more details.
  24489. + *
  24490. + * You should have received a copy of the GNU General Public License
  24491. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24492. + */
  24493. +
  24494. +/*
  24495. + * mount options/flags
  24496. + */
  24497. +
  24498. +#include <linux/namei.h>
  24499. +#include <linux/types.h> /* a distribution requires */
  24500. +#include <linux/parser.h>
  24501. +#include "aufs.h"
  24502. +
  24503. +/* ---------------------------------------------------------------------- */
  24504. +
  24505. +enum {
  24506. + Opt_br,
  24507. + Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
  24508. + Opt_idel, Opt_imod,
  24509. + Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
  24510. + Opt_rdblk_def, Opt_rdhash_def,
  24511. + Opt_xino, Opt_noxino,
  24512. + Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
  24513. + Opt_trunc_xino_path, Opt_itrunc_xino,
  24514. + Opt_trunc_xib, Opt_notrunc_xib,
  24515. + Opt_shwh, Opt_noshwh,
  24516. + Opt_plink, Opt_noplink, Opt_list_plink,
  24517. + Opt_udba,
  24518. + Opt_dio, Opt_nodio,
  24519. + Opt_diropq_a, Opt_diropq_w,
  24520. + Opt_warn_perm, Opt_nowarn_perm,
  24521. + Opt_wbr_copyup, Opt_wbr_create,
  24522. + Opt_fhsm_sec,
  24523. + Opt_verbose, Opt_noverbose,
  24524. + Opt_sum, Opt_nosum, Opt_wsum,
  24525. + Opt_dirperm1, Opt_nodirperm1,
  24526. + Opt_dirren, Opt_nodirren,
  24527. + Opt_acl, Opt_noacl,
  24528. + Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
  24529. +};
  24530. +
  24531. +static match_table_t options = {
  24532. + {Opt_br, "br=%s"},
  24533. + {Opt_br, "br:%s"},
  24534. +
  24535. + {Opt_add, "add=%d:%s"},
  24536. + {Opt_add, "add:%d:%s"},
  24537. + {Opt_add, "ins=%d:%s"},
  24538. + {Opt_add, "ins:%d:%s"},
  24539. + {Opt_append, "append=%s"},
  24540. + {Opt_append, "append:%s"},
  24541. + {Opt_prepend, "prepend=%s"},
  24542. + {Opt_prepend, "prepend:%s"},
  24543. +
  24544. + {Opt_del, "del=%s"},
  24545. + {Opt_del, "del:%s"},
  24546. + /* {Opt_idel, "idel:%d"}, */
  24547. + {Opt_mod, "mod=%s"},
  24548. + {Opt_mod, "mod:%s"},
  24549. + /* {Opt_imod, "imod:%d:%s"}, */
  24550. +
  24551. + {Opt_dirwh, "dirwh=%d"},
  24552. +
  24553. + {Opt_xino, "xino=%s"},
  24554. + {Opt_noxino, "noxino"},
  24555. + {Opt_trunc_xino, "trunc_xino"},
  24556. + {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
  24557. + {Opt_notrunc_xino, "notrunc_xino"},
  24558. + {Opt_trunc_xino_path, "trunc_xino=%s"},
  24559. + {Opt_itrunc_xino, "itrunc_xino=%d"},
  24560. + /* {Opt_zxino, "zxino=%s"}, */
  24561. + {Opt_trunc_xib, "trunc_xib"},
  24562. + {Opt_notrunc_xib, "notrunc_xib"},
  24563. +
  24564. +#ifdef CONFIG_PROC_FS
  24565. + {Opt_plink, "plink"},
  24566. +#else
  24567. + {Opt_ignore_silent, "plink"},
  24568. +#endif
  24569. +
  24570. + {Opt_noplink, "noplink"},
  24571. +
  24572. +#ifdef CONFIG_AUFS_DEBUG
  24573. + {Opt_list_plink, "list_plink"},
  24574. +#endif
  24575. +
  24576. + {Opt_udba, "udba=%s"},
  24577. +
  24578. + {Opt_dio, "dio"},
  24579. + {Opt_nodio, "nodio"},
  24580. +
  24581. +#ifdef CONFIG_AUFS_DIRREN
  24582. + {Opt_dirren, "dirren"},
  24583. + {Opt_nodirren, "nodirren"},
  24584. +#else
  24585. + {Opt_ignore, "dirren"},
  24586. + {Opt_ignore_silent, "nodirren"},
  24587. +#endif
  24588. +
  24589. +#ifdef CONFIG_AUFS_FHSM
  24590. + {Opt_fhsm_sec, "fhsm_sec=%d"},
  24591. +#else
  24592. + {Opt_ignore, "fhsm_sec=%d"},
  24593. +#endif
  24594. +
  24595. + {Opt_diropq_a, "diropq=always"},
  24596. + {Opt_diropq_a, "diropq=a"},
  24597. + {Opt_diropq_w, "diropq=whiteouted"},
  24598. + {Opt_diropq_w, "diropq=w"},
  24599. +
  24600. + {Opt_warn_perm, "warn_perm"},
  24601. + {Opt_nowarn_perm, "nowarn_perm"},
  24602. +
  24603. + /* keep them temporary */
  24604. + {Opt_ignore_silent, "nodlgt"},
  24605. + {Opt_ignore, "clean_plink"},
  24606. +
  24607. +#ifdef CONFIG_AUFS_SHWH
  24608. + {Opt_shwh, "shwh"},
  24609. +#endif
  24610. + {Opt_noshwh, "noshwh"},
  24611. +
  24612. + {Opt_dirperm1, "dirperm1"},
  24613. + {Opt_nodirperm1, "nodirperm1"},
  24614. +
  24615. + {Opt_verbose, "verbose"},
  24616. + {Opt_verbose, "v"},
  24617. + {Opt_noverbose, "noverbose"},
  24618. + {Opt_noverbose, "quiet"},
  24619. + {Opt_noverbose, "q"},
  24620. + {Opt_noverbose, "silent"},
  24621. +
  24622. + {Opt_sum, "sum"},
  24623. + {Opt_nosum, "nosum"},
  24624. + {Opt_wsum, "wsum"},
  24625. +
  24626. + {Opt_rdcache, "rdcache=%d"},
  24627. + {Opt_rdblk, "rdblk=%d"},
  24628. + {Opt_rdblk_def, "rdblk=def"},
  24629. + {Opt_rdhash, "rdhash=%d"},
  24630. + {Opt_rdhash_def, "rdhash=def"},
  24631. +
  24632. + {Opt_wbr_create, "create=%s"},
  24633. + {Opt_wbr_create, "create_policy=%s"},
  24634. + {Opt_wbr_copyup, "cpup=%s"},
  24635. + {Opt_wbr_copyup, "copyup=%s"},
  24636. + {Opt_wbr_copyup, "copyup_policy=%s"},
  24637. +
  24638. + /* generic VFS flag */
  24639. +#ifdef CONFIG_FS_POSIX_ACL
  24640. + {Opt_acl, "acl"},
  24641. + {Opt_noacl, "noacl"},
  24642. +#else
  24643. + {Opt_ignore, "acl"},
  24644. + {Opt_ignore_silent, "noacl"},
  24645. +#endif
  24646. +
  24647. + /* internal use for the scripts */
  24648. + {Opt_ignore_silent, "si=%s"},
  24649. +
  24650. + {Opt_br, "dirs=%s"},
  24651. + {Opt_ignore, "debug=%d"},
  24652. + {Opt_ignore, "delete=whiteout"},
  24653. + {Opt_ignore, "delete=all"},
  24654. + {Opt_ignore, "imap=%s"},
  24655. +
  24656. + /* temporary workaround, due to old mount(8)? */
  24657. + {Opt_ignore_silent, "relatime"},
  24658. +
  24659. + {Opt_err, NULL}
  24660. +};
  24661. +
  24662. +/* ---------------------------------------------------------------------- */
  24663. +
  24664. +static const char *au_parser_pattern(int val, match_table_t tbl)
  24665. +{
  24666. + struct match_token *p;
  24667. +
  24668. + p = tbl;
  24669. + while (p->pattern) {
  24670. + if (p->token == val)
  24671. + return p->pattern;
  24672. + p++;
  24673. + }
  24674. + BUG();
  24675. + return "??";
  24676. +}
  24677. +
  24678. +static const char *au_optstr(int *val, match_table_t tbl)
  24679. +{
  24680. + struct match_token *p;
  24681. + int v;
  24682. +
  24683. + v = *val;
  24684. + if (!v)
  24685. + goto out;
  24686. + p = tbl;
  24687. + while (p->pattern) {
  24688. + if (p->token
  24689. + && (v & p->token) == p->token) {
  24690. + *val &= ~p->token;
  24691. + return p->pattern;
  24692. + }
  24693. + p++;
  24694. + }
  24695. +
  24696. +out:
  24697. + return NULL;
  24698. +}
  24699. +
  24700. +/* ---------------------------------------------------------------------- */
  24701. +
  24702. +static match_table_t brperm = {
  24703. + {AuBrPerm_RO, AUFS_BRPERM_RO},
  24704. + {AuBrPerm_RR, AUFS_BRPERM_RR},
  24705. + {AuBrPerm_RW, AUFS_BRPERM_RW},
  24706. + {0, NULL}
  24707. +};
  24708. +
  24709. +static match_table_t brattr = {
  24710. + /* general */
  24711. + {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
  24712. + {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
  24713. + /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
  24714. + {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
  24715. +#ifdef CONFIG_AUFS_FHSM
  24716. + {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
  24717. +#endif
  24718. +#ifdef CONFIG_AUFS_XATTR
  24719. + {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
  24720. + {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
  24721. + {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
  24722. + {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
  24723. + {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
  24724. + {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
  24725. +#endif
  24726. +
  24727. + /* ro/rr branch */
  24728. + {AuBrRAttr_WH, AUFS_BRRATTR_WH},
  24729. +
  24730. + /* rw branch */
  24731. + {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
  24732. + {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
  24733. +
  24734. + {0, NULL}
  24735. +};
  24736. +
  24737. +static int br_attr_val(char *str, match_table_t table, substring_t args[])
  24738. +{
  24739. + int attr, v;
  24740. + char *p;
  24741. +
  24742. + attr = 0;
  24743. + do {
  24744. + p = strchr(str, '+');
  24745. + if (p)
  24746. + *p = 0;
  24747. + v = match_token(str, table, args);
  24748. + if (v) {
  24749. + if (v & AuBrAttr_CMOO_Mask)
  24750. + attr &= ~AuBrAttr_CMOO_Mask;
  24751. + attr |= v;
  24752. + } else {
  24753. + if (p)
  24754. + *p = '+';
  24755. + pr_warn("ignored branch attribute %s\n", str);
  24756. + break;
  24757. + }
  24758. + if (p)
  24759. + str = p + 1;
  24760. + } while (p);
  24761. +
  24762. + return attr;
  24763. +}
  24764. +
  24765. +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
  24766. +{
  24767. + int sz;
  24768. + const char *p;
  24769. + char *q;
  24770. +
  24771. + q = str->a;
  24772. + *q = 0;
  24773. + p = au_optstr(&perm, brattr);
  24774. + if (p) {
  24775. + sz = strlen(p);
  24776. + memcpy(q, p, sz + 1);
  24777. + q += sz;
  24778. + } else
  24779. + goto out;
  24780. +
  24781. + do {
  24782. + p = au_optstr(&perm, brattr);
  24783. + if (p) {
  24784. + *q++ = '+';
  24785. + sz = strlen(p);
  24786. + memcpy(q, p, sz + 1);
  24787. + q += sz;
  24788. + }
  24789. + } while (p);
  24790. +
  24791. +out:
  24792. + return q - str->a;
  24793. +}
  24794. +
  24795. +static int noinline_for_stack br_perm_val(char *perm)
  24796. +{
  24797. + int val, bad, sz;
  24798. + char *p;
  24799. + substring_t args[MAX_OPT_ARGS];
  24800. + au_br_perm_str_t attr;
  24801. +
  24802. + p = strchr(perm, '+');
  24803. + if (p)
  24804. + *p = 0;
  24805. + val = match_token(perm, brperm, args);
  24806. + if (!val) {
  24807. + if (p)
  24808. + *p = '+';
  24809. + pr_warn("ignored branch permission %s\n", perm);
  24810. + val = AuBrPerm_RO;
  24811. + goto out;
  24812. + }
  24813. + if (!p)
  24814. + goto out;
  24815. +
  24816. + val |= br_attr_val(p + 1, brattr, args);
  24817. +
  24818. + bad = 0;
  24819. + switch (val & AuBrPerm_Mask) {
  24820. + case AuBrPerm_RO:
  24821. + case AuBrPerm_RR:
  24822. + bad = val & AuBrWAttr_Mask;
  24823. + val &= ~AuBrWAttr_Mask;
  24824. + break;
  24825. + case AuBrPerm_RW:
  24826. + bad = val & AuBrRAttr_Mask;
  24827. + val &= ~AuBrRAttr_Mask;
  24828. + break;
  24829. + }
  24830. +
  24831. + /*
  24832. + * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
  24833. + * does not treat it as an error, just warning.
  24834. + * this is a tiny guard for the user operation.
  24835. + */
  24836. + if (val & AuBrAttr_UNPIN) {
  24837. + bad |= AuBrAttr_UNPIN;
  24838. + val &= ~AuBrAttr_UNPIN;
  24839. + }
  24840. +
  24841. + if (unlikely(bad)) {
  24842. + sz = au_do_optstr_br_attr(&attr, bad);
  24843. + AuDebugOn(!sz);
  24844. + pr_warn("ignored branch attribute %s\n", attr.a);
  24845. + }
  24846. +
  24847. +out:
  24848. + return val;
  24849. +}
  24850. +
  24851. +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
  24852. +{
  24853. + au_br_perm_str_t attr;
  24854. + const char *p;
  24855. + char *q;
  24856. + int sz;
  24857. +
  24858. + q = str->a;
  24859. + p = au_optstr(&perm, brperm);
  24860. + AuDebugOn(!p || !*p);
  24861. + sz = strlen(p);
  24862. + memcpy(q, p, sz + 1);
  24863. + q += sz;
  24864. +
  24865. + sz = au_do_optstr_br_attr(&attr, perm);
  24866. + if (sz) {
  24867. + *q++ = '+';
  24868. + memcpy(q, attr.a, sz + 1);
  24869. + }
  24870. +
  24871. + AuDebugOn(strlen(str->a) >= sizeof(str->a));
  24872. +}
  24873. +
  24874. +/* ---------------------------------------------------------------------- */
  24875. +
  24876. +static match_table_t udbalevel = {
  24877. + {AuOpt_UDBA_REVAL, "reval"},
  24878. + {AuOpt_UDBA_NONE, "none"},
  24879. +#ifdef CONFIG_AUFS_HNOTIFY
  24880. + {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
  24881. +#ifdef CONFIG_AUFS_HFSNOTIFY
  24882. + {AuOpt_UDBA_HNOTIFY, "fsnotify"},
  24883. +#endif
  24884. +#endif
  24885. + {-1, NULL}
  24886. +};
  24887. +
  24888. +static int noinline_for_stack udba_val(char *str)
  24889. +{
  24890. + substring_t args[MAX_OPT_ARGS];
  24891. +
  24892. + return match_token(str, udbalevel, args);
  24893. +}
  24894. +
  24895. +const char *au_optstr_udba(int udba)
  24896. +{
  24897. + return au_parser_pattern(udba, udbalevel);
  24898. +}
  24899. +
  24900. +/* ---------------------------------------------------------------------- */
  24901. +
  24902. +static match_table_t au_wbr_create_policy = {
  24903. + {AuWbrCreate_TDP, "tdp"},
  24904. + {AuWbrCreate_TDP, "top-down-parent"},
  24905. + {AuWbrCreate_RR, "rr"},
  24906. + {AuWbrCreate_RR, "round-robin"},
  24907. + {AuWbrCreate_MFS, "mfs"},
  24908. + {AuWbrCreate_MFS, "most-free-space"},
  24909. + {AuWbrCreate_MFSV, "mfs:%d"},
  24910. + {AuWbrCreate_MFSV, "most-free-space:%d"},
  24911. +
  24912. + /* top-down regardless the parent, and then mfs */
  24913. + {AuWbrCreate_TDMFS, "tdmfs:%d"},
  24914. + {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
  24915. +
  24916. + {AuWbrCreate_MFSRR, "mfsrr:%d"},
  24917. + {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
  24918. + {AuWbrCreate_PMFS, "pmfs"},
  24919. + {AuWbrCreate_PMFSV, "pmfs:%d"},
  24920. + {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
  24921. + {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
  24922. +
  24923. + {-1, NULL}
  24924. +};
  24925. +
  24926. +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
  24927. + struct au_opt_wbr_create *create)
  24928. +{
  24929. + int err;
  24930. + unsigned long long ull;
  24931. +
  24932. + err = 0;
  24933. + if (!match_u64(arg, &ull))
  24934. + create->mfsrr_watermark = ull;
  24935. + else {
  24936. + pr_err("bad integer in %s\n", str);
  24937. + err = -EINVAL;
  24938. + }
  24939. +
  24940. + return err;
  24941. +}
  24942. +
  24943. +static int au_wbr_mfs_sec(substring_t *arg, char *str,
  24944. + struct au_opt_wbr_create *create)
  24945. +{
  24946. + int n, err;
  24947. +
  24948. + err = 0;
  24949. + if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
  24950. + create->mfs_second = n;
  24951. + else {
  24952. + pr_err("bad integer in %s\n", str);
  24953. + err = -EINVAL;
  24954. + }
  24955. +
  24956. + return err;
  24957. +}
  24958. +
  24959. +static int noinline_for_stack
  24960. +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
  24961. +{
  24962. + int err, e;
  24963. + substring_t args[MAX_OPT_ARGS];
  24964. +
  24965. + err = match_token(str, au_wbr_create_policy, args);
  24966. + create->wbr_create = err;
  24967. + switch (err) {
  24968. + case AuWbrCreate_MFSRRV:
  24969. + case AuWbrCreate_TDMFSV:
  24970. + case AuWbrCreate_PMFSRRV:
  24971. + e = au_wbr_mfs_wmark(&args[0], str, create);
  24972. + if (!e)
  24973. + e = au_wbr_mfs_sec(&args[1], str, create);
  24974. + if (unlikely(e))
  24975. + err = e;
  24976. + break;
  24977. + case AuWbrCreate_MFSRR:
  24978. + case AuWbrCreate_TDMFS:
  24979. + case AuWbrCreate_PMFSRR:
  24980. + e = au_wbr_mfs_wmark(&args[0], str, create);
  24981. + if (unlikely(e)) {
  24982. + err = e;
  24983. + break;
  24984. + }
  24985. + /*FALLTHROUGH*/
  24986. + case AuWbrCreate_MFS:
  24987. + case AuWbrCreate_PMFS:
  24988. + create->mfs_second = AUFS_MFS_DEF_SEC;
  24989. + break;
  24990. + case AuWbrCreate_MFSV:
  24991. + case AuWbrCreate_PMFSV:
  24992. + e = au_wbr_mfs_sec(&args[0], str, create);
  24993. + if (unlikely(e))
  24994. + err = e;
  24995. + break;
  24996. + }
  24997. +
  24998. + return err;
  24999. +}
  25000. +
  25001. +const char *au_optstr_wbr_create(int wbr_create)
  25002. +{
  25003. + return au_parser_pattern(wbr_create, au_wbr_create_policy);
  25004. +}
  25005. +
  25006. +static match_table_t au_wbr_copyup_policy = {
  25007. + {AuWbrCopyup_TDP, "tdp"},
  25008. + {AuWbrCopyup_TDP, "top-down-parent"},
  25009. + {AuWbrCopyup_BUP, "bup"},
  25010. + {AuWbrCopyup_BUP, "bottom-up-parent"},
  25011. + {AuWbrCopyup_BU, "bu"},
  25012. + {AuWbrCopyup_BU, "bottom-up"},
  25013. + {-1, NULL}
  25014. +};
  25015. +
  25016. +static int noinline_for_stack au_wbr_copyup_val(char *str)
  25017. +{
  25018. + substring_t args[MAX_OPT_ARGS];
  25019. +
  25020. + return match_token(str, au_wbr_copyup_policy, args);
  25021. +}
  25022. +
  25023. +const char *au_optstr_wbr_copyup(int wbr_copyup)
  25024. +{
  25025. + return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
  25026. +}
  25027. +
  25028. +/* ---------------------------------------------------------------------- */
  25029. +
  25030. +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  25031. +
  25032. +static void dump_opts(struct au_opts *opts)
  25033. +{
  25034. +#ifdef CONFIG_AUFS_DEBUG
  25035. + /* reduce stack space */
  25036. + union {
  25037. + struct au_opt_add *add;
  25038. + struct au_opt_del *del;
  25039. + struct au_opt_mod *mod;
  25040. + struct au_opt_xino *xino;
  25041. + struct au_opt_xino_itrunc *xino_itrunc;
  25042. + struct au_opt_wbr_create *create;
  25043. + } u;
  25044. + struct au_opt *opt;
  25045. +
  25046. + opt = opts->opt;
  25047. + while (opt->type != Opt_tail) {
  25048. + switch (opt->type) {
  25049. + case Opt_add:
  25050. + u.add = &opt->add;
  25051. + AuDbg("add {b%d, %s, 0x%x, %p}\n",
  25052. + u.add->bindex, u.add->pathname, u.add->perm,
  25053. + u.add->path.dentry);
  25054. + break;
  25055. + case Opt_del:
  25056. + case Opt_idel:
  25057. + u.del = &opt->del;
  25058. + AuDbg("del {%s, %p}\n",
  25059. + u.del->pathname, u.del->h_path.dentry);
  25060. + break;
  25061. + case Opt_mod:
  25062. + case Opt_imod:
  25063. + u.mod = &opt->mod;
  25064. + AuDbg("mod {%s, 0x%x, %p}\n",
  25065. + u.mod->path, u.mod->perm, u.mod->h_root);
  25066. + break;
  25067. + case Opt_append:
  25068. + u.add = &opt->add;
  25069. + AuDbg("append {b%d, %s, 0x%x, %p}\n",
  25070. + u.add->bindex, u.add->pathname, u.add->perm,
  25071. + u.add->path.dentry);
  25072. + break;
  25073. + case Opt_prepend:
  25074. + u.add = &opt->add;
  25075. + AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
  25076. + u.add->bindex, u.add->pathname, u.add->perm,
  25077. + u.add->path.dentry);
  25078. + break;
  25079. + case Opt_dirwh:
  25080. + AuDbg("dirwh %d\n", opt->dirwh);
  25081. + break;
  25082. + case Opt_rdcache:
  25083. + AuDbg("rdcache %d\n", opt->rdcache);
  25084. + break;
  25085. + case Opt_rdblk:
  25086. + AuDbg("rdblk %u\n", opt->rdblk);
  25087. + break;
  25088. + case Opt_rdblk_def:
  25089. + AuDbg("rdblk_def\n");
  25090. + break;
  25091. + case Opt_rdhash:
  25092. + AuDbg("rdhash %u\n", opt->rdhash);
  25093. + break;
  25094. + case Opt_rdhash_def:
  25095. + AuDbg("rdhash_def\n");
  25096. + break;
  25097. + case Opt_xino:
  25098. + u.xino = &opt->xino;
  25099. + AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
  25100. + break;
  25101. + case Opt_trunc_xino:
  25102. + AuLabel(trunc_xino);
  25103. + break;
  25104. + case Opt_notrunc_xino:
  25105. + AuLabel(notrunc_xino);
  25106. + break;
  25107. + case Opt_trunc_xino_path:
  25108. + case Opt_itrunc_xino:
  25109. + u.xino_itrunc = &opt->xino_itrunc;
  25110. + AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
  25111. + break;
  25112. + case Opt_noxino:
  25113. + AuLabel(noxino);
  25114. + break;
  25115. + case Opt_trunc_xib:
  25116. + AuLabel(trunc_xib);
  25117. + break;
  25118. + case Opt_notrunc_xib:
  25119. + AuLabel(notrunc_xib);
  25120. + break;
  25121. + case Opt_shwh:
  25122. + AuLabel(shwh);
  25123. + break;
  25124. + case Opt_noshwh:
  25125. + AuLabel(noshwh);
  25126. + break;
  25127. + case Opt_dirperm1:
  25128. + AuLabel(dirperm1);
  25129. + break;
  25130. + case Opt_nodirperm1:
  25131. + AuLabel(nodirperm1);
  25132. + break;
  25133. + case Opt_plink:
  25134. + AuLabel(plink);
  25135. + break;
  25136. + case Opt_noplink:
  25137. + AuLabel(noplink);
  25138. + break;
  25139. + case Opt_list_plink:
  25140. + AuLabel(list_plink);
  25141. + break;
  25142. + case Opt_udba:
  25143. + AuDbg("udba %d, %s\n",
  25144. + opt->udba, au_optstr_udba(opt->udba));
  25145. + break;
  25146. + case Opt_dio:
  25147. + AuLabel(dio);
  25148. + break;
  25149. + case Opt_nodio:
  25150. + AuLabel(nodio);
  25151. + break;
  25152. + case Opt_diropq_a:
  25153. + AuLabel(diropq_a);
  25154. + break;
  25155. + case Opt_diropq_w:
  25156. + AuLabel(diropq_w);
  25157. + break;
  25158. + case Opt_warn_perm:
  25159. + AuLabel(warn_perm);
  25160. + break;
  25161. + case Opt_nowarn_perm:
  25162. + AuLabel(nowarn_perm);
  25163. + break;
  25164. + case Opt_verbose:
  25165. + AuLabel(verbose);
  25166. + break;
  25167. + case Opt_noverbose:
  25168. + AuLabel(noverbose);
  25169. + break;
  25170. + case Opt_sum:
  25171. + AuLabel(sum);
  25172. + break;
  25173. + case Opt_nosum:
  25174. + AuLabel(nosum);
  25175. + break;
  25176. + case Opt_wsum:
  25177. + AuLabel(wsum);
  25178. + break;
  25179. + case Opt_wbr_create:
  25180. + u.create = &opt->wbr_create;
  25181. + AuDbg("create %d, %s\n", u.create->wbr_create,
  25182. + au_optstr_wbr_create(u.create->wbr_create));
  25183. + switch (u.create->wbr_create) {
  25184. + case AuWbrCreate_MFSV:
  25185. + case AuWbrCreate_PMFSV:
  25186. + AuDbg("%d sec\n", u.create->mfs_second);
  25187. + break;
  25188. + case AuWbrCreate_MFSRR:
  25189. + case AuWbrCreate_TDMFS:
  25190. + AuDbg("%llu watermark\n",
  25191. + u.create->mfsrr_watermark);
  25192. + break;
  25193. + case AuWbrCreate_MFSRRV:
  25194. + case AuWbrCreate_TDMFSV:
  25195. + case AuWbrCreate_PMFSRRV:
  25196. + AuDbg("%llu watermark, %d sec\n",
  25197. + u.create->mfsrr_watermark,
  25198. + u.create->mfs_second);
  25199. + break;
  25200. + }
  25201. + break;
  25202. + case Opt_wbr_copyup:
  25203. + AuDbg("copyup %d, %s\n", opt->wbr_copyup,
  25204. + au_optstr_wbr_copyup(opt->wbr_copyup));
  25205. + break;
  25206. + case Opt_fhsm_sec:
  25207. + AuDbg("fhsm_sec %u\n", opt->fhsm_second);
  25208. + break;
  25209. + case Opt_dirren:
  25210. + AuLabel(dirren);
  25211. + break;
  25212. + case Opt_nodirren:
  25213. + AuLabel(nodirren);
  25214. + break;
  25215. + case Opt_acl:
  25216. + AuLabel(acl);
  25217. + break;
  25218. + case Opt_noacl:
  25219. + AuLabel(noacl);
  25220. + break;
  25221. + default:
  25222. + BUG();
  25223. + }
  25224. + opt++;
  25225. + }
  25226. +#endif
  25227. +}
  25228. +
  25229. +void au_opts_free(struct au_opts *opts)
  25230. +{
  25231. + struct au_opt *opt;
  25232. +
  25233. + opt = opts->opt;
  25234. + while (opt->type != Opt_tail) {
  25235. + switch (opt->type) {
  25236. + case Opt_add:
  25237. + case Opt_append:
  25238. + case Opt_prepend:
  25239. + path_put(&opt->add.path);
  25240. + break;
  25241. + case Opt_del:
  25242. + case Opt_idel:
  25243. + path_put(&opt->del.h_path);
  25244. + break;
  25245. + case Opt_mod:
  25246. + case Opt_imod:
  25247. + dput(opt->mod.h_root);
  25248. + break;
  25249. + case Opt_xino:
  25250. + fput(opt->xino.file);
  25251. + break;
  25252. + }
  25253. + opt++;
  25254. + }
  25255. +}
  25256. +
  25257. +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
  25258. + aufs_bindex_t bindex)
  25259. +{
  25260. + int err;
  25261. + struct au_opt_add *add = &opt->add;
  25262. + char *p;
  25263. +
  25264. + add->bindex = bindex;
  25265. + add->perm = AuBrPerm_RO;
  25266. + add->pathname = opt_str;
  25267. + p = strchr(opt_str, '=');
  25268. + if (p) {
  25269. + *p++ = 0;
  25270. + if (*p)
  25271. + add->perm = br_perm_val(p);
  25272. + }
  25273. +
  25274. + err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
  25275. + if (!err) {
  25276. + if (!p) {
  25277. + add->perm = AuBrPerm_RO;
  25278. + if (au_test_fs_rr(add->path.dentry->d_sb))
  25279. + add->perm = AuBrPerm_RR;
  25280. + else if (!bindex && !(sb_flags & SB_RDONLY))
  25281. + add->perm = AuBrPerm_RW;
  25282. + }
  25283. + opt->type = Opt_add;
  25284. + goto out;
  25285. + }
  25286. + pr_err("lookup failed %s (%d)\n", add->pathname, err);
  25287. + err = -EINVAL;
  25288. +
  25289. +out:
  25290. + return err;
  25291. +}
  25292. +
  25293. +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
  25294. +{
  25295. + int err;
  25296. +
  25297. + del->pathname = args[0].from;
  25298. + AuDbg("del path %s\n", del->pathname);
  25299. +
  25300. + err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
  25301. + if (unlikely(err))
  25302. + pr_err("lookup failed %s (%d)\n", del->pathname, err);
  25303. +
  25304. + return err;
  25305. +}
  25306. +
  25307. +#if 0 /* reserved for future use */
  25308. +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
  25309. + struct au_opt_del *del, substring_t args[])
  25310. +{
  25311. + int err;
  25312. + struct dentry *root;
  25313. +
  25314. + err = -EINVAL;
  25315. + root = sb->s_root;
  25316. + aufs_read_lock(root, AuLock_FLUSH);
  25317. + if (bindex < 0 || au_sbbot(sb) < bindex) {
  25318. + pr_err("out of bounds, %d\n", bindex);
  25319. + goto out;
  25320. + }
  25321. +
  25322. + err = 0;
  25323. + del->h_path.dentry = dget(au_h_dptr(root, bindex));
  25324. + del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
  25325. +
  25326. +out:
  25327. + aufs_read_unlock(root, !AuLock_IR);
  25328. + return err;
  25329. +}
  25330. +#endif
  25331. +
  25332. +static int noinline_for_stack
  25333. +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
  25334. +{
  25335. + int err;
  25336. + struct path path;
  25337. + char *p;
  25338. +
  25339. + err = -EINVAL;
  25340. + mod->path = args[0].from;
  25341. + p = strchr(mod->path, '=');
  25342. + if (unlikely(!p)) {
  25343. + pr_err("no permission %s\n", args[0].from);
  25344. + goto out;
  25345. + }
  25346. +
  25347. + *p++ = 0;
  25348. + err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
  25349. + if (unlikely(err)) {
  25350. + pr_err("lookup failed %s (%d)\n", mod->path, err);
  25351. + goto out;
  25352. + }
  25353. +
  25354. + mod->perm = br_perm_val(p);
  25355. + AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
  25356. + mod->h_root = dget(path.dentry);
  25357. + path_put(&path);
  25358. +
  25359. +out:
  25360. + return err;
  25361. +}
  25362. +
  25363. +#if 0 /* reserved for future use */
  25364. +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
  25365. + struct au_opt_mod *mod, substring_t args[])
  25366. +{
  25367. + int err;
  25368. + struct dentry *root;
  25369. +
  25370. + err = -EINVAL;
  25371. + root = sb->s_root;
  25372. + aufs_read_lock(root, AuLock_FLUSH);
  25373. + if (bindex < 0 || au_sbbot(sb) < bindex) {
  25374. + pr_err("out of bounds, %d\n", bindex);
  25375. + goto out;
  25376. + }
  25377. +
  25378. + err = 0;
  25379. + mod->perm = br_perm_val(args[1].from);
  25380. + AuDbg("mod path %s, perm 0x%x, %s\n",
  25381. + mod->path, mod->perm, args[1].from);
  25382. + mod->h_root = dget(au_h_dptr(root, bindex));
  25383. +
  25384. +out:
  25385. + aufs_read_unlock(root, !AuLock_IR);
  25386. + return err;
  25387. +}
  25388. +#endif
  25389. +
  25390. +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
  25391. + substring_t args[])
  25392. +{
  25393. + int err;
  25394. + struct file *file;
  25395. +
  25396. + file = au_xino_create(sb, args[0].from, /*silent*/0);
  25397. + err = PTR_ERR(file);
  25398. + if (IS_ERR(file))
  25399. + goto out;
  25400. +
  25401. + err = -EINVAL;
  25402. + if (unlikely(file->f_path.dentry->d_sb == sb)) {
  25403. + fput(file);
  25404. + pr_err("%s must be outside\n", args[0].from);
  25405. + goto out;
  25406. + }
  25407. +
  25408. + err = 0;
  25409. + xino->file = file;
  25410. + xino->path = args[0].from;
  25411. +
  25412. +out:
  25413. + return err;
  25414. +}
  25415. +
  25416. +static int noinline_for_stack
  25417. +au_opts_parse_xino_itrunc_path(struct super_block *sb,
  25418. + struct au_opt_xino_itrunc *xino_itrunc,
  25419. + substring_t args[])
  25420. +{
  25421. + int err;
  25422. + aufs_bindex_t bbot, bindex;
  25423. + struct path path;
  25424. + struct dentry *root;
  25425. +
  25426. + err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
  25427. + if (unlikely(err)) {
  25428. + pr_err("lookup failed %s (%d)\n", args[0].from, err);
  25429. + goto out;
  25430. + }
  25431. +
  25432. + xino_itrunc->bindex = -1;
  25433. + root = sb->s_root;
  25434. + aufs_read_lock(root, AuLock_FLUSH);
  25435. + bbot = au_sbbot(sb);
  25436. + for (bindex = 0; bindex <= bbot; bindex++) {
  25437. + if (au_h_dptr(root, bindex) == path.dentry) {
  25438. + xino_itrunc->bindex = bindex;
  25439. + break;
  25440. + }
  25441. + }
  25442. + aufs_read_unlock(root, !AuLock_IR);
  25443. + path_put(&path);
  25444. +
  25445. + if (unlikely(xino_itrunc->bindex < 0)) {
  25446. + pr_err("no such branch %s\n", args[0].from);
  25447. + err = -EINVAL;
  25448. + }
  25449. +
  25450. +out:
  25451. + return err;
  25452. +}
  25453. +
  25454. +/* called without aufs lock */
  25455. +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
  25456. +{
  25457. + int err, n, token;
  25458. + aufs_bindex_t bindex;
  25459. + unsigned char skipped;
  25460. + struct dentry *root;
  25461. + struct au_opt *opt, *opt_tail;
  25462. + char *opt_str;
  25463. + /* reduce the stack space */
  25464. + union {
  25465. + struct au_opt_xino_itrunc *xino_itrunc;
  25466. + struct au_opt_wbr_create *create;
  25467. + } u;
  25468. + struct {
  25469. + substring_t args[MAX_OPT_ARGS];
  25470. + } *a;
  25471. +
  25472. + err = -ENOMEM;
  25473. + a = kmalloc(sizeof(*a), GFP_NOFS);
  25474. + if (unlikely(!a))
  25475. + goto out;
  25476. +
  25477. + root = sb->s_root;
  25478. + err = 0;
  25479. + bindex = 0;
  25480. + opt = opts->opt;
  25481. + opt_tail = opt + opts->max_opt - 1;
  25482. + opt->type = Opt_tail;
  25483. + while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
  25484. + err = -EINVAL;
  25485. + skipped = 0;
  25486. + token = match_token(opt_str, options, a->args);
  25487. + switch (token) {
  25488. + case Opt_br:
  25489. + err = 0;
  25490. + while (!err && (opt_str = strsep(&a->args[0].from, ":"))
  25491. + && *opt_str) {
  25492. + err = opt_add(opt, opt_str, opts->sb_flags,
  25493. + bindex++);
  25494. + if (unlikely(!err && ++opt > opt_tail)) {
  25495. + err = -E2BIG;
  25496. + break;
  25497. + }
  25498. + opt->type = Opt_tail;
  25499. + skipped = 1;
  25500. + }
  25501. + break;
  25502. + case Opt_add:
  25503. + if (unlikely(match_int(&a->args[0], &n))) {
  25504. + pr_err("bad integer in %s\n", opt_str);
  25505. + break;
  25506. + }
  25507. + bindex = n;
  25508. + err = opt_add(opt, a->args[1].from, opts->sb_flags,
  25509. + bindex);
  25510. + if (!err)
  25511. + opt->type = token;
  25512. + break;
  25513. + case Opt_append:
  25514. + err = opt_add(opt, a->args[0].from, opts->sb_flags,
  25515. + /*dummy bindex*/1);
  25516. + if (!err)
  25517. + opt->type = token;
  25518. + break;
  25519. + case Opt_prepend:
  25520. + err = opt_add(opt, a->args[0].from, opts->sb_flags,
  25521. + /*bindex*/0);
  25522. + if (!err)
  25523. + opt->type = token;
  25524. + break;
  25525. + case Opt_del:
  25526. + err = au_opts_parse_del(&opt->del, a->args);
  25527. + if (!err)
  25528. + opt->type = token;
  25529. + break;
  25530. +#if 0 /* reserved for future use */
  25531. + case Opt_idel:
  25532. + del->pathname = "(indexed)";
  25533. + if (unlikely(match_int(&args[0], &n))) {
  25534. + pr_err("bad integer in %s\n", opt_str);
  25535. + break;
  25536. + }
  25537. + err = au_opts_parse_idel(sb, n, &opt->del, a->args);
  25538. + if (!err)
  25539. + opt->type = token;
  25540. + break;
  25541. +#endif
  25542. + case Opt_mod:
  25543. + err = au_opts_parse_mod(&opt->mod, a->args);
  25544. + if (!err)
  25545. + opt->type = token;
  25546. + break;
  25547. +#ifdef IMOD /* reserved for future use */
  25548. + case Opt_imod:
  25549. + u.mod->path = "(indexed)";
  25550. + if (unlikely(match_int(&a->args[0], &n))) {
  25551. + pr_err("bad integer in %s\n", opt_str);
  25552. + break;
  25553. + }
  25554. + err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
  25555. + if (!err)
  25556. + opt->type = token;
  25557. + break;
  25558. +#endif
  25559. + case Opt_xino:
  25560. + err = au_opts_parse_xino(sb, &opt->xino, a->args);
  25561. + if (!err)
  25562. + opt->type = token;
  25563. + break;
  25564. +
  25565. + case Opt_trunc_xino_path:
  25566. + err = au_opts_parse_xino_itrunc_path
  25567. + (sb, &opt->xino_itrunc, a->args);
  25568. + if (!err)
  25569. + opt->type = token;
  25570. + break;
  25571. +
  25572. + case Opt_itrunc_xino:
  25573. + u.xino_itrunc = &opt->xino_itrunc;
  25574. + if (unlikely(match_int(&a->args[0], &n))) {
  25575. + pr_err("bad integer in %s\n", opt_str);
  25576. + break;
  25577. + }
  25578. + u.xino_itrunc->bindex = n;
  25579. + aufs_read_lock(root, AuLock_FLUSH);
  25580. + if (n < 0 || au_sbbot(sb) < n) {
  25581. + pr_err("out of bounds, %d\n", n);
  25582. + aufs_read_unlock(root, !AuLock_IR);
  25583. + break;
  25584. + }
  25585. + aufs_read_unlock(root, !AuLock_IR);
  25586. + err = 0;
  25587. + opt->type = token;
  25588. + break;
  25589. +
  25590. + case Opt_dirwh:
  25591. + if (unlikely(match_int(&a->args[0], &opt->dirwh)))
  25592. + break;
  25593. + err = 0;
  25594. + opt->type = token;
  25595. + break;
  25596. +
  25597. + case Opt_rdcache:
  25598. + if (unlikely(match_int(&a->args[0], &n))) {
  25599. + pr_err("bad integer in %s\n", opt_str);
  25600. + break;
  25601. + }
  25602. + if (unlikely(n > AUFS_RDCACHE_MAX)) {
  25603. + pr_err("rdcache must be smaller than %d\n",
  25604. + AUFS_RDCACHE_MAX);
  25605. + break;
  25606. + }
  25607. + opt->rdcache = n;
  25608. + err = 0;
  25609. + opt->type = token;
  25610. + break;
  25611. + case Opt_rdblk:
  25612. + if (unlikely(match_int(&a->args[0], &n)
  25613. + || n < 0
  25614. + || n > KMALLOC_MAX_SIZE)) {
  25615. + pr_err("bad integer in %s\n", opt_str);
  25616. + break;
  25617. + }
  25618. + if (unlikely(n && n < NAME_MAX)) {
  25619. + pr_err("rdblk must be larger than %d\n",
  25620. + NAME_MAX);
  25621. + break;
  25622. + }
  25623. + opt->rdblk = n;
  25624. + err = 0;
  25625. + opt->type = token;
  25626. + break;
  25627. + case Opt_rdhash:
  25628. + if (unlikely(match_int(&a->args[0], &n)
  25629. + || n < 0
  25630. + || n * sizeof(struct hlist_head)
  25631. + > KMALLOC_MAX_SIZE)) {
  25632. + pr_err("bad integer in %s\n", opt_str);
  25633. + break;
  25634. + }
  25635. + opt->rdhash = n;
  25636. + err = 0;
  25637. + opt->type = token;
  25638. + break;
  25639. +
  25640. + case Opt_trunc_xino:
  25641. + case Opt_notrunc_xino:
  25642. + case Opt_noxino:
  25643. + case Opt_trunc_xib:
  25644. + case Opt_notrunc_xib:
  25645. + case Opt_shwh:
  25646. + case Opt_noshwh:
  25647. + case Opt_dirperm1:
  25648. + case Opt_nodirperm1:
  25649. + case Opt_plink:
  25650. + case Opt_noplink:
  25651. + case Opt_list_plink:
  25652. + case Opt_dio:
  25653. + case Opt_nodio:
  25654. + case Opt_diropq_a:
  25655. + case Opt_diropq_w:
  25656. + case Opt_warn_perm:
  25657. + case Opt_nowarn_perm:
  25658. + case Opt_verbose:
  25659. + case Opt_noverbose:
  25660. + case Opt_sum:
  25661. + case Opt_nosum:
  25662. + case Opt_wsum:
  25663. + case Opt_rdblk_def:
  25664. + case Opt_rdhash_def:
  25665. + case Opt_dirren:
  25666. + case Opt_nodirren:
  25667. + case Opt_acl:
  25668. + case Opt_noacl:
  25669. + err = 0;
  25670. + opt->type = token;
  25671. + break;
  25672. +
  25673. + case Opt_udba:
  25674. + opt->udba = udba_val(a->args[0].from);
  25675. + if (opt->udba >= 0) {
  25676. + err = 0;
  25677. + opt->type = token;
  25678. + } else
  25679. + pr_err("wrong value, %s\n", opt_str);
  25680. + break;
  25681. +
  25682. + case Opt_wbr_create:
  25683. + u.create = &opt->wbr_create;
  25684. + u.create->wbr_create
  25685. + = au_wbr_create_val(a->args[0].from, u.create);
  25686. + if (u.create->wbr_create >= 0) {
  25687. + err = 0;
  25688. + opt->type = token;
  25689. + } else
  25690. + pr_err("wrong value, %s\n", opt_str);
  25691. + break;
  25692. + case Opt_wbr_copyup:
  25693. + opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
  25694. + if (opt->wbr_copyup >= 0) {
  25695. + err = 0;
  25696. + opt->type = token;
  25697. + } else
  25698. + pr_err("wrong value, %s\n", opt_str);
  25699. + break;
  25700. +
  25701. + case Opt_fhsm_sec:
  25702. + if (unlikely(match_int(&a->args[0], &n)
  25703. + || n < 0)) {
  25704. + pr_err("bad integer in %s\n", opt_str);
  25705. + break;
  25706. + }
  25707. + if (sysaufs_brs) {
  25708. + opt->fhsm_second = n;
  25709. + opt->type = token;
  25710. + } else
  25711. + pr_warn("ignored %s\n", opt_str);
  25712. + err = 0;
  25713. + break;
  25714. +
  25715. + case Opt_ignore:
  25716. + pr_warn("ignored %s\n", opt_str);
  25717. + /*FALLTHROUGH*/
  25718. + case Opt_ignore_silent:
  25719. + skipped = 1;
  25720. + err = 0;
  25721. + break;
  25722. + case Opt_err:
  25723. + pr_err("unknown option %s\n", opt_str);
  25724. + break;
  25725. + }
  25726. +
  25727. + if (!err && !skipped) {
  25728. + if (unlikely(++opt > opt_tail)) {
  25729. + err = -E2BIG;
  25730. + opt--;
  25731. + opt->type = Opt_tail;
  25732. + break;
  25733. + }
  25734. + opt->type = Opt_tail;
  25735. + }
  25736. + }
  25737. +
  25738. + kfree(a);
  25739. + dump_opts(opts);
  25740. + if (unlikely(err))
  25741. + au_opts_free(opts);
  25742. +
  25743. +out:
  25744. + return err;
  25745. +}
  25746. +
  25747. +static int au_opt_wbr_create(struct super_block *sb,
  25748. + struct au_opt_wbr_create *create)
  25749. +{
  25750. + int err;
  25751. + struct au_sbinfo *sbinfo;
  25752. +
  25753. + SiMustWriteLock(sb);
  25754. +
  25755. + err = 1; /* handled */
  25756. + sbinfo = au_sbi(sb);
  25757. + if (sbinfo->si_wbr_create_ops->fin) {
  25758. + err = sbinfo->si_wbr_create_ops->fin(sb);
  25759. + if (!err)
  25760. + err = 1;
  25761. + }
  25762. +
  25763. + sbinfo->si_wbr_create = create->wbr_create;
  25764. + sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
  25765. + switch (create->wbr_create) {
  25766. + case AuWbrCreate_MFSRRV:
  25767. + case AuWbrCreate_MFSRR:
  25768. + case AuWbrCreate_TDMFS:
  25769. + case AuWbrCreate_TDMFSV:
  25770. + case AuWbrCreate_PMFSRR:
  25771. + case AuWbrCreate_PMFSRRV:
  25772. + sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
  25773. + /*FALLTHROUGH*/
  25774. + case AuWbrCreate_MFS:
  25775. + case AuWbrCreate_MFSV:
  25776. + case AuWbrCreate_PMFS:
  25777. + case AuWbrCreate_PMFSV:
  25778. + sbinfo->si_wbr_mfs.mfs_expire
  25779. + = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
  25780. + break;
  25781. + }
  25782. +
  25783. + if (sbinfo->si_wbr_create_ops->init)
  25784. + sbinfo->si_wbr_create_ops->init(sb); /* ignore */
  25785. +
  25786. + return err;
  25787. +}
  25788. +
  25789. +/*
  25790. + * returns,
  25791. + * plus: processed without an error
  25792. + * zero: unprocessed
  25793. + */
  25794. +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
  25795. + struct au_opts *opts)
  25796. +{
  25797. + int err;
  25798. + struct au_sbinfo *sbinfo;
  25799. +
  25800. + SiMustWriteLock(sb);
  25801. +
  25802. + err = 1; /* handled */
  25803. + sbinfo = au_sbi(sb);
  25804. + switch (opt->type) {
  25805. + case Opt_udba:
  25806. + sbinfo->si_mntflags &= ~AuOptMask_UDBA;
  25807. + sbinfo->si_mntflags |= opt->udba;
  25808. + opts->given_udba |= opt->udba;
  25809. + break;
  25810. +
  25811. + case Opt_plink:
  25812. + au_opt_set(sbinfo->si_mntflags, PLINK);
  25813. + break;
  25814. + case Opt_noplink:
  25815. + if (au_opt_test(sbinfo->si_mntflags, PLINK))
  25816. + au_plink_put(sb, /*verbose*/1);
  25817. + au_opt_clr(sbinfo->si_mntflags, PLINK);
  25818. + break;
  25819. + case Opt_list_plink:
  25820. + if (au_opt_test(sbinfo->si_mntflags, PLINK))
  25821. + au_plink_list(sb);
  25822. + break;
  25823. +
  25824. + case Opt_dio:
  25825. + au_opt_set(sbinfo->si_mntflags, DIO);
  25826. + au_fset_opts(opts->flags, REFRESH_DYAOP);
  25827. + break;
  25828. + case Opt_nodio:
  25829. + au_opt_clr(sbinfo->si_mntflags, DIO);
  25830. + au_fset_opts(opts->flags, REFRESH_DYAOP);
  25831. + break;
  25832. +
  25833. + case Opt_fhsm_sec:
  25834. + au_fhsm_set(sbinfo, opt->fhsm_second);
  25835. + break;
  25836. +
  25837. + case Opt_diropq_a:
  25838. + au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
  25839. + break;
  25840. + case Opt_diropq_w:
  25841. + au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
  25842. + break;
  25843. +
  25844. + case Opt_warn_perm:
  25845. + au_opt_set(sbinfo->si_mntflags, WARN_PERM);
  25846. + break;
  25847. + case Opt_nowarn_perm:
  25848. + au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
  25849. + break;
  25850. +
  25851. + case Opt_verbose:
  25852. + au_opt_set(sbinfo->si_mntflags, VERBOSE);
  25853. + break;
  25854. + case Opt_noverbose:
  25855. + au_opt_clr(sbinfo->si_mntflags, VERBOSE);
  25856. + break;
  25857. +
  25858. + case Opt_sum:
  25859. + au_opt_set(sbinfo->si_mntflags, SUM);
  25860. + break;
  25861. + case Opt_wsum:
  25862. + au_opt_clr(sbinfo->si_mntflags, SUM);
  25863. + au_opt_set(sbinfo->si_mntflags, SUM_W);
  25864. + case Opt_nosum:
  25865. + au_opt_clr(sbinfo->si_mntflags, SUM);
  25866. + au_opt_clr(sbinfo->si_mntflags, SUM_W);
  25867. + break;
  25868. +
  25869. + case Opt_wbr_create:
  25870. + err = au_opt_wbr_create(sb, &opt->wbr_create);
  25871. + break;
  25872. + case Opt_wbr_copyup:
  25873. + sbinfo->si_wbr_copyup = opt->wbr_copyup;
  25874. + sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
  25875. + break;
  25876. +
  25877. + case Opt_dirwh:
  25878. + sbinfo->si_dirwh = opt->dirwh;
  25879. + break;
  25880. +
  25881. + case Opt_rdcache:
  25882. + sbinfo->si_rdcache
  25883. + = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
  25884. + break;
  25885. + case Opt_rdblk:
  25886. + sbinfo->si_rdblk = opt->rdblk;
  25887. + break;
  25888. + case Opt_rdblk_def:
  25889. + sbinfo->si_rdblk = AUFS_RDBLK_DEF;
  25890. + break;
  25891. + case Opt_rdhash:
  25892. + sbinfo->si_rdhash = opt->rdhash;
  25893. + break;
  25894. + case Opt_rdhash_def:
  25895. + sbinfo->si_rdhash = AUFS_RDHASH_DEF;
  25896. + break;
  25897. +
  25898. + case Opt_shwh:
  25899. + au_opt_set(sbinfo->si_mntflags, SHWH);
  25900. + break;
  25901. + case Opt_noshwh:
  25902. + au_opt_clr(sbinfo->si_mntflags, SHWH);
  25903. + break;
  25904. +
  25905. + case Opt_dirperm1:
  25906. + au_opt_set(sbinfo->si_mntflags, DIRPERM1);
  25907. + break;
  25908. + case Opt_nodirperm1:
  25909. + au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
  25910. + break;
  25911. +
  25912. + case Opt_trunc_xino:
  25913. + au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
  25914. + break;
  25915. + case Opt_notrunc_xino:
  25916. + au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
  25917. + break;
  25918. +
  25919. + case Opt_trunc_xino_path:
  25920. + case Opt_itrunc_xino:
  25921. + err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
  25922. + /*idx_begin*/0);
  25923. + if (!err)
  25924. + err = 1;
  25925. + break;
  25926. +
  25927. + case Opt_trunc_xib:
  25928. + au_fset_opts(opts->flags, TRUNC_XIB);
  25929. + break;
  25930. + case Opt_notrunc_xib:
  25931. + au_fclr_opts(opts->flags, TRUNC_XIB);
  25932. + break;
  25933. +
  25934. + case Opt_dirren:
  25935. + err = 1;
  25936. + if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
  25937. + err = au_dr_opt_set(sb);
  25938. + if (!err)
  25939. + err = 1;
  25940. + }
  25941. + if (err == 1)
  25942. + au_opt_set(sbinfo->si_mntflags, DIRREN);
  25943. + break;
  25944. + case Opt_nodirren:
  25945. + err = 1;
  25946. + if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
  25947. + err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
  25948. + DR_FLUSHED));
  25949. + if (!err)
  25950. + err = 1;
  25951. + }
  25952. + if (err == 1)
  25953. + au_opt_clr(sbinfo->si_mntflags, DIRREN);
  25954. + break;
  25955. +
  25956. + case Opt_acl:
  25957. + sb->s_flags |= SB_POSIXACL;
  25958. + break;
  25959. + case Opt_noacl:
  25960. + sb->s_flags &= ~SB_POSIXACL;
  25961. + break;
  25962. +
  25963. + default:
  25964. + err = 0;
  25965. + break;
  25966. + }
  25967. +
  25968. + return err;
  25969. +}
  25970. +
  25971. +/*
  25972. + * returns tri-state.
  25973. + * plus: processed without an error
  25974. + * zero: unprocessed
  25975. + * minus: error
  25976. + */
  25977. +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
  25978. + struct au_opts *opts)
  25979. +{
  25980. + int err, do_refresh;
  25981. +
  25982. + err = 0;
  25983. + switch (opt->type) {
  25984. + case Opt_append:
  25985. + opt->add.bindex = au_sbbot(sb) + 1;
  25986. + if (opt->add.bindex < 0)
  25987. + opt->add.bindex = 0;
  25988. + goto add;
  25989. + case Opt_prepend:
  25990. + opt->add.bindex = 0;
  25991. + add: /* indented label */
  25992. + case Opt_add:
  25993. + err = au_br_add(sb, &opt->add,
  25994. + au_ftest_opts(opts->flags, REMOUNT));
  25995. + if (!err) {
  25996. + err = 1;
  25997. + au_fset_opts(opts->flags, REFRESH);
  25998. + }
  25999. + break;
  26000. +
  26001. + case Opt_del:
  26002. + case Opt_idel:
  26003. + err = au_br_del(sb, &opt->del,
  26004. + au_ftest_opts(opts->flags, REMOUNT));
  26005. + if (!err) {
  26006. + err = 1;
  26007. + au_fset_opts(opts->flags, TRUNC_XIB);
  26008. + au_fset_opts(opts->flags, REFRESH);
  26009. + }
  26010. + break;
  26011. +
  26012. + case Opt_mod:
  26013. + case Opt_imod:
  26014. + err = au_br_mod(sb, &opt->mod,
  26015. + au_ftest_opts(opts->flags, REMOUNT),
  26016. + &do_refresh);
  26017. + if (!err) {
  26018. + err = 1;
  26019. + if (do_refresh)
  26020. + au_fset_opts(opts->flags, REFRESH);
  26021. + }
  26022. + break;
  26023. + }
  26024. + return err;
  26025. +}
  26026. +
  26027. +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
  26028. + struct au_opt_xino **opt_xino,
  26029. + struct au_opts *opts)
  26030. +{
  26031. + int err;
  26032. +
  26033. + err = 0;
  26034. + switch (opt->type) {
  26035. + case Opt_xino:
  26036. + err = au_xino_set(sb, &opt->xino,
  26037. + !!au_ftest_opts(opts->flags, REMOUNT));
  26038. + if (unlikely(err))
  26039. + break;
  26040. +
  26041. + *opt_xino = &opt->xino;
  26042. + break;
  26043. +
  26044. + case Opt_noxino:
  26045. + au_xino_clr(sb);
  26046. + *opt_xino = (void *)-1;
  26047. + break;
  26048. + }
  26049. +
  26050. + return err;
  26051. +}
  26052. +
  26053. +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
  26054. + unsigned int pending)
  26055. +{
  26056. + int err, fhsm;
  26057. + aufs_bindex_t bindex, bbot;
  26058. + unsigned char do_plink, skip, do_free, can_no_dreval;
  26059. + struct au_branch *br;
  26060. + struct au_wbr *wbr;
  26061. + struct dentry *root, *dentry;
  26062. + struct inode *dir, *h_dir;
  26063. + struct au_sbinfo *sbinfo;
  26064. + struct au_hinode *hdir;
  26065. +
  26066. + SiMustAnyLock(sb);
  26067. +
  26068. + sbinfo = au_sbi(sb);
  26069. + AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
  26070. +
  26071. + if (!(sb_flags & SB_RDONLY)) {
  26072. + if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
  26073. + pr_warn("first branch should be rw\n");
  26074. + if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
  26075. + pr_warn_once("shwh should be used with ro\n");
  26076. + }
  26077. +
  26078. + if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
  26079. + && !au_opt_test(sbinfo->si_mntflags, XINO))
  26080. + pr_warn_once("udba=*notify requires xino\n");
  26081. +
  26082. + if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
  26083. + pr_warn_once("dirperm1 breaks the protection"
  26084. + " by the permission bits on the lower branch\n");
  26085. +
  26086. + err = 0;
  26087. + fhsm = 0;
  26088. + root = sb->s_root;
  26089. + dir = d_inode(root);
  26090. + do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
  26091. + can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
  26092. + UDBA_NONE);
  26093. + bbot = au_sbbot(sb);
  26094. + for (bindex = 0; !err && bindex <= bbot; bindex++) {
  26095. + skip = 0;
  26096. + h_dir = au_h_iptr(dir, bindex);
  26097. + br = au_sbr(sb, bindex);
  26098. +
  26099. + if ((br->br_perm & AuBrAttr_ICEX)
  26100. + && !h_dir->i_op->listxattr)
  26101. + br->br_perm &= ~AuBrAttr_ICEX;
  26102. +#if 0
  26103. + if ((br->br_perm & AuBrAttr_ICEX_SEC)
  26104. + && (au_br_sb(br)->s_flags & SB_NOSEC))
  26105. + br->br_perm &= ~AuBrAttr_ICEX_SEC;
  26106. +#endif
  26107. +
  26108. + do_free = 0;
  26109. + wbr = br->br_wbr;
  26110. + if (wbr)
  26111. + wbr_wh_read_lock(wbr);
  26112. +
  26113. + if (!au_br_writable(br->br_perm)) {
  26114. + do_free = !!wbr;
  26115. + skip = (!wbr
  26116. + || (!wbr->wbr_whbase
  26117. + && !wbr->wbr_plink
  26118. + && !wbr->wbr_orph));
  26119. + } else if (!au_br_wh_linkable(br->br_perm)) {
  26120. + /* skip = (!br->br_whbase && !br->br_orph); */
  26121. + skip = (!wbr || !wbr->wbr_whbase);
  26122. + if (skip && wbr) {
  26123. + if (do_plink)
  26124. + skip = !!wbr->wbr_plink;
  26125. + else
  26126. + skip = !wbr->wbr_plink;
  26127. + }
  26128. + } else {
  26129. + /* skip = (br->br_whbase && br->br_ohph); */
  26130. + skip = (wbr && wbr->wbr_whbase);
  26131. + if (skip) {
  26132. + if (do_plink)
  26133. + skip = !!wbr->wbr_plink;
  26134. + else
  26135. + skip = !wbr->wbr_plink;
  26136. + }
  26137. + }
  26138. + if (wbr)
  26139. + wbr_wh_read_unlock(wbr);
  26140. +
  26141. + if (can_no_dreval) {
  26142. + dentry = br->br_path.dentry;
  26143. + spin_lock(&dentry->d_lock);
  26144. + if (dentry->d_flags &
  26145. + (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
  26146. + can_no_dreval = 0;
  26147. + spin_unlock(&dentry->d_lock);
  26148. + }
  26149. +
  26150. + if (au_br_fhsm(br->br_perm)) {
  26151. + fhsm++;
  26152. + AuDebugOn(!br->br_fhsm);
  26153. + }
  26154. +
  26155. + if (skip)
  26156. + continue;
  26157. +
  26158. + hdir = au_hi(dir, bindex);
  26159. + au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
  26160. + if (wbr)
  26161. + wbr_wh_write_lock(wbr);
  26162. + err = au_wh_init(br, sb);
  26163. + if (wbr)
  26164. + wbr_wh_write_unlock(wbr);
  26165. + au_hn_inode_unlock(hdir);
  26166. +
  26167. + if (!err && do_free) {
  26168. + kfree(wbr);
  26169. + br->br_wbr = NULL;
  26170. + }
  26171. + }
  26172. +
  26173. + if (can_no_dreval)
  26174. + au_fset_si(sbinfo, NO_DREVAL);
  26175. + else
  26176. + au_fclr_si(sbinfo, NO_DREVAL);
  26177. +
  26178. + if (fhsm >= 2) {
  26179. + au_fset_si(sbinfo, FHSM);
  26180. + for (bindex = bbot; bindex >= 0; bindex--) {
  26181. + br = au_sbr(sb, bindex);
  26182. + if (au_br_fhsm(br->br_perm)) {
  26183. + au_fhsm_set_bottom(sb, bindex);
  26184. + break;
  26185. + }
  26186. + }
  26187. + } else {
  26188. + au_fclr_si(sbinfo, FHSM);
  26189. + au_fhsm_set_bottom(sb, -1);
  26190. + }
  26191. +
  26192. + return err;
  26193. +}
  26194. +
  26195. +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
  26196. +{
  26197. + int err;
  26198. + unsigned int tmp;
  26199. + aufs_bindex_t bindex, bbot;
  26200. + struct au_opt *opt;
  26201. + struct au_opt_xino *opt_xino, xino;
  26202. + struct au_sbinfo *sbinfo;
  26203. + struct au_branch *br;
  26204. + struct inode *dir;
  26205. +
  26206. + SiMustWriteLock(sb);
  26207. +
  26208. + err = 0;
  26209. + opt_xino = NULL;
  26210. + opt = opts->opt;
  26211. + while (err >= 0 && opt->type != Opt_tail)
  26212. + err = au_opt_simple(sb, opt++, opts);
  26213. + if (err > 0)
  26214. + err = 0;
  26215. + else if (unlikely(err < 0))
  26216. + goto out;
  26217. +
  26218. + /* disable xino and udba temporary */
  26219. + sbinfo = au_sbi(sb);
  26220. + tmp = sbinfo->si_mntflags;
  26221. + au_opt_clr(sbinfo->si_mntflags, XINO);
  26222. + au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
  26223. +
  26224. + opt = opts->opt;
  26225. + while (err >= 0 && opt->type != Opt_tail)
  26226. + err = au_opt_br(sb, opt++, opts);
  26227. + if (err > 0)
  26228. + err = 0;
  26229. + else if (unlikely(err < 0))
  26230. + goto out;
  26231. +
  26232. + bbot = au_sbbot(sb);
  26233. + if (unlikely(bbot < 0)) {
  26234. + err = -EINVAL;
  26235. + pr_err("no branches\n");
  26236. + goto out;
  26237. + }
  26238. +
  26239. + if (au_opt_test(tmp, XINO))
  26240. + au_opt_set(sbinfo->si_mntflags, XINO);
  26241. + opt = opts->opt;
  26242. + while (!err && opt->type != Opt_tail)
  26243. + err = au_opt_xino(sb, opt++, &opt_xino, opts);
  26244. + if (unlikely(err))
  26245. + goto out;
  26246. +
  26247. + err = au_opts_verify(sb, sb->s_flags, tmp);
  26248. + if (unlikely(err))
  26249. + goto out;
  26250. +
  26251. + /* restore xino */
  26252. + if (au_opt_test(tmp, XINO) && !opt_xino) {
  26253. + xino.file = au_xino_def(sb);
  26254. + err = PTR_ERR(xino.file);
  26255. + if (IS_ERR(xino.file))
  26256. + goto out;
  26257. +
  26258. + err = au_xino_set(sb, &xino, /*remount*/0);
  26259. + fput(xino.file);
  26260. + if (unlikely(err))
  26261. + goto out;
  26262. + }
  26263. +
  26264. + /* restore udba */
  26265. + tmp &= AuOptMask_UDBA;
  26266. + sbinfo->si_mntflags &= ~AuOptMask_UDBA;
  26267. + sbinfo->si_mntflags |= tmp;
  26268. + bbot = au_sbbot(sb);
  26269. + for (bindex = 0; bindex <= bbot; bindex++) {
  26270. + br = au_sbr(sb, bindex);
  26271. + err = au_hnotify_reset_br(tmp, br, br->br_perm);
  26272. + if (unlikely(err))
  26273. + AuIOErr("hnotify failed on br %d, %d, ignored\n",
  26274. + bindex, err);
  26275. + /* go on even if err */
  26276. + }
  26277. + if (au_opt_test(tmp, UDBA_HNOTIFY)) {
  26278. + dir = d_inode(sb->s_root);
  26279. + au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
  26280. + }
  26281. +
  26282. +out:
  26283. + return err;
  26284. +}
  26285. +
  26286. +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
  26287. +{
  26288. + int err, rerr;
  26289. + unsigned char no_dreval;
  26290. + struct inode *dir;
  26291. + struct au_opt_xino *opt_xino;
  26292. + struct au_opt *opt;
  26293. + struct au_sbinfo *sbinfo;
  26294. +
  26295. + SiMustWriteLock(sb);
  26296. +
  26297. + err = au_dr_opt_flush(sb);
  26298. + if (unlikely(err))
  26299. + goto out;
  26300. + au_fset_opts(opts->flags, DR_FLUSHED);
  26301. +
  26302. + dir = d_inode(sb->s_root);
  26303. + sbinfo = au_sbi(sb);
  26304. + opt_xino = NULL;
  26305. + opt = opts->opt;
  26306. + while (err >= 0 && opt->type != Opt_tail) {
  26307. + err = au_opt_simple(sb, opt, opts);
  26308. + if (!err)
  26309. + err = au_opt_br(sb, opt, opts);
  26310. + if (!err)
  26311. + err = au_opt_xino(sb, opt, &opt_xino, opts);
  26312. + opt++;
  26313. + }
  26314. + if (err > 0)
  26315. + err = 0;
  26316. + AuTraceErr(err);
  26317. + /* go on even err */
  26318. +
  26319. + no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
  26320. + rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
  26321. + if (unlikely(rerr && !err))
  26322. + err = rerr;
  26323. +
  26324. + if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
  26325. + au_fset_opts(opts->flags, REFRESH_IDOP);
  26326. +
  26327. + if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
  26328. + rerr = au_xib_trunc(sb);
  26329. + if (unlikely(rerr && !err))
  26330. + err = rerr;
  26331. + }
  26332. +
  26333. + /* will be handled by the caller */
  26334. + if (!au_ftest_opts(opts->flags, REFRESH)
  26335. + && (opts->given_udba
  26336. + || au_opt_test(sbinfo->si_mntflags, XINO)
  26337. + || au_ftest_opts(opts->flags, REFRESH_IDOP)
  26338. + ))
  26339. + au_fset_opts(opts->flags, REFRESH);
  26340. +
  26341. + AuDbg("status 0x%x\n", opts->flags);
  26342. +
  26343. +out:
  26344. + return err;
  26345. +}
  26346. +
  26347. +/* ---------------------------------------------------------------------- */
  26348. +
  26349. +unsigned int au_opt_udba(struct super_block *sb)
  26350. +{
  26351. + return au_mntflags(sb) & AuOptMask_UDBA;
  26352. +}
  26353. --- /dev/null
  26354. +++ linux-4.19/fs/aufs/opts.h 2018-11-20 09:35:15.037195504 +0200
  26355. @@ -0,0 +1,225 @@
  26356. +/* SPDX-License-Identifier: GPL-2.0 */
  26357. +/*
  26358. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  26359. + *
  26360. + * This program, aufs is free software; you can redistribute it and/or modify
  26361. + * it under the terms of the GNU General Public License as published by
  26362. + * the Free Software Foundation; either version 2 of the License, or
  26363. + * (at your option) any later version.
  26364. + *
  26365. + * This program is distributed in the hope that it will be useful,
  26366. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26367. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26368. + * GNU General Public License for more details.
  26369. + *
  26370. + * You should have received a copy of the GNU General Public License
  26371. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26372. + */
  26373. +
  26374. +/*
  26375. + * mount options/flags
  26376. + */
  26377. +
  26378. +#ifndef __AUFS_OPTS_H__
  26379. +#define __AUFS_OPTS_H__
  26380. +
  26381. +#ifdef __KERNEL__
  26382. +
  26383. +#include <linux/path.h>
  26384. +
  26385. +struct file;
  26386. +
  26387. +/* ---------------------------------------------------------------------- */
  26388. +
  26389. +/* mount flags */
  26390. +#define AuOpt_XINO 1 /* external inode number bitmap
  26391. + and translation table */
  26392. +#define AuOpt_TRUNC_XINO (1 << 1) /* truncate xino files */
  26393. +#define AuOpt_UDBA_NONE (1 << 2) /* users direct branch access */
  26394. +#define AuOpt_UDBA_REVAL (1 << 3)
  26395. +#define AuOpt_UDBA_HNOTIFY (1 << 4)
  26396. +#define AuOpt_SHWH (1 << 5) /* show whiteout */
  26397. +#define AuOpt_PLINK (1 << 6) /* pseudo-link */
  26398. +#define AuOpt_DIRPERM1 (1 << 7) /* ignore the lower dir's perm
  26399. + bits */
  26400. +#define AuOpt_ALWAYS_DIROPQ (1 << 9) /* policy to creating diropq */
  26401. +#define AuOpt_SUM (1 << 10) /* summation for statfs(2) */
  26402. +#define AuOpt_SUM_W (1 << 11) /* unimplemented */
  26403. +#define AuOpt_WARN_PERM (1 << 12) /* warn when add-branch */
  26404. +#define AuOpt_VERBOSE (1 << 13) /* busy inode when del-branch */
  26405. +#define AuOpt_DIO (1 << 14) /* direct io */
  26406. +#define AuOpt_DIRREN (1 << 15) /* directory rename */
  26407. +
  26408. +#ifndef CONFIG_AUFS_HNOTIFY
  26409. +#undef AuOpt_UDBA_HNOTIFY
  26410. +#define AuOpt_UDBA_HNOTIFY 0
  26411. +#endif
  26412. +#ifndef CONFIG_AUFS_DIRREN
  26413. +#undef AuOpt_DIRREN
  26414. +#define AuOpt_DIRREN 0
  26415. +#endif
  26416. +#ifndef CONFIG_AUFS_SHWH
  26417. +#undef AuOpt_SHWH
  26418. +#define AuOpt_SHWH 0
  26419. +#endif
  26420. +
  26421. +#define AuOpt_Def (AuOpt_XINO \
  26422. + | AuOpt_UDBA_REVAL \
  26423. + | AuOpt_PLINK \
  26424. + /* | AuOpt_DIRPERM1 */ \
  26425. + | AuOpt_WARN_PERM)
  26426. +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
  26427. + | AuOpt_UDBA_REVAL \
  26428. + | AuOpt_UDBA_HNOTIFY)
  26429. +
  26430. +#define au_opt_test(flags, name) (flags & AuOpt_##name)
  26431. +#define au_opt_set(flags, name) do { \
  26432. + BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
  26433. + ((flags) |= AuOpt_##name); \
  26434. +} while (0)
  26435. +#define au_opt_set_udba(flags, name) do { \
  26436. + (flags) &= ~AuOptMask_UDBA; \
  26437. + ((flags) |= AuOpt_##name); \
  26438. +} while (0)
  26439. +#define au_opt_clr(flags, name) do { \
  26440. + ((flags) &= ~AuOpt_##name); \
  26441. +} while (0)
  26442. +
  26443. +static inline unsigned int au_opts_plink(unsigned int mntflags)
  26444. +{
  26445. +#ifdef CONFIG_PROC_FS
  26446. + return mntflags;
  26447. +#else
  26448. + return mntflags & ~AuOpt_PLINK;
  26449. +#endif
  26450. +}
  26451. +
  26452. +/* ---------------------------------------------------------------------- */
  26453. +
  26454. +/* policies to select one among multiple writable branches */
  26455. +enum {
  26456. + AuWbrCreate_TDP, /* top down parent */
  26457. + AuWbrCreate_RR, /* round robin */
  26458. + AuWbrCreate_MFS, /* most free space */
  26459. + AuWbrCreate_MFSV, /* mfs with seconds */
  26460. + AuWbrCreate_MFSRR, /* mfs then rr */
  26461. + AuWbrCreate_MFSRRV, /* mfs then rr with seconds */
  26462. + AuWbrCreate_TDMFS, /* top down regardless parent and mfs */
  26463. + AuWbrCreate_TDMFSV, /* top down regardless parent and mfs */
  26464. + AuWbrCreate_PMFS, /* parent and mfs */
  26465. + AuWbrCreate_PMFSV, /* parent and mfs with seconds */
  26466. + AuWbrCreate_PMFSRR, /* parent, mfs and round-robin */
  26467. + AuWbrCreate_PMFSRRV, /* plus seconds */
  26468. +
  26469. + AuWbrCreate_Def = AuWbrCreate_TDP
  26470. +};
  26471. +
  26472. +enum {
  26473. + AuWbrCopyup_TDP, /* top down parent */
  26474. + AuWbrCopyup_BUP, /* bottom up parent */
  26475. + AuWbrCopyup_BU, /* bottom up */
  26476. +
  26477. + AuWbrCopyup_Def = AuWbrCopyup_TDP
  26478. +};
  26479. +
  26480. +/* ---------------------------------------------------------------------- */
  26481. +
  26482. +struct au_opt_add {
  26483. + aufs_bindex_t bindex;
  26484. + char *pathname;
  26485. + int perm;
  26486. + struct path path;
  26487. +};
  26488. +
  26489. +struct au_opt_del {
  26490. + char *pathname;
  26491. + struct path h_path;
  26492. +};
  26493. +
  26494. +struct au_opt_mod {
  26495. + char *path;
  26496. + int perm;
  26497. + struct dentry *h_root;
  26498. +};
  26499. +
  26500. +struct au_opt_xino {
  26501. + char *path;
  26502. + struct file *file;
  26503. +};
  26504. +
  26505. +struct au_opt_xino_itrunc {
  26506. + aufs_bindex_t bindex;
  26507. +};
  26508. +
  26509. +struct au_opt_wbr_create {
  26510. + int wbr_create;
  26511. + int mfs_second;
  26512. + unsigned long long mfsrr_watermark;
  26513. +};
  26514. +
  26515. +struct au_opt {
  26516. + int type;
  26517. + union {
  26518. + struct au_opt_xino xino;
  26519. + struct au_opt_xino_itrunc xino_itrunc;
  26520. + struct au_opt_add add;
  26521. + struct au_opt_del del;
  26522. + struct au_opt_mod mod;
  26523. + int dirwh;
  26524. + int rdcache;
  26525. + unsigned int rdblk;
  26526. + unsigned int rdhash;
  26527. + int udba;
  26528. + struct au_opt_wbr_create wbr_create;
  26529. + int wbr_copyup;
  26530. + unsigned int fhsm_second;
  26531. + };
  26532. +};
  26533. +
  26534. +/* opts flags */
  26535. +#define AuOpts_REMOUNT 1
  26536. +#define AuOpts_REFRESH (1 << 1)
  26537. +#define AuOpts_TRUNC_XIB (1 << 2)
  26538. +#define AuOpts_REFRESH_DYAOP (1 << 3)
  26539. +#define AuOpts_REFRESH_IDOP (1 << 4)
  26540. +#define AuOpts_DR_FLUSHED (1 << 5)
  26541. +#define au_ftest_opts(flags, name) ((flags) & AuOpts_##name)
  26542. +#define au_fset_opts(flags, name) \
  26543. + do { (flags) |= AuOpts_##name; } while (0)
  26544. +#define au_fclr_opts(flags, name) \
  26545. + do { (flags) &= ~AuOpts_##name; } while (0)
  26546. +
  26547. +#ifndef CONFIG_AUFS_DIRREN
  26548. +#undef AuOpts_DR_FLUSHED
  26549. +#define AuOpts_DR_FLUSHED 0
  26550. +#endif
  26551. +
  26552. +struct au_opts {
  26553. + struct au_opt *opt;
  26554. + int max_opt;
  26555. +
  26556. + unsigned int given_udba;
  26557. + unsigned int flags;
  26558. + unsigned long sb_flags;
  26559. +};
  26560. +
  26561. +/* ---------------------------------------------------------------------- */
  26562. +
  26563. +/* opts.c */
  26564. +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
  26565. +const char *au_optstr_udba(int udba);
  26566. +const char *au_optstr_wbr_copyup(int wbr_copyup);
  26567. +const char *au_optstr_wbr_create(int wbr_create);
  26568. +
  26569. +void au_opts_free(struct au_opts *opts);
  26570. +struct super_block;
  26571. +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
  26572. +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
  26573. + unsigned int pending);
  26574. +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
  26575. +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
  26576. +
  26577. +unsigned int au_opt_udba(struct super_block *sb);
  26578. +
  26579. +#endif /* __KERNEL__ */
  26580. +#endif /* __AUFS_OPTS_H__ */
  26581. --- /dev/null
  26582. +++ linux-4.19/fs/aufs/plink.c 2018-11-20 09:35:15.038195505 +0200
  26583. @@ -0,0 +1,516 @@
  26584. +// SPDX-License-Identifier: GPL-2.0
  26585. +/*
  26586. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  26587. + *
  26588. + * This program, aufs is free software; you can redistribute it and/or modify
  26589. + * it under the terms of the GNU General Public License as published by
  26590. + * the Free Software Foundation; either version 2 of the License, or
  26591. + * (at your option) any later version.
  26592. + *
  26593. + * This program is distributed in the hope that it will be useful,
  26594. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26595. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26596. + * GNU General Public License for more details.
  26597. + *
  26598. + * You should have received a copy of the GNU General Public License
  26599. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26600. + */
  26601. +
  26602. +/*
  26603. + * pseudo-link
  26604. + */
  26605. +
  26606. +#include "aufs.h"
  26607. +
  26608. +/*
  26609. + * the pseudo-link maintenance mode.
  26610. + * during a user process maintains the pseudo-links,
  26611. + * prohibit adding a new plink and branch manipulation.
  26612. + *
  26613. + * Flags
  26614. + * NOPLM:
  26615. + * For entry functions which will handle plink, and i_mutex is already held
  26616. + * in VFS.
  26617. + * They cannot wait and should return an error at once.
  26618. + * Callers has to check the error.
  26619. + * NOPLMW:
  26620. + * For entry functions which will handle plink, but i_mutex is not held
  26621. + * in VFS.
  26622. + * They can wait the plink maintenance mode to finish.
  26623. + *
  26624. + * They behave like F_SETLK and F_SETLKW.
  26625. + * If the caller never handle plink, then both flags are unnecessary.
  26626. + */
  26627. +
  26628. +int au_plink_maint(struct super_block *sb, int flags)
  26629. +{
  26630. + int err;
  26631. + pid_t pid, ppid;
  26632. + struct task_struct *parent, *prev;
  26633. + struct au_sbinfo *sbi;
  26634. +
  26635. + SiMustAnyLock(sb);
  26636. +
  26637. + err = 0;
  26638. + if (!au_opt_test(au_mntflags(sb), PLINK))
  26639. + goto out;
  26640. +
  26641. + sbi = au_sbi(sb);
  26642. + pid = sbi->si_plink_maint_pid;
  26643. + if (!pid || pid == current->pid)
  26644. + goto out;
  26645. +
  26646. + /* todo: it highly depends upon /sbin/mount.aufs */
  26647. + prev = NULL;
  26648. + parent = current;
  26649. + ppid = 0;
  26650. + rcu_read_lock();
  26651. + while (1) {
  26652. + parent = rcu_dereference(parent->real_parent);
  26653. + if (parent == prev)
  26654. + break;
  26655. + ppid = task_pid_vnr(parent);
  26656. + if (pid == ppid) {
  26657. + rcu_read_unlock();
  26658. + goto out;
  26659. + }
  26660. + prev = parent;
  26661. + }
  26662. + rcu_read_unlock();
  26663. +
  26664. + if (au_ftest_lock(flags, NOPLMW)) {
  26665. + /* if there is no i_mutex lock in VFS, we don't need to wait */
  26666. + /* AuDebugOn(!lockdep_depth(current)); */
  26667. + while (sbi->si_plink_maint_pid) {
  26668. + si_read_unlock(sb);
  26669. + /* gave up wake_up_bit() */
  26670. + wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
  26671. +
  26672. + if (au_ftest_lock(flags, FLUSH))
  26673. + au_nwt_flush(&sbi->si_nowait);
  26674. + si_noflush_read_lock(sb);
  26675. + }
  26676. + } else if (au_ftest_lock(flags, NOPLM)) {
  26677. + AuDbg("ppid %d, pid %d\n", ppid, pid);
  26678. + err = -EAGAIN;
  26679. + }
  26680. +
  26681. +out:
  26682. + return err;
  26683. +}
  26684. +
  26685. +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
  26686. +{
  26687. + spin_lock(&sbinfo->si_plink_maint_lock);
  26688. + sbinfo->si_plink_maint_pid = 0;
  26689. + spin_unlock(&sbinfo->si_plink_maint_lock);
  26690. + wake_up_all(&sbinfo->si_plink_wq);
  26691. +}
  26692. +
  26693. +int au_plink_maint_enter(struct super_block *sb)
  26694. +{
  26695. + int err;
  26696. + struct au_sbinfo *sbinfo;
  26697. +
  26698. + err = 0;
  26699. + sbinfo = au_sbi(sb);
  26700. + /* make sure i am the only one in this fs */
  26701. + si_write_lock(sb, AuLock_FLUSH);
  26702. + if (au_opt_test(au_mntflags(sb), PLINK)) {
  26703. + spin_lock(&sbinfo->si_plink_maint_lock);
  26704. + if (!sbinfo->si_plink_maint_pid)
  26705. + sbinfo->si_plink_maint_pid = current->pid;
  26706. + else
  26707. + err = -EBUSY;
  26708. + spin_unlock(&sbinfo->si_plink_maint_lock);
  26709. + }
  26710. + si_write_unlock(sb);
  26711. +
  26712. + return err;
  26713. +}
  26714. +
  26715. +/* ---------------------------------------------------------------------- */
  26716. +
  26717. +#ifdef CONFIG_AUFS_DEBUG
  26718. +void au_plink_list(struct super_block *sb)
  26719. +{
  26720. + int i;
  26721. + struct au_sbinfo *sbinfo;
  26722. + struct hlist_bl_head *hbl;
  26723. + struct hlist_bl_node *pos;
  26724. + struct au_icntnr *icntnr;
  26725. +
  26726. + SiMustAnyLock(sb);
  26727. +
  26728. + sbinfo = au_sbi(sb);
  26729. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  26730. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  26731. +
  26732. + for (i = 0; i < AuPlink_NHASH; i++) {
  26733. + hbl = sbinfo->si_plink + i;
  26734. + hlist_bl_lock(hbl);
  26735. + hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
  26736. + AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
  26737. + hlist_bl_unlock(hbl);
  26738. + }
  26739. +}
  26740. +#endif
  26741. +
  26742. +/* is the inode pseudo-linked? */
  26743. +int au_plink_test(struct inode *inode)
  26744. +{
  26745. + int found, i;
  26746. + struct au_sbinfo *sbinfo;
  26747. + struct hlist_bl_head *hbl;
  26748. + struct hlist_bl_node *pos;
  26749. + struct au_icntnr *icntnr;
  26750. +
  26751. + sbinfo = au_sbi(inode->i_sb);
  26752. + AuRwMustAnyLock(&sbinfo->si_rwsem);
  26753. + AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
  26754. + AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
  26755. +
  26756. + found = 0;
  26757. + i = au_plink_hash(inode->i_ino);
  26758. + hbl = sbinfo->si_plink + i;
  26759. + hlist_bl_lock(hbl);
  26760. + hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
  26761. + if (&icntnr->vfs_inode == inode) {
  26762. + found = 1;
  26763. + break;
  26764. + }
  26765. + hlist_bl_unlock(hbl);
  26766. + return found;
  26767. +}
  26768. +
  26769. +/* ---------------------------------------------------------------------- */
  26770. +
  26771. +/*
  26772. + * generate a name for plink.
  26773. + * the file will be stored under AUFS_WH_PLINKDIR.
  26774. + */
  26775. +/* 20 is max digits length of ulong 64 */
  26776. +#define PLINK_NAME_LEN ((20 + 1) * 2)
  26777. +
  26778. +static int plink_name(char *name, int len, struct inode *inode,
  26779. + aufs_bindex_t bindex)
  26780. +{
  26781. + int rlen;
  26782. + struct inode *h_inode;
  26783. +
  26784. + h_inode = au_h_iptr(inode, bindex);
  26785. + rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
  26786. + return rlen;
  26787. +}
  26788. +
  26789. +struct au_do_plink_lkup_args {
  26790. + struct dentry **errp;
  26791. + struct qstr *tgtname;
  26792. + struct dentry *h_parent;
  26793. + struct au_branch *br;
  26794. +};
  26795. +
  26796. +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
  26797. + struct dentry *h_parent,
  26798. + struct au_branch *br)
  26799. +{
  26800. + struct dentry *h_dentry;
  26801. + struct inode *h_inode;
  26802. +
  26803. + h_inode = d_inode(h_parent);
  26804. + inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
  26805. + h_dentry = vfsub_lkup_one(tgtname, h_parent);
  26806. + inode_unlock_shared(h_inode);
  26807. + return h_dentry;
  26808. +}
  26809. +
  26810. +static void au_call_do_plink_lkup(void *args)
  26811. +{
  26812. + struct au_do_plink_lkup_args *a = args;
  26813. + *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
  26814. +}
  26815. +
  26816. +/* lookup the plink-ed @inode under the branch at @bindex */
  26817. +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
  26818. +{
  26819. + struct dentry *h_dentry, *h_parent;
  26820. + struct au_branch *br;
  26821. + int wkq_err;
  26822. + char a[PLINK_NAME_LEN];
  26823. + struct qstr tgtname = QSTR_INIT(a, 0);
  26824. +
  26825. + AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
  26826. +
  26827. + br = au_sbr(inode->i_sb, bindex);
  26828. + h_parent = br->br_wbr->wbr_plink;
  26829. + tgtname.len = plink_name(a, sizeof(a), inode, bindex);
  26830. +
  26831. + if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
  26832. + struct au_do_plink_lkup_args args = {
  26833. + .errp = &h_dentry,
  26834. + .tgtname = &tgtname,
  26835. + .h_parent = h_parent,
  26836. + .br = br
  26837. + };
  26838. +
  26839. + wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
  26840. + if (unlikely(wkq_err))
  26841. + h_dentry = ERR_PTR(wkq_err);
  26842. + } else
  26843. + h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
  26844. +
  26845. + return h_dentry;
  26846. +}
  26847. +
  26848. +/* create a pseudo-link */
  26849. +static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
  26850. + struct dentry *h_dentry, struct au_branch *br)
  26851. +{
  26852. + int err;
  26853. + struct path h_path = {
  26854. + .mnt = au_br_mnt(br)
  26855. + };
  26856. + struct inode *h_dir, *delegated;
  26857. +
  26858. + h_dir = d_inode(h_parent);
  26859. + inode_lock_nested(h_dir, AuLsc_I_CHILD2);
  26860. +again:
  26861. + h_path.dentry = vfsub_lkup_one(tgt, h_parent);
  26862. + err = PTR_ERR(h_path.dentry);
  26863. + if (IS_ERR(h_path.dentry))
  26864. + goto out;
  26865. +
  26866. + err = 0;
  26867. + /* wh.plink dir is not monitored */
  26868. + /* todo: is it really safe? */
  26869. + if (d_is_positive(h_path.dentry)
  26870. + && d_inode(h_path.dentry) != d_inode(h_dentry)) {
  26871. + delegated = NULL;
  26872. + err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
  26873. + if (unlikely(err == -EWOULDBLOCK)) {
  26874. + pr_warn("cannot retry for NFSv4 delegation"
  26875. + " for an internal unlink\n");
  26876. + iput(delegated);
  26877. + }
  26878. + dput(h_path.dentry);
  26879. + h_path.dentry = NULL;
  26880. + if (!err)
  26881. + goto again;
  26882. + }
  26883. + if (!err && d_is_negative(h_path.dentry)) {
  26884. + delegated = NULL;
  26885. + err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
  26886. + if (unlikely(err == -EWOULDBLOCK)) {
  26887. + pr_warn("cannot retry for NFSv4 delegation"
  26888. + " for an internal link\n");
  26889. + iput(delegated);
  26890. + }
  26891. + }
  26892. + dput(h_path.dentry);
  26893. +
  26894. +out:
  26895. + inode_unlock(h_dir);
  26896. + return err;
  26897. +}
  26898. +
  26899. +struct do_whplink_args {
  26900. + int *errp;
  26901. + struct qstr *tgt;
  26902. + struct dentry *h_parent;
  26903. + struct dentry *h_dentry;
  26904. + struct au_branch *br;
  26905. +};
  26906. +
  26907. +static void call_do_whplink(void *args)
  26908. +{
  26909. + struct do_whplink_args *a = args;
  26910. + *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
  26911. +}
  26912. +
  26913. +static int whplink(struct dentry *h_dentry, struct inode *inode,
  26914. + aufs_bindex_t bindex, struct au_branch *br)
  26915. +{
  26916. + int err, wkq_err;
  26917. + struct au_wbr *wbr;
  26918. + struct dentry *h_parent;
  26919. + char a[PLINK_NAME_LEN];
  26920. + struct qstr tgtname = QSTR_INIT(a, 0);
  26921. +
  26922. + wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
  26923. + h_parent = wbr->wbr_plink;
  26924. + tgtname.len = plink_name(a, sizeof(a), inode, bindex);
  26925. +
  26926. + /* always superio. */
  26927. + if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
  26928. + struct do_whplink_args args = {
  26929. + .errp = &err,
  26930. + .tgt = &tgtname,
  26931. + .h_parent = h_parent,
  26932. + .h_dentry = h_dentry,
  26933. + .br = br
  26934. + };
  26935. + wkq_err = au_wkq_wait(call_do_whplink, &args);
  26936. + if (unlikely(wkq_err))
  26937. + err = wkq_err;
  26938. + } else
  26939. + err = do_whplink(&tgtname, h_parent, h_dentry, br);
  26940. +
  26941. + return err;
  26942. +}
  26943. +
  26944. +/*
  26945. + * create a new pseudo-link for @h_dentry on @bindex.
  26946. + * the linked inode is held in aufs @inode.
  26947. + */
  26948. +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
  26949. + struct dentry *h_dentry)
  26950. +{
  26951. + struct super_block *sb;
  26952. + struct au_sbinfo *sbinfo;
  26953. + struct hlist_bl_head *hbl;
  26954. + struct hlist_bl_node *pos;
  26955. + struct au_icntnr *icntnr;
  26956. + int found, err, cnt, i;
  26957. +
  26958. + sb = inode->i_sb;
  26959. + sbinfo = au_sbi(sb);
  26960. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  26961. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  26962. +
  26963. + found = au_plink_test(inode);
  26964. + if (found)
  26965. + return;
  26966. +
  26967. + i = au_plink_hash(inode->i_ino);
  26968. + hbl = sbinfo->si_plink + i;
  26969. + au_igrab(inode);
  26970. +
  26971. + hlist_bl_lock(hbl);
  26972. + hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
  26973. + if (&icntnr->vfs_inode == inode) {
  26974. + found = 1;
  26975. + break;
  26976. + }
  26977. + }
  26978. + if (!found) {
  26979. + icntnr = container_of(inode, struct au_icntnr, vfs_inode);
  26980. + hlist_bl_add_head(&icntnr->plink, hbl);
  26981. + }
  26982. + hlist_bl_unlock(hbl);
  26983. + if (!found) {
  26984. + cnt = au_hbl_count(hbl);
  26985. +#define msg "unexpectedly unbalanced or too many pseudo-links"
  26986. + if (cnt > AUFS_PLINK_WARN)
  26987. + AuWarn1(msg ", %d\n", cnt);
  26988. +#undef msg
  26989. + err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
  26990. + if (unlikely(err)) {
  26991. + pr_warn("err %d, damaged pseudo link.\n", err);
  26992. + au_hbl_del(&icntnr->plink, hbl);
  26993. + iput(&icntnr->vfs_inode);
  26994. + }
  26995. + } else
  26996. + iput(&icntnr->vfs_inode);
  26997. +}
  26998. +
  26999. +/* free all plinks */
  27000. +void au_plink_put(struct super_block *sb, int verbose)
  27001. +{
  27002. + int i, warned;
  27003. + struct au_sbinfo *sbinfo;
  27004. + struct hlist_bl_head *hbl;
  27005. + struct hlist_bl_node *pos, *tmp;
  27006. + struct au_icntnr *icntnr;
  27007. +
  27008. + SiMustWriteLock(sb);
  27009. +
  27010. + sbinfo = au_sbi(sb);
  27011. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  27012. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  27013. +
  27014. + /* no spin_lock since sbinfo is write-locked */
  27015. + warned = 0;
  27016. + for (i = 0; i < AuPlink_NHASH; i++) {
  27017. + hbl = sbinfo->si_plink + i;
  27018. + if (!warned && verbose && !hlist_bl_empty(hbl)) {
  27019. + pr_warn("pseudo-link is not flushed");
  27020. + warned = 1;
  27021. + }
  27022. + hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
  27023. + iput(&icntnr->vfs_inode);
  27024. + INIT_HLIST_BL_HEAD(hbl);
  27025. + }
  27026. +}
  27027. +
  27028. +void au_plink_clean(struct super_block *sb, int verbose)
  27029. +{
  27030. + struct dentry *root;
  27031. +
  27032. + root = sb->s_root;
  27033. + aufs_write_lock(root);
  27034. + if (au_opt_test(au_mntflags(sb), PLINK))
  27035. + au_plink_put(sb, verbose);
  27036. + aufs_write_unlock(root);
  27037. +}
  27038. +
  27039. +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
  27040. +{
  27041. + int do_put;
  27042. + aufs_bindex_t btop, bbot, bindex;
  27043. +
  27044. + do_put = 0;
  27045. + btop = au_ibtop(inode);
  27046. + bbot = au_ibbot(inode);
  27047. + if (btop >= 0) {
  27048. + for (bindex = btop; bindex <= bbot; bindex++) {
  27049. + if (!au_h_iptr(inode, bindex)
  27050. + || au_ii_br_id(inode, bindex) != br_id)
  27051. + continue;
  27052. + au_set_h_iptr(inode, bindex, NULL, 0);
  27053. + do_put = 1;
  27054. + break;
  27055. + }
  27056. + if (do_put)
  27057. + for (bindex = btop; bindex <= bbot; bindex++)
  27058. + if (au_h_iptr(inode, bindex)) {
  27059. + do_put = 0;
  27060. + break;
  27061. + }
  27062. + } else
  27063. + do_put = 1;
  27064. +
  27065. + return do_put;
  27066. +}
  27067. +
  27068. +/* free the plinks on a branch specified by @br_id */
  27069. +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
  27070. +{
  27071. + struct au_sbinfo *sbinfo;
  27072. + struct hlist_bl_head *hbl;
  27073. + struct hlist_bl_node *pos, *tmp;
  27074. + struct au_icntnr *icntnr;
  27075. + struct inode *inode;
  27076. + int i, do_put;
  27077. +
  27078. + SiMustWriteLock(sb);
  27079. +
  27080. + sbinfo = au_sbi(sb);
  27081. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  27082. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  27083. +
  27084. + /* no bit_lock since sbinfo is write-locked */
  27085. + for (i = 0; i < AuPlink_NHASH; i++) {
  27086. + hbl = sbinfo->si_plink + i;
  27087. + hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
  27088. + inode = au_igrab(&icntnr->vfs_inode);
  27089. + ii_write_lock_child(inode);
  27090. + do_put = au_plink_do_half_refresh(inode, br_id);
  27091. + if (do_put) {
  27092. + hlist_bl_del(&icntnr->plink);
  27093. + iput(inode);
  27094. + }
  27095. + ii_write_unlock(inode);
  27096. + iput(inode);
  27097. + }
  27098. + }
  27099. +}
  27100. --- /dev/null
  27101. +++ linux-4.19/fs/aufs/poll.c 2018-11-20 09:35:15.038195505 +0200
  27102. @@ -0,0 +1,51 @@
  27103. +// SPDX-License-Identifier: GPL-2.0
  27104. +/*
  27105. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  27106. + *
  27107. + * This program, aufs is free software; you can redistribute it and/or modify
  27108. + * it under the terms of the GNU General Public License as published by
  27109. + * the Free Software Foundation; either version 2 of the License, or
  27110. + * (at your option) any later version.
  27111. + *
  27112. + * This program is distributed in the hope that it will be useful,
  27113. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27114. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27115. + * GNU General Public License for more details.
  27116. + *
  27117. + * You should have received a copy of the GNU General Public License
  27118. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27119. + */
  27120. +
  27121. +/*
  27122. + * poll operation
  27123. + * There is only one filesystem which implements ->poll operation, currently.
  27124. + */
  27125. +
  27126. +#include "aufs.h"
  27127. +
  27128. +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
  27129. +{
  27130. + __poll_t mask;
  27131. + struct file *h_file;
  27132. + struct super_block *sb;
  27133. +
  27134. + /* We should pretend an error happened. */
  27135. + mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
  27136. + sb = file->f_path.dentry->d_sb;
  27137. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  27138. +
  27139. + h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
  27140. + if (IS_ERR(h_file)) {
  27141. + AuDbg("h_file %ld\n", PTR_ERR(h_file));
  27142. + goto out;
  27143. + }
  27144. +
  27145. + mask = vfs_poll(h_file, pt);
  27146. + fput(h_file); /* instead of au_read_post() */
  27147. +
  27148. +out:
  27149. + si_read_unlock(sb);
  27150. + if (mask & EPOLLERR)
  27151. + AuDbg("mask 0x%x\n", mask);
  27152. + return mask;
  27153. +}
  27154. --- /dev/null
  27155. +++ linux-4.19/fs/aufs/posix_acl.c 2018-11-20 09:35:15.039195505 +0200
  27156. @@ -0,0 +1,103 @@
  27157. +// SPDX-License-Identifier: GPL-2.0
  27158. +/*
  27159. + * Copyright (C) 2014-2018 Junjiro R. Okajima
  27160. + *
  27161. + * This program, aufs is free software; you can redistribute it and/or modify
  27162. + * it under the terms of the GNU General Public License as published by
  27163. + * the Free Software Foundation; either version 2 of the License, or
  27164. + * (at your option) any later version.
  27165. + *
  27166. + * This program is distributed in the hope that it will be useful,
  27167. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27168. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27169. + * GNU General Public License for more details.
  27170. + *
  27171. + * You should have received a copy of the GNU General Public License
  27172. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27173. + */
  27174. +
  27175. +/*
  27176. + * posix acl operations
  27177. + */
  27178. +
  27179. +#include <linux/fs.h>
  27180. +#include "aufs.h"
  27181. +
  27182. +struct posix_acl *aufs_get_acl(struct inode *inode, int type)
  27183. +{
  27184. + struct posix_acl *acl;
  27185. + int err;
  27186. + aufs_bindex_t bindex;
  27187. + struct inode *h_inode;
  27188. + struct super_block *sb;
  27189. +
  27190. + acl = NULL;
  27191. + sb = inode->i_sb;
  27192. + si_read_lock(sb, AuLock_FLUSH);
  27193. + ii_read_lock_child(inode);
  27194. + if (!(sb->s_flags & SB_POSIXACL))
  27195. + goto out;
  27196. +
  27197. + bindex = au_ibtop(inode);
  27198. + h_inode = au_h_iptr(inode, bindex);
  27199. + if (unlikely(!h_inode
  27200. + || ((h_inode->i_mode & S_IFMT)
  27201. + != (inode->i_mode & S_IFMT)))) {
  27202. + err = au_busy_or_stale();
  27203. + acl = ERR_PTR(err);
  27204. + goto out;
  27205. + }
  27206. +
  27207. + /* always topmost only */
  27208. + acl = get_acl(h_inode, type);
  27209. + if (!IS_ERR_OR_NULL(acl))
  27210. + set_cached_acl(inode, type, acl);
  27211. +
  27212. +out:
  27213. + ii_read_unlock(inode);
  27214. + si_read_unlock(sb);
  27215. +
  27216. + AuTraceErrPtr(acl);
  27217. + return acl;
  27218. +}
  27219. +
  27220. +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  27221. +{
  27222. + int err;
  27223. + ssize_t ssz;
  27224. + struct dentry *dentry;
  27225. + struct au_sxattr arg = {
  27226. + .type = AU_ACL_SET,
  27227. + .u.acl_set = {
  27228. + .acl = acl,
  27229. + .type = type
  27230. + },
  27231. + };
  27232. +
  27233. + IMustLock(inode);
  27234. +
  27235. + if (inode->i_ino == AUFS_ROOT_INO)
  27236. + dentry = dget(inode->i_sb->s_root);
  27237. + else {
  27238. + dentry = d_find_alias(inode);
  27239. + if (!dentry)
  27240. + dentry = d_find_any_alias(inode);
  27241. + if (!dentry) {
  27242. + pr_warn("cannot handle this inode, "
  27243. + "please report to aufs-users ML\n");
  27244. + err = -ENOENT;
  27245. + goto out;
  27246. + }
  27247. + }
  27248. +
  27249. + ssz = au_sxattr(dentry, inode, &arg);
  27250. + dput(dentry);
  27251. + err = ssz;
  27252. + if (ssz >= 0) {
  27253. + err = 0;
  27254. + set_cached_acl(inode, type, acl);
  27255. + }
  27256. +
  27257. +out:
  27258. + return err;
  27259. +}
  27260. --- /dev/null
  27261. +++ linux-4.19/fs/aufs/procfs.c 2018-11-20 09:35:15.039195505 +0200
  27262. @@ -0,0 +1,171 @@
  27263. +// SPDX-License-Identifier: GPL-2.0
  27264. +/*
  27265. + * Copyright (C) 2010-2018 Junjiro R. Okajima
  27266. + *
  27267. + * This program, aufs is free software; you can redistribute it and/or modify
  27268. + * it under the terms of the GNU General Public License as published by
  27269. + * the Free Software Foundation; either version 2 of the License, or
  27270. + * (at your option) any later version.
  27271. + *
  27272. + * This program is distributed in the hope that it will be useful,
  27273. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27274. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27275. + * GNU General Public License for more details.
  27276. + *
  27277. + * You should have received a copy of the GNU General Public License
  27278. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27279. + */
  27280. +
  27281. +/*
  27282. + * procfs interfaces
  27283. + */
  27284. +
  27285. +#include <linux/proc_fs.h>
  27286. +#include "aufs.h"
  27287. +
  27288. +static int au_procfs_plm_release(struct inode *inode, struct file *file)
  27289. +{
  27290. + struct au_sbinfo *sbinfo;
  27291. +
  27292. + sbinfo = file->private_data;
  27293. + if (sbinfo) {
  27294. + au_plink_maint_leave(sbinfo);
  27295. + kobject_put(&sbinfo->si_kobj);
  27296. + }
  27297. +
  27298. + return 0;
  27299. +}
  27300. +
  27301. +static void au_procfs_plm_write_clean(struct file *file)
  27302. +{
  27303. + struct au_sbinfo *sbinfo;
  27304. +
  27305. + sbinfo = file->private_data;
  27306. + if (sbinfo)
  27307. + au_plink_clean(sbinfo->si_sb, /*verbose*/0);
  27308. +}
  27309. +
  27310. +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
  27311. +{
  27312. + int err;
  27313. + struct super_block *sb;
  27314. + struct au_sbinfo *sbinfo;
  27315. + struct hlist_bl_node *pos;
  27316. +
  27317. + err = -EBUSY;
  27318. + if (unlikely(file->private_data))
  27319. + goto out;
  27320. +
  27321. + sb = NULL;
  27322. + /* don't use au_sbilist_lock() here */
  27323. + hlist_bl_lock(&au_sbilist);
  27324. + hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
  27325. + if (id == sysaufs_si_id(sbinfo)) {
  27326. + kobject_get(&sbinfo->si_kobj);
  27327. + sb = sbinfo->si_sb;
  27328. + break;
  27329. + }
  27330. + hlist_bl_unlock(&au_sbilist);
  27331. +
  27332. + err = -EINVAL;
  27333. + if (unlikely(!sb))
  27334. + goto out;
  27335. +
  27336. + err = au_plink_maint_enter(sb);
  27337. + if (!err)
  27338. + /* keep kobject_get() */
  27339. + file->private_data = sbinfo;
  27340. + else
  27341. + kobject_put(&sbinfo->si_kobj);
  27342. +out:
  27343. + return err;
  27344. +}
  27345. +
  27346. +/*
  27347. + * Accept a valid "si=xxxx" only.
  27348. + * Once it is accepted successfully, accept "clean" too.
  27349. + */
  27350. +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
  27351. + size_t count, loff_t *ppos)
  27352. +{
  27353. + ssize_t err;
  27354. + unsigned long id;
  27355. + /* last newline is allowed */
  27356. + char buf[3 + sizeof(unsigned long) * 2 + 1];
  27357. +
  27358. + err = -EACCES;
  27359. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  27360. + goto out;
  27361. +
  27362. + err = -EINVAL;
  27363. + if (unlikely(count > sizeof(buf)))
  27364. + goto out;
  27365. +
  27366. + err = copy_from_user(buf, ubuf, count);
  27367. + if (unlikely(err)) {
  27368. + err = -EFAULT;
  27369. + goto out;
  27370. + }
  27371. + buf[count] = 0;
  27372. +
  27373. + err = -EINVAL;
  27374. + if (!strcmp("clean", buf)) {
  27375. + au_procfs_plm_write_clean(file);
  27376. + goto out_success;
  27377. + } else if (unlikely(strncmp("si=", buf, 3)))
  27378. + goto out;
  27379. +
  27380. + err = kstrtoul(buf + 3, 16, &id);
  27381. + if (unlikely(err))
  27382. + goto out;
  27383. +
  27384. + err = au_procfs_plm_write_si(file, id);
  27385. + if (unlikely(err))
  27386. + goto out;
  27387. +
  27388. +out_success:
  27389. + err = count; /* success */
  27390. +out:
  27391. + return err;
  27392. +}
  27393. +
  27394. +static const struct file_operations au_procfs_plm_fop = {
  27395. + .write = au_procfs_plm_write,
  27396. + .release = au_procfs_plm_release,
  27397. + .owner = THIS_MODULE
  27398. +};
  27399. +
  27400. +/* ---------------------------------------------------------------------- */
  27401. +
  27402. +static struct proc_dir_entry *au_procfs_dir;
  27403. +
  27404. +void au_procfs_fin(void)
  27405. +{
  27406. + remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
  27407. + remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
  27408. +}
  27409. +
  27410. +int __init au_procfs_init(void)
  27411. +{
  27412. + int err;
  27413. + struct proc_dir_entry *entry;
  27414. +
  27415. + err = -ENOMEM;
  27416. + au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
  27417. + if (unlikely(!au_procfs_dir))
  27418. + goto out;
  27419. +
  27420. + entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
  27421. + au_procfs_dir, &au_procfs_plm_fop);
  27422. + if (unlikely(!entry))
  27423. + goto out_dir;
  27424. +
  27425. + err = 0;
  27426. + goto out; /* success */
  27427. +
  27428. +
  27429. +out_dir:
  27430. + remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
  27431. +out:
  27432. + return err;
  27433. +}
  27434. --- /dev/null
  27435. +++ linux-4.19/fs/aufs/rdu.c 2018-11-20 09:35:15.040195505 +0200
  27436. @@ -0,0 +1,382 @@
  27437. +// SPDX-License-Identifier: GPL-2.0
  27438. +/*
  27439. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  27440. + *
  27441. + * This program, aufs is free software; you can redistribute it and/or modify
  27442. + * it under the terms of the GNU General Public License as published by
  27443. + * the Free Software Foundation; either version 2 of the License, or
  27444. + * (at your option) any later version.
  27445. + *
  27446. + * This program is distributed in the hope that it will be useful,
  27447. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27448. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27449. + * GNU General Public License for more details.
  27450. + *
  27451. + * You should have received a copy of the GNU General Public License
  27452. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27453. + */
  27454. +
  27455. +/*
  27456. + * readdir in userspace.
  27457. + */
  27458. +
  27459. +#include <linux/compat.h>
  27460. +#include <linux/fs_stack.h>
  27461. +#include <linux/security.h>
  27462. +#include "aufs.h"
  27463. +
  27464. +/* bits for struct aufs_rdu.flags */
  27465. +#define AuRdu_CALLED 1
  27466. +#define AuRdu_CONT (1 << 1)
  27467. +#define AuRdu_FULL (1 << 2)
  27468. +#define au_ftest_rdu(flags, name) ((flags) & AuRdu_##name)
  27469. +#define au_fset_rdu(flags, name) \
  27470. + do { (flags) |= AuRdu_##name; } while (0)
  27471. +#define au_fclr_rdu(flags, name) \
  27472. + do { (flags) &= ~AuRdu_##name; } while (0)
  27473. +
  27474. +struct au_rdu_arg {
  27475. + struct dir_context ctx;
  27476. + struct aufs_rdu *rdu;
  27477. + union au_rdu_ent_ul ent;
  27478. + unsigned long end;
  27479. +
  27480. + struct super_block *sb;
  27481. + int err;
  27482. +};
  27483. +
  27484. +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
  27485. + loff_t offset, u64 h_ino, unsigned int d_type)
  27486. +{
  27487. + int err, len;
  27488. + struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
  27489. + struct aufs_rdu *rdu = arg->rdu;
  27490. + struct au_rdu_ent ent;
  27491. +
  27492. + err = 0;
  27493. + arg->err = 0;
  27494. + au_fset_rdu(rdu->cookie.flags, CALLED);
  27495. + len = au_rdu_len(nlen);
  27496. + if (arg->ent.ul + len < arg->end) {
  27497. + ent.ino = h_ino;
  27498. + ent.bindex = rdu->cookie.bindex;
  27499. + ent.type = d_type;
  27500. + ent.nlen = nlen;
  27501. + if (unlikely(nlen > AUFS_MAX_NAMELEN))
  27502. + ent.type = DT_UNKNOWN;
  27503. +
  27504. + /* unnecessary to support mmap_sem since this is a dir */
  27505. + err = -EFAULT;
  27506. + if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
  27507. + goto out;
  27508. + if (copy_to_user(arg->ent.e->name, name, nlen))
  27509. + goto out;
  27510. + /* the terminating NULL */
  27511. + if (__put_user(0, arg->ent.e->name + nlen))
  27512. + goto out;
  27513. + err = 0;
  27514. + /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
  27515. + arg->ent.ul += len;
  27516. + rdu->rent++;
  27517. + } else {
  27518. + err = -EFAULT;
  27519. + au_fset_rdu(rdu->cookie.flags, FULL);
  27520. + rdu->full = 1;
  27521. + rdu->tail = arg->ent;
  27522. + }
  27523. +
  27524. +out:
  27525. + /* AuTraceErr(err); */
  27526. + return err;
  27527. +}
  27528. +
  27529. +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
  27530. +{
  27531. + int err;
  27532. + loff_t offset;
  27533. + struct au_rdu_cookie *cookie = &arg->rdu->cookie;
  27534. +
  27535. + /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
  27536. + offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
  27537. + err = offset;
  27538. + if (unlikely(offset != cookie->h_pos))
  27539. + goto out;
  27540. +
  27541. + err = 0;
  27542. + do {
  27543. + arg->err = 0;
  27544. + au_fclr_rdu(cookie->flags, CALLED);
  27545. + /* smp_mb(); */
  27546. + err = vfsub_iterate_dir(h_file, &arg->ctx);
  27547. + if (err >= 0)
  27548. + err = arg->err;
  27549. + } while (!err
  27550. + && au_ftest_rdu(cookie->flags, CALLED)
  27551. + && !au_ftest_rdu(cookie->flags, FULL));
  27552. + cookie->h_pos = h_file->f_pos;
  27553. +
  27554. +out:
  27555. + AuTraceErr(err);
  27556. + return err;
  27557. +}
  27558. +
  27559. +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
  27560. +{
  27561. + int err;
  27562. + aufs_bindex_t bbot;
  27563. + struct au_rdu_arg arg = {
  27564. + .ctx = {
  27565. + .actor = au_rdu_fill
  27566. + }
  27567. + };
  27568. + struct dentry *dentry;
  27569. + struct inode *inode;
  27570. + struct file *h_file;
  27571. + struct au_rdu_cookie *cookie = &rdu->cookie;
  27572. +
  27573. + err = !access_ok(VERIFY_WRITE, rdu->ent.e, rdu->sz);
  27574. + if (unlikely(err)) {
  27575. + err = -EFAULT;
  27576. + AuTraceErr(err);
  27577. + goto out;
  27578. + }
  27579. + rdu->rent = 0;
  27580. + rdu->tail = rdu->ent;
  27581. + rdu->full = 0;
  27582. + arg.rdu = rdu;
  27583. + arg.ent = rdu->ent;
  27584. + arg.end = arg.ent.ul;
  27585. + arg.end += rdu->sz;
  27586. +
  27587. + err = -ENOTDIR;
  27588. + if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
  27589. + goto out;
  27590. +
  27591. + err = security_file_permission(file, MAY_READ);
  27592. + AuTraceErr(err);
  27593. + if (unlikely(err))
  27594. + goto out;
  27595. +
  27596. + dentry = file->f_path.dentry;
  27597. + inode = d_inode(dentry);
  27598. + inode_lock_shared(inode);
  27599. +
  27600. + arg.sb = inode->i_sb;
  27601. + err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
  27602. + if (unlikely(err))
  27603. + goto out_mtx;
  27604. + err = au_alive_dir(dentry);
  27605. + if (unlikely(err))
  27606. + goto out_si;
  27607. + /* todo: reval? */
  27608. + fi_read_lock(file);
  27609. +
  27610. + err = -EAGAIN;
  27611. + if (unlikely(au_ftest_rdu(cookie->flags, CONT)
  27612. + && cookie->generation != au_figen(file)))
  27613. + goto out_unlock;
  27614. +
  27615. + err = 0;
  27616. + if (!rdu->blk) {
  27617. + rdu->blk = au_sbi(arg.sb)->si_rdblk;
  27618. + if (!rdu->blk)
  27619. + rdu->blk = au_dir_size(file, /*dentry*/NULL);
  27620. + }
  27621. + bbot = au_fbtop(file);
  27622. + if (cookie->bindex < bbot)
  27623. + cookie->bindex = bbot;
  27624. + bbot = au_fbbot_dir(file);
  27625. + /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
  27626. + for (; !err && cookie->bindex <= bbot;
  27627. + cookie->bindex++, cookie->h_pos = 0) {
  27628. + h_file = au_hf_dir(file, cookie->bindex);
  27629. + if (!h_file)
  27630. + continue;
  27631. +
  27632. + au_fclr_rdu(cookie->flags, FULL);
  27633. + err = au_rdu_do(h_file, &arg);
  27634. + AuTraceErr(err);
  27635. + if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
  27636. + break;
  27637. + }
  27638. + AuDbg("rent %llu\n", rdu->rent);
  27639. +
  27640. + if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
  27641. + rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
  27642. + au_fset_rdu(cookie->flags, CONT);
  27643. + cookie->generation = au_figen(file);
  27644. + }
  27645. +
  27646. + ii_read_lock_child(inode);
  27647. + fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
  27648. + ii_read_unlock(inode);
  27649. +
  27650. +out_unlock:
  27651. + fi_read_unlock(file);
  27652. +out_si:
  27653. + si_read_unlock(arg.sb);
  27654. +out_mtx:
  27655. + inode_unlock_shared(inode);
  27656. +out:
  27657. + AuTraceErr(err);
  27658. + return err;
  27659. +}
  27660. +
  27661. +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
  27662. +{
  27663. + int err;
  27664. + ino_t ino;
  27665. + unsigned long long nent;
  27666. + union au_rdu_ent_ul *u;
  27667. + struct au_rdu_ent ent;
  27668. + struct super_block *sb;
  27669. +
  27670. + err = 0;
  27671. + nent = rdu->nent;
  27672. + u = &rdu->ent;
  27673. + sb = file->f_path.dentry->d_sb;
  27674. + si_read_lock(sb, AuLock_FLUSH);
  27675. + while (nent-- > 0) {
  27676. + /* unnecessary to support mmap_sem since this is a dir */
  27677. + err = copy_from_user(&ent, u->e, sizeof(ent));
  27678. + if (!err)
  27679. + err = !access_ok(VERIFY_WRITE, &u->e->ino, sizeof(ino));
  27680. + if (unlikely(err)) {
  27681. + err = -EFAULT;
  27682. + AuTraceErr(err);
  27683. + break;
  27684. + }
  27685. +
  27686. + /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
  27687. + if (!ent.wh)
  27688. + err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
  27689. + else
  27690. + err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
  27691. + &ino);
  27692. + if (unlikely(err)) {
  27693. + AuTraceErr(err);
  27694. + break;
  27695. + }
  27696. +
  27697. + err = __put_user(ino, &u->e->ino);
  27698. + if (unlikely(err)) {
  27699. + err = -EFAULT;
  27700. + AuTraceErr(err);
  27701. + break;
  27702. + }
  27703. + u->ul += au_rdu_len(ent.nlen);
  27704. + }
  27705. + si_read_unlock(sb);
  27706. +
  27707. + return err;
  27708. +}
  27709. +
  27710. +/* ---------------------------------------------------------------------- */
  27711. +
  27712. +static int au_rdu_verify(struct aufs_rdu *rdu)
  27713. +{
  27714. + AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
  27715. + "%llu, b%d, 0x%x, g%u}\n",
  27716. + rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
  27717. + rdu->blk,
  27718. + rdu->rent, rdu->shwh, rdu->full,
  27719. + rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
  27720. + rdu->cookie.generation);
  27721. +
  27722. + if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
  27723. + return 0;
  27724. +
  27725. + AuDbg("%u:%u\n",
  27726. + rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
  27727. + return -EINVAL;
  27728. +}
  27729. +
  27730. +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  27731. +{
  27732. + long err, e;
  27733. + struct aufs_rdu rdu;
  27734. + void __user *p = (void __user *)arg;
  27735. +
  27736. + err = copy_from_user(&rdu, p, sizeof(rdu));
  27737. + if (unlikely(err)) {
  27738. + err = -EFAULT;
  27739. + AuTraceErr(err);
  27740. + goto out;
  27741. + }
  27742. + err = au_rdu_verify(&rdu);
  27743. + if (unlikely(err))
  27744. + goto out;
  27745. +
  27746. + switch (cmd) {
  27747. + case AUFS_CTL_RDU:
  27748. + err = au_rdu(file, &rdu);
  27749. + if (unlikely(err))
  27750. + break;
  27751. +
  27752. + e = copy_to_user(p, &rdu, sizeof(rdu));
  27753. + if (unlikely(e)) {
  27754. + err = -EFAULT;
  27755. + AuTraceErr(err);
  27756. + }
  27757. + break;
  27758. + case AUFS_CTL_RDU_INO:
  27759. + err = au_rdu_ino(file, &rdu);
  27760. + break;
  27761. +
  27762. + default:
  27763. + /* err = -ENOTTY; */
  27764. + err = -EINVAL;
  27765. + }
  27766. +
  27767. +out:
  27768. + AuTraceErr(err);
  27769. + return err;
  27770. +}
  27771. +
  27772. +#ifdef CONFIG_COMPAT
  27773. +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  27774. +{
  27775. + long err, e;
  27776. + struct aufs_rdu rdu;
  27777. + void __user *p = compat_ptr(arg);
  27778. +
  27779. + /* todo: get_user()? */
  27780. + err = copy_from_user(&rdu, p, sizeof(rdu));
  27781. + if (unlikely(err)) {
  27782. + err = -EFAULT;
  27783. + AuTraceErr(err);
  27784. + goto out;
  27785. + }
  27786. + rdu.ent.e = compat_ptr(rdu.ent.ul);
  27787. + err = au_rdu_verify(&rdu);
  27788. + if (unlikely(err))
  27789. + goto out;
  27790. +
  27791. + switch (cmd) {
  27792. + case AUFS_CTL_RDU:
  27793. + err = au_rdu(file, &rdu);
  27794. + if (unlikely(err))
  27795. + break;
  27796. +
  27797. + rdu.ent.ul = ptr_to_compat(rdu.ent.e);
  27798. + rdu.tail.ul = ptr_to_compat(rdu.tail.e);
  27799. + e = copy_to_user(p, &rdu, sizeof(rdu));
  27800. + if (unlikely(e)) {
  27801. + err = -EFAULT;
  27802. + AuTraceErr(err);
  27803. + }
  27804. + break;
  27805. + case AUFS_CTL_RDU_INO:
  27806. + err = au_rdu_ino(file, &rdu);
  27807. + break;
  27808. +
  27809. + default:
  27810. + /* err = -ENOTTY; */
  27811. + err = -EINVAL;
  27812. + }
  27813. +
  27814. +out:
  27815. + AuTraceErr(err);
  27816. + return err;
  27817. +}
  27818. +#endif
  27819. --- /dev/null
  27820. +++ linux-4.19/fs/aufs/rwsem.h 2018-11-20 09:35:15.040195505 +0200
  27821. @@ -0,0 +1,73 @@
  27822. +/* SPDX-License-Identifier: GPL-2.0 */
  27823. +/*
  27824. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  27825. + *
  27826. + * This program, aufs is free software; you can redistribute it and/or modify
  27827. + * it under the terms of the GNU General Public License as published by
  27828. + * the Free Software Foundation; either version 2 of the License, or
  27829. + * (at your option) any later version.
  27830. + *
  27831. + * This program is distributed in the hope that it will be useful,
  27832. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27833. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27834. + * GNU General Public License for more details.
  27835. + *
  27836. + * You should have received a copy of the GNU General Public License
  27837. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27838. + */
  27839. +
  27840. +/*
  27841. + * simple read-write semaphore wrappers
  27842. + */
  27843. +
  27844. +#ifndef __AUFS_RWSEM_H__
  27845. +#define __AUFS_RWSEM_H__
  27846. +
  27847. +#ifdef __KERNEL__
  27848. +
  27849. +#include "debug.h"
  27850. +
  27851. +/* in the future, the name 'au_rwsem' will be totally gone */
  27852. +#define au_rwsem rw_semaphore
  27853. +
  27854. +/* to debug easier, do not make them inlined functions */
  27855. +#define AuRwMustNoWaiters(rw) AuDebugOn(rwsem_is_contended(rw))
  27856. +/* rwsem_is_locked() is unusable */
  27857. +#define AuRwMustReadLock(rw) AuDebugOn(!lockdep_recursing(current) \
  27858. + && debug_locks \
  27859. + && !lockdep_is_held_type(rw, 1))
  27860. +#define AuRwMustWriteLock(rw) AuDebugOn(!lockdep_recursing(current) \
  27861. + && debug_locks \
  27862. + && !lockdep_is_held_type(rw, 0))
  27863. +#define AuRwMustAnyLock(rw) AuDebugOn(!lockdep_recursing(current) \
  27864. + && debug_locks \
  27865. + && !lockdep_is_held(rw))
  27866. +#define AuRwDestroy(rw) AuDebugOn(!lockdep_recursing(current) \
  27867. + && debug_locks \
  27868. + && lockdep_is_held(rw))
  27869. +
  27870. +#define au_rw_init(rw) init_rwsem(rw)
  27871. +
  27872. +#define au_rw_init_wlock(rw) do { \
  27873. + au_rw_init(rw); \
  27874. + down_write(rw); \
  27875. + } while (0)
  27876. +
  27877. +#define au_rw_init_wlock_nested(rw, lsc) do { \
  27878. + au_rw_init(rw); \
  27879. + down_write_nested(rw, lsc); \
  27880. + } while (0)
  27881. +
  27882. +#define au_rw_read_lock(rw) down_read(rw)
  27883. +#define au_rw_read_lock_nested(rw, lsc) down_read_nested(rw, lsc)
  27884. +#define au_rw_read_unlock(rw) up_read(rw)
  27885. +#define au_rw_dgrade_lock(rw) downgrade_write(rw)
  27886. +#define au_rw_write_lock(rw) down_write(rw)
  27887. +#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
  27888. +#define au_rw_write_unlock(rw) up_write(rw)
  27889. +/* why is not _nested version defined? */
  27890. +#define au_rw_read_trylock(rw) down_read_trylock(rw)
  27891. +#define au_rw_write_trylock(rw) down_write_trylock(rw)
  27892. +
  27893. +#endif /* __KERNEL__ */
  27894. +#endif /* __AUFS_RWSEM_H__ */
  27895. --- /dev/null
  27896. +++ linux-4.19/fs/aufs/sbinfo.c 2018-11-20 09:35:15.040195505 +0200
  27897. @@ -0,0 +1,313 @@
  27898. +// SPDX-License-Identifier: GPL-2.0
  27899. +/*
  27900. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  27901. + *
  27902. + * This program, aufs is free software; you can redistribute it and/or modify
  27903. + * it under the terms of the GNU General Public License as published by
  27904. + * the Free Software Foundation; either version 2 of the License, or
  27905. + * (at your option) any later version.
  27906. + *
  27907. + * This program is distributed in the hope that it will be useful,
  27908. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27909. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27910. + * GNU General Public License for more details.
  27911. + *
  27912. + * You should have received a copy of the GNU General Public License
  27913. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27914. + */
  27915. +
  27916. +/*
  27917. + * superblock private data
  27918. + */
  27919. +
  27920. +#include "aufs.h"
  27921. +
  27922. +/*
  27923. + * they are necessary regardless sysfs is disabled.
  27924. + */
  27925. +void au_si_free(struct kobject *kobj)
  27926. +{
  27927. + int i;
  27928. + struct au_sbinfo *sbinfo;
  27929. + char *locked __maybe_unused; /* debug only */
  27930. +
  27931. + sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
  27932. + for (i = 0; i < AuPlink_NHASH; i++)
  27933. + AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
  27934. + AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
  27935. +
  27936. + AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
  27937. + au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
  27938. + AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
  27939. + au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
  27940. +
  27941. + dbgaufs_si_fin(sbinfo);
  27942. + au_rw_write_lock(&sbinfo->si_rwsem);
  27943. + au_br_free(sbinfo);
  27944. + au_rw_write_unlock(&sbinfo->si_rwsem);
  27945. +
  27946. + kfree(sbinfo->si_branch);
  27947. + mutex_destroy(&sbinfo->si_xib_mtx);
  27948. + AuRwDestroy(&sbinfo->si_rwsem);
  27949. +
  27950. + au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
  27951. + /* si_nfiles is waited too */
  27952. + kfree(sbinfo);
  27953. +}
  27954. +
  27955. +int au_si_alloc(struct super_block *sb)
  27956. +{
  27957. + int err, i;
  27958. + struct au_sbinfo *sbinfo;
  27959. +
  27960. + err = -ENOMEM;
  27961. + sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
  27962. + if (unlikely(!sbinfo))
  27963. + goto out;
  27964. +
  27965. + /* will be reallocated separately */
  27966. + sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
  27967. + if (unlikely(!sbinfo->si_branch))
  27968. + goto out_sbinfo;
  27969. +
  27970. + err = sysaufs_si_init(sbinfo);
  27971. + if (!err) {
  27972. + dbgaufs_si_null(sbinfo);
  27973. + err = dbgaufs_si_init(sbinfo);
  27974. + if (unlikely(err))
  27975. + kobject_put(&sbinfo->si_kobj);
  27976. + }
  27977. + if (unlikely(err))
  27978. + goto out_br;
  27979. +
  27980. + au_nwt_init(&sbinfo->si_nowait);
  27981. + au_rw_init_wlock(&sbinfo->si_rwsem);
  27982. +
  27983. + au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
  27984. + au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
  27985. +
  27986. + sbinfo->si_bbot = -1;
  27987. + sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
  27988. +
  27989. + sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
  27990. + sbinfo->si_wbr_create = AuWbrCreate_Def;
  27991. + sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
  27992. + sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
  27993. +
  27994. + au_fhsm_init(sbinfo);
  27995. +
  27996. + sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
  27997. +
  27998. + sbinfo->si_xino_jiffy = jiffies;
  27999. + sbinfo->si_xino_expire
  28000. + = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
  28001. + mutex_init(&sbinfo->si_xib_mtx);
  28002. + /* leave si_xib_last_pindex and si_xib_next_bit */
  28003. +
  28004. + INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
  28005. +
  28006. + sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
  28007. + sbinfo->si_rdblk = AUFS_RDBLK_DEF;
  28008. + sbinfo->si_rdhash = AUFS_RDHASH_DEF;
  28009. + sbinfo->si_dirwh = AUFS_DIRWH_DEF;
  28010. +
  28011. + for (i = 0; i < AuPlink_NHASH; i++)
  28012. + INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
  28013. + init_waitqueue_head(&sbinfo->si_plink_wq);
  28014. + spin_lock_init(&sbinfo->si_plink_maint_lock);
  28015. +
  28016. + INIT_HLIST_BL_HEAD(&sbinfo->si_files);
  28017. +
  28018. + /* with getattr by default */
  28019. + sbinfo->si_iop_array = aufs_iop;
  28020. +
  28021. + /* leave other members for sysaufs and si_mnt. */
  28022. + sbinfo->si_sb = sb;
  28023. + sb->s_fs_info = sbinfo;
  28024. + si_pid_set(sb);
  28025. + return 0; /* success */
  28026. +
  28027. +out_br:
  28028. + kfree(sbinfo->si_branch);
  28029. +out_sbinfo:
  28030. + kfree(sbinfo);
  28031. +out:
  28032. + return err;
  28033. +}
  28034. +
  28035. +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
  28036. +{
  28037. + int err, sz;
  28038. + struct au_branch **brp;
  28039. +
  28040. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  28041. +
  28042. + err = -ENOMEM;
  28043. + sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
  28044. + if (unlikely(!sz))
  28045. + sz = sizeof(*brp);
  28046. + brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
  28047. + may_shrink);
  28048. + if (brp) {
  28049. + sbinfo->si_branch = brp;
  28050. + err = 0;
  28051. + }
  28052. +
  28053. + return err;
  28054. +}
  28055. +
  28056. +/* ---------------------------------------------------------------------- */
  28057. +
  28058. +unsigned int au_sigen_inc(struct super_block *sb)
  28059. +{
  28060. + unsigned int gen;
  28061. + struct inode *inode;
  28062. +
  28063. + SiMustWriteLock(sb);
  28064. +
  28065. + gen = ++au_sbi(sb)->si_generation;
  28066. + au_update_digen(sb->s_root);
  28067. + inode = d_inode(sb->s_root);
  28068. + au_update_iigen(inode, /*half*/0);
  28069. + inode_inc_iversion(inode);
  28070. + return gen;
  28071. +}
  28072. +
  28073. +aufs_bindex_t au_new_br_id(struct super_block *sb)
  28074. +{
  28075. + aufs_bindex_t br_id;
  28076. + int i;
  28077. + struct au_sbinfo *sbinfo;
  28078. +
  28079. + SiMustWriteLock(sb);
  28080. +
  28081. + sbinfo = au_sbi(sb);
  28082. + for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
  28083. + br_id = ++sbinfo->si_last_br_id;
  28084. + AuDebugOn(br_id < 0);
  28085. + if (br_id && au_br_index(sb, br_id) < 0)
  28086. + return br_id;
  28087. + }
  28088. +
  28089. + return -1;
  28090. +}
  28091. +
  28092. +/* ---------------------------------------------------------------------- */
  28093. +
  28094. +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
  28095. +int si_read_lock(struct super_block *sb, int flags)
  28096. +{
  28097. + int err;
  28098. +
  28099. + err = 0;
  28100. + if (au_ftest_lock(flags, FLUSH))
  28101. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  28102. +
  28103. + si_noflush_read_lock(sb);
  28104. + err = au_plink_maint(sb, flags);
  28105. + if (unlikely(err))
  28106. + si_read_unlock(sb);
  28107. +
  28108. + return err;
  28109. +}
  28110. +
  28111. +int si_write_lock(struct super_block *sb, int flags)
  28112. +{
  28113. + int err;
  28114. +
  28115. + if (au_ftest_lock(flags, FLUSH))
  28116. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  28117. +
  28118. + si_noflush_write_lock(sb);
  28119. + err = au_plink_maint(sb, flags);
  28120. + if (unlikely(err))
  28121. + si_write_unlock(sb);
  28122. +
  28123. + return err;
  28124. +}
  28125. +
  28126. +/* dentry and super_block lock. call at entry point */
  28127. +int aufs_read_lock(struct dentry *dentry, int flags)
  28128. +{
  28129. + int err;
  28130. + struct super_block *sb;
  28131. +
  28132. + sb = dentry->d_sb;
  28133. + err = si_read_lock(sb, flags);
  28134. + if (unlikely(err))
  28135. + goto out;
  28136. +
  28137. + if (au_ftest_lock(flags, DW))
  28138. + di_write_lock_child(dentry);
  28139. + else
  28140. + di_read_lock_child(dentry, flags);
  28141. +
  28142. + if (au_ftest_lock(flags, GEN)) {
  28143. + err = au_digen_test(dentry, au_sigen(sb));
  28144. + if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
  28145. + AuDebugOn(!err && au_dbrange_test(dentry));
  28146. + else if (!err)
  28147. + err = au_dbrange_test(dentry);
  28148. + if (unlikely(err))
  28149. + aufs_read_unlock(dentry, flags);
  28150. + }
  28151. +
  28152. +out:
  28153. + return err;
  28154. +}
  28155. +
  28156. +void aufs_read_unlock(struct dentry *dentry, int flags)
  28157. +{
  28158. + if (au_ftest_lock(flags, DW))
  28159. + di_write_unlock(dentry);
  28160. + else
  28161. + di_read_unlock(dentry, flags);
  28162. + si_read_unlock(dentry->d_sb);
  28163. +}
  28164. +
  28165. +void aufs_write_lock(struct dentry *dentry)
  28166. +{
  28167. + si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
  28168. + di_write_lock_child(dentry);
  28169. +}
  28170. +
  28171. +void aufs_write_unlock(struct dentry *dentry)
  28172. +{
  28173. + di_write_unlock(dentry);
  28174. + si_write_unlock(dentry->d_sb);
  28175. +}
  28176. +
  28177. +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
  28178. +{
  28179. + int err;
  28180. + unsigned int sigen;
  28181. + struct super_block *sb;
  28182. +
  28183. + sb = d1->d_sb;
  28184. + err = si_read_lock(sb, flags);
  28185. + if (unlikely(err))
  28186. + goto out;
  28187. +
  28188. + di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
  28189. +
  28190. + if (au_ftest_lock(flags, GEN)) {
  28191. + sigen = au_sigen(sb);
  28192. + err = au_digen_test(d1, sigen);
  28193. + AuDebugOn(!err && au_dbrange_test(d1));
  28194. + if (!err) {
  28195. + err = au_digen_test(d2, sigen);
  28196. + AuDebugOn(!err && au_dbrange_test(d2));
  28197. + }
  28198. + if (unlikely(err))
  28199. + aufs_read_and_write_unlock2(d1, d2);
  28200. + }
  28201. +
  28202. +out:
  28203. + return err;
  28204. +}
  28205. +
  28206. +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
  28207. +{
  28208. + di_write_unlock2(d1, d2);
  28209. + si_read_unlock(d1->d_sb);
  28210. +}
  28211. --- /dev/null
  28212. +++ linux-4.19/fs/aufs/super.c 2018-11-20 09:35:15.042195505 +0200
  28213. @@ -0,0 +1,1048 @@
  28214. +// SPDX-License-Identifier: GPL-2.0
  28215. +/*
  28216. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  28217. + *
  28218. + * This program, aufs is free software; you can redistribute it and/or modify
  28219. + * it under the terms of the GNU General Public License as published by
  28220. + * the Free Software Foundation; either version 2 of the License, or
  28221. + * (at your option) any later version.
  28222. + *
  28223. + * This program is distributed in the hope that it will be useful,
  28224. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28225. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28226. + * GNU General Public License for more details.
  28227. + *
  28228. + * You should have received a copy of the GNU General Public License
  28229. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28230. + */
  28231. +
  28232. +/*
  28233. + * mount and super_block operations
  28234. + */
  28235. +
  28236. +#include <linux/mm.h>
  28237. +#include <linux/seq_file.h>
  28238. +#include <linux/statfs.h>
  28239. +#include <linux/vmalloc.h>
  28240. +#include "aufs.h"
  28241. +
  28242. +/*
  28243. + * super_operations
  28244. + */
  28245. +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
  28246. +{
  28247. + struct au_icntnr *c;
  28248. +
  28249. + c = au_cache_alloc_icntnr();
  28250. + if (c) {
  28251. + au_icntnr_init(c);
  28252. + inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
  28253. + c->iinfo.ii_hinode = NULL;
  28254. + return &c->vfs_inode;
  28255. + }
  28256. + return NULL;
  28257. +}
  28258. +
  28259. +static void aufs_destroy_inode_cb(struct rcu_head *head)
  28260. +{
  28261. + struct inode *inode = container_of(head, struct inode, i_rcu);
  28262. +
  28263. + au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
  28264. +}
  28265. +
  28266. +static void aufs_destroy_inode(struct inode *inode)
  28267. +{
  28268. + if (!au_is_bad_inode(inode))
  28269. + au_iinfo_fin(inode);
  28270. + call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
  28271. +}
  28272. +
  28273. +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
  28274. +{
  28275. + struct inode *inode;
  28276. + int err;
  28277. +
  28278. + inode = iget_locked(sb, ino);
  28279. + if (unlikely(!inode)) {
  28280. + inode = ERR_PTR(-ENOMEM);
  28281. + goto out;
  28282. + }
  28283. + if (!(inode->i_state & I_NEW))
  28284. + goto out;
  28285. +
  28286. + err = au_xigen_new(inode);
  28287. + if (!err)
  28288. + err = au_iinfo_init(inode);
  28289. + if (!err)
  28290. + inode_inc_iversion(inode);
  28291. + else {
  28292. + iget_failed(inode);
  28293. + inode = ERR_PTR(err);
  28294. + }
  28295. +
  28296. +out:
  28297. + /* never return NULL */
  28298. + AuDebugOn(!inode);
  28299. + AuTraceErrPtr(inode);
  28300. + return inode;
  28301. +}
  28302. +
  28303. +/* lock free root dinfo */
  28304. +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
  28305. +{
  28306. + int err;
  28307. + aufs_bindex_t bindex, bbot;
  28308. + struct path path;
  28309. + struct au_hdentry *hdp;
  28310. + struct au_branch *br;
  28311. + au_br_perm_str_t perm;
  28312. +
  28313. + err = 0;
  28314. + bbot = au_sbbot(sb);
  28315. + bindex = 0;
  28316. + hdp = au_hdentry(au_di(sb->s_root), bindex);
  28317. + for (; !err && bindex <= bbot; bindex++, hdp++) {
  28318. + br = au_sbr(sb, bindex);
  28319. + path.mnt = au_br_mnt(br);
  28320. + path.dentry = hdp->hd_dentry;
  28321. + err = au_seq_path(seq, &path);
  28322. + if (!err) {
  28323. + au_optstr_br_perm(&perm, br->br_perm);
  28324. + seq_printf(seq, "=%s", perm.a);
  28325. + if (bindex != bbot)
  28326. + seq_putc(seq, ':');
  28327. + }
  28328. + }
  28329. + if (unlikely(err || seq_has_overflowed(seq)))
  28330. + err = -E2BIG;
  28331. +
  28332. + return err;
  28333. +}
  28334. +
  28335. +static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
  28336. + const char *append)
  28337. +{
  28338. + char *p;
  28339. +
  28340. + p = fmt;
  28341. + while (*pat != ':')
  28342. + *p++ = *pat++;
  28343. + *p++ = *pat++;
  28344. + strcpy(p, append);
  28345. + AuDebugOn(strlen(fmt) >= len);
  28346. +}
  28347. +
  28348. +static void au_show_wbr_create(struct seq_file *m, int v,
  28349. + struct au_sbinfo *sbinfo)
  28350. +{
  28351. + const char *pat;
  28352. + char fmt[32];
  28353. + struct au_wbr_mfs *mfs;
  28354. +
  28355. + AuRwMustAnyLock(&sbinfo->si_rwsem);
  28356. +
  28357. + seq_puts(m, ",create=");
  28358. + pat = au_optstr_wbr_create(v);
  28359. + mfs = &sbinfo->si_wbr_mfs;
  28360. + switch (v) {
  28361. + case AuWbrCreate_TDP:
  28362. + case AuWbrCreate_RR:
  28363. + case AuWbrCreate_MFS:
  28364. + case AuWbrCreate_PMFS:
  28365. + seq_puts(m, pat);
  28366. + break;
  28367. + case AuWbrCreate_MFSRR:
  28368. + case AuWbrCreate_TDMFS:
  28369. + case AuWbrCreate_PMFSRR:
  28370. + au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
  28371. + seq_printf(m, fmt, mfs->mfsrr_watermark);
  28372. + break;
  28373. + case AuWbrCreate_MFSV:
  28374. + case AuWbrCreate_PMFSV:
  28375. + au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
  28376. + seq_printf(m, fmt,
  28377. + jiffies_to_msecs(mfs->mfs_expire)
  28378. + / MSEC_PER_SEC);
  28379. + break;
  28380. + case AuWbrCreate_MFSRRV:
  28381. + case AuWbrCreate_TDMFSV:
  28382. + case AuWbrCreate_PMFSRRV:
  28383. + au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
  28384. + seq_printf(m, fmt, mfs->mfsrr_watermark,
  28385. + jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
  28386. + break;
  28387. + default:
  28388. + BUG();
  28389. + }
  28390. +}
  28391. +
  28392. +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
  28393. +{
  28394. +#ifdef CONFIG_SYSFS
  28395. + return 0;
  28396. +#else
  28397. + int err;
  28398. + const int len = sizeof(AUFS_XINO_FNAME) - 1;
  28399. + aufs_bindex_t bindex, brid;
  28400. + struct qstr *name;
  28401. + struct file *f;
  28402. + struct dentry *d, *h_root;
  28403. + struct au_branch *br;
  28404. +
  28405. + AuRwMustAnyLock(&sbinfo->si_rwsem);
  28406. +
  28407. + err = 0;
  28408. + f = au_sbi(sb)->si_xib;
  28409. + if (!f)
  28410. + goto out;
  28411. +
  28412. + /* stop printing the default xino path on the first writable branch */
  28413. + h_root = NULL;
  28414. + bindex = au_xi_root(sb, f->f_path.dentry);
  28415. + if (bindex >= 0) {
  28416. + br = au_sbr_sb(sb, bindex);
  28417. + h_root = au_br_dentry(br);
  28418. + }
  28419. +
  28420. + d = f->f_path.dentry;
  28421. + name = &d->d_name;
  28422. + /* safe ->d_parent because the file is unlinked */
  28423. + if (d->d_parent == h_root
  28424. + && name->len == len
  28425. + && !memcmp(name->name, AUFS_XINO_FNAME, len))
  28426. + goto out;
  28427. +
  28428. + seq_puts(seq, ",xino=");
  28429. + err = au_xino_path(seq, f);
  28430. +
  28431. +out:
  28432. + return err;
  28433. +#endif
  28434. +}
  28435. +
  28436. +/* seq_file will re-call me in case of too long string */
  28437. +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
  28438. +{
  28439. + int err;
  28440. + unsigned int mnt_flags, v;
  28441. + struct super_block *sb;
  28442. + struct au_sbinfo *sbinfo;
  28443. +
  28444. +#define AuBool(name, str) do { \
  28445. + v = au_opt_test(mnt_flags, name); \
  28446. + if (v != au_opt_test(AuOpt_Def, name)) \
  28447. + seq_printf(m, ",%s" #str, v ? "" : "no"); \
  28448. +} while (0)
  28449. +
  28450. +#define AuStr(name, str) do { \
  28451. + v = mnt_flags & AuOptMask_##name; \
  28452. + if (v != (AuOpt_Def & AuOptMask_##name)) \
  28453. + seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
  28454. +} while (0)
  28455. +
  28456. +#define AuUInt(name, str, val) do { \
  28457. + if (val != AUFS_##name##_DEF) \
  28458. + seq_printf(m, "," #str "=%u", val); \
  28459. +} while (0)
  28460. +
  28461. + sb = dentry->d_sb;
  28462. + if (sb->s_flags & SB_POSIXACL)
  28463. + seq_puts(m, ",acl");
  28464. +#if 0
  28465. + if (sb->s_flags & SB_I_VERSION)
  28466. + seq_puts(m, ",i_version");
  28467. +#endif
  28468. +
  28469. + /* lock free root dinfo */
  28470. + si_noflush_read_lock(sb);
  28471. + sbinfo = au_sbi(sb);
  28472. + seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
  28473. +
  28474. + mnt_flags = au_mntflags(sb);
  28475. + if (au_opt_test(mnt_flags, XINO)) {
  28476. + err = au_show_xino(m, sb);
  28477. + if (unlikely(err))
  28478. + goto out;
  28479. + } else
  28480. + seq_puts(m, ",noxino");
  28481. +
  28482. + AuBool(TRUNC_XINO, trunc_xino);
  28483. + AuStr(UDBA, udba);
  28484. + AuBool(SHWH, shwh);
  28485. + AuBool(PLINK, plink);
  28486. + AuBool(DIO, dio);
  28487. + AuBool(DIRPERM1, dirperm1);
  28488. +
  28489. + v = sbinfo->si_wbr_create;
  28490. + if (v != AuWbrCreate_Def)
  28491. + au_show_wbr_create(m, v, sbinfo);
  28492. +
  28493. + v = sbinfo->si_wbr_copyup;
  28494. + if (v != AuWbrCopyup_Def)
  28495. + seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
  28496. +
  28497. + v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
  28498. + if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
  28499. + seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
  28500. +
  28501. + AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
  28502. +
  28503. + v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
  28504. + AuUInt(RDCACHE, rdcache, v);
  28505. +
  28506. + AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
  28507. + AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
  28508. +
  28509. + au_fhsm_show(m, sbinfo);
  28510. +
  28511. + AuBool(DIRREN, dirren);
  28512. + AuBool(SUM, sum);
  28513. + /* AuBool(SUM_W, wsum); */
  28514. + AuBool(WARN_PERM, warn_perm);
  28515. + AuBool(VERBOSE, verbose);
  28516. +
  28517. +out:
  28518. + /* be sure to print "br:" last */
  28519. + if (!sysaufs_brs) {
  28520. + seq_puts(m, ",br:");
  28521. + au_show_brs(m, sb);
  28522. + }
  28523. + si_read_unlock(sb);
  28524. + return 0;
  28525. +
  28526. +#undef AuBool
  28527. +#undef AuStr
  28528. +#undef AuUInt
  28529. +}
  28530. +
  28531. +/* ---------------------------------------------------------------------- */
  28532. +
  28533. +/* sum mode which returns the summation for statfs(2) */
  28534. +
  28535. +static u64 au_add_till_max(u64 a, u64 b)
  28536. +{
  28537. + u64 old;
  28538. +
  28539. + old = a;
  28540. + a += b;
  28541. + if (old <= a)
  28542. + return a;
  28543. + return ULLONG_MAX;
  28544. +}
  28545. +
  28546. +static u64 au_mul_till_max(u64 a, long mul)
  28547. +{
  28548. + u64 old;
  28549. +
  28550. + old = a;
  28551. + a *= mul;
  28552. + if (old <= a)
  28553. + return a;
  28554. + return ULLONG_MAX;
  28555. +}
  28556. +
  28557. +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
  28558. +{
  28559. + int err;
  28560. + long bsize, factor;
  28561. + u64 blocks, bfree, bavail, files, ffree;
  28562. + aufs_bindex_t bbot, bindex, i;
  28563. + unsigned char shared;
  28564. + struct path h_path;
  28565. + struct super_block *h_sb;
  28566. +
  28567. + err = 0;
  28568. + bsize = LONG_MAX;
  28569. + files = 0;
  28570. + ffree = 0;
  28571. + blocks = 0;
  28572. + bfree = 0;
  28573. + bavail = 0;
  28574. + bbot = au_sbbot(sb);
  28575. + for (bindex = 0; bindex <= bbot; bindex++) {
  28576. + h_path.mnt = au_sbr_mnt(sb, bindex);
  28577. + h_sb = h_path.mnt->mnt_sb;
  28578. + shared = 0;
  28579. + for (i = 0; !shared && i < bindex; i++)
  28580. + shared = (au_sbr_sb(sb, i) == h_sb);
  28581. + if (shared)
  28582. + continue;
  28583. +
  28584. + /* sb->s_root for NFS is unreliable */
  28585. + h_path.dentry = h_path.mnt->mnt_root;
  28586. + err = vfs_statfs(&h_path, buf);
  28587. + if (unlikely(err))
  28588. + goto out;
  28589. +
  28590. + if (bsize > buf->f_bsize) {
  28591. + /*
  28592. + * we will reduce bsize, so we have to expand blocks
  28593. + * etc. to match them again
  28594. + */
  28595. + factor = (bsize / buf->f_bsize);
  28596. + blocks = au_mul_till_max(blocks, factor);
  28597. + bfree = au_mul_till_max(bfree, factor);
  28598. + bavail = au_mul_till_max(bavail, factor);
  28599. + bsize = buf->f_bsize;
  28600. + }
  28601. +
  28602. + factor = (buf->f_bsize / bsize);
  28603. + blocks = au_add_till_max(blocks,
  28604. + au_mul_till_max(buf->f_blocks, factor));
  28605. + bfree = au_add_till_max(bfree,
  28606. + au_mul_till_max(buf->f_bfree, factor));
  28607. + bavail = au_add_till_max(bavail,
  28608. + au_mul_till_max(buf->f_bavail, factor));
  28609. + files = au_add_till_max(files, buf->f_files);
  28610. + ffree = au_add_till_max(ffree, buf->f_ffree);
  28611. + }
  28612. +
  28613. + buf->f_bsize = bsize;
  28614. + buf->f_blocks = blocks;
  28615. + buf->f_bfree = bfree;
  28616. + buf->f_bavail = bavail;
  28617. + buf->f_files = files;
  28618. + buf->f_ffree = ffree;
  28619. + buf->f_frsize = 0;
  28620. +
  28621. +out:
  28622. + return err;
  28623. +}
  28624. +
  28625. +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
  28626. +{
  28627. + int err;
  28628. + struct path h_path;
  28629. + struct super_block *sb;
  28630. +
  28631. + /* lock free root dinfo */
  28632. + sb = dentry->d_sb;
  28633. + si_noflush_read_lock(sb);
  28634. + if (!au_opt_test(au_mntflags(sb), SUM)) {
  28635. + /* sb->s_root for NFS is unreliable */
  28636. + h_path.mnt = au_sbr_mnt(sb, 0);
  28637. + h_path.dentry = h_path.mnt->mnt_root;
  28638. + err = vfs_statfs(&h_path, buf);
  28639. + } else
  28640. + err = au_statfs_sum(sb, buf);
  28641. + si_read_unlock(sb);
  28642. +
  28643. + if (!err) {
  28644. + buf->f_type = AUFS_SUPER_MAGIC;
  28645. + buf->f_namelen = AUFS_MAX_NAMELEN;
  28646. + memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
  28647. + }
  28648. + /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
  28649. +
  28650. + return err;
  28651. +}
  28652. +
  28653. +/* ---------------------------------------------------------------------- */
  28654. +
  28655. +static int aufs_sync_fs(struct super_block *sb, int wait)
  28656. +{
  28657. + int err, e;
  28658. + aufs_bindex_t bbot, bindex;
  28659. + struct au_branch *br;
  28660. + struct super_block *h_sb;
  28661. +
  28662. + err = 0;
  28663. + si_noflush_read_lock(sb);
  28664. + bbot = au_sbbot(sb);
  28665. + for (bindex = 0; bindex <= bbot; bindex++) {
  28666. + br = au_sbr(sb, bindex);
  28667. + if (!au_br_writable(br->br_perm))
  28668. + continue;
  28669. +
  28670. + h_sb = au_sbr_sb(sb, bindex);
  28671. + e = vfsub_sync_filesystem(h_sb, wait);
  28672. + if (unlikely(e && !err))
  28673. + err = e;
  28674. + /* go on even if an error happens */
  28675. + }
  28676. + si_read_unlock(sb);
  28677. +
  28678. + return err;
  28679. +}
  28680. +
  28681. +/* ---------------------------------------------------------------------- */
  28682. +
  28683. +/* final actions when unmounting a file system */
  28684. +static void aufs_put_super(struct super_block *sb)
  28685. +{
  28686. + struct au_sbinfo *sbinfo;
  28687. +
  28688. + sbinfo = au_sbi(sb);
  28689. + if (sbinfo)
  28690. + kobject_put(&sbinfo->si_kobj);
  28691. +}
  28692. +
  28693. +/* ---------------------------------------------------------------------- */
  28694. +
  28695. +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
  28696. + struct super_block *sb, void *arg)
  28697. +{
  28698. + void *array;
  28699. + unsigned long long n, sz;
  28700. +
  28701. + array = NULL;
  28702. + n = 0;
  28703. + if (!*hint)
  28704. + goto out;
  28705. +
  28706. + if (*hint > ULLONG_MAX / sizeof(array)) {
  28707. + array = ERR_PTR(-EMFILE);
  28708. + pr_err("hint %llu\n", *hint);
  28709. + goto out;
  28710. + }
  28711. +
  28712. + sz = sizeof(array) * *hint;
  28713. + array = kzalloc(sz, GFP_NOFS);
  28714. + if (unlikely(!array))
  28715. + array = vzalloc(sz);
  28716. + if (unlikely(!array)) {
  28717. + array = ERR_PTR(-ENOMEM);
  28718. + goto out;
  28719. + }
  28720. +
  28721. + n = cb(sb, array, *hint, arg);
  28722. + AuDebugOn(n > *hint);
  28723. +
  28724. +out:
  28725. + *hint = n;
  28726. + return array;
  28727. +}
  28728. +
  28729. +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
  28730. + unsigned long long max __maybe_unused,
  28731. + void *arg)
  28732. +{
  28733. + unsigned long long n;
  28734. + struct inode **p, *inode;
  28735. + struct list_head *head;
  28736. +
  28737. + n = 0;
  28738. + p = a;
  28739. + head = arg;
  28740. + spin_lock(&sb->s_inode_list_lock);
  28741. + list_for_each_entry(inode, head, i_sb_list) {
  28742. + if (!au_is_bad_inode(inode)
  28743. + && au_ii(inode)->ii_btop >= 0) {
  28744. + spin_lock(&inode->i_lock);
  28745. + if (atomic_read(&inode->i_count)) {
  28746. + au_igrab(inode);
  28747. + *p++ = inode;
  28748. + n++;
  28749. + AuDebugOn(n > max);
  28750. + }
  28751. + spin_unlock(&inode->i_lock);
  28752. + }
  28753. + }
  28754. + spin_unlock(&sb->s_inode_list_lock);
  28755. +
  28756. + return n;
  28757. +}
  28758. +
  28759. +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
  28760. +{
  28761. + struct au_sbinfo *sbi;
  28762. +
  28763. + sbi = au_sbi(sb);
  28764. + *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
  28765. + return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
  28766. +}
  28767. +
  28768. +void au_iarray_free(struct inode **a, unsigned long long max)
  28769. +{
  28770. + unsigned long long ull;
  28771. +
  28772. + for (ull = 0; ull < max; ull++)
  28773. + iput(a[ull]);
  28774. + kvfree(a);
  28775. +}
  28776. +
  28777. +/* ---------------------------------------------------------------------- */
  28778. +
  28779. +/*
  28780. + * refresh dentry and inode at remount time.
  28781. + */
  28782. +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
  28783. +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
  28784. + struct dentry *parent)
  28785. +{
  28786. + int err;
  28787. +
  28788. + di_write_lock_child(dentry);
  28789. + di_read_lock_parent(parent, AuLock_IR);
  28790. + err = au_refresh_dentry(dentry, parent);
  28791. + if (!err && dir_flags)
  28792. + au_hn_reset(d_inode(dentry), dir_flags);
  28793. + di_read_unlock(parent, AuLock_IR);
  28794. + di_write_unlock(dentry);
  28795. +
  28796. + return err;
  28797. +}
  28798. +
  28799. +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
  28800. + struct au_sbinfo *sbinfo,
  28801. + const unsigned int dir_flags, unsigned int do_idop)
  28802. +{
  28803. + int err;
  28804. + struct dentry *parent;
  28805. +
  28806. + err = 0;
  28807. + parent = dget_parent(dentry);
  28808. + if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
  28809. + if (d_really_is_positive(dentry)) {
  28810. + if (!d_is_dir(dentry))
  28811. + err = au_do_refresh(dentry, /*dir_flags*/0,
  28812. + parent);
  28813. + else {
  28814. + err = au_do_refresh(dentry, dir_flags, parent);
  28815. + if (unlikely(err))
  28816. + au_fset_si(sbinfo, FAILED_REFRESH_DIR);
  28817. + }
  28818. + } else
  28819. + err = au_do_refresh(dentry, /*dir_flags*/0, parent);
  28820. + AuDbgDentry(dentry);
  28821. + }
  28822. + dput(parent);
  28823. +
  28824. + if (!err) {
  28825. + if (do_idop)
  28826. + au_refresh_dop(dentry, /*force_reval*/0);
  28827. + } else
  28828. + au_refresh_dop(dentry, /*force_reval*/1);
  28829. +
  28830. + AuTraceErr(err);
  28831. + return err;
  28832. +}
  28833. +
  28834. +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
  28835. +{
  28836. + int err, i, j, ndentry, e;
  28837. + unsigned int sigen;
  28838. + struct au_dcsub_pages dpages;
  28839. + struct au_dpage *dpage;
  28840. + struct dentry **dentries, *d;
  28841. + struct au_sbinfo *sbinfo;
  28842. + struct dentry *root = sb->s_root;
  28843. + const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
  28844. +
  28845. + if (do_idop)
  28846. + au_refresh_dop(root, /*force_reval*/0);
  28847. +
  28848. + err = au_dpages_init(&dpages, GFP_NOFS);
  28849. + if (unlikely(err))
  28850. + goto out;
  28851. + err = au_dcsub_pages(&dpages, root, NULL, NULL);
  28852. + if (unlikely(err))
  28853. + goto out_dpages;
  28854. +
  28855. + sigen = au_sigen(sb);
  28856. + sbinfo = au_sbi(sb);
  28857. + for (i = 0; i < dpages.ndpage; i++) {
  28858. + dpage = dpages.dpages + i;
  28859. + dentries = dpage->dentries;
  28860. + ndentry = dpage->ndentry;
  28861. + for (j = 0; j < ndentry; j++) {
  28862. + d = dentries[j];
  28863. + e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
  28864. + do_idop);
  28865. + if (unlikely(e && !err))
  28866. + err = e;
  28867. + /* go on even err */
  28868. + }
  28869. + }
  28870. +
  28871. +out_dpages:
  28872. + au_dpages_free(&dpages);
  28873. +out:
  28874. + return err;
  28875. +}
  28876. +
  28877. +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
  28878. +{
  28879. + int err, e;
  28880. + unsigned int sigen;
  28881. + unsigned long long max, ull;
  28882. + struct inode *inode, **array;
  28883. +
  28884. + array = au_iarray_alloc(sb, &max);
  28885. + err = PTR_ERR(array);
  28886. + if (IS_ERR(array))
  28887. + goto out;
  28888. +
  28889. + err = 0;
  28890. + sigen = au_sigen(sb);
  28891. + for (ull = 0; ull < max; ull++) {
  28892. + inode = array[ull];
  28893. + if (unlikely(!inode))
  28894. + break;
  28895. +
  28896. + e = 0;
  28897. + ii_write_lock_child(inode);
  28898. + if (au_iigen(inode, NULL) != sigen) {
  28899. + e = au_refresh_hinode_self(inode);
  28900. + if (unlikely(e)) {
  28901. + au_refresh_iop(inode, /*force_getattr*/1);
  28902. + pr_err("error %d, i%lu\n", e, inode->i_ino);
  28903. + if (!err)
  28904. + err = e;
  28905. + /* go on even if err */
  28906. + }
  28907. + }
  28908. + if (!e && do_idop)
  28909. + au_refresh_iop(inode, /*force_getattr*/0);
  28910. + ii_write_unlock(inode);
  28911. + }
  28912. +
  28913. + au_iarray_free(array, max);
  28914. +
  28915. +out:
  28916. + return err;
  28917. +}
  28918. +
  28919. +static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
  28920. +{
  28921. + int err, e;
  28922. + unsigned int udba;
  28923. + aufs_bindex_t bindex, bbot;
  28924. + struct dentry *root;
  28925. + struct inode *inode;
  28926. + struct au_branch *br;
  28927. + struct au_sbinfo *sbi;
  28928. +
  28929. + au_sigen_inc(sb);
  28930. + sbi = au_sbi(sb);
  28931. + au_fclr_si(sbi, FAILED_REFRESH_DIR);
  28932. +
  28933. + root = sb->s_root;
  28934. + DiMustNoWaiters(root);
  28935. + inode = d_inode(root);
  28936. + IiMustNoWaiters(inode);
  28937. +
  28938. + udba = au_opt_udba(sb);
  28939. + bbot = au_sbbot(sb);
  28940. + for (bindex = 0; bindex <= bbot; bindex++) {
  28941. + br = au_sbr(sb, bindex);
  28942. + err = au_hnotify_reset_br(udba, br, br->br_perm);
  28943. + if (unlikely(err))
  28944. + AuIOErr("hnotify failed on br %d, %d, ignored\n",
  28945. + bindex, err);
  28946. + /* go on even if err */
  28947. + }
  28948. + au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
  28949. +
  28950. + if (do_idop) {
  28951. + if (au_ftest_si(sbi, NO_DREVAL)) {
  28952. + AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
  28953. + sb->s_d_op = &aufs_dop_noreval;
  28954. + AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
  28955. + sbi->si_iop_array = aufs_iop_nogetattr;
  28956. + } else {
  28957. + AuDebugOn(sb->s_d_op == &aufs_dop);
  28958. + sb->s_d_op = &aufs_dop;
  28959. + AuDebugOn(sbi->si_iop_array == aufs_iop);
  28960. + sbi->si_iop_array = aufs_iop;
  28961. + }
  28962. + pr_info("reset to %ps and %ps\n",
  28963. + sb->s_d_op, sbi->si_iop_array);
  28964. + }
  28965. +
  28966. + di_write_unlock(root);
  28967. + err = au_refresh_d(sb, do_idop);
  28968. + e = au_refresh_i(sb, do_idop);
  28969. + if (unlikely(e && !err))
  28970. + err = e;
  28971. + /* aufs_write_lock() calls ..._child() */
  28972. + di_write_lock_child(root);
  28973. +
  28974. + au_cpup_attr_all(inode, /*force*/1);
  28975. +
  28976. + if (unlikely(err))
  28977. + AuIOErr("refresh failed, ignored, %d\n", err);
  28978. +}
  28979. +
  28980. +/* stop extra interpretation of errno in mount(8), and strange error messages */
  28981. +static int cvt_err(int err)
  28982. +{
  28983. + AuTraceErr(err);
  28984. +
  28985. + switch (err) {
  28986. + case -ENOENT:
  28987. + case -ENOTDIR:
  28988. + case -EEXIST:
  28989. + case -EIO:
  28990. + err = -EINVAL;
  28991. + }
  28992. + return err;
  28993. +}
  28994. +
  28995. +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
  28996. +{
  28997. + int err, do_dx;
  28998. + unsigned int mntflags;
  28999. + struct au_opts opts = {
  29000. + .opt = NULL
  29001. + };
  29002. + struct dentry *root;
  29003. + struct inode *inode;
  29004. + struct au_sbinfo *sbinfo;
  29005. +
  29006. + err = 0;
  29007. + root = sb->s_root;
  29008. + if (!data || !*data) {
  29009. + err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  29010. + if (!err) {
  29011. + di_write_lock_child(root);
  29012. + err = au_opts_verify(sb, *flags, /*pending*/0);
  29013. + aufs_write_unlock(root);
  29014. + }
  29015. + goto out;
  29016. + }
  29017. +
  29018. + err = -ENOMEM;
  29019. + opts.opt = (void *)__get_free_page(GFP_NOFS);
  29020. + if (unlikely(!opts.opt))
  29021. + goto out;
  29022. + opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
  29023. + opts.flags = AuOpts_REMOUNT;
  29024. + opts.sb_flags = *flags;
  29025. +
  29026. + /* parse it before aufs lock */
  29027. + err = au_opts_parse(sb, data, &opts);
  29028. + if (unlikely(err))
  29029. + goto out_opts;
  29030. +
  29031. + sbinfo = au_sbi(sb);
  29032. + inode = d_inode(root);
  29033. + inode_lock(inode);
  29034. + err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  29035. + if (unlikely(err))
  29036. + goto out_mtx;
  29037. + di_write_lock_child(root);
  29038. +
  29039. + /* au_opts_remount() may return an error */
  29040. + err = au_opts_remount(sb, &opts);
  29041. + au_opts_free(&opts);
  29042. +
  29043. + if (au_ftest_opts(opts.flags, REFRESH))
  29044. + au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
  29045. +
  29046. + if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
  29047. + mntflags = au_mntflags(sb);
  29048. + do_dx = !!au_opt_test(mntflags, DIO);
  29049. + au_dy_arefresh(do_dx);
  29050. + }
  29051. +
  29052. + au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
  29053. + aufs_write_unlock(root);
  29054. +
  29055. +out_mtx:
  29056. + inode_unlock(inode);
  29057. +out_opts:
  29058. + free_page((unsigned long)opts.opt);
  29059. +out:
  29060. + err = cvt_err(err);
  29061. + AuTraceErr(err);
  29062. + return err;
  29063. +}
  29064. +
  29065. +static const struct super_operations aufs_sop = {
  29066. + .alloc_inode = aufs_alloc_inode,
  29067. + .destroy_inode = aufs_destroy_inode,
  29068. + /* always deleting, no clearing */
  29069. + .drop_inode = generic_delete_inode,
  29070. + .show_options = aufs_show_options,
  29071. + .statfs = aufs_statfs,
  29072. + .put_super = aufs_put_super,
  29073. + .sync_fs = aufs_sync_fs,
  29074. + .remount_fs = aufs_remount_fs
  29075. +};
  29076. +
  29077. +/* ---------------------------------------------------------------------- */
  29078. +
  29079. +static int alloc_root(struct super_block *sb)
  29080. +{
  29081. + int err;
  29082. + struct inode *inode;
  29083. + struct dentry *root;
  29084. +
  29085. + err = -ENOMEM;
  29086. + inode = au_iget_locked(sb, AUFS_ROOT_INO);
  29087. + err = PTR_ERR(inode);
  29088. + if (IS_ERR(inode))
  29089. + goto out;
  29090. +
  29091. + inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
  29092. + inode->i_fop = &aufs_dir_fop;
  29093. + inode->i_mode = S_IFDIR;
  29094. + set_nlink(inode, 2);
  29095. + unlock_new_inode(inode);
  29096. +
  29097. + root = d_make_root(inode);
  29098. + if (unlikely(!root))
  29099. + goto out;
  29100. + err = PTR_ERR(root);
  29101. + if (IS_ERR(root))
  29102. + goto out;
  29103. +
  29104. + err = au_di_init(root);
  29105. + if (!err) {
  29106. + sb->s_root = root;
  29107. + return 0; /* success */
  29108. + }
  29109. + dput(root);
  29110. +
  29111. +out:
  29112. + return err;
  29113. +}
  29114. +
  29115. +static int aufs_fill_super(struct super_block *sb, void *raw_data,
  29116. + int silent __maybe_unused)
  29117. +{
  29118. + int err;
  29119. + struct au_opts opts = {
  29120. + .opt = NULL
  29121. + };
  29122. + struct au_sbinfo *sbinfo;
  29123. + struct dentry *root;
  29124. + struct inode *inode;
  29125. + char *arg = raw_data;
  29126. +
  29127. + if (unlikely(!arg || !*arg)) {
  29128. + err = -EINVAL;
  29129. + pr_err("no arg\n");
  29130. + goto out;
  29131. + }
  29132. +
  29133. + err = -ENOMEM;
  29134. + opts.opt = (void *)__get_free_page(GFP_NOFS);
  29135. + if (unlikely(!opts.opt))
  29136. + goto out;
  29137. + opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
  29138. + opts.sb_flags = sb->s_flags;
  29139. +
  29140. + err = au_si_alloc(sb);
  29141. + if (unlikely(err))
  29142. + goto out_opts;
  29143. + sbinfo = au_sbi(sb);
  29144. +
  29145. + /* all timestamps always follow the ones on the branch */
  29146. + sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
  29147. + sb->s_flags |= SB_I_VERSION; /* do we really need this? */
  29148. + sb->s_op = &aufs_sop;
  29149. + sb->s_d_op = &aufs_dop;
  29150. + sb->s_magic = AUFS_SUPER_MAGIC;
  29151. + sb->s_maxbytes = 0;
  29152. + sb->s_stack_depth = 1;
  29153. + au_export_init(sb);
  29154. + au_xattr_init(sb);
  29155. +
  29156. + err = alloc_root(sb);
  29157. + if (unlikely(err)) {
  29158. + si_write_unlock(sb);
  29159. + goto out_info;
  29160. + }
  29161. + root = sb->s_root;
  29162. + inode = d_inode(root);
  29163. +
  29164. + /*
  29165. + * actually we can parse options regardless aufs lock here.
  29166. + * but at remount time, parsing must be done before aufs lock.
  29167. + * so we follow the same rule.
  29168. + */
  29169. + ii_write_lock_parent(inode);
  29170. + aufs_write_unlock(root);
  29171. + err = au_opts_parse(sb, arg, &opts);
  29172. + if (unlikely(err))
  29173. + goto out_root;
  29174. +
  29175. + /* lock vfs_inode first, then aufs. */
  29176. + inode_lock(inode);
  29177. + aufs_write_lock(root);
  29178. + err = au_opts_mount(sb, &opts);
  29179. + au_opts_free(&opts);
  29180. + if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
  29181. + sb->s_d_op = &aufs_dop_noreval;
  29182. + pr_info("%ps\n", sb->s_d_op);
  29183. + au_refresh_dop(root, /*force_reval*/0);
  29184. + sbinfo->si_iop_array = aufs_iop_nogetattr;
  29185. + au_refresh_iop(inode, /*force_getattr*/0);
  29186. + }
  29187. + aufs_write_unlock(root);
  29188. + inode_unlock(inode);
  29189. + if (!err)
  29190. + goto out_opts; /* success */
  29191. +
  29192. +out_root:
  29193. + dput(root);
  29194. + sb->s_root = NULL;
  29195. +out_info:
  29196. + kobject_put(&sbinfo->si_kobj);
  29197. + sb->s_fs_info = NULL;
  29198. +out_opts:
  29199. + free_page((unsigned long)opts.opt);
  29200. +out:
  29201. + AuTraceErr(err);
  29202. + err = cvt_err(err);
  29203. + AuTraceErr(err);
  29204. + return err;
  29205. +}
  29206. +
  29207. +/* ---------------------------------------------------------------------- */
  29208. +
  29209. +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
  29210. + const char *dev_name __maybe_unused,
  29211. + void *raw_data)
  29212. +{
  29213. + struct dentry *root;
  29214. +
  29215. + /* all timestamps always follow the ones on the branch */
  29216. + /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
  29217. + root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
  29218. + if (IS_ERR(root))
  29219. + goto out;
  29220. +
  29221. + au_sbilist_add(root->d_sb);
  29222. +
  29223. +out:
  29224. + return root;
  29225. +}
  29226. +
  29227. +static void aufs_kill_sb(struct super_block *sb)
  29228. +{
  29229. + struct au_sbinfo *sbinfo;
  29230. +
  29231. + sbinfo = au_sbi(sb);
  29232. + if (sbinfo) {
  29233. + au_sbilist_del(sb);
  29234. + aufs_write_lock(sb->s_root);
  29235. + au_fhsm_fin(sb);
  29236. + if (sbinfo->si_wbr_create_ops->fin)
  29237. + sbinfo->si_wbr_create_ops->fin(sb);
  29238. + if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
  29239. + au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
  29240. + au_remount_refresh(sb, /*do_idop*/0);
  29241. + }
  29242. + if (au_opt_test(sbinfo->si_mntflags, PLINK))
  29243. + au_plink_put(sb, /*verbose*/1);
  29244. + au_xino_clr(sb);
  29245. + au_dr_opt_flush(sb);
  29246. + sbinfo->si_sb = NULL;
  29247. + aufs_write_unlock(sb->s_root);
  29248. + au_nwt_flush(&sbinfo->si_nowait);
  29249. + }
  29250. + kill_anon_super(sb);
  29251. +}
  29252. +
  29253. +struct file_system_type aufs_fs_type = {
  29254. + .name = AUFS_FSTYPE,
  29255. + /* a race between rename and others */
  29256. + .fs_flags = FS_RENAME_DOES_D_MOVE,
  29257. + .mount = aufs_mount,
  29258. + .kill_sb = aufs_kill_sb,
  29259. + /* no need to __module_get() and module_put(). */
  29260. + .owner = THIS_MODULE,
  29261. +};
  29262. --- /dev/null
  29263. +++ linux-4.19/fs/aufs/super.h 2018-11-20 09:35:15.042195505 +0200
  29264. @@ -0,0 +1,589 @@
  29265. +/* SPDX-License-Identifier: GPL-2.0 */
  29266. +/*
  29267. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  29268. + *
  29269. + * This program, aufs is free software; you can redistribute it and/or modify
  29270. + * it under the terms of the GNU General Public License as published by
  29271. + * the Free Software Foundation; either version 2 of the License, or
  29272. + * (at your option) any later version.
  29273. + *
  29274. + * This program is distributed in the hope that it will be useful,
  29275. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29276. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29277. + * GNU General Public License for more details.
  29278. + *
  29279. + * You should have received a copy of the GNU General Public License
  29280. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29281. + */
  29282. +
  29283. +/*
  29284. + * super_block operations
  29285. + */
  29286. +
  29287. +#ifndef __AUFS_SUPER_H__
  29288. +#define __AUFS_SUPER_H__
  29289. +
  29290. +#ifdef __KERNEL__
  29291. +
  29292. +#include <linux/fs.h>
  29293. +#include <linux/kobject.h>
  29294. +#include "hbl.h"
  29295. +#include "lcnt.h"
  29296. +#include "rwsem.h"
  29297. +#include "wkq.h"
  29298. +
  29299. +/* policies to select one among multiple writable branches */
  29300. +struct au_wbr_copyup_operations {
  29301. + int (*copyup)(struct dentry *dentry);
  29302. +};
  29303. +
  29304. +#define AuWbr_DIR 1 /* target is a dir */
  29305. +#define AuWbr_PARENT (1 << 1) /* always require a parent */
  29306. +
  29307. +#define au_ftest_wbr(flags, name) ((flags) & AuWbr_##name)
  29308. +#define au_fset_wbr(flags, name) { (flags) |= AuWbr_##name; }
  29309. +#define au_fclr_wbr(flags, name) { (flags) &= ~AuWbr_##name; }
  29310. +
  29311. +struct au_wbr_create_operations {
  29312. + int (*create)(struct dentry *dentry, unsigned int flags);
  29313. + int (*init)(struct super_block *sb);
  29314. + int (*fin)(struct super_block *sb);
  29315. +};
  29316. +
  29317. +struct au_wbr_mfs {
  29318. + struct mutex mfs_lock; /* protect this structure */
  29319. + unsigned long mfs_jiffy;
  29320. + unsigned long mfs_expire;
  29321. + aufs_bindex_t mfs_bindex;
  29322. +
  29323. + unsigned long long mfsrr_bytes;
  29324. + unsigned long long mfsrr_watermark;
  29325. +};
  29326. +
  29327. +#define AuPlink_NHASH 100
  29328. +static inline int au_plink_hash(ino_t ino)
  29329. +{
  29330. + return ino % AuPlink_NHASH;
  29331. +}
  29332. +
  29333. +/* File-based Hierarchical Storage Management */
  29334. +struct au_fhsm {
  29335. +#ifdef CONFIG_AUFS_FHSM
  29336. + /* allow only one process who can receive the notification */
  29337. + spinlock_t fhsm_spin;
  29338. + pid_t fhsm_pid;
  29339. + wait_queue_head_t fhsm_wqh;
  29340. + atomic_t fhsm_readable;
  29341. +
  29342. + /* these are protected by si_rwsem */
  29343. + unsigned long fhsm_expire;
  29344. + aufs_bindex_t fhsm_bottom;
  29345. +#endif
  29346. +};
  29347. +
  29348. +struct au_branch;
  29349. +struct au_sbinfo {
  29350. + /* nowait tasks in the system-wide workqueue */
  29351. + struct au_nowait_tasks si_nowait;
  29352. +
  29353. + /*
  29354. + * tried sb->s_umount, but failed due to the dependency between i_mutex.
  29355. + * rwsem for au_sbinfo is necessary.
  29356. + */
  29357. + struct au_rwsem si_rwsem;
  29358. +
  29359. + /*
  29360. + * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
  29361. + * remount.
  29362. + */
  29363. + au_lcnt_t si_ninodes, si_nfiles;
  29364. +
  29365. + /* branch management */
  29366. + unsigned int si_generation;
  29367. +
  29368. + /* see AuSi_ flags */
  29369. + unsigned char au_si_status;
  29370. +
  29371. + aufs_bindex_t si_bbot;
  29372. +
  29373. + /* dirty trick to keep br_id plus */
  29374. + unsigned int si_last_br_id :
  29375. + sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
  29376. + struct au_branch **si_branch;
  29377. +
  29378. + /* policy to select a writable branch */
  29379. + unsigned char si_wbr_copyup;
  29380. + unsigned char si_wbr_create;
  29381. + struct au_wbr_copyup_operations *si_wbr_copyup_ops;
  29382. + struct au_wbr_create_operations *si_wbr_create_ops;
  29383. +
  29384. + /* round robin */
  29385. + atomic_t si_wbr_rr_next;
  29386. +
  29387. + /* most free space */
  29388. + struct au_wbr_mfs si_wbr_mfs;
  29389. +
  29390. + /* File-based Hierarchical Storage Management */
  29391. + struct au_fhsm si_fhsm;
  29392. +
  29393. + /* mount flags */
  29394. + /* include/asm-ia64/siginfo.h defines a macro named si_flags */
  29395. + unsigned int si_mntflags;
  29396. +
  29397. + /* external inode number (bitmap and translation table) */
  29398. + vfs_readf_t si_xread;
  29399. + vfs_writef_t si_xwrite;
  29400. + loff_t si_ximaxent; /* max entries in a xino */
  29401. +
  29402. + struct file *si_xib;
  29403. + struct mutex si_xib_mtx; /* protect xib members */
  29404. + unsigned long *si_xib_buf;
  29405. + unsigned long si_xib_last_pindex;
  29406. + int si_xib_next_bit;
  29407. +
  29408. + unsigned long si_xino_jiffy;
  29409. + unsigned long si_xino_expire;
  29410. + /* reserved for future use */
  29411. + /* unsigned long long si_xib_limit; */ /* Max xib file size */
  29412. +
  29413. +#ifdef CONFIG_AUFS_EXPORT
  29414. + /* i_generation */
  29415. + /* todo: make xigen file an array to support many inode numbers */
  29416. + struct file *si_xigen;
  29417. + atomic_t si_xigen_next;
  29418. +#endif
  29419. +
  29420. + /* dirty trick to support atomic_open */
  29421. + struct hlist_bl_head si_aopen;
  29422. +
  29423. + /* vdir parameters */
  29424. + unsigned long si_rdcache; /* max cache time in jiffies */
  29425. + unsigned int si_rdblk; /* deblk size */
  29426. + unsigned int si_rdhash; /* hash size */
  29427. +
  29428. + /*
  29429. + * If the number of whiteouts are larger than si_dirwh, leave all of
  29430. + * them after au_whtmp_ren to reduce the cost of rmdir(2).
  29431. + * future fsck.aufs or kernel thread will remove them later.
  29432. + * Otherwise, remove all whiteouts and the dir in rmdir(2).
  29433. + */
  29434. + unsigned int si_dirwh;
  29435. +
  29436. + /* pseudo_link list */
  29437. + struct hlist_bl_head si_plink[AuPlink_NHASH];
  29438. + wait_queue_head_t si_plink_wq;
  29439. + spinlock_t si_plink_maint_lock;
  29440. + pid_t si_plink_maint_pid;
  29441. +
  29442. + /* file list */
  29443. + struct hlist_bl_head si_files;
  29444. +
  29445. + /* with/without getattr, brother of sb->s_d_op */
  29446. + struct inode_operations *si_iop_array;
  29447. +
  29448. + /*
  29449. + * sysfs and lifetime management.
  29450. + * this is not a small structure and it may be a waste of memory in case
  29451. + * of sysfs is disabled, particularly when many aufs-es are mounted.
  29452. + * but using sysfs is majority.
  29453. + */
  29454. + struct kobject si_kobj;
  29455. +#ifdef CONFIG_DEBUG_FS
  29456. + struct dentry *si_dbgaufs;
  29457. + struct dentry *si_dbgaufs_plink;
  29458. + struct dentry *si_dbgaufs_xib;
  29459. +#ifdef CONFIG_AUFS_EXPORT
  29460. + struct dentry *si_dbgaufs_xigen;
  29461. +#endif
  29462. +#endif
  29463. +
  29464. +#ifdef CONFIG_AUFS_SBILIST
  29465. + struct hlist_bl_node si_list;
  29466. +#endif
  29467. +
  29468. + /* dirty, necessary for unmounting, sysfs and sysrq */
  29469. + struct super_block *si_sb;
  29470. +};
  29471. +
  29472. +/* sbinfo status flags */
  29473. +/*
  29474. + * set true when refresh_dirs() failed at remount time.
  29475. + * then try refreshing dirs at access time again.
  29476. + * if it is false, refreshing dirs at access time is unnecessary
  29477. + */
  29478. +#define AuSi_FAILED_REFRESH_DIR 1
  29479. +#define AuSi_FHSM (1 << 1) /* fhsm is active now */
  29480. +#define AuSi_NO_DREVAL (1 << 2) /* disable all d_revalidate */
  29481. +
  29482. +#ifndef CONFIG_AUFS_FHSM
  29483. +#undef AuSi_FHSM
  29484. +#define AuSi_FHSM 0
  29485. +#endif
  29486. +
  29487. +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
  29488. + unsigned int flag)
  29489. +{
  29490. + AuRwMustAnyLock(&sbi->si_rwsem);
  29491. + return sbi->au_si_status & flag;
  29492. +}
  29493. +#define au_ftest_si(sbinfo, name) au_do_ftest_si(sbinfo, AuSi_##name)
  29494. +#define au_fset_si(sbinfo, name) do { \
  29495. + AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
  29496. + (sbinfo)->au_si_status |= AuSi_##name; \
  29497. +} while (0)
  29498. +#define au_fclr_si(sbinfo, name) do { \
  29499. + AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
  29500. + (sbinfo)->au_si_status &= ~AuSi_##name; \
  29501. +} while (0)
  29502. +
  29503. +/* ---------------------------------------------------------------------- */
  29504. +
  29505. +/* policy to select one among writable branches */
  29506. +#define AuWbrCopyup(sbinfo, ...) \
  29507. + ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
  29508. +#define AuWbrCreate(sbinfo, ...) \
  29509. + ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
  29510. +
  29511. +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
  29512. +#define AuLock_DW 1 /* write-lock dentry */
  29513. +#define AuLock_IR (1 << 1) /* read-lock inode */
  29514. +#define AuLock_IW (1 << 2) /* write-lock inode */
  29515. +#define AuLock_FLUSH (1 << 3) /* wait for 'nowait' tasks */
  29516. +#define AuLock_DIRS (1 << 4) /* target is a pair of dirs */
  29517. + /* except RENAME_EXCHANGE */
  29518. +#define AuLock_NOPLM (1 << 5) /* return err in plm mode */
  29519. +#define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
  29520. +#define AuLock_GEN (1 << 7) /* test digen/iigen */
  29521. +#define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
  29522. +#define au_fset_lock(flags, name) \
  29523. + do { (flags) |= AuLock_##name; } while (0)
  29524. +#define au_fclr_lock(flags, name) \
  29525. + do { (flags) &= ~AuLock_##name; } while (0)
  29526. +
  29527. +/* ---------------------------------------------------------------------- */
  29528. +
  29529. +/* super.c */
  29530. +extern struct file_system_type aufs_fs_type;
  29531. +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
  29532. +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
  29533. + unsigned long long max, void *arg);
  29534. +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
  29535. + struct super_block *sb, void *arg);
  29536. +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
  29537. +void au_iarray_free(struct inode **a, unsigned long long max);
  29538. +
  29539. +/* sbinfo.c */
  29540. +void au_si_free(struct kobject *kobj);
  29541. +int au_si_alloc(struct super_block *sb);
  29542. +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
  29543. +
  29544. +unsigned int au_sigen_inc(struct super_block *sb);
  29545. +aufs_bindex_t au_new_br_id(struct super_block *sb);
  29546. +
  29547. +int si_read_lock(struct super_block *sb, int flags);
  29548. +int si_write_lock(struct super_block *sb, int flags);
  29549. +int aufs_read_lock(struct dentry *dentry, int flags);
  29550. +void aufs_read_unlock(struct dentry *dentry, int flags);
  29551. +void aufs_write_lock(struct dentry *dentry);
  29552. +void aufs_write_unlock(struct dentry *dentry);
  29553. +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
  29554. +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
  29555. +
  29556. +/* wbr_policy.c */
  29557. +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
  29558. +extern struct au_wbr_create_operations au_wbr_create_ops[];
  29559. +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
  29560. +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
  29561. +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
  29562. +
  29563. +/* mvdown.c */
  29564. +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
  29565. +
  29566. +#ifdef CONFIG_AUFS_FHSM
  29567. +/* fhsm.c */
  29568. +
  29569. +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
  29570. +{
  29571. + pid_t pid;
  29572. +
  29573. + spin_lock(&fhsm->fhsm_spin);
  29574. + pid = fhsm->fhsm_pid;
  29575. + spin_unlock(&fhsm->fhsm_spin);
  29576. +
  29577. + return pid;
  29578. +}
  29579. +
  29580. +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
  29581. +void au_fhsm_wrote_all(struct super_block *sb, int force);
  29582. +int au_fhsm_fd(struct super_block *sb, int oflags);
  29583. +int au_fhsm_br_alloc(struct au_branch *br);
  29584. +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
  29585. +void au_fhsm_fin(struct super_block *sb);
  29586. +void au_fhsm_init(struct au_sbinfo *sbinfo);
  29587. +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
  29588. +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
  29589. +#else
  29590. +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
  29591. + int force)
  29592. +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
  29593. +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
  29594. +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
  29595. +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
  29596. +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
  29597. +AuStubVoid(au_fhsm_fin, struct super_block *sb)
  29598. +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
  29599. +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
  29600. +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
  29601. +#endif
  29602. +
  29603. +/* ---------------------------------------------------------------------- */
  29604. +
  29605. +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
  29606. +{
  29607. + return sb->s_fs_info;
  29608. +}
  29609. +
  29610. +/* ---------------------------------------------------------------------- */
  29611. +
  29612. +#ifdef CONFIG_AUFS_EXPORT
  29613. +int au_test_nfsd(void);
  29614. +void au_export_init(struct super_block *sb);
  29615. +void au_xigen_inc(struct inode *inode);
  29616. +int au_xigen_new(struct inode *inode);
  29617. +int au_xigen_set(struct super_block *sb, struct path *path);
  29618. +void au_xigen_clr(struct super_block *sb);
  29619. +
  29620. +static inline int au_busy_or_stale(void)
  29621. +{
  29622. + if (!au_test_nfsd())
  29623. + return -EBUSY;
  29624. + return -ESTALE;
  29625. +}
  29626. +#else
  29627. +AuStubInt0(au_test_nfsd, void)
  29628. +AuStubVoid(au_export_init, struct super_block *sb)
  29629. +AuStubVoid(au_xigen_inc, struct inode *inode)
  29630. +AuStubInt0(au_xigen_new, struct inode *inode)
  29631. +AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
  29632. +AuStubVoid(au_xigen_clr, struct super_block *sb)
  29633. +AuStub(int, au_busy_or_stale, return -EBUSY, void)
  29634. +#endif /* CONFIG_AUFS_EXPORT */
  29635. +
  29636. +/* ---------------------------------------------------------------------- */
  29637. +
  29638. +#ifdef CONFIG_AUFS_SBILIST
  29639. +/* module.c */
  29640. +extern struct hlist_bl_head au_sbilist;
  29641. +
  29642. +static inline void au_sbilist_init(void)
  29643. +{
  29644. + INIT_HLIST_BL_HEAD(&au_sbilist);
  29645. +}
  29646. +
  29647. +static inline void au_sbilist_add(struct super_block *sb)
  29648. +{
  29649. + au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
  29650. +}
  29651. +
  29652. +static inline void au_sbilist_del(struct super_block *sb)
  29653. +{
  29654. + au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
  29655. +}
  29656. +
  29657. +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
  29658. +static inline void au_sbilist_lock(void)
  29659. +{
  29660. + hlist_bl_lock(&au_sbilist);
  29661. +}
  29662. +
  29663. +static inline void au_sbilist_unlock(void)
  29664. +{
  29665. + hlist_bl_unlock(&au_sbilist);
  29666. +}
  29667. +#define AuGFP_SBILIST GFP_ATOMIC
  29668. +#else
  29669. +AuStubVoid(au_sbilist_lock, void)
  29670. +AuStubVoid(au_sbilist_unlock, void)
  29671. +#define AuGFP_SBILIST GFP_NOFS
  29672. +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
  29673. +#else
  29674. +AuStubVoid(au_sbilist_init, void)
  29675. +AuStubVoid(au_sbilist_add, struct super_block *sb)
  29676. +AuStubVoid(au_sbilist_del, struct super_block *sb)
  29677. +AuStubVoid(au_sbilist_lock, void)
  29678. +AuStubVoid(au_sbilist_unlock, void)
  29679. +#define AuGFP_SBILIST GFP_NOFS
  29680. +#endif
  29681. +
  29682. +/* ---------------------------------------------------------------------- */
  29683. +
  29684. +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
  29685. +{
  29686. + /*
  29687. + * This function is a dynamic '__init' function actually,
  29688. + * so the tiny check for si_rwsem is unnecessary.
  29689. + */
  29690. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  29691. +#ifdef CONFIG_DEBUG_FS
  29692. + sbinfo->si_dbgaufs = NULL;
  29693. + sbinfo->si_dbgaufs_plink = NULL;
  29694. + sbinfo->si_dbgaufs_xib = NULL;
  29695. +#ifdef CONFIG_AUFS_EXPORT
  29696. + sbinfo->si_dbgaufs_xigen = NULL;
  29697. +#endif
  29698. +#endif
  29699. +}
  29700. +
  29701. +/* ---------------------------------------------------------------------- */
  29702. +
  29703. +/* current->atomic_flags */
  29704. +/* this value should never corrupt the ones defined in linux/sched.h */
  29705. +#define PFA_AUFS 7
  29706. +
  29707. +TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
  29708. +TASK_PFA_SET(AUFS, aufs) /* task_set_aufs */
  29709. +TASK_PFA_CLEAR(AUFS, aufs) /* task_clear_aufs */
  29710. +
  29711. +static inline int si_pid_test(struct super_block *sb)
  29712. +{
  29713. + return !!task_test_aufs(current);
  29714. +}
  29715. +
  29716. +static inline void si_pid_clr(struct super_block *sb)
  29717. +{
  29718. + AuDebugOn(!task_test_aufs(current));
  29719. + task_clear_aufs(current);
  29720. +}
  29721. +
  29722. +static inline void si_pid_set(struct super_block *sb)
  29723. +{
  29724. + AuDebugOn(task_test_aufs(current));
  29725. + task_set_aufs(current);
  29726. +}
  29727. +
  29728. +/* ---------------------------------------------------------------------- */
  29729. +
  29730. +/* lock superblock. mainly for entry point functions */
  29731. +#define __si_read_lock(sb) au_rw_read_lock(&au_sbi(sb)->si_rwsem)
  29732. +#define __si_write_lock(sb) au_rw_write_lock(&au_sbi(sb)->si_rwsem)
  29733. +#define __si_read_trylock(sb) au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
  29734. +#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
  29735. +/*
  29736. +#define __si_read_trylock_nested(sb) \
  29737. + au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
  29738. +#define __si_write_trylock_nested(sb) \
  29739. + au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
  29740. +*/
  29741. +
  29742. +#define __si_read_unlock(sb) au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
  29743. +#define __si_write_unlock(sb) au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
  29744. +#define __si_downgrade_lock(sb) au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
  29745. +
  29746. +#define SiMustNoWaiters(sb) AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
  29747. +#define SiMustAnyLock(sb) AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
  29748. +#define SiMustWriteLock(sb) AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
  29749. +
  29750. +static inline void si_noflush_read_lock(struct super_block *sb)
  29751. +{
  29752. + __si_read_lock(sb);
  29753. + si_pid_set(sb);
  29754. +}
  29755. +
  29756. +static inline int si_noflush_read_trylock(struct super_block *sb)
  29757. +{
  29758. + int locked;
  29759. +
  29760. + locked = __si_read_trylock(sb);
  29761. + if (locked)
  29762. + si_pid_set(sb);
  29763. + return locked;
  29764. +}
  29765. +
  29766. +static inline void si_noflush_write_lock(struct super_block *sb)
  29767. +{
  29768. + __si_write_lock(sb);
  29769. + si_pid_set(sb);
  29770. +}
  29771. +
  29772. +static inline int si_noflush_write_trylock(struct super_block *sb)
  29773. +{
  29774. + int locked;
  29775. +
  29776. + locked = __si_write_trylock(sb);
  29777. + if (locked)
  29778. + si_pid_set(sb);
  29779. + return locked;
  29780. +}
  29781. +
  29782. +#if 0 /* reserved */
  29783. +static inline int si_read_trylock(struct super_block *sb, int flags)
  29784. +{
  29785. + if (au_ftest_lock(flags, FLUSH))
  29786. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  29787. + return si_noflush_read_trylock(sb);
  29788. +}
  29789. +#endif
  29790. +
  29791. +static inline void si_read_unlock(struct super_block *sb)
  29792. +{
  29793. + si_pid_clr(sb);
  29794. + __si_read_unlock(sb);
  29795. +}
  29796. +
  29797. +#if 0 /* reserved */
  29798. +static inline int si_write_trylock(struct super_block *sb, int flags)
  29799. +{
  29800. + if (au_ftest_lock(flags, FLUSH))
  29801. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  29802. + return si_noflush_write_trylock(sb);
  29803. +}
  29804. +#endif
  29805. +
  29806. +static inline void si_write_unlock(struct super_block *sb)
  29807. +{
  29808. + si_pid_clr(sb);
  29809. + __si_write_unlock(sb);
  29810. +}
  29811. +
  29812. +#if 0 /* reserved */
  29813. +static inline void si_downgrade_lock(struct super_block *sb)
  29814. +{
  29815. + __si_downgrade_lock(sb);
  29816. +}
  29817. +#endif
  29818. +
  29819. +/* ---------------------------------------------------------------------- */
  29820. +
  29821. +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
  29822. +{
  29823. + SiMustAnyLock(sb);
  29824. + return au_sbi(sb)->si_bbot;
  29825. +}
  29826. +
  29827. +static inline unsigned int au_mntflags(struct super_block *sb)
  29828. +{
  29829. + SiMustAnyLock(sb);
  29830. + return au_sbi(sb)->si_mntflags;
  29831. +}
  29832. +
  29833. +static inline unsigned int au_sigen(struct super_block *sb)
  29834. +{
  29835. + SiMustAnyLock(sb);
  29836. + return au_sbi(sb)->si_generation;
  29837. +}
  29838. +
  29839. +static inline struct au_branch *au_sbr(struct super_block *sb,
  29840. + aufs_bindex_t bindex)
  29841. +{
  29842. + SiMustAnyLock(sb);
  29843. + return au_sbi(sb)->si_branch[0 + bindex];
  29844. +}
  29845. +
  29846. +static inline loff_t au_xi_maxent(struct super_block *sb)
  29847. +{
  29848. + SiMustAnyLock(sb);
  29849. + return au_sbi(sb)->si_ximaxent;
  29850. +}
  29851. +
  29852. +#endif /* __KERNEL__ */
  29853. +#endif /* __AUFS_SUPER_H__ */
  29854. --- /dev/null
  29855. +++ linux-4.19/fs/aufs/sysaufs.c 2018-11-20 09:35:15.043195505 +0200
  29856. @@ -0,0 +1,93 @@
  29857. +// SPDX-License-Identifier: GPL-2.0
  29858. +/*
  29859. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  29860. + *
  29861. + * This program, aufs is free software; you can redistribute it and/or modify
  29862. + * it under the terms of the GNU General Public License as published by
  29863. + * the Free Software Foundation; either version 2 of the License, or
  29864. + * (at your option) any later version.
  29865. + *
  29866. + * This program is distributed in the hope that it will be useful,
  29867. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29868. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29869. + * GNU General Public License for more details.
  29870. + *
  29871. + * You should have received a copy of the GNU General Public License
  29872. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29873. + */
  29874. +
  29875. +/*
  29876. + * sysfs interface and lifetime management
  29877. + * they are necessary regardless sysfs is disabled.
  29878. + */
  29879. +
  29880. +#include <linux/random.h>
  29881. +#include "aufs.h"
  29882. +
  29883. +unsigned long sysaufs_si_mask;
  29884. +struct kset *sysaufs_kset;
  29885. +
  29886. +#define AuSiAttr(_name) { \
  29887. + .attr = { .name = __stringify(_name), .mode = 0444 }, \
  29888. + .show = sysaufs_si_##_name, \
  29889. +}
  29890. +
  29891. +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
  29892. +struct attribute *sysaufs_si_attrs[] = {
  29893. + &sysaufs_si_attr_xi_path.attr,
  29894. + NULL,
  29895. +};
  29896. +
  29897. +static const struct sysfs_ops au_sbi_ops = {
  29898. + .show = sysaufs_si_show
  29899. +};
  29900. +
  29901. +static struct kobj_type au_sbi_ktype = {
  29902. + .release = au_si_free,
  29903. + .sysfs_ops = &au_sbi_ops,
  29904. + .default_attrs = sysaufs_si_attrs
  29905. +};
  29906. +
  29907. +/* ---------------------------------------------------------------------- */
  29908. +
  29909. +int sysaufs_si_init(struct au_sbinfo *sbinfo)
  29910. +{
  29911. + int err;
  29912. +
  29913. + sbinfo->si_kobj.kset = sysaufs_kset;
  29914. + /* cf. sysaufs_name() */
  29915. + err = kobject_init_and_add
  29916. + (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
  29917. + SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
  29918. +
  29919. + return err;
  29920. +}
  29921. +
  29922. +void sysaufs_fin(void)
  29923. +{
  29924. + sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
  29925. + kset_unregister(sysaufs_kset);
  29926. +}
  29927. +
  29928. +int __init sysaufs_init(void)
  29929. +{
  29930. + int err;
  29931. +
  29932. + do {
  29933. + get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
  29934. + } while (!sysaufs_si_mask);
  29935. +
  29936. + err = -EINVAL;
  29937. + sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
  29938. + if (unlikely(!sysaufs_kset))
  29939. + goto out;
  29940. + err = PTR_ERR(sysaufs_kset);
  29941. + if (IS_ERR(sysaufs_kset))
  29942. + goto out;
  29943. + err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
  29944. + if (unlikely(err))
  29945. + kset_unregister(sysaufs_kset);
  29946. +
  29947. +out:
  29948. + return err;
  29949. +}
  29950. --- /dev/null
  29951. +++ linux-4.19/fs/aufs/sysaufs.h 2018-11-20 09:35:15.043195505 +0200
  29952. @@ -0,0 +1,102 @@
  29953. +/* SPDX-License-Identifier: GPL-2.0 */
  29954. +/*
  29955. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  29956. + *
  29957. + * This program, aufs is free software; you can redistribute it and/or modify
  29958. + * it under the terms of the GNU General Public License as published by
  29959. + * the Free Software Foundation; either version 2 of the License, or
  29960. + * (at your option) any later version.
  29961. + *
  29962. + * This program is distributed in the hope that it will be useful,
  29963. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29964. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29965. + * GNU General Public License for more details.
  29966. + *
  29967. + * You should have received a copy of the GNU General Public License
  29968. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29969. + */
  29970. +
  29971. +/*
  29972. + * sysfs interface and mount lifetime management
  29973. + */
  29974. +
  29975. +#ifndef __SYSAUFS_H__
  29976. +#define __SYSAUFS_H__
  29977. +
  29978. +#ifdef __KERNEL__
  29979. +
  29980. +#include <linux/sysfs.h>
  29981. +#include "module.h"
  29982. +
  29983. +struct super_block;
  29984. +struct au_sbinfo;
  29985. +
  29986. +struct sysaufs_si_attr {
  29987. + struct attribute attr;
  29988. + int (*show)(struct seq_file *seq, struct super_block *sb);
  29989. +};
  29990. +
  29991. +/* ---------------------------------------------------------------------- */
  29992. +
  29993. +/* sysaufs.c */
  29994. +extern unsigned long sysaufs_si_mask;
  29995. +extern struct kset *sysaufs_kset;
  29996. +extern struct attribute *sysaufs_si_attrs[];
  29997. +int sysaufs_si_init(struct au_sbinfo *sbinfo);
  29998. +int __init sysaufs_init(void);
  29999. +void sysaufs_fin(void);
  30000. +
  30001. +/* ---------------------------------------------------------------------- */
  30002. +
  30003. +/* some people doesn't like to show a pointer in kernel */
  30004. +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
  30005. +{
  30006. + return sysaufs_si_mask ^ (unsigned long)sbinfo;
  30007. +}
  30008. +
  30009. +#define SysaufsSiNamePrefix "si_"
  30010. +#define SysaufsSiNameLen (sizeof(SysaufsSiNamePrefix) + 16)
  30011. +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
  30012. +{
  30013. + snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
  30014. + sysaufs_si_id(sbinfo));
  30015. +}
  30016. +
  30017. +struct au_branch;
  30018. +#ifdef CONFIG_SYSFS
  30019. +/* sysfs.c */
  30020. +extern struct attribute_group *sysaufs_attr_group;
  30021. +
  30022. +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
  30023. +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
  30024. + char *buf);
  30025. +long au_brinfo_ioctl(struct file *file, unsigned long arg);
  30026. +#ifdef CONFIG_COMPAT
  30027. +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
  30028. +#endif
  30029. +
  30030. +void sysaufs_br_init(struct au_branch *br);
  30031. +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
  30032. +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
  30033. +
  30034. +#define sysaufs_brs_init() do {} while (0)
  30035. +
  30036. +#else
  30037. +#define sysaufs_attr_group NULL
  30038. +
  30039. +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
  30040. +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
  30041. + struct attribute *attr, char *buf)
  30042. +AuStubVoid(sysaufs_br_init, struct au_branch *br)
  30043. +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
  30044. +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
  30045. +
  30046. +static inline void sysaufs_brs_init(void)
  30047. +{
  30048. + sysaufs_brs = 0;
  30049. +}
  30050. +
  30051. +#endif /* CONFIG_SYSFS */
  30052. +
  30053. +#endif /* __KERNEL__ */
  30054. +#endif /* __SYSAUFS_H__ */
  30055. --- /dev/null
  30056. +++ linux-4.19/fs/aufs/sysfs.c 2018-11-20 09:35:15.044195505 +0200
  30057. @@ -0,0 +1,373 @@
  30058. +// SPDX-License-Identifier: GPL-2.0
  30059. +/*
  30060. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  30061. + *
  30062. + * This program, aufs is free software; you can redistribute it and/or modify
  30063. + * it under the terms of the GNU General Public License as published by
  30064. + * the Free Software Foundation; either version 2 of the License, or
  30065. + * (at your option) any later version.
  30066. + *
  30067. + * This program is distributed in the hope that it will be useful,
  30068. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30069. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30070. + * GNU General Public License for more details.
  30071. + *
  30072. + * You should have received a copy of the GNU General Public License
  30073. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30074. + */
  30075. +
  30076. +/*
  30077. + * sysfs interface
  30078. + */
  30079. +
  30080. +#include <linux/compat.h>
  30081. +#include <linux/seq_file.h>
  30082. +#include "aufs.h"
  30083. +
  30084. +#ifdef CONFIG_AUFS_FS_MODULE
  30085. +/* this entry violates the "one line per file" policy of sysfs */
  30086. +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
  30087. + char *buf)
  30088. +{
  30089. + ssize_t err;
  30090. + static char *conf =
  30091. +/* this file is generated at compiling */
  30092. +#include "conf.str"
  30093. + ;
  30094. +
  30095. + err = snprintf(buf, PAGE_SIZE, conf);
  30096. + if (unlikely(err >= PAGE_SIZE))
  30097. + err = -EFBIG;
  30098. + return err;
  30099. +}
  30100. +
  30101. +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
  30102. +#endif
  30103. +
  30104. +static struct attribute *au_attr[] = {
  30105. +#ifdef CONFIG_AUFS_FS_MODULE
  30106. + &au_config_attr.attr,
  30107. +#endif
  30108. + NULL, /* need to NULL terminate the list of attributes */
  30109. +};
  30110. +
  30111. +static struct attribute_group sysaufs_attr_group_body = {
  30112. + .attrs = au_attr
  30113. +};
  30114. +
  30115. +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
  30116. +
  30117. +/* ---------------------------------------------------------------------- */
  30118. +
  30119. +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
  30120. +{
  30121. + int err;
  30122. +
  30123. + SiMustAnyLock(sb);
  30124. +
  30125. + err = 0;
  30126. + if (au_opt_test(au_mntflags(sb), XINO)) {
  30127. + err = au_xino_path(seq, au_sbi(sb)->si_xib);
  30128. + seq_putc(seq, '\n');
  30129. + }
  30130. + return err;
  30131. +}
  30132. +
  30133. +/*
  30134. + * the lifetime of branch is independent from the entry under sysfs.
  30135. + * sysfs handles the lifetime of the entry, and never call ->show() after it is
  30136. + * unlinked.
  30137. + */
  30138. +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
  30139. + aufs_bindex_t bindex, int idx)
  30140. +{
  30141. + int err;
  30142. + struct path path;
  30143. + struct dentry *root;
  30144. + struct au_branch *br;
  30145. + au_br_perm_str_t perm;
  30146. +
  30147. + AuDbg("b%d\n", bindex);
  30148. +
  30149. + err = 0;
  30150. + root = sb->s_root;
  30151. + di_read_lock_parent(root, !AuLock_IR);
  30152. + br = au_sbr(sb, bindex);
  30153. +
  30154. + switch (idx) {
  30155. + case AuBrSysfs_BR:
  30156. + path.mnt = au_br_mnt(br);
  30157. + path.dentry = au_h_dptr(root, bindex);
  30158. + err = au_seq_path(seq, &path);
  30159. + if (!err) {
  30160. + au_optstr_br_perm(&perm, br->br_perm);
  30161. + seq_printf(seq, "=%s\n", perm.a);
  30162. + }
  30163. + break;
  30164. + case AuBrSysfs_BRID:
  30165. + seq_printf(seq, "%d\n", br->br_id);
  30166. + break;
  30167. + }
  30168. + di_read_unlock(root, !AuLock_IR);
  30169. + if (unlikely(err || seq_has_overflowed(seq)))
  30170. + err = -E2BIG;
  30171. +
  30172. + return err;
  30173. +}
  30174. +
  30175. +/* ---------------------------------------------------------------------- */
  30176. +
  30177. +static struct seq_file *au_seq(char *p, ssize_t len)
  30178. +{
  30179. + struct seq_file *seq;
  30180. +
  30181. + seq = kzalloc(sizeof(*seq), GFP_NOFS);
  30182. + if (seq) {
  30183. + /* mutex_init(&seq.lock); */
  30184. + seq->buf = p;
  30185. + seq->size = len;
  30186. + return seq; /* success */
  30187. + }
  30188. +
  30189. + seq = ERR_PTR(-ENOMEM);
  30190. + return seq;
  30191. +}
  30192. +
  30193. +#define SysaufsBr_PREFIX "br"
  30194. +#define SysaufsBrid_PREFIX "brid"
  30195. +
  30196. +/* todo: file size may exceed PAGE_SIZE */
  30197. +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
  30198. + char *buf)
  30199. +{
  30200. + ssize_t err;
  30201. + int idx;
  30202. + long l;
  30203. + aufs_bindex_t bbot;
  30204. + struct au_sbinfo *sbinfo;
  30205. + struct super_block *sb;
  30206. + struct seq_file *seq;
  30207. + char *name;
  30208. + struct attribute **cattr;
  30209. +
  30210. + sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
  30211. + sb = sbinfo->si_sb;
  30212. +
  30213. + /*
  30214. + * prevent a race condition between sysfs and aufs.
  30215. + * for instance, sysfs_file_read() calls sysfs_get_active_two() which
  30216. + * prohibits maintaining the sysfs entries.
  30217. + * hew we acquire read lock after sysfs_get_active_two().
  30218. + * on the other hand, the remount process may maintain the sysfs/aufs
  30219. + * entries after acquiring write lock.
  30220. + * it can cause a deadlock.
  30221. + * simply we gave up processing read here.
  30222. + */
  30223. + err = -EBUSY;
  30224. + if (unlikely(!si_noflush_read_trylock(sb)))
  30225. + goto out;
  30226. +
  30227. + seq = au_seq(buf, PAGE_SIZE);
  30228. + err = PTR_ERR(seq);
  30229. + if (IS_ERR(seq))
  30230. + goto out_unlock;
  30231. +
  30232. + name = (void *)attr->name;
  30233. + cattr = sysaufs_si_attrs;
  30234. + while (*cattr) {
  30235. + if (!strcmp(name, (*cattr)->name)) {
  30236. + err = container_of(*cattr, struct sysaufs_si_attr, attr)
  30237. + ->show(seq, sb);
  30238. + goto out_seq;
  30239. + }
  30240. + cattr++;
  30241. + }
  30242. +
  30243. + if (!strncmp(name, SysaufsBrid_PREFIX,
  30244. + sizeof(SysaufsBrid_PREFIX) - 1)) {
  30245. + idx = AuBrSysfs_BRID;
  30246. + name += sizeof(SysaufsBrid_PREFIX) - 1;
  30247. + } else if (!strncmp(name, SysaufsBr_PREFIX,
  30248. + sizeof(SysaufsBr_PREFIX) - 1)) {
  30249. + idx = AuBrSysfs_BR;
  30250. + name += sizeof(SysaufsBr_PREFIX) - 1;
  30251. + } else
  30252. + BUG();
  30253. +
  30254. + err = kstrtol(name, 10, &l);
  30255. + if (!err) {
  30256. + bbot = au_sbbot(sb);
  30257. + if (l <= bbot)
  30258. + err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
  30259. + else
  30260. + err = -ENOENT;
  30261. + }
  30262. +
  30263. +out_seq:
  30264. + if (!err) {
  30265. + err = seq->count;
  30266. + /* sysfs limit */
  30267. + if (unlikely(err == PAGE_SIZE))
  30268. + err = -EFBIG;
  30269. + }
  30270. + kfree(seq);
  30271. +out_unlock:
  30272. + si_read_unlock(sb);
  30273. +out:
  30274. + return err;
  30275. +}
  30276. +
  30277. +/* ---------------------------------------------------------------------- */
  30278. +
  30279. +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
  30280. +{
  30281. + int err;
  30282. + int16_t brid;
  30283. + aufs_bindex_t bindex, bbot;
  30284. + size_t sz;
  30285. + char *buf;
  30286. + struct seq_file *seq;
  30287. + struct au_branch *br;
  30288. +
  30289. + si_read_lock(sb, AuLock_FLUSH);
  30290. + bbot = au_sbbot(sb);
  30291. + err = bbot + 1;
  30292. + if (!arg)
  30293. + goto out;
  30294. +
  30295. + err = -ENOMEM;
  30296. + buf = (void *)__get_free_page(GFP_NOFS);
  30297. + if (unlikely(!buf))
  30298. + goto out;
  30299. +
  30300. + seq = au_seq(buf, PAGE_SIZE);
  30301. + err = PTR_ERR(seq);
  30302. + if (IS_ERR(seq))
  30303. + goto out_buf;
  30304. +
  30305. + sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
  30306. + for (bindex = 0; bindex <= bbot; bindex++, arg++) {
  30307. + err = !access_ok(VERIFY_WRITE, arg, sizeof(*arg));
  30308. + if (unlikely(err))
  30309. + break;
  30310. +
  30311. + br = au_sbr(sb, bindex);
  30312. + brid = br->br_id;
  30313. + BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
  30314. + err = __put_user(brid, &arg->id);
  30315. + if (unlikely(err))
  30316. + break;
  30317. +
  30318. + BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
  30319. + err = __put_user(br->br_perm, &arg->perm);
  30320. + if (unlikely(err))
  30321. + break;
  30322. +
  30323. + err = au_seq_path(seq, &br->br_path);
  30324. + if (unlikely(err))
  30325. + break;
  30326. + seq_putc(seq, '\0');
  30327. + if (!seq_has_overflowed(seq)) {
  30328. + err = copy_to_user(arg->path, seq->buf, seq->count);
  30329. + seq->count = 0;
  30330. + if (unlikely(err))
  30331. + break;
  30332. + } else {
  30333. + err = -E2BIG;
  30334. + goto out_seq;
  30335. + }
  30336. + }
  30337. + if (unlikely(err))
  30338. + err = -EFAULT;
  30339. +
  30340. +out_seq:
  30341. + kfree(seq);
  30342. +out_buf:
  30343. + free_page((unsigned long)buf);
  30344. +out:
  30345. + si_read_unlock(sb);
  30346. + return err;
  30347. +}
  30348. +
  30349. +long au_brinfo_ioctl(struct file *file, unsigned long arg)
  30350. +{
  30351. + return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
  30352. +}
  30353. +
  30354. +#ifdef CONFIG_COMPAT
  30355. +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
  30356. +{
  30357. + return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
  30358. +}
  30359. +#endif
  30360. +
  30361. +/* ---------------------------------------------------------------------- */
  30362. +
  30363. +void sysaufs_br_init(struct au_branch *br)
  30364. +{
  30365. + int i;
  30366. + struct au_brsysfs *br_sysfs;
  30367. + struct attribute *attr;
  30368. +
  30369. + br_sysfs = br->br_sysfs;
  30370. + for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
  30371. + attr = &br_sysfs->attr;
  30372. + sysfs_attr_init(attr);
  30373. + attr->name = br_sysfs->name;
  30374. + attr->mode = 0444;
  30375. + br_sysfs++;
  30376. + }
  30377. +}
  30378. +
  30379. +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
  30380. +{
  30381. + struct au_branch *br;
  30382. + struct kobject *kobj;
  30383. + struct au_brsysfs *br_sysfs;
  30384. + int i;
  30385. + aufs_bindex_t bbot;
  30386. +
  30387. + if (!sysaufs_brs)
  30388. + return;
  30389. +
  30390. + kobj = &au_sbi(sb)->si_kobj;
  30391. + bbot = au_sbbot(sb);
  30392. + for (; bindex <= bbot; bindex++) {
  30393. + br = au_sbr(sb, bindex);
  30394. + br_sysfs = br->br_sysfs;
  30395. + for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
  30396. + sysfs_remove_file(kobj, &br_sysfs->attr);
  30397. + br_sysfs++;
  30398. + }
  30399. + }
  30400. +}
  30401. +
  30402. +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
  30403. +{
  30404. + int err, i;
  30405. + aufs_bindex_t bbot;
  30406. + struct kobject *kobj;
  30407. + struct au_branch *br;
  30408. + struct au_brsysfs *br_sysfs;
  30409. +
  30410. + if (!sysaufs_brs)
  30411. + return;
  30412. +
  30413. + kobj = &au_sbi(sb)->si_kobj;
  30414. + bbot = au_sbbot(sb);
  30415. + for (; bindex <= bbot; bindex++) {
  30416. + br = au_sbr(sb, bindex);
  30417. + br_sysfs = br->br_sysfs;
  30418. + snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
  30419. + SysaufsBr_PREFIX "%d", bindex);
  30420. + snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
  30421. + SysaufsBrid_PREFIX "%d", bindex);
  30422. + for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
  30423. + err = sysfs_create_file(kobj, &br_sysfs->attr);
  30424. + if (unlikely(err))
  30425. + pr_warn("failed %s under sysfs(%d)\n",
  30426. + br_sysfs->name, err);
  30427. + br_sysfs++;
  30428. + }
  30429. + }
  30430. +}
  30431. --- /dev/null
  30432. +++ linux-4.19/fs/aufs/sysrq.c 2018-11-20 09:35:15.044195505 +0200
  30433. @@ -0,0 +1,160 @@
  30434. +// SPDX-License-Identifier: GPL-2.0
  30435. +/*
  30436. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  30437. + *
  30438. + * This program, aufs is free software; you can redistribute it and/or modify
  30439. + * it under the terms of the GNU General Public License as published by
  30440. + * the Free Software Foundation; either version 2 of the License, or
  30441. + * (at your option) any later version.
  30442. + *
  30443. + * This program is distributed in the hope that it will be useful,
  30444. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30445. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30446. + * GNU General Public License for more details.
  30447. + *
  30448. + * You should have received a copy of the GNU General Public License
  30449. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30450. + */
  30451. +
  30452. +/*
  30453. + * magic sysrq handler
  30454. + */
  30455. +
  30456. +/* #include <linux/sysrq.h> */
  30457. +#include <linux/writeback.h>
  30458. +#include "aufs.h"
  30459. +
  30460. +/* ---------------------------------------------------------------------- */
  30461. +
  30462. +static void sysrq_sb(struct super_block *sb)
  30463. +{
  30464. + char *plevel;
  30465. + struct au_sbinfo *sbinfo;
  30466. + struct file *file;
  30467. + struct hlist_bl_head *files;
  30468. + struct hlist_bl_node *pos;
  30469. + struct au_finfo *finfo;
  30470. +
  30471. + plevel = au_plevel;
  30472. + au_plevel = KERN_WARNING;
  30473. +
  30474. + /* since we define pr_fmt, call printk directly */
  30475. +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
  30476. +
  30477. + sbinfo = au_sbi(sb);
  30478. + printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
  30479. + pr("superblock\n");
  30480. + au_dpri_sb(sb);
  30481. +
  30482. +#if 0
  30483. + pr("root dentry\n");
  30484. + au_dpri_dentry(sb->s_root);
  30485. + pr("root inode\n");
  30486. + au_dpri_inode(d_inode(sb->s_root));
  30487. +#endif
  30488. +
  30489. +#if 0
  30490. + do {
  30491. + int err, i, j, ndentry;
  30492. + struct au_dcsub_pages dpages;
  30493. + struct au_dpage *dpage;
  30494. +
  30495. + err = au_dpages_init(&dpages, GFP_ATOMIC);
  30496. + if (unlikely(err))
  30497. + break;
  30498. + err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
  30499. + if (!err)
  30500. + for (i = 0; i < dpages.ndpage; i++) {
  30501. + dpage = dpages.dpages + i;
  30502. + ndentry = dpage->ndentry;
  30503. + for (j = 0; j < ndentry; j++)
  30504. + au_dpri_dentry(dpage->dentries[j]);
  30505. + }
  30506. + au_dpages_free(&dpages);
  30507. + } while (0);
  30508. +#endif
  30509. +
  30510. +#if 1
  30511. + {
  30512. + struct inode *i;
  30513. +
  30514. + pr("isolated inode\n");
  30515. + spin_lock(&sb->s_inode_list_lock);
  30516. + list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
  30517. + spin_lock(&i->i_lock);
  30518. + if (1 || hlist_empty(&i->i_dentry))
  30519. + au_dpri_inode(i);
  30520. + spin_unlock(&i->i_lock);
  30521. + }
  30522. + spin_unlock(&sb->s_inode_list_lock);
  30523. + }
  30524. +#endif
  30525. + pr("files\n");
  30526. + files = &au_sbi(sb)->si_files;
  30527. + hlist_bl_lock(files);
  30528. + hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
  30529. + umode_t mode;
  30530. +
  30531. + file = finfo->fi_file;
  30532. + mode = file_inode(file)->i_mode;
  30533. + if (!special_file(mode))
  30534. + au_dpri_file(file);
  30535. + }
  30536. + hlist_bl_unlock(files);
  30537. + pr("done\n");
  30538. +
  30539. +#undef pr
  30540. + au_plevel = plevel;
  30541. +}
  30542. +
  30543. +/* ---------------------------------------------------------------------- */
  30544. +
  30545. +/* module parameter */
  30546. +static char *aufs_sysrq_key = "a";
  30547. +module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
  30548. +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
  30549. +
  30550. +static void au_sysrq(int key __maybe_unused)
  30551. +{
  30552. + struct au_sbinfo *sbinfo;
  30553. + struct hlist_bl_node *pos;
  30554. +
  30555. + lockdep_off();
  30556. + au_sbilist_lock();
  30557. + hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
  30558. + sysrq_sb(sbinfo->si_sb);
  30559. + au_sbilist_unlock();
  30560. + lockdep_on();
  30561. +}
  30562. +
  30563. +static struct sysrq_key_op au_sysrq_op = {
  30564. + .handler = au_sysrq,
  30565. + .help_msg = "Aufs",
  30566. + .action_msg = "Aufs",
  30567. + .enable_mask = SYSRQ_ENABLE_DUMP
  30568. +};
  30569. +
  30570. +/* ---------------------------------------------------------------------- */
  30571. +
  30572. +int __init au_sysrq_init(void)
  30573. +{
  30574. + int err;
  30575. + char key;
  30576. +
  30577. + err = -1;
  30578. + key = *aufs_sysrq_key;
  30579. + if ('a' <= key && key <= 'z')
  30580. + err = register_sysrq_key(key, &au_sysrq_op);
  30581. + if (unlikely(err))
  30582. + pr_err("err %d, sysrq=%c\n", err, key);
  30583. + return err;
  30584. +}
  30585. +
  30586. +void au_sysrq_fin(void)
  30587. +{
  30588. + int err;
  30589. +
  30590. + err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
  30591. + if (unlikely(err))
  30592. + pr_err("err %d (ignored)\n", err);
  30593. +}
  30594. --- /dev/null
  30595. +++ linux-4.19/fs/aufs/vdir.c 2018-11-20 09:35:15.045195505 +0200
  30596. @@ -0,0 +1,895 @@
  30597. +// SPDX-License-Identifier: GPL-2.0
  30598. +/*
  30599. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  30600. + *
  30601. + * This program, aufs is free software; you can redistribute it and/or modify
  30602. + * it under the terms of the GNU General Public License as published by
  30603. + * the Free Software Foundation; either version 2 of the License, or
  30604. + * (at your option) any later version.
  30605. + *
  30606. + * This program is distributed in the hope that it will be useful,
  30607. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30608. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30609. + * GNU General Public License for more details.
  30610. + *
  30611. + * You should have received a copy of the GNU General Public License
  30612. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30613. + */
  30614. +
  30615. +/*
  30616. + * virtual or vertical directory
  30617. + */
  30618. +
  30619. +#include "aufs.h"
  30620. +
  30621. +static unsigned int calc_size(int nlen)
  30622. +{
  30623. + return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
  30624. +}
  30625. +
  30626. +static int set_deblk_end(union au_vdir_deblk_p *p,
  30627. + union au_vdir_deblk_p *deblk_end)
  30628. +{
  30629. + if (calc_size(0) <= deblk_end->deblk - p->deblk) {
  30630. + p->de->de_str.len = 0;
  30631. + /* smp_mb(); */
  30632. + return 0;
  30633. + }
  30634. + return -1; /* error */
  30635. +}
  30636. +
  30637. +/* returns true or false */
  30638. +static int is_deblk_end(union au_vdir_deblk_p *p,
  30639. + union au_vdir_deblk_p *deblk_end)
  30640. +{
  30641. + if (calc_size(0) <= deblk_end->deblk - p->deblk)
  30642. + return !p->de->de_str.len;
  30643. + return 1;
  30644. +}
  30645. +
  30646. +static unsigned char *last_deblk(struct au_vdir *vdir)
  30647. +{
  30648. + return vdir->vd_deblk[vdir->vd_nblk - 1];
  30649. +}
  30650. +
  30651. +/* ---------------------------------------------------------------------- */
  30652. +
  30653. +/* estimate the appropriate size for name hash table */
  30654. +unsigned int au_rdhash_est(loff_t sz)
  30655. +{
  30656. + unsigned int n;
  30657. +
  30658. + n = UINT_MAX;
  30659. + sz >>= 10;
  30660. + if (sz < n)
  30661. + n = sz;
  30662. + if (sz < AUFS_RDHASH_DEF)
  30663. + n = AUFS_RDHASH_DEF;
  30664. + /* pr_info("n %u\n", n); */
  30665. + return n;
  30666. +}
  30667. +
  30668. +/*
  30669. + * the allocated memory has to be freed by
  30670. + * au_nhash_wh_free() or au_nhash_de_free().
  30671. + */
  30672. +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
  30673. +{
  30674. + struct hlist_head *head;
  30675. + unsigned int u;
  30676. + size_t sz;
  30677. +
  30678. + sz = sizeof(*nhash->nh_head) * num_hash;
  30679. + head = kmalloc(sz, gfp);
  30680. + if (head) {
  30681. + nhash->nh_num = num_hash;
  30682. + nhash->nh_head = head;
  30683. + for (u = 0; u < num_hash; u++)
  30684. + INIT_HLIST_HEAD(head++);
  30685. + return 0; /* success */
  30686. + }
  30687. +
  30688. + return -ENOMEM;
  30689. +}
  30690. +
  30691. +static void nhash_count(struct hlist_head *head)
  30692. +{
  30693. +#if 0
  30694. + unsigned long n;
  30695. + struct hlist_node *pos;
  30696. +
  30697. + n = 0;
  30698. + hlist_for_each(pos, head)
  30699. + n++;
  30700. + pr_info("%lu\n", n);
  30701. +#endif
  30702. +}
  30703. +
  30704. +static void au_nhash_wh_do_free(struct hlist_head *head)
  30705. +{
  30706. + struct au_vdir_wh *pos;
  30707. + struct hlist_node *node;
  30708. +
  30709. + hlist_for_each_entry_safe(pos, node, head, wh_hash)
  30710. + kfree(pos);
  30711. +}
  30712. +
  30713. +static void au_nhash_de_do_free(struct hlist_head *head)
  30714. +{
  30715. + struct au_vdir_dehstr *pos;
  30716. + struct hlist_node *node;
  30717. +
  30718. + hlist_for_each_entry_safe(pos, node, head, hash)
  30719. + au_cache_free_vdir_dehstr(pos);
  30720. +}
  30721. +
  30722. +static void au_nhash_do_free(struct au_nhash *nhash,
  30723. + void (*free)(struct hlist_head *head))
  30724. +{
  30725. + unsigned int n;
  30726. + struct hlist_head *head;
  30727. +
  30728. + n = nhash->nh_num;
  30729. + if (!n)
  30730. + return;
  30731. +
  30732. + head = nhash->nh_head;
  30733. + while (n-- > 0) {
  30734. + nhash_count(head);
  30735. + free(head++);
  30736. + }
  30737. + kfree(nhash->nh_head);
  30738. +}
  30739. +
  30740. +void au_nhash_wh_free(struct au_nhash *whlist)
  30741. +{
  30742. + au_nhash_do_free(whlist, au_nhash_wh_do_free);
  30743. +}
  30744. +
  30745. +static void au_nhash_de_free(struct au_nhash *delist)
  30746. +{
  30747. + au_nhash_do_free(delist, au_nhash_de_do_free);
  30748. +}
  30749. +
  30750. +/* ---------------------------------------------------------------------- */
  30751. +
  30752. +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
  30753. + int limit)
  30754. +{
  30755. + int num;
  30756. + unsigned int u, n;
  30757. + struct hlist_head *head;
  30758. + struct au_vdir_wh *pos;
  30759. +
  30760. + num = 0;
  30761. + n = whlist->nh_num;
  30762. + head = whlist->nh_head;
  30763. + for (u = 0; u < n; u++, head++)
  30764. + hlist_for_each_entry(pos, head, wh_hash)
  30765. + if (pos->wh_bindex == btgt && ++num > limit)
  30766. + return 1;
  30767. + return 0;
  30768. +}
  30769. +
  30770. +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
  30771. + unsigned char *name,
  30772. + unsigned int len)
  30773. +{
  30774. + unsigned int v;
  30775. + /* const unsigned int magic_bit = 12; */
  30776. +
  30777. + AuDebugOn(!nhash->nh_num || !nhash->nh_head);
  30778. +
  30779. + v = 0;
  30780. + if (len > 8)
  30781. + len = 8;
  30782. + while (len--)
  30783. + v += *name++;
  30784. + /* v = hash_long(v, magic_bit); */
  30785. + v %= nhash->nh_num;
  30786. + return nhash->nh_head + v;
  30787. +}
  30788. +
  30789. +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
  30790. + int nlen)
  30791. +{
  30792. + return str->len == nlen && !memcmp(str->name, name, nlen);
  30793. +}
  30794. +
  30795. +/* returns found or not */
  30796. +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
  30797. +{
  30798. + struct hlist_head *head;
  30799. + struct au_vdir_wh *pos;
  30800. + struct au_vdir_destr *str;
  30801. +
  30802. + head = au_name_hash(whlist, name, nlen);
  30803. + hlist_for_each_entry(pos, head, wh_hash) {
  30804. + str = &pos->wh_str;
  30805. + AuDbg("%.*s\n", str->len, str->name);
  30806. + if (au_nhash_test_name(str, name, nlen))
  30807. + return 1;
  30808. + }
  30809. + return 0;
  30810. +}
  30811. +
  30812. +/* returns found(true) or not */
  30813. +static int test_known(struct au_nhash *delist, char *name, int nlen)
  30814. +{
  30815. + struct hlist_head *head;
  30816. + struct au_vdir_dehstr *pos;
  30817. + struct au_vdir_destr *str;
  30818. +
  30819. + head = au_name_hash(delist, name, nlen);
  30820. + hlist_for_each_entry(pos, head, hash) {
  30821. + str = pos->str;
  30822. + AuDbg("%.*s\n", str->len, str->name);
  30823. + if (au_nhash_test_name(str, name, nlen))
  30824. + return 1;
  30825. + }
  30826. + return 0;
  30827. +}
  30828. +
  30829. +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
  30830. + unsigned char d_type)
  30831. +{
  30832. +#ifdef CONFIG_AUFS_SHWH
  30833. + wh->wh_ino = ino;
  30834. + wh->wh_type = d_type;
  30835. +#endif
  30836. +}
  30837. +
  30838. +/* ---------------------------------------------------------------------- */
  30839. +
  30840. +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
  30841. + unsigned int d_type, aufs_bindex_t bindex,
  30842. + unsigned char shwh)
  30843. +{
  30844. + int err;
  30845. + struct au_vdir_destr *str;
  30846. + struct au_vdir_wh *wh;
  30847. +
  30848. + AuDbg("%.*s\n", nlen, name);
  30849. + AuDebugOn(!whlist->nh_num || !whlist->nh_head);
  30850. +
  30851. + err = -ENOMEM;
  30852. + wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
  30853. + if (unlikely(!wh))
  30854. + goto out;
  30855. +
  30856. + err = 0;
  30857. + wh->wh_bindex = bindex;
  30858. + if (shwh)
  30859. + au_shwh_init_wh(wh, ino, d_type);
  30860. + str = &wh->wh_str;
  30861. + str->len = nlen;
  30862. + memcpy(str->name, name, nlen);
  30863. + hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
  30864. + /* smp_mb(); */
  30865. +
  30866. +out:
  30867. + return err;
  30868. +}
  30869. +
  30870. +static int append_deblk(struct au_vdir *vdir)
  30871. +{
  30872. + int err;
  30873. + unsigned long ul;
  30874. + const unsigned int deblk_sz = vdir->vd_deblk_sz;
  30875. + union au_vdir_deblk_p p, deblk_end;
  30876. + unsigned char **o;
  30877. +
  30878. + err = -ENOMEM;
  30879. + o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
  30880. + GFP_NOFS, /*may_shrink*/0);
  30881. + if (unlikely(!o))
  30882. + goto out;
  30883. +
  30884. + vdir->vd_deblk = o;
  30885. + p.deblk = kmalloc(deblk_sz, GFP_NOFS);
  30886. + if (p.deblk) {
  30887. + ul = vdir->vd_nblk++;
  30888. + vdir->vd_deblk[ul] = p.deblk;
  30889. + vdir->vd_last.ul = ul;
  30890. + vdir->vd_last.p.deblk = p.deblk;
  30891. + deblk_end.deblk = p.deblk + deblk_sz;
  30892. + err = set_deblk_end(&p, &deblk_end);
  30893. + }
  30894. +
  30895. +out:
  30896. + return err;
  30897. +}
  30898. +
  30899. +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
  30900. + unsigned int d_type, struct au_nhash *delist)
  30901. +{
  30902. + int err;
  30903. + unsigned int sz;
  30904. + const unsigned int deblk_sz = vdir->vd_deblk_sz;
  30905. + union au_vdir_deblk_p p, *room, deblk_end;
  30906. + struct au_vdir_dehstr *dehstr;
  30907. +
  30908. + p.deblk = last_deblk(vdir);
  30909. + deblk_end.deblk = p.deblk + deblk_sz;
  30910. + room = &vdir->vd_last.p;
  30911. + AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
  30912. + || !is_deblk_end(room, &deblk_end));
  30913. +
  30914. + sz = calc_size(nlen);
  30915. + if (unlikely(sz > deblk_end.deblk - room->deblk)) {
  30916. + err = append_deblk(vdir);
  30917. + if (unlikely(err))
  30918. + goto out;
  30919. +
  30920. + p.deblk = last_deblk(vdir);
  30921. + deblk_end.deblk = p.deblk + deblk_sz;
  30922. + /* smp_mb(); */
  30923. + AuDebugOn(room->deblk != p.deblk);
  30924. + }
  30925. +
  30926. + err = -ENOMEM;
  30927. + dehstr = au_cache_alloc_vdir_dehstr();
  30928. + if (unlikely(!dehstr))
  30929. + goto out;
  30930. +
  30931. + dehstr->str = &room->de->de_str;
  30932. + hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
  30933. + room->de->de_ino = ino;
  30934. + room->de->de_type = d_type;
  30935. + room->de->de_str.len = nlen;
  30936. + memcpy(room->de->de_str.name, name, nlen);
  30937. +
  30938. + err = 0;
  30939. + room->deblk += sz;
  30940. + if (unlikely(set_deblk_end(room, &deblk_end)))
  30941. + err = append_deblk(vdir);
  30942. + /* smp_mb(); */
  30943. +
  30944. +out:
  30945. + return err;
  30946. +}
  30947. +
  30948. +/* ---------------------------------------------------------------------- */
  30949. +
  30950. +void au_vdir_free(struct au_vdir *vdir)
  30951. +{
  30952. + unsigned char **deblk;
  30953. +
  30954. + deblk = vdir->vd_deblk;
  30955. + while (vdir->vd_nblk--)
  30956. + kfree(*deblk++);
  30957. + kfree(vdir->vd_deblk);
  30958. + au_cache_free_vdir(vdir);
  30959. +}
  30960. +
  30961. +static struct au_vdir *alloc_vdir(struct file *file)
  30962. +{
  30963. + struct au_vdir *vdir;
  30964. + struct super_block *sb;
  30965. + int err;
  30966. +
  30967. + sb = file->f_path.dentry->d_sb;
  30968. + SiMustAnyLock(sb);
  30969. +
  30970. + err = -ENOMEM;
  30971. + vdir = au_cache_alloc_vdir();
  30972. + if (unlikely(!vdir))
  30973. + goto out;
  30974. +
  30975. + vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
  30976. + if (unlikely(!vdir->vd_deblk))
  30977. + goto out_free;
  30978. +
  30979. + vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
  30980. + if (!vdir->vd_deblk_sz) {
  30981. + /* estimate the appropriate size for deblk */
  30982. + vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
  30983. + /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
  30984. + }
  30985. + vdir->vd_nblk = 0;
  30986. + vdir->vd_version = 0;
  30987. + vdir->vd_jiffy = 0;
  30988. + err = append_deblk(vdir);
  30989. + if (!err)
  30990. + return vdir; /* success */
  30991. +
  30992. + kfree(vdir->vd_deblk);
  30993. +
  30994. +out_free:
  30995. + au_cache_free_vdir(vdir);
  30996. +out:
  30997. + vdir = ERR_PTR(err);
  30998. + return vdir;
  30999. +}
  31000. +
  31001. +static int reinit_vdir(struct au_vdir *vdir)
  31002. +{
  31003. + int err;
  31004. + union au_vdir_deblk_p p, deblk_end;
  31005. +
  31006. + while (vdir->vd_nblk > 1) {
  31007. + kfree(vdir->vd_deblk[vdir->vd_nblk - 1]);
  31008. + /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
  31009. + vdir->vd_nblk--;
  31010. + }
  31011. + p.deblk = vdir->vd_deblk[0];
  31012. + deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
  31013. + err = set_deblk_end(&p, &deblk_end);
  31014. + /* keep vd_dblk_sz */
  31015. + vdir->vd_last.ul = 0;
  31016. + vdir->vd_last.p.deblk = vdir->vd_deblk[0];
  31017. + vdir->vd_version = 0;
  31018. + vdir->vd_jiffy = 0;
  31019. + /* smp_mb(); */
  31020. + return err;
  31021. +}
  31022. +
  31023. +/* ---------------------------------------------------------------------- */
  31024. +
  31025. +#define AuFillVdir_CALLED 1
  31026. +#define AuFillVdir_WHABLE (1 << 1)
  31027. +#define AuFillVdir_SHWH (1 << 2)
  31028. +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
  31029. +#define au_fset_fillvdir(flags, name) \
  31030. + do { (flags) |= AuFillVdir_##name; } while (0)
  31031. +#define au_fclr_fillvdir(flags, name) \
  31032. + do { (flags) &= ~AuFillVdir_##name; } while (0)
  31033. +
  31034. +#ifndef CONFIG_AUFS_SHWH
  31035. +#undef AuFillVdir_SHWH
  31036. +#define AuFillVdir_SHWH 0
  31037. +#endif
  31038. +
  31039. +struct fillvdir_arg {
  31040. + struct dir_context ctx;
  31041. + struct file *file;
  31042. + struct au_vdir *vdir;
  31043. + struct au_nhash delist;
  31044. + struct au_nhash whlist;
  31045. + aufs_bindex_t bindex;
  31046. + unsigned int flags;
  31047. + int err;
  31048. +};
  31049. +
  31050. +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
  31051. + loff_t offset __maybe_unused, u64 h_ino,
  31052. + unsigned int d_type)
  31053. +{
  31054. + struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
  31055. + char *name = (void *)__name;
  31056. + struct super_block *sb;
  31057. + ino_t ino;
  31058. + const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
  31059. +
  31060. + arg->err = 0;
  31061. + sb = arg->file->f_path.dentry->d_sb;
  31062. + au_fset_fillvdir(arg->flags, CALLED);
  31063. + /* smp_mb(); */
  31064. + if (nlen <= AUFS_WH_PFX_LEN
  31065. + || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
  31066. + if (test_known(&arg->delist, name, nlen)
  31067. + || au_nhash_test_known_wh(&arg->whlist, name, nlen))
  31068. + goto out; /* already exists or whiteouted */
  31069. +
  31070. + arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
  31071. + if (!arg->err) {
  31072. + if (unlikely(nlen > AUFS_MAX_NAMELEN))
  31073. + d_type = DT_UNKNOWN;
  31074. + arg->err = append_de(arg->vdir, name, nlen, ino,
  31075. + d_type, &arg->delist);
  31076. + }
  31077. + } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
  31078. + name += AUFS_WH_PFX_LEN;
  31079. + nlen -= AUFS_WH_PFX_LEN;
  31080. + if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
  31081. + goto out; /* already whiteouted */
  31082. +
  31083. + ino = 0; /* just to suppress a warning */
  31084. + if (shwh)
  31085. + arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
  31086. + &ino);
  31087. + if (!arg->err) {
  31088. + if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
  31089. + d_type = DT_UNKNOWN;
  31090. + arg->err = au_nhash_append_wh
  31091. + (&arg->whlist, name, nlen, ino, d_type,
  31092. + arg->bindex, shwh);
  31093. + }
  31094. + }
  31095. +
  31096. +out:
  31097. + if (!arg->err)
  31098. + arg->vdir->vd_jiffy = jiffies;
  31099. + /* smp_mb(); */
  31100. + AuTraceErr(arg->err);
  31101. + return arg->err;
  31102. +}
  31103. +
  31104. +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
  31105. + struct au_nhash *whlist, struct au_nhash *delist)
  31106. +{
  31107. +#ifdef CONFIG_AUFS_SHWH
  31108. + int err;
  31109. + unsigned int nh, u;
  31110. + struct hlist_head *head;
  31111. + struct au_vdir_wh *pos;
  31112. + struct hlist_node *n;
  31113. + char *p, *o;
  31114. + struct au_vdir_destr *destr;
  31115. +
  31116. + AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
  31117. +
  31118. + err = -ENOMEM;
  31119. + o = p = (void *)__get_free_page(GFP_NOFS);
  31120. + if (unlikely(!p))
  31121. + goto out;
  31122. +
  31123. + err = 0;
  31124. + nh = whlist->nh_num;
  31125. + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
  31126. + p += AUFS_WH_PFX_LEN;
  31127. + for (u = 0; u < nh; u++) {
  31128. + head = whlist->nh_head + u;
  31129. + hlist_for_each_entry_safe(pos, n, head, wh_hash) {
  31130. + destr = &pos->wh_str;
  31131. + memcpy(p, destr->name, destr->len);
  31132. + err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
  31133. + pos->wh_ino, pos->wh_type, delist);
  31134. + if (unlikely(err))
  31135. + break;
  31136. + }
  31137. + }
  31138. +
  31139. + free_page((unsigned long)o);
  31140. +
  31141. +out:
  31142. + AuTraceErr(err);
  31143. + return err;
  31144. +#else
  31145. + return 0;
  31146. +#endif
  31147. +}
  31148. +
  31149. +static int au_do_read_vdir(struct fillvdir_arg *arg)
  31150. +{
  31151. + int err;
  31152. + unsigned int rdhash;
  31153. + loff_t offset;
  31154. + aufs_bindex_t bbot, bindex, btop;
  31155. + unsigned char shwh;
  31156. + struct file *hf, *file;
  31157. + struct super_block *sb;
  31158. +
  31159. + file = arg->file;
  31160. + sb = file->f_path.dentry->d_sb;
  31161. + SiMustAnyLock(sb);
  31162. +
  31163. + rdhash = au_sbi(sb)->si_rdhash;
  31164. + if (!rdhash)
  31165. + rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
  31166. + err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
  31167. + if (unlikely(err))
  31168. + goto out;
  31169. + err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
  31170. + if (unlikely(err))
  31171. + goto out_delist;
  31172. +
  31173. + err = 0;
  31174. + arg->flags = 0;
  31175. + shwh = 0;
  31176. + if (au_opt_test(au_mntflags(sb), SHWH)) {
  31177. + shwh = 1;
  31178. + au_fset_fillvdir(arg->flags, SHWH);
  31179. + }
  31180. + btop = au_fbtop(file);
  31181. + bbot = au_fbbot_dir(file);
  31182. + for (bindex = btop; !err && bindex <= bbot; bindex++) {
  31183. + hf = au_hf_dir(file, bindex);
  31184. + if (!hf)
  31185. + continue;
  31186. +
  31187. + offset = vfsub_llseek(hf, 0, SEEK_SET);
  31188. + err = offset;
  31189. + if (unlikely(offset))
  31190. + break;
  31191. +
  31192. + arg->bindex = bindex;
  31193. + au_fclr_fillvdir(arg->flags, WHABLE);
  31194. + if (shwh
  31195. + || (bindex != bbot
  31196. + && au_br_whable(au_sbr_perm(sb, bindex))))
  31197. + au_fset_fillvdir(arg->flags, WHABLE);
  31198. + do {
  31199. + arg->err = 0;
  31200. + au_fclr_fillvdir(arg->flags, CALLED);
  31201. + /* smp_mb(); */
  31202. + err = vfsub_iterate_dir(hf, &arg->ctx);
  31203. + if (err >= 0)
  31204. + err = arg->err;
  31205. + } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
  31206. +
  31207. + /*
  31208. + * dir_relax() may be good for concurrency, but aufs should not
  31209. + * use it since it will cause a lockdep problem.
  31210. + */
  31211. + }
  31212. +
  31213. + if (!err && shwh)
  31214. + err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
  31215. +
  31216. + au_nhash_wh_free(&arg->whlist);
  31217. +
  31218. +out_delist:
  31219. + au_nhash_de_free(&arg->delist);
  31220. +out:
  31221. + return err;
  31222. +}
  31223. +
  31224. +static int read_vdir(struct file *file, int may_read)
  31225. +{
  31226. + int err;
  31227. + unsigned long expire;
  31228. + unsigned char do_read;
  31229. + struct fillvdir_arg arg = {
  31230. + .ctx = {
  31231. + .actor = fillvdir
  31232. + }
  31233. + };
  31234. + struct inode *inode;
  31235. + struct au_vdir *vdir, *allocated;
  31236. +
  31237. + err = 0;
  31238. + inode = file_inode(file);
  31239. + IMustLock(inode);
  31240. + IiMustWriteLock(inode);
  31241. + SiMustAnyLock(inode->i_sb);
  31242. +
  31243. + allocated = NULL;
  31244. + do_read = 0;
  31245. + expire = au_sbi(inode->i_sb)->si_rdcache;
  31246. + vdir = au_ivdir(inode);
  31247. + if (!vdir) {
  31248. + do_read = 1;
  31249. + vdir = alloc_vdir(file);
  31250. + err = PTR_ERR(vdir);
  31251. + if (IS_ERR(vdir))
  31252. + goto out;
  31253. + err = 0;
  31254. + allocated = vdir;
  31255. + } else if (may_read
  31256. + && (!inode_eq_iversion(inode, vdir->vd_version)
  31257. + || time_after(jiffies, vdir->vd_jiffy + expire))) {
  31258. + do_read = 1;
  31259. + err = reinit_vdir(vdir);
  31260. + if (unlikely(err))
  31261. + goto out;
  31262. + }
  31263. +
  31264. + if (!do_read)
  31265. + return 0; /* success */
  31266. +
  31267. + arg.file = file;
  31268. + arg.vdir = vdir;
  31269. + err = au_do_read_vdir(&arg);
  31270. + if (!err) {
  31271. + /* file->f_pos = 0; */ /* todo: ctx->pos? */
  31272. + vdir->vd_version = inode_query_iversion(inode);
  31273. + vdir->vd_last.ul = 0;
  31274. + vdir->vd_last.p.deblk = vdir->vd_deblk[0];
  31275. + if (allocated)
  31276. + au_set_ivdir(inode, allocated);
  31277. + } else if (allocated)
  31278. + au_vdir_free(allocated);
  31279. +
  31280. +out:
  31281. + return err;
  31282. +}
  31283. +
  31284. +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
  31285. +{
  31286. + int err, rerr;
  31287. + unsigned long ul, n;
  31288. + const unsigned int deblk_sz = src->vd_deblk_sz;
  31289. +
  31290. + AuDebugOn(tgt->vd_nblk != 1);
  31291. +
  31292. + err = -ENOMEM;
  31293. + if (tgt->vd_nblk < src->vd_nblk) {
  31294. + unsigned char **p;
  31295. +
  31296. + p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
  31297. + GFP_NOFS, /*may_shrink*/0);
  31298. + if (unlikely(!p))
  31299. + goto out;
  31300. + tgt->vd_deblk = p;
  31301. + }
  31302. +
  31303. + if (tgt->vd_deblk_sz != deblk_sz) {
  31304. + unsigned char *p;
  31305. +
  31306. + tgt->vd_deblk_sz = deblk_sz;
  31307. + p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
  31308. + /*may_shrink*/1);
  31309. + if (unlikely(!p))
  31310. + goto out;
  31311. + tgt->vd_deblk[0] = p;
  31312. + }
  31313. + memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
  31314. + tgt->vd_version = src->vd_version;
  31315. + tgt->vd_jiffy = src->vd_jiffy;
  31316. +
  31317. + n = src->vd_nblk;
  31318. + for (ul = 1; ul < n; ul++) {
  31319. + tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
  31320. + GFP_NOFS);
  31321. + if (unlikely(!tgt->vd_deblk[ul]))
  31322. + goto out;
  31323. + tgt->vd_nblk++;
  31324. + }
  31325. + tgt->vd_nblk = n;
  31326. + tgt->vd_last.ul = tgt->vd_last.ul;
  31327. + tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
  31328. + tgt->vd_last.p.deblk += src->vd_last.p.deblk
  31329. + - src->vd_deblk[src->vd_last.ul];
  31330. + /* smp_mb(); */
  31331. + return 0; /* success */
  31332. +
  31333. +out:
  31334. + rerr = reinit_vdir(tgt);
  31335. + BUG_ON(rerr);
  31336. + return err;
  31337. +}
  31338. +
  31339. +int au_vdir_init(struct file *file)
  31340. +{
  31341. + int err;
  31342. + struct inode *inode;
  31343. + struct au_vdir *vdir_cache, *allocated;
  31344. +
  31345. + /* test file->f_pos here instead of ctx->pos */
  31346. + err = read_vdir(file, !file->f_pos);
  31347. + if (unlikely(err))
  31348. + goto out;
  31349. +
  31350. + allocated = NULL;
  31351. + vdir_cache = au_fvdir_cache(file);
  31352. + if (!vdir_cache) {
  31353. + vdir_cache = alloc_vdir(file);
  31354. + err = PTR_ERR(vdir_cache);
  31355. + if (IS_ERR(vdir_cache))
  31356. + goto out;
  31357. + allocated = vdir_cache;
  31358. + } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
  31359. + /* test file->f_pos here instead of ctx->pos */
  31360. + err = reinit_vdir(vdir_cache);
  31361. + if (unlikely(err))
  31362. + goto out;
  31363. + } else
  31364. + return 0; /* success */
  31365. +
  31366. + inode = file_inode(file);
  31367. + err = copy_vdir(vdir_cache, au_ivdir(inode));
  31368. + if (!err) {
  31369. + file->f_version = inode_query_iversion(inode);
  31370. + if (allocated)
  31371. + au_set_fvdir_cache(file, allocated);
  31372. + } else if (allocated)
  31373. + au_vdir_free(allocated);
  31374. +
  31375. +out:
  31376. + return err;
  31377. +}
  31378. +
  31379. +static loff_t calc_offset(struct au_vdir *vdir)
  31380. +{
  31381. + loff_t offset;
  31382. + union au_vdir_deblk_p p;
  31383. +
  31384. + p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
  31385. + offset = vdir->vd_last.p.deblk - p.deblk;
  31386. + offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
  31387. + return offset;
  31388. +}
  31389. +
  31390. +/* returns true or false */
  31391. +static int seek_vdir(struct file *file, struct dir_context *ctx)
  31392. +{
  31393. + int valid;
  31394. + unsigned int deblk_sz;
  31395. + unsigned long ul, n;
  31396. + loff_t offset;
  31397. + union au_vdir_deblk_p p, deblk_end;
  31398. + struct au_vdir *vdir_cache;
  31399. +
  31400. + valid = 1;
  31401. + vdir_cache = au_fvdir_cache(file);
  31402. + offset = calc_offset(vdir_cache);
  31403. + AuDbg("offset %lld\n", offset);
  31404. + if (ctx->pos == offset)
  31405. + goto out;
  31406. +
  31407. + vdir_cache->vd_last.ul = 0;
  31408. + vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
  31409. + if (!ctx->pos)
  31410. + goto out;
  31411. +
  31412. + valid = 0;
  31413. + deblk_sz = vdir_cache->vd_deblk_sz;
  31414. + ul = div64_u64(ctx->pos, deblk_sz);
  31415. + AuDbg("ul %lu\n", ul);
  31416. + if (ul >= vdir_cache->vd_nblk)
  31417. + goto out;
  31418. +
  31419. + n = vdir_cache->vd_nblk;
  31420. + for (; ul < n; ul++) {
  31421. + p.deblk = vdir_cache->vd_deblk[ul];
  31422. + deblk_end.deblk = p.deblk + deblk_sz;
  31423. + offset = ul;
  31424. + offset *= deblk_sz;
  31425. + while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
  31426. + unsigned int l;
  31427. +
  31428. + l = calc_size(p.de->de_str.len);
  31429. + offset += l;
  31430. + p.deblk += l;
  31431. + }
  31432. + if (!is_deblk_end(&p, &deblk_end)) {
  31433. + valid = 1;
  31434. + vdir_cache->vd_last.ul = ul;
  31435. + vdir_cache->vd_last.p = p;
  31436. + break;
  31437. + }
  31438. + }
  31439. +
  31440. +out:
  31441. + /* smp_mb(); */
  31442. + if (!valid)
  31443. + AuDbg("valid %d\n", !valid);
  31444. + return valid;
  31445. +}
  31446. +
  31447. +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
  31448. +{
  31449. + unsigned int l, deblk_sz;
  31450. + union au_vdir_deblk_p deblk_end;
  31451. + struct au_vdir *vdir_cache;
  31452. + struct au_vdir_de *de;
  31453. +
  31454. + if (!seek_vdir(file, ctx))
  31455. + return 0;
  31456. +
  31457. + vdir_cache = au_fvdir_cache(file);
  31458. + deblk_sz = vdir_cache->vd_deblk_sz;
  31459. + while (1) {
  31460. + deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
  31461. + deblk_end.deblk += deblk_sz;
  31462. + while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
  31463. + de = vdir_cache->vd_last.p.de;
  31464. + AuDbg("%.*s, off%lld, i%lu, dt%d\n",
  31465. + de->de_str.len, de->de_str.name, ctx->pos,
  31466. + (unsigned long)de->de_ino, de->de_type);
  31467. + if (unlikely(!dir_emit(ctx, de->de_str.name,
  31468. + de->de_str.len, de->de_ino,
  31469. + de->de_type))) {
  31470. + /* todo: ignore the error caused by udba? */
  31471. + /* return err; */
  31472. + return 0;
  31473. + }
  31474. +
  31475. + l = calc_size(de->de_str.len);
  31476. + vdir_cache->vd_last.p.deblk += l;
  31477. + ctx->pos += l;
  31478. + }
  31479. + if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
  31480. + vdir_cache->vd_last.ul++;
  31481. + vdir_cache->vd_last.p.deblk
  31482. + = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
  31483. + ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
  31484. + continue;
  31485. + }
  31486. + break;
  31487. + }
  31488. +
  31489. + /* smp_mb(); */
  31490. + return 0;
  31491. +}
  31492. --- /dev/null
  31493. +++ linux-4.19/fs/aufs/vfsub.c 2018-11-20 09:35:15.046195505 +0200
  31494. @@ -0,0 +1,902 @@
  31495. +// SPDX-License-Identifier: GPL-2.0
  31496. +/*
  31497. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  31498. + *
  31499. + * This program, aufs is free software; you can redistribute it and/or modify
  31500. + * it under the terms of the GNU General Public License as published by
  31501. + * the Free Software Foundation; either version 2 of the License, or
  31502. + * (at your option) any later version.
  31503. + *
  31504. + * This program is distributed in the hope that it will be useful,
  31505. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31506. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31507. + * GNU General Public License for more details.
  31508. + *
  31509. + * You should have received a copy of the GNU General Public License
  31510. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  31511. + */
  31512. +
  31513. +/*
  31514. + * sub-routines for VFS
  31515. + */
  31516. +
  31517. +#include <linux/mnt_namespace.h>
  31518. +#include <linux/namei.h>
  31519. +#include <linux/nsproxy.h>
  31520. +#include <linux/security.h>
  31521. +#include <linux/splice.h>
  31522. +#include "aufs.h"
  31523. +
  31524. +#ifdef CONFIG_AUFS_BR_FUSE
  31525. +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
  31526. +{
  31527. + if (!au_test_fuse(h_sb) || !au_userns)
  31528. + return 0;
  31529. +
  31530. + return is_current_mnt_ns(mnt) ? 0 : -EACCES;
  31531. +}
  31532. +#endif
  31533. +
  31534. +int vfsub_sync_filesystem(struct super_block *h_sb, int wait)
  31535. +{
  31536. + int err;
  31537. +
  31538. + lockdep_off();
  31539. + down_read(&h_sb->s_umount);
  31540. + err = __sync_filesystem(h_sb, wait);
  31541. + up_read(&h_sb->s_umount);
  31542. + lockdep_on();
  31543. +
  31544. + return err;
  31545. +}
  31546. +
  31547. +/* ---------------------------------------------------------------------- */
  31548. +
  31549. +int vfsub_update_h_iattr(struct path *h_path, int *did)
  31550. +{
  31551. + int err;
  31552. + struct kstat st;
  31553. + struct super_block *h_sb;
  31554. +
  31555. + /* for remote fs, leave work for its getattr or d_revalidate */
  31556. + /* for bad i_attr fs, handle them in aufs_getattr() */
  31557. + /* still some fs may acquire i_mutex. we need to skip them */
  31558. + err = 0;
  31559. + if (!did)
  31560. + did = &err;
  31561. + h_sb = h_path->dentry->d_sb;
  31562. + *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
  31563. + if (*did)
  31564. + err = vfsub_getattr(h_path, &st);
  31565. +
  31566. + return err;
  31567. +}
  31568. +
  31569. +/* ---------------------------------------------------------------------- */
  31570. +
  31571. +struct file *vfsub_dentry_open(struct path *path, int flags)
  31572. +{
  31573. + struct file *file;
  31574. +
  31575. + file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
  31576. + current_cred());
  31577. + if (!IS_ERR_OR_NULL(file)
  31578. + && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  31579. + i_readcount_inc(d_inode(path->dentry));
  31580. +
  31581. + return file;
  31582. +}
  31583. +
  31584. +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
  31585. +{
  31586. + struct file *file;
  31587. +
  31588. + lockdep_off();
  31589. + file = filp_open(path,
  31590. + oflags /* | __FMODE_NONOTIFY */,
  31591. + mode);
  31592. + lockdep_on();
  31593. + if (IS_ERR(file))
  31594. + goto out;
  31595. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  31596. +
  31597. +out:
  31598. + return file;
  31599. +}
  31600. +
  31601. +/*
  31602. + * Ideally this function should call VFS:do_last() in order to keep all its
  31603. + * checkings. But it is very hard for aufs to regenerate several VFS internal
  31604. + * structure such as nameidata. This is a second (or third) best approach.
  31605. + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
  31606. + */
  31607. +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
  31608. + struct vfsub_aopen_args *args)
  31609. +{
  31610. + int err;
  31611. + struct au_branch *br = args->br;
  31612. + struct file *file = args->file;
  31613. + /* copied from linux/fs/namei.c:atomic_open() */
  31614. + struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
  31615. +
  31616. + IMustLock(dir);
  31617. + AuDebugOn(!dir->i_op->atomic_open);
  31618. +
  31619. + err = au_br_test_oflag(args->open_flag, br);
  31620. + if (unlikely(err))
  31621. + goto out;
  31622. +
  31623. + au_lcnt_inc(&br->br_nfiles);
  31624. + file->f_path.dentry = DENTRY_NOT_SET;
  31625. + file->f_path.mnt = au_br_mnt(br);
  31626. + AuDbg("%ps\n", dir->i_op->atomic_open);
  31627. + err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
  31628. + args->create_mode);
  31629. + if (unlikely(err < 0)) {
  31630. + au_lcnt_dec(&br->br_nfiles);
  31631. + goto out;
  31632. + }
  31633. +
  31634. + /* temporary workaround for nfsv4 branch */
  31635. + if (au_test_nfs(dir->i_sb))
  31636. + nfs_mark_for_revalidate(dir);
  31637. +
  31638. + if (file->f_mode & FMODE_CREATED)
  31639. + fsnotify_create(dir, dentry);
  31640. + if (!(file->f_mode & FMODE_OPENED)) {
  31641. + au_lcnt_dec(&br->br_nfiles);
  31642. + goto out;
  31643. + }
  31644. +
  31645. + /* todo: call VFS:may_open() here */
  31646. + /* todo: ima_file_check() too? */
  31647. + if (!err && (args->open_flag & __FMODE_EXEC))
  31648. + err = deny_write_access(file);
  31649. + if (!err)
  31650. + fsnotify_open(file);
  31651. + else
  31652. + au_lcnt_dec(&br->br_nfiles);
  31653. + /* note that the file is created and still opened */
  31654. +
  31655. +out:
  31656. + return err;
  31657. +}
  31658. +
  31659. +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
  31660. +{
  31661. + int err;
  31662. +
  31663. + err = kern_path(name, flags, path);
  31664. + if (!err && d_is_positive(path->dentry))
  31665. + vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
  31666. + return err;
  31667. +}
  31668. +
  31669. +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
  31670. + struct dentry *parent, int len)
  31671. +{
  31672. + struct path path = {
  31673. + .mnt = NULL
  31674. + };
  31675. +
  31676. + path.dentry = lookup_one_len_unlocked(name, parent, len);
  31677. + if (IS_ERR(path.dentry))
  31678. + goto out;
  31679. + if (d_is_positive(path.dentry))
  31680. + vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
  31681. +
  31682. +out:
  31683. + AuTraceErrPtr(path.dentry);
  31684. + return path.dentry;
  31685. +}
  31686. +
  31687. +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
  31688. + int len)
  31689. +{
  31690. + struct path path = {
  31691. + .mnt = NULL
  31692. + };
  31693. +
  31694. + /* VFS checks it too, but by WARN_ON_ONCE() */
  31695. + IMustLock(d_inode(parent));
  31696. +
  31697. + path.dentry = lookup_one_len(name, parent, len);
  31698. + if (IS_ERR(path.dentry))
  31699. + goto out;
  31700. + if (d_is_positive(path.dentry))
  31701. + vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
  31702. +
  31703. +out:
  31704. + AuTraceErrPtr(path.dentry);
  31705. + return path.dentry;
  31706. +}
  31707. +
  31708. +void vfsub_call_lkup_one(void *args)
  31709. +{
  31710. + struct vfsub_lkup_one_args *a = args;
  31711. + *a->errp = vfsub_lkup_one(a->name, a->parent);
  31712. +}
  31713. +
  31714. +/* ---------------------------------------------------------------------- */
  31715. +
  31716. +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
  31717. + struct dentry *d2, struct au_hinode *hdir2)
  31718. +{
  31719. + struct dentry *d;
  31720. +
  31721. + lockdep_off();
  31722. + d = lock_rename(d1, d2);
  31723. + lockdep_on();
  31724. + au_hn_suspend(hdir1);
  31725. + if (hdir1 != hdir2)
  31726. + au_hn_suspend(hdir2);
  31727. +
  31728. + return d;
  31729. +}
  31730. +
  31731. +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
  31732. + struct dentry *d2, struct au_hinode *hdir2)
  31733. +{
  31734. + au_hn_resume(hdir1);
  31735. + if (hdir1 != hdir2)
  31736. + au_hn_resume(hdir2);
  31737. + lockdep_off();
  31738. + unlock_rename(d1, d2);
  31739. + lockdep_on();
  31740. +}
  31741. +
  31742. +/* ---------------------------------------------------------------------- */
  31743. +
  31744. +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
  31745. +{
  31746. + int err;
  31747. + struct dentry *d;
  31748. +
  31749. + IMustLock(dir);
  31750. +
  31751. + d = path->dentry;
  31752. + path->dentry = d->d_parent;
  31753. + err = security_path_mknod(path, d, mode, 0);
  31754. + path->dentry = d;
  31755. + if (unlikely(err))
  31756. + goto out;
  31757. +
  31758. + lockdep_off();
  31759. + err = vfs_create(dir, path->dentry, mode, want_excl);
  31760. + lockdep_on();
  31761. + if (!err) {
  31762. + struct path tmp = *path;
  31763. + int did;
  31764. +
  31765. + vfsub_update_h_iattr(&tmp, &did);
  31766. + if (did) {
  31767. + tmp.dentry = path->dentry->d_parent;
  31768. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  31769. + }
  31770. + /*ignore*/
  31771. + }
  31772. +
  31773. +out:
  31774. + return err;
  31775. +}
  31776. +
  31777. +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
  31778. +{
  31779. + int err;
  31780. + struct dentry *d;
  31781. +
  31782. + IMustLock(dir);
  31783. +
  31784. + d = path->dentry;
  31785. + path->dentry = d->d_parent;
  31786. + err = security_path_symlink(path, d, symname);
  31787. + path->dentry = d;
  31788. + if (unlikely(err))
  31789. + goto out;
  31790. +
  31791. + lockdep_off();
  31792. + err = vfs_symlink(dir, path->dentry, symname);
  31793. + lockdep_on();
  31794. + if (!err) {
  31795. + struct path tmp = *path;
  31796. + int did;
  31797. +
  31798. + vfsub_update_h_iattr(&tmp, &did);
  31799. + if (did) {
  31800. + tmp.dentry = path->dentry->d_parent;
  31801. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  31802. + }
  31803. + /*ignore*/
  31804. + }
  31805. +
  31806. +out:
  31807. + return err;
  31808. +}
  31809. +
  31810. +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
  31811. +{
  31812. + int err;
  31813. + struct dentry *d;
  31814. +
  31815. + IMustLock(dir);
  31816. +
  31817. + d = path->dentry;
  31818. + path->dentry = d->d_parent;
  31819. + err = security_path_mknod(path, d, mode, new_encode_dev(dev));
  31820. + path->dentry = d;
  31821. + if (unlikely(err))
  31822. + goto out;
  31823. +
  31824. + lockdep_off();
  31825. + err = vfs_mknod(dir, path->dentry, mode, dev);
  31826. + lockdep_on();
  31827. + if (!err) {
  31828. + struct path tmp = *path;
  31829. + int did;
  31830. +
  31831. + vfsub_update_h_iattr(&tmp, &did);
  31832. + if (did) {
  31833. + tmp.dentry = path->dentry->d_parent;
  31834. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  31835. + }
  31836. + /*ignore*/
  31837. + }
  31838. +
  31839. +out:
  31840. + return err;
  31841. +}
  31842. +
  31843. +static int au_test_nlink(struct inode *inode)
  31844. +{
  31845. + const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
  31846. +
  31847. + if (!au_test_fs_no_limit_nlink(inode->i_sb)
  31848. + || inode->i_nlink < link_max)
  31849. + return 0;
  31850. + return -EMLINK;
  31851. +}
  31852. +
  31853. +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
  31854. + struct inode **delegated_inode)
  31855. +{
  31856. + int err;
  31857. + struct dentry *d;
  31858. +
  31859. + IMustLock(dir);
  31860. +
  31861. + err = au_test_nlink(d_inode(src_dentry));
  31862. + if (unlikely(err))
  31863. + return err;
  31864. +
  31865. + /* we don't call may_linkat() */
  31866. + d = path->dentry;
  31867. + path->dentry = d->d_parent;
  31868. + err = security_path_link(src_dentry, path, d);
  31869. + path->dentry = d;
  31870. + if (unlikely(err))
  31871. + goto out;
  31872. +
  31873. + lockdep_off();
  31874. + err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
  31875. + lockdep_on();
  31876. + if (!err) {
  31877. + struct path tmp = *path;
  31878. + int did;
  31879. +
  31880. + /* fuse has different memory inode for the same inumber */
  31881. + vfsub_update_h_iattr(&tmp, &did);
  31882. + if (did) {
  31883. + tmp.dentry = path->dentry->d_parent;
  31884. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  31885. + tmp.dentry = src_dentry;
  31886. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  31887. + }
  31888. + /*ignore*/
  31889. + }
  31890. +
  31891. +out:
  31892. + return err;
  31893. +}
  31894. +
  31895. +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
  31896. + struct inode *dir, struct path *path,
  31897. + struct inode **delegated_inode, unsigned int flags)
  31898. +{
  31899. + int err;
  31900. + struct path tmp = {
  31901. + .mnt = path->mnt
  31902. + };
  31903. + struct dentry *d;
  31904. +
  31905. + IMustLock(dir);
  31906. + IMustLock(src_dir);
  31907. +
  31908. + d = path->dentry;
  31909. + path->dentry = d->d_parent;
  31910. + tmp.dentry = src_dentry->d_parent;
  31911. + err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
  31912. + path->dentry = d;
  31913. + if (unlikely(err))
  31914. + goto out;
  31915. +
  31916. + lockdep_off();
  31917. + err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
  31918. + delegated_inode, flags);
  31919. + lockdep_on();
  31920. + if (!err) {
  31921. + int did;
  31922. +
  31923. + tmp.dentry = d->d_parent;
  31924. + vfsub_update_h_iattr(&tmp, &did);
  31925. + if (did) {
  31926. + tmp.dentry = src_dentry;
  31927. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  31928. + tmp.dentry = src_dentry->d_parent;
  31929. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  31930. + }
  31931. + /*ignore*/
  31932. + }
  31933. +
  31934. +out:
  31935. + return err;
  31936. +}
  31937. +
  31938. +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
  31939. +{
  31940. + int err;
  31941. + struct dentry *d;
  31942. +
  31943. + IMustLock(dir);
  31944. +
  31945. + d = path->dentry;
  31946. + path->dentry = d->d_parent;
  31947. + err = security_path_mkdir(path, d, mode);
  31948. + path->dentry = d;
  31949. + if (unlikely(err))
  31950. + goto out;
  31951. +
  31952. + lockdep_off();
  31953. + err = vfs_mkdir(dir, path->dentry, mode);
  31954. + lockdep_on();
  31955. + if (!err) {
  31956. + struct path tmp = *path;
  31957. + int did;
  31958. +
  31959. + vfsub_update_h_iattr(&tmp, &did);
  31960. + if (did) {
  31961. + tmp.dentry = path->dentry->d_parent;
  31962. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  31963. + }
  31964. + /*ignore*/
  31965. + }
  31966. +
  31967. +out:
  31968. + return err;
  31969. +}
  31970. +
  31971. +int vfsub_rmdir(struct inode *dir, struct path *path)
  31972. +{
  31973. + int err;
  31974. + struct dentry *d;
  31975. +
  31976. + IMustLock(dir);
  31977. +
  31978. + d = path->dentry;
  31979. + path->dentry = d->d_parent;
  31980. + err = security_path_rmdir(path, d);
  31981. + path->dentry = d;
  31982. + if (unlikely(err))
  31983. + goto out;
  31984. +
  31985. + lockdep_off();
  31986. + err = vfs_rmdir(dir, path->dentry);
  31987. + lockdep_on();
  31988. + if (!err) {
  31989. + struct path tmp = {
  31990. + .dentry = path->dentry->d_parent,
  31991. + .mnt = path->mnt
  31992. + };
  31993. +
  31994. + vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
  31995. + }
  31996. +
  31997. +out:
  31998. + return err;
  31999. +}
  32000. +
  32001. +/* ---------------------------------------------------------------------- */
  32002. +
  32003. +/* todo: support mmap_sem? */
  32004. +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
  32005. + loff_t *ppos)
  32006. +{
  32007. + ssize_t err;
  32008. +
  32009. + lockdep_off();
  32010. + err = vfs_read(file, ubuf, count, ppos);
  32011. + lockdep_on();
  32012. + if (err >= 0)
  32013. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  32014. + return err;
  32015. +}
  32016. +
  32017. +/* todo: kernel_read()? */
  32018. +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
  32019. + loff_t *ppos)
  32020. +{
  32021. + ssize_t err;
  32022. + mm_segment_t oldfs;
  32023. + union {
  32024. + void *k;
  32025. + char __user *u;
  32026. + } buf;
  32027. +
  32028. + buf.k = kbuf;
  32029. + oldfs = get_fs();
  32030. + set_fs(KERNEL_DS);
  32031. + err = vfsub_read_u(file, buf.u, count, ppos);
  32032. + set_fs(oldfs);
  32033. + return err;
  32034. +}
  32035. +
  32036. +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
  32037. + loff_t *ppos)
  32038. +{
  32039. + ssize_t err;
  32040. +
  32041. + lockdep_off();
  32042. + err = vfs_write(file, ubuf, count, ppos);
  32043. + lockdep_on();
  32044. + if (err >= 0)
  32045. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  32046. + return err;
  32047. +}
  32048. +
  32049. +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
  32050. +{
  32051. + ssize_t err;
  32052. + mm_segment_t oldfs;
  32053. + union {
  32054. + void *k;
  32055. + const char __user *u;
  32056. + } buf;
  32057. +
  32058. + buf.k = kbuf;
  32059. + oldfs = get_fs();
  32060. + set_fs(KERNEL_DS);
  32061. + err = vfsub_write_u(file, buf.u, count, ppos);
  32062. + set_fs(oldfs);
  32063. + return err;
  32064. +}
  32065. +
  32066. +int vfsub_flush(struct file *file, fl_owner_t id)
  32067. +{
  32068. + int err;
  32069. +
  32070. + err = 0;
  32071. + if (file->f_op->flush) {
  32072. + if (!au_test_nfs(file->f_path.dentry->d_sb))
  32073. + err = file->f_op->flush(file, id);
  32074. + else {
  32075. + lockdep_off();
  32076. + err = file->f_op->flush(file, id);
  32077. + lockdep_on();
  32078. + }
  32079. + if (!err)
  32080. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
  32081. + /*ignore*/
  32082. + }
  32083. + return err;
  32084. +}
  32085. +
  32086. +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
  32087. +{
  32088. + int err;
  32089. +
  32090. + AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
  32091. +
  32092. + lockdep_off();
  32093. + err = iterate_dir(file, ctx);
  32094. + lockdep_on();
  32095. + if (err >= 0)
  32096. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  32097. +
  32098. + return err;
  32099. +}
  32100. +
  32101. +long vfsub_splice_to(struct file *in, loff_t *ppos,
  32102. + struct pipe_inode_info *pipe, size_t len,
  32103. + unsigned int flags)
  32104. +{
  32105. + long err;
  32106. +
  32107. + lockdep_off();
  32108. + err = do_splice_to(in, ppos, pipe, len, flags);
  32109. + lockdep_on();
  32110. + file_accessed(in);
  32111. + if (err >= 0)
  32112. + vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
  32113. + return err;
  32114. +}
  32115. +
  32116. +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
  32117. + loff_t *ppos, size_t len, unsigned int flags)
  32118. +{
  32119. + long err;
  32120. +
  32121. + lockdep_off();
  32122. + err = do_splice_from(pipe, out, ppos, len, flags);
  32123. + lockdep_on();
  32124. + if (err >= 0)
  32125. + vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
  32126. + return err;
  32127. +}
  32128. +
  32129. +int vfsub_fsync(struct file *file, struct path *path, int datasync)
  32130. +{
  32131. + int err;
  32132. +
  32133. + /* file can be NULL */
  32134. + lockdep_off();
  32135. + err = vfs_fsync(file, datasync);
  32136. + lockdep_on();
  32137. + if (!err) {
  32138. + if (!path) {
  32139. + AuDebugOn(!file);
  32140. + path = &file->f_path;
  32141. + }
  32142. + vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
  32143. + }
  32144. + return err;
  32145. +}
  32146. +
  32147. +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
  32148. +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
  32149. + struct file *h_file)
  32150. +{
  32151. + int err;
  32152. + struct inode *h_inode;
  32153. + struct super_block *h_sb;
  32154. +
  32155. + if (!h_file) {
  32156. + err = vfsub_truncate(h_path, length);
  32157. + goto out;
  32158. + }
  32159. +
  32160. + h_inode = d_inode(h_path->dentry);
  32161. + h_sb = h_inode->i_sb;
  32162. + lockdep_off();
  32163. + sb_start_write(h_sb);
  32164. + lockdep_on();
  32165. + err = locks_verify_truncate(h_inode, h_file, length);
  32166. + if (!err)
  32167. + err = security_path_truncate(h_path);
  32168. + if (!err) {
  32169. + lockdep_off();
  32170. + err = do_truncate(h_path->dentry, length, attr, h_file);
  32171. + lockdep_on();
  32172. + }
  32173. + lockdep_off();
  32174. + sb_end_write(h_sb);
  32175. + lockdep_on();
  32176. +
  32177. +out:
  32178. + return err;
  32179. +}
  32180. +
  32181. +/* ---------------------------------------------------------------------- */
  32182. +
  32183. +struct au_vfsub_mkdir_args {
  32184. + int *errp;
  32185. + struct inode *dir;
  32186. + struct path *path;
  32187. + int mode;
  32188. +};
  32189. +
  32190. +static void au_call_vfsub_mkdir(void *args)
  32191. +{
  32192. + struct au_vfsub_mkdir_args *a = args;
  32193. + *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
  32194. +}
  32195. +
  32196. +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
  32197. +{
  32198. + int err, do_sio, wkq_err;
  32199. +
  32200. + do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
  32201. + if (!do_sio) {
  32202. + lockdep_off();
  32203. + err = vfsub_mkdir(dir, path, mode);
  32204. + lockdep_on();
  32205. + } else {
  32206. + struct au_vfsub_mkdir_args args = {
  32207. + .errp = &err,
  32208. + .dir = dir,
  32209. + .path = path,
  32210. + .mode = mode
  32211. + };
  32212. + wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
  32213. + if (unlikely(wkq_err))
  32214. + err = wkq_err;
  32215. + }
  32216. +
  32217. + return err;
  32218. +}
  32219. +
  32220. +struct au_vfsub_rmdir_args {
  32221. + int *errp;
  32222. + struct inode *dir;
  32223. + struct path *path;
  32224. +};
  32225. +
  32226. +static void au_call_vfsub_rmdir(void *args)
  32227. +{
  32228. + struct au_vfsub_rmdir_args *a = args;
  32229. + *a->errp = vfsub_rmdir(a->dir, a->path);
  32230. +}
  32231. +
  32232. +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
  32233. +{
  32234. + int err, do_sio, wkq_err;
  32235. +
  32236. + do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
  32237. + if (!do_sio) {
  32238. + lockdep_off();
  32239. + err = vfsub_rmdir(dir, path);
  32240. + lockdep_on();
  32241. + } else {
  32242. + struct au_vfsub_rmdir_args args = {
  32243. + .errp = &err,
  32244. + .dir = dir,
  32245. + .path = path
  32246. + };
  32247. + wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
  32248. + if (unlikely(wkq_err))
  32249. + err = wkq_err;
  32250. + }
  32251. +
  32252. + return err;
  32253. +}
  32254. +
  32255. +/* ---------------------------------------------------------------------- */
  32256. +
  32257. +struct notify_change_args {
  32258. + int *errp;
  32259. + struct path *path;
  32260. + struct iattr *ia;
  32261. + struct inode **delegated_inode;
  32262. +};
  32263. +
  32264. +static void call_notify_change(void *args)
  32265. +{
  32266. + struct notify_change_args *a = args;
  32267. + struct inode *h_inode;
  32268. +
  32269. + h_inode = d_inode(a->path->dentry);
  32270. + IMustLock(h_inode);
  32271. +
  32272. + *a->errp = -EPERM;
  32273. + if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
  32274. + lockdep_off();
  32275. + *a->errp = notify_change(a->path->dentry, a->ia,
  32276. + a->delegated_inode);
  32277. + lockdep_on();
  32278. + if (!*a->errp)
  32279. + vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
  32280. + }
  32281. + AuTraceErr(*a->errp);
  32282. +}
  32283. +
  32284. +int vfsub_notify_change(struct path *path, struct iattr *ia,
  32285. + struct inode **delegated_inode)
  32286. +{
  32287. + int err;
  32288. + struct notify_change_args args = {
  32289. + .errp = &err,
  32290. + .path = path,
  32291. + .ia = ia,
  32292. + .delegated_inode = delegated_inode
  32293. + };
  32294. +
  32295. + call_notify_change(&args);
  32296. +
  32297. + return err;
  32298. +}
  32299. +
  32300. +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
  32301. + struct inode **delegated_inode)
  32302. +{
  32303. + int err, wkq_err;
  32304. + struct notify_change_args args = {
  32305. + .errp = &err,
  32306. + .path = path,
  32307. + .ia = ia,
  32308. + .delegated_inode = delegated_inode
  32309. + };
  32310. +
  32311. + wkq_err = au_wkq_wait(call_notify_change, &args);
  32312. + if (unlikely(wkq_err))
  32313. + err = wkq_err;
  32314. +
  32315. + return err;
  32316. +}
  32317. +
  32318. +/* ---------------------------------------------------------------------- */
  32319. +
  32320. +struct unlink_args {
  32321. + int *errp;
  32322. + struct inode *dir;
  32323. + struct path *path;
  32324. + struct inode **delegated_inode;
  32325. +};
  32326. +
  32327. +static void call_unlink(void *args)
  32328. +{
  32329. + struct unlink_args *a = args;
  32330. + struct dentry *d = a->path->dentry;
  32331. + struct inode *h_inode;
  32332. + const int stop_sillyrename = (au_test_nfs(d->d_sb)
  32333. + && au_dcount(d) == 1);
  32334. +
  32335. + IMustLock(a->dir);
  32336. +
  32337. + a->path->dentry = d->d_parent;
  32338. + *a->errp = security_path_unlink(a->path, d);
  32339. + a->path->dentry = d;
  32340. + if (unlikely(*a->errp))
  32341. + return;
  32342. +
  32343. + if (!stop_sillyrename)
  32344. + dget(d);
  32345. + h_inode = NULL;
  32346. + if (d_is_positive(d)) {
  32347. + h_inode = d_inode(d);
  32348. + ihold(h_inode);
  32349. + }
  32350. +
  32351. + lockdep_off();
  32352. + *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
  32353. + lockdep_on();
  32354. + if (!*a->errp) {
  32355. + struct path tmp = {
  32356. + .dentry = d->d_parent,
  32357. + .mnt = a->path->mnt
  32358. + };
  32359. + vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
  32360. + }
  32361. +
  32362. + if (!stop_sillyrename)
  32363. + dput(d);
  32364. + if (h_inode)
  32365. + iput(h_inode);
  32366. +
  32367. + AuTraceErr(*a->errp);
  32368. +}
  32369. +
  32370. +/*
  32371. + * @dir: must be locked.
  32372. + * @dentry: target dentry.
  32373. + */
  32374. +int vfsub_unlink(struct inode *dir, struct path *path,
  32375. + struct inode **delegated_inode, int force)
  32376. +{
  32377. + int err;
  32378. + struct unlink_args args = {
  32379. + .errp = &err,
  32380. + .dir = dir,
  32381. + .path = path,
  32382. + .delegated_inode = delegated_inode
  32383. + };
  32384. +
  32385. + if (!force)
  32386. + call_unlink(&args);
  32387. + else {
  32388. + int wkq_err;
  32389. +
  32390. + wkq_err = au_wkq_wait(call_unlink, &args);
  32391. + if (unlikely(wkq_err))
  32392. + err = wkq_err;
  32393. + }
  32394. +
  32395. + return err;
  32396. +}
  32397. --- /dev/null
  32398. +++ linux-4.19/fs/aufs/vfsub.h 2018-11-20 09:35:15.047195505 +0200
  32399. @@ -0,0 +1,355 @@
  32400. +/* SPDX-License-Identifier: GPL-2.0 */
  32401. +/*
  32402. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  32403. + *
  32404. + * This program, aufs is free software; you can redistribute it and/or modify
  32405. + * it under the terms of the GNU General Public License as published by
  32406. + * the Free Software Foundation; either version 2 of the License, or
  32407. + * (at your option) any later version.
  32408. + *
  32409. + * This program is distributed in the hope that it will be useful,
  32410. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32411. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32412. + * GNU General Public License for more details.
  32413. + *
  32414. + * You should have received a copy of the GNU General Public License
  32415. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  32416. + */
  32417. +
  32418. +/*
  32419. + * sub-routines for VFS
  32420. + */
  32421. +
  32422. +#ifndef __AUFS_VFSUB_H__
  32423. +#define __AUFS_VFSUB_H__
  32424. +
  32425. +#ifdef __KERNEL__
  32426. +
  32427. +#include <linux/fs.h>
  32428. +#include <linux/iversion.h>
  32429. +#include <linux/mount.h>
  32430. +#include <linux/posix_acl.h>
  32431. +#include <linux/xattr.h>
  32432. +#include "debug.h"
  32433. +
  32434. +/* copied from linux/fs/internal.h */
  32435. +/* todo: BAD approach!! */
  32436. +extern void __mnt_drop_write(struct vfsmount *);
  32437. +extern struct file *alloc_empty_file(int, const struct cred *);
  32438. +
  32439. +/* ---------------------------------------------------------------------- */
  32440. +
  32441. +/* lock subclass for lower inode */
  32442. +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
  32443. +/* reduce? gave up. */
  32444. +enum {
  32445. + AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
  32446. + AuLsc_I_PARENT, /* lower inode, parent first */
  32447. + AuLsc_I_PARENT2, /* copyup dirs */
  32448. + AuLsc_I_PARENT3, /* copyup wh */
  32449. + AuLsc_I_CHILD,
  32450. + AuLsc_I_CHILD2,
  32451. + AuLsc_I_End
  32452. +};
  32453. +
  32454. +/* to debug easier, do not make them inlined functions */
  32455. +#define MtxMustLock(mtx) AuDebugOn(!mutex_is_locked(mtx))
  32456. +#define IMustLock(i) AuDebugOn(!inode_is_locked(i))
  32457. +
  32458. +/* ---------------------------------------------------------------------- */
  32459. +
  32460. +static inline void vfsub_drop_nlink(struct inode *inode)
  32461. +{
  32462. + AuDebugOn(!inode->i_nlink);
  32463. + drop_nlink(inode);
  32464. +}
  32465. +
  32466. +static inline void vfsub_dead_dir(struct inode *inode)
  32467. +{
  32468. + AuDebugOn(!S_ISDIR(inode->i_mode));
  32469. + inode->i_flags |= S_DEAD;
  32470. + clear_nlink(inode);
  32471. +}
  32472. +
  32473. +static inline int vfsub_native_ro(struct inode *inode)
  32474. +{
  32475. + return sb_rdonly(inode->i_sb)
  32476. + || IS_RDONLY(inode)
  32477. + /* || IS_APPEND(inode) */
  32478. + || IS_IMMUTABLE(inode);
  32479. +}
  32480. +
  32481. +#ifdef CONFIG_AUFS_BR_FUSE
  32482. +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
  32483. +#else
  32484. +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
  32485. +#endif
  32486. +
  32487. +int vfsub_sync_filesystem(struct super_block *h_sb, int wait);
  32488. +
  32489. +/* ---------------------------------------------------------------------- */
  32490. +
  32491. +int vfsub_update_h_iattr(struct path *h_path, int *did);
  32492. +struct file *vfsub_dentry_open(struct path *path, int flags);
  32493. +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
  32494. +struct au_branch;
  32495. +struct vfsub_aopen_args {
  32496. + struct file *file;
  32497. + unsigned int open_flag;
  32498. + umode_t create_mode;
  32499. + struct au_branch *br;
  32500. +};
  32501. +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
  32502. + struct vfsub_aopen_args *args);
  32503. +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
  32504. +
  32505. +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
  32506. + struct dentry *parent, int len);
  32507. +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
  32508. + int len);
  32509. +
  32510. +struct vfsub_lkup_one_args {
  32511. + struct dentry **errp;
  32512. + struct qstr *name;
  32513. + struct dentry *parent;
  32514. +};
  32515. +
  32516. +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
  32517. + struct dentry *parent)
  32518. +{
  32519. + return vfsub_lookup_one_len(name->name, parent, name->len);
  32520. +}
  32521. +
  32522. +void vfsub_call_lkup_one(void *args);
  32523. +
  32524. +/* ---------------------------------------------------------------------- */
  32525. +
  32526. +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
  32527. +{
  32528. + int err;
  32529. +
  32530. + lockdep_off();
  32531. + err = mnt_want_write(mnt);
  32532. + lockdep_on();
  32533. + return err;
  32534. +}
  32535. +
  32536. +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
  32537. +{
  32538. + lockdep_off();
  32539. + mnt_drop_write(mnt);
  32540. + lockdep_on();
  32541. +}
  32542. +
  32543. +#if 0 /* reserved */
  32544. +static inline void vfsub_mnt_drop_write_file(struct file *file)
  32545. +{
  32546. + lockdep_off();
  32547. + mnt_drop_write_file(file);
  32548. + lockdep_on();
  32549. +}
  32550. +#endif
  32551. +
  32552. +/* ---------------------------------------------------------------------- */
  32553. +
  32554. +struct au_hinode;
  32555. +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
  32556. + struct dentry *d2, struct au_hinode *hdir2);
  32557. +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
  32558. + struct dentry *d2, struct au_hinode *hdir2);
  32559. +
  32560. +int vfsub_create(struct inode *dir, struct path *path, int mode,
  32561. + bool want_excl);
  32562. +int vfsub_symlink(struct inode *dir, struct path *path,
  32563. + const char *symname);
  32564. +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
  32565. +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
  32566. + struct path *path, struct inode **delegated_inode);
  32567. +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
  32568. + struct inode *hdir, struct path *path,
  32569. + struct inode **delegated_inode, unsigned int flags);
  32570. +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
  32571. +int vfsub_rmdir(struct inode *dir, struct path *path);
  32572. +
  32573. +/* ---------------------------------------------------------------------- */
  32574. +
  32575. +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
  32576. + loff_t *ppos);
  32577. +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
  32578. + loff_t *ppos);
  32579. +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
  32580. + loff_t *ppos);
  32581. +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
  32582. + loff_t *ppos);
  32583. +int vfsub_flush(struct file *file, fl_owner_t id);
  32584. +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
  32585. +
  32586. +static inline loff_t vfsub_f_size_read(struct file *file)
  32587. +{
  32588. + return i_size_read(file_inode(file));
  32589. +}
  32590. +
  32591. +static inline unsigned int vfsub_file_flags(struct file *file)
  32592. +{
  32593. + unsigned int flags;
  32594. +
  32595. + spin_lock(&file->f_lock);
  32596. + flags = file->f_flags;
  32597. + spin_unlock(&file->f_lock);
  32598. +
  32599. + return flags;
  32600. +}
  32601. +
  32602. +static inline int vfsub_file_execed(struct file *file)
  32603. +{
  32604. + /* todo: direct access f_flags */
  32605. + return !!(vfsub_file_flags(file) & __FMODE_EXEC);
  32606. +}
  32607. +
  32608. +#if 0 /* reserved */
  32609. +static inline void vfsub_file_accessed(struct file *h_file)
  32610. +{
  32611. + file_accessed(h_file);
  32612. + vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
  32613. +}
  32614. +#endif
  32615. +
  32616. +#if 0 /* reserved */
  32617. +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
  32618. + struct dentry *h_dentry)
  32619. +{
  32620. + struct path h_path = {
  32621. + .dentry = h_dentry,
  32622. + .mnt = h_mnt
  32623. + };
  32624. + touch_atime(&h_path);
  32625. + vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
  32626. +}
  32627. +#endif
  32628. +
  32629. +static inline int vfsub_update_time(struct inode *h_inode,
  32630. + struct timespec64 *ts, int flags)
  32631. +{
  32632. + return update_time(h_inode, ts, flags);
  32633. + /* no vfsub_update_h_iattr() since we don't have struct path */
  32634. +}
  32635. +
  32636. +#ifdef CONFIG_FS_POSIX_ACL
  32637. +static inline int vfsub_acl_chmod(struct inode *h_inode, umode_t h_mode)
  32638. +{
  32639. + int err;
  32640. +
  32641. + err = posix_acl_chmod(h_inode, h_mode);
  32642. + if (err == -EOPNOTSUPP)
  32643. + err = 0;
  32644. + return err;
  32645. +}
  32646. +#else
  32647. +AuStubInt0(vfsub_acl_chmod, struct inode *h_inode, umode_t h_mode);
  32648. +#endif
  32649. +
  32650. +long vfsub_splice_to(struct file *in, loff_t *ppos,
  32651. + struct pipe_inode_info *pipe, size_t len,
  32652. + unsigned int flags);
  32653. +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
  32654. + loff_t *ppos, size_t len, unsigned int flags);
  32655. +
  32656. +static inline long vfsub_truncate(struct path *path, loff_t length)
  32657. +{
  32658. + long err;
  32659. +
  32660. + lockdep_off();
  32661. + err = vfs_truncate(path, length);
  32662. + lockdep_on();
  32663. + return err;
  32664. +}
  32665. +
  32666. +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
  32667. + struct file *h_file);
  32668. +int vfsub_fsync(struct file *file, struct path *path, int datasync);
  32669. +
  32670. +/*
  32671. + * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
  32672. + * ioctl.
  32673. + */
  32674. +static inline int vfsub_clone_file_range(struct file *src, struct file *dst,
  32675. + u64 len)
  32676. +{
  32677. + int err;
  32678. +
  32679. + lockdep_off();
  32680. + err = vfs_clone_file_range(src, 0, dst, 0, len);
  32681. + lockdep_on();
  32682. +
  32683. + return err;
  32684. +}
  32685. +
  32686. +/* copy_file_range(2) is a systemcall */
  32687. +static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
  32688. + struct file *dst, loff_t dst_pos,
  32689. + size_t len, unsigned int flags)
  32690. +{
  32691. + ssize_t ssz;
  32692. +
  32693. + lockdep_off();
  32694. + ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
  32695. + lockdep_on();
  32696. +
  32697. + return ssz;
  32698. +}
  32699. +
  32700. +/* ---------------------------------------------------------------------- */
  32701. +
  32702. +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
  32703. +{
  32704. + loff_t err;
  32705. +
  32706. + lockdep_off();
  32707. + err = vfs_llseek(file, offset, origin);
  32708. + lockdep_on();
  32709. + return err;
  32710. +}
  32711. +
  32712. +/* ---------------------------------------------------------------------- */
  32713. +
  32714. +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
  32715. +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
  32716. +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
  32717. + struct inode **delegated_inode);
  32718. +int vfsub_notify_change(struct path *path, struct iattr *ia,
  32719. + struct inode **delegated_inode);
  32720. +int vfsub_unlink(struct inode *dir, struct path *path,
  32721. + struct inode **delegated_inode, int force);
  32722. +
  32723. +static inline int vfsub_getattr(const struct path *path, struct kstat *st)
  32724. +{
  32725. + return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
  32726. +}
  32727. +
  32728. +/* ---------------------------------------------------------------------- */
  32729. +
  32730. +static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
  32731. + const void *value, size_t size, int flags)
  32732. +{
  32733. + int err;
  32734. +
  32735. + lockdep_off();
  32736. + err = vfs_setxattr(dentry, name, value, size, flags);
  32737. + lockdep_on();
  32738. +
  32739. + return err;
  32740. +}
  32741. +
  32742. +static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
  32743. +{
  32744. + int err;
  32745. +
  32746. + lockdep_off();
  32747. + err = vfs_removexattr(dentry, name);
  32748. + lockdep_on();
  32749. +
  32750. + return err;
  32751. +}
  32752. +
  32753. +#endif /* __KERNEL__ */
  32754. +#endif /* __AUFS_VFSUB_H__ */
  32755. --- /dev/null
  32756. +++ linux-4.19/fs/aufs/wbr_policy.c 2018-11-20 09:35:15.048195505 +0200
  32757. @@ -0,0 +1,830 @@
  32758. +// SPDX-License-Identifier: GPL-2.0
  32759. +/*
  32760. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  32761. + *
  32762. + * This program, aufs is free software; you can redistribute it and/or modify
  32763. + * it under the terms of the GNU General Public License as published by
  32764. + * the Free Software Foundation; either version 2 of the License, or
  32765. + * (at your option) any later version.
  32766. + *
  32767. + * This program is distributed in the hope that it will be useful,
  32768. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32769. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32770. + * GNU General Public License for more details.
  32771. + *
  32772. + * You should have received a copy of the GNU General Public License
  32773. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  32774. + */
  32775. +
  32776. +/*
  32777. + * policies for selecting one among multiple writable branches
  32778. + */
  32779. +
  32780. +#include <linux/statfs.h>
  32781. +#include "aufs.h"
  32782. +
  32783. +/* subset of cpup_attr() */
  32784. +static noinline_for_stack
  32785. +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
  32786. +{
  32787. + int err, sbits;
  32788. + struct iattr ia;
  32789. + struct inode *h_isrc;
  32790. +
  32791. + h_isrc = d_inode(h_src);
  32792. + ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
  32793. + ia.ia_mode = h_isrc->i_mode;
  32794. + ia.ia_uid = h_isrc->i_uid;
  32795. + ia.ia_gid = h_isrc->i_gid;
  32796. + sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
  32797. + au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
  32798. + /* no delegation since it is just created */
  32799. + err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
  32800. +
  32801. + /* is this nfs only? */
  32802. + if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
  32803. + ia.ia_valid = ATTR_FORCE | ATTR_MODE;
  32804. + ia.ia_mode = h_isrc->i_mode;
  32805. + err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
  32806. + }
  32807. +
  32808. + return err;
  32809. +}
  32810. +
  32811. +#define AuCpdown_PARENT_OPQ 1
  32812. +#define AuCpdown_WHED (1 << 1)
  32813. +#define AuCpdown_MADE_DIR (1 << 2)
  32814. +#define AuCpdown_DIROPQ (1 << 3)
  32815. +#define au_ftest_cpdown(flags, name) ((flags) & AuCpdown_##name)
  32816. +#define au_fset_cpdown(flags, name) \
  32817. + do { (flags) |= AuCpdown_##name; } while (0)
  32818. +#define au_fclr_cpdown(flags, name) \
  32819. + do { (flags) &= ~AuCpdown_##name; } while (0)
  32820. +
  32821. +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
  32822. + unsigned int *flags)
  32823. +{
  32824. + int err;
  32825. + struct dentry *opq_dentry;
  32826. +
  32827. + opq_dentry = au_diropq_create(dentry, bdst);
  32828. + err = PTR_ERR(opq_dentry);
  32829. + if (IS_ERR(opq_dentry))
  32830. + goto out;
  32831. + dput(opq_dentry);
  32832. + au_fset_cpdown(*flags, DIROPQ);
  32833. +
  32834. +out:
  32835. + return err;
  32836. +}
  32837. +
  32838. +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
  32839. + struct inode *dir, aufs_bindex_t bdst)
  32840. +{
  32841. + int err;
  32842. + struct path h_path;
  32843. + struct au_branch *br;
  32844. +
  32845. + br = au_sbr(dentry->d_sb, bdst);
  32846. + h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
  32847. + err = PTR_ERR(h_path.dentry);
  32848. + if (IS_ERR(h_path.dentry))
  32849. + goto out;
  32850. +
  32851. + err = 0;
  32852. + if (d_is_positive(h_path.dentry)) {
  32853. + h_path.mnt = au_br_mnt(br);
  32854. + err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
  32855. + dentry);
  32856. + }
  32857. + dput(h_path.dentry);
  32858. +
  32859. +out:
  32860. + return err;
  32861. +}
  32862. +
  32863. +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
  32864. + struct au_pin *pin,
  32865. + struct dentry *h_parent, void *arg)
  32866. +{
  32867. + int err, rerr;
  32868. + aufs_bindex_t bopq, btop;
  32869. + struct path h_path;
  32870. + struct dentry *parent;
  32871. + struct inode *h_dir, *h_inode, *inode, *dir;
  32872. + unsigned int *flags = arg;
  32873. +
  32874. + btop = au_dbtop(dentry);
  32875. + /* dentry is di-locked */
  32876. + parent = dget_parent(dentry);
  32877. + dir = d_inode(parent);
  32878. + h_dir = d_inode(h_parent);
  32879. + AuDebugOn(h_dir != au_h_iptr(dir, bdst));
  32880. + IMustLock(h_dir);
  32881. +
  32882. + err = au_lkup_neg(dentry, bdst, /*wh*/0);
  32883. + if (unlikely(err < 0))
  32884. + goto out;
  32885. + h_path.dentry = au_h_dptr(dentry, bdst);
  32886. + h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
  32887. + err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
  32888. + if (unlikely(err))
  32889. + goto out_put;
  32890. + au_fset_cpdown(*flags, MADE_DIR);
  32891. +
  32892. + bopq = au_dbdiropq(dentry);
  32893. + au_fclr_cpdown(*flags, WHED);
  32894. + au_fclr_cpdown(*flags, DIROPQ);
  32895. + if (au_dbwh(dentry) == bdst)
  32896. + au_fset_cpdown(*flags, WHED);
  32897. + if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
  32898. + au_fset_cpdown(*flags, PARENT_OPQ);
  32899. + h_inode = d_inode(h_path.dentry);
  32900. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  32901. + if (au_ftest_cpdown(*flags, WHED)) {
  32902. + err = au_cpdown_dir_opq(dentry, bdst, flags);
  32903. + if (unlikely(err)) {
  32904. + inode_unlock(h_inode);
  32905. + goto out_dir;
  32906. + }
  32907. + }
  32908. +
  32909. + err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
  32910. + inode_unlock(h_inode);
  32911. + if (unlikely(err))
  32912. + goto out_opq;
  32913. +
  32914. + if (au_ftest_cpdown(*flags, WHED)) {
  32915. + err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
  32916. + if (unlikely(err))
  32917. + goto out_opq;
  32918. + }
  32919. +
  32920. + inode = d_inode(dentry);
  32921. + if (au_ibbot(inode) < bdst)
  32922. + au_set_ibbot(inode, bdst);
  32923. + au_set_h_iptr(inode, bdst, au_igrab(h_inode),
  32924. + au_hi_flags(inode, /*isdir*/1));
  32925. + au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
  32926. + goto out; /* success */
  32927. +
  32928. + /* revert */
  32929. +out_opq:
  32930. + if (au_ftest_cpdown(*flags, DIROPQ)) {
  32931. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  32932. + rerr = au_diropq_remove(dentry, bdst);
  32933. + inode_unlock(h_inode);
  32934. + if (unlikely(rerr)) {
  32935. + AuIOErr("failed removing diropq for %pd b%d (%d)\n",
  32936. + dentry, bdst, rerr);
  32937. + err = -EIO;
  32938. + goto out;
  32939. + }
  32940. + }
  32941. +out_dir:
  32942. + if (au_ftest_cpdown(*flags, MADE_DIR)) {
  32943. + rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
  32944. + if (unlikely(rerr)) {
  32945. + AuIOErr("failed removing %pd b%d (%d)\n",
  32946. + dentry, bdst, rerr);
  32947. + err = -EIO;
  32948. + }
  32949. + }
  32950. +out_put:
  32951. + au_set_h_dptr(dentry, bdst, NULL);
  32952. + if (au_dbbot(dentry) == bdst)
  32953. + au_update_dbbot(dentry);
  32954. +out:
  32955. + dput(parent);
  32956. + return err;
  32957. +}
  32958. +
  32959. +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
  32960. +{
  32961. + int err;
  32962. + unsigned int flags;
  32963. +
  32964. + flags = 0;
  32965. + err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
  32966. +
  32967. + return err;
  32968. +}
  32969. +
  32970. +/* ---------------------------------------------------------------------- */
  32971. +
  32972. +/* policies for create */
  32973. +
  32974. +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
  32975. +{
  32976. + int err, i, j, ndentry;
  32977. + aufs_bindex_t bopq;
  32978. + struct au_dcsub_pages dpages;
  32979. + struct au_dpage *dpage;
  32980. + struct dentry **dentries, *parent, *d;
  32981. +
  32982. + err = au_dpages_init(&dpages, GFP_NOFS);
  32983. + if (unlikely(err))
  32984. + goto out;
  32985. + parent = dget_parent(dentry);
  32986. + err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
  32987. + if (unlikely(err))
  32988. + goto out_free;
  32989. +
  32990. + err = bindex;
  32991. + for (i = 0; i < dpages.ndpage; i++) {
  32992. + dpage = dpages.dpages + i;
  32993. + dentries = dpage->dentries;
  32994. + ndentry = dpage->ndentry;
  32995. + for (j = 0; j < ndentry; j++) {
  32996. + d = dentries[j];
  32997. + di_read_lock_parent2(d, !AuLock_IR);
  32998. + bopq = au_dbdiropq(d);
  32999. + di_read_unlock(d, !AuLock_IR);
  33000. + if (bopq >= 0 && bopq < err)
  33001. + err = bopq;
  33002. + }
  33003. + }
  33004. +
  33005. +out_free:
  33006. + dput(parent);
  33007. + au_dpages_free(&dpages);
  33008. +out:
  33009. + return err;
  33010. +}
  33011. +
  33012. +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
  33013. +{
  33014. + for (; bindex >= 0; bindex--)
  33015. + if (!au_br_rdonly(au_sbr(sb, bindex)))
  33016. + return bindex;
  33017. + return -EROFS;
  33018. +}
  33019. +
  33020. +/* top down parent */
  33021. +static int au_wbr_create_tdp(struct dentry *dentry,
  33022. + unsigned int flags __maybe_unused)
  33023. +{
  33024. + int err;
  33025. + aufs_bindex_t btop, bindex;
  33026. + struct super_block *sb;
  33027. + struct dentry *parent, *h_parent;
  33028. +
  33029. + sb = dentry->d_sb;
  33030. + btop = au_dbtop(dentry);
  33031. + err = btop;
  33032. + if (!au_br_rdonly(au_sbr(sb, btop)))
  33033. + goto out;
  33034. +
  33035. + err = -EROFS;
  33036. + parent = dget_parent(dentry);
  33037. + for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
  33038. + h_parent = au_h_dptr(parent, bindex);
  33039. + if (!h_parent || d_is_negative(h_parent))
  33040. + continue;
  33041. +
  33042. + if (!au_br_rdonly(au_sbr(sb, bindex))) {
  33043. + err = bindex;
  33044. + break;
  33045. + }
  33046. + }
  33047. + dput(parent);
  33048. +
  33049. + /* bottom up here */
  33050. + if (unlikely(err < 0)) {
  33051. + err = au_wbr_bu(sb, btop - 1);
  33052. + if (err >= 0)
  33053. + err = au_wbr_nonopq(dentry, err);
  33054. + }
  33055. +
  33056. +out:
  33057. + AuDbg("b%d\n", err);
  33058. + return err;
  33059. +}
  33060. +
  33061. +/* ---------------------------------------------------------------------- */
  33062. +
  33063. +/* an exception for the policy other than tdp */
  33064. +static int au_wbr_create_exp(struct dentry *dentry)
  33065. +{
  33066. + int err;
  33067. + aufs_bindex_t bwh, bdiropq;
  33068. + struct dentry *parent;
  33069. +
  33070. + err = -1;
  33071. + bwh = au_dbwh(dentry);
  33072. + parent = dget_parent(dentry);
  33073. + bdiropq = au_dbdiropq(parent);
  33074. + if (bwh >= 0) {
  33075. + if (bdiropq >= 0)
  33076. + err = min(bdiropq, bwh);
  33077. + else
  33078. + err = bwh;
  33079. + AuDbg("%d\n", err);
  33080. + } else if (bdiropq >= 0) {
  33081. + err = bdiropq;
  33082. + AuDbg("%d\n", err);
  33083. + }
  33084. + dput(parent);
  33085. +
  33086. + if (err >= 0)
  33087. + err = au_wbr_nonopq(dentry, err);
  33088. +
  33089. + if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
  33090. + err = -1;
  33091. +
  33092. + AuDbg("%d\n", err);
  33093. + return err;
  33094. +}
  33095. +
  33096. +/* ---------------------------------------------------------------------- */
  33097. +
  33098. +/* round robin */
  33099. +static int au_wbr_create_init_rr(struct super_block *sb)
  33100. +{
  33101. + int err;
  33102. +
  33103. + err = au_wbr_bu(sb, au_sbbot(sb));
  33104. + atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
  33105. + /* smp_mb(); */
  33106. +
  33107. + AuDbg("b%d\n", err);
  33108. + return err;
  33109. +}
  33110. +
  33111. +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
  33112. +{
  33113. + int err, nbr;
  33114. + unsigned int u;
  33115. + aufs_bindex_t bindex, bbot;
  33116. + struct super_block *sb;
  33117. + atomic_t *next;
  33118. +
  33119. + err = au_wbr_create_exp(dentry);
  33120. + if (err >= 0)
  33121. + goto out;
  33122. +
  33123. + sb = dentry->d_sb;
  33124. + next = &au_sbi(sb)->si_wbr_rr_next;
  33125. + bbot = au_sbbot(sb);
  33126. + nbr = bbot + 1;
  33127. + for (bindex = 0; bindex <= bbot; bindex++) {
  33128. + if (!au_ftest_wbr(flags, DIR)) {
  33129. + err = atomic_dec_return(next) + 1;
  33130. + /* modulo for 0 is meaningless */
  33131. + if (unlikely(!err))
  33132. + err = atomic_dec_return(next) + 1;
  33133. + } else
  33134. + err = atomic_read(next);
  33135. + AuDbg("%d\n", err);
  33136. + u = err;
  33137. + err = u % nbr;
  33138. + AuDbg("%d\n", err);
  33139. + if (!au_br_rdonly(au_sbr(sb, err)))
  33140. + break;
  33141. + err = -EROFS;
  33142. + }
  33143. +
  33144. + if (err >= 0)
  33145. + err = au_wbr_nonopq(dentry, err);
  33146. +
  33147. +out:
  33148. + AuDbg("%d\n", err);
  33149. + return err;
  33150. +}
  33151. +
  33152. +/* ---------------------------------------------------------------------- */
  33153. +
  33154. +/* most free space */
  33155. +static void au_mfs(struct dentry *dentry, struct dentry *parent)
  33156. +{
  33157. + struct super_block *sb;
  33158. + struct au_branch *br;
  33159. + struct au_wbr_mfs *mfs;
  33160. + struct dentry *h_parent;
  33161. + aufs_bindex_t bindex, bbot;
  33162. + int err;
  33163. + unsigned long long b, bavail;
  33164. + struct path h_path;
  33165. + /* reduce the stack usage */
  33166. + struct kstatfs *st;
  33167. +
  33168. + st = kmalloc(sizeof(*st), GFP_NOFS);
  33169. + if (unlikely(!st)) {
  33170. + AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
  33171. + return;
  33172. + }
  33173. +
  33174. + bavail = 0;
  33175. + sb = dentry->d_sb;
  33176. + mfs = &au_sbi(sb)->si_wbr_mfs;
  33177. + MtxMustLock(&mfs->mfs_lock);
  33178. + mfs->mfs_bindex = -EROFS;
  33179. + mfs->mfsrr_bytes = 0;
  33180. + if (!parent) {
  33181. + bindex = 0;
  33182. + bbot = au_sbbot(sb);
  33183. + } else {
  33184. + bindex = au_dbtop(parent);
  33185. + bbot = au_dbtaildir(parent);
  33186. + }
  33187. +
  33188. + for (; bindex <= bbot; bindex++) {
  33189. + if (parent) {
  33190. + h_parent = au_h_dptr(parent, bindex);
  33191. + if (!h_parent || d_is_negative(h_parent))
  33192. + continue;
  33193. + }
  33194. + br = au_sbr(sb, bindex);
  33195. + if (au_br_rdonly(br))
  33196. + continue;
  33197. +
  33198. + /* sb->s_root for NFS is unreliable */
  33199. + h_path.mnt = au_br_mnt(br);
  33200. + h_path.dentry = h_path.mnt->mnt_root;
  33201. + err = vfs_statfs(&h_path, st);
  33202. + if (unlikely(err)) {
  33203. + AuWarn1("failed statfs, b%d, %d\n", bindex, err);
  33204. + continue;
  33205. + }
  33206. +
  33207. + /* when the available size is equal, select the lower one */
  33208. + BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
  33209. + || sizeof(b) < sizeof(st->f_bsize));
  33210. + b = st->f_bavail * st->f_bsize;
  33211. + br->br_wbr->wbr_bytes = b;
  33212. + if (b >= bavail) {
  33213. + bavail = b;
  33214. + mfs->mfs_bindex = bindex;
  33215. + mfs->mfs_jiffy = jiffies;
  33216. + }
  33217. + }
  33218. +
  33219. + mfs->mfsrr_bytes = bavail;
  33220. + AuDbg("b%d\n", mfs->mfs_bindex);
  33221. + kfree(st);
  33222. +}
  33223. +
  33224. +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
  33225. +{
  33226. + int err;
  33227. + struct dentry *parent;
  33228. + struct super_block *sb;
  33229. + struct au_wbr_mfs *mfs;
  33230. +
  33231. + err = au_wbr_create_exp(dentry);
  33232. + if (err >= 0)
  33233. + goto out;
  33234. +
  33235. + sb = dentry->d_sb;
  33236. + parent = NULL;
  33237. + if (au_ftest_wbr(flags, PARENT))
  33238. + parent = dget_parent(dentry);
  33239. + mfs = &au_sbi(sb)->si_wbr_mfs;
  33240. + mutex_lock(&mfs->mfs_lock);
  33241. + if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
  33242. + || mfs->mfs_bindex < 0
  33243. + || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
  33244. + au_mfs(dentry, parent);
  33245. + mutex_unlock(&mfs->mfs_lock);
  33246. + err = mfs->mfs_bindex;
  33247. + dput(parent);
  33248. +
  33249. + if (err >= 0)
  33250. + err = au_wbr_nonopq(dentry, err);
  33251. +
  33252. +out:
  33253. + AuDbg("b%d\n", err);
  33254. + return err;
  33255. +}
  33256. +
  33257. +static int au_wbr_create_init_mfs(struct super_block *sb)
  33258. +{
  33259. + struct au_wbr_mfs *mfs;
  33260. +
  33261. + mfs = &au_sbi(sb)->si_wbr_mfs;
  33262. + mutex_init(&mfs->mfs_lock);
  33263. + mfs->mfs_jiffy = 0;
  33264. + mfs->mfs_bindex = -EROFS;
  33265. +
  33266. + return 0;
  33267. +}
  33268. +
  33269. +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
  33270. +{
  33271. + mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
  33272. + return 0;
  33273. +}
  33274. +
  33275. +/* ---------------------------------------------------------------------- */
  33276. +
  33277. +/* top down regardless parent, and then mfs */
  33278. +static int au_wbr_create_tdmfs(struct dentry *dentry,
  33279. + unsigned int flags __maybe_unused)
  33280. +{
  33281. + int err;
  33282. + aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
  33283. + unsigned long long watermark;
  33284. + struct super_block *sb;
  33285. + struct au_wbr_mfs *mfs;
  33286. + struct au_branch *br;
  33287. + struct dentry *parent;
  33288. +
  33289. + sb = dentry->d_sb;
  33290. + mfs = &au_sbi(sb)->si_wbr_mfs;
  33291. + mutex_lock(&mfs->mfs_lock);
  33292. + if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
  33293. + || mfs->mfs_bindex < 0)
  33294. + au_mfs(dentry, /*parent*/NULL);
  33295. + watermark = mfs->mfsrr_watermark;
  33296. + bmfs = mfs->mfs_bindex;
  33297. + mutex_unlock(&mfs->mfs_lock);
  33298. +
  33299. + /* another style of au_wbr_create_exp() */
  33300. + bwh = au_dbwh(dentry);
  33301. + parent = dget_parent(dentry);
  33302. + btail = au_dbtaildir(parent);
  33303. + if (bwh >= 0 && bwh < btail)
  33304. + btail = bwh;
  33305. +
  33306. + err = au_wbr_nonopq(dentry, btail);
  33307. + if (unlikely(err < 0))
  33308. + goto out;
  33309. + btail = err;
  33310. + bfound = -1;
  33311. + for (bindex = 0; bindex <= btail; bindex++) {
  33312. + br = au_sbr(sb, bindex);
  33313. + if (au_br_rdonly(br))
  33314. + continue;
  33315. + if (br->br_wbr->wbr_bytes > watermark) {
  33316. + bfound = bindex;
  33317. + break;
  33318. + }
  33319. + }
  33320. + err = bfound;
  33321. + if (err < 0)
  33322. + err = bmfs;
  33323. +
  33324. +out:
  33325. + dput(parent);
  33326. + AuDbg("b%d\n", err);
  33327. + return err;
  33328. +}
  33329. +
  33330. +/* ---------------------------------------------------------------------- */
  33331. +
  33332. +/* most free space and then round robin */
  33333. +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
  33334. +{
  33335. + int err;
  33336. + struct au_wbr_mfs *mfs;
  33337. +
  33338. + err = au_wbr_create_mfs(dentry, flags);
  33339. + if (err >= 0) {
  33340. + mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
  33341. + mutex_lock(&mfs->mfs_lock);
  33342. + if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
  33343. + err = au_wbr_create_rr(dentry, flags);
  33344. + mutex_unlock(&mfs->mfs_lock);
  33345. + }
  33346. +
  33347. + AuDbg("b%d\n", err);
  33348. + return err;
  33349. +}
  33350. +
  33351. +static int au_wbr_create_init_mfsrr(struct super_block *sb)
  33352. +{
  33353. + int err;
  33354. +
  33355. + au_wbr_create_init_mfs(sb); /* ignore */
  33356. + err = au_wbr_create_init_rr(sb);
  33357. +
  33358. + return err;
  33359. +}
  33360. +
  33361. +/* ---------------------------------------------------------------------- */
  33362. +
  33363. +/* top down parent and most free space */
  33364. +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
  33365. +{
  33366. + int err, e2;
  33367. + unsigned long long b;
  33368. + aufs_bindex_t bindex, btop, bbot;
  33369. + struct super_block *sb;
  33370. + struct dentry *parent, *h_parent;
  33371. + struct au_branch *br;
  33372. +
  33373. + err = au_wbr_create_tdp(dentry, flags);
  33374. + if (unlikely(err < 0))
  33375. + goto out;
  33376. + parent = dget_parent(dentry);
  33377. + btop = au_dbtop(parent);
  33378. + bbot = au_dbtaildir(parent);
  33379. + if (btop == bbot)
  33380. + goto out_parent; /* success */
  33381. +
  33382. + e2 = au_wbr_create_mfs(dentry, flags);
  33383. + if (e2 < 0)
  33384. + goto out_parent; /* success */
  33385. +
  33386. + /* when the available size is equal, select upper one */
  33387. + sb = dentry->d_sb;
  33388. + br = au_sbr(sb, err);
  33389. + b = br->br_wbr->wbr_bytes;
  33390. + AuDbg("b%d, %llu\n", err, b);
  33391. +
  33392. + for (bindex = btop; bindex <= bbot; bindex++) {
  33393. + h_parent = au_h_dptr(parent, bindex);
  33394. + if (!h_parent || d_is_negative(h_parent))
  33395. + continue;
  33396. +
  33397. + br = au_sbr(sb, bindex);
  33398. + if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
  33399. + b = br->br_wbr->wbr_bytes;
  33400. + err = bindex;
  33401. + AuDbg("b%d, %llu\n", err, b);
  33402. + }
  33403. + }
  33404. +
  33405. + if (err >= 0)
  33406. + err = au_wbr_nonopq(dentry, err);
  33407. +
  33408. +out_parent:
  33409. + dput(parent);
  33410. +out:
  33411. + AuDbg("b%d\n", err);
  33412. + return err;
  33413. +}
  33414. +
  33415. +/* ---------------------------------------------------------------------- */
  33416. +
  33417. +/*
  33418. + * - top down parent
  33419. + * - most free space with parent
  33420. + * - most free space round-robin regardless parent
  33421. + */
  33422. +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
  33423. +{
  33424. + int err;
  33425. + unsigned long long watermark;
  33426. + struct super_block *sb;
  33427. + struct au_branch *br;
  33428. + struct au_wbr_mfs *mfs;
  33429. +
  33430. + err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
  33431. + if (unlikely(err < 0))
  33432. + goto out;
  33433. +
  33434. + sb = dentry->d_sb;
  33435. + br = au_sbr(sb, err);
  33436. + mfs = &au_sbi(sb)->si_wbr_mfs;
  33437. + mutex_lock(&mfs->mfs_lock);
  33438. + watermark = mfs->mfsrr_watermark;
  33439. + mutex_unlock(&mfs->mfs_lock);
  33440. + if (br->br_wbr->wbr_bytes < watermark)
  33441. + /* regardless the parent dir */
  33442. + err = au_wbr_create_mfsrr(dentry, flags);
  33443. +
  33444. +out:
  33445. + AuDbg("b%d\n", err);
  33446. + return err;
  33447. +}
  33448. +
  33449. +/* ---------------------------------------------------------------------- */
  33450. +
  33451. +/* policies for copyup */
  33452. +
  33453. +/* top down parent */
  33454. +static int au_wbr_copyup_tdp(struct dentry *dentry)
  33455. +{
  33456. + return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
  33457. +}
  33458. +
  33459. +/* bottom up parent */
  33460. +static int au_wbr_copyup_bup(struct dentry *dentry)
  33461. +{
  33462. + int err;
  33463. + aufs_bindex_t bindex, btop;
  33464. + struct dentry *parent, *h_parent;
  33465. + struct super_block *sb;
  33466. +
  33467. + err = -EROFS;
  33468. + sb = dentry->d_sb;
  33469. + parent = dget_parent(dentry);
  33470. + btop = au_dbtop(parent);
  33471. + for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
  33472. + h_parent = au_h_dptr(parent, bindex);
  33473. + if (!h_parent || d_is_negative(h_parent))
  33474. + continue;
  33475. +
  33476. + if (!au_br_rdonly(au_sbr(sb, bindex))) {
  33477. + err = bindex;
  33478. + break;
  33479. + }
  33480. + }
  33481. + dput(parent);
  33482. +
  33483. + /* bottom up here */
  33484. + if (unlikely(err < 0))
  33485. + err = au_wbr_bu(sb, btop - 1);
  33486. +
  33487. + AuDbg("b%d\n", err);
  33488. + return err;
  33489. +}
  33490. +
  33491. +/* bottom up */
  33492. +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
  33493. +{
  33494. + int err;
  33495. +
  33496. + err = au_wbr_bu(dentry->d_sb, btop);
  33497. + AuDbg("b%d\n", err);
  33498. + if (err > btop)
  33499. + err = au_wbr_nonopq(dentry, err);
  33500. +
  33501. + AuDbg("b%d\n", err);
  33502. + return err;
  33503. +}
  33504. +
  33505. +static int au_wbr_copyup_bu(struct dentry *dentry)
  33506. +{
  33507. + int err;
  33508. + aufs_bindex_t btop;
  33509. +
  33510. + btop = au_dbtop(dentry);
  33511. + err = au_wbr_do_copyup_bu(dentry, btop);
  33512. + return err;
  33513. +}
  33514. +
  33515. +/* ---------------------------------------------------------------------- */
  33516. +
  33517. +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
  33518. + [AuWbrCopyup_TDP] = {
  33519. + .copyup = au_wbr_copyup_tdp
  33520. + },
  33521. + [AuWbrCopyup_BUP] = {
  33522. + .copyup = au_wbr_copyup_bup
  33523. + },
  33524. + [AuWbrCopyup_BU] = {
  33525. + .copyup = au_wbr_copyup_bu
  33526. + }
  33527. +};
  33528. +
  33529. +struct au_wbr_create_operations au_wbr_create_ops[] = {
  33530. + [AuWbrCreate_TDP] = {
  33531. + .create = au_wbr_create_tdp
  33532. + },
  33533. + [AuWbrCreate_RR] = {
  33534. + .create = au_wbr_create_rr,
  33535. + .init = au_wbr_create_init_rr
  33536. + },
  33537. + [AuWbrCreate_MFS] = {
  33538. + .create = au_wbr_create_mfs,
  33539. + .init = au_wbr_create_init_mfs,
  33540. + .fin = au_wbr_create_fin_mfs
  33541. + },
  33542. + [AuWbrCreate_MFSV] = {
  33543. + .create = au_wbr_create_mfs,
  33544. + .init = au_wbr_create_init_mfs,
  33545. + .fin = au_wbr_create_fin_mfs
  33546. + },
  33547. + [AuWbrCreate_MFSRR] = {
  33548. + .create = au_wbr_create_mfsrr,
  33549. + .init = au_wbr_create_init_mfsrr,
  33550. + .fin = au_wbr_create_fin_mfs
  33551. + },
  33552. + [AuWbrCreate_MFSRRV] = {
  33553. + .create = au_wbr_create_mfsrr,
  33554. + .init = au_wbr_create_init_mfsrr,
  33555. + .fin = au_wbr_create_fin_mfs
  33556. + },
  33557. + [AuWbrCreate_TDMFS] = {
  33558. + .create = au_wbr_create_tdmfs,
  33559. + .init = au_wbr_create_init_mfs,
  33560. + .fin = au_wbr_create_fin_mfs
  33561. + },
  33562. + [AuWbrCreate_TDMFSV] = {
  33563. + .create = au_wbr_create_tdmfs,
  33564. + .init = au_wbr_create_init_mfs,
  33565. + .fin = au_wbr_create_fin_mfs
  33566. + },
  33567. + [AuWbrCreate_PMFS] = {
  33568. + .create = au_wbr_create_pmfs,
  33569. + .init = au_wbr_create_init_mfs,
  33570. + .fin = au_wbr_create_fin_mfs
  33571. + },
  33572. + [AuWbrCreate_PMFSV] = {
  33573. + .create = au_wbr_create_pmfs,
  33574. + .init = au_wbr_create_init_mfs,
  33575. + .fin = au_wbr_create_fin_mfs
  33576. + },
  33577. + [AuWbrCreate_PMFSRR] = {
  33578. + .create = au_wbr_create_pmfsrr,
  33579. + .init = au_wbr_create_init_mfsrr,
  33580. + .fin = au_wbr_create_fin_mfs
  33581. + },
  33582. + [AuWbrCreate_PMFSRRV] = {
  33583. + .create = au_wbr_create_pmfsrr,
  33584. + .init = au_wbr_create_init_mfsrr,
  33585. + .fin = au_wbr_create_fin_mfs
  33586. + }
  33587. +};
  33588. --- /dev/null
  33589. +++ linux-4.19/fs/aufs/whout.c 2018-11-20 09:35:15.049195505 +0200
  33590. @@ -0,0 +1,1062 @@
  33591. +// SPDX-License-Identifier: GPL-2.0
  33592. +/*
  33593. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  33594. + *
  33595. + * This program, aufs is free software; you can redistribute it and/or modify
  33596. + * it under the terms of the GNU General Public License as published by
  33597. + * the Free Software Foundation; either version 2 of the License, or
  33598. + * (at your option) any later version.
  33599. + *
  33600. + * This program is distributed in the hope that it will be useful,
  33601. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33602. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33603. + * GNU General Public License for more details.
  33604. + *
  33605. + * You should have received a copy of the GNU General Public License
  33606. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  33607. + */
  33608. +
  33609. +/*
  33610. + * whiteout for logical deletion and opaque directory
  33611. + */
  33612. +
  33613. +#include "aufs.h"
  33614. +
  33615. +#define WH_MASK 0444
  33616. +
  33617. +/*
  33618. + * If a directory contains this file, then it is opaque. We start with the
  33619. + * .wh. flag so that it is blocked by lookup.
  33620. + */
  33621. +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
  33622. + sizeof(AUFS_WH_DIROPQ) - 1);
  33623. +
  33624. +/*
  33625. + * generate whiteout name, which is NOT terminated by NULL.
  33626. + * @name: original d_name.name
  33627. + * @len: original d_name.len
  33628. + * @wh: whiteout qstr
  33629. + * returns zero when succeeds, otherwise error.
  33630. + * succeeded value as wh->name should be freed by kfree().
  33631. + */
  33632. +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
  33633. +{
  33634. + char *p;
  33635. +
  33636. + if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
  33637. + return -ENAMETOOLONG;
  33638. +
  33639. + wh->len = name->len + AUFS_WH_PFX_LEN;
  33640. + p = kmalloc(wh->len, GFP_NOFS);
  33641. + wh->name = p;
  33642. + if (p) {
  33643. + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
  33644. + memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
  33645. + /* smp_mb(); */
  33646. + return 0;
  33647. + }
  33648. + return -ENOMEM;
  33649. +}
  33650. +
  33651. +/* ---------------------------------------------------------------------- */
  33652. +
  33653. +/*
  33654. + * test if the @wh_name exists under @h_parent.
  33655. + * @try_sio specifies the necessary of super-io.
  33656. + */
  33657. +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
  33658. +{
  33659. + int err;
  33660. + struct dentry *wh_dentry;
  33661. +
  33662. + if (!try_sio)
  33663. + wh_dentry = vfsub_lkup_one(wh_name, h_parent);
  33664. + else
  33665. + wh_dentry = au_sio_lkup_one(wh_name, h_parent);
  33666. + err = PTR_ERR(wh_dentry);
  33667. + if (IS_ERR(wh_dentry)) {
  33668. + if (err == -ENAMETOOLONG)
  33669. + err = 0;
  33670. + goto out;
  33671. + }
  33672. +
  33673. + err = 0;
  33674. + if (d_is_negative(wh_dentry))
  33675. + goto out_wh; /* success */
  33676. +
  33677. + err = 1;
  33678. + if (d_is_reg(wh_dentry))
  33679. + goto out_wh; /* success */
  33680. +
  33681. + err = -EIO;
  33682. + AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
  33683. + wh_dentry, d_inode(wh_dentry)->i_mode);
  33684. +
  33685. +out_wh:
  33686. + dput(wh_dentry);
  33687. +out:
  33688. + return err;
  33689. +}
  33690. +
  33691. +/*
  33692. + * test if the @h_dentry sets opaque or not.
  33693. + */
  33694. +int au_diropq_test(struct dentry *h_dentry)
  33695. +{
  33696. + int err;
  33697. + struct inode *h_dir;
  33698. +
  33699. + h_dir = d_inode(h_dentry);
  33700. + err = au_wh_test(h_dentry, &diropq_name,
  33701. + au_test_h_perm_sio(h_dir, MAY_EXEC));
  33702. + return err;
  33703. +}
  33704. +
  33705. +/*
  33706. + * returns a negative dentry whose name is unique and temporary.
  33707. + */
  33708. +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
  33709. + struct qstr *prefix)
  33710. +{
  33711. + struct dentry *dentry;
  33712. + int i;
  33713. + char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
  33714. + *name, *p;
  33715. + /* strict atomic_t is unnecessary here */
  33716. + static unsigned short cnt;
  33717. + struct qstr qs;
  33718. +
  33719. + BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
  33720. +
  33721. + name = defname;
  33722. + qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
  33723. + if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
  33724. + dentry = ERR_PTR(-ENAMETOOLONG);
  33725. + if (unlikely(qs.len > NAME_MAX))
  33726. + goto out;
  33727. + dentry = ERR_PTR(-ENOMEM);
  33728. + name = kmalloc(qs.len + 1, GFP_NOFS);
  33729. + if (unlikely(!name))
  33730. + goto out;
  33731. + }
  33732. +
  33733. + /* doubly whiteout-ed */
  33734. + memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
  33735. + p = name + AUFS_WH_PFX_LEN * 2;
  33736. + memcpy(p, prefix->name, prefix->len);
  33737. + p += prefix->len;
  33738. + *p++ = '.';
  33739. + AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
  33740. +
  33741. + qs.name = name;
  33742. + for (i = 0; i < 3; i++) {
  33743. + sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
  33744. + dentry = au_sio_lkup_one(&qs, h_parent);
  33745. + if (IS_ERR(dentry) || d_is_negative(dentry))
  33746. + goto out_name;
  33747. + dput(dentry);
  33748. + }
  33749. + /* pr_warn("could not get random name\n"); */
  33750. + dentry = ERR_PTR(-EEXIST);
  33751. + AuDbg("%.*s\n", AuLNPair(&qs));
  33752. + BUG();
  33753. +
  33754. +out_name:
  33755. + if (name != defname)
  33756. + kfree(name);
  33757. +out:
  33758. + AuTraceErrPtr(dentry);
  33759. + return dentry;
  33760. +}
  33761. +
  33762. +/*
  33763. + * rename the @h_dentry on @br to the whiteouted temporary name.
  33764. + */
  33765. +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
  33766. +{
  33767. + int err;
  33768. + struct path h_path = {
  33769. + .mnt = au_br_mnt(br)
  33770. + };
  33771. + struct inode *h_dir, *delegated;
  33772. + struct dentry *h_parent;
  33773. +
  33774. + h_parent = h_dentry->d_parent; /* dir inode is locked */
  33775. + h_dir = d_inode(h_parent);
  33776. + IMustLock(h_dir);
  33777. +
  33778. + h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
  33779. + err = PTR_ERR(h_path.dentry);
  33780. + if (IS_ERR(h_path.dentry))
  33781. + goto out;
  33782. +
  33783. + /* under the same dir, no need to lock_rename() */
  33784. + delegated = NULL;
  33785. + err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
  33786. + /*flags*/0);
  33787. + AuTraceErr(err);
  33788. + if (unlikely(err == -EWOULDBLOCK)) {
  33789. + pr_warn("cannot retry for NFSv4 delegation"
  33790. + " for an internal rename\n");
  33791. + iput(delegated);
  33792. + }
  33793. + dput(h_path.dentry);
  33794. +
  33795. +out:
  33796. + AuTraceErr(err);
  33797. + return err;
  33798. +}
  33799. +
  33800. +/* ---------------------------------------------------------------------- */
  33801. +/*
  33802. + * functions for removing a whiteout
  33803. + */
  33804. +
  33805. +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
  33806. +{
  33807. + int err, force;
  33808. + struct inode *delegated;
  33809. +
  33810. + /*
  33811. + * forces superio when the dir has a sticky bit.
  33812. + * this may be a violation of unix fs semantics.
  33813. + */
  33814. + force = (h_dir->i_mode & S_ISVTX)
  33815. + && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
  33816. + delegated = NULL;
  33817. + err = vfsub_unlink(h_dir, h_path, &delegated, force);
  33818. + if (unlikely(err == -EWOULDBLOCK)) {
  33819. + pr_warn("cannot retry for NFSv4 delegation"
  33820. + " for an internal unlink\n");
  33821. + iput(delegated);
  33822. + }
  33823. + return err;
  33824. +}
  33825. +
  33826. +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
  33827. + struct dentry *dentry)
  33828. +{
  33829. + int err;
  33830. +
  33831. + err = do_unlink_wh(h_dir, h_path);
  33832. + if (!err && dentry)
  33833. + au_set_dbwh(dentry, -1);
  33834. +
  33835. + return err;
  33836. +}
  33837. +
  33838. +static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
  33839. + struct au_branch *br)
  33840. +{
  33841. + int err;
  33842. + struct path h_path = {
  33843. + .mnt = au_br_mnt(br)
  33844. + };
  33845. +
  33846. + err = 0;
  33847. + h_path.dentry = vfsub_lkup_one(wh, h_parent);
  33848. + if (IS_ERR(h_path.dentry))
  33849. + err = PTR_ERR(h_path.dentry);
  33850. + else {
  33851. + if (d_is_reg(h_path.dentry))
  33852. + err = do_unlink_wh(d_inode(h_parent), &h_path);
  33853. + dput(h_path.dentry);
  33854. + }
  33855. +
  33856. + return err;
  33857. +}
  33858. +
  33859. +/* ---------------------------------------------------------------------- */
  33860. +/*
  33861. + * initialize/clean whiteout for a branch
  33862. + */
  33863. +
  33864. +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
  33865. + const int isdir)
  33866. +{
  33867. + int err;
  33868. + struct inode *delegated;
  33869. +
  33870. + if (d_is_negative(whpath->dentry))
  33871. + return;
  33872. +
  33873. + if (isdir)
  33874. + err = vfsub_rmdir(h_dir, whpath);
  33875. + else {
  33876. + delegated = NULL;
  33877. + err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
  33878. + if (unlikely(err == -EWOULDBLOCK)) {
  33879. + pr_warn("cannot retry for NFSv4 delegation"
  33880. + " for an internal unlink\n");
  33881. + iput(delegated);
  33882. + }
  33883. + }
  33884. + if (unlikely(err))
  33885. + pr_warn("failed removing %pd (%d), ignored.\n",
  33886. + whpath->dentry, err);
  33887. +}
  33888. +
  33889. +static int test_linkable(struct dentry *h_root)
  33890. +{
  33891. + struct inode *h_dir = d_inode(h_root);
  33892. +
  33893. + if (h_dir->i_op->link)
  33894. + return 0;
  33895. +
  33896. + pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
  33897. + h_root, au_sbtype(h_root->d_sb));
  33898. + return -ENOSYS;
  33899. +}
  33900. +
  33901. +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
  33902. +static int au_whdir(struct inode *h_dir, struct path *path)
  33903. +{
  33904. + int err;
  33905. +
  33906. + err = -EEXIST;
  33907. + if (d_is_negative(path->dentry)) {
  33908. + int mode = 0700;
  33909. +
  33910. + if (au_test_nfs(path->dentry->d_sb))
  33911. + mode |= 0111;
  33912. + err = vfsub_mkdir(h_dir, path, mode);
  33913. + } else if (d_is_dir(path->dentry))
  33914. + err = 0;
  33915. + else
  33916. + pr_err("unknown %pd exists\n", path->dentry);
  33917. +
  33918. + return err;
  33919. +}
  33920. +
  33921. +struct au_wh_base {
  33922. + const struct qstr *name;
  33923. + struct dentry *dentry;
  33924. +};
  33925. +
  33926. +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
  33927. + struct path *h_path)
  33928. +{
  33929. + h_path->dentry = base[AuBrWh_BASE].dentry;
  33930. + au_wh_clean(h_dir, h_path, /*isdir*/0);
  33931. + h_path->dentry = base[AuBrWh_PLINK].dentry;
  33932. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  33933. + h_path->dentry = base[AuBrWh_ORPH].dentry;
  33934. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  33935. +}
  33936. +
  33937. +/*
  33938. + * returns tri-state,
  33939. + * minus: error, caller should print the message
  33940. + * zero: success
  33941. + * plus: error, caller should NOT print the message
  33942. + */
  33943. +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
  33944. + int do_plink, struct au_wh_base base[],
  33945. + struct path *h_path)
  33946. +{
  33947. + int err;
  33948. + struct inode *h_dir;
  33949. +
  33950. + h_dir = d_inode(h_root);
  33951. + h_path->dentry = base[AuBrWh_BASE].dentry;
  33952. + au_wh_clean(h_dir, h_path, /*isdir*/0);
  33953. + h_path->dentry = base[AuBrWh_PLINK].dentry;
  33954. + if (do_plink) {
  33955. + err = test_linkable(h_root);
  33956. + if (unlikely(err)) {
  33957. + err = 1;
  33958. + goto out;
  33959. + }
  33960. +
  33961. + err = au_whdir(h_dir, h_path);
  33962. + if (unlikely(err))
  33963. + goto out;
  33964. + wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
  33965. + } else
  33966. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  33967. + h_path->dentry = base[AuBrWh_ORPH].dentry;
  33968. + err = au_whdir(h_dir, h_path);
  33969. + if (unlikely(err))
  33970. + goto out;
  33971. + wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
  33972. +
  33973. +out:
  33974. + return err;
  33975. +}
  33976. +
  33977. +/*
  33978. + * for the moment, aufs supports the branch filesystem which does not support
  33979. + * link(2). testing on FAT which does not support i_op->setattr() fully either,
  33980. + * copyup failed. finally, such filesystem will not be used as the writable
  33981. + * branch.
  33982. + *
  33983. + * returns tri-state, see above.
  33984. + */
  33985. +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
  33986. + int do_plink, struct au_wh_base base[],
  33987. + struct path *h_path)
  33988. +{
  33989. + int err;
  33990. + struct inode *h_dir;
  33991. +
  33992. + WbrWhMustWriteLock(wbr);
  33993. +
  33994. + err = test_linkable(h_root);
  33995. + if (unlikely(err)) {
  33996. + err = 1;
  33997. + goto out;
  33998. + }
  33999. +
  34000. + /*
  34001. + * todo: should this create be done in /sbin/mount.aufs helper?
  34002. + */
  34003. + err = -EEXIST;
  34004. + h_dir = d_inode(h_root);
  34005. + if (d_is_negative(base[AuBrWh_BASE].dentry)) {
  34006. + h_path->dentry = base[AuBrWh_BASE].dentry;
  34007. + err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
  34008. + } else if (d_is_reg(base[AuBrWh_BASE].dentry))
  34009. + err = 0;
  34010. + else
  34011. + pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
  34012. + if (unlikely(err))
  34013. + goto out;
  34014. +
  34015. + h_path->dentry = base[AuBrWh_PLINK].dentry;
  34016. + if (do_plink) {
  34017. + err = au_whdir(h_dir, h_path);
  34018. + if (unlikely(err))
  34019. + goto out;
  34020. + wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
  34021. + } else
  34022. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  34023. + wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
  34024. +
  34025. + h_path->dentry = base[AuBrWh_ORPH].dentry;
  34026. + err = au_whdir(h_dir, h_path);
  34027. + if (unlikely(err))
  34028. + goto out;
  34029. + wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
  34030. +
  34031. +out:
  34032. + return err;
  34033. +}
  34034. +
  34035. +/*
  34036. + * initialize the whiteout base file/dir for @br.
  34037. + */
  34038. +int au_wh_init(struct au_branch *br, struct super_block *sb)
  34039. +{
  34040. + int err, i;
  34041. + const unsigned char do_plink
  34042. + = !!au_opt_test(au_mntflags(sb), PLINK);
  34043. + struct inode *h_dir;
  34044. + struct path path = br->br_path;
  34045. + struct dentry *h_root = path.dentry;
  34046. + struct au_wbr *wbr = br->br_wbr;
  34047. + static const struct qstr base_name[] = {
  34048. + [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
  34049. + sizeof(AUFS_BASE_NAME) - 1),
  34050. + [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
  34051. + sizeof(AUFS_PLINKDIR_NAME) - 1),
  34052. + [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
  34053. + sizeof(AUFS_ORPHDIR_NAME) - 1)
  34054. + };
  34055. + struct au_wh_base base[] = {
  34056. + [AuBrWh_BASE] = {
  34057. + .name = base_name + AuBrWh_BASE,
  34058. + .dentry = NULL
  34059. + },
  34060. + [AuBrWh_PLINK] = {
  34061. + .name = base_name + AuBrWh_PLINK,
  34062. + .dentry = NULL
  34063. + },
  34064. + [AuBrWh_ORPH] = {
  34065. + .name = base_name + AuBrWh_ORPH,
  34066. + .dentry = NULL
  34067. + }
  34068. + };
  34069. +
  34070. + if (wbr)
  34071. + WbrWhMustWriteLock(wbr);
  34072. +
  34073. + for (i = 0; i < AuBrWh_Last; i++) {
  34074. + /* doubly whiteouted */
  34075. + struct dentry *d;
  34076. +
  34077. + d = au_wh_lkup(h_root, (void *)base[i].name, br);
  34078. + err = PTR_ERR(d);
  34079. + if (IS_ERR(d))
  34080. + goto out;
  34081. +
  34082. + base[i].dentry = d;
  34083. + AuDebugOn(wbr
  34084. + && wbr->wbr_wh[i]
  34085. + && wbr->wbr_wh[i] != base[i].dentry);
  34086. + }
  34087. +
  34088. + if (wbr)
  34089. + for (i = 0; i < AuBrWh_Last; i++) {
  34090. + dput(wbr->wbr_wh[i]);
  34091. + wbr->wbr_wh[i] = NULL;
  34092. + }
  34093. +
  34094. + err = 0;
  34095. + if (!au_br_writable(br->br_perm)) {
  34096. + h_dir = d_inode(h_root);
  34097. + au_wh_init_ro(h_dir, base, &path);
  34098. + } else if (!au_br_wh_linkable(br->br_perm)) {
  34099. + err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
  34100. + if (err > 0)
  34101. + goto out;
  34102. + else if (err)
  34103. + goto out_err;
  34104. + } else {
  34105. + err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
  34106. + if (err > 0)
  34107. + goto out;
  34108. + else if (err)
  34109. + goto out_err;
  34110. + }
  34111. + goto out; /* success */
  34112. +
  34113. +out_err:
  34114. + pr_err("an error(%d) on the writable branch %pd(%s)\n",
  34115. + err, h_root, au_sbtype(h_root->d_sb));
  34116. +out:
  34117. + for (i = 0; i < AuBrWh_Last; i++)
  34118. + dput(base[i].dentry);
  34119. + return err;
  34120. +}
  34121. +
  34122. +/* ---------------------------------------------------------------------- */
  34123. +/*
  34124. + * whiteouts are all hard-linked usually.
  34125. + * when its link count reaches a ceiling, we create a new whiteout base
  34126. + * asynchronously.
  34127. + */
  34128. +
  34129. +struct reinit_br_wh {
  34130. + struct super_block *sb;
  34131. + struct au_branch *br;
  34132. +};
  34133. +
  34134. +static void reinit_br_wh(void *arg)
  34135. +{
  34136. + int err;
  34137. + aufs_bindex_t bindex;
  34138. + struct path h_path;
  34139. + struct reinit_br_wh *a = arg;
  34140. + struct au_wbr *wbr;
  34141. + struct inode *dir, *delegated;
  34142. + struct dentry *h_root;
  34143. + struct au_hinode *hdir;
  34144. +
  34145. + err = 0;
  34146. + wbr = a->br->br_wbr;
  34147. + /* big aufs lock */
  34148. + si_noflush_write_lock(a->sb);
  34149. + if (!au_br_writable(a->br->br_perm))
  34150. + goto out;
  34151. + bindex = au_br_index(a->sb, a->br->br_id);
  34152. + if (unlikely(bindex < 0))
  34153. + goto out;
  34154. +
  34155. + di_read_lock_parent(a->sb->s_root, AuLock_IR);
  34156. + dir = d_inode(a->sb->s_root);
  34157. + hdir = au_hi(dir, bindex);
  34158. + h_root = au_h_dptr(a->sb->s_root, bindex);
  34159. + AuDebugOn(h_root != au_br_dentry(a->br));
  34160. +
  34161. + au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
  34162. + wbr_wh_write_lock(wbr);
  34163. + err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
  34164. + h_root, a->br);
  34165. + if (!err) {
  34166. + h_path.dentry = wbr->wbr_whbase;
  34167. + h_path.mnt = au_br_mnt(a->br);
  34168. + delegated = NULL;
  34169. + err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
  34170. + /*force*/0);
  34171. + if (unlikely(err == -EWOULDBLOCK)) {
  34172. + pr_warn("cannot retry for NFSv4 delegation"
  34173. + " for an internal unlink\n");
  34174. + iput(delegated);
  34175. + }
  34176. + } else {
  34177. + pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
  34178. + err = 0;
  34179. + }
  34180. + dput(wbr->wbr_whbase);
  34181. + wbr->wbr_whbase = NULL;
  34182. + if (!err)
  34183. + err = au_wh_init(a->br, a->sb);
  34184. + wbr_wh_write_unlock(wbr);
  34185. + au_hn_inode_unlock(hdir);
  34186. + di_read_unlock(a->sb->s_root, AuLock_IR);
  34187. + if (!err)
  34188. + au_fhsm_wrote(a->sb, bindex, /*force*/0);
  34189. +
  34190. +out:
  34191. + if (wbr)
  34192. + atomic_dec(&wbr->wbr_wh_running);
  34193. + au_lcnt_dec(&a->br->br_count);
  34194. + si_write_unlock(a->sb);
  34195. + au_nwt_done(&au_sbi(a->sb)->si_nowait);
  34196. + kfree(arg);
  34197. + if (unlikely(err))
  34198. + AuIOErr("err %d\n", err);
  34199. +}
  34200. +
  34201. +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
  34202. +{
  34203. + int do_dec, wkq_err;
  34204. + struct reinit_br_wh *arg;
  34205. +
  34206. + do_dec = 1;
  34207. + if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
  34208. + goto out;
  34209. +
  34210. + /* ignore ENOMEM */
  34211. + arg = kmalloc(sizeof(*arg), GFP_NOFS);
  34212. + if (arg) {
  34213. + /*
  34214. + * dec(wh_running), kfree(arg) and dec(br_count)
  34215. + * in reinit function
  34216. + */
  34217. + arg->sb = sb;
  34218. + arg->br = br;
  34219. + au_lcnt_inc(&br->br_count);
  34220. + wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
  34221. + if (unlikely(wkq_err)) {
  34222. + atomic_dec(&br->br_wbr->wbr_wh_running);
  34223. + au_lcnt_dec(&br->br_count);
  34224. + kfree(arg);
  34225. + }
  34226. + do_dec = 0;
  34227. + }
  34228. +
  34229. +out:
  34230. + if (do_dec)
  34231. + atomic_dec(&br->br_wbr->wbr_wh_running);
  34232. +}
  34233. +
  34234. +/* ---------------------------------------------------------------------- */
  34235. +
  34236. +/*
  34237. + * create the whiteout @wh.
  34238. + */
  34239. +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
  34240. + struct dentry *wh)
  34241. +{
  34242. + int err;
  34243. + struct path h_path = {
  34244. + .dentry = wh
  34245. + };
  34246. + struct au_branch *br;
  34247. + struct au_wbr *wbr;
  34248. + struct dentry *h_parent;
  34249. + struct inode *h_dir, *delegated;
  34250. +
  34251. + h_parent = wh->d_parent; /* dir inode is locked */
  34252. + h_dir = d_inode(h_parent);
  34253. + IMustLock(h_dir);
  34254. +
  34255. + br = au_sbr(sb, bindex);
  34256. + h_path.mnt = au_br_mnt(br);
  34257. + wbr = br->br_wbr;
  34258. + wbr_wh_read_lock(wbr);
  34259. + if (wbr->wbr_whbase) {
  34260. + delegated = NULL;
  34261. + err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
  34262. + if (unlikely(err == -EWOULDBLOCK)) {
  34263. + pr_warn("cannot retry for NFSv4 delegation"
  34264. + " for an internal link\n");
  34265. + iput(delegated);
  34266. + }
  34267. + if (!err || err != -EMLINK)
  34268. + goto out;
  34269. +
  34270. + /* link count full. re-initialize br_whbase. */
  34271. + kick_reinit_br_wh(sb, br);
  34272. + }
  34273. +
  34274. + /* return this error in this context */
  34275. + err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
  34276. + if (!err)
  34277. + au_fhsm_wrote(sb, bindex, /*force*/0);
  34278. +
  34279. +out:
  34280. + wbr_wh_read_unlock(wbr);
  34281. + return err;
  34282. +}
  34283. +
  34284. +/* ---------------------------------------------------------------------- */
  34285. +
  34286. +/*
  34287. + * create or remove the diropq.
  34288. + */
  34289. +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
  34290. + unsigned int flags)
  34291. +{
  34292. + struct dentry *opq_dentry, *h_dentry;
  34293. + struct super_block *sb;
  34294. + struct au_branch *br;
  34295. + int err;
  34296. +
  34297. + sb = dentry->d_sb;
  34298. + br = au_sbr(sb, bindex);
  34299. + h_dentry = au_h_dptr(dentry, bindex);
  34300. + opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
  34301. + if (IS_ERR(opq_dentry))
  34302. + goto out;
  34303. +
  34304. + if (au_ftest_diropq(flags, CREATE)) {
  34305. + err = link_or_create_wh(sb, bindex, opq_dentry);
  34306. + if (!err) {
  34307. + au_set_dbdiropq(dentry, bindex);
  34308. + goto out; /* success */
  34309. + }
  34310. + } else {
  34311. + struct path tmp = {
  34312. + .dentry = opq_dentry,
  34313. + .mnt = au_br_mnt(br)
  34314. + };
  34315. + err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
  34316. + if (!err)
  34317. + au_set_dbdiropq(dentry, -1);
  34318. + }
  34319. + dput(opq_dentry);
  34320. + opq_dentry = ERR_PTR(err);
  34321. +
  34322. +out:
  34323. + return opq_dentry;
  34324. +}
  34325. +
  34326. +struct do_diropq_args {
  34327. + struct dentry **errp;
  34328. + struct dentry *dentry;
  34329. + aufs_bindex_t bindex;
  34330. + unsigned int flags;
  34331. +};
  34332. +
  34333. +static void call_do_diropq(void *args)
  34334. +{
  34335. + struct do_diropq_args *a = args;
  34336. + *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
  34337. +}
  34338. +
  34339. +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
  34340. + unsigned int flags)
  34341. +{
  34342. + struct dentry *diropq, *h_dentry;
  34343. +
  34344. + h_dentry = au_h_dptr(dentry, bindex);
  34345. + if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
  34346. + diropq = do_diropq(dentry, bindex, flags);
  34347. + else {
  34348. + int wkq_err;
  34349. + struct do_diropq_args args = {
  34350. + .errp = &diropq,
  34351. + .dentry = dentry,
  34352. + .bindex = bindex,
  34353. + .flags = flags
  34354. + };
  34355. +
  34356. + wkq_err = au_wkq_wait(call_do_diropq, &args);
  34357. + if (unlikely(wkq_err))
  34358. + diropq = ERR_PTR(wkq_err);
  34359. + }
  34360. +
  34361. + return diropq;
  34362. +}
  34363. +
  34364. +/* ---------------------------------------------------------------------- */
  34365. +
  34366. +/*
  34367. + * lookup whiteout dentry.
  34368. + * @h_parent: lower parent dentry which must exist and be locked
  34369. + * @base_name: name of dentry which will be whiteouted
  34370. + * returns dentry for whiteout.
  34371. + */
  34372. +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
  34373. + struct au_branch *br)
  34374. +{
  34375. + int err;
  34376. + struct qstr wh_name;
  34377. + struct dentry *wh_dentry;
  34378. +
  34379. + err = au_wh_name_alloc(&wh_name, base_name);
  34380. + wh_dentry = ERR_PTR(err);
  34381. + if (!err) {
  34382. + wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
  34383. + kfree(wh_name.name);
  34384. + }
  34385. + return wh_dentry;
  34386. +}
  34387. +
  34388. +/*
  34389. + * link/create a whiteout for @dentry on @bindex.
  34390. + */
  34391. +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
  34392. + struct dentry *h_parent)
  34393. +{
  34394. + struct dentry *wh_dentry;
  34395. + struct super_block *sb;
  34396. + int err;
  34397. +
  34398. + sb = dentry->d_sb;
  34399. + wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
  34400. + if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
  34401. + err = link_or_create_wh(sb, bindex, wh_dentry);
  34402. + if (!err) {
  34403. + au_set_dbwh(dentry, bindex);
  34404. + au_fhsm_wrote(sb, bindex, /*force*/0);
  34405. + } else {
  34406. + dput(wh_dentry);
  34407. + wh_dentry = ERR_PTR(err);
  34408. + }
  34409. + }
  34410. +
  34411. + return wh_dentry;
  34412. +}
  34413. +
  34414. +/* ---------------------------------------------------------------------- */
  34415. +
  34416. +/* Delete all whiteouts in this directory on branch bindex. */
  34417. +static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
  34418. + aufs_bindex_t bindex, struct au_branch *br)
  34419. +{
  34420. + int err;
  34421. + unsigned long ul, n;
  34422. + struct qstr wh_name;
  34423. + char *p;
  34424. + struct hlist_head *head;
  34425. + struct au_vdir_wh *pos;
  34426. + struct au_vdir_destr *str;
  34427. +
  34428. + err = -ENOMEM;
  34429. + p = (void *)__get_free_page(GFP_NOFS);
  34430. + wh_name.name = p;
  34431. + if (unlikely(!wh_name.name))
  34432. + goto out;
  34433. +
  34434. + err = 0;
  34435. + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
  34436. + p += AUFS_WH_PFX_LEN;
  34437. + n = whlist->nh_num;
  34438. + head = whlist->nh_head;
  34439. + for (ul = 0; !err && ul < n; ul++, head++) {
  34440. + hlist_for_each_entry(pos, head, wh_hash) {
  34441. + if (pos->wh_bindex != bindex)
  34442. + continue;
  34443. +
  34444. + str = &pos->wh_str;
  34445. + if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
  34446. + memcpy(p, str->name, str->len);
  34447. + wh_name.len = AUFS_WH_PFX_LEN + str->len;
  34448. + err = unlink_wh_name(h_dentry, &wh_name, br);
  34449. + if (!err)
  34450. + continue;
  34451. + break;
  34452. + }
  34453. + AuIOErr("whiteout name too long %.*s\n",
  34454. + str->len, str->name);
  34455. + err = -EIO;
  34456. + break;
  34457. + }
  34458. + }
  34459. + free_page((unsigned long)wh_name.name);
  34460. +
  34461. +out:
  34462. + return err;
  34463. +}
  34464. +
  34465. +struct del_wh_children_args {
  34466. + int *errp;
  34467. + struct dentry *h_dentry;
  34468. + struct au_nhash *whlist;
  34469. + aufs_bindex_t bindex;
  34470. + struct au_branch *br;
  34471. +};
  34472. +
  34473. +static void call_del_wh_children(void *args)
  34474. +{
  34475. + struct del_wh_children_args *a = args;
  34476. + *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
  34477. +}
  34478. +
  34479. +/* ---------------------------------------------------------------------- */
  34480. +
  34481. +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
  34482. +{
  34483. + struct au_whtmp_rmdir *whtmp;
  34484. + int err;
  34485. + unsigned int rdhash;
  34486. +
  34487. + SiMustAnyLock(sb);
  34488. +
  34489. + whtmp = kzalloc(sizeof(*whtmp), gfp);
  34490. + if (unlikely(!whtmp)) {
  34491. + whtmp = ERR_PTR(-ENOMEM);
  34492. + goto out;
  34493. + }
  34494. +
  34495. + /* no estimation for dir size */
  34496. + rdhash = au_sbi(sb)->si_rdhash;
  34497. + if (!rdhash)
  34498. + rdhash = AUFS_RDHASH_DEF;
  34499. + err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
  34500. + if (unlikely(err)) {
  34501. + kfree(whtmp);
  34502. + whtmp = ERR_PTR(err);
  34503. + }
  34504. +
  34505. +out:
  34506. + return whtmp;
  34507. +}
  34508. +
  34509. +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
  34510. +{
  34511. + if (whtmp->br)
  34512. + au_lcnt_dec(&whtmp->br->br_count);
  34513. + dput(whtmp->wh_dentry);
  34514. + iput(whtmp->dir);
  34515. + au_nhash_wh_free(&whtmp->whlist);
  34516. + kfree(whtmp);
  34517. +}
  34518. +
  34519. +/*
  34520. + * rmdir the whiteouted temporary named dir @h_dentry.
  34521. + * @whlist: whiteouted children.
  34522. + */
  34523. +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
  34524. + struct dentry *wh_dentry, struct au_nhash *whlist)
  34525. +{
  34526. + int err;
  34527. + unsigned int h_nlink;
  34528. + struct path h_tmp;
  34529. + struct inode *wh_inode, *h_dir;
  34530. + struct au_branch *br;
  34531. +
  34532. + h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
  34533. + IMustLock(h_dir);
  34534. +
  34535. + br = au_sbr(dir->i_sb, bindex);
  34536. + wh_inode = d_inode(wh_dentry);
  34537. + inode_lock_nested(wh_inode, AuLsc_I_CHILD);
  34538. +
  34539. + /*
  34540. + * someone else might change some whiteouts while we were sleeping.
  34541. + * it means this whlist may have an obsoleted entry.
  34542. + */
  34543. + if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
  34544. + err = del_wh_children(wh_dentry, whlist, bindex, br);
  34545. + else {
  34546. + int wkq_err;
  34547. + struct del_wh_children_args args = {
  34548. + .errp = &err,
  34549. + .h_dentry = wh_dentry,
  34550. + .whlist = whlist,
  34551. + .bindex = bindex,
  34552. + .br = br
  34553. + };
  34554. +
  34555. + wkq_err = au_wkq_wait(call_del_wh_children, &args);
  34556. + if (unlikely(wkq_err))
  34557. + err = wkq_err;
  34558. + }
  34559. + inode_unlock(wh_inode);
  34560. +
  34561. + if (!err) {
  34562. + h_tmp.dentry = wh_dentry;
  34563. + h_tmp.mnt = au_br_mnt(br);
  34564. + h_nlink = h_dir->i_nlink;
  34565. + err = vfsub_rmdir(h_dir, &h_tmp);
  34566. + /* some fs doesn't change the parent nlink in some cases */
  34567. + h_nlink -= h_dir->i_nlink;
  34568. + }
  34569. +
  34570. + if (!err) {
  34571. + if (au_ibtop(dir) == bindex) {
  34572. + /* todo: dir->i_mutex is necessary */
  34573. + au_cpup_attr_timesizes(dir);
  34574. + if (h_nlink)
  34575. + vfsub_drop_nlink(dir);
  34576. + }
  34577. + return 0; /* success */
  34578. + }
  34579. +
  34580. + pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
  34581. + return err;
  34582. +}
  34583. +
  34584. +static void call_rmdir_whtmp(void *args)
  34585. +{
  34586. + int err;
  34587. + aufs_bindex_t bindex;
  34588. + struct au_whtmp_rmdir *a = args;
  34589. + struct super_block *sb;
  34590. + struct dentry *h_parent;
  34591. + struct inode *h_dir;
  34592. + struct au_hinode *hdir;
  34593. +
  34594. + /* rmdir by nfsd may cause deadlock with this i_mutex */
  34595. + /* inode_lock(a->dir); */
  34596. + err = -EROFS;
  34597. + sb = a->dir->i_sb;
  34598. + si_read_lock(sb, !AuLock_FLUSH);
  34599. + if (!au_br_writable(a->br->br_perm))
  34600. + goto out;
  34601. + bindex = au_br_index(sb, a->br->br_id);
  34602. + if (unlikely(bindex < 0))
  34603. + goto out;
  34604. +
  34605. + err = -EIO;
  34606. + ii_write_lock_parent(a->dir);
  34607. + h_parent = dget_parent(a->wh_dentry);
  34608. + h_dir = d_inode(h_parent);
  34609. + hdir = au_hi(a->dir, bindex);
  34610. + err = vfsub_mnt_want_write(au_br_mnt(a->br));
  34611. + if (unlikely(err))
  34612. + goto out_mnt;
  34613. + au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
  34614. + err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
  34615. + a->br);
  34616. + if (!err)
  34617. + err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
  34618. + au_hn_inode_unlock(hdir);
  34619. + vfsub_mnt_drop_write(au_br_mnt(a->br));
  34620. +
  34621. +out_mnt:
  34622. + dput(h_parent);
  34623. + ii_write_unlock(a->dir);
  34624. +out:
  34625. + /* inode_unlock(a->dir); */
  34626. + au_whtmp_rmdir_free(a);
  34627. + si_read_unlock(sb);
  34628. + au_nwt_done(&au_sbi(sb)->si_nowait);
  34629. + if (unlikely(err))
  34630. + AuIOErr("err %d\n", err);
  34631. +}
  34632. +
  34633. +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
  34634. + struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
  34635. +{
  34636. + int wkq_err;
  34637. + struct super_block *sb;
  34638. +
  34639. + IMustLock(dir);
  34640. +
  34641. + /* all post-process will be done in do_rmdir_whtmp(). */
  34642. + sb = dir->i_sb;
  34643. + args->dir = au_igrab(dir);
  34644. + args->br = au_sbr(sb, bindex);
  34645. + au_lcnt_inc(&args->br->br_count);
  34646. + args->wh_dentry = dget(wh_dentry);
  34647. + wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
  34648. + if (unlikely(wkq_err)) {
  34649. + pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
  34650. + au_whtmp_rmdir_free(args);
  34651. + }
  34652. +}
  34653. --- /dev/null
  34654. +++ linux-4.19/fs/aufs/whout.h 2018-11-20 09:35:15.050195505 +0200
  34655. @@ -0,0 +1,86 @@
  34656. +/* SPDX-License-Identifier: GPL-2.0 */
  34657. +/*
  34658. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  34659. + *
  34660. + * This program, aufs is free software; you can redistribute it and/or modify
  34661. + * it under the terms of the GNU General Public License as published by
  34662. + * the Free Software Foundation; either version 2 of the License, or
  34663. + * (at your option) any later version.
  34664. + *
  34665. + * This program is distributed in the hope that it will be useful,
  34666. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  34667. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34668. + * GNU General Public License for more details.
  34669. + *
  34670. + * You should have received a copy of the GNU General Public License
  34671. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  34672. + */
  34673. +
  34674. +/*
  34675. + * whiteout for logical deletion and opaque directory
  34676. + */
  34677. +
  34678. +#ifndef __AUFS_WHOUT_H__
  34679. +#define __AUFS_WHOUT_H__
  34680. +
  34681. +#ifdef __KERNEL__
  34682. +
  34683. +#include "dir.h"
  34684. +
  34685. +/* whout.c */
  34686. +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
  34687. +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
  34688. +int au_diropq_test(struct dentry *h_dentry);
  34689. +struct au_branch;
  34690. +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
  34691. + struct qstr *prefix);
  34692. +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
  34693. +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
  34694. + struct dentry *dentry);
  34695. +int au_wh_init(struct au_branch *br, struct super_block *sb);
  34696. +
  34697. +/* diropq flags */
  34698. +#define AuDiropq_CREATE 1
  34699. +#define au_ftest_diropq(flags, name) ((flags) & AuDiropq_##name)
  34700. +#define au_fset_diropq(flags, name) \
  34701. + do { (flags) |= AuDiropq_##name; } while (0)
  34702. +#define au_fclr_diropq(flags, name) \
  34703. + do { (flags) &= ~AuDiropq_##name; } while (0)
  34704. +
  34705. +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
  34706. + unsigned int flags);
  34707. +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
  34708. + struct au_branch *br);
  34709. +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
  34710. + struct dentry *h_parent);
  34711. +
  34712. +/* real rmdir for the whiteout-ed dir */
  34713. +struct au_whtmp_rmdir {
  34714. + struct inode *dir;
  34715. + struct au_branch *br;
  34716. + struct dentry *wh_dentry;
  34717. + struct au_nhash whlist;
  34718. +};
  34719. +
  34720. +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
  34721. +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
  34722. +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
  34723. + struct dentry *wh_dentry, struct au_nhash *whlist);
  34724. +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
  34725. + struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
  34726. +
  34727. +/* ---------------------------------------------------------------------- */
  34728. +
  34729. +static inline struct dentry *au_diropq_create(struct dentry *dentry,
  34730. + aufs_bindex_t bindex)
  34731. +{
  34732. + return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
  34733. +}
  34734. +
  34735. +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
  34736. +{
  34737. + return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
  34738. +}
  34739. +
  34740. +#endif /* __KERNEL__ */
  34741. +#endif /* __AUFS_WHOUT_H__ */
  34742. --- /dev/null
  34743. +++ linux-4.19/fs/aufs/wkq.c 2018-11-20 09:35:15.050195505 +0200
  34744. @@ -0,0 +1,392 @@
  34745. +// SPDX-License-Identifier: GPL-2.0
  34746. +/*
  34747. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  34748. + *
  34749. + * This program, aufs is free software; you can redistribute it and/or modify
  34750. + * it under the terms of the GNU General Public License as published by
  34751. + * the Free Software Foundation; either version 2 of the License, or
  34752. + * (at your option) any later version.
  34753. + *
  34754. + * This program is distributed in the hope that it will be useful,
  34755. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  34756. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34757. + * GNU General Public License for more details.
  34758. + *
  34759. + * You should have received a copy of the GNU General Public License
  34760. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  34761. + */
  34762. +
  34763. +/*
  34764. + * workqueue for asynchronous/super-io operations
  34765. + * todo: try new credential scheme
  34766. + */
  34767. +
  34768. +#include <linux/module.h>
  34769. +#include "aufs.h"
  34770. +
  34771. +/* internal workqueue named AUFS_WKQ_NAME */
  34772. +
  34773. +static struct workqueue_struct *au_wkq;
  34774. +
  34775. +struct au_wkinfo {
  34776. + struct work_struct wk;
  34777. + struct kobject *kobj;
  34778. +
  34779. + unsigned int flags; /* see wkq.h */
  34780. +
  34781. + au_wkq_func_t func;
  34782. + void *args;
  34783. +
  34784. +#ifdef CONFIG_LOCKDEP
  34785. + int dont_check;
  34786. + struct held_lock **hlock;
  34787. +#endif
  34788. +
  34789. + struct completion *comp;
  34790. +};
  34791. +
  34792. +/* ---------------------------------------------------------------------- */
  34793. +/*
  34794. + * Aufs passes some operations to the workqueue such as the internal copyup.
  34795. + * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
  34796. + * job run by workqueue depends upon the locks acquired in the other task.
  34797. + * Delegating a small operation to the workqueue, aufs passes its lockdep
  34798. + * information too. And the job in the workqueue restores the info in order to
  34799. + * pretend as if it acquired those locks. This is just to make LOCKDEP work
  34800. + * correctly and expectedly.
  34801. + */
  34802. +
  34803. +#ifndef CONFIG_LOCKDEP
  34804. +AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
  34805. +AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
  34806. +AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
  34807. +AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
  34808. +AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
  34809. +#else
  34810. +static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
  34811. +{
  34812. + wkinfo->hlock = NULL;
  34813. + wkinfo->dont_check = 0;
  34814. +}
  34815. +
  34816. +/*
  34817. + * 1: matched
  34818. + * 0: unmatched
  34819. + */
  34820. +static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
  34821. +{
  34822. + static DEFINE_SPINLOCK(spin);
  34823. + static struct {
  34824. + char *name;
  34825. + struct lock_class_key *key;
  34826. + } a[] = {
  34827. + { .name = "&sbinfo->si_rwsem" },
  34828. + { .name = "&finfo->fi_rwsem" },
  34829. + { .name = "&dinfo->di_rwsem" },
  34830. + { .name = "&iinfo->ii_rwsem" }
  34831. + };
  34832. + static int set;
  34833. + int i;
  34834. +
  34835. + /* lockless read from 'set.' see below */
  34836. + if (set == ARRAY_SIZE(a)) {
  34837. + for (i = 0; i < ARRAY_SIZE(a); i++)
  34838. + if (a[i].key == key)
  34839. + goto match;
  34840. + goto unmatch;
  34841. + }
  34842. +
  34843. + spin_lock(&spin);
  34844. + if (set)
  34845. + for (i = 0; i < ARRAY_SIZE(a); i++)
  34846. + if (a[i].key == key) {
  34847. + spin_unlock(&spin);
  34848. + goto match;
  34849. + }
  34850. + for (i = 0; i < ARRAY_SIZE(a); i++) {
  34851. + if (a[i].key) {
  34852. + if (unlikely(a[i].key == key)) { /* rare but possible */
  34853. + spin_unlock(&spin);
  34854. + goto match;
  34855. + } else
  34856. + continue;
  34857. + }
  34858. + if (strstr(a[i].name, name)) {
  34859. + /*
  34860. + * the order of these three lines is important for the
  34861. + * lockless read above.
  34862. + */
  34863. + a[i].key = key;
  34864. + spin_unlock(&spin);
  34865. + set++;
  34866. + /* AuDbg("%d, %s\n", set, name); */
  34867. + goto match;
  34868. + }
  34869. + }
  34870. + spin_unlock(&spin);
  34871. + goto unmatch;
  34872. +
  34873. +match:
  34874. + return 1;
  34875. +unmatch:
  34876. + return 0;
  34877. +}
  34878. +
  34879. +static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
  34880. +{
  34881. + int err, n;
  34882. + struct task_struct *curr;
  34883. + struct held_lock **hl, *held_locks, *p;
  34884. +
  34885. + err = 0;
  34886. + curr = current;
  34887. + wkinfo->dont_check = lockdep_recursing(curr);
  34888. + if (wkinfo->dont_check)
  34889. + goto out;
  34890. + n = curr->lockdep_depth;
  34891. + if (!n)
  34892. + goto out;
  34893. +
  34894. + err = -ENOMEM;
  34895. + wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
  34896. + if (unlikely(!wkinfo->hlock))
  34897. + goto out;
  34898. +
  34899. + err = 0;
  34900. +#if 0
  34901. + if (0 && au_debug_test()) /* left for debugging */
  34902. + lockdep_print_held_locks(curr);
  34903. +#endif
  34904. + held_locks = curr->held_locks;
  34905. + hl = wkinfo->hlock;
  34906. + while (n--) {
  34907. + p = held_locks++;
  34908. + if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
  34909. + *hl++ = p;
  34910. + }
  34911. + *hl = NULL;
  34912. +
  34913. +out:
  34914. + return err;
  34915. +}
  34916. +
  34917. +static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
  34918. +{
  34919. + kfree(wkinfo->hlock);
  34920. +}
  34921. +
  34922. +static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
  34923. +{
  34924. + struct held_lock *p, **hl = wkinfo->hlock;
  34925. + int subclass;
  34926. +
  34927. + if (wkinfo->dont_check)
  34928. + lockdep_off();
  34929. + if (!hl)
  34930. + return;
  34931. + while ((p = *hl++)) { /* assignment */
  34932. + subclass = lockdep_hlock_class(p)->subclass;
  34933. + /* AuDbg("%s, %d\n", p->instance->name, subclass); */
  34934. + if (p->read)
  34935. + rwsem_acquire_read(p->instance, subclass, 0,
  34936. + /*p->acquire_ip*/_RET_IP_);
  34937. + else
  34938. + rwsem_acquire(p->instance, subclass, 0,
  34939. + /*p->acquire_ip*/_RET_IP_);
  34940. + }
  34941. +}
  34942. +
  34943. +static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
  34944. +{
  34945. + struct held_lock *p, **hl = wkinfo->hlock;
  34946. +
  34947. + if (wkinfo->dont_check)
  34948. + lockdep_on();
  34949. + if (!hl)
  34950. + return;
  34951. + while ((p = *hl++)) /* assignment */
  34952. + rwsem_release(p->instance, 0, /*p->acquire_ip*/_RET_IP_);
  34953. +}
  34954. +#endif
  34955. +
  34956. +static void wkq_func(struct work_struct *wk)
  34957. +{
  34958. + struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
  34959. +
  34960. + AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
  34961. + AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
  34962. +
  34963. + au_wkq_lockdep_pre(wkinfo);
  34964. + wkinfo->func(wkinfo->args);
  34965. + au_wkq_lockdep_post(wkinfo);
  34966. + if (au_ftest_wkq(wkinfo->flags, WAIT))
  34967. + complete(wkinfo->comp);
  34968. + else {
  34969. + kobject_put(wkinfo->kobj);
  34970. + module_put(THIS_MODULE); /* todo: ?? */
  34971. + kfree(wkinfo);
  34972. + }
  34973. +}
  34974. +
  34975. +/*
  34976. + * Since struct completion is large, try allocating it dynamically.
  34977. + */
  34978. +#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
  34979. +#define AuWkqCompDeclare(name) struct completion *comp = NULL
  34980. +
  34981. +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
  34982. +{
  34983. + *comp = kmalloc(sizeof(**comp), GFP_NOFS);
  34984. + if (*comp) {
  34985. + init_completion(*comp);
  34986. + wkinfo->comp = *comp;
  34987. + return 0;
  34988. + }
  34989. + return -ENOMEM;
  34990. +}
  34991. +
  34992. +static void au_wkq_comp_free(struct completion *comp)
  34993. +{
  34994. + kfree(comp);
  34995. +}
  34996. +
  34997. +#else
  34998. +
  34999. +/* no braces */
  35000. +#define AuWkqCompDeclare(name) \
  35001. + DECLARE_COMPLETION_ONSTACK(_ ## name); \
  35002. + struct completion *comp = &_ ## name
  35003. +
  35004. +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
  35005. +{
  35006. + wkinfo->comp = *comp;
  35007. + return 0;
  35008. +}
  35009. +
  35010. +static void au_wkq_comp_free(struct completion *comp __maybe_unused)
  35011. +{
  35012. + /* empty */
  35013. +}
  35014. +#endif /* 4KSTACKS */
  35015. +
  35016. +static void au_wkq_run(struct au_wkinfo *wkinfo)
  35017. +{
  35018. + if (au_ftest_wkq(wkinfo->flags, NEST)) {
  35019. + if (au_wkq_test()) {
  35020. + AuWarn1("wkq from wkq, unless silly-rename on NFS,"
  35021. + " due to a dead dir by UDBA,"
  35022. + " or async xino write?\n");
  35023. + AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
  35024. + }
  35025. + } else
  35026. + au_dbg_verify_kthread();
  35027. +
  35028. + if (au_ftest_wkq(wkinfo->flags, WAIT)) {
  35029. + INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
  35030. + queue_work(au_wkq, &wkinfo->wk);
  35031. + } else {
  35032. + INIT_WORK(&wkinfo->wk, wkq_func);
  35033. + schedule_work(&wkinfo->wk);
  35034. + }
  35035. +}
  35036. +
  35037. +/*
  35038. + * Be careful. It is easy to make deadlock happen.
  35039. + * processA: lock, wkq and wait
  35040. + * processB: wkq and wait, lock in wkq
  35041. + * --> deadlock
  35042. + */
  35043. +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
  35044. +{
  35045. + int err;
  35046. + AuWkqCompDeclare(comp);
  35047. + struct au_wkinfo wkinfo = {
  35048. + .flags = flags,
  35049. + .func = func,
  35050. + .args = args
  35051. + };
  35052. +
  35053. + err = au_wkq_comp_alloc(&wkinfo, &comp);
  35054. + if (unlikely(err))
  35055. + goto out;
  35056. + err = au_wkq_lockdep_alloc(&wkinfo);
  35057. + if (unlikely(err))
  35058. + goto out_comp;
  35059. + if (!err) {
  35060. + au_wkq_run(&wkinfo);
  35061. + /* no timeout, no interrupt */
  35062. + wait_for_completion(wkinfo.comp);
  35063. + }
  35064. + au_wkq_lockdep_free(&wkinfo);
  35065. +
  35066. +out_comp:
  35067. + au_wkq_comp_free(comp);
  35068. +out:
  35069. + destroy_work_on_stack(&wkinfo.wk);
  35070. + return err;
  35071. +}
  35072. +
  35073. +/*
  35074. + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
  35075. + * problem in a concurrent umounting.
  35076. + */
  35077. +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
  35078. + unsigned int flags)
  35079. +{
  35080. + int err;
  35081. + struct au_wkinfo *wkinfo;
  35082. +
  35083. + atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
  35084. +
  35085. + /*
  35086. + * wkq_func() must free this wkinfo.
  35087. + * it highly depends upon the implementation of workqueue.
  35088. + */
  35089. + err = 0;
  35090. + wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
  35091. + if (wkinfo) {
  35092. + wkinfo->kobj = &au_sbi(sb)->si_kobj;
  35093. + wkinfo->flags = flags & ~AuWkq_WAIT;
  35094. + wkinfo->func = func;
  35095. + wkinfo->args = args;
  35096. + wkinfo->comp = NULL;
  35097. + au_wkq_lockdep_init(wkinfo);
  35098. + kobject_get(wkinfo->kobj);
  35099. + __module_get(THIS_MODULE); /* todo: ?? */
  35100. +
  35101. + au_wkq_run(wkinfo);
  35102. + } else {
  35103. + err = -ENOMEM;
  35104. + au_nwt_done(&au_sbi(sb)->si_nowait);
  35105. + }
  35106. +
  35107. + return err;
  35108. +}
  35109. +
  35110. +/* ---------------------------------------------------------------------- */
  35111. +
  35112. +void au_nwt_init(struct au_nowait_tasks *nwt)
  35113. +{
  35114. + atomic_set(&nwt->nw_len, 0);
  35115. + /* smp_mb(); */ /* atomic_set */
  35116. + init_waitqueue_head(&nwt->nw_wq);
  35117. +}
  35118. +
  35119. +void au_wkq_fin(void)
  35120. +{
  35121. + destroy_workqueue(au_wkq);
  35122. +}
  35123. +
  35124. +int __init au_wkq_init(void)
  35125. +{
  35126. + int err;
  35127. +
  35128. + err = 0;
  35129. + au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
  35130. + if (IS_ERR(au_wkq))
  35131. + err = PTR_ERR(au_wkq);
  35132. + else if (!au_wkq)
  35133. + err = -ENOMEM;
  35134. +
  35135. + return err;
  35136. +}
  35137. --- /dev/null
  35138. +++ linux-4.19/fs/aufs/wkq.h 2018-11-20 09:35:15.051195505 +0200
  35139. @@ -0,0 +1,89 @@
  35140. +/* SPDX-License-Identifier: GPL-2.0 */
  35141. +/*
  35142. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  35143. + *
  35144. + * This program, aufs is free software; you can redistribute it and/or modify
  35145. + * it under the terms of the GNU General Public License as published by
  35146. + * the Free Software Foundation; either version 2 of the License, or
  35147. + * (at your option) any later version.
  35148. + *
  35149. + * This program is distributed in the hope that it will be useful,
  35150. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35151. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  35152. + * GNU General Public License for more details.
  35153. + *
  35154. + * You should have received a copy of the GNU General Public License
  35155. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  35156. + */
  35157. +
  35158. +/*
  35159. + * workqueue for asynchronous/super-io operations
  35160. + * todo: try new credentials management scheme
  35161. + */
  35162. +
  35163. +#ifndef __AUFS_WKQ_H__
  35164. +#define __AUFS_WKQ_H__
  35165. +
  35166. +#ifdef __KERNEL__
  35167. +
  35168. +#include <linux/wait.h>
  35169. +
  35170. +struct super_block;
  35171. +
  35172. +/* ---------------------------------------------------------------------- */
  35173. +
  35174. +/*
  35175. + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
  35176. + */
  35177. +struct au_nowait_tasks {
  35178. + atomic_t nw_len;
  35179. + wait_queue_head_t nw_wq;
  35180. +};
  35181. +
  35182. +/* ---------------------------------------------------------------------- */
  35183. +
  35184. +typedef void (*au_wkq_func_t)(void *args);
  35185. +
  35186. +/* wkq flags */
  35187. +#define AuWkq_WAIT 1
  35188. +#define AuWkq_NEST (1 << 1)
  35189. +#define au_ftest_wkq(flags, name) ((flags) & AuWkq_##name)
  35190. +#define au_fset_wkq(flags, name) \
  35191. + do { (flags) |= AuWkq_##name; } while (0)
  35192. +#define au_fclr_wkq(flags, name) \
  35193. + do { (flags) &= ~AuWkq_##name; } while (0)
  35194. +
  35195. +/* wkq.c */
  35196. +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
  35197. +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
  35198. + unsigned int flags);
  35199. +void au_nwt_init(struct au_nowait_tasks *nwt);
  35200. +int __init au_wkq_init(void);
  35201. +void au_wkq_fin(void);
  35202. +
  35203. +/* ---------------------------------------------------------------------- */
  35204. +
  35205. +static inline int au_wkq_test(void)
  35206. +{
  35207. + return current->flags & PF_WQ_WORKER;
  35208. +}
  35209. +
  35210. +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
  35211. +{
  35212. + return au_wkq_do_wait(AuWkq_WAIT, func, args);
  35213. +}
  35214. +
  35215. +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
  35216. +{
  35217. + if (atomic_dec_and_test(&nwt->nw_len))
  35218. + wake_up_all(&nwt->nw_wq);
  35219. +}
  35220. +
  35221. +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
  35222. +{
  35223. + wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
  35224. + return 0;
  35225. +}
  35226. +
  35227. +#endif /* __KERNEL__ */
  35228. +#endif /* __AUFS_WKQ_H__ */
  35229. --- /dev/null
  35230. +++ linux-4.19/fs/aufs/xattr.c 2018-11-20 09:35:15.051195505 +0200
  35231. @@ -0,0 +1,356 @@
  35232. +// SPDX-License-Identifier: GPL-2.0
  35233. +/*
  35234. + * Copyright (C) 2014-2018 Junjiro R. Okajima
  35235. + *
  35236. + * This program, aufs is free software; you can redistribute it and/or modify
  35237. + * it under the terms of the GNU General Public License as published by
  35238. + * the Free Software Foundation; either version 2 of the License, or
  35239. + * (at your option) any later version.
  35240. + *
  35241. + * This program is distributed in the hope that it will be useful,
  35242. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35243. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  35244. + * GNU General Public License for more details.
  35245. + *
  35246. + * You should have received a copy of the GNU General Public License
  35247. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  35248. + */
  35249. +
  35250. +/*
  35251. + * handling xattr functions
  35252. + */
  35253. +
  35254. +#include <linux/fs.h>
  35255. +#include <linux/posix_acl_xattr.h>
  35256. +#include <linux/xattr.h>
  35257. +#include "aufs.h"
  35258. +
  35259. +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
  35260. +{
  35261. + if (!ignore_flags)
  35262. + goto out;
  35263. + switch (err) {
  35264. + case -ENOMEM:
  35265. + case -EDQUOT:
  35266. + goto out;
  35267. + }
  35268. +
  35269. + if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
  35270. + err = 0;
  35271. + goto out;
  35272. + }
  35273. +
  35274. +#define cmp(brattr, prefix) do { \
  35275. + if (!strncmp(name, XATTR_##prefix##_PREFIX, \
  35276. + XATTR_##prefix##_PREFIX_LEN)) { \
  35277. + if (ignore_flags & AuBrAttr_ICEX_##brattr) \
  35278. + err = 0; \
  35279. + goto out; \
  35280. + } \
  35281. + } while (0)
  35282. +
  35283. + cmp(SEC, SECURITY);
  35284. + cmp(SYS, SYSTEM);
  35285. + cmp(TR, TRUSTED);
  35286. + cmp(USR, USER);
  35287. +#undef cmp
  35288. +
  35289. + if (ignore_flags & AuBrAttr_ICEX_OTH)
  35290. + err = 0;
  35291. +
  35292. +out:
  35293. + return err;
  35294. +}
  35295. +
  35296. +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
  35297. +
  35298. +static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
  35299. + char *name, char **buf, unsigned int ignore_flags,
  35300. + unsigned int verbose)
  35301. +{
  35302. + int err;
  35303. + ssize_t ssz;
  35304. + struct inode *h_idst;
  35305. +
  35306. + ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
  35307. + err = ssz;
  35308. + if (unlikely(err <= 0)) {
  35309. + if (err == -ENODATA
  35310. + || (err == -EOPNOTSUPP
  35311. + && ((ignore_flags & au_xattr_out_of_list)
  35312. + || (au_test_nfs_noacl(d_inode(h_src))
  35313. + && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
  35314. + || !strcmp(name,
  35315. + XATTR_NAME_POSIX_ACL_DEFAULT))))
  35316. + ))
  35317. + err = 0;
  35318. + if (err && (verbose || au_debug_test()))
  35319. + pr_err("%s, err %d\n", name, err);
  35320. + goto out;
  35321. + }
  35322. +
  35323. + /* unlock it temporary */
  35324. + h_idst = d_inode(h_dst);
  35325. + inode_unlock(h_idst);
  35326. + err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
  35327. + inode_lock_nested(h_idst, AuLsc_I_CHILD2);
  35328. + if (unlikely(err)) {
  35329. + if (verbose || au_debug_test())
  35330. + pr_err("%s, err %d\n", name, err);
  35331. + err = au_xattr_ignore(err, name, ignore_flags);
  35332. + }
  35333. +
  35334. +out:
  35335. + return err;
  35336. +}
  35337. +
  35338. +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
  35339. + unsigned int verbose)
  35340. +{
  35341. + int err, unlocked, acl_access, acl_default;
  35342. + ssize_t ssz;
  35343. + struct inode *h_isrc, *h_idst;
  35344. + char *value, *p, *o, *e;
  35345. +
  35346. + /* try stopping to update the source inode while we are referencing */
  35347. + /* there should not be the parent-child relationship between them */
  35348. + h_isrc = d_inode(h_src);
  35349. + h_idst = d_inode(h_dst);
  35350. + inode_unlock(h_idst);
  35351. + inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
  35352. + inode_lock_nested(h_idst, AuLsc_I_CHILD2);
  35353. + unlocked = 0;
  35354. +
  35355. + /* some filesystems don't list POSIX ACL, for example tmpfs */
  35356. + ssz = vfs_listxattr(h_src, NULL, 0);
  35357. + err = ssz;
  35358. + if (unlikely(err < 0)) {
  35359. + AuTraceErr(err);
  35360. + if (err == -ENODATA
  35361. + || err == -EOPNOTSUPP)
  35362. + err = 0; /* ignore */
  35363. + goto out;
  35364. + }
  35365. +
  35366. + err = 0;
  35367. + p = NULL;
  35368. + o = NULL;
  35369. + if (ssz) {
  35370. + err = -ENOMEM;
  35371. + p = kmalloc(ssz, GFP_NOFS);
  35372. + o = p;
  35373. + if (unlikely(!p))
  35374. + goto out;
  35375. + err = vfs_listxattr(h_src, p, ssz);
  35376. + }
  35377. + inode_unlock_shared(h_isrc);
  35378. + unlocked = 1;
  35379. + AuDbg("err %d, ssz %zd\n", err, ssz);
  35380. + if (unlikely(err < 0))
  35381. + goto out_free;
  35382. +
  35383. + err = 0;
  35384. + e = p + ssz;
  35385. + value = NULL;
  35386. + acl_access = 0;
  35387. + acl_default = 0;
  35388. + while (!err && p < e) {
  35389. + acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
  35390. + sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
  35391. + acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
  35392. + sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
  35393. + - 1);
  35394. + err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
  35395. + verbose);
  35396. + p += strlen(p) + 1;
  35397. + }
  35398. + AuTraceErr(err);
  35399. + ignore_flags |= au_xattr_out_of_list;
  35400. + if (!err && !acl_access) {
  35401. + err = au_do_cpup_xattr(h_dst, h_src,
  35402. + XATTR_NAME_POSIX_ACL_ACCESS, &value,
  35403. + ignore_flags, verbose);
  35404. + AuTraceErr(err);
  35405. + }
  35406. + if (!err && !acl_default) {
  35407. + err = au_do_cpup_xattr(h_dst, h_src,
  35408. + XATTR_NAME_POSIX_ACL_DEFAULT, &value,
  35409. + ignore_flags, verbose);
  35410. + AuTraceErr(err);
  35411. + }
  35412. +
  35413. + kfree(value);
  35414. +
  35415. +out_free:
  35416. + kfree(o);
  35417. +out:
  35418. + if (!unlocked)
  35419. + inode_unlock_shared(h_isrc);
  35420. + AuTraceErr(err);
  35421. + return err;
  35422. +}
  35423. +
  35424. +/* ---------------------------------------------------------------------- */
  35425. +
  35426. +static int au_smack_reentering(struct super_block *sb)
  35427. +{
  35428. +#if IS_ENABLED(CONFIG_SECURITY_SMACK)
  35429. + /*
  35430. + * as a part of lookup, smack_d_instantiate() is called, and it calls
  35431. + * i_op->getxattr(). ouch.
  35432. + */
  35433. + return si_pid_test(sb);
  35434. +#else
  35435. + return 0;
  35436. +#endif
  35437. +}
  35438. +
  35439. +enum {
  35440. + AU_XATTR_LIST,
  35441. + AU_XATTR_GET
  35442. +};
  35443. +
  35444. +struct au_lgxattr {
  35445. + int type;
  35446. + union {
  35447. + struct {
  35448. + char *list;
  35449. + size_t size;
  35450. + } list;
  35451. + struct {
  35452. + const char *name;
  35453. + void *value;
  35454. + size_t size;
  35455. + } get;
  35456. + } u;
  35457. +};
  35458. +
  35459. +static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
  35460. +{
  35461. + ssize_t err;
  35462. + int reenter;
  35463. + struct path h_path;
  35464. + struct super_block *sb;
  35465. +
  35466. + sb = dentry->d_sb;
  35467. + reenter = au_smack_reentering(sb);
  35468. + if (!reenter) {
  35469. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  35470. + if (unlikely(err))
  35471. + goto out;
  35472. + }
  35473. + err = au_h_path_getattr(dentry, /*force*/1, &h_path, reenter);
  35474. + if (unlikely(err))
  35475. + goto out_si;
  35476. + if (unlikely(!h_path.dentry))
  35477. + /* illegally overlapped or something */
  35478. + goto out_di; /* pretending success */
  35479. +
  35480. + /* always topmost entry only */
  35481. + switch (arg->type) {
  35482. + case AU_XATTR_LIST:
  35483. + err = vfs_listxattr(h_path.dentry,
  35484. + arg->u.list.list, arg->u.list.size);
  35485. + break;
  35486. + case AU_XATTR_GET:
  35487. + AuDebugOn(d_is_negative(h_path.dentry));
  35488. + err = vfs_getxattr(h_path.dentry,
  35489. + arg->u.get.name, arg->u.get.value,
  35490. + arg->u.get.size);
  35491. + break;
  35492. + }
  35493. +
  35494. +out_di:
  35495. + if (!reenter)
  35496. + di_read_unlock(dentry, AuLock_IR);
  35497. +out_si:
  35498. + if (!reenter)
  35499. + si_read_unlock(sb);
  35500. +out:
  35501. + AuTraceErr(err);
  35502. + return err;
  35503. +}
  35504. +
  35505. +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
  35506. +{
  35507. + struct au_lgxattr arg = {
  35508. + .type = AU_XATTR_LIST,
  35509. + .u.list = {
  35510. + .list = list,
  35511. + .size = size
  35512. + },
  35513. + };
  35514. +
  35515. + return au_lgxattr(dentry, &arg);
  35516. +}
  35517. +
  35518. +static ssize_t au_getxattr(struct dentry *dentry,
  35519. + struct inode *inode __maybe_unused,
  35520. + const char *name, void *value, size_t size)
  35521. +{
  35522. + struct au_lgxattr arg = {
  35523. + .type = AU_XATTR_GET,
  35524. + .u.get = {
  35525. + .name = name,
  35526. + .value = value,
  35527. + .size = size
  35528. + },
  35529. + };
  35530. +
  35531. + return au_lgxattr(dentry, &arg);
  35532. +}
  35533. +
  35534. +static int au_setxattr(struct dentry *dentry, struct inode *inode,
  35535. + const char *name, const void *value, size_t size,
  35536. + int flags)
  35537. +{
  35538. + struct au_sxattr arg = {
  35539. + .type = AU_XATTR_SET,
  35540. + .u.set = {
  35541. + .name = name,
  35542. + .value = value,
  35543. + .size = size,
  35544. + .flags = flags
  35545. + },
  35546. + };
  35547. +
  35548. + return au_sxattr(dentry, inode, &arg);
  35549. +}
  35550. +
  35551. +/* ---------------------------------------------------------------------- */
  35552. +
  35553. +static int au_xattr_get(const struct xattr_handler *handler,
  35554. + struct dentry *dentry, struct inode *inode,
  35555. + const char *name, void *buffer, size_t size)
  35556. +{
  35557. + return au_getxattr(dentry, inode, name, buffer, size);
  35558. +}
  35559. +
  35560. +static int au_xattr_set(const struct xattr_handler *handler,
  35561. + struct dentry *dentry, struct inode *inode,
  35562. + const char *name, const void *value, size_t size,
  35563. + int flags)
  35564. +{
  35565. + return au_setxattr(dentry, inode, name, value, size, flags);
  35566. +}
  35567. +
  35568. +static const struct xattr_handler au_xattr_handler = {
  35569. + .name = "",
  35570. + .prefix = "",
  35571. + .get = au_xattr_get,
  35572. + .set = au_xattr_set
  35573. +};
  35574. +
  35575. +static const struct xattr_handler *au_xattr_handlers[] = {
  35576. +#ifdef CONFIG_FS_POSIX_ACL
  35577. + &posix_acl_access_xattr_handler,
  35578. + &posix_acl_default_xattr_handler,
  35579. +#endif
  35580. + &au_xattr_handler, /* must be last */
  35581. + NULL
  35582. +};
  35583. +
  35584. +void au_xattr_init(struct super_block *sb)
  35585. +{
  35586. + sb->s_xattr = au_xattr_handlers;
  35587. +}
  35588. --- /dev/null
  35589. +++ linux-4.19/fs/aufs/xino.c 2018-11-20 09:35:15.054195505 +0200
  35590. @@ -0,0 +1,1956 @@
  35591. +// SPDX-License-Identifier: GPL-2.0
  35592. +/*
  35593. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  35594. + *
  35595. + * This program, aufs is free software; you can redistribute it and/or modify
  35596. + * it under the terms of the GNU General Public License as published by
  35597. + * the Free Software Foundation; either version 2 of the License, or
  35598. + * (at your option) any later version.
  35599. + *
  35600. + * This program is distributed in the hope that it will be useful,
  35601. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35602. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  35603. + * GNU General Public License for more details.
  35604. + *
  35605. + * You should have received a copy of the GNU General Public License
  35606. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  35607. + */
  35608. +
  35609. +/*
  35610. + * external inode number translation table and bitmap
  35611. + *
  35612. + * things to consider
  35613. + * - the lifetime
  35614. + * + au_xino object
  35615. + * + XINO files (xino, xib, xigen)
  35616. + * + dynamic debugfs entries (xiN)
  35617. + * + static debugfs entries (xib, xigen)
  35618. + * + static sysfs entry (xi_path)
  35619. + * - several entry points to handle them.
  35620. + * + mount(2) without xino option (default)
  35621. + * + mount(2) with xino option
  35622. + * + mount(2) with noxino option
  35623. + * + umount(2)
  35624. + * + remount with add/del branches
  35625. + * + remount with xino/noxino options
  35626. + */
  35627. +
  35628. +#include <linux/seq_file.h>
  35629. +#include <linux/statfs.h>
  35630. +#include "aufs.h"
  35631. +
  35632. +static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
  35633. + aufs_bindex_t bbot,
  35634. + struct super_block *h_sb)
  35635. +{
  35636. + /* todo: try binary-search if the branches are many */
  35637. + for (; btop <= bbot; btop++)
  35638. + if (h_sb == au_sbr_sb(sb, btop))
  35639. + return btop;
  35640. + return -1;
  35641. +}
  35642. +
  35643. +/*
  35644. + * find another branch who is on the same filesystem of the specified
  35645. + * branch{@btgt}. search until @bbot.
  35646. + */
  35647. +static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
  35648. + aufs_bindex_t bbot)
  35649. +{
  35650. + aufs_bindex_t bindex;
  35651. + struct super_block *tgt_sb;
  35652. +
  35653. + tgt_sb = au_sbr_sb(sb, btgt);
  35654. + bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
  35655. + if (bindex < 0)
  35656. + bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
  35657. +
  35658. + return bindex;
  35659. +}
  35660. +
  35661. +/* ---------------------------------------------------------------------- */
  35662. +
  35663. +/*
  35664. + * stop unnecessary notify events at creating xino files
  35665. + */
  35666. +
  35667. +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
  35668. +{
  35669. + aufs_bindex_t bfound, bindex, bbot;
  35670. + struct dentry *parent;
  35671. + struct au_branch *br;
  35672. +
  35673. + bfound = -1;
  35674. + parent = dentry->d_parent; /* safe d_parent access */
  35675. + bbot = au_sbbot(sb);
  35676. + for (bindex = 0; bindex <= bbot; bindex++) {
  35677. + br = au_sbr(sb, bindex);
  35678. + if (au_br_dentry(br) == parent) {
  35679. + bfound = bindex;
  35680. + break;
  35681. + }
  35682. + }
  35683. +
  35684. + AuDbg("bfound b%d\n", bfound);
  35685. + return bfound;
  35686. +}
  35687. +
  35688. +struct au_xino_lock_dir {
  35689. + struct au_hinode *hdir;
  35690. + struct dentry *parent;
  35691. + struct inode *dir;
  35692. +};
  35693. +
  35694. +static struct dentry *au_dget_parent_lock(struct dentry *dentry,
  35695. + unsigned int lsc)
  35696. +{
  35697. + struct dentry *parent;
  35698. + struct inode *dir;
  35699. +
  35700. + parent = dget_parent(dentry);
  35701. + dir = d_inode(parent);
  35702. + inode_lock_nested(dir, lsc);
  35703. +#if 0 /* it should not happen */
  35704. + spin_lock(&dentry->d_lock);
  35705. + if (unlikely(dentry->d_parent != parent)) {
  35706. + spin_unlock(&dentry->d_lock);
  35707. + inode_unlock(dir);
  35708. + dput(parent);
  35709. + parent = NULL;
  35710. + goto out;
  35711. + }
  35712. + spin_unlock(&dentry->d_lock);
  35713. +
  35714. +out:
  35715. +#endif
  35716. + return parent;
  35717. +}
  35718. +
  35719. +static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
  35720. + struct au_xino_lock_dir *ldir)
  35721. +{
  35722. + aufs_bindex_t bindex;
  35723. +
  35724. + ldir->hdir = NULL;
  35725. + bindex = au_xi_root(sb, xipath->dentry);
  35726. + if (bindex >= 0) {
  35727. + /* rw branch root */
  35728. + ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
  35729. + au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
  35730. + } else {
  35731. + /* other */
  35732. + ldir->parent = au_dget_parent_lock(xipath->dentry,
  35733. + AuLsc_I_PARENT);
  35734. + ldir->dir = d_inode(ldir->parent);
  35735. + }
  35736. +}
  35737. +
  35738. +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
  35739. +{
  35740. + if (ldir->hdir)
  35741. + au_hn_inode_unlock(ldir->hdir);
  35742. + else {
  35743. + inode_unlock(ldir->dir);
  35744. + dput(ldir->parent);
  35745. + }
  35746. +}
  35747. +
  35748. +/* ---------------------------------------------------------------------- */
  35749. +
  35750. +/*
  35751. + * create and set a new xino file
  35752. + */
  35753. +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent)
  35754. +{
  35755. + struct file *file;
  35756. + struct dentry *h_parent, *d;
  35757. + struct inode *h_dir, *inode;
  35758. + int err;
  35759. +
  35760. + /*
  35761. + * at mount-time, and the xino file is the default path,
  35762. + * hnotify is disabled so we have no notify events to ignore.
  35763. + * when a user specified the xino, we cannot get au_hdir to be ignored.
  35764. + */
  35765. + file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
  35766. + /* | __FMODE_NONOTIFY */,
  35767. + 0666);
  35768. + if (IS_ERR(file)) {
  35769. + if (!silent)
  35770. + pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
  35771. + return file;
  35772. + }
  35773. +
  35774. + /* keep file count */
  35775. + err = 0;
  35776. + d = file->f_path.dentry;
  35777. + h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
  35778. + /* mnt_want_write() is unnecessary here */
  35779. + h_dir = d_inode(h_parent);
  35780. + inode = file_inode(file);
  35781. + /* no delegation since it is just created */
  35782. + if (inode->i_nlink)
  35783. + err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
  35784. + /*force*/0);
  35785. + inode_unlock(h_dir);
  35786. + dput(h_parent);
  35787. + if (unlikely(err)) {
  35788. + if (!silent)
  35789. + pr_err("unlink %s(%d)\n", fpath, err);
  35790. + goto out;
  35791. + }
  35792. +
  35793. + err = -EINVAL;
  35794. + if (unlikely(sb == d->d_sb)) {
  35795. + if (!silent)
  35796. + pr_err("%s must be outside\n", fpath);
  35797. + goto out;
  35798. + }
  35799. + if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
  35800. + if (!silent)
  35801. + pr_err("xino doesn't support %s(%s)\n",
  35802. + fpath, au_sbtype(d->d_sb));
  35803. + goto out;
  35804. + }
  35805. + return file; /* success */
  35806. +
  35807. +out:
  35808. + fput(file);
  35809. + file = ERR_PTR(err);
  35810. + return file;
  35811. +}
  35812. +
  35813. +/*
  35814. + * create a new xinofile at the same place/path as @base.
  35815. + */
  35816. +struct file *au_xino_create2(struct super_block *sb, struct path *base,
  35817. + struct file *copy_src)
  35818. +{
  35819. + struct file *file;
  35820. + struct dentry *dentry, *parent;
  35821. + struct inode *dir, *delegated;
  35822. + struct qstr *name;
  35823. + struct path path;
  35824. + int err, do_unlock;
  35825. + struct au_xino_lock_dir ldir;
  35826. +
  35827. + do_unlock = 1;
  35828. + au_xino_lock_dir(sb, base, &ldir);
  35829. + dentry = base->dentry;
  35830. + parent = dentry->d_parent; /* dir inode is locked */
  35831. + dir = d_inode(parent);
  35832. + IMustLock(dir);
  35833. +
  35834. + name = &dentry->d_name;
  35835. + path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
  35836. + if (IS_ERR(path.dentry)) {
  35837. + file = (void *)path.dentry;
  35838. + pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
  35839. + goto out;
  35840. + }
  35841. +
  35842. + /* no need to mnt_want_write() since we call dentry_open() later */
  35843. + err = vfs_create(dir, path.dentry, 0666, NULL);
  35844. + if (unlikely(err)) {
  35845. + file = ERR_PTR(err);
  35846. + pr_err("%pd create err %d\n", dentry, err);
  35847. + goto out_dput;
  35848. + }
  35849. +
  35850. + path.mnt = base->mnt;
  35851. + file = vfsub_dentry_open(&path,
  35852. + O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
  35853. + /* | __FMODE_NONOTIFY */);
  35854. + if (IS_ERR(file)) {
  35855. + pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
  35856. + goto out_dput;
  35857. + }
  35858. +
  35859. + delegated = NULL;
  35860. + err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
  35861. + au_xino_unlock_dir(&ldir);
  35862. + do_unlock = 0;
  35863. + if (unlikely(err == -EWOULDBLOCK)) {
  35864. + pr_warn("cannot retry for NFSv4 delegation"
  35865. + " for an internal unlink\n");
  35866. + iput(delegated);
  35867. + }
  35868. + if (unlikely(err)) {
  35869. + pr_err("%pd unlink err %d\n", dentry, err);
  35870. + goto out_fput;
  35871. + }
  35872. +
  35873. + if (copy_src) {
  35874. + /* no one can touch copy_src xino */
  35875. + err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
  35876. + if (unlikely(err)) {
  35877. + pr_err("%pd copy err %d\n", dentry, err);
  35878. + goto out_fput;
  35879. + }
  35880. + }
  35881. + goto out_dput; /* success */
  35882. +
  35883. +out_fput:
  35884. + fput(file);
  35885. + file = ERR_PTR(err);
  35886. +out_dput:
  35887. + dput(path.dentry);
  35888. +out:
  35889. + if (do_unlock)
  35890. + au_xino_unlock_dir(&ldir);
  35891. + return file;
  35892. +}
  35893. +
  35894. +struct file *au_xino_file1(struct au_xino *xi)
  35895. +{
  35896. + struct file *file;
  35897. + unsigned int u, nfile;
  35898. +
  35899. + file = NULL;
  35900. + nfile = xi->xi_nfile;
  35901. + for (u = 0; u < nfile; u++) {
  35902. + file = xi->xi_file[u];
  35903. + if (file)
  35904. + break;
  35905. + }
  35906. +
  35907. + return file;
  35908. +}
  35909. +
  35910. +static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
  35911. +{
  35912. + int err;
  35913. + struct file *f;
  35914. + void *p;
  35915. +
  35916. + if (file)
  35917. + get_file(file);
  35918. +
  35919. + err = 0;
  35920. + f = NULL;
  35921. + if (idx < xi->xi_nfile) {
  35922. + f = xi->xi_file[idx];
  35923. + if (f)
  35924. + fput(f);
  35925. + } else {
  35926. + p = au_kzrealloc(xi->xi_file,
  35927. + sizeof(*xi->xi_file) * xi->xi_nfile,
  35928. + sizeof(*xi->xi_file) * (idx + 1),
  35929. + GFP_NOFS, /*may_shrink*/0);
  35930. + if (p) {
  35931. + MtxMustLock(&xi->xi_mtx);
  35932. + xi->xi_file = p;
  35933. + xi->xi_nfile = idx + 1;
  35934. + } else {
  35935. + err = -ENOMEM;
  35936. + if (file)
  35937. + fput(file);
  35938. + goto out;
  35939. + }
  35940. + }
  35941. + xi->xi_file[idx] = file;
  35942. +
  35943. +out:
  35944. + return err;
  35945. +}
  35946. +
  35947. +/*
  35948. + * if @xinew->xi is not set, then create new xigen file.
  35949. + */
  35950. +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
  35951. +{
  35952. + struct file *file;
  35953. + int err;
  35954. +
  35955. + SiMustAnyLock(sb);
  35956. +
  35957. + file = au_xino_create2(sb, xinew->base, xinew->copy_src);
  35958. + if (IS_ERR(file)) {
  35959. + err = PTR_ERR(file);
  35960. + pr_err("%s[%d], err %d\n",
  35961. + xinew->xi ? "xino" : "xigen",
  35962. + xinew->idx, err);
  35963. + goto out;
  35964. + }
  35965. +
  35966. + if (xinew->xi)
  35967. + err = au_xino_file_set(xinew->xi, xinew->idx, file);
  35968. + else {
  35969. + BUG();
  35970. + /* todo: make xigen file an array */
  35971. + /* err = au_xigen_file_set(sb, xinew->idx, file); */
  35972. + }
  35973. + fput(file);
  35974. + if (unlikely(err))
  35975. + file = ERR_PTR(err);
  35976. +
  35977. +out:
  35978. + return file;
  35979. +}
  35980. +
  35981. +/* ---------------------------------------------------------------------- */
  35982. +
  35983. +/*
  35984. + * truncate xino files
  35985. + */
  35986. +static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
  35987. + int idx, struct kstatfs *st)
  35988. +{
  35989. + int err;
  35990. + blkcnt_t blocks;
  35991. + struct file *file, *new_xino;
  35992. + struct au_xi_new xinew = {
  35993. + .idx = idx
  35994. + };
  35995. +
  35996. + err = 0;
  35997. + xinew.xi = au_sbr(sb, bindex)->br_xino;
  35998. + file = au_xino_file(xinew.xi, idx);
  35999. + if (!file)
  36000. + goto out;
  36001. +
  36002. + xinew.base = &file->f_path;
  36003. + err = vfs_statfs(xinew.base, st);
  36004. + if (unlikely(err)) {
  36005. + AuErr1("statfs err %d, ignored\n", err);
  36006. + err = 0;
  36007. + goto out;
  36008. + }
  36009. +
  36010. + blocks = file_inode(file)->i_blocks;
  36011. + pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
  36012. + bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
  36013. +
  36014. + xinew.copy_src = file;
  36015. + new_xino = au_xi_new(sb, &xinew);
  36016. + if (IS_ERR(new_xino)) {
  36017. + err = PTR_ERR(new_xino);
  36018. + pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
  36019. + goto out;
  36020. + }
  36021. +
  36022. + err = vfs_statfs(&new_xino->f_path, st);
  36023. + if (!err)
  36024. + pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
  36025. + bindex, idx, (u64)file_inode(new_xino)->i_blocks,
  36026. + st->f_bfree, st->f_blocks);
  36027. + else {
  36028. + AuErr1("statfs err %d, ignored\n", err);
  36029. + err = 0;
  36030. + }
  36031. +
  36032. +out:
  36033. + return err;
  36034. +}
  36035. +
  36036. +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
  36037. +{
  36038. + int err, i;
  36039. + unsigned long jiffy;
  36040. + aufs_bindex_t bbot;
  36041. + struct kstatfs *st;
  36042. + struct au_branch *br;
  36043. + struct au_xino *xi;
  36044. +
  36045. + err = -ENOMEM;
  36046. + st = kmalloc(sizeof(*st), GFP_NOFS);
  36047. + if (unlikely(!st))
  36048. + goto out;
  36049. +
  36050. + err = -EINVAL;
  36051. + bbot = au_sbbot(sb);
  36052. + if (unlikely(bindex < 0 || bbot < bindex))
  36053. + goto out_st;
  36054. +
  36055. + err = 0;
  36056. + jiffy = jiffies;
  36057. + br = au_sbr(sb, bindex);
  36058. + xi = br->br_xino;
  36059. + for (i = idx_begin; !err && i < xi->xi_nfile; i++)
  36060. + err = au_xino_do_trunc(sb, bindex, i, st);
  36061. + if (!err)
  36062. + au_sbi(sb)->si_xino_jiffy = jiffy;
  36063. +
  36064. +out_st:
  36065. + kfree(st);
  36066. +out:
  36067. + return err;
  36068. +}
  36069. +
  36070. +struct xino_do_trunc_args {
  36071. + struct super_block *sb;
  36072. + struct au_branch *br;
  36073. + int idx;
  36074. +};
  36075. +
  36076. +static void xino_do_trunc(void *_args)
  36077. +{
  36078. + struct xino_do_trunc_args *args = _args;
  36079. + struct super_block *sb;
  36080. + struct au_branch *br;
  36081. + struct inode *dir;
  36082. + int err, idx;
  36083. + aufs_bindex_t bindex;
  36084. +
  36085. + err = 0;
  36086. + sb = args->sb;
  36087. + dir = d_inode(sb->s_root);
  36088. + br = args->br;
  36089. + idx = args->idx;
  36090. +
  36091. + si_noflush_write_lock(sb);
  36092. + ii_read_lock_parent(dir);
  36093. + bindex = au_br_index(sb, br->br_id);
  36094. + err = au_xino_trunc(sb, bindex, idx);
  36095. + ii_read_unlock(dir);
  36096. + if (unlikely(err))
  36097. + pr_warn("err b%d, (%d)\n", bindex, err);
  36098. + atomic_dec(&br->br_xino->xi_truncating);
  36099. + au_lcnt_dec(&br->br_count);
  36100. + si_write_unlock(sb);
  36101. + au_nwt_done(&au_sbi(sb)->si_nowait);
  36102. + kfree(args);
  36103. +}
  36104. +
  36105. +/*
  36106. + * returns the index in the xi_file array whose corresponding file is necessary
  36107. + * to truncate, or -1 which means no need to truncate.
  36108. + */
  36109. +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
  36110. +{
  36111. + int err;
  36112. + unsigned int u;
  36113. + struct kstatfs st;
  36114. + struct au_sbinfo *sbinfo;
  36115. + struct au_xino *xi;
  36116. + struct file *file;
  36117. +
  36118. + /* todo: si_xino_expire and the ratio should be customizable */
  36119. + sbinfo = au_sbi(sb);
  36120. + if (time_before(jiffies,
  36121. + sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
  36122. + return -1;
  36123. +
  36124. + /* truncation border */
  36125. + xi = br->br_xino;
  36126. + for (u = 0; u < xi->xi_nfile; u++) {
  36127. + file = au_xino_file(xi, u);
  36128. + if (!file)
  36129. + continue;
  36130. +
  36131. + err = vfs_statfs(&file->f_path, &st);
  36132. + if (unlikely(err)) {
  36133. + AuErr1("statfs err %d, ignored\n", err);
  36134. + return -1;
  36135. + }
  36136. + if (div64_u64(st.f_bfree * 100, st.f_blocks)
  36137. + >= AUFS_XINO_DEF_TRUNC)
  36138. + return u;
  36139. + }
  36140. +
  36141. + return -1;
  36142. +}
  36143. +
  36144. +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
  36145. +{
  36146. + int idx;
  36147. + struct xino_do_trunc_args *args;
  36148. + int wkq_err;
  36149. +
  36150. + idx = xino_trunc_test(sb, br);
  36151. + if (idx < 0)
  36152. + return;
  36153. +
  36154. + if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
  36155. + goto out;
  36156. +
  36157. + /* lock and kfree() will be called in trunc_xino() */
  36158. + args = kmalloc(sizeof(*args), GFP_NOFS);
  36159. + if (unlikely(!args)) {
  36160. + AuErr1("no memory\n");
  36161. + goto out;
  36162. + }
  36163. +
  36164. + au_lcnt_inc(&br->br_count);
  36165. + args->sb = sb;
  36166. + args->br = br;
  36167. + args->idx = idx;
  36168. + wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
  36169. + if (!wkq_err)
  36170. + return; /* success */
  36171. +
  36172. + pr_err("wkq %d\n", wkq_err);
  36173. + au_lcnt_dec(&br->br_count);
  36174. + kfree(args);
  36175. +
  36176. +out:
  36177. + atomic_dec(&br->br_xino->xi_truncating);
  36178. +}
  36179. +
  36180. +/* ---------------------------------------------------------------------- */
  36181. +
  36182. +struct au_xi_calc {
  36183. + int idx;
  36184. + loff_t pos;
  36185. +};
  36186. +
  36187. +static void au_xi_calc(struct super_block *sb, ino_t h_ino,
  36188. + struct au_xi_calc *calc)
  36189. +{
  36190. + loff_t maxent;
  36191. +
  36192. + maxent = au_xi_maxent(sb);
  36193. + calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
  36194. + calc->pos *= sizeof(ino_t);
  36195. +}
  36196. +
  36197. +static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
  36198. + struct au_xi_calc *calc)
  36199. +{
  36200. + int err;
  36201. + struct file *file;
  36202. + struct au_xino *xi = br->br_xino;
  36203. + struct au_xi_new xinew = {
  36204. + .xi = xi
  36205. + };
  36206. +
  36207. + SiMustAnyLock(sb);
  36208. +
  36209. + err = 0;
  36210. + if (!xi)
  36211. + goto out;
  36212. +
  36213. + mutex_lock(&xi->xi_mtx);
  36214. + file = au_xino_file(xi, calc->idx);
  36215. + if (file)
  36216. + goto out_mtx;
  36217. +
  36218. + file = au_xino_file(xi, /*idx*/-1);
  36219. + AuDebugOn(!file);
  36220. + xinew.idx = calc->idx;
  36221. + xinew.base = &file->f_path;
  36222. + /* xinew.copy_src = NULL; */
  36223. + file = au_xi_new(sb, &xinew);
  36224. + if (IS_ERR(file))
  36225. + err = PTR_ERR(file);
  36226. +
  36227. +out_mtx:
  36228. + mutex_unlock(&xi->xi_mtx);
  36229. +out:
  36230. + return err;
  36231. +}
  36232. +
  36233. +struct au_xino_do_new_async_args {
  36234. + struct super_block *sb;
  36235. + struct au_branch *br;
  36236. + struct au_xi_calc calc;
  36237. + ino_t ino;
  36238. +};
  36239. +
  36240. +struct au_xi_writing {
  36241. + struct hlist_bl_node node;
  36242. + ino_t h_ino, ino;
  36243. +};
  36244. +
  36245. +static int au_xino_do_write(vfs_writef_t write, struct file *file,
  36246. + struct au_xi_calc *calc, ino_t ino);
  36247. +
  36248. +static void au_xino_call_do_new_async(void *args)
  36249. +{
  36250. + struct au_xino_do_new_async_args *a = args;
  36251. + struct au_branch *br;
  36252. + struct super_block *sb;
  36253. + struct au_sbinfo *sbi;
  36254. + struct inode *root;
  36255. + struct file *file;
  36256. + struct au_xi_writing *del, *p;
  36257. + struct hlist_bl_head *hbl;
  36258. + struct hlist_bl_node *pos;
  36259. + int err;
  36260. +
  36261. + br = a->br;
  36262. + sb = a->sb;
  36263. + sbi = au_sbi(sb);
  36264. + si_noflush_read_lock(sb);
  36265. + root = d_inode(sb->s_root);
  36266. + ii_read_lock_child(root);
  36267. + err = au_xino_do_new_async(sb, br, &a->calc);
  36268. + if (unlikely(err)) {
  36269. + AuIOErr("err %d\n", err);
  36270. + goto out;
  36271. + }
  36272. +
  36273. + file = au_xino_file(br->br_xino, a->calc.idx);
  36274. + AuDebugOn(!file);
  36275. + err = au_xino_do_write(sbi->si_xwrite, file, &a->calc, a->ino);
  36276. + if (unlikely(err)) {
  36277. + AuIOErr("err %d\n", err);
  36278. + goto out;
  36279. + }
  36280. +
  36281. + del = NULL;
  36282. + hbl = &br->br_xino->xi_writing;
  36283. + hlist_bl_lock(hbl);
  36284. + au_hbl_for_each(pos, hbl) {
  36285. + p = container_of(pos, struct au_xi_writing, node);
  36286. + if (p->ino == a->ino) {
  36287. + del = p;
  36288. + hlist_bl_del(&p->node);
  36289. + break;
  36290. + }
  36291. + }
  36292. + hlist_bl_unlock(hbl);
  36293. + kfree(del);
  36294. +
  36295. +out:
  36296. + au_lcnt_dec(&br->br_count);
  36297. + ii_read_unlock(root);
  36298. + si_read_unlock(sb);
  36299. + au_nwt_done(&sbi->si_nowait);
  36300. + kfree(args);
  36301. +}
  36302. +
  36303. +/*
  36304. + * create a new xino file asynchronously
  36305. + */
  36306. +static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
  36307. + struct au_xi_calc *calc, ino_t ino)
  36308. +{
  36309. + int err;
  36310. + struct au_xino_do_new_async_args *arg;
  36311. +
  36312. + err = -ENOMEM;
  36313. + arg = kmalloc(sizeof(*arg), GFP_NOFS);
  36314. + if (unlikely(!arg))
  36315. + goto out;
  36316. +
  36317. + arg->sb = sb;
  36318. + arg->br = br;
  36319. + arg->calc = *calc;
  36320. + arg->ino = ino;
  36321. + au_lcnt_inc(&br->br_count);
  36322. + err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
  36323. + if (unlikely(err)) {
  36324. + pr_err("wkq %d\n", err);
  36325. + au_lcnt_dec(&br->br_count);
  36326. + kfree(arg);
  36327. + }
  36328. +
  36329. +out:
  36330. + return err;
  36331. +}
  36332. +
  36333. +/*
  36334. + * read @ino from xinofile for the specified branch{@sb, @bindex}
  36335. + * at the position of @h_ino.
  36336. + */
  36337. +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  36338. + ino_t *ino)
  36339. +{
  36340. + int err;
  36341. + ssize_t sz;
  36342. + struct au_xi_calc calc;
  36343. + struct au_sbinfo *sbinfo;
  36344. + struct file *file;
  36345. + struct au_xino *xi;
  36346. + struct hlist_bl_head *hbl;
  36347. + struct hlist_bl_node *pos;
  36348. + struct au_xi_writing *p;
  36349. +
  36350. + *ino = 0;
  36351. + if (!au_opt_test(au_mntflags(sb), XINO))
  36352. + return 0; /* no xino */
  36353. +
  36354. + err = 0;
  36355. + au_xi_calc(sb, h_ino, &calc);
  36356. + xi = au_sbr(sb, bindex)->br_xino;
  36357. + file = au_xino_file(xi, calc.idx);
  36358. + if (!file) {
  36359. + hbl = &xi->xi_writing;
  36360. + hlist_bl_lock(hbl);
  36361. + au_hbl_for_each(pos, hbl) {
  36362. + p = container_of(pos, struct au_xi_writing, node);
  36363. + if (p->h_ino == h_ino) {
  36364. + AuDbg("hi%llu, i%llu, found\n",
  36365. + (u64)p->h_ino, (u64)p->ino);
  36366. + *ino = p->ino;
  36367. + break;
  36368. + }
  36369. + }
  36370. + hlist_bl_unlock(hbl);
  36371. + return 0;
  36372. + } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
  36373. + return 0; /* no xino */
  36374. +
  36375. + sbinfo = au_sbi(sb);
  36376. + sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &calc.pos);
  36377. + if (sz == sizeof(*ino))
  36378. + return 0; /* success */
  36379. +
  36380. + err = sz;
  36381. + if (unlikely(sz >= 0)) {
  36382. + err = -EIO;
  36383. + AuIOErr("xino read error (%zd)\n", sz);
  36384. + }
  36385. + return err;
  36386. +}
  36387. +
  36388. +static int au_xino_do_write(vfs_writef_t write, struct file *file,
  36389. + struct au_xi_calc *calc, ino_t ino)
  36390. +{
  36391. + ssize_t sz;
  36392. +
  36393. + sz = xino_fwrite(write, file, &ino, sizeof(ino), &calc->pos);
  36394. + if (sz == sizeof(ino))
  36395. + return 0; /* success */
  36396. +
  36397. + AuIOErr("write failed (%zd)\n", sz);
  36398. + return -EIO;
  36399. +}
  36400. +
  36401. +/*
  36402. + * write @ino to the xinofile for the specified branch{@sb, @bindex}
  36403. + * at the position of @h_ino.
  36404. + * even if @ino is zero, it is written to the xinofile and means no entry.
  36405. + * if the size of the xino file on a specific filesystem exceeds the watermark,
  36406. + * try truncating it.
  36407. + */
  36408. +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  36409. + ino_t ino)
  36410. +{
  36411. + int err;
  36412. + unsigned int mnt_flags;
  36413. + struct au_xi_calc calc;
  36414. + struct file *file;
  36415. + struct au_branch *br;
  36416. + struct au_xino *xi;
  36417. + struct au_xi_writing *p;
  36418. +
  36419. + SiMustAnyLock(sb);
  36420. +
  36421. + mnt_flags = au_mntflags(sb);
  36422. + if (!au_opt_test(mnt_flags, XINO))
  36423. + return 0;
  36424. +
  36425. + au_xi_calc(sb, h_ino, &calc);
  36426. + br = au_sbr(sb, bindex);
  36427. + xi = br->br_xino;
  36428. + file = au_xino_file(xi, calc.idx);
  36429. + if (!file) {
  36430. + /* store the inum pair into the list */
  36431. + p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
  36432. + p->h_ino = h_ino;
  36433. + p->ino = ino;
  36434. + au_hbl_add(&p->node, &xi->xi_writing);
  36435. +
  36436. + /* create and write a new xino file asynchronously */
  36437. + err = au_xino_new_async(sb, br, &calc, ino);
  36438. + if (!err)
  36439. + return 0; /* success */
  36440. + goto out;
  36441. + }
  36442. +
  36443. + err = au_xino_do_write(au_sbi(sb)->si_xwrite, file, &calc, ino);
  36444. + if (!err) {
  36445. + br = au_sbr(sb, bindex);
  36446. + if (au_opt_test(mnt_flags, TRUNC_XINO)
  36447. + && au_test_fs_trunc_xino(au_br_sb(br)))
  36448. + xino_try_trunc(sb, br);
  36449. + return 0; /* success */
  36450. + }
  36451. +
  36452. +out:
  36453. + AuIOErr("write failed (%d)\n", err);
  36454. + return -EIO;
  36455. +}
  36456. +
  36457. +static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
  36458. + size_t size, loff_t *pos);
  36459. +
  36460. +/* todo: unnecessary to support mmap_sem since kernel-space? */
  36461. +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *kbuf, size_t size,
  36462. + loff_t *pos)
  36463. +{
  36464. + ssize_t err;
  36465. + mm_segment_t oldfs;
  36466. + union {
  36467. + void *k;
  36468. + char __user *u;
  36469. + } buf;
  36470. + int i;
  36471. + const int prevent_endless = 10;
  36472. +
  36473. + i = 0;
  36474. + buf.k = kbuf;
  36475. + oldfs = get_fs();
  36476. + set_fs(KERNEL_DS);
  36477. + do {
  36478. + err = func(file, buf.u, size, pos);
  36479. + if (err == -EINTR
  36480. + && !au_wkq_test()
  36481. + && fatal_signal_pending(current)) {
  36482. + set_fs(oldfs);
  36483. + err = xino_fread_wkq(func, file, kbuf, size, pos);
  36484. + BUG_ON(err == -EINTR);
  36485. + oldfs = get_fs();
  36486. + set_fs(KERNEL_DS);
  36487. + }
  36488. + } while (i++ < prevent_endless
  36489. + && (err == -EAGAIN || err == -EINTR));
  36490. + set_fs(oldfs);
  36491. +
  36492. +#if 0 /* reserved for future use */
  36493. + if (err > 0)
  36494. + fsnotify_access(file->f_path.dentry);
  36495. +#endif
  36496. +
  36497. + return err;
  36498. +}
  36499. +
  36500. +struct xino_fread_args {
  36501. + ssize_t *errp;
  36502. + vfs_readf_t func;
  36503. + struct file *file;
  36504. + void *buf;
  36505. + size_t size;
  36506. + loff_t *pos;
  36507. +};
  36508. +
  36509. +static void call_xino_fread(void *args)
  36510. +{
  36511. + struct xino_fread_args *a = args;
  36512. + *a->errp = xino_fread(a->func, a->file, a->buf, a->size, a->pos);
  36513. +}
  36514. +
  36515. +static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
  36516. + size_t size, loff_t *pos)
  36517. +{
  36518. + ssize_t err;
  36519. + int wkq_err;
  36520. + struct xino_fread_args args = {
  36521. + .errp = &err,
  36522. + .func = func,
  36523. + .file = file,
  36524. + .buf = buf,
  36525. + .size = size,
  36526. + .pos = pos
  36527. + };
  36528. +
  36529. + wkq_err = au_wkq_wait(call_xino_fread, &args);
  36530. + if (unlikely(wkq_err))
  36531. + err = wkq_err;
  36532. +
  36533. + return err;
  36534. +}
  36535. +
  36536. +static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
  36537. + size_t size, loff_t *pos);
  36538. +
  36539. +static ssize_t do_xino_fwrite(vfs_writef_t func, struct file *file, void *kbuf,
  36540. + size_t size, loff_t *pos)
  36541. +{
  36542. + ssize_t err;
  36543. + mm_segment_t oldfs;
  36544. + union {
  36545. + void *k;
  36546. + const char __user *u;
  36547. + } buf;
  36548. + int i;
  36549. + const int prevent_endless = 10;
  36550. +
  36551. + i = 0;
  36552. + buf.k = kbuf;
  36553. + oldfs = get_fs();
  36554. + set_fs(KERNEL_DS);
  36555. + do {
  36556. + err = func(file, buf.u, size, pos);
  36557. + if (err == -EINTR
  36558. + && !au_wkq_test()
  36559. + && fatal_signal_pending(current)) {
  36560. + set_fs(oldfs);
  36561. + err = xino_fwrite_wkq(func, file, kbuf, size, pos);
  36562. + BUG_ON(err == -EINTR);
  36563. + oldfs = get_fs();
  36564. + set_fs(KERNEL_DS);
  36565. + }
  36566. + } while (i++ < prevent_endless
  36567. + && (err == -EAGAIN || err == -EINTR));
  36568. + set_fs(oldfs);
  36569. +
  36570. +#if 0 /* reserved for future use */
  36571. + if (err > 0)
  36572. + fsnotify_modify(file->f_path.dentry);
  36573. +#endif
  36574. +
  36575. + return err;
  36576. +}
  36577. +
  36578. +struct do_xino_fwrite_args {
  36579. + ssize_t *errp;
  36580. + vfs_writef_t func;
  36581. + struct file *file;
  36582. + void *buf;
  36583. + size_t size;
  36584. + loff_t *pos;
  36585. +};
  36586. +
  36587. +static void call_do_xino_fwrite(void *args)
  36588. +{
  36589. + struct do_xino_fwrite_args *a = args;
  36590. + *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
  36591. +}
  36592. +
  36593. +static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
  36594. + size_t size, loff_t *pos)
  36595. +{
  36596. + ssize_t err;
  36597. + int wkq_err;
  36598. + struct do_xino_fwrite_args args = {
  36599. + .errp = &err,
  36600. + .func = func,
  36601. + .file = file,
  36602. + .buf = buf,
  36603. + .size = size,
  36604. + .pos = pos
  36605. + };
  36606. +
  36607. + /*
  36608. + * it breaks RLIMIT_FSIZE and normal user's limit,
  36609. + * users should care about quota and real 'filesystem full.'
  36610. + */
  36611. + wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
  36612. + if (unlikely(wkq_err))
  36613. + err = wkq_err;
  36614. +
  36615. + return err;
  36616. +}
  36617. +
  36618. +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
  36619. + size_t size, loff_t *pos)
  36620. +{
  36621. + ssize_t err;
  36622. +
  36623. + if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
  36624. + lockdep_off();
  36625. + err = do_xino_fwrite(func, file, buf, size, pos);
  36626. + lockdep_on();
  36627. + } else {
  36628. + lockdep_off();
  36629. + err = xino_fwrite_wkq(func, file, buf, size, pos);
  36630. + lockdep_on();
  36631. + }
  36632. +
  36633. + return err;
  36634. +}
  36635. +
  36636. +/* ---------------------------------------------------------------------- */
  36637. +
  36638. +/*
  36639. + * inode number bitmap
  36640. + */
  36641. +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
  36642. +static ino_t xib_calc_ino(unsigned long pindex, int bit)
  36643. +{
  36644. + ino_t ino;
  36645. +
  36646. + AuDebugOn(bit < 0 || page_bits <= bit);
  36647. + ino = AUFS_FIRST_INO + pindex * page_bits + bit;
  36648. + return ino;
  36649. +}
  36650. +
  36651. +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
  36652. +{
  36653. + AuDebugOn(ino < AUFS_FIRST_INO);
  36654. + ino -= AUFS_FIRST_INO;
  36655. + *pindex = ino / page_bits;
  36656. + *bit = ino % page_bits;
  36657. +}
  36658. +
  36659. +static int xib_pindex(struct super_block *sb, unsigned long pindex)
  36660. +{
  36661. + int err;
  36662. + loff_t pos;
  36663. + ssize_t sz;
  36664. + struct au_sbinfo *sbinfo;
  36665. + struct file *xib;
  36666. + unsigned long *p;
  36667. +
  36668. + sbinfo = au_sbi(sb);
  36669. + MtxMustLock(&sbinfo->si_xib_mtx);
  36670. + AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
  36671. + || !au_opt_test(sbinfo->si_mntflags, XINO));
  36672. +
  36673. + if (pindex == sbinfo->si_xib_last_pindex)
  36674. + return 0;
  36675. +
  36676. + xib = sbinfo->si_xib;
  36677. + p = sbinfo->si_xib_buf;
  36678. + pos = sbinfo->si_xib_last_pindex;
  36679. + pos *= PAGE_SIZE;
  36680. + sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
  36681. + if (unlikely(sz != PAGE_SIZE))
  36682. + goto out;
  36683. +
  36684. + pos = pindex;
  36685. + pos *= PAGE_SIZE;
  36686. + if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
  36687. + sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
  36688. + else {
  36689. + memset(p, 0, PAGE_SIZE);
  36690. + sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
  36691. + }
  36692. + if (sz == PAGE_SIZE) {
  36693. + sbinfo->si_xib_last_pindex = pindex;
  36694. + return 0; /* success */
  36695. + }
  36696. +
  36697. +out:
  36698. + AuIOErr1("write failed (%zd)\n", sz);
  36699. + err = sz;
  36700. + if (sz >= 0)
  36701. + err = -EIO;
  36702. + return err;
  36703. +}
  36704. +
  36705. +static void au_xib_clear_bit(struct inode *inode)
  36706. +{
  36707. + int err, bit;
  36708. + unsigned long pindex;
  36709. + struct super_block *sb;
  36710. + struct au_sbinfo *sbinfo;
  36711. +
  36712. + AuDebugOn(inode->i_nlink);
  36713. +
  36714. + sb = inode->i_sb;
  36715. + xib_calc_bit(inode->i_ino, &pindex, &bit);
  36716. + AuDebugOn(page_bits <= bit);
  36717. + sbinfo = au_sbi(sb);
  36718. + mutex_lock(&sbinfo->si_xib_mtx);
  36719. + err = xib_pindex(sb, pindex);
  36720. + if (!err) {
  36721. + clear_bit(bit, sbinfo->si_xib_buf);
  36722. + sbinfo->si_xib_next_bit = bit;
  36723. + }
  36724. + mutex_unlock(&sbinfo->si_xib_mtx);
  36725. +}
  36726. +
  36727. +/* ---------------------------------------------------------------------- */
  36728. +
  36729. +/*
  36730. + * truncate a xino bitmap file
  36731. + */
  36732. +
  36733. +/* todo: slow */
  36734. +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
  36735. +{
  36736. + int err, bit;
  36737. + ssize_t sz;
  36738. + unsigned long pindex;
  36739. + loff_t pos, pend;
  36740. + struct au_sbinfo *sbinfo;
  36741. + vfs_readf_t func;
  36742. + ino_t *ino;
  36743. + unsigned long *p;
  36744. +
  36745. + err = 0;
  36746. + sbinfo = au_sbi(sb);
  36747. + MtxMustLock(&sbinfo->si_xib_mtx);
  36748. + p = sbinfo->si_xib_buf;
  36749. + func = sbinfo->si_xread;
  36750. + pend = vfsub_f_size_read(file);
  36751. + pos = 0;
  36752. + while (pos < pend) {
  36753. + sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
  36754. + err = sz;
  36755. + if (unlikely(sz <= 0))
  36756. + goto out;
  36757. +
  36758. + err = 0;
  36759. + for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
  36760. + if (unlikely(*ino < AUFS_FIRST_INO))
  36761. + continue;
  36762. +
  36763. + xib_calc_bit(*ino, &pindex, &bit);
  36764. + AuDebugOn(page_bits <= bit);
  36765. + err = xib_pindex(sb, pindex);
  36766. + if (!err)
  36767. + set_bit(bit, p);
  36768. + else
  36769. + goto out;
  36770. + }
  36771. + }
  36772. +
  36773. +out:
  36774. + return err;
  36775. +}
  36776. +
  36777. +static int xib_restore(struct super_block *sb)
  36778. +{
  36779. + int err, i;
  36780. + unsigned int nfile;
  36781. + aufs_bindex_t bindex, bbot;
  36782. + void *page;
  36783. + struct au_branch *br;
  36784. + struct au_xino *xi;
  36785. + struct file *file;
  36786. +
  36787. + err = -ENOMEM;
  36788. + page = (void *)__get_free_page(GFP_NOFS);
  36789. + if (unlikely(!page))
  36790. + goto out;
  36791. +
  36792. + err = 0;
  36793. + bbot = au_sbbot(sb);
  36794. + for (bindex = 0; !err && bindex <= bbot; bindex++)
  36795. + if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
  36796. + br = au_sbr(sb, bindex);
  36797. + xi = br->br_xino;
  36798. + nfile = xi->xi_nfile;
  36799. + for (i = 0; i < nfile; i++) {
  36800. + file = au_xino_file(xi, i);
  36801. + if (file)
  36802. + err = do_xib_restore(sb, file, page);
  36803. + }
  36804. + } else
  36805. + AuDbg("skip shared b%d\n", bindex);
  36806. + free_page((unsigned long)page);
  36807. +
  36808. +out:
  36809. + return err;
  36810. +}
  36811. +
  36812. +int au_xib_trunc(struct super_block *sb)
  36813. +{
  36814. + int err;
  36815. + ssize_t sz;
  36816. + loff_t pos;
  36817. + struct au_sbinfo *sbinfo;
  36818. + unsigned long *p;
  36819. + struct file *file;
  36820. +
  36821. + SiMustWriteLock(sb);
  36822. +
  36823. + err = 0;
  36824. + sbinfo = au_sbi(sb);
  36825. + if (!au_opt_test(sbinfo->si_mntflags, XINO))
  36826. + goto out;
  36827. +
  36828. + file = sbinfo->si_xib;
  36829. + if (vfsub_f_size_read(file) <= PAGE_SIZE)
  36830. + goto out;
  36831. +
  36832. + file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
  36833. + err = PTR_ERR(file);
  36834. + if (IS_ERR(file))
  36835. + goto out;
  36836. + fput(sbinfo->si_xib);
  36837. + sbinfo->si_xib = file;
  36838. +
  36839. + p = sbinfo->si_xib_buf;
  36840. + memset(p, 0, PAGE_SIZE);
  36841. + pos = 0;
  36842. + sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
  36843. + if (unlikely(sz != PAGE_SIZE)) {
  36844. + err = sz;
  36845. + AuIOErr("err %d\n", err);
  36846. + if (sz >= 0)
  36847. + err = -EIO;
  36848. + goto out;
  36849. + }
  36850. +
  36851. + mutex_lock(&sbinfo->si_xib_mtx);
  36852. + /* mnt_want_write() is unnecessary here */
  36853. + err = xib_restore(sb);
  36854. + mutex_unlock(&sbinfo->si_xib_mtx);
  36855. +
  36856. +out:
  36857. + return err;
  36858. +}
  36859. +
  36860. +/* ---------------------------------------------------------------------- */
  36861. +
  36862. +struct au_xino *au_xino_alloc(unsigned int nfile)
  36863. +{
  36864. + struct au_xino *xi;
  36865. +
  36866. + xi = kzalloc(sizeof(*xi), GFP_NOFS);
  36867. + if (unlikely(!xi))
  36868. + goto out;
  36869. + xi->xi_nfile = nfile;
  36870. + xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
  36871. + if (unlikely(!xi->xi_file))
  36872. + goto out_free;
  36873. +
  36874. + xi->xi_nondir.total = 8; /* initial size */
  36875. + xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
  36876. + GFP_NOFS);
  36877. + if (unlikely(!xi->xi_nondir.array))
  36878. + goto out_file;
  36879. +
  36880. + spin_lock_init(&xi->xi_nondir.spin);
  36881. + init_waitqueue_head(&xi->xi_nondir.wqh);
  36882. + mutex_init(&xi->xi_mtx);
  36883. + INIT_HLIST_BL_HEAD(&xi->xi_writing);
  36884. + atomic_set(&xi->xi_truncating, 0);
  36885. + kref_init(&xi->xi_kref);
  36886. + goto out; /* success */
  36887. +
  36888. +out_file:
  36889. + kfree(xi->xi_file);
  36890. +out_free:
  36891. + kfree(xi);
  36892. + xi = NULL;
  36893. +out:
  36894. + return xi;
  36895. +}
  36896. +
  36897. +static int au_xino_init(struct au_branch *br, int idx, struct file *file)
  36898. +{
  36899. + int err;
  36900. + struct au_xino *xi;
  36901. +
  36902. + err = 0;
  36903. + xi = au_xino_alloc(idx + 1);
  36904. + if (unlikely(!xi)) {
  36905. + err = -ENOMEM;
  36906. + goto out;
  36907. + }
  36908. +
  36909. + if (file)
  36910. + get_file(file);
  36911. + xi->xi_file[idx] = file;
  36912. + AuDebugOn(br->br_xino);
  36913. + br->br_xino = xi;
  36914. +
  36915. +out:
  36916. + return err;
  36917. +}
  36918. +
  36919. +static void au_xino_release(struct kref *kref)
  36920. +{
  36921. + struct au_xino *xi;
  36922. + int i;
  36923. + unsigned long ul;
  36924. + struct hlist_bl_head *hbl;
  36925. + struct hlist_bl_node *pos, *n;
  36926. + struct au_xi_writing *p;
  36927. +
  36928. + xi = container_of(kref, struct au_xino, xi_kref);
  36929. + for (i = 0; i < xi->xi_nfile; i++)
  36930. + if (xi->xi_file[i])
  36931. + fput(xi->xi_file[i]);
  36932. + for (i = xi->xi_nondir.total - 1; i >= 0; i--)
  36933. + AuDebugOn(xi->xi_nondir.array[i]);
  36934. + mutex_destroy(&xi->xi_mtx);
  36935. + hbl = &xi->xi_writing;
  36936. + ul = au_hbl_count(hbl);
  36937. + if (unlikely(ul)) {
  36938. + pr_warn("xi_writing %lu\n", ul);
  36939. + hlist_bl_lock(hbl);
  36940. + hlist_bl_for_each_entry_safe (p, pos, n, hbl, node) {
  36941. + hlist_bl_del(&p->node);
  36942. + kfree(p);
  36943. + }
  36944. + hlist_bl_unlock(hbl);
  36945. + }
  36946. + kfree(xi->xi_file);
  36947. + kfree(xi->xi_nondir.array);
  36948. + kfree(xi);
  36949. +}
  36950. +
  36951. +int au_xino_put(struct au_branch *br)
  36952. +{
  36953. + int ret;
  36954. + struct au_xino *xi;
  36955. +
  36956. + ret = 0;
  36957. + xi = br->br_xino;
  36958. + if (xi) {
  36959. + br->br_xino = NULL;
  36960. + ret = kref_put(&xi->xi_kref, au_xino_release);
  36961. + }
  36962. +
  36963. + return ret;
  36964. +}
  36965. +
  36966. +/* ---------------------------------------------------------------------- */
  36967. +
  36968. +/*
  36969. + * xino mount option handlers
  36970. + */
  36971. +
  36972. +/* xino bitmap */
  36973. +static void xino_clear_xib(struct super_block *sb)
  36974. +{
  36975. + struct au_sbinfo *sbinfo;
  36976. +
  36977. + SiMustWriteLock(sb);
  36978. +
  36979. + sbinfo = au_sbi(sb);
  36980. + /* unnecessary to clear sbinfo->si_xread and ->si_xwrite */
  36981. + if (sbinfo->si_xib)
  36982. + fput(sbinfo->si_xib);
  36983. + sbinfo->si_xib = NULL;
  36984. + if (sbinfo->si_xib_buf)
  36985. + free_page((unsigned long)sbinfo->si_xib_buf);
  36986. + sbinfo->si_xib_buf = NULL;
  36987. +}
  36988. +
  36989. +static int au_xino_set_xib(struct super_block *sb, struct path *path)
  36990. +{
  36991. + int err;
  36992. + loff_t pos;
  36993. + struct au_sbinfo *sbinfo;
  36994. + struct file *file;
  36995. + struct super_block *xi_sb;
  36996. +
  36997. + SiMustWriteLock(sb);
  36998. +
  36999. + sbinfo = au_sbi(sb);
  37000. + file = au_xino_create2(sb, path, sbinfo->si_xib);
  37001. + err = PTR_ERR(file);
  37002. + if (IS_ERR(file))
  37003. + goto out;
  37004. + if (sbinfo->si_xib)
  37005. + fput(sbinfo->si_xib);
  37006. + sbinfo->si_xib = file;
  37007. + sbinfo->si_xread = vfs_readf(file);
  37008. + sbinfo->si_xwrite = vfs_writef(file);
  37009. + xi_sb = file_inode(file)->i_sb;
  37010. + sbinfo->si_ximaxent = xi_sb->s_maxbytes;
  37011. + if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
  37012. + err = -EIO;
  37013. + pr_err("s_maxbytes(%llu) on %s is too small\n",
  37014. + (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
  37015. + goto out_unset;
  37016. + }
  37017. + sbinfo->si_ximaxent /= sizeof(ino_t);
  37018. +
  37019. + err = -ENOMEM;
  37020. + if (!sbinfo->si_xib_buf)
  37021. + sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
  37022. + if (unlikely(!sbinfo->si_xib_buf))
  37023. + goto out_unset;
  37024. +
  37025. + sbinfo->si_xib_last_pindex = 0;
  37026. + sbinfo->si_xib_next_bit = 0;
  37027. + if (vfsub_f_size_read(file) < PAGE_SIZE) {
  37028. + pos = 0;
  37029. + err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
  37030. + PAGE_SIZE, &pos);
  37031. + if (unlikely(err != PAGE_SIZE))
  37032. + goto out_free;
  37033. + }
  37034. + err = 0;
  37035. + goto out; /* success */
  37036. +
  37037. +out_free:
  37038. + if (sbinfo->si_xib_buf)
  37039. + free_page((unsigned long)sbinfo->si_xib_buf);
  37040. + sbinfo->si_xib_buf = NULL;
  37041. + if (err >= 0)
  37042. + err = -EIO;
  37043. +out_unset:
  37044. + fput(sbinfo->si_xib);
  37045. + sbinfo->si_xib = NULL;
  37046. +out:
  37047. + AuTraceErr(err);
  37048. + return err;
  37049. +}
  37050. +
  37051. +/* xino for each branch */
  37052. +static void xino_clear_br(struct super_block *sb)
  37053. +{
  37054. + aufs_bindex_t bindex, bbot;
  37055. + struct au_branch *br;
  37056. +
  37057. + bbot = au_sbbot(sb);
  37058. + for (bindex = 0; bindex <= bbot; bindex++) {
  37059. + br = au_sbr(sb, bindex);
  37060. + AuDebugOn(!br);
  37061. + au_xino_put(br);
  37062. + }
  37063. +}
  37064. +
  37065. +static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
  37066. + aufs_bindex_t bshared)
  37067. +{
  37068. + struct au_branch *brshared;
  37069. +
  37070. + brshared = au_sbr(sb, bshared);
  37071. + AuDebugOn(!brshared->br_xino);
  37072. + AuDebugOn(!brshared->br_xino->xi_file);
  37073. + if (br->br_xino != brshared->br_xino) {
  37074. + au_xino_get(brshared);
  37075. + au_xino_put(br);
  37076. + br->br_xino = brshared->br_xino;
  37077. + }
  37078. +}
  37079. +
  37080. +struct au_xino_do_set_br {
  37081. + vfs_writef_t writef;
  37082. + struct au_branch *br;
  37083. + ino_t h_ino;
  37084. + aufs_bindex_t bshared;
  37085. +};
  37086. +
  37087. +static int au_xino_do_set_br(struct super_block *sb, struct path *path,
  37088. + struct au_xino_do_set_br *args)
  37089. +{
  37090. + int err;
  37091. + struct au_xi_calc calc;
  37092. + struct file *file;
  37093. + struct au_branch *br;
  37094. + struct au_xi_new xinew = {
  37095. + .base = path
  37096. + };
  37097. +
  37098. + br = args->br;
  37099. + xinew.xi = br->br_xino;
  37100. + au_xi_calc(sb, args->h_ino, &calc);
  37101. + xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
  37102. + if (args->bshared >= 0)
  37103. + /* shared xino */
  37104. + au_xino_set_br_shared(sb, br, args->bshared);
  37105. + else if (!xinew.xi) {
  37106. + /* new xino */
  37107. + err = au_xino_init(br, calc.idx, xinew.copy_src);
  37108. + if (unlikely(err))
  37109. + goto out;
  37110. + }
  37111. +
  37112. + /* force re-creating */
  37113. + xinew.xi = br->br_xino;
  37114. + xinew.idx = calc.idx;
  37115. + mutex_lock(&xinew.xi->xi_mtx);
  37116. + file = au_xi_new(sb, &xinew);
  37117. + mutex_unlock(&xinew.xi->xi_mtx);
  37118. + err = PTR_ERR(file);
  37119. + if (IS_ERR(file))
  37120. + goto out;
  37121. + AuDebugOn(!file);
  37122. +
  37123. + err = au_xino_do_write(args->writef, file, &calc, AUFS_ROOT_INO);
  37124. + if (unlikely(err))
  37125. + au_xino_put(br);
  37126. +
  37127. +out:
  37128. + AuTraceErr(err);
  37129. + return err;
  37130. +}
  37131. +
  37132. +static int au_xino_set_br(struct super_block *sb, struct path *path)
  37133. +{
  37134. + int err;
  37135. + aufs_bindex_t bindex, bbot;
  37136. + struct au_xino_do_set_br args;
  37137. + struct inode *inode;
  37138. +
  37139. + SiMustWriteLock(sb);
  37140. +
  37141. + bbot = au_sbbot(sb);
  37142. + inode = d_inode(sb->s_root);
  37143. + args.writef = au_sbi(sb)->si_xwrite;
  37144. + for (bindex = 0; bindex <= bbot; bindex++) {
  37145. + args.h_ino = au_h_iptr(inode, bindex)->i_ino;
  37146. + args.br = au_sbr(sb, bindex);
  37147. + args.bshared = is_sb_shared(sb, bindex, bindex - 1);
  37148. + err = au_xino_do_set_br(sb, path, &args);
  37149. + if (unlikely(err))
  37150. + break;
  37151. + }
  37152. +
  37153. + AuTraceErr(err);
  37154. + return err;
  37155. +}
  37156. +
  37157. +void au_xino_clr(struct super_block *sb)
  37158. +{
  37159. + struct au_sbinfo *sbinfo;
  37160. +
  37161. + au_xigen_clr(sb);
  37162. + xino_clear_xib(sb);
  37163. + xino_clear_br(sb);
  37164. + dbgaufs_brs_del(sb, 0);
  37165. + sbinfo = au_sbi(sb);
  37166. + /* lvalue, do not call au_mntflags() */
  37167. + au_opt_clr(sbinfo->si_mntflags, XINO);
  37168. +}
  37169. +
  37170. +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
  37171. +{
  37172. + int err, skip;
  37173. + struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
  37174. + struct qstr *dname, *cur_name;
  37175. + struct file *cur_xino;
  37176. + struct au_sbinfo *sbinfo;
  37177. + struct path *path, *cur_path;
  37178. +
  37179. + SiMustWriteLock(sb);
  37180. +
  37181. + err = 0;
  37182. + sbinfo = au_sbi(sb);
  37183. + path = &xiopt->file->f_path;
  37184. + dentry = path->dentry;
  37185. + parent = dget_parent(dentry);
  37186. + if (remount) {
  37187. + skip = 0;
  37188. + cur_xino = sbinfo->si_xib;
  37189. + if (cur_xino) {
  37190. + cur_path = &cur_xino->f_path;
  37191. + cur_dentry = cur_path->dentry;
  37192. + cur_parent = dget_parent(cur_dentry);
  37193. + cur_name = &cur_dentry->d_name;
  37194. + dname = &dentry->d_name;
  37195. + skip = (cur_parent == parent
  37196. + && au_qstreq(dname, cur_name));
  37197. + dput(cur_parent);
  37198. + }
  37199. + if (skip)
  37200. + goto out;
  37201. + }
  37202. +
  37203. + au_opt_set(sbinfo->si_mntflags, XINO);
  37204. + err = au_xino_set_xib(sb, path);
  37205. + /* si_x{read,write} are set */
  37206. + if (!err)
  37207. + err = au_xigen_set(sb, path);
  37208. + if (!err)
  37209. + err = au_xino_set_br(sb, path);
  37210. + if (!err) {
  37211. + dbgaufs_brs_add(sb, 0, /*topdown*/1);
  37212. + goto out; /* success */
  37213. + }
  37214. +
  37215. + /* reset all */
  37216. + AuIOErr("failed setting xino(%d).\n", err);
  37217. + au_xino_clr(sb);
  37218. +
  37219. +out:
  37220. + dput(parent);
  37221. + return err;
  37222. +}
  37223. +
  37224. +/*
  37225. + * create a xinofile at the default place/path.
  37226. + */
  37227. +struct file *au_xino_def(struct super_block *sb)
  37228. +{
  37229. + struct file *file;
  37230. + char *page, *p;
  37231. + struct au_branch *br;
  37232. + struct super_block *h_sb;
  37233. + struct path path;
  37234. + aufs_bindex_t bbot, bindex, bwr;
  37235. +
  37236. + br = NULL;
  37237. + bbot = au_sbbot(sb);
  37238. + bwr = -1;
  37239. + for (bindex = 0; bindex <= bbot; bindex++) {
  37240. + br = au_sbr(sb, bindex);
  37241. + if (au_br_writable(br->br_perm)
  37242. + && !au_test_fs_bad_xino(au_br_sb(br))) {
  37243. + bwr = bindex;
  37244. + break;
  37245. + }
  37246. + }
  37247. +
  37248. + if (bwr >= 0) {
  37249. + file = ERR_PTR(-ENOMEM);
  37250. + page = (void *)__get_free_page(GFP_NOFS);
  37251. + if (unlikely(!page))
  37252. + goto out;
  37253. + path.mnt = au_br_mnt(br);
  37254. + path.dentry = au_h_dptr(sb->s_root, bwr);
  37255. + p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
  37256. + file = (void *)p;
  37257. + if (!IS_ERR(p)) {
  37258. + strcat(p, "/" AUFS_XINO_FNAME);
  37259. + AuDbg("%s\n", p);
  37260. + file = au_xino_create(sb, p, /*silent*/0);
  37261. + }
  37262. + free_page((unsigned long)page);
  37263. + } else {
  37264. + file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
  37265. + if (IS_ERR(file))
  37266. + goto out;
  37267. + h_sb = file->f_path.dentry->d_sb;
  37268. + if (unlikely(au_test_fs_bad_xino(h_sb))) {
  37269. + pr_err("xino doesn't support %s(%s)\n",
  37270. + AUFS_XINO_DEFPATH, au_sbtype(h_sb));
  37271. + fput(file);
  37272. + file = ERR_PTR(-EINVAL);
  37273. + }
  37274. + }
  37275. +
  37276. +out:
  37277. + return file;
  37278. +}
  37279. +
  37280. +/* ---------------------------------------------------------------------- */
  37281. +
  37282. +/*
  37283. + * initialize the xinofile for the specified branch @br
  37284. + * at the place/path where @base_file indicates.
  37285. + * test whether another branch is on the same filesystem or not,
  37286. + * if found then share the xinofile with another branch.
  37287. + */
  37288. +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
  37289. + struct path *base)
  37290. +{
  37291. + int err;
  37292. + struct au_xino_do_set_br args = {
  37293. + .h_ino = h_ino,
  37294. + .br = br
  37295. + };
  37296. +
  37297. + args.writef = au_sbi(sb)->si_xwrite;
  37298. + args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
  37299. + au_br_sb(br));
  37300. + err = au_xino_do_set_br(sb, base, &args);
  37301. + if (unlikely(err))
  37302. + au_xino_put(br);
  37303. +
  37304. + return err;
  37305. +}
  37306. +
  37307. +/* ---------------------------------------------------------------------- */
  37308. +
  37309. +/*
  37310. + * get an unused inode number from bitmap
  37311. + */
  37312. +ino_t au_xino_new_ino(struct super_block *sb)
  37313. +{
  37314. + ino_t ino;
  37315. + unsigned long *p, pindex, ul, pend;
  37316. + struct au_sbinfo *sbinfo;
  37317. + struct file *file;
  37318. + int free_bit, err;
  37319. +
  37320. + if (!au_opt_test(au_mntflags(sb), XINO))
  37321. + return iunique(sb, AUFS_FIRST_INO);
  37322. +
  37323. + sbinfo = au_sbi(sb);
  37324. + mutex_lock(&sbinfo->si_xib_mtx);
  37325. + p = sbinfo->si_xib_buf;
  37326. + free_bit = sbinfo->si_xib_next_bit;
  37327. + if (free_bit < page_bits && !test_bit(free_bit, p))
  37328. + goto out; /* success */
  37329. + free_bit = find_first_zero_bit(p, page_bits);
  37330. + if (free_bit < page_bits)
  37331. + goto out; /* success */
  37332. +
  37333. + pindex = sbinfo->si_xib_last_pindex;
  37334. + for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
  37335. + err = xib_pindex(sb, ul);
  37336. + if (unlikely(err))
  37337. + goto out_err;
  37338. + free_bit = find_first_zero_bit(p, page_bits);
  37339. + if (free_bit < page_bits)
  37340. + goto out; /* success */
  37341. + }
  37342. +
  37343. + file = sbinfo->si_xib;
  37344. + pend = vfsub_f_size_read(file) / PAGE_SIZE;
  37345. + for (ul = pindex + 1; ul <= pend; ul++) {
  37346. + err = xib_pindex(sb, ul);
  37347. + if (unlikely(err))
  37348. + goto out_err;
  37349. + free_bit = find_first_zero_bit(p, page_bits);
  37350. + if (free_bit < page_bits)
  37351. + goto out; /* success */
  37352. + }
  37353. + BUG();
  37354. +
  37355. +out:
  37356. + set_bit(free_bit, p);
  37357. + sbinfo->si_xib_next_bit = free_bit + 1;
  37358. + pindex = sbinfo->si_xib_last_pindex;
  37359. + mutex_unlock(&sbinfo->si_xib_mtx);
  37360. + ino = xib_calc_ino(pindex, free_bit);
  37361. + AuDbg("i%lu\n", (unsigned long)ino);
  37362. + return ino;
  37363. +out_err:
  37364. + mutex_unlock(&sbinfo->si_xib_mtx);
  37365. + AuDbg("i0\n");
  37366. + return 0;
  37367. +}
  37368. +
  37369. +/* for s_op->delete_inode() */
  37370. +void au_xino_delete_inode(struct inode *inode, const int unlinked)
  37371. +{
  37372. + int err;
  37373. + unsigned int mnt_flags;
  37374. + aufs_bindex_t bindex, bbot, bi;
  37375. + unsigned char try_trunc;
  37376. + struct au_iinfo *iinfo;
  37377. + struct super_block *sb;
  37378. + struct au_hinode *hi;
  37379. + struct inode *h_inode;
  37380. + struct au_branch *br;
  37381. + vfs_writef_t xwrite;
  37382. + struct au_xi_calc calc;
  37383. + struct file *file;
  37384. +
  37385. + AuDebugOn(au_is_bad_inode(inode));
  37386. +
  37387. + sb = inode->i_sb;
  37388. + mnt_flags = au_mntflags(sb);
  37389. + if (!au_opt_test(mnt_flags, XINO)
  37390. + || inode->i_ino == AUFS_ROOT_INO)
  37391. + return;
  37392. +
  37393. + if (unlinked) {
  37394. + au_xigen_inc(inode);
  37395. + au_xib_clear_bit(inode);
  37396. + }
  37397. +
  37398. + iinfo = au_ii(inode);
  37399. + bindex = iinfo->ii_btop;
  37400. + if (bindex < 0)
  37401. + return;
  37402. +
  37403. + xwrite = au_sbi(sb)->si_xwrite;
  37404. + try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
  37405. + hi = au_hinode(iinfo, bindex);
  37406. + bbot = iinfo->ii_bbot;
  37407. + for (; bindex <= bbot; bindex++, hi++) {
  37408. + h_inode = hi->hi_inode;
  37409. + if (!h_inode
  37410. + || (!unlinked && h_inode->i_nlink))
  37411. + continue;
  37412. +
  37413. + /* inode may not be revalidated */
  37414. + bi = au_br_index(sb, hi->hi_id);
  37415. + if (bi < 0)
  37416. + continue;
  37417. +
  37418. + br = au_sbr(sb, bi);
  37419. + au_xi_calc(sb, h_inode->i_ino, &calc);
  37420. + file = au_xino_file(br->br_xino, calc.idx);
  37421. + if (IS_ERR_OR_NULL(file))
  37422. + continue;
  37423. +
  37424. + err = au_xino_do_write(xwrite, file, &calc, /*ino*/0);
  37425. + if (!err && try_trunc
  37426. + && au_test_fs_trunc_xino(au_br_sb(br)))
  37427. + xino_try_trunc(sb, br);
  37428. + }
  37429. +}
  37430. +
  37431. +/* ---------------------------------------------------------------------- */
  37432. +
  37433. +static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
  37434. +{
  37435. + int found, total, i;
  37436. +
  37437. + found = -1;
  37438. + total = xi->xi_nondir.total;
  37439. + for (i = 0; i < total; i++) {
  37440. + if (xi->xi_nondir.array[i] != h_ino)
  37441. + continue;
  37442. + found = i;
  37443. + break;
  37444. + }
  37445. +
  37446. + return found;
  37447. +}
  37448. +
  37449. +static int au_xinondir_expand(struct au_xino *xi)
  37450. +{
  37451. + int err, sz;
  37452. + ino_t *p;
  37453. +
  37454. + BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
  37455. +
  37456. + err = -ENOMEM;
  37457. + sz = xi->xi_nondir.total * sizeof(ino_t);
  37458. + if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
  37459. + goto out;
  37460. + p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
  37461. + /*may_shrink*/0);
  37462. + if (p) {
  37463. + xi->xi_nondir.array = p;
  37464. + xi->xi_nondir.total <<= 1;
  37465. + AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
  37466. + err = 0;
  37467. + }
  37468. +
  37469. +out:
  37470. + return err;
  37471. +}
  37472. +
  37473. +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
  37474. + ino_t h_ino, int idx)
  37475. +{
  37476. + struct au_xino *xi;
  37477. +
  37478. + AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
  37479. + xi = au_sbr(sb, bindex)->br_xino;
  37480. + AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
  37481. +
  37482. + spin_lock(&xi->xi_nondir.spin);
  37483. + AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
  37484. + xi->xi_nondir.array[idx] = 0;
  37485. + spin_unlock(&xi->xi_nondir.spin);
  37486. + wake_up_all(&xi->xi_nondir.wqh);
  37487. +}
  37488. +
  37489. +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  37490. + int *idx)
  37491. +{
  37492. + int err, found, empty;
  37493. + struct au_xino *xi;
  37494. +
  37495. + err = 0;
  37496. + *idx = -1;
  37497. + if (!au_opt_test(au_mntflags(sb), XINO))
  37498. + goto out; /* no xino */
  37499. +
  37500. + xi = au_sbr(sb, bindex)->br_xino;
  37501. +
  37502. +again:
  37503. + spin_lock(&xi->xi_nondir.spin);
  37504. + found = au_xinondir_find(xi, h_ino);
  37505. + if (found == -1) {
  37506. + empty = au_xinondir_find(xi, /*h_ino*/0);
  37507. + if (empty == -1) {
  37508. + empty = xi->xi_nondir.total;
  37509. + err = au_xinondir_expand(xi);
  37510. + if (unlikely(err))
  37511. + goto out_unlock;
  37512. + }
  37513. + xi->xi_nondir.array[empty] = h_ino;
  37514. + *idx = empty;
  37515. + } else {
  37516. + spin_unlock(&xi->xi_nondir.spin);
  37517. + wait_event(xi->xi_nondir.wqh,
  37518. + xi->xi_nondir.array[found] != h_ino);
  37519. + goto again;
  37520. + }
  37521. +
  37522. +out_unlock:
  37523. + spin_unlock(&xi->xi_nondir.spin);
  37524. +out:
  37525. + return err;
  37526. +}
  37527. +
  37528. +/* ---------------------------------------------------------------------- */
  37529. +
  37530. +int au_xino_path(struct seq_file *seq, struct file *file)
  37531. +{
  37532. + int err;
  37533. +
  37534. + err = au_seq_path(seq, &file->f_path);
  37535. + if (unlikely(err))
  37536. + goto out;
  37537. +
  37538. +#define Deleted "\\040(deleted)"
  37539. + seq->count -= sizeof(Deleted) - 1;
  37540. + AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
  37541. + sizeof(Deleted) - 1));
  37542. +#undef Deleted
  37543. +
  37544. +out:
  37545. + return err;
  37546. +}
  37547. --- /dev/null
  37548. +++ linux-4.19/include/uapi/linux/aufs_type.h 2018-11-20 09:35:15.055195505 +0200
  37549. @@ -0,0 +1,448 @@
  37550. +/* SPDX-License-Identifier: GPL-2.0 */
  37551. +/*
  37552. + * Copyright (C) 2005-2018 Junjiro R. Okajima
  37553. + *
  37554. + * This program, aufs is free software; you can redistribute it and/or modify
  37555. + * it under the terms of the GNU General Public License as published by
  37556. + * the Free Software Foundation; either version 2 of the License, or
  37557. + * (at your option) any later version.
  37558. + *
  37559. + * This program is distributed in the hope that it will be useful,
  37560. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37561. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37562. + * GNU General Public License for more details.
  37563. + *
  37564. + * You should have received a copy of the GNU General Public License
  37565. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  37566. + */
  37567. +
  37568. +#ifndef __AUFS_TYPE_H__
  37569. +#define __AUFS_TYPE_H__
  37570. +
  37571. +#define AUFS_NAME "aufs"
  37572. +
  37573. +#ifdef __KERNEL__
  37574. +/*
  37575. + * define it before including all other headers.
  37576. + * sched.h may use pr_* macros before defining "current", so define the
  37577. + * no-current version first, and re-define later.
  37578. + */
  37579. +#define pr_fmt(fmt) AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
  37580. +#include <linux/sched.h>
  37581. +#undef pr_fmt
  37582. +#define pr_fmt(fmt) \
  37583. + AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
  37584. + (int)sizeof(current->comm), current->comm, current->pid
  37585. +#else
  37586. +#include <stdint.h>
  37587. +#include <sys/types.h>
  37588. +#endif /* __KERNEL__ */
  37589. +
  37590. +#include <linux/limits.h>
  37591. +
  37592. +#define AUFS_VERSION "4.19-20181119"
  37593. +
  37594. +/* todo? move this to linux-2.6.19/include/magic.h */
  37595. +#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
  37596. +
  37597. +/* ---------------------------------------------------------------------- */
  37598. +
  37599. +#ifdef CONFIG_AUFS_BRANCH_MAX_127
  37600. +typedef int8_t aufs_bindex_t;
  37601. +#define AUFS_BRANCH_MAX 127
  37602. +#else
  37603. +typedef int16_t aufs_bindex_t;
  37604. +#ifdef CONFIG_AUFS_BRANCH_MAX_511
  37605. +#define AUFS_BRANCH_MAX 511
  37606. +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
  37607. +#define AUFS_BRANCH_MAX 1023
  37608. +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
  37609. +#define AUFS_BRANCH_MAX 32767
  37610. +#endif
  37611. +#endif
  37612. +
  37613. +#ifdef __KERNEL__
  37614. +#ifndef AUFS_BRANCH_MAX
  37615. +#error unknown CONFIG_AUFS_BRANCH_MAX value
  37616. +#endif
  37617. +#endif /* __KERNEL__ */
  37618. +
  37619. +/* ---------------------------------------------------------------------- */
  37620. +
  37621. +#define AUFS_FSTYPE AUFS_NAME
  37622. +
  37623. +#define AUFS_ROOT_INO 2
  37624. +#define AUFS_FIRST_INO 11
  37625. +
  37626. +#define AUFS_WH_PFX ".wh."
  37627. +#define AUFS_WH_PFX_LEN ((int)sizeof(AUFS_WH_PFX) - 1)
  37628. +#define AUFS_WH_TMP_LEN 4
  37629. +/* a limit for rmdir/rename a dir and copyup */
  37630. +#define AUFS_MAX_NAMELEN (NAME_MAX \
  37631. + - AUFS_WH_PFX_LEN * 2 /* doubly whiteouted */\
  37632. + - 1 /* dot */\
  37633. + - AUFS_WH_TMP_LEN) /* hex */
  37634. +#define AUFS_XINO_FNAME "." AUFS_NAME ".xino"
  37635. +#define AUFS_XINO_DEFPATH "/tmp/" AUFS_XINO_FNAME
  37636. +#define AUFS_XINO_DEF_SEC 30 /* seconds */
  37637. +#define AUFS_XINO_DEF_TRUNC 45 /* percentage */
  37638. +#define AUFS_DIRWH_DEF 3
  37639. +#define AUFS_RDCACHE_DEF 10 /* seconds */
  37640. +#define AUFS_RDCACHE_MAX 3600 /* seconds */
  37641. +#define AUFS_RDBLK_DEF 512 /* bytes */
  37642. +#define AUFS_RDHASH_DEF 32
  37643. +#define AUFS_WKQ_NAME AUFS_NAME "d"
  37644. +#define AUFS_MFS_DEF_SEC 30 /* seconds */
  37645. +#define AUFS_MFS_MAX_SEC 3600 /* seconds */
  37646. +#define AUFS_FHSM_CACHE_DEF_SEC 30 /* seconds */
  37647. +#define AUFS_PLINK_WARN 50 /* number of plinks in a single bucket */
  37648. +
  37649. +/* pseudo-link maintenace under /proc */
  37650. +#define AUFS_PLINK_MAINT_NAME "plink_maint"
  37651. +#define AUFS_PLINK_MAINT_DIR "fs/" AUFS_NAME
  37652. +#define AUFS_PLINK_MAINT_PATH AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
  37653. +
  37654. +/* dirren, renamed dir */
  37655. +#define AUFS_DR_INFO_PFX AUFS_WH_PFX ".dr."
  37656. +#define AUFS_DR_BRHINO_NAME AUFS_WH_PFX "hino"
  37657. +/* whiteouted doubly */
  37658. +#define AUFS_WH_DR_INFO_PFX AUFS_WH_PFX AUFS_DR_INFO_PFX
  37659. +#define AUFS_WH_DR_BRHINO AUFS_WH_PFX AUFS_DR_BRHINO_NAME
  37660. +
  37661. +#define AUFS_DIROPQ_NAME AUFS_WH_PFX ".opq" /* whiteouted doubly */
  37662. +#define AUFS_WH_DIROPQ AUFS_WH_PFX AUFS_DIROPQ_NAME
  37663. +
  37664. +#define AUFS_BASE_NAME AUFS_WH_PFX AUFS_NAME
  37665. +#define AUFS_PLINKDIR_NAME AUFS_WH_PFX "plnk"
  37666. +#define AUFS_ORPHDIR_NAME AUFS_WH_PFX "orph"
  37667. +
  37668. +/* doubly whiteouted */
  37669. +#define AUFS_WH_BASE AUFS_WH_PFX AUFS_BASE_NAME
  37670. +#define AUFS_WH_PLINKDIR AUFS_WH_PFX AUFS_PLINKDIR_NAME
  37671. +#define AUFS_WH_ORPHDIR AUFS_WH_PFX AUFS_ORPHDIR_NAME
  37672. +
  37673. +/* branch permissions and attributes */
  37674. +#define AUFS_BRPERM_RW "rw"
  37675. +#define AUFS_BRPERM_RO "ro"
  37676. +#define AUFS_BRPERM_RR "rr"
  37677. +#define AUFS_BRATTR_COO_REG "coo_reg"
  37678. +#define AUFS_BRATTR_COO_ALL "coo_all"
  37679. +#define AUFS_BRATTR_FHSM "fhsm"
  37680. +#define AUFS_BRATTR_UNPIN "unpin"
  37681. +#define AUFS_BRATTR_ICEX "icex"
  37682. +#define AUFS_BRATTR_ICEX_SEC "icexsec"
  37683. +#define AUFS_BRATTR_ICEX_SYS "icexsys"
  37684. +#define AUFS_BRATTR_ICEX_TR "icextr"
  37685. +#define AUFS_BRATTR_ICEX_USR "icexusr"
  37686. +#define AUFS_BRATTR_ICEX_OTH "icexoth"
  37687. +#define AUFS_BRRATTR_WH "wh"
  37688. +#define AUFS_BRWATTR_NLWH "nolwh"
  37689. +#define AUFS_BRWATTR_MOO "moo"
  37690. +
  37691. +#define AuBrPerm_RW 1 /* writable, hardlinkable wh */
  37692. +#define AuBrPerm_RO (1 << 1) /* readonly */
  37693. +#define AuBrPerm_RR (1 << 2) /* natively readonly */
  37694. +#define AuBrPerm_Mask (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
  37695. +
  37696. +#define AuBrAttr_COO_REG (1 << 3) /* copy-up on open */
  37697. +#define AuBrAttr_COO_ALL (1 << 4)
  37698. +#define AuBrAttr_COO_Mask (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
  37699. +
  37700. +#define AuBrAttr_FHSM (1 << 5) /* file-based hsm */
  37701. +#define AuBrAttr_UNPIN (1 << 6) /* rename-able top dir of
  37702. + branch. meaningless since
  37703. + linux-3.18-rc1 */
  37704. +
  37705. +/* ignore error in copying XATTR */
  37706. +#define AuBrAttr_ICEX_SEC (1 << 7)
  37707. +#define AuBrAttr_ICEX_SYS (1 << 8)
  37708. +#define AuBrAttr_ICEX_TR (1 << 9)
  37709. +#define AuBrAttr_ICEX_USR (1 << 10)
  37710. +#define AuBrAttr_ICEX_OTH (1 << 11)
  37711. +#define AuBrAttr_ICEX (AuBrAttr_ICEX_SEC \
  37712. + | AuBrAttr_ICEX_SYS \
  37713. + | AuBrAttr_ICEX_TR \
  37714. + | AuBrAttr_ICEX_USR \
  37715. + | AuBrAttr_ICEX_OTH)
  37716. +
  37717. +#define AuBrRAttr_WH (1 << 12) /* whiteout-able */
  37718. +#define AuBrRAttr_Mask AuBrRAttr_WH
  37719. +
  37720. +#define AuBrWAttr_NoLinkWH (1 << 13) /* un-hardlinkable whiteouts */
  37721. +#define AuBrWAttr_MOO (1 << 14) /* move-up on open */
  37722. +#define AuBrWAttr_Mask (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
  37723. +
  37724. +#define AuBrAttr_CMOO_Mask (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
  37725. +
  37726. +/* #warning test userspace */
  37727. +#ifdef __KERNEL__
  37728. +#ifndef CONFIG_AUFS_FHSM
  37729. +#undef AuBrAttr_FHSM
  37730. +#define AuBrAttr_FHSM 0
  37731. +#endif
  37732. +#ifndef CONFIG_AUFS_XATTR
  37733. +#undef AuBrAttr_ICEX
  37734. +#define AuBrAttr_ICEX 0
  37735. +#undef AuBrAttr_ICEX_SEC
  37736. +#define AuBrAttr_ICEX_SEC 0
  37737. +#undef AuBrAttr_ICEX_SYS
  37738. +#define AuBrAttr_ICEX_SYS 0
  37739. +#undef AuBrAttr_ICEX_TR
  37740. +#define AuBrAttr_ICEX_TR 0
  37741. +#undef AuBrAttr_ICEX_USR
  37742. +#define AuBrAttr_ICEX_USR 0
  37743. +#undef AuBrAttr_ICEX_OTH
  37744. +#define AuBrAttr_ICEX_OTH 0
  37745. +#endif
  37746. +#endif
  37747. +
  37748. +/* the longest combination */
  37749. +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
  37750. +#define AuBrPermStrSz sizeof(AUFS_BRPERM_RW \
  37751. + "+" AUFS_BRATTR_COO_REG \
  37752. + "+" AUFS_BRATTR_FHSM \
  37753. + "+" AUFS_BRATTR_UNPIN \
  37754. + "+" AUFS_BRATTR_ICEX_SEC \
  37755. + "+" AUFS_BRATTR_ICEX_SYS \
  37756. + "+" AUFS_BRATTR_ICEX_USR \
  37757. + "+" AUFS_BRATTR_ICEX_OTH \
  37758. + "+" AUFS_BRWATTR_NLWH)
  37759. +
  37760. +typedef struct {
  37761. + char a[AuBrPermStrSz];
  37762. +} au_br_perm_str_t;
  37763. +
  37764. +static inline int au_br_writable(int brperm)
  37765. +{
  37766. + return brperm & AuBrPerm_RW;
  37767. +}
  37768. +
  37769. +static inline int au_br_whable(int brperm)
  37770. +{
  37771. + return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
  37772. +}
  37773. +
  37774. +static inline int au_br_wh_linkable(int brperm)
  37775. +{
  37776. + return !(brperm & AuBrWAttr_NoLinkWH);
  37777. +}
  37778. +
  37779. +static inline int au_br_cmoo(int brperm)
  37780. +{
  37781. + return brperm & AuBrAttr_CMOO_Mask;
  37782. +}
  37783. +
  37784. +static inline int au_br_fhsm(int brperm)
  37785. +{
  37786. + return brperm & AuBrAttr_FHSM;
  37787. +}
  37788. +
  37789. +/* ---------------------------------------------------------------------- */
  37790. +
  37791. +/* ioctl */
  37792. +enum {
  37793. + /* readdir in userspace */
  37794. + AuCtl_RDU,
  37795. + AuCtl_RDU_INO,
  37796. +
  37797. + AuCtl_WBR_FD, /* pathconf wrapper */
  37798. + AuCtl_IBUSY, /* busy inode */
  37799. + AuCtl_MVDOWN, /* move-down */
  37800. + AuCtl_BR, /* info about branches */
  37801. + AuCtl_FHSM_FD /* connection for fhsm */
  37802. +};
  37803. +
  37804. +/* borrowed from linux/include/linux/kernel.h */
  37805. +#ifndef ALIGN
  37806. +#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
  37807. +#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
  37808. +#endif
  37809. +
  37810. +/* borrowed from linux/include/linux/compiler-gcc3.h */
  37811. +#ifndef __aligned
  37812. +#define __aligned(x) __attribute__((aligned(x)))
  37813. +#endif
  37814. +
  37815. +#ifdef __KERNEL__
  37816. +#ifndef __packed
  37817. +#define __packed __attribute__((packed))
  37818. +#endif
  37819. +#endif
  37820. +
  37821. +struct au_rdu_cookie {
  37822. + uint64_t h_pos;
  37823. + int16_t bindex;
  37824. + uint8_t flags;
  37825. + uint8_t pad;
  37826. + uint32_t generation;
  37827. +} __aligned(8);
  37828. +
  37829. +struct au_rdu_ent {
  37830. + uint64_t ino;
  37831. + int16_t bindex;
  37832. + uint8_t type;
  37833. + uint8_t nlen;
  37834. + uint8_t wh;
  37835. + char name[0];
  37836. +} __aligned(8);
  37837. +
  37838. +static inline int au_rdu_len(int nlen)
  37839. +{
  37840. + /* include the terminating NULL */
  37841. + return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
  37842. + sizeof(uint64_t));
  37843. +}
  37844. +
  37845. +union au_rdu_ent_ul {
  37846. + struct au_rdu_ent __user *e;
  37847. + uint64_t ul;
  37848. +};
  37849. +
  37850. +enum {
  37851. + AufsCtlRduV_SZ,
  37852. + AufsCtlRduV_End
  37853. +};
  37854. +
  37855. +struct aufs_rdu {
  37856. + /* input */
  37857. + union {
  37858. + uint64_t sz; /* AuCtl_RDU */
  37859. + uint64_t nent; /* AuCtl_RDU_INO */
  37860. + };
  37861. + union au_rdu_ent_ul ent;
  37862. + uint16_t verify[AufsCtlRduV_End];
  37863. +
  37864. + /* input/output */
  37865. + uint32_t blk;
  37866. +
  37867. + /* output */
  37868. + union au_rdu_ent_ul tail;
  37869. + /* number of entries which were added in a single call */
  37870. + uint64_t rent;
  37871. + uint8_t full;
  37872. + uint8_t shwh;
  37873. +
  37874. + struct au_rdu_cookie cookie;
  37875. +} __aligned(8);
  37876. +
  37877. +/* ---------------------------------------------------------------------- */
  37878. +
  37879. +/* dirren. the branch is identified by the filename who contains this */
  37880. +struct au_drinfo {
  37881. + uint64_t ino;
  37882. + union {
  37883. + uint8_t oldnamelen;
  37884. + uint64_t _padding;
  37885. + };
  37886. + uint8_t oldname[0];
  37887. +} __aligned(8);
  37888. +
  37889. +struct au_drinfo_fdata {
  37890. + uint32_t magic;
  37891. + struct au_drinfo drinfo;
  37892. +} __aligned(8);
  37893. +
  37894. +#define AUFS_DRINFO_MAGIC_V1 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
  37895. +/* future */
  37896. +#define AUFS_DRINFO_MAGIC_V2 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
  37897. +
  37898. +/* ---------------------------------------------------------------------- */
  37899. +
  37900. +struct aufs_wbr_fd {
  37901. + uint32_t oflags;
  37902. + int16_t brid;
  37903. +} __aligned(8);
  37904. +
  37905. +/* ---------------------------------------------------------------------- */
  37906. +
  37907. +struct aufs_ibusy {
  37908. + uint64_t ino, h_ino;
  37909. + int16_t bindex;
  37910. +} __aligned(8);
  37911. +
  37912. +/* ---------------------------------------------------------------------- */
  37913. +
  37914. +/* error code for move-down */
  37915. +/* the actual message strings are implemented in aufs-util.git */
  37916. +enum {
  37917. + EAU_MVDOWN_OPAQUE = 1,
  37918. + EAU_MVDOWN_WHITEOUT,
  37919. + EAU_MVDOWN_UPPER,
  37920. + EAU_MVDOWN_BOTTOM,
  37921. + EAU_MVDOWN_NOUPPER,
  37922. + EAU_MVDOWN_NOLOWERBR,
  37923. + EAU_Last
  37924. +};
  37925. +
  37926. +/* flags for move-down */
  37927. +#define AUFS_MVDOWN_DMSG 1
  37928. +#define AUFS_MVDOWN_OWLOWER (1 << 1) /* overwrite lower */
  37929. +#define AUFS_MVDOWN_KUPPER (1 << 2) /* keep upper */
  37930. +#define AUFS_MVDOWN_ROLOWER (1 << 3) /* do even if lower is RO */
  37931. +#define AUFS_MVDOWN_ROLOWER_R (1 << 4) /* did on lower RO */
  37932. +#define AUFS_MVDOWN_ROUPPER (1 << 5) /* do even if upper is RO */
  37933. +#define AUFS_MVDOWN_ROUPPER_R (1 << 6) /* did on upper RO */
  37934. +#define AUFS_MVDOWN_BRID_UPPER (1 << 7) /* upper brid */
  37935. +#define AUFS_MVDOWN_BRID_LOWER (1 << 8) /* lower brid */
  37936. +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9) /* find fhsm attr for lower */
  37937. +#define AUFS_MVDOWN_STFS (1 << 10) /* req. stfs */
  37938. +#define AUFS_MVDOWN_STFS_FAILED (1 << 11) /* output: stfs is unusable */
  37939. +#define AUFS_MVDOWN_BOTTOM (1 << 12) /* output: no more lowers */
  37940. +
  37941. +/* index for move-down */
  37942. +enum {
  37943. + AUFS_MVDOWN_UPPER,
  37944. + AUFS_MVDOWN_LOWER,
  37945. + AUFS_MVDOWN_NARRAY
  37946. +};
  37947. +
  37948. +/*
  37949. + * additional info of move-down
  37950. + * number of free blocks and inodes.
  37951. + * subset of struct kstatfs, but smaller and always 64bit.
  37952. + */
  37953. +struct aufs_stfs {
  37954. + uint64_t f_blocks;
  37955. + uint64_t f_bavail;
  37956. + uint64_t f_files;
  37957. + uint64_t f_ffree;
  37958. +};
  37959. +
  37960. +struct aufs_stbr {
  37961. + int16_t brid; /* optional input */
  37962. + int16_t bindex; /* output */
  37963. + struct aufs_stfs stfs; /* output when AUFS_MVDOWN_STFS set */
  37964. +} __aligned(8);
  37965. +
  37966. +struct aufs_mvdown {
  37967. + uint32_t flags; /* input/output */
  37968. + struct aufs_stbr stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
  37969. + int8_t au_errno; /* output */
  37970. +} __aligned(8);
  37971. +
  37972. +/* ---------------------------------------------------------------------- */
  37973. +
  37974. +union aufs_brinfo {
  37975. + /* PATH_MAX may differ between kernel-space and user-space */
  37976. + char _spacer[4096];
  37977. + struct {
  37978. + int16_t id;
  37979. + int perm;
  37980. + char path[0];
  37981. + };
  37982. +} __aligned(8);
  37983. +
  37984. +/* ---------------------------------------------------------------------- */
  37985. +
  37986. +#define AuCtlType 'A'
  37987. +#define AUFS_CTL_RDU _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
  37988. +#define AUFS_CTL_RDU_INO _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
  37989. +#define AUFS_CTL_WBR_FD _IOW(AuCtlType, AuCtl_WBR_FD, \
  37990. + struct aufs_wbr_fd)
  37991. +#define AUFS_CTL_IBUSY _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
  37992. +#define AUFS_CTL_MVDOWN _IOWR(AuCtlType, AuCtl_MVDOWN, \
  37993. + struct aufs_mvdown)
  37994. +#define AUFS_CTL_BRINFO _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
  37995. +#define AUFS_CTL_FHSM_FD _IOW(AuCtlType, AuCtl_FHSM_FD, int)
  37996. +
  37997. +#endif /* __AUFS_TYPE_H__ */