aufs5.patch 972 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852168531685416855168561685716858168591686016861168621686316864168651686616867168681686916870168711687216873168741687516876168771687816879168801688116882168831688416885168861688716888168891689016891168921689316894168951689616897168981689916900169011690216903169041690516906169071690816909169101691116912169131691416915169161691716918169191692016921169221692316924169251692616927169281692916930169311693216933169341693516936169371693816939169401694116942169431694416945169461694716948169491695016951169521695316954169551695616957169581695916960169611696216963169641696516966169671696816969169701697116972169731697416975169761697716978169791698016981169821698316984169851698616987169881698916990169911699216993169941699516996169971699816999170001700117002170031700417005170061700717008170091701017011170121701317014170151701617017170181701917020170211702217023170241702517026170271702817029170301703117032170331703417035170361703717038170391704017041170421704317044170451704617047170481704917050170511705217053170541705517056170571705817059170601706117062170631706417065170661706717068170691707017071170721707317074170751707617077170781707917080170811708217083170841708517086170871708817089170901709117092170931709417095170961709717098170991710017101171021710317104171051710617107171081710917110171111711217113171141711517116171171711817119171201712117122171231712417125171261712717128171291713017131171321713317134171351713617137171381713917140171411714217143171441714517146171471714817149171501715117152171531715417155171561715717158171591716017161171621716317164171651716617167171681716917170171711717217173171741717517176171771717817179171801718117182171831718417185171861718717188171891719017191171921719317194171951719617197171981719917200172011720217203172041720517206172071720817209172101721117212172131721417215172161721717218172191722017221172221722317224172251722617227172281722917230172311723217233172341723517236172371723817239172401724117242172431724417245172461724717248172491725017251172521725317254172551725617257172581725917260172611726217263172641726517266172671726817269172701727117272172731727417275172761727717278172791728017281172821728317284172851728617287172881728917290172911729217293172941729517296172971729817299173001730117302173031730417305173061730717308173091731017311173121731317314173151731617317173181731917320173211732217323173241732517326173271732817329173301733117332173331733417335173361733717338173391734017341173421734317344173451734617347173481734917350173511735217353173541735517356173571735817359173601736117362173631736417365173661736717368173691737017371173721737317374173751737617377173781737917380173811738217383173841738517386173871738817389173901739117392173931739417395173961739717398173991740017401174021740317404174051740617407174081740917410174111741217413174141741517416174171741817419174201742117422174231742417425174261742717428174291743017431174321743317434174351743617437174381743917440174411744217443174441744517446174471744817449174501745117452174531745417455174561745717458174591746017461174621746317464174651746617467174681746917470174711747217473174741747517476174771747817479174801748117482174831748417485174861748717488174891749017491174921749317494174951749617497174981749917500175011750217503175041750517506175071750817509175101751117512175131751417515175161751717518175191752017521175221752317524175251752617527175281752917530175311753217533175341753517536175371753817539175401754117542175431754417545175461754717548175491755017551175521755317554175551755617557175581755917560175611756217563175641756517566175671756817569175701757117572175731757417575175761757717578175791758017581175821758317584175851758617587175881758917590175911759217593175941759517596175971759817599176001760117602176031760417605176061760717608176091761017611176121761317614176151761617617176181761917620176211762217623176241762517626176271762817629176301763117632176331763417635176361763717638176391764017641176421764317644176451764617647176481764917650176511765217653176541765517656176571765817659176601766117662176631766417665176661766717668176691767017671176721767317674176751767617677176781767917680176811768217683176841768517686176871768817689176901769117692176931769417695176961769717698176991770017701177021770317704177051770617707177081770917710177111771217713177141771517716177171771817719177201772117722177231772417725177261772717728177291773017731177321773317734177351773617737177381773917740177411774217743177441774517746177471774817749177501775117752177531775417755177561775717758177591776017761177621776317764177651776617767177681776917770177711777217773177741777517776177771777817779177801778117782177831778417785177861778717788177891779017791177921779317794177951779617797177981779917800178011780217803178041780517806178071780817809178101781117812178131781417815178161781717818178191782017821178221782317824178251782617827178281782917830178311783217833178341783517836178371783817839178401784117842178431784417845178461784717848178491785017851178521785317854178551785617857178581785917860178611786217863178641786517866178671786817869178701787117872178731787417875178761787717878178791788017881178821788317884178851788617887178881788917890178911789217893178941789517896178971789817899179001790117902179031790417905179061790717908179091791017911179121791317914179151791617917179181791917920179211792217923179241792517926179271792817929179301793117932179331793417935179361793717938179391794017941179421794317944179451794617947179481794917950179511795217953179541795517956179571795817959179601796117962179631796417965179661796717968179691797017971179721797317974179751797617977179781797917980179811798217983179841798517986179871798817989179901799117992179931799417995179961799717998179991800018001180021800318004180051800618007180081800918010180111801218013180141801518016180171801818019180201802118022180231802418025180261802718028180291803018031180321803318034180351803618037180381803918040180411804218043180441804518046180471804818049180501805118052180531805418055180561805718058180591806018061180621806318064180651806618067180681806918070180711807218073180741807518076180771807818079180801808118082180831808418085180861808718088180891809018091180921809318094180951809618097180981809918100181011810218103181041810518106181071810818109181101811118112181131811418115181161811718118181191812018121181221812318124181251812618127181281812918130181311813218133181341813518136181371813818139181401814118142181431814418145181461814718148181491815018151181521815318154181551815618157181581815918160181611816218163181641816518166181671816818169181701817118172181731817418175181761817718178181791818018181181821818318184181851818618187181881818918190181911819218193181941819518196181971819818199182001820118202182031820418205182061820718208182091821018211182121821318214182151821618217182181821918220182211822218223182241822518226182271822818229182301823118232182331823418235182361823718238182391824018241182421824318244182451824618247182481824918250182511825218253182541825518256182571825818259182601826118262182631826418265182661826718268182691827018271182721827318274182751827618277182781827918280182811828218283182841828518286182871828818289182901829118292182931829418295182961829718298182991830018301183021830318304183051830618307183081830918310183111831218313183141831518316183171831818319183201832118322183231832418325183261832718328183291833018331183321833318334183351833618337183381833918340183411834218343183441834518346183471834818349183501835118352183531835418355183561835718358183591836018361183621836318364183651836618367183681836918370183711837218373183741837518376183771837818379183801838118382183831838418385183861838718388183891839018391183921839318394183951839618397183981839918400184011840218403184041840518406184071840818409184101841118412184131841418415184161841718418184191842018421184221842318424184251842618427184281842918430184311843218433184341843518436184371843818439184401844118442184431844418445184461844718448184491845018451184521845318454184551845618457184581845918460184611846218463184641846518466184671846818469184701847118472184731847418475184761847718478184791848018481184821848318484184851848618487184881848918490184911849218493184941849518496184971849818499185001850118502185031850418505185061850718508185091851018511185121851318514185151851618517185181851918520185211852218523185241852518526185271852818529185301853118532185331853418535185361853718538185391854018541185421854318544185451854618547185481854918550185511855218553185541855518556185571855818559185601856118562185631856418565185661856718568185691857018571185721857318574185751857618577185781857918580185811858218583185841858518586185871858818589185901859118592185931859418595185961859718598185991860018601186021860318604186051860618607186081860918610186111861218613186141861518616186171861818619186201862118622186231862418625186261862718628186291863018631186321863318634186351863618637186381863918640186411864218643186441864518646186471864818649186501865118652186531865418655186561865718658186591866018661186621866318664186651866618667186681866918670186711867218673186741867518676186771867818679186801868118682186831868418685186861868718688186891869018691186921869318694186951869618697186981869918700187011870218703187041870518706187071870818709187101871118712187131871418715187161871718718187191872018721187221872318724187251872618727187281872918730187311873218733187341873518736187371873818739187401874118742187431874418745187461874718748187491875018751187521875318754187551875618757187581875918760187611876218763187641876518766187671876818769187701877118772187731877418775187761877718778187791878018781187821878318784187851878618787187881878918790187911879218793187941879518796187971879818799188001880118802188031880418805188061880718808188091881018811188121881318814188151881618817188181881918820188211882218823188241882518826188271882818829188301883118832188331883418835188361883718838188391884018841188421884318844188451884618847188481884918850188511885218853188541885518856188571885818859188601886118862188631886418865188661886718868188691887018871188721887318874188751887618877188781887918880188811888218883188841888518886188871888818889188901889118892188931889418895188961889718898188991890018901189021890318904189051890618907189081890918910189111891218913189141891518916189171891818919189201892118922189231892418925189261892718928189291893018931189321893318934189351893618937189381893918940189411894218943189441894518946189471894818949189501895118952189531895418955189561895718958189591896018961189621896318964189651896618967189681896918970189711897218973189741897518976189771897818979189801898118982189831898418985189861898718988189891899018991189921899318994189951899618997189981899919000190011900219003190041900519006190071900819009190101901119012190131901419015190161901719018190191902019021190221902319024190251902619027190281902919030190311903219033190341903519036190371903819039190401904119042190431904419045190461904719048190491905019051190521905319054190551905619057190581905919060190611906219063190641906519066190671906819069190701907119072190731907419075190761907719078190791908019081190821908319084190851908619087190881908919090190911909219093190941909519096190971909819099191001910119102191031910419105191061910719108191091911019111191121911319114191151911619117191181911919120191211912219123191241912519126191271912819129191301913119132191331913419135191361913719138191391914019141191421914319144191451914619147191481914919150191511915219153191541915519156191571915819159191601916119162191631916419165191661916719168191691917019171191721917319174191751917619177191781917919180191811918219183191841918519186191871918819189191901919119192191931919419195191961919719198191991920019201192021920319204192051920619207192081920919210192111921219213192141921519216192171921819219192201922119222192231922419225192261922719228192291923019231192321923319234192351923619237192381923919240192411924219243192441924519246192471924819249192501925119252192531925419255192561925719258192591926019261192621926319264192651926619267192681926919270192711927219273192741927519276192771927819279192801928119282192831928419285192861928719288192891929019291192921929319294192951929619297192981929919300193011930219303193041930519306193071930819309193101931119312193131931419315193161931719318193191932019321193221932319324193251932619327193281932919330193311933219333193341933519336193371933819339193401934119342193431934419345193461934719348193491935019351193521935319354193551935619357193581935919360193611936219363193641936519366193671936819369193701937119372193731937419375193761937719378193791938019381193821938319384193851938619387193881938919390193911939219393193941939519396193971939819399194001940119402194031940419405194061940719408194091941019411194121941319414194151941619417194181941919420194211942219423194241942519426194271942819429194301943119432194331943419435194361943719438194391944019441194421944319444194451944619447194481944919450194511945219453194541945519456194571945819459194601946119462194631946419465194661946719468194691947019471194721947319474194751947619477194781947919480194811948219483194841948519486194871948819489194901949119492194931949419495194961949719498194991950019501195021950319504195051950619507195081950919510195111951219513195141951519516195171951819519195201952119522195231952419525195261952719528195291953019531195321953319534195351953619537195381953919540195411954219543195441954519546195471954819549195501955119552195531955419555195561955719558195591956019561195621956319564195651956619567195681956919570195711957219573195741957519576195771957819579195801958119582195831958419585195861958719588195891959019591195921959319594195951959619597195981959919600196011960219603196041960519606196071960819609196101961119612196131961419615196161961719618196191962019621196221962319624196251962619627196281962919630196311963219633196341963519636196371963819639196401964119642196431964419645196461964719648196491965019651196521965319654196551965619657196581965919660196611966219663196641966519666196671966819669196701967119672196731967419675196761967719678196791968019681196821968319684196851968619687196881968919690196911969219693196941969519696196971969819699197001970119702197031970419705197061970719708197091971019711197121971319714197151971619717197181971919720197211972219723197241972519726197271972819729197301973119732197331973419735197361973719738197391974019741197421974319744197451974619747197481974919750197511975219753197541975519756197571975819759197601976119762197631976419765197661976719768197691977019771197721977319774197751977619777197781977919780197811978219783197841978519786197871978819789197901979119792197931979419795197961979719798197991980019801198021980319804198051980619807198081980919810198111981219813198141981519816198171981819819198201982119822198231982419825198261982719828198291983019831198321983319834198351983619837198381983919840198411984219843198441984519846198471984819849198501985119852198531985419855198561985719858198591986019861198621986319864198651986619867198681986919870198711987219873198741987519876198771987819879198801988119882198831988419885198861988719888198891989019891198921989319894198951989619897198981989919900199011990219903199041990519906199071990819909199101991119912199131991419915199161991719918199191992019921199221992319924199251992619927199281992919930199311993219933199341993519936199371993819939199401994119942199431994419945199461994719948199491995019951199521995319954199551995619957199581995919960199611996219963199641996519966199671996819969199701997119972199731997419975199761997719978199791998019981199821998319984199851998619987199881998919990199911999219993199941999519996199971999819999200002000120002200032000420005200062000720008200092001020011200122001320014200152001620017200182001920020200212002220023200242002520026200272002820029200302003120032200332003420035200362003720038200392004020041200422004320044200452004620047200482004920050200512005220053200542005520056200572005820059200602006120062200632006420065200662006720068200692007020071200722007320074200752007620077200782007920080200812008220083200842008520086200872008820089200902009120092200932009420095200962009720098200992010020101201022010320104201052010620107201082010920110201112011220113201142011520116201172011820119201202012120122201232012420125201262012720128201292013020131201322013320134201352013620137201382013920140201412014220143201442014520146201472014820149201502015120152201532015420155201562015720158201592016020161201622016320164201652016620167201682016920170201712017220173201742017520176201772017820179201802018120182201832018420185201862018720188201892019020191201922019320194201952019620197201982019920200202012020220203202042020520206202072020820209202102021120212202132021420215202162021720218202192022020221202222022320224202252022620227202282022920230202312023220233202342023520236202372023820239202402024120242202432024420245202462024720248202492025020251202522025320254202552025620257202582025920260202612026220263202642026520266202672026820269202702027120272202732027420275202762027720278202792028020281202822028320284202852028620287202882028920290202912029220293202942029520296202972029820299203002030120302203032030420305203062030720308203092031020311203122031320314203152031620317203182031920320203212032220323203242032520326203272032820329203302033120332203332033420335203362033720338203392034020341203422034320344203452034620347203482034920350203512035220353203542035520356203572035820359203602036120362203632036420365203662036720368203692037020371203722037320374203752037620377203782037920380203812038220383203842038520386203872038820389203902039120392203932039420395203962039720398203992040020401204022040320404204052040620407204082040920410204112041220413204142041520416204172041820419204202042120422204232042420425204262042720428204292043020431204322043320434204352043620437204382043920440204412044220443204442044520446204472044820449204502045120452204532045420455204562045720458204592046020461204622046320464204652046620467204682046920470204712047220473204742047520476204772047820479204802048120482204832048420485204862048720488204892049020491204922049320494204952049620497204982049920500205012050220503205042050520506205072050820509205102051120512205132051420515205162051720518205192052020521205222052320524205252052620527205282052920530205312053220533205342053520536205372053820539205402054120542205432054420545205462054720548205492055020551205522055320554205552055620557205582055920560205612056220563205642056520566205672056820569205702057120572205732057420575205762057720578205792058020581205822058320584205852058620587205882058920590205912059220593205942059520596205972059820599206002060120602206032060420605206062060720608206092061020611206122061320614206152061620617206182061920620206212062220623206242062520626206272062820629206302063120632206332063420635206362063720638206392064020641206422064320644206452064620647206482064920650206512065220653206542065520656206572065820659206602066120662206632066420665206662066720668206692067020671206722067320674206752067620677206782067920680206812068220683206842068520686206872068820689206902069120692206932069420695206962069720698206992070020701207022070320704207052070620707207082070920710207112071220713207142071520716207172071820719207202072120722207232072420725207262072720728207292073020731207322073320734207352073620737207382073920740207412074220743207442074520746207472074820749207502075120752207532075420755207562075720758207592076020761207622076320764207652076620767207682076920770207712077220773207742077520776207772077820779207802078120782207832078420785207862078720788207892079020791207922079320794207952079620797207982079920800208012080220803208042080520806208072080820809208102081120812208132081420815208162081720818208192082020821208222082320824208252082620827208282082920830208312083220833208342083520836208372083820839208402084120842208432084420845208462084720848208492085020851208522085320854208552085620857208582085920860208612086220863208642086520866208672086820869208702087120872208732087420875208762087720878208792088020881208822088320884208852088620887208882088920890208912089220893208942089520896208972089820899209002090120902209032090420905209062090720908209092091020911209122091320914209152091620917209182091920920209212092220923209242092520926209272092820929209302093120932209332093420935209362093720938209392094020941209422094320944209452094620947209482094920950209512095220953209542095520956209572095820959209602096120962209632096420965209662096720968209692097020971209722097320974209752097620977209782097920980209812098220983209842098520986209872098820989209902099120992209932099420995209962099720998209992100021001210022100321004210052100621007210082100921010210112101221013210142101521016210172101821019210202102121022210232102421025210262102721028210292103021031210322103321034210352103621037210382103921040210412104221043210442104521046210472104821049210502105121052210532105421055210562105721058210592106021061210622106321064210652106621067210682106921070210712107221073210742107521076210772107821079210802108121082210832108421085210862108721088210892109021091210922109321094210952109621097210982109921100211012110221103211042110521106211072110821109211102111121112211132111421115211162111721118211192112021121211222112321124211252112621127211282112921130211312113221133211342113521136211372113821139211402114121142211432114421145211462114721148211492115021151211522115321154211552115621157211582115921160211612116221163211642116521166211672116821169211702117121172211732117421175211762117721178211792118021181211822118321184211852118621187211882118921190211912119221193211942119521196211972119821199212002120121202212032120421205212062120721208212092121021211212122121321214212152121621217212182121921220212212122221223212242122521226212272122821229212302123121232212332123421235212362123721238212392124021241212422124321244212452124621247212482124921250212512125221253212542125521256212572125821259212602126121262212632126421265212662126721268212692127021271212722127321274212752127621277212782127921280212812128221283212842128521286212872128821289212902129121292212932129421295212962129721298212992130021301213022130321304213052130621307213082130921310213112131221313213142131521316213172131821319213202132121322213232132421325213262132721328213292133021331213322133321334213352133621337213382133921340213412134221343213442134521346213472134821349213502135121352213532135421355213562135721358213592136021361213622136321364213652136621367213682136921370213712137221373213742137521376213772137821379213802138121382213832138421385213862138721388213892139021391213922139321394213952139621397213982139921400214012140221403214042140521406214072140821409214102141121412214132141421415214162141721418214192142021421214222142321424214252142621427214282142921430214312143221433214342143521436214372143821439214402144121442214432144421445214462144721448214492145021451214522145321454214552145621457214582145921460214612146221463214642146521466214672146821469214702147121472214732147421475214762147721478214792148021481214822148321484214852148621487214882148921490214912149221493214942149521496214972149821499215002150121502215032150421505215062150721508215092151021511215122151321514215152151621517215182151921520215212152221523215242152521526215272152821529215302153121532215332153421535215362153721538215392154021541215422154321544215452154621547215482154921550215512155221553215542155521556215572155821559215602156121562215632156421565215662156721568215692157021571215722157321574215752157621577215782157921580215812158221583215842158521586215872158821589215902159121592215932159421595215962159721598215992160021601216022160321604216052160621607216082160921610216112161221613216142161521616216172161821619216202162121622216232162421625216262162721628216292163021631216322163321634216352163621637216382163921640216412164221643216442164521646216472164821649216502165121652216532165421655216562165721658216592166021661216622166321664216652166621667216682166921670216712167221673216742167521676216772167821679216802168121682216832168421685216862168721688216892169021691216922169321694216952169621697216982169921700217012170221703217042170521706217072170821709217102171121712217132171421715217162171721718217192172021721217222172321724217252172621727217282172921730217312173221733217342173521736217372173821739217402174121742217432174421745217462174721748217492175021751217522175321754217552175621757217582175921760217612176221763217642176521766217672176821769217702177121772217732177421775217762177721778217792178021781217822178321784217852178621787217882178921790217912179221793217942179521796217972179821799218002180121802218032180421805218062180721808218092181021811218122181321814218152181621817218182181921820218212182221823218242182521826218272182821829218302183121832218332183421835218362183721838218392184021841218422184321844218452184621847218482184921850218512185221853218542185521856218572185821859218602186121862218632186421865218662186721868218692187021871218722187321874218752187621877218782187921880218812188221883218842188521886218872188821889218902189121892218932189421895218962189721898218992190021901219022190321904219052190621907219082190921910219112191221913219142191521916219172191821919219202192121922219232192421925219262192721928219292193021931219322193321934219352193621937219382193921940219412194221943219442194521946219472194821949219502195121952219532195421955219562195721958219592196021961219622196321964219652196621967219682196921970219712197221973219742197521976219772197821979219802198121982219832198421985219862198721988219892199021991219922199321994219952199621997219982199922000220012200222003220042200522006220072200822009220102201122012220132201422015220162201722018220192202022021220222202322024220252202622027220282202922030220312203222033220342203522036220372203822039220402204122042220432204422045220462204722048220492205022051220522205322054220552205622057220582205922060220612206222063220642206522066220672206822069220702207122072220732207422075220762207722078220792208022081220822208322084220852208622087220882208922090220912209222093220942209522096220972209822099221002210122102221032210422105221062210722108221092211022111221122211322114221152211622117221182211922120221212212222123221242212522126221272212822129221302213122132221332213422135221362213722138221392214022141221422214322144221452214622147221482214922150221512215222153221542215522156221572215822159221602216122162221632216422165221662216722168221692217022171221722217322174221752217622177221782217922180221812218222183221842218522186221872218822189221902219122192221932219422195221962219722198221992220022201222022220322204222052220622207222082220922210222112221222213222142221522216222172221822219222202222122222222232222422225222262222722228222292223022231222322223322234222352223622237222382223922240222412224222243222442224522246222472224822249222502225122252222532225422255222562225722258222592226022261222622226322264222652226622267222682226922270222712227222273222742227522276222772227822279222802228122282222832228422285222862228722288222892229022291222922229322294222952229622297222982229922300223012230222303223042230522306223072230822309223102231122312223132231422315223162231722318223192232022321223222232322324223252232622327223282232922330223312233222333223342233522336223372233822339223402234122342223432234422345223462234722348223492235022351223522235322354223552235622357223582235922360223612236222363223642236522366223672236822369223702237122372223732237422375223762237722378223792238022381223822238322384223852238622387223882238922390223912239222393223942239522396223972239822399224002240122402224032240422405224062240722408224092241022411224122241322414224152241622417224182241922420224212242222423224242242522426224272242822429224302243122432224332243422435224362243722438224392244022441224422244322444224452244622447224482244922450224512245222453224542245522456224572245822459224602246122462224632246422465224662246722468224692247022471224722247322474224752247622477224782247922480224812248222483224842248522486224872248822489224902249122492224932249422495224962249722498224992250022501225022250322504225052250622507225082250922510225112251222513225142251522516225172251822519225202252122522225232252422525225262252722528225292253022531225322253322534225352253622537225382253922540225412254222543225442254522546225472254822549225502255122552225532255422555225562255722558225592256022561225622256322564225652256622567225682256922570225712257222573225742257522576225772257822579225802258122582225832258422585225862258722588225892259022591225922259322594225952259622597225982259922600226012260222603226042260522606226072260822609226102261122612226132261422615226162261722618226192262022621226222262322624226252262622627226282262922630226312263222633226342263522636226372263822639226402264122642226432264422645226462264722648226492265022651226522265322654226552265622657226582265922660226612266222663226642266522666226672266822669226702267122672226732267422675226762267722678226792268022681226822268322684226852268622687226882268922690226912269222693226942269522696226972269822699227002270122702227032270422705227062270722708227092271022711227122271322714227152271622717227182271922720227212272222723227242272522726227272272822729227302273122732227332273422735227362273722738227392274022741227422274322744227452274622747227482274922750227512275222753227542275522756227572275822759227602276122762227632276422765227662276722768227692277022771227722277322774227752277622777227782277922780227812278222783227842278522786227872278822789227902279122792227932279422795227962279722798227992280022801228022280322804228052280622807228082280922810228112281222813228142281522816228172281822819228202282122822228232282422825228262282722828228292283022831228322283322834228352283622837228382283922840228412284222843228442284522846228472284822849228502285122852228532285422855228562285722858228592286022861228622286322864228652286622867228682286922870228712287222873228742287522876228772287822879228802288122882228832288422885228862288722888228892289022891228922289322894228952289622897228982289922900229012290222903229042290522906229072290822909229102291122912229132291422915229162291722918229192292022921229222292322924229252292622927229282292922930229312293222933229342293522936229372293822939229402294122942229432294422945229462294722948229492295022951229522295322954229552295622957229582295922960229612296222963229642296522966229672296822969229702297122972229732297422975229762297722978229792298022981229822298322984229852298622987229882298922990229912299222993229942299522996229972299822999230002300123002230032300423005230062300723008230092301023011230122301323014230152301623017230182301923020230212302223023230242302523026230272302823029230302303123032230332303423035230362303723038230392304023041230422304323044230452304623047230482304923050230512305223053230542305523056230572305823059230602306123062230632306423065230662306723068230692307023071230722307323074230752307623077230782307923080230812308223083230842308523086230872308823089230902309123092230932309423095230962309723098230992310023101231022310323104231052310623107231082310923110231112311223113231142311523116231172311823119231202312123122231232312423125231262312723128231292313023131231322313323134231352313623137231382313923140231412314223143231442314523146231472314823149231502315123152231532315423155231562315723158231592316023161231622316323164231652316623167231682316923170231712317223173231742317523176231772317823179231802318123182231832318423185231862318723188231892319023191231922319323194231952319623197231982319923200232012320223203232042320523206232072320823209232102321123212232132321423215232162321723218232192322023221232222322323224232252322623227232282322923230232312323223233232342323523236232372323823239232402324123242232432324423245232462324723248232492325023251232522325323254232552325623257232582325923260232612326223263232642326523266232672326823269232702327123272232732327423275232762327723278232792328023281232822328323284232852328623287232882328923290232912329223293232942329523296232972329823299233002330123302233032330423305233062330723308233092331023311233122331323314233152331623317233182331923320233212332223323233242332523326233272332823329233302333123332233332333423335233362333723338233392334023341233422334323344233452334623347233482334923350233512335223353233542335523356233572335823359233602336123362233632336423365233662336723368233692337023371233722337323374233752337623377233782337923380233812338223383233842338523386233872338823389233902339123392233932339423395233962339723398233992340023401234022340323404234052340623407234082340923410234112341223413234142341523416234172341823419234202342123422234232342423425234262342723428234292343023431234322343323434234352343623437234382343923440234412344223443234442344523446234472344823449234502345123452234532345423455234562345723458234592346023461234622346323464234652346623467234682346923470234712347223473234742347523476234772347823479234802348123482234832348423485234862348723488234892349023491234922349323494234952349623497234982349923500235012350223503235042350523506235072350823509235102351123512235132351423515235162351723518235192352023521235222352323524235252352623527235282352923530235312353223533235342353523536235372353823539235402354123542235432354423545235462354723548235492355023551235522355323554235552355623557235582355923560235612356223563235642356523566235672356823569235702357123572235732357423575235762357723578235792358023581235822358323584235852358623587235882358923590235912359223593235942359523596235972359823599236002360123602236032360423605236062360723608236092361023611236122361323614236152361623617236182361923620236212362223623236242362523626236272362823629236302363123632236332363423635236362363723638236392364023641236422364323644236452364623647236482364923650236512365223653236542365523656236572365823659236602366123662236632366423665236662366723668236692367023671236722367323674236752367623677236782367923680236812368223683236842368523686236872368823689236902369123692236932369423695236962369723698236992370023701237022370323704237052370623707237082370923710237112371223713237142371523716237172371823719237202372123722237232372423725237262372723728237292373023731237322373323734237352373623737237382373923740237412374223743237442374523746237472374823749237502375123752237532375423755237562375723758237592376023761237622376323764237652376623767237682376923770237712377223773237742377523776237772377823779237802378123782237832378423785237862378723788237892379023791237922379323794237952379623797237982379923800238012380223803238042380523806238072380823809238102381123812238132381423815238162381723818238192382023821238222382323824238252382623827238282382923830238312383223833238342383523836238372383823839238402384123842238432384423845238462384723848238492385023851238522385323854238552385623857238582385923860238612386223863238642386523866238672386823869238702387123872238732387423875238762387723878238792388023881238822388323884238852388623887238882388923890238912389223893238942389523896238972389823899239002390123902239032390423905239062390723908239092391023911239122391323914239152391623917239182391923920239212392223923239242392523926239272392823929239302393123932239332393423935239362393723938239392394023941239422394323944239452394623947239482394923950239512395223953239542395523956239572395823959239602396123962239632396423965239662396723968239692397023971239722397323974239752397623977239782397923980239812398223983239842398523986239872398823989239902399123992239932399423995239962399723998239992400024001240022400324004240052400624007240082400924010240112401224013240142401524016240172401824019240202402124022240232402424025240262402724028240292403024031240322403324034240352403624037240382403924040240412404224043240442404524046240472404824049240502405124052240532405424055240562405724058240592406024061240622406324064240652406624067240682406924070240712407224073240742407524076240772407824079240802408124082240832408424085240862408724088240892409024091240922409324094240952409624097240982409924100241012410224103241042410524106241072410824109241102411124112241132411424115241162411724118241192412024121241222412324124241252412624127241282412924130241312413224133241342413524136241372413824139241402414124142241432414424145241462414724148241492415024151241522415324154241552415624157241582415924160241612416224163241642416524166241672416824169241702417124172241732417424175241762417724178241792418024181241822418324184241852418624187241882418924190241912419224193241942419524196241972419824199242002420124202242032420424205242062420724208242092421024211242122421324214242152421624217242182421924220242212422224223242242422524226242272422824229242302423124232242332423424235242362423724238242392424024241242422424324244242452424624247242482424924250242512425224253242542425524256242572425824259242602426124262242632426424265242662426724268242692427024271242722427324274242752427624277242782427924280242812428224283242842428524286242872428824289242902429124292242932429424295242962429724298242992430024301243022430324304243052430624307243082430924310243112431224313243142431524316243172431824319243202432124322243232432424325243262432724328243292433024331243322433324334243352433624337243382433924340243412434224343243442434524346243472434824349243502435124352243532435424355243562435724358243592436024361243622436324364243652436624367243682436924370243712437224373243742437524376243772437824379243802438124382243832438424385243862438724388243892439024391243922439324394243952439624397243982439924400244012440224403244042440524406244072440824409244102441124412244132441424415244162441724418244192442024421244222442324424244252442624427244282442924430244312443224433244342443524436244372443824439244402444124442244432444424445244462444724448244492445024451244522445324454244552445624457244582445924460244612446224463244642446524466244672446824469244702447124472244732447424475244762447724478244792448024481244822448324484244852448624487244882448924490244912449224493244942449524496244972449824499245002450124502245032450424505245062450724508245092451024511245122451324514245152451624517245182451924520245212452224523245242452524526245272452824529245302453124532245332453424535245362453724538245392454024541245422454324544245452454624547245482454924550245512455224553245542455524556245572455824559245602456124562245632456424565245662456724568245692457024571245722457324574245752457624577245782457924580245812458224583245842458524586245872458824589245902459124592245932459424595245962459724598245992460024601246022460324604246052460624607246082460924610246112461224613246142461524616246172461824619246202462124622246232462424625246262462724628246292463024631246322463324634246352463624637246382463924640246412464224643246442464524646246472464824649246502465124652246532465424655246562465724658246592466024661246622466324664246652466624667246682466924670246712467224673246742467524676246772467824679246802468124682246832468424685246862468724688246892469024691246922469324694246952469624697246982469924700247012470224703247042470524706247072470824709247102471124712247132471424715247162471724718247192472024721247222472324724247252472624727247282472924730247312473224733247342473524736247372473824739247402474124742247432474424745247462474724748247492475024751247522475324754247552475624757247582475924760247612476224763247642476524766247672476824769247702477124772247732477424775247762477724778247792478024781247822478324784247852478624787247882478924790247912479224793247942479524796247972479824799248002480124802248032480424805248062480724808248092481024811248122481324814248152481624817248182481924820248212482224823248242482524826248272482824829248302483124832248332483424835248362483724838248392484024841248422484324844248452484624847248482484924850248512485224853248542485524856248572485824859248602486124862248632486424865248662486724868248692487024871248722487324874248752487624877248782487924880248812488224883248842488524886248872488824889248902489124892248932489424895248962489724898248992490024901249022490324904249052490624907249082490924910249112491224913249142491524916249172491824919249202492124922249232492424925249262492724928249292493024931249322493324934249352493624937249382493924940249412494224943249442494524946249472494824949249502495124952249532495424955249562495724958249592496024961249622496324964249652496624967249682496924970249712497224973249742497524976249772497824979249802498124982249832498424985249862498724988249892499024991249922499324994249952499624997249982499925000250012500225003250042500525006250072500825009250102501125012250132501425015250162501725018250192502025021250222502325024250252502625027250282502925030250312503225033250342503525036250372503825039250402504125042250432504425045250462504725048250492505025051250522505325054250552505625057250582505925060250612506225063250642506525066250672506825069250702507125072250732507425075250762507725078250792508025081250822508325084250852508625087250882508925090250912509225093250942509525096250972509825099251002510125102251032510425105251062510725108251092511025111251122511325114251152511625117251182511925120251212512225123251242512525126251272512825129251302513125132251332513425135251362513725138251392514025141251422514325144251452514625147251482514925150251512515225153251542515525156251572515825159251602516125162251632516425165251662516725168251692517025171251722517325174251752517625177251782517925180251812518225183251842518525186251872518825189251902519125192251932519425195251962519725198251992520025201252022520325204252052520625207252082520925210252112521225213252142521525216252172521825219252202522125222252232522425225252262522725228252292523025231252322523325234252352523625237252382523925240252412524225243252442524525246252472524825249252502525125252252532525425255252562525725258252592526025261252622526325264252652526625267252682526925270252712527225273252742527525276252772527825279252802528125282252832528425285252862528725288252892529025291252922529325294252952529625297252982529925300253012530225303253042530525306253072530825309253102531125312253132531425315253162531725318253192532025321253222532325324253252532625327253282532925330253312533225333253342533525336253372533825339253402534125342253432534425345253462534725348253492535025351253522535325354253552535625357253582535925360253612536225363253642536525366253672536825369253702537125372253732537425375253762537725378253792538025381253822538325384253852538625387253882538925390253912539225393253942539525396253972539825399254002540125402254032540425405254062540725408254092541025411254122541325414254152541625417254182541925420254212542225423254242542525426254272542825429254302543125432254332543425435254362543725438254392544025441254422544325444254452544625447254482544925450254512545225453254542545525456254572545825459254602546125462254632546425465254662546725468254692547025471254722547325474254752547625477254782547925480254812548225483254842548525486254872548825489254902549125492254932549425495254962549725498254992550025501255022550325504255052550625507255082550925510255112551225513255142551525516255172551825519255202552125522255232552425525255262552725528255292553025531255322553325534255352553625537255382553925540255412554225543255442554525546255472554825549255502555125552255532555425555255562555725558255592556025561255622556325564255652556625567255682556925570255712557225573255742557525576255772557825579255802558125582255832558425585255862558725588255892559025591255922559325594255952559625597255982559925600256012560225603256042560525606256072560825609256102561125612256132561425615256162561725618256192562025621256222562325624256252562625627256282562925630256312563225633256342563525636256372563825639256402564125642256432564425645256462564725648256492565025651256522565325654256552565625657256582565925660256612566225663256642566525666256672566825669256702567125672256732567425675256762567725678256792568025681256822568325684256852568625687256882568925690256912569225693256942569525696256972569825699257002570125702257032570425705257062570725708257092571025711257122571325714257152571625717257182571925720257212572225723257242572525726257272572825729257302573125732257332573425735257362573725738257392574025741257422574325744257452574625747257482574925750257512575225753257542575525756257572575825759257602576125762257632576425765257662576725768257692577025771257722577325774257752577625777257782577925780257812578225783257842578525786257872578825789257902579125792257932579425795257962579725798257992580025801258022580325804258052580625807258082580925810258112581225813258142581525816258172581825819258202582125822258232582425825258262582725828258292583025831258322583325834258352583625837258382583925840258412584225843258442584525846258472584825849258502585125852258532585425855258562585725858258592586025861258622586325864258652586625867258682586925870258712587225873258742587525876258772587825879258802588125882258832588425885258862588725888258892589025891258922589325894258952589625897258982589925900259012590225903259042590525906259072590825909259102591125912259132591425915259162591725918259192592025921259222592325924259252592625927259282592925930259312593225933259342593525936259372593825939259402594125942259432594425945259462594725948259492595025951259522595325954259552595625957259582595925960259612596225963259642596525966259672596825969259702597125972259732597425975259762597725978259792598025981259822598325984259852598625987259882598925990259912599225993259942599525996259972599825999260002600126002260032600426005260062600726008260092601026011260122601326014260152601626017260182601926020260212602226023260242602526026260272602826029260302603126032260332603426035260362603726038260392604026041260422604326044260452604626047260482604926050260512605226053260542605526056260572605826059260602606126062260632606426065260662606726068260692607026071260722607326074260752607626077260782607926080260812608226083260842608526086260872608826089260902609126092260932609426095260962609726098260992610026101261022610326104261052610626107261082610926110261112611226113261142611526116261172611826119261202612126122261232612426125261262612726128261292613026131261322613326134261352613626137261382613926140261412614226143261442614526146261472614826149261502615126152261532615426155261562615726158261592616026161261622616326164261652616626167261682616926170261712617226173261742617526176261772617826179261802618126182261832618426185261862618726188261892619026191261922619326194261952619626197261982619926200262012620226203262042620526206262072620826209262102621126212262132621426215262162621726218262192622026221262222622326224262252622626227262282622926230262312623226233262342623526236262372623826239262402624126242262432624426245262462624726248262492625026251262522625326254262552625626257262582625926260262612626226263262642626526266262672626826269262702627126272262732627426275262762627726278262792628026281262822628326284262852628626287262882628926290262912629226293262942629526296262972629826299263002630126302263032630426305263062630726308263092631026311263122631326314263152631626317263182631926320263212632226323263242632526326263272632826329263302633126332263332633426335263362633726338263392634026341263422634326344263452634626347263482634926350263512635226353263542635526356263572635826359263602636126362263632636426365263662636726368263692637026371263722637326374263752637626377263782637926380263812638226383263842638526386263872638826389263902639126392263932639426395263962639726398263992640026401264022640326404264052640626407264082640926410264112641226413264142641526416264172641826419264202642126422264232642426425264262642726428264292643026431264322643326434264352643626437264382643926440264412644226443264442644526446264472644826449264502645126452264532645426455264562645726458264592646026461264622646326464264652646626467264682646926470264712647226473264742647526476264772647826479264802648126482264832648426485264862648726488264892649026491264922649326494264952649626497264982649926500265012650226503265042650526506265072650826509265102651126512265132651426515265162651726518265192652026521265222652326524265252652626527265282652926530265312653226533265342653526536265372653826539265402654126542265432654426545265462654726548265492655026551265522655326554265552655626557265582655926560265612656226563265642656526566265672656826569265702657126572265732657426575265762657726578265792658026581265822658326584265852658626587265882658926590265912659226593265942659526596265972659826599266002660126602266032660426605266062660726608266092661026611266122661326614266152661626617266182661926620266212662226623266242662526626266272662826629266302663126632266332663426635266362663726638266392664026641266422664326644266452664626647266482664926650266512665226653266542665526656266572665826659266602666126662266632666426665266662666726668266692667026671266722667326674266752667626677266782667926680266812668226683266842668526686266872668826689266902669126692266932669426695266962669726698266992670026701267022670326704267052670626707267082670926710267112671226713267142671526716267172671826719267202672126722267232672426725267262672726728267292673026731267322673326734267352673626737267382673926740267412674226743267442674526746267472674826749267502675126752267532675426755267562675726758267592676026761267622676326764267652676626767267682676926770267712677226773267742677526776267772677826779267802678126782267832678426785267862678726788267892679026791267922679326794267952679626797267982679926800268012680226803268042680526806268072680826809268102681126812268132681426815268162681726818268192682026821268222682326824268252682626827268282682926830268312683226833268342683526836268372683826839268402684126842268432684426845268462684726848268492685026851268522685326854268552685626857268582685926860268612686226863268642686526866268672686826869268702687126872268732687426875268762687726878268792688026881268822688326884268852688626887268882688926890268912689226893268942689526896268972689826899269002690126902269032690426905269062690726908269092691026911269122691326914269152691626917269182691926920269212692226923269242692526926269272692826929269302693126932269332693426935269362693726938269392694026941269422694326944269452694626947269482694926950269512695226953269542695526956269572695826959269602696126962269632696426965269662696726968269692697026971269722697326974269752697626977269782697926980269812698226983269842698526986269872698826989269902699126992269932699426995269962699726998269992700027001270022700327004270052700627007270082700927010270112701227013270142701527016270172701827019270202702127022270232702427025270262702727028270292703027031270322703327034270352703627037270382703927040270412704227043270442704527046270472704827049270502705127052270532705427055270562705727058270592706027061270622706327064270652706627067270682706927070270712707227073270742707527076270772707827079270802708127082270832708427085270862708727088270892709027091270922709327094270952709627097270982709927100271012710227103271042710527106271072710827109271102711127112271132711427115271162711727118271192712027121271222712327124271252712627127271282712927130271312713227133271342713527136271372713827139271402714127142271432714427145271462714727148271492715027151271522715327154271552715627157271582715927160271612716227163271642716527166271672716827169271702717127172271732717427175271762717727178271792718027181271822718327184271852718627187271882718927190271912719227193271942719527196271972719827199272002720127202272032720427205272062720727208272092721027211272122721327214272152721627217272182721927220272212722227223272242722527226272272722827229272302723127232272332723427235272362723727238272392724027241272422724327244272452724627247272482724927250272512725227253272542725527256272572725827259272602726127262272632726427265272662726727268272692727027271272722727327274272752727627277272782727927280272812728227283272842728527286272872728827289272902729127292272932729427295272962729727298272992730027301273022730327304273052730627307273082730927310273112731227313273142731527316273172731827319273202732127322273232732427325273262732727328273292733027331273322733327334273352733627337273382733927340273412734227343273442734527346273472734827349273502735127352273532735427355273562735727358273592736027361273622736327364273652736627367273682736927370273712737227373273742737527376273772737827379273802738127382273832738427385273862738727388273892739027391273922739327394273952739627397273982739927400274012740227403274042740527406274072740827409274102741127412274132741427415274162741727418274192742027421274222742327424274252742627427274282742927430274312743227433274342743527436274372743827439274402744127442274432744427445274462744727448274492745027451274522745327454274552745627457274582745927460274612746227463274642746527466274672746827469274702747127472274732747427475274762747727478274792748027481274822748327484274852748627487274882748927490274912749227493274942749527496274972749827499275002750127502275032750427505275062750727508275092751027511275122751327514275152751627517275182751927520275212752227523275242752527526275272752827529275302753127532275332753427535275362753727538275392754027541275422754327544275452754627547275482754927550275512755227553275542755527556275572755827559275602756127562275632756427565275662756727568275692757027571275722757327574275752757627577275782757927580275812758227583275842758527586275872758827589275902759127592275932759427595275962759727598275992760027601276022760327604276052760627607276082760927610276112761227613276142761527616276172761827619276202762127622276232762427625276262762727628276292763027631276322763327634276352763627637276382763927640276412764227643276442764527646276472764827649276502765127652276532765427655276562765727658276592766027661276622766327664276652766627667276682766927670276712767227673276742767527676276772767827679276802768127682276832768427685276862768727688276892769027691276922769327694276952769627697276982769927700277012770227703277042770527706277072770827709277102771127712277132771427715277162771727718277192772027721277222772327724277252772627727277282772927730277312773227733277342773527736277372773827739277402774127742277432774427745277462774727748277492775027751277522775327754277552775627757277582775927760277612776227763277642776527766277672776827769277702777127772277732777427775277762777727778277792778027781277822778327784277852778627787277882778927790277912779227793277942779527796277972779827799278002780127802278032780427805278062780727808278092781027811278122781327814278152781627817278182781927820278212782227823278242782527826278272782827829278302783127832278332783427835278362783727838278392784027841278422784327844278452784627847278482784927850278512785227853278542785527856278572785827859278602786127862278632786427865278662786727868278692787027871278722787327874278752787627877278782787927880278812788227883278842788527886278872788827889278902789127892278932789427895278962789727898278992790027901279022790327904279052790627907279082790927910279112791227913279142791527916279172791827919279202792127922279232792427925279262792727928279292793027931279322793327934279352793627937279382793927940279412794227943279442794527946279472794827949279502795127952279532795427955279562795727958279592796027961279622796327964279652796627967279682796927970279712797227973279742797527976279772797827979279802798127982279832798427985279862798727988279892799027991279922799327994279952799627997279982799928000280012800228003280042800528006280072800828009280102801128012280132801428015280162801728018280192802028021280222802328024280252802628027280282802928030280312803228033280342803528036280372803828039280402804128042280432804428045280462804728048280492805028051280522805328054280552805628057280582805928060280612806228063280642806528066280672806828069280702807128072280732807428075280762807728078280792808028081280822808328084280852808628087280882808928090280912809228093280942809528096280972809828099281002810128102281032810428105281062810728108281092811028111281122811328114281152811628117281182811928120281212812228123281242812528126281272812828129281302813128132281332813428135281362813728138281392814028141281422814328144281452814628147281482814928150281512815228153281542815528156281572815828159281602816128162281632816428165281662816728168281692817028171281722817328174281752817628177281782817928180281812818228183281842818528186281872818828189281902819128192281932819428195281962819728198281992820028201282022820328204282052820628207282082820928210282112821228213282142821528216282172821828219282202822128222282232822428225282262822728228282292823028231282322823328234282352823628237282382823928240282412824228243282442824528246282472824828249282502825128252282532825428255282562825728258282592826028261282622826328264282652826628267282682826928270282712827228273282742827528276282772827828279282802828128282282832828428285282862828728288282892829028291282922829328294282952829628297282982829928300283012830228303283042830528306283072830828309283102831128312283132831428315283162831728318283192832028321283222832328324283252832628327283282832928330283312833228333283342833528336283372833828339283402834128342283432834428345283462834728348283492835028351283522835328354283552835628357283582835928360283612836228363283642836528366283672836828369283702837128372283732837428375283762837728378283792838028381283822838328384283852838628387283882838928390283912839228393283942839528396283972839828399284002840128402284032840428405284062840728408284092841028411284122841328414284152841628417284182841928420284212842228423284242842528426284272842828429284302843128432284332843428435284362843728438284392844028441284422844328444284452844628447284482844928450284512845228453284542845528456284572845828459284602846128462284632846428465284662846728468284692847028471284722847328474284752847628477284782847928480284812848228483284842848528486284872848828489284902849128492284932849428495284962849728498284992850028501285022850328504285052850628507285082850928510285112851228513285142851528516285172851828519285202852128522285232852428525285262852728528285292853028531285322853328534285352853628537285382853928540285412854228543285442854528546285472854828549285502855128552285532855428555285562855728558285592856028561285622856328564285652856628567285682856928570285712857228573285742857528576285772857828579285802858128582285832858428585285862858728588285892859028591285922859328594285952859628597285982859928600286012860228603286042860528606286072860828609286102861128612286132861428615286162861728618286192862028621286222862328624286252862628627286282862928630286312863228633286342863528636286372863828639286402864128642286432864428645286462864728648286492865028651286522865328654286552865628657286582865928660286612866228663286642866528666286672866828669286702867128672286732867428675286762867728678286792868028681286822868328684286852868628687286882868928690286912869228693286942869528696286972869828699287002870128702287032870428705287062870728708287092871028711287122871328714287152871628717287182871928720287212872228723287242872528726287272872828729287302873128732287332873428735287362873728738287392874028741287422874328744287452874628747287482874928750287512875228753287542875528756287572875828759287602876128762287632876428765287662876728768287692877028771287722877328774287752877628777287782877928780287812878228783287842878528786287872878828789287902879128792287932879428795287962879728798287992880028801288022880328804288052880628807288082880928810288112881228813288142881528816288172881828819288202882128822288232882428825288262882728828288292883028831288322883328834288352883628837288382883928840288412884228843288442884528846288472884828849288502885128852288532885428855288562885728858288592886028861288622886328864288652886628867288682886928870288712887228873288742887528876288772887828879288802888128882288832888428885288862888728888288892889028891288922889328894288952889628897288982889928900289012890228903289042890528906289072890828909289102891128912289132891428915289162891728918289192892028921289222892328924289252892628927289282892928930289312893228933289342893528936289372893828939289402894128942289432894428945289462894728948289492895028951289522895328954289552895628957289582895928960289612896228963289642896528966289672896828969289702897128972289732897428975289762897728978289792898028981289822898328984289852898628987289882898928990289912899228993289942899528996289972899828999290002900129002290032900429005290062900729008290092901029011290122901329014290152901629017290182901929020290212902229023290242902529026290272902829029290302903129032290332903429035290362903729038290392904029041290422904329044290452904629047290482904929050290512905229053290542905529056290572905829059290602906129062290632906429065290662906729068290692907029071290722907329074290752907629077290782907929080290812908229083290842908529086290872908829089290902909129092290932909429095290962909729098290992910029101291022910329104291052910629107291082910929110291112911229113291142911529116291172911829119291202912129122291232912429125291262912729128291292913029131291322913329134291352913629137291382913929140291412914229143291442914529146291472914829149291502915129152291532915429155291562915729158291592916029161291622916329164291652916629167291682916929170291712917229173291742917529176291772917829179291802918129182291832918429185291862918729188291892919029191291922919329194291952919629197291982919929200292012920229203292042920529206292072920829209292102921129212292132921429215292162921729218292192922029221292222922329224292252922629227292282922929230292312923229233292342923529236292372923829239292402924129242292432924429245292462924729248292492925029251292522925329254292552925629257292582925929260292612926229263292642926529266292672926829269292702927129272292732927429275292762927729278292792928029281292822928329284292852928629287292882928929290292912929229293292942929529296292972929829299293002930129302293032930429305293062930729308293092931029311293122931329314293152931629317293182931929320293212932229323293242932529326293272932829329293302933129332293332933429335293362933729338293392934029341293422934329344293452934629347293482934929350293512935229353293542935529356293572935829359293602936129362293632936429365293662936729368293692937029371293722937329374293752937629377293782937929380293812938229383293842938529386293872938829389293902939129392293932939429395293962939729398293992940029401294022940329404294052940629407294082940929410294112941229413294142941529416294172941829419294202942129422294232942429425294262942729428294292943029431294322943329434294352943629437294382943929440294412944229443294442944529446294472944829449294502945129452294532945429455294562945729458294592946029461294622946329464294652946629467294682946929470294712947229473294742947529476294772947829479294802948129482294832948429485294862948729488294892949029491294922949329494294952949629497294982949929500295012950229503295042950529506295072950829509295102951129512295132951429515295162951729518295192952029521295222952329524295252952629527295282952929530295312953229533295342953529536295372953829539295402954129542295432954429545295462954729548295492955029551295522955329554295552955629557295582955929560295612956229563295642956529566295672956829569295702957129572295732957429575295762957729578295792958029581295822958329584295852958629587295882958929590295912959229593295942959529596295972959829599296002960129602296032960429605296062960729608296092961029611296122961329614296152961629617296182961929620296212962229623296242962529626296272962829629296302963129632296332963429635296362963729638296392964029641296422964329644296452964629647296482964929650296512965229653296542965529656296572965829659296602966129662296632966429665296662966729668296692967029671296722967329674296752967629677296782967929680296812968229683296842968529686296872968829689296902969129692296932969429695296962969729698296992970029701297022970329704297052970629707297082970929710297112971229713297142971529716297172971829719297202972129722297232972429725297262972729728297292973029731297322973329734297352973629737297382973929740297412974229743297442974529746297472974829749297502975129752297532975429755297562975729758297592976029761297622976329764297652976629767297682976929770297712977229773297742977529776297772977829779297802978129782297832978429785297862978729788297892979029791297922979329794297952979629797297982979929800298012980229803298042980529806298072980829809298102981129812298132981429815298162981729818298192982029821298222982329824298252982629827298282982929830298312983229833298342983529836298372983829839298402984129842298432984429845298462984729848298492985029851298522985329854298552985629857298582985929860298612986229863298642986529866298672986829869298702987129872298732987429875298762987729878298792988029881298822988329884298852988629887298882988929890298912989229893298942989529896298972989829899299002990129902299032990429905299062990729908299092991029911299122991329914299152991629917299182991929920299212992229923299242992529926299272992829929299302993129932299332993429935299362993729938299392994029941299422994329944299452994629947299482994929950299512995229953299542995529956299572995829959299602996129962299632996429965299662996729968299692997029971299722997329974299752997629977299782997929980299812998229983299842998529986299872998829989299902999129992299932999429995299962999729998299993000030001300023000330004300053000630007300083000930010300113001230013300143001530016300173001830019300203002130022300233002430025300263002730028300293003030031300323003330034300353003630037300383003930040300413004230043300443004530046300473004830049300503005130052300533005430055300563005730058300593006030061300623006330064300653006630067300683006930070300713007230073300743007530076300773007830079300803008130082300833008430085300863008730088300893009030091300923009330094300953009630097300983009930100301013010230103301043010530106301073010830109301103011130112301133011430115301163011730118301193012030121301223012330124301253012630127301283012930130301313013230133301343013530136301373013830139301403014130142301433014430145301463014730148301493015030151301523015330154301553015630157301583015930160301613016230163301643016530166301673016830169301703017130172301733017430175301763017730178301793018030181301823018330184301853018630187301883018930190301913019230193301943019530196301973019830199302003020130202302033020430205302063020730208302093021030211302123021330214302153021630217302183021930220302213022230223302243022530226302273022830229302303023130232302333023430235302363023730238302393024030241302423024330244302453024630247302483024930250302513025230253302543025530256302573025830259302603026130262302633026430265302663026730268302693027030271302723027330274302753027630277302783027930280302813028230283302843028530286302873028830289302903029130292302933029430295302963029730298302993030030301303023030330304303053030630307303083030930310303113031230313303143031530316303173031830319303203032130322303233032430325303263032730328303293033030331303323033330334303353033630337303383033930340303413034230343303443034530346303473034830349303503035130352303533035430355303563035730358303593036030361303623036330364303653036630367303683036930370303713037230373303743037530376303773037830379303803038130382303833038430385303863038730388303893039030391303923039330394303953039630397303983039930400304013040230403304043040530406304073040830409304103041130412304133041430415304163041730418304193042030421304223042330424304253042630427304283042930430304313043230433304343043530436304373043830439304403044130442304433044430445304463044730448304493045030451304523045330454304553045630457304583045930460304613046230463304643046530466304673046830469304703047130472304733047430475304763047730478304793048030481304823048330484304853048630487304883048930490304913049230493304943049530496304973049830499305003050130502305033050430505305063050730508305093051030511305123051330514305153051630517305183051930520305213052230523305243052530526305273052830529305303053130532305333053430535305363053730538305393054030541305423054330544305453054630547305483054930550305513055230553305543055530556305573055830559305603056130562305633056430565305663056730568305693057030571305723057330574305753057630577305783057930580305813058230583305843058530586305873058830589305903059130592305933059430595305963059730598305993060030601306023060330604306053060630607306083060930610306113061230613306143061530616306173061830619306203062130622306233062430625306263062730628306293063030631306323063330634306353063630637306383063930640306413064230643306443064530646306473064830649306503065130652306533065430655306563065730658306593066030661306623066330664306653066630667306683066930670306713067230673306743067530676306773067830679306803068130682306833068430685306863068730688306893069030691306923069330694306953069630697306983069930700307013070230703307043070530706307073070830709307103071130712307133071430715307163071730718307193072030721307223072330724307253072630727307283072930730307313073230733307343073530736307373073830739307403074130742307433074430745307463074730748307493075030751307523075330754307553075630757307583075930760307613076230763307643076530766307673076830769307703077130772307733077430775307763077730778307793078030781307823078330784307853078630787307883078930790307913079230793307943079530796307973079830799308003080130802308033080430805308063080730808308093081030811308123081330814308153081630817308183081930820308213082230823308243082530826308273082830829308303083130832308333083430835308363083730838308393084030841308423084330844308453084630847308483084930850308513085230853308543085530856308573085830859308603086130862308633086430865308663086730868308693087030871308723087330874308753087630877308783087930880308813088230883308843088530886308873088830889308903089130892308933089430895308963089730898308993090030901309023090330904309053090630907309083090930910309113091230913309143091530916309173091830919309203092130922309233092430925309263092730928309293093030931309323093330934309353093630937309383093930940309413094230943309443094530946309473094830949309503095130952309533095430955309563095730958309593096030961309623096330964309653096630967309683096930970309713097230973309743097530976309773097830979309803098130982309833098430985309863098730988309893099030991309923099330994309953099630997309983099931000310013100231003310043100531006310073100831009310103101131012310133101431015310163101731018310193102031021310223102331024310253102631027310283102931030310313103231033310343103531036310373103831039310403104131042310433104431045310463104731048310493105031051310523105331054310553105631057310583105931060310613106231063310643106531066310673106831069310703107131072310733107431075310763107731078310793108031081310823108331084310853108631087310883108931090310913109231093310943109531096310973109831099311003110131102311033110431105311063110731108311093111031111311123111331114311153111631117311183111931120311213112231123311243112531126311273112831129311303113131132311333113431135311363113731138311393114031141311423114331144311453114631147311483114931150311513115231153311543115531156311573115831159311603116131162311633116431165311663116731168311693117031171311723117331174311753117631177311783117931180311813118231183311843118531186311873118831189311903119131192311933119431195311963119731198311993120031201312023120331204312053120631207312083120931210312113121231213312143121531216312173121831219312203122131222312233122431225312263122731228312293123031231312323123331234312353123631237312383123931240312413124231243312443124531246312473124831249312503125131252312533125431255312563125731258312593126031261312623126331264312653126631267312683126931270312713127231273312743127531276312773127831279312803128131282312833128431285312863128731288312893129031291312923129331294312953129631297312983129931300313013130231303313043130531306313073130831309313103131131312313133131431315313163131731318313193132031321313223132331324313253132631327313283132931330313313133231333313343133531336313373133831339313403134131342313433134431345313463134731348313493135031351313523135331354313553135631357313583135931360313613136231363313643136531366313673136831369313703137131372313733137431375313763137731378313793138031381313823138331384313853138631387313883138931390313913139231393313943139531396313973139831399314003140131402314033140431405314063140731408314093141031411314123141331414314153141631417314183141931420314213142231423314243142531426314273142831429314303143131432314333143431435314363143731438314393144031441314423144331444314453144631447314483144931450314513145231453314543145531456314573145831459314603146131462314633146431465314663146731468314693147031471314723147331474314753147631477314783147931480314813148231483314843148531486314873148831489314903149131492314933149431495314963149731498314993150031501315023150331504315053150631507315083150931510315113151231513315143151531516315173151831519315203152131522315233152431525315263152731528315293153031531315323153331534315353153631537315383153931540315413154231543315443154531546315473154831549315503155131552315533155431555315563155731558315593156031561315623156331564315653156631567315683156931570315713157231573315743157531576315773157831579315803158131582315833158431585315863158731588315893159031591315923159331594315953159631597315983159931600316013160231603316043160531606316073160831609316103161131612316133161431615316163161731618316193162031621316223162331624316253162631627316283162931630316313163231633316343163531636316373163831639316403164131642316433164431645316463164731648316493165031651316523165331654316553165631657316583165931660316613166231663316643166531666316673166831669316703167131672316733167431675316763167731678316793168031681316823168331684316853168631687316883168931690316913169231693316943169531696316973169831699317003170131702317033170431705317063170731708317093171031711317123171331714317153171631717317183171931720317213172231723317243172531726317273172831729317303173131732317333173431735317363173731738317393174031741317423174331744317453174631747317483174931750317513175231753317543175531756317573175831759317603176131762317633176431765317663176731768317693177031771317723177331774317753177631777317783177931780317813178231783317843178531786317873178831789317903179131792317933179431795317963179731798317993180031801318023180331804318053180631807318083180931810318113181231813318143181531816318173181831819318203182131822318233182431825318263182731828318293183031831318323183331834318353183631837318383183931840318413184231843318443184531846318473184831849318503185131852318533185431855318563185731858318593186031861318623186331864318653186631867318683186931870318713187231873318743187531876318773187831879318803188131882318833188431885318863188731888318893189031891318923189331894318953189631897318983189931900319013190231903319043190531906319073190831909319103191131912319133191431915319163191731918319193192031921319223192331924319253192631927319283192931930319313193231933319343193531936319373193831939319403194131942319433194431945319463194731948319493195031951319523195331954319553195631957319583195931960319613196231963319643196531966319673196831969319703197131972319733197431975319763197731978319793198031981319823198331984319853198631987319883198931990319913199231993319943199531996319973199831999320003200132002320033200432005320063200732008320093201032011320123201332014320153201632017320183201932020320213202232023320243202532026320273202832029320303203132032320333203432035320363203732038320393204032041320423204332044320453204632047320483204932050320513205232053320543205532056320573205832059320603206132062320633206432065320663206732068320693207032071320723207332074320753207632077320783207932080320813208232083320843208532086320873208832089320903209132092320933209432095320963209732098320993210032101321023210332104321053210632107321083210932110321113211232113321143211532116321173211832119321203212132122321233212432125321263212732128321293213032131321323213332134321353213632137321383213932140321413214232143321443214532146321473214832149321503215132152321533215432155321563215732158321593216032161321623216332164321653216632167321683216932170321713217232173321743217532176321773217832179321803218132182321833218432185321863218732188321893219032191321923219332194321953219632197321983219932200322013220232203322043220532206322073220832209322103221132212322133221432215322163221732218322193222032221322223222332224322253222632227322283222932230322313223232233322343223532236322373223832239322403224132242322433224432245322463224732248322493225032251322523225332254322553225632257322583225932260322613226232263322643226532266322673226832269322703227132272322733227432275322763227732278322793228032281322823228332284322853228632287322883228932290322913229232293322943229532296322973229832299323003230132302323033230432305323063230732308323093231032311323123231332314323153231632317323183231932320323213232232323323243232532326323273232832329323303233132332323333233432335323363233732338323393234032341323423234332344323453234632347323483234932350323513235232353323543235532356323573235832359323603236132362323633236432365323663236732368323693237032371323723237332374323753237632377323783237932380323813238232383323843238532386323873238832389323903239132392323933239432395323963239732398323993240032401324023240332404324053240632407324083240932410324113241232413324143241532416324173241832419324203242132422324233242432425324263242732428324293243032431324323243332434324353243632437324383243932440324413244232443324443244532446324473244832449324503245132452324533245432455324563245732458324593246032461324623246332464324653246632467324683246932470324713247232473324743247532476324773247832479324803248132482324833248432485324863248732488324893249032491324923249332494324953249632497324983249932500325013250232503325043250532506325073250832509325103251132512325133251432515325163251732518325193252032521325223252332524325253252632527325283252932530325313253232533325343253532536325373253832539325403254132542325433254432545325463254732548325493255032551325523255332554325553255632557325583255932560325613256232563325643256532566325673256832569325703257132572325733257432575325763257732578325793258032581325823258332584325853258632587325883258932590325913259232593325943259532596325973259832599326003260132602326033260432605326063260732608326093261032611326123261332614326153261632617326183261932620326213262232623326243262532626326273262832629326303263132632326333263432635326363263732638326393264032641326423264332644326453264632647326483264932650326513265232653326543265532656326573265832659326603266132662326633266432665326663266732668326693267032671326723267332674326753267632677326783267932680326813268232683326843268532686326873268832689326903269132692326933269432695326963269732698326993270032701327023270332704327053270632707327083270932710327113271232713327143271532716327173271832719327203272132722327233272432725327263272732728327293273032731327323273332734327353273632737327383273932740327413274232743327443274532746327473274832749327503275132752327533275432755327563275732758327593276032761327623276332764327653276632767327683276932770327713277232773327743277532776327773277832779327803278132782327833278432785327863278732788327893279032791327923279332794327953279632797327983279932800328013280232803328043280532806328073280832809328103281132812328133281432815328163281732818328193282032821328223282332824328253282632827328283282932830328313283232833328343283532836328373283832839328403284132842328433284432845328463284732848328493285032851328523285332854328553285632857328583285932860328613286232863328643286532866328673286832869328703287132872328733287432875328763287732878328793288032881328823288332884328853288632887328883288932890328913289232893328943289532896328973289832899329003290132902329033290432905329063290732908329093291032911329123291332914329153291632917329183291932920329213292232923329243292532926329273292832929329303293132932329333293432935329363293732938329393294032941329423294332944329453294632947329483294932950329513295232953329543295532956329573295832959329603296132962329633296432965329663296732968329693297032971329723297332974329753297632977329783297932980329813298232983329843298532986329873298832989329903299132992329933299432995329963299732998329993300033001330023300333004330053300633007330083300933010330113301233013330143301533016330173301833019330203302133022330233302433025330263302733028330293303033031330323303333034330353303633037330383303933040330413304233043330443304533046330473304833049330503305133052330533305433055330563305733058330593306033061330623306333064330653306633067330683306933070330713307233073330743307533076330773307833079330803308133082330833308433085330863308733088330893309033091330923309333094330953309633097330983309933100331013310233103331043310533106331073310833109331103311133112331133311433115331163311733118331193312033121331223312333124331253312633127331283312933130331313313233133331343313533136331373313833139331403314133142331433314433145331463314733148331493315033151331523315333154331553315633157331583315933160331613316233163331643316533166331673316833169331703317133172331733317433175331763317733178331793318033181331823318333184331853318633187331883318933190331913319233193331943319533196331973319833199332003320133202332033320433205332063320733208332093321033211332123321333214332153321633217332183321933220332213322233223332243322533226332273322833229332303323133232332333323433235332363323733238332393324033241332423324333244332453324633247332483324933250332513325233253332543325533256332573325833259332603326133262332633326433265332663326733268332693327033271332723327333274332753327633277332783327933280332813328233283332843328533286332873328833289332903329133292332933329433295332963329733298332993330033301333023330333304333053330633307333083330933310333113331233313333143331533316333173331833319333203332133322333233332433325333263332733328333293333033331333323333333334333353333633337333383333933340333413334233343333443334533346333473334833349333503335133352333533335433355333563335733358333593336033361333623336333364333653336633367333683336933370333713337233373333743337533376333773337833379333803338133382333833338433385333863338733388333893339033391333923339333394333953339633397333983339933400334013340233403334043340533406334073340833409334103341133412334133341433415334163341733418334193342033421334223342333424334253342633427334283342933430334313343233433334343343533436334373343833439334403344133442334433344433445334463344733448334493345033451334523345333454334553345633457334583345933460334613346233463334643346533466334673346833469334703347133472334733347433475334763347733478334793348033481334823348333484334853348633487334883348933490334913349233493334943349533496334973349833499335003350133502335033350433505335063350733508335093351033511335123351333514335153351633517335183351933520335213352233523335243352533526335273352833529335303353133532335333353433535335363353733538335393354033541335423354333544335453354633547335483354933550335513355233553335543355533556335573355833559335603356133562335633356433565335663356733568335693357033571335723357333574335753357633577335783357933580335813358233583335843358533586335873358833589335903359133592335933359433595335963359733598335993360033601336023360333604336053360633607336083360933610336113361233613336143361533616336173361833619336203362133622336233362433625336263362733628336293363033631336323363333634336353363633637336383363933640336413364233643336443364533646336473364833649336503365133652336533365433655336563365733658336593366033661336623366333664336653366633667336683366933670336713367233673336743367533676336773367833679336803368133682336833368433685336863368733688336893369033691336923369333694336953369633697336983369933700337013370233703337043370533706337073370833709337103371133712337133371433715337163371733718337193372033721337223372333724337253372633727337283372933730337313373233733337343373533736337373373833739337403374133742337433374433745337463374733748337493375033751337523375333754337553375633757337583375933760337613376233763337643376533766337673376833769337703377133772337733377433775337763377733778337793378033781337823378333784337853378633787337883378933790337913379233793337943379533796337973379833799338003380133802338033380433805338063380733808338093381033811338123381333814338153381633817338183381933820338213382233823338243382533826338273382833829338303383133832338333383433835338363383733838338393384033841338423384333844338453384633847338483384933850338513385233853338543385533856338573385833859338603386133862338633386433865338663386733868338693387033871338723387333874338753387633877338783387933880338813388233883338843388533886338873388833889338903389133892338933389433895338963389733898338993390033901339023390333904339053390633907339083390933910339113391233913339143391533916339173391833919339203392133922339233392433925339263392733928339293393033931339323393333934339353393633937339383393933940339413394233943339443394533946339473394833949339503395133952339533395433955339563395733958339593396033961339623396333964339653396633967339683396933970339713397233973339743397533976339773397833979339803398133982339833398433985339863398733988339893399033991339923399333994339953399633997339983399934000340013400234003340043400534006340073400834009340103401134012340133401434015340163401734018340193402034021340223402334024340253402634027340283402934030340313403234033340343403534036340373403834039340403404134042340433404434045340463404734048340493405034051340523405334054340553405634057340583405934060340613406234063340643406534066340673406834069340703407134072340733407434075340763407734078340793408034081340823408334084340853408634087340883408934090340913409234093340943409534096340973409834099341003410134102341033410434105341063410734108341093411034111341123411334114341153411634117341183411934120341213412234123341243412534126341273412834129341303413134132341333413434135341363413734138341393414034141341423414334144341453414634147341483414934150341513415234153341543415534156341573415834159341603416134162341633416434165341663416734168341693417034171341723417334174341753417634177341783417934180341813418234183341843418534186341873418834189341903419134192341933419434195341963419734198341993420034201342023420334204342053420634207342083420934210342113421234213342143421534216342173421834219342203422134222342233422434225342263422734228342293423034231342323423334234342353423634237342383423934240342413424234243342443424534246342473424834249342503425134252342533425434255342563425734258342593426034261342623426334264342653426634267342683426934270342713427234273342743427534276342773427834279342803428134282342833428434285342863428734288342893429034291342923429334294342953429634297342983429934300343013430234303343043430534306343073430834309343103431134312343133431434315343163431734318343193432034321343223432334324343253432634327343283432934330343313433234333343343433534336343373433834339343403434134342343433434434345343463434734348343493435034351343523435334354343553435634357343583435934360343613436234363343643436534366343673436834369343703437134372343733437434375343763437734378343793438034381343823438334384343853438634387343883438934390343913439234393343943439534396343973439834399344003440134402344033440434405344063440734408344093441034411344123441334414344153441634417344183441934420344213442234423344243442534426344273442834429344303443134432344333443434435344363443734438344393444034441344423444334444344453444634447344483444934450344513445234453344543445534456344573445834459344603446134462344633446434465344663446734468344693447034471344723447334474344753447634477344783447934480344813448234483344843448534486344873448834489344903449134492344933449434495344963449734498344993450034501345023450334504345053450634507345083450934510345113451234513345143451534516345173451834519345203452134522345233452434525345263452734528345293453034531345323453334534345353453634537345383453934540345413454234543345443454534546345473454834549345503455134552345533455434555345563455734558345593456034561345623456334564345653456634567345683456934570345713457234573345743457534576345773457834579345803458134582345833458434585345863458734588345893459034591345923459334594345953459634597345983459934600346013460234603346043460534606346073460834609346103461134612346133461434615346163461734618346193462034621346223462334624346253462634627346283462934630346313463234633346343463534636346373463834639346403464134642346433464434645346463464734648346493465034651346523465334654346553465634657346583465934660346613466234663346643466534666346673466834669346703467134672346733467434675346763467734678346793468034681346823468334684346853468634687346883468934690346913469234693346943469534696346973469834699347003470134702347033470434705347063470734708347093471034711347123471334714347153471634717347183471934720347213472234723347243472534726347273472834729347303473134732347333473434735347363473734738347393474034741347423474334744347453474634747347483474934750347513475234753347543475534756347573475834759347603476134762347633476434765347663476734768347693477034771347723477334774347753477634777347783477934780347813478234783347843478534786347873478834789347903479134792347933479434795347963479734798347993480034801348023480334804348053480634807348083480934810348113481234813348143481534816348173481834819348203482134822348233482434825348263482734828348293483034831348323483334834348353483634837348383483934840348413484234843348443484534846348473484834849348503485134852348533485434855348563485734858348593486034861348623486334864348653486634867348683486934870348713487234873348743487534876348773487834879348803488134882348833488434885348863488734888348893489034891348923489334894348953489634897348983489934900349013490234903349043490534906349073490834909349103491134912349133491434915349163491734918349193492034921349223492334924349253492634927349283492934930349313493234933349343493534936349373493834939349403494134942349433494434945349463494734948349493495034951349523495334954349553495634957349583495934960349613496234963349643496534966349673496834969349703497134972349733497434975349763497734978349793498034981349823498334984349853498634987349883498934990349913499234993349943499534996349973499834999350003500135002350033500435005350063500735008350093501035011350123501335014350153501635017350183501935020350213502235023350243502535026350273502835029350303503135032350333503435035350363503735038350393504035041350423504335044350453504635047350483504935050350513505235053350543505535056350573505835059350603506135062350633506435065350663506735068350693507035071350723507335074350753507635077350783507935080350813508235083350843508535086350873508835089350903509135092350933509435095350963509735098350993510035101351023510335104351053510635107351083510935110351113511235113351143511535116351173511835119351203512135122351233512435125351263512735128351293513035131351323513335134351353513635137351383513935140351413514235143351443514535146351473514835149351503515135152351533515435155351563515735158351593516035161351623516335164351653516635167351683516935170351713517235173351743517535176351773517835179351803518135182351833518435185351863518735188351893519035191351923519335194351953519635197351983519935200352013520235203352043520535206352073520835209352103521135212352133521435215352163521735218352193522035221352223522335224352253522635227352283522935230352313523235233352343523535236352373523835239352403524135242352433524435245352463524735248352493525035251352523525335254352553525635257352583525935260352613526235263352643526535266352673526835269352703527135272352733527435275352763527735278352793528035281352823528335284352853528635287352883528935290352913529235293352943529535296352973529835299353003530135302353033530435305353063530735308353093531035311353123531335314353153531635317353183531935320353213532235323353243532535326353273532835329353303533135332353333533435335353363533735338353393534035341353423534335344353453534635347353483534935350353513535235353353543535535356353573535835359353603536135362353633536435365353663536735368353693537035371353723537335374353753537635377353783537935380353813538235383353843538535386353873538835389353903539135392353933539435395353963539735398353993540035401354023540335404354053540635407354083540935410354113541235413354143541535416354173541835419354203542135422354233542435425354263542735428354293543035431354323543335434354353543635437354383543935440354413544235443354443544535446354473544835449354503545135452354533545435455354563545735458354593546035461354623546335464354653546635467354683546935470354713547235473354743547535476354773547835479354803548135482354833548435485354863548735488354893549035491354923549335494354953549635497354983549935500355013550235503355043550535506355073550835509355103551135512355133551435515355163551735518355193552035521355223552335524355253552635527355283552935530355313553235533355343553535536355373553835539355403554135542355433554435545355463554735548355493555035551355523555335554355553555635557355583555935560355613556235563355643556535566355673556835569355703557135572355733557435575355763557735578355793558035581355823558335584355853558635587355883558935590355913559235593355943559535596355973559835599356003560135602356033560435605356063560735608356093561035611356123561335614356153561635617356183561935620356213562235623356243562535626356273562835629356303563135632356333563435635356363563735638356393564035641356423564335644356453564635647356483564935650356513565235653356543565535656356573565835659356603566135662356633566435665356663566735668356693567035671356723567335674356753567635677356783567935680356813568235683356843568535686356873568835689356903569135692356933569435695356963569735698356993570035701357023570335704357053570635707357083570935710357113571235713357143571535716357173571835719357203572135722357233572435725357263572735728357293573035731357323573335734357353573635737357383573935740357413574235743357443574535746357473574835749357503575135752357533575435755357563575735758357593576035761357623576335764357653576635767357683576935770357713577235773357743577535776357773577835779357803578135782357833578435785357863578735788357893579035791357923579335794357953579635797357983579935800358013580235803358043580535806358073580835809358103581135812358133581435815358163581735818358193582035821358223582335824358253582635827358283582935830358313583235833358343583535836358373583835839358403584135842358433584435845358463584735848358493585035851358523585335854358553585635857358583585935860358613586235863358643586535866358673586835869358703587135872358733587435875358763587735878358793588035881358823588335884358853588635887358883588935890358913589235893358943589535896358973589835899359003590135902359033590435905359063590735908359093591035911359123591335914359153591635917359183591935920359213592235923359243592535926359273592835929359303593135932359333593435935359363593735938359393594035941359423594335944359453594635947359483594935950359513595235953359543595535956359573595835959359603596135962359633596435965359663596735968359693597035971359723597335974359753597635977359783597935980359813598235983359843598535986359873598835989359903599135992359933599435995359963599735998359993600036001360023600336004360053600636007360083600936010360113601236013360143601536016360173601836019360203602136022360233602436025360263602736028360293603036031360323603336034360353603636037360383603936040360413604236043360443604536046360473604836049360503605136052360533605436055360563605736058360593606036061360623606336064360653606636067360683606936070360713607236073360743607536076360773607836079360803608136082360833608436085360863608736088360893609036091360923609336094360953609636097360983609936100361013610236103361043610536106361073610836109361103611136112361133611436115361163611736118361193612036121361223612336124361253612636127361283612936130361313613236133361343613536136361373613836139361403614136142361433614436145361463614736148361493615036151361523615336154361553615636157361583615936160361613616236163361643616536166361673616836169361703617136172361733617436175361763617736178361793618036181361823618336184361853618636187361883618936190361913619236193361943619536196361973619836199362003620136202362033620436205362063620736208362093621036211362123621336214362153621636217362183621936220362213622236223362243622536226362273622836229362303623136232362333623436235362363623736238362393624036241362423624336244362453624636247362483624936250362513625236253362543625536256362573625836259362603626136262362633626436265362663626736268362693627036271362723627336274362753627636277362783627936280362813628236283362843628536286362873628836289362903629136292362933629436295362963629736298362993630036301363023630336304363053630636307363083630936310363113631236313363143631536316363173631836319363203632136322363233632436325363263632736328363293633036331363323633336334363353633636337363383633936340363413634236343363443634536346363473634836349363503635136352363533635436355363563635736358363593636036361363623636336364363653636636367363683636936370363713637236373363743637536376363773637836379363803638136382363833638436385363863638736388363893639036391363923639336394363953639636397363983639936400364013640236403364043640536406364073640836409364103641136412364133641436415364163641736418364193642036421364223642336424364253642636427364283642936430364313643236433364343643536436364373643836439364403644136442364433644436445364463644736448364493645036451364523645336454364553645636457364583645936460364613646236463364643646536466364673646836469364703647136472364733647436475364763647736478364793648036481364823648336484364853648636487364883648936490364913649236493364943649536496364973649836499365003650136502365033650436505365063650736508365093651036511365123651336514365153651636517365183651936520365213652236523365243652536526365273652836529365303653136532365333653436535365363653736538365393654036541365423654336544365453654636547365483654936550365513655236553365543655536556365573655836559365603656136562365633656436565365663656736568365693657036571365723657336574365753657636577365783657936580365813658236583365843658536586365873658836589365903659136592365933659436595365963659736598365993660036601366023660336604366053660636607366083660936610366113661236613366143661536616366173661836619366203662136622366233662436625366263662736628366293663036631366323663336634366353663636637366383663936640366413664236643366443664536646366473664836649366503665136652366533665436655366563665736658366593666036661366623666336664366653666636667366683666936670366713667236673366743667536676366773667836679366803668136682366833668436685366863668736688366893669036691366923669336694366953669636697366983669936700367013670236703367043670536706367073670836709367103671136712367133671436715367163671736718367193672036721367223672336724367253672636727367283672936730367313673236733367343673536736367373673836739367403674136742367433674436745367463674736748367493675036751367523675336754367553675636757367583675936760367613676236763367643676536766367673676836769367703677136772367733677436775367763677736778367793678036781367823678336784367853678636787367883678936790367913679236793367943679536796367973679836799368003680136802368033680436805368063680736808368093681036811368123681336814368153681636817368183681936820368213682236823368243682536826368273682836829368303683136832368333683436835368363683736838368393684036841368423684336844368453684636847368483684936850368513685236853368543685536856368573685836859368603686136862368633686436865368663686736868368693687036871368723687336874368753687636877368783687936880368813688236883368843688536886368873688836889368903689136892368933689436895368963689736898368993690036901369023690336904369053690636907369083690936910369113691236913369143691536916369173691836919369203692136922369233692436925369263692736928369293693036931369323693336934369353693636937369383693936940369413694236943369443694536946369473694836949369503695136952369533695436955369563695736958369593696036961369623696336964369653696636967369683696936970369713697236973369743697536976369773697836979369803698136982369833698436985369863698736988369893699036991369923699336994369953699636997369983699937000370013700237003370043700537006370073700837009370103701137012370133701437015370163701737018370193702037021370223702337024370253702637027370283702937030370313703237033370343703537036370373703837039370403704137042370433704437045370463704737048370493705037051370523705337054370553705637057370583705937060370613706237063370643706537066370673706837069370703707137072370733707437075370763707737078370793708037081370823708337084370853708637087370883708937090370913709237093370943709537096370973709837099371003710137102371033710437105371063710737108371093711037111371123711337114371153711637117371183711937120371213712237123371243712537126371273712837129371303713137132371333713437135371363713737138371393714037141371423714337144371453714637147371483714937150371513715237153371543715537156371573715837159371603716137162371633716437165371663716737168371693717037171371723717337174371753717637177371783717937180371813718237183371843718537186371873718837189371903719137192371933719437195371963719737198371993720037201372023720337204372053720637207372083720937210372113721237213372143721537216372173721837219372203722137222372233722437225372263722737228372293723037231372323723337234372353723637237372383723937240372413724237243372443724537246372473724837249372503725137252372533725437255372563725737258372593726037261372623726337264372653726637267372683726937270372713727237273372743727537276372773727837279372803728137282372833728437285372863728737288372893729037291372923729337294372953729637297372983729937300373013730237303373043730537306373073730837309373103731137312373133731437315373163731737318373193732037321373223732337324373253732637327373283732937330373313733237333373343733537336373373733837339373403734137342373433734437345373463734737348373493735037351373523735337354373553735637357373583735937360373613736237363373643736537366373673736837369373703737137372373733737437375373763737737378373793738037381373823738337384373853738637387373883738937390373913739237393373943739537396373973739837399374003740137402374033740437405374063740737408374093741037411374123741337414374153741637417374183741937420374213742237423374243742537426374273742837429374303743137432374333743437435374363743737438374393744037441374423744337444374453744637447374483744937450374513745237453374543745537456374573745837459374603746137462374633746437465374663746737468374693747037471374723747337474374753747637477374783747937480374813748237483374843748537486374873748837489374903749137492374933749437495374963749737498374993750037501375023750337504375053750637507375083750937510375113751237513375143751537516375173751837519375203752137522375233752437525375263752737528375293753037531375323753337534375353753637537375383753937540375413754237543375443754537546375473754837549375503755137552375533755437555375563755737558375593756037561375623756337564375653756637567375683756937570375713757237573375743757537576375773757837579375803758137582375833758437585375863758737588375893759037591375923759337594375953759637597375983759937600376013760237603376043760537606376073760837609376103761137612376133761437615376163761737618376193762037621376223762337624376253762637627376283762937630376313763237633376343763537636376373763837639376403764137642376433764437645376463764737648376493765037651376523765337654376553765637657376583765937660376613766237663376643766537666376673766837669376703767137672376733767437675376763767737678376793768037681376823768337684376853768637687376883768937690376913769237693376943769537696376973769837699377003770137702377033770437705377063770737708377093771037711377123771337714377153771637717377183771937720377213772237723377243772537726377273772837729377303773137732377333773437735377363773737738377393774037741377423774337744377453774637747377483774937750377513775237753377543775537756377573775837759377603776137762377633776437765377663776737768377693777037771377723777337774377753777637777377783777937780377813778237783377843778537786377873778837789377903779137792377933779437795377963779737798377993780037801378023780337804378053780637807378083780937810378113781237813378143781537816378173781837819378203782137822378233782437825378263782737828378293783037831378323783337834378353783637837378383783937840378413784237843378443784537846378473784837849378503785137852378533785437855378563785737858378593786037861378623786337864378653786637867378683786937870378713787237873378743787537876378773787837879378803788137882378833788437885378863788737888378893789037891378923789337894378953789637897378983789937900379013790237903379043790537906379073790837909379103791137912379133791437915379163791737918379193792037921379223792337924379253792637927379283792937930379313793237933379343793537936379373793837939379403794137942379433794437945379463794737948379493795037951379523795337954379553795637957379583795937960379613796237963379643796537966379673796837969379703797137972379733797437975379763797737978379793798037981379823798337984379853798637987379883798937990379913799237993379943799537996379973799837999380003800138002380033800438005380063800738008380093801038011380123801338014380153801638017380183801938020380213802238023380243802538026380273802838029380303803138032380333803438035380363803738038380393804038041380423804338044380453804638047380483804938050380513805238053380543805538056380573805838059380603806138062380633806438065380663806738068380693807038071380723807338074380753807638077380783807938080380813808238083380843808538086380873808838089380903809138092380933809438095380963809738098380993810038101381023810338104381053810638107381083810938110381113811238113381143811538116381173811838119381203812138122381233812438125381263812738128381293813038131381323813338134381353813638137381383813938140381413814238143381443814538146381473814838149381503815138152381533815438155381563815738158381593816038161381623816338164381653816638167381683816938170381713817238173381743817538176381773817838179381803818138182381833818438185381863818738188381893819038191381923819338194
  1. diff -Naur null/Documentation/ABI/testing/debugfs-aufs linux-5.1/Documentation/ABI/testing/debugfs-aufs
  2. --- /dev/null
  3. +++ linux-5.1/Documentation/ABI/testing/debugfs-aufs 2019-05-22 11:23:07.435141272 +0200
  4. @@ -0,0 +1,55 @@
  5. +What: /debug/aufs/si_<id>/
  6. +Date: March 2009
  7. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  8. +Description:
  9. + Under /debug/aufs, a directory named si_<id> is created
  10. + per aufs mount, where <id> is a unique id generated
  11. + internally.
  12. +
  13. +What: /debug/aufs/si_<id>/plink
  14. +Date: Apr 2013
  15. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  16. +Description:
  17. + It has three lines and shows the information about the
  18. + pseudo-link. The first line is a single number
  19. + representing a number of buckets. The second line is a
  20. + number of pseudo-links per buckets (separated by a
  21. + blank). The last line is a single number representing a
  22. + total number of psedo-links.
  23. + When the aufs mount option 'noplink' is specified, it
  24. + will show "1\n0\n0\n".
  25. +
  26. +What: /debug/aufs/si_<id>/xib
  27. +Date: March 2009
  28. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  29. +Description:
  30. + It shows the consumed blocks by xib (External Inode Number
  31. + Bitmap), its block size and file size.
  32. + When the aufs mount option 'noxino' is specified, it
  33. + will be empty. About XINO files, see the aufs manual.
  34. +
  35. +What: /debug/aufs/si_<id>/xi0, xi1 ... xiN and xiN-N
  36. +Date: March 2009
  37. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  38. +Description:
  39. + It shows the consumed blocks by xino (External Inode Number
  40. + Translation Table), its link count, block size and file
  41. + size.
  42. + Due to the file size limit, there may exist multiple
  43. + xino files per branch. In this case, "-N" is added to
  44. + the filename and it corresponds to the index of the
  45. + internal xino array. "-0" is omitted.
  46. + When the aufs mount option 'noxino' is specified, Those
  47. + entries won't exist. About XINO files, see the aufs
  48. + manual.
  49. +
  50. +What: /debug/aufs/si_<id>/xigen
  51. +Date: March 2009
  52. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  53. +Description:
  54. + It shows the consumed blocks by xigen (External Inode
  55. + Generation Table), its block size and file size.
  56. + If CONFIG_AUFS_EXPORT is disabled, this entry will not
  57. + be created.
  58. + When the aufs mount option 'noxino' is specified, it
  59. + will be empty. About XINO files, see the aufs manual.
  60. diff -Naur null/Documentation/ABI/testing/sysfs-aufs linux-5.1/Documentation/ABI/testing/sysfs-aufs
  61. --- /dev/null
  62. +++ linux-5.1/Documentation/ABI/testing/sysfs-aufs 2019-05-22 11:23:07.435141272 +0200
  63. @@ -0,0 +1,31 @@
  64. +What: /sys/fs/aufs/si_<id>/
  65. +Date: March 2009
  66. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  67. +Description:
  68. + Under /sys/fs/aufs, a directory named si_<id> is created
  69. + per aufs mount, where <id> is a unique id generated
  70. + internally.
  71. +
  72. +What: /sys/fs/aufs/si_<id>/br0, br1 ... brN
  73. +Date: March 2009
  74. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  75. +Description:
  76. + It shows the abolute path of a member directory (which
  77. + is called branch) in aufs, and its permission.
  78. +
  79. +What: /sys/fs/aufs/si_<id>/brid0, brid1 ... bridN
  80. +Date: July 2013
  81. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  82. +Description:
  83. + It shows the id of a member directory (which is called
  84. + branch) in aufs.
  85. +
  86. +What: /sys/fs/aufs/si_<id>/xi_path
  87. +Date: March 2009
  88. +Contact: J. R. Okajima <hooanon05g@gmail.com>
  89. +Description:
  90. + It shows the abolute path of XINO (External Inode Number
  91. + Bitmap, Translation Table and Generation Table) file
  92. + even if it is the default path.
  93. + When the aufs mount option 'noxino' is specified, it
  94. + will be empty. About XINO files, see the aufs manual.
  95. diff -Naur null/Documentation/filesystems/aufs/design/01intro.txt linux-5.1/Documentation/filesystems/aufs/design/01intro.txt
  96. --- /dev/null
  97. +++ linux-5.1/Documentation/filesystems/aufs/design/01intro.txt 2019-05-22 11:23:07.436141226 +0200
  98. @@ -0,0 +1,171 @@
  99. +
  100. +# Copyright (C) 2005-2019 Junjiro R. Okajima
  101. +#
  102. +# This program is free software; you can redistribute it and/or modify
  103. +# it under the terms of the GNU General Public License as published by
  104. +# the Free Software Foundation; either version 2 of the License, or
  105. +# (at your option) any later version.
  106. +#
  107. +# This program is distributed in the hope that it will be useful,
  108. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  109. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  110. +# GNU General Public License for more details.
  111. +#
  112. +# You should have received a copy of the GNU General Public License
  113. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  114. +
  115. +Introduction
  116. +----------------------------------------
  117. +
  118. +aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
  119. +1. abbrev. for "advanced multi-layered unification filesystem".
  120. +2. abbrev. for "another unionfs".
  121. +3. abbrev. for "auf das" in German which means "on the" in English.
  122. + Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
  123. + But "Filesystem aufs Filesystem" is hard to understand.
  124. +4. abbrev. for "African Urban Fashion Show".
  125. +
  126. +AUFS is a filesystem with features:
  127. +- multi layered stackable unification filesystem, the member directory
  128. + is called as a branch.
  129. +- branch permission and attribute, 'readonly', 'real-readonly',
  130. + 'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
  131. + combination.
  132. +- internal "file copy-on-write".
  133. +- logical deletion, whiteout.
  134. +- dynamic branch manipulation, adding, deleting and changing permission.
  135. +- allow bypassing aufs, user's direct branch access.
  136. +- external inode number translation table and bitmap which maintains the
  137. + persistent aufs inode number.
  138. +- seekable directory, including NFS readdir.
  139. +- file mapping, mmap and sharing pages.
  140. +- pseudo-link, hardlink over branches.
  141. +- loopback mounted filesystem as a branch.
  142. +- several policies to select one among multiple writable branches.
  143. +- revert a single systemcall when an error occurs in aufs.
  144. +- and more...
  145. +
  146. +
  147. +Multi Layered Stackable Unification Filesystem
  148. +----------------------------------------------------------------------
  149. +Most people already knows what it is.
  150. +It is a filesystem which unifies several directories and provides a
  151. +merged single directory. When users access a file, the access will be
  152. +passed/re-directed/converted (sorry, I am not sure which English word is
  153. +correct) to the real file on the member filesystem. The member
  154. +filesystem is called 'lower filesystem' or 'branch' and has a mode
  155. +'readonly' and 'readwrite.' And the deletion for a file on the lower
  156. +readonly branch is handled by creating 'whiteout' on the upper writable
  157. +branch.
  158. +
  159. +On LKML, there have been discussions about UnionMount (Jan Blunck,
  160. +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
  161. +different approaches to implement the merged-view.
  162. +The former tries putting it into VFS, and the latter implements as a
  163. +separate filesystem.
  164. +(If I misunderstand about these implementations, please let me know and
  165. +I shall correct it. Because it is a long time ago when I read their
  166. +source files last time).
  167. +
  168. +UnionMount's approach will be able to small, but may be hard to share
  169. +branches between several UnionMount since the whiteout in it is
  170. +implemented in the inode on branch filesystem and always
  171. +shared. According to Bharata's post, readdir does not seems to be
  172. +finished yet.
  173. +There are several missing features known in this implementations such as
  174. +- for users, the inode number may change silently. eg. copy-up.
  175. +- link(2) may break by copy-up.
  176. +- read(2) may get an obsoleted filedata (fstat(2) too).
  177. +- fcntl(F_SETLK) may be broken by copy-up.
  178. +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
  179. + open(O_RDWR).
  180. +
  181. +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
  182. +merged into mainline. This is another implementation of UnionMount as a
  183. +separated filesystem. All the limitations and known problems which
  184. +UnionMount are equally inherited to "overlay" filesystem.
  185. +
  186. +Unionfs has a longer history. When I started implementing a stackable
  187. +filesystem (Aug 2005), it already existed. It has virtual super_block,
  188. +inode, dentry and file objects and they have an array pointing lower
  189. +same kind objects. After contributing many patches for Unionfs, I
  190. +re-started my project AUFS (Jun 2006).
  191. +
  192. +In AUFS, the structure of filesystem resembles to Unionfs, but I
  193. +implemented my own ideas, approaches and enhancements and it became
  194. +totally different one.
  195. +
  196. +Comparing DM snapshot and fs based implementation
  197. +- the number of bytes to be copied between devices is much smaller.
  198. +- the type of filesystem must be one and only.
  199. +- the fs must be writable, no readonly fs, even for the lower original
  200. + device. so the compression fs will not be usable. but if we use
  201. + loopback mount, we may address this issue.
  202. + for instance,
  203. + mount /cdrom/squashfs.img /sq
  204. + losetup /sq/ext2.img
  205. + losetup /somewhere/cow
  206. + dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
  207. +- it will be difficult (or needs more operations) to extract the
  208. + difference between the original device and COW.
  209. +- DM snapshot-merge may help a lot when users try merging. in the
  210. + fs-layer union, users will use rsync(1).
  211. +
  212. +You may want to read my old paper "Filesystems in LiveCD"
  213. +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
  214. +
  215. +
  216. +Several characters/aspects/persona of aufs
  217. +----------------------------------------------------------------------
  218. +
  219. +Aufs has several characters, aspects or persona.
  220. +1. a filesystem, callee of VFS helper
  221. +2. sub-VFS, caller of VFS helper for branches
  222. +3. a virtual filesystem which maintains persistent inode number
  223. +4. reader/writer of files on branches such like an application
  224. +
  225. +1. Callee of VFS Helper
  226. +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
  227. +unlink(2) from an application reaches sys_unlink() kernel function and
  228. +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
  229. +calls filesystem specific unlink operation. Actually aufs implements the
  230. +unlink operation but it behaves like a redirector.
  231. +
  232. +2. Caller of VFS Helper for Branches
  233. +aufs_unlink() passes the unlink request to the branch filesystem as if
  234. +it were called from VFS. So the called unlink operation of the branch
  235. +filesystem acts as usual. As a caller of VFS helper, aufs should handle
  236. +every necessary pre/post operation for the branch filesystem.
  237. +- acquire the lock for the parent dir on a branch
  238. +- lookup in a branch
  239. +- revalidate dentry on a branch
  240. +- mnt_want_write() for a branch
  241. +- vfs_unlink() for a branch
  242. +- mnt_drop_write() for a branch
  243. +- release the lock on a branch
  244. +
  245. +3. Persistent Inode Number
  246. +One of the most important issue for a filesystem is to maintain inode
  247. +numbers. This is particularly important to support exporting a
  248. +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
  249. +backend block device for its own. But some storage is necessary to
  250. +keep and maintain the inode numbers. It may be a large space and may not
  251. +suit to keep in memory. Aufs rents some space from its first writable
  252. +branch filesystem (by default) and creates file(s) on it. These files
  253. +are created by aufs internally and removed soon (currently) keeping
  254. +opened.
  255. +Note: Because these files are removed, they are totally gone after
  256. + unmounting aufs. It means the inode numbers are not persistent
  257. + across unmount or reboot. I have a plan to make them really
  258. + persistent which will be important for aufs on NFS server.
  259. +
  260. +4. Read/Write Files Internally (copy-on-write)
  261. +Because a branch can be readonly, when you write a file on it, aufs will
  262. +"copy-up" it to the upper writable branch internally. And then write the
  263. +originally requested thing to the file. Generally kernel doesn't
  264. +open/read/write file actively. In aufs, even a single write may cause a
  265. +internal "file copy". This behaviour is very similar to cp(1) command.
  266. +
  267. +Some people may think it is better to pass such work to user space
  268. +helper, instead of doing in kernel space. Actually I am still thinking
  269. +about it. But currently I have implemented it in kernel space.
  270. diff -Naur null/Documentation/filesystems/aufs/design/02struct.txt linux-5.1/Documentation/filesystems/aufs/design/02struct.txt
  271. --- /dev/null
  272. +++ linux-5.1/Documentation/filesystems/aufs/design/02struct.txt 2019-05-22 11:23:07.436141226 +0200
  273. @@ -0,0 +1,258 @@
  274. +
  275. +# Copyright (C) 2005-2019 Junjiro R. Okajima
  276. +#
  277. +# This program is free software; you can redistribute it and/or modify
  278. +# it under the terms of the GNU General Public License as published by
  279. +# the Free Software Foundation; either version 2 of the License, or
  280. +# (at your option) any later version.
  281. +#
  282. +# This program is distributed in the hope that it will be useful,
  283. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  284. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  285. +# GNU General Public License for more details.
  286. +#
  287. +# You should have received a copy of the GNU General Public License
  288. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  289. +
  290. +Basic Aufs Internal Structure
  291. +
  292. +Superblock/Inode/Dentry/File Objects
  293. +----------------------------------------------------------------------
  294. +As like an ordinary filesystem, aufs has its own
  295. +superblock/inode/dentry/file objects. All these objects have a
  296. +dynamically allocated array and store the same kind of pointers to the
  297. +lower filesystem, branch.
  298. +For example, when you build a union with one readwrite branch and one
  299. +readonly, mounted /au, /rw and /ro respectively.
  300. +- /au = /rw + /ro
  301. +- /ro/fileA exists but /rw/fileA
  302. +
  303. +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
  304. +pointers are stored in a aufs dentry. The array in aufs dentry will be,
  305. +- [0] = NULL (because /rw/fileA doesn't exist)
  306. +- [1] = /ro/fileA
  307. +
  308. +This style of an array is essentially same to the aufs
  309. +superblock/inode/dentry/file objects.
  310. +
  311. +Because aufs supports manipulating branches, ie. add/delete/change
  312. +branches dynamically, these objects has its own generation. When
  313. +branches are changed, the generation in aufs superblock is
  314. +incremented. And a generation in other object are compared when it is
  315. +accessed. When a generation in other objects are obsoleted, aufs
  316. +refreshes the internal array.
  317. +
  318. +
  319. +Superblock
  320. +----------------------------------------------------------------------
  321. +Additionally aufs superblock has some data for policies to select one
  322. +among multiple writable branches, XIB files, pseudo-links and kobject.
  323. +See below in detail.
  324. +About the policies which supports copy-down a directory, see
  325. +wbr_policy.txt too.
  326. +
  327. +
  328. +Branch and XINO(External Inode Number Translation Table)
  329. +----------------------------------------------------------------------
  330. +Every branch has its own xino (external inode number translation table)
  331. +file. The xino file is created and unlinked by aufs internally. When two
  332. +members of a union exist on the same filesystem, they share the single
  333. +xino file.
  334. +The struct of a xino file is simple, just a sequence of aufs inode
  335. +numbers which is indexed by the lower inode number.
  336. +In the above sample, assume the inode number of /ro/fileA is i111 and
  337. +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
  338. +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
  339. +
  340. +When the inode numbers are not contiguous, the xino file will be sparse
  341. +which has a hole in it and doesn't consume as much disk space as it
  342. +might appear. If your branch filesystem consumes disk space for such
  343. +holes, then you should specify 'xino=' option at mounting aufs.
  344. +
  345. +Aufs has a mount option to free the disk blocks for such holes in XINO
  346. +files on tmpfs or ramdisk. But it is not so effective actually. If you
  347. +meet a problem of disk shortage due to XINO files, then you should try
  348. +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
  349. +The patch localizes the assignment inumbers per tmpfs-mount and avoid
  350. +the holes in XINO files.
  351. +
  352. +Also a writable branch has three kinds of "whiteout bases". All these
  353. +are existed when the branch is joined to aufs, and their names are
  354. +whiteout-ed doubly, so that users will never see their names in aufs
  355. +hierarchy.
  356. +1. a regular file which will be hardlinked to all whiteouts.
  357. +2. a directory to store a pseudo-link.
  358. +3. a directory to store an "orphan"-ed file temporary.
  359. +
  360. +1. Whiteout Base
  361. + When you remove a file on a readonly branch, aufs handles it as a
  362. + logical deletion and creates a whiteout on the upper writable branch
  363. + as a hardlink of this file in order not to consume inode on the
  364. + writable branch.
  365. +2. Pseudo-link Dir
  366. + See below, Pseudo-link.
  367. +3. Step-Parent Dir
  368. + When "fileC" exists on the lower readonly branch only and it is
  369. + opened and removed with its parent dir, and then user writes
  370. + something into it, then aufs copies-up fileC to this
  371. + directory. Because there is no other dir to store fileC. After
  372. + creating a file under this dir, the file is unlinked.
  373. +
  374. +Because aufs supports manipulating branches, ie. add/delete/change
  375. +dynamically, a branch has its own id. When the branch order changes,
  376. +aufs finds the new index by searching the branch id.
  377. +
  378. +
  379. +Pseudo-link
  380. +----------------------------------------------------------------------
  381. +Assume "fileA" exists on the lower readonly branch only and it is
  382. +hardlinked to "fileB" on the branch. When you write something to fileA,
  383. +aufs copies-up it to the upper writable branch. Additionally aufs
  384. +creates a hardlink under the Pseudo-link Directory of the writable
  385. +branch. The inode of a pseudo-link is kept in aufs super_block as a
  386. +simple list. If fileB is read after unlinking fileA, aufs returns
  387. +filedata from the pseudo-link instead of the lower readonly
  388. +branch. Because the pseudo-link is based upon the inode, to keep the
  389. +inode number by xino (see above) is essentially necessary.
  390. +
  391. +All the hardlinks under the Pseudo-link Directory of the writable branch
  392. +should be restored in a proper location later. Aufs provides a utility
  393. +to do this. The userspace helpers executed at remounting and unmounting
  394. +aufs by default.
  395. +During this utility is running, it puts aufs into the pseudo-link
  396. +maintenance mode. In this mode, only the process which began the
  397. +maintenance mode (and its child processes) is allowed to operate in
  398. +aufs. Some other processes which are not related to the pseudo-link will
  399. +be allowed to run too, but the rest have to return an error or wait
  400. +until the maintenance mode ends. If a process already acquires an inode
  401. +mutex (in VFS), it has to return an error.
  402. +
  403. +
  404. +XIB(external inode number bitmap)
  405. +----------------------------------------------------------------------
  406. +Addition to the xino file per a branch, aufs has an external inode number
  407. +bitmap in a superblock object. It is also an internal file such like a
  408. +xino file.
  409. +It is a simple bitmap to mark whether the aufs inode number is in-use or
  410. +not.
  411. +To reduce the file I/O, aufs prepares a single memory page to cache xib.
  412. +
  413. +As well as XINO files, aufs has a feature to truncate/refresh XIB to
  414. +reduce the number of consumed disk blocks for these files.
  415. +
  416. +
  417. +Virtual or Vertical Dir, and Readdir in Userspace
  418. +----------------------------------------------------------------------
  419. +In order to support multiple layers (branches), aufs readdir operation
  420. +constructs a virtual dir block on memory. For readdir, aufs calls
  421. +vfs_readdir() internally for each dir on branches, merges their entries
  422. +with eliminating the whiteout-ed ones, and sets it to file (dir)
  423. +object. So the file object has its entry list until it is closed. The
  424. +entry list will be updated when the file position is zero and becomes
  425. +obsoleted. This decision is made in aufs automatically.
  426. +
  427. +The dynamically allocated memory block for the name of entries has a
  428. +unit of 512 bytes (by default) and stores the names contiguously (no
  429. +padding). Another block for each entry is handled by kmem_cache too.
  430. +During building dir blocks, aufs creates hash list and judging whether
  431. +the entry is whiteouted by its upper branch or already listed.
  432. +The merged result is cached in the corresponding inode object and
  433. +maintained by a customizable life-time option.
  434. +
  435. +Some people may call it can be a security hole or invite DoS attack
  436. +since the opened and once readdir-ed dir (file object) holds its entry
  437. +list and becomes a pressure for system memory. But I'd say it is similar
  438. +to files under /proc or /sys. The virtual files in them also holds a
  439. +memory page (generally) while they are opened. When an idea to reduce
  440. +memory for them is introduced, it will be applied to aufs too.
  441. +For those who really hate this situation, I've developed readdir(3)
  442. +library which operates this merging in userspace. You just need to set
  443. +LD_PRELOAD environment variable, and aufs will not consume no memory in
  444. +kernel space for readdir(3).
  445. +
  446. +
  447. +Workqueue
  448. +----------------------------------------------------------------------
  449. +Aufs sometimes requires privilege access to a branch. For instance,
  450. +in copy-up/down operation. When a user process is going to make changes
  451. +to a file which exists in the lower readonly branch only, and the mode
  452. +of one of ancestor directories may not be writable by a user
  453. +process. Here aufs copy-up the file with its ancestors and they may
  454. +require privilege to set its owner/group/mode/etc.
  455. +This is a typical case of a application character of aufs (see
  456. +Introduction).
  457. +
  458. +Aufs uses workqueue synchronously for this case. It creates its own
  459. +workqueue. The workqueue is a kernel thread and has privilege. Aufs
  460. +passes the request to call mkdir or write (for example), and wait for
  461. +its completion. This approach solves a problem of a signal handler
  462. +simply.
  463. +If aufs didn't adopt the workqueue and changed the privilege of the
  464. +process, then the process may receive the unexpected SIGXFSZ or other
  465. +signals.
  466. +
  467. +Also aufs uses the system global workqueue ("events" kernel thread) too
  468. +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
  469. +whiteout base and etc. This is unrelated to a privilege.
  470. +Most of aufs operation tries acquiring a rw_semaphore for aufs
  471. +superblock at the beginning, at the same time waits for the completion
  472. +of all queued asynchronous tasks.
  473. +
  474. +
  475. +Whiteout
  476. +----------------------------------------------------------------------
  477. +The whiteout in aufs is very similar to Unionfs's. That is represented
  478. +by its filename. UnionMount takes an approach of a file mode, but I am
  479. +afraid several utilities (find(1) or something) will have to support it.
  480. +
  481. +Basically the whiteout represents "logical deletion" which stops aufs to
  482. +lookup further, but also it represents "dir is opaque" which also stop
  483. +further lookup.
  484. +
  485. +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
  486. +In order to make several functions in a single systemcall to be
  487. +revertible, aufs adopts an approach to rename a directory to a temporary
  488. +unique whiteouted name.
  489. +For example, in rename(2) dir where the target dir already existed, aufs
  490. +renames the target dir to a temporary unique whiteouted name before the
  491. +actual rename on a branch, and then handles other actions (make it opaque,
  492. +update the attributes, etc). If an error happens in these actions, aufs
  493. +simply renames the whiteouted name back and returns an error. If all are
  494. +succeeded, aufs registers a function to remove the whiteouted unique
  495. +temporary name completely and asynchronously to the system global
  496. +workqueue.
  497. +
  498. +
  499. +Copy-up
  500. +----------------------------------------------------------------------
  501. +It is a well-known feature or concept.
  502. +When user modifies a file on a readonly branch, aufs operate "copy-up"
  503. +internally and makes change to the new file on the upper writable branch.
  504. +When the trigger systemcall does not update the timestamps of the parent
  505. +dir, aufs reverts it after copy-up.
  506. +
  507. +
  508. +Move-down (aufs3.9 and later)
  509. +----------------------------------------------------------------------
  510. +"Copy-up" is one of the essential feature in aufs. It copies a file from
  511. +the lower readonly branch to the upper writable branch when a user
  512. +changes something about the file.
  513. +"Move-down" is an opposite action of copy-up. Basically this action is
  514. +ran manually instead of automatically and internally.
  515. +For desgin and implementation, aufs has to consider these issues.
  516. +- whiteout for the file may exist on the lower branch.
  517. +- ancestor directories may not exist on the lower branch.
  518. +- diropq for the ancestor directories may exist on the upper branch.
  519. +- free space on the lower branch will reduce.
  520. +- another access to the file may happen during moving-down, including
  521. + UDBA (see "Revalidate Dentry and UDBA").
  522. +- the file should not be hard-linked nor pseudo-linked. they should be
  523. + handled by auplink utility later.
  524. +
  525. +Sometimes users want to move-down a file from the upper writable branch
  526. +to the lower readonly or writable branch. For instance,
  527. +- the free space of the upper writable branch is going to run out.
  528. +- create a new intermediate branch between the upper and lower branch.
  529. +- etc.
  530. +
  531. +For this purpose, use "aumvdown" command in aufs-util.git.
  532. diff -Naur null/Documentation/filesystems/aufs/design/03atomic_open.txt linux-5.1/Documentation/filesystems/aufs/design/03atomic_open.txt
  533. --- /dev/null
  534. +++ linux-5.1/Documentation/filesystems/aufs/design/03atomic_open.txt 2019-05-22 11:23:07.436141226 +0200
  535. @@ -0,0 +1,85 @@
  536. +
  537. +# Copyright (C) 2015-2019 Junjiro R. Okajima
  538. +#
  539. +# This program is free software; you can redistribute it and/or modify
  540. +# it under the terms of the GNU General Public License as published by
  541. +# the Free Software Foundation; either version 2 of the License, or
  542. +# (at your option) any later version.
  543. +#
  544. +# This program is distributed in the hope that it will be useful,
  545. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  546. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  547. +# GNU General Public License for more details.
  548. +#
  549. +# You should have received a copy of the GNU General Public License
  550. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  551. +
  552. +Support for a branch who has its ->atomic_open()
  553. +----------------------------------------------------------------------
  554. +The filesystems who implement its ->atomic_open() are not majority. For
  555. +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
  556. +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
  557. +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
  558. +sure whether all filesystems who have ->atomic_open() behave like this,
  559. +but NFSv4 surely returns the error.
  560. +
  561. +In order to support ->atomic_open() for aufs, there are a few
  562. +approaches.
  563. +
  564. +A. Introduce aufs_atomic_open()
  565. + - calls one of VFS:do_last(), lookup_open() or atomic_open() for
  566. + branch fs.
  567. +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
  568. + an aufs user Pip Cet's approach
  569. + - calls aufs_create(), VFS finish_open() and notify_change().
  570. + - pass fake-mode to finish_open(), and then correct the mode by
  571. + notify_change().
  572. +C. Extend aufs_open() to call branch fs's ->atomic_open()
  573. + - no aufs_atomic_open().
  574. + - aufs_lookup() registers the TID to an aufs internal object.
  575. + - aufs_create() does nothing when the matching TID is registered, but
  576. + registers the mode.
  577. + - aufs_open() calls branch fs's ->atomic_open() when the matching
  578. + TID is registered.
  579. +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
  580. + credential
  581. + - no aufs_atomic_open().
  582. + - aufs_create() registers the TID to an internal object. this info
  583. + represents "this process created this file just now."
  584. + - when aufs gets EACCES from branch fs's ->open(), then confirm the
  585. + registered TID and re-try open() with superuser's credential.
  586. +
  587. +Pros and cons for each approach.
  588. +
  589. +A.
  590. + - straightforward but highly depends upon VFS internal.
  591. + - the atomic behavaiour is kept.
  592. + - some of parameters such as nameidata are hard to reproduce for
  593. + branch fs.
  594. + - large overhead.
  595. +B.
  596. + - easy to implement.
  597. + - the atomic behavaiour is lost.
  598. +C.
  599. + - the atomic behavaiour is kept.
  600. + - dirty and tricky.
  601. + - VFS checks whether the file is created correctly after calling
  602. + ->create(), which means this approach doesn't work.
  603. +D.
  604. + - easy to implement.
  605. + - the atomic behavaiour is lost.
  606. + - to open a file with superuser's credential and give it to a user
  607. + process is a bad idea, since the file object keeps the credential
  608. + in it. It may affect LSM or something. This approach doesn't work
  609. + either.
  610. +
  611. +The approach A is ideal, but it hard to implement. So here is a
  612. +variation of A, which is to be implemented.
  613. +
  614. +A-1. Introduce aufs_atomic_open()
  615. + - calls branch fs ->atomic_open() if exists. otherwise calls
  616. + vfs_create() and finish_open().
  617. + - the demerit is that the several checks after branch fs
  618. + ->atomic_open() are lost. in the ordinary case, the checks are
  619. + done by VFS:do_last(), lookup_open() and atomic_open(). some can
  620. + be implemented in aufs, but not all I am afraid.
  621. diff -Naur null/Documentation/filesystems/aufs/design/03lookup.txt linux-5.1/Documentation/filesystems/aufs/design/03lookup.txt
  622. --- /dev/null
  623. +++ linux-5.1/Documentation/filesystems/aufs/design/03lookup.txt 2019-05-22 11:23:07.436141226 +0200
  624. @@ -0,0 +1,113 @@
  625. +
  626. +# Copyright (C) 2005-2019 Junjiro R. Okajima
  627. +#
  628. +# This program is free software; you can redistribute it and/or modify
  629. +# it under the terms of the GNU General Public License as published by
  630. +# the Free Software Foundation; either version 2 of the License, or
  631. +# (at your option) any later version.
  632. +#
  633. +# This program is distributed in the hope that it will be useful,
  634. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  635. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  636. +# GNU General Public License for more details.
  637. +#
  638. +# You should have received a copy of the GNU General Public License
  639. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  640. +
  641. +Lookup in a Branch
  642. +----------------------------------------------------------------------
  643. +Since aufs has a character of sub-VFS (see Introduction), it operates
  644. +lookup for branches as VFS does. It may be a heavy work. But almost all
  645. +lookup operation in aufs is the simplest case, ie. lookup only an entry
  646. +directly connected to its parent. Digging down the directory hierarchy
  647. +is unnecessary. VFS has a function lookup_one_len() for that use, and
  648. +aufs calls it.
  649. +
  650. +When a branch is a remote filesystem, aufs basically relies upon its
  651. +->d_revalidate(), also aufs forces the hardest revalidate tests for
  652. +them.
  653. +For d_revalidate, aufs implements three levels of revalidate tests. See
  654. +"Revalidate Dentry and UDBA" in detail.
  655. +
  656. +
  657. +Test Only the Highest One for the Directory Permission (dirperm1 option)
  658. +----------------------------------------------------------------------
  659. +Let's try case study.
  660. +- aufs has two branches, upper readwrite and lower readonly.
  661. + /au = /rw + /ro
  662. +- "dirA" exists under /ro, but /rw. and its mode is 0700.
  663. +- user invoked "chmod a+rx /au/dirA"
  664. +- the internal copy-up is activated and "/rw/dirA" is created and its
  665. + permission bits are set to world readable.
  666. +- then "/au/dirA" becomes world readable?
  667. +
  668. +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
  669. +or it may be a natively readonly filesystem. If aufs respects the lower
  670. +branch, it should not respond readdir request from other users. But user
  671. +allowed it by chmod. Should really aufs rejects showing the entries
  672. +under /ro/dirA?
  673. +
  674. +To be honest, I don't have a good solution for this case. So aufs
  675. +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
  676. +users.
  677. +When dirperm1 is specified, aufs checks only the highest one for the
  678. +directory permission, and shows the entries. Otherwise, as usual, checks
  679. +every dir existing on all branches and rejects the request.
  680. +
  681. +As a side effect, dirperm1 option improves the performance of aufs
  682. +because the number of permission check is reduced when the number of
  683. +branch is many.
  684. +
  685. +
  686. +Revalidate Dentry and UDBA (User's Direct Branch Access)
  687. +----------------------------------------------------------------------
  688. +Generally VFS helpers re-validate a dentry as a part of lookup.
  689. +0. digging down the directory hierarchy.
  690. +1. lock the parent dir by its i_mutex.
  691. +2. lookup the final (child) entry.
  692. +3. revalidate it.
  693. +4. call the actual operation (create, unlink, etc.)
  694. +5. unlock the parent dir
  695. +
  696. +If the filesystem implements its ->d_revalidate() (step 3), then it is
  697. +called. Actually aufs implements it and checks the dentry on a branch is
  698. +still valid.
  699. +But it is not enough. Because aufs has to release the lock for the
  700. +parent dir on a branch at the end of ->lookup() (step 2) and
  701. +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
  702. +held by VFS.
  703. +If the file on a branch is changed directly, eg. bypassing aufs, after
  704. +aufs released the lock, then the subsequent operation may cause
  705. +something unpleasant result.
  706. +
  707. +This situation is a result of VFS architecture, ->lookup() and
  708. +->d_revalidate() is separated. But I never say it is wrong. It is a good
  709. +design from VFS's point of view. It is just not suitable for sub-VFS
  710. +character in aufs.
  711. +
  712. +Aufs supports such case by three level of revalidation which is
  713. +selectable by user.
  714. +1. Simple Revalidate
  715. + Addition to the native flow in VFS's, confirm the child-parent
  716. + relationship on the branch just after locking the parent dir on the
  717. + branch in the "actual operation" (step 4). When this validation
  718. + fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
  719. + checks the validation of the dentry on branches.
  720. +2. Monitor Changes Internally by Inotify/Fsnotify
  721. + Addition to above, in the "actual operation" (step 4) aufs re-lookup
  722. + the dentry on the branch, and returns EBUSY if it finds different
  723. + dentry.
  724. + Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
  725. + during it is in cache. When the event is notified, aufs registers a
  726. + function to kernel 'events' thread by schedule_work(). And the
  727. + function sets some special status to the cached aufs dentry and inode
  728. + private data. If they are not cached, then aufs has nothing to
  729. + do. When the same file is accessed through aufs (step 0-3) later,
  730. + aufs will detect the status and refresh all necessary data.
  731. + In this mode, aufs has to ignore the event which is fired by aufs
  732. + itself.
  733. +3. No Extra Validation
  734. + This is the simplest test and doesn't add any additional revalidation
  735. + test, and skip the revalidation in step 4. It is useful and improves
  736. + aufs performance when system surely hide the aufs branches from user,
  737. + by over-mounting something (or another method).
  738. diff -Naur null/Documentation/filesystems/aufs/design/04branch.txt linux-5.1/Documentation/filesystems/aufs/design/04branch.txt
  739. --- /dev/null
  740. +++ linux-5.1/Documentation/filesystems/aufs/design/04branch.txt 2019-05-22 11:23:07.436141226 +0200
  741. @@ -0,0 +1,74 @@
  742. +
  743. +# Copyright (C) 2005-2019 Junjiro R. Okajima
  744. +#
  745. +# This program is free software; you can redistribute it and/or modify
  746. +# it under the terms of the GNU General Public License as published by
  747. +# the Free Software Foundation; either version 2 of the License, or
  748. +# (at your option) any later version.
  749. +#
  750. +# This program is distributed in the hope that it will be useful,
  751. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  752. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  753. +# GNU General Public License for more details.
  754. +#
  755. +# You should have received a copy of the GNU General Public License
  756. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  757. +
  758. +Branch Manipulation
  759. +
  760. +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
  761. +and changing its permission/attribute, there are a lot of works to do.
  762. +
  763. +
  764. +Add a Branch
  765. +----------------------------------------------------------------------
  766. +o Confirm the adding dir exists outside of aufs, including loopback
  767. + mount, and its various attributes.
  768. +o Initialize the xino file and whiteout bases if necessary.
  769. + See struct.txt.
  770. +
  771. +o Check the owner/group/mode of the directory
  772. + When the owner/group/mode of the adding directory differs from the
  773. + existing branch, aufs issues a warning because it may impose a
  774. + security risk.
  775. + For example, when a upper writable branch has a world writable empty
  776. + top directory, a malicious user can create any files on the writable
  777. + branch directly, like copy-up and modify manually. If something like
  778. + /etc/{passwd,shadow} exists on the lower readonly branch but the upper
  779. + writable branch, and the writable branch is world-writable, then a
  780. + malicious guy may create /etc/passwd on the writable branch directly
  781. + and the infected file will be valid in aufs.
  782. + I am afraid it can be a security issue, but aufs can do nothing except
  783. + producing a warning.
  784. +
  785. +
  786. +Delete a Branch
  787. +----------------------------------------------------------------------
  788. +o Confirm the deleting branch is not busy
  789. + To be general, there is one merit to adopt "remount" interface to
  790. + manipulate branches. It is to discard caches. At deleting a branch,
  791. + aufs checks the still cached (and connected) dentries and inodes. If
  792. + there are any, then they are all in-use. An inode without its
  793. + corresponding dentry can be alive alone (for example, inotify/fsnotify case).
  794. +
  795. + For the cached one, aufs checks whether the same named entry exists on
  796. + other branches.
  797. + If the cached one is a directory, because aufs provides a merged view
  798. + to users, as long as one dir is left on any branch aufs can show the
  799. + dir to users. In this case, the branch can be removed from aufs.
  800. + Otherwise aufs rejects deleting the branch.
  801. +
  802. + If any file on the deleting branch is opened by aufs, then aufs
  803. + rejects deleting.
  804. +
  805. +
  806. +Modify the Permission of a Branch
  807. +----------------------------------------------------------------------
  808. +o Re-initialize or remove the xino file and whiteout bases if necessary.
  809. + See struct.txt.
  810. +
  811. +o rw --> ro: Confirm the modifying branch is not busy
  812. + Aufs rejects the request if any of these conditions are true.
  813. + - a file on the branch is mmap-ed.
  814. + - a regular file on the branch is opened for write and there is no
  815. + same named entry on the upper branch.
  816. diff -Naur null/Documentation/filesystems/aufs/design/05wbr_policy.txt linux-5.1/Documentation/filesystems/aufs/design/05wbr_policy.txt
  817. --- /dev/null
  818. +++ linux-5.1/Documentation/filesystems/aufs/design/05wbr_policy.txt 2019-05-22 11:23:07.436141226 +0200
  819. @@ -0,0 +1,64 @@
  820. +
  821. +# Copyright (C) 2005-2019 Junjiro R. Okajima
  822. +#
  823. +# This program is free software; you can redistribute it and/or modify
  824. +# it under the terms of the GNU General Public License as published by
  825. +# the Free Software Foundation; either version 2 of the License, or
  826. +# (at your option) any later version.
  827. +#
  828. +# This program is distributed in the hope that it will be useful,
  829. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  830. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  831. +# GNU General Public License for more details.
  832. +#
  833. +# You should have received a copy of the GNU General Public License
  834. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  835. +
  836. +Policies to Select One among Multiple Writable Branches
  837. +----------------------------------------------------------------------
  838. +When the number of writable branch is more than one, aufs has to decide
  839. +the target branch for file creation or copy-up. By default, the highest
  840. +writable branch which has the parent (or ancestor) dir of the target
  841. +file is chosen (top-down-parent policy).
  842. +By user's request, aufs implements some other policies to select the
  843. +writable branch, for file creation several policies, round-robin,
  844. +most-free-space, and other policies. For copy-up, top-down-parent,
  845. +bottom-up-parent, bottom-up and others.
  846. +
  847. +As expected, the round-robin policy selects the branch in circular. When
  848. +you have two writable branches and creates 10 new files, 5 files will be
  849. +created for each branch. mkdir(2) systemcall is an exception. When you
  850. +create 10 new directories, all will be created on the same branch.
  851. +And the most-free-space policy selects the one which has most free
  852. +space among the writable branches. The amount of free space will be
  853. +checked by aufs internally, and users can specify its time interval.
  854. +
  855. +The policies for copy-up is more simple,
  856. +top-down-parent is equivalent to the same named on in create policy,
  857. +bottom-up-parent selects the writable branch where the parent dir
  858. +exists and the nearest upper one from the copyup-source,
  859. +bottom-up selects the nearest upper writable branch from the
  860. +copyup-source, regardless the existence of the parent dir.
  861. +
  862. +There are some rules or exceptions to apply these policies.
  863. +- If there is a readonly branch above the policy-selected branch and
  864. + the parent dir is marked as opaque (a variation of whiteout), or the
  865. + target (creating) file is whiteout-ed on the upper readonly branch,
  866. + then the result of the policy is ignored and the target file will be
  867. + created on the nearest upper writable branch than the readonly branch.
  868. +- If there is a writable branch above the policy-selected branch and
  869. + the parent dir is marked as opaque or the target file is whiteouted
  870. + on the branch, then the result of the policy is ignored and the target
  871. + file will be created on the highest one among the upper writable
  872. + branches who has diropq or whiteout. In case of whiteout, aufs removes
  873. + it as usual.
  874. +- link(2) and rename(2) systemcalls are exceptions in every policy.
  875. + They try selecting the branch where the source exists as possible
  876. + since copyup a large file will take long time. If it can't be,
  877. + ie. the branch where the source exists is readonly, then they will
  878. + follow the copyup policy.
  879. +- There is an exception for rename(2) when the target exists.
  880. + If the rename target exists, aufs compares the index of the branches
  881. + where the source and the target exists and selects the higher
  882. + one. If the selected branch is readonly, then aufs follows the
  883. + copyup policy.
  884. diff -Naur null/Documentation/filesystems/aufs/design/06dirren.dot linux-5.1/Documentation/filesystems/aufs/design/06dirren.dot
  885. --- /dev/null
  886. +++ linux-5.1/Documentation/filesystems/aufs/design/06dirren.dot 2019-05-22 11:23:07.436141226 +0200
  887. @@ -0,0 +1,31 @@
  888. +
  889. +// to view this graph, run dot(1) command in GRAPHVIZ.
  890. +
  891. +digraph G {
  892. +node [shape=box];
  893. +whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
  894. +
  895. +node [shape=oval];
  896. +
  897. +aufs_rename -> whinfo [label="store/remove"];
  898. +
  899. +node [shape=oval];
  900. +inode_list [label="h_inum list in branch\ncache"];
  901. +
  902. +node [shape=box];
  903. +whinode [label="h_inum list file"];
  904. +
  905. +node [shape=oval];
  906. +brmgmt [label="br_add/del/mod/umount"];
  907. +
  908. +brmgmt -> inode_list [label="create/remove"];
  909. +brmgmt -> whinode [label="load/store"];
  910. +
  911. +inode_list -> whinode [style=dashed,dir=both];
  912. +
  913. +aufs_rename -> inode_list [label="add/del"];
  914. +
  915. +aufs_lookup -> inode_list [label="search"];
  916. +
  917. +aufs_lookup -> whinfo [label="load/remove"];
  918. +}
  919. diff -Naur null/Documentation/filesystems/aufs/design/06dirren.txt linux-5.1/Documentation/filesystems/aufs/design/06dirren.txt
  920. --- /dev/null
  921. +++ linux-5.1/Documentation/filesystems/aufs/design/06dirren.txt 2019-05-22 11:23:07.436141226 +0200
  922. @@ -0,0 +1,102 @@
  923. +
  924. +# Copyright (C) 2017-2019 Junjiro R. Okajima
  925. +#
  926. +# This program is free software; you can redistribute it and/or modify
  927. +# it under the terms of the GNU General Public License as published by
  928. +# the Free Software Foundation; either version 2 of the License, or
  929. +# (at your option) any later version.
  930. +#
  931. +# This program is distributed in the hope that it will be useful,
  932. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  933. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  934. +# GNU General Public License for more details.
  935. +#
  936. +# You should have received a copy of the GNU General Public License
  937. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  938. +
  939. +Special handling for renaming a directory (DIRREN)
  940. +----------------------------------------------------------------------
  941. +First, let's assume we have a simple usecase.
  942. +
  943. +- /u = /rw + /ro
  944. +- /rw/dirA exists
  945. +- /ro/dirA and /ro/dirA/file exist too
  946. +- there is no dirB on both branches
  947. +- a user issues rename("dirA", "dirB")
  948. +
  949. +Now, what should aufs behave against this rename(2)?
  950. +There are a few possible cases.
  951. +
  952. +A. returns EROFS.
  953. + since dirA exists on a readonly branch which cannot be renamed.
  954. +B. returns EXDEV.
  955. + it is possible to copy-up dirA (only the dir itself), but the child
  956. + entries ("file" in this case) should not be. it must be a bad
  957. + approach to copy-up recursively.
  958. +C. returns a success.
  959. + even the branch /ro is readonly, aufs tries renaming it. Obviously it
  960. + is a violation of aufs' policy.
  961. +D. construct an extra information which indicates that /ro/dirA should
  962. + be handled as the name of dirB.
  963. + overlayfs has a similar feature called REDIRECT.
  964. +
  965. +Until now, aufs implements the case B only which returns EXDEV, and
  966. +expects the userspace application behaves like mv(1) which tries
  967. +issueing rename(2) recursively.
  968. +
  969. +A new aufs feature called DIRREN is introduced which implements the case
  970. +D. There are several "extra information" added.
  971. +
  972. +1. detailed info per renamed directory
  973. + path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
  974. +2. the inode-number list of directories on a branch
  975. + path: /rw/dirB/$AUFS_WH_DR_BRHINO
  976. +
  977. +The filename of "detailed info per directory" represents the lower
  978. +branch, and its format is
  979. +- a type of the branch id
  980. + one of these.
  981. + + uuid (not implemented yet)
  982. + + fsid
  983. + + dev
  984. +- the inode-number of the branch root dir
  985. +
  986. +And it contains these info in a single regular file.
  987. +- magic number
  988. +- branch's inode-number of the logically renamed dir
  989. +- the name of the before-renamed dir
  990. +
  991. +The "detailed info per directory" file is created in aufs rename(2), and
  992. +loaded in any lookup.
  993. +The info is considered in lookup for the matching case only. Here
  994. +"matching" means that the root of branch (in the info filename) is same
  995. +to the current looking-up branch. After looking-up the before-renamed
  996. +name, the inode-number is compared. And the matched dentry is used.
  997. +
  998. +The "inode-number list of directories" is a regular file which contains
  999. +simply the inode-numbers on the branch. The file is created or updated
  1000. +in removing the branch, and loaded in adding the branch. Its lifetime is
  1001. +equal to the branch.
  1002. +The list is refered in lookup, and when the current target inode is
  1003. +found in the list, the aufs tries loading the "detailed info per
  1004. +directory" and get the changed and valid name of the dir.
  1005. +
  1006. +Theoretically these "extra informaiton" may be able to be put into XATTR
  1007. +in the dir inode. But aufs doesn't choose this way because
  1008. +1. XATTR may not be supported by the branch (or its configuration)
  1009. +2. XATTR may have its size limit.
  1010. +3. XATTR may be less easy to convert than a regular file, when the
  1011. + format of the info is changed in the future.
  1012. +At the same time, I agree that the regular file approach is much slower
  1013. +than XATTR approach. So, in the future, aufs may take the XATTR or other
  1014. +better approach.
  1015. +
  1016. +This DIRREN feature is enabled by aufs configuration, and is activated
  1017. +by a new mount option.
  1018. +
  1019. +For the more complicated case, there is a work with UDBA option, which
  1020. +is to dected the direct access to the branches (by-passing aufs) and to
  1021. +maintain the cashes in aufs. Since a single cached aufs dentry may
  1022. +contains two names, before- and after-rename, the name comparision in
  1023. +UDBA handler may not work correctly. In this case, the behaviour will be
  1024. +equivalen to udba=reval case.
  1025. diff -Naur null/Documentation/filesystems/aufs/design/06fhsm.txt linux-5.1/Documentation/filesystems/aufs/design/06fhsm.txt
  1026. --- /dev/null
  1027. +++ linux-5.1/Documentation/filesystems/aufs/design/06fhsm.txt 2019-05-22 11:23:07.436141226 +0200
  1028. @@ -0,0 +1,120 @@
  1029. +
  1030. +# Copyright (C) 2011-2019 Junjiro R. Okajima
  1031. +#
  1032. +# This program is free software; you can redistribute it and/or modify
  1033. +# it under the terms of the GNU General Public License as published by
  1034. +# the Free Software Foundation; either version 2 of the License, or
  1035. +# (at your option) any later version.
  1036. +#
  1037. +# This program is distributed in the hope that it will be useful,
  1038. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1039. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1040. +# GNU General Public License for more details.
  1041. +#
  1042. +# You should have received a copy of the GNU General Public License
  1043. +# along with this program; if not, write to the Free Software
  1044. +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  1045. +
  1046. +
  1047. +File-based Hierarchical Storage Management (FHSM)
  1048. +----------------------------------------------------------------------
  1049. +Hierarchical Storage Management (or HSM) is a well-known feature in the
  1050. +storage world. Aufs provides this feature as file-based with multiple
  1051. +writable branches, based upon the principle of "Colder, the Lower".
  1052. +Here the word "colder" means that the less used files, and "lower" means
  1053. +that the position in the order of the stacked branches vertically.
  1054. +These multiple writable branches are prioritized, ie. the topmost one
  1055. +should be the fastest drive and be used heavily.
  1056. +
  1057. +o Characters in aufs FHSM story
  1058. +- aufs itself and a new branch attribute.
  1059. +- a new ioctl interface to move-down and to establish a connection with
  1060. + the daemon ("move-down" is a converse of "copy-up").
  1061. +- userspace tool and daemon.
  1062. +
  1063. +The userspace daemon establishes a connection with aufs and waits for
  1064. +the notification. The notified information is very similar to struct
  1065. +statfs containing the number of consumed blocks and inodes.
  1066. +When the consumed blocks/inodes of a branch exceeds the user-specified
  1067. +upper watermark, the daemon activates its move-down process until the
  1068. +consumed blocks/inodes reaches the user-specified lower watermark.
  1069. +
  1070. +The actual move-down is done by aufs based upon the request from
  1071. +user-space since we need to maintain the inode number and the internal
  1072. +pointer arrays in aufs.
  1073. +
  1074. +Currently aufs FHSM handles the regular files only. Additionally they
  1075. +must not be hard-linked nor pseudo-linked.
  1076. +
  1077. +
  1078. +o Cowork of aufs and the user-space daemon
  1079. + During the userspace daemon established the connection, aufs sends a
  1080. + small notification to it whenever aufs writes something into the
  1081. + writable branch. But it may cost high since aufs issues statfs(2)
  1082. + internally. So user can specify a new option to cache the
  1083. + info. Actually the notification is controlled by these factors.
  1084. + + the specified cache time.
  1085. + + classified as "force" by aufs internally.
  1086. + Until the specified time expires, aufs doesn't send the info
  1087. + except the forced cases. When aufs decide forcing, the info is always
  1088. + notified to userspace.
  1089. + For example, the number of free inodes is generally large enough and
  1090. + the shortage of it happens rarely. So aufs doesn't force the
  1091. + notification when creating a new file, directory and others. This is
  1092. + the typical case which aufs doesn't force.
  1093. + When aufs writes the actual filedata and the files consumes any of new
  1094. + blocks, the aufs forces notifying.
  1095. +
  1096. +
  1097. +o Interfaces in aufs
  1098. +- New branch attribute.
  1099. + + fhsm
  1100. + Specifies that the branch is managed by FHSM feature. In other word,
  1101. + participant in the FHSM.
  1102. + When nofhsm is set to the branch, it will not be the source/target
  1103. + branch of the move-down operation. This attribute is set
  1104. + independently from coo and moo attributes, and if you want full
  1105. + FHSM, you should specify them as well.
  1106. +- New mount option.
  1107. + + fhsm_sec
  1108. + Specifies a second to suppress many less important info to be
  1109. + notified.
  1110. +- New ioctl.
  1111. + + AUFS_CTL_FHSM_FD
  1112. + create a new file descriptor which userspace can read the notification
  1113. + (a subset of struct statfs) from aufs.
  1114. +- Module parameter 'brs'
  1115. + It has to be set to 1. Otherwise the new mount option 'fhsm' will not
  1116. + be set.
  1117. +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
  1118. + When there are two or more branches with fhsm attributes,
  1119. + /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
  1120. + terminates it. As a result of remounting and branch-manipulation, the
  1121. + number of branches with fhsm attribute can be one. In this case,
  1122. + /sbin/mount.aufs will terminate the user-space daemon.
  1123. +
  1124. +
  1125. +Finally the operation is done as these steps in kernel-space.
  1126. +- make sure that,
  1127. + + no one else is using the file.
  1128. + + the file is not hard-linked.
  1129. + + the file is not pseudo-linked.
  1130. + + the file is a regular file.
  1131. + + the parent dir is not opaqued.
  1132. +- find the target writable branch.
  1133. +- make sure the file is not whiteout-ed by the upper (than the target)
  1134. + branch.
  1135. +- make the parent dir on the target branch.
  1136. +- mutex lock the inode on the branch.
  1137. +- unlink the whiteout on the target branch (if exists).
  1138. +- lookup and create the whiteout-ed temporary name on the target branch.
  1139. +- copy the file as the whiteout-ed temporary name on the target branch.
  1140. +- rename the whiteout-ed temporary name to the original name.
  1141. +- unlink the file on the source branch.
  1142. +- maintain the internal pointer array and the external inode number
  1143. + table (XINO).
  1144. +- maintain the timestamps and other attributes of the parent dir and the
  1145. + file.
  1146. +
  1147. +And of course, in every step, an error may happen. So the operation
  1148. +should restore the original file state after an error happens.
  1149. diff -Naur null/Documentation/filesystems/aufs/design/06mmap.txt linux-5.1/Documentation/filesystems/aufs/design/06mmap.txt
  1150. --- /dev/null
  1151. +++ linux-5.1/Documentation/filesystems/aufs/design/06mmap.txt 2019-05-22 11:23:07.436141226 +0200
  1152. @@ -0,0 +1,72 @@
  1153. +
  1154. +# Copyright (C) 2005-2019 Junjiro R. Okajima
  1155. +#
  1156. +# This program is free software; you can redistribute it and/or modify
  1157. +# it under the terms of the GNU General Public License as published by
  1158. +# the Free Software Foundation; either version 2 of the License, or
  1159. +# (at your option) any later version.
  1160. +#
  1161. +# This program is distributed in the hope that it will be useful,
  1162. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1163. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1164. +# GNU General Public License for more details.
  1165. +#
  1166. +# You should have received a copy of the GNU General Public License
  1167. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  1168. +
  1169. +mmap(2) -- File Memory Mapping
  1170. +----------------------------------------------------------------------
  1171. +In aufs, the file-mapped pages are handled by a branch fs directly, no
  1172. +interaction with aufs. It means aufs_mmap() calls the branch fs's
  1173. +->mmap().
  1174. +This approach is simple and good, but there is one problem.
  1175. +Under /proc, several entries show the mmapped files by its path (with
  1176. +device and inode number), and the printed path will be the path on the
  1177. +branch fs's instead of virtual aufs's.
  1178. +This is not a problem in most cases, but some utilities lsof(1) (and its
  1179. +user) may expect the path on aufs.
  1180. +
  1181. +To address this issue, aufs adds a new member called vm_prfile in struct
  1182. +vm_area_struct (and struct vm_region). The original vm_file points to
  1183. +the file on the branch fs in order to handle everything correctly as
  1184. +usual. The new vm_prfile points to a virtual file in aufs, and the
  1185. +show-functions in procfs refers to vm_prfile if it is set.
  1186. +Also we need to maintain several other places where touching vm_file
  1187. +such like
  1188. +- fork()/clone() copies vma and the reference count of vm_file is
  1189. + incremented.
  1190. +- merging vma maintains the ref count too.
  1191. +
  1192. +This is not a good approach. It just fakes the printed path. But it
  1193. +leaves all behaviour around f_mapping unchanged. This is surely an
  1194. +advantage.
  1195. +Actually aufs had adopted another complicated approach which calls
  1196. +generic_file_mmap() and handles struct vm_operations_struct. In this
  1197. +approach, aufs met a hard problem and I could not solve it without
  1198. +switching the approach.
  1199. +
  1200. +There may be one more another approach which is
  1201. +- bind-mount the branch-root onto the aufs-root internally
  1202. +- grab the new vfsmount (ie. struct mount)
  1203. +- lazy-umount the branch-root internally
  1204. +- in open(2) the aufs-file, open the branch-file with the hidden
  1205. + vfsmount (instead of the original branch's vfsmount)
  1206. +- ideally this "bind-mount and lazy-umount" should be done atomically,
  1207. + but it may be possible from userspace by the mount helper.
  1208. +
  1209. +Adding the internal hidden vfsmount and using it in opening a file, the
  1210. +file path under /proc will be printed correctly. This approach looks
  1211. +smarter, but is not possible I am afraid.
  1212. +- aufs-root may be bind-mount later. when it happens, another hidden
  1213. + vfsmount will be required.
  1214. +- it is hard to get the chance to bind-mount and lazy-umount
  1215. + + in kernel-space, FS can have vfsmount in open(2) via
  1216. + file->f_path, and aufs can know its vfsmount. But several locks are
  1217. + already acquired, and if aufs tries to bind-mount and lazy-umount
  1218. + here, then it may cause a deadlock.
  1219. + + in user-space, bind-mount doesn't invoke the mount helper.
  1220. +- since /proc shows dev and ino, aufs has to give vma these info. it
  1221. + means a new member vm_prinode will be necessary. this is essentially
  1222. + equivalent to vm_prfile described above.
  1223. +
  1224. +I have to give up this "looks-smater" approach.
  1225. diff -Naur null/Documentation/filesystems/aufs/design/06xattr.txt linux-5.1/Documentation/filesystems/aufs/design/06xattr.txt
  1226. --- /dev/null
  1227. +++ linux-5.1/Documentation/filesystems/aufs/design/06xattr.txt 2019-05-22 11:23:07.436141226 +0200
  1228. @@ -0,0 +1,96 @@
  1229. +
  1230. +# Copyright (C) 2014-2019 Junjiro R. Okajima
  1231. +#
  1232. +# This program is free software; you can redistribute it and/or modify
  1233. +# it under the terms of the GNU General Public License as published by
  1234. +# the Free Software Foundation; either version 2 of the License, or
  1235. +# (at your option) any later version.
  1236. +#
  1237. +# This program is distributed in the hope that it will be useful,
  1238. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1239. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1240. +# GNU General Public License for more details.
  1241. +#
  1242. +# You should have received a copy of the GNU General Public License
  1243. +# along with this program; if not, write to the Free Software
  1244. +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  1245. +
  1246. +
  1247. +Listing XATTR/EA and getting the value
  1248. +----------------------------------------------------------------------
  1249. +For the inode standard attributes (owner, group, timestamps, etc.), aufs
  1250. +shows the values from the topmost existing file. This behaviour is good
  1251. +for the non-dir entries since the bahaviour exactly matches the shown
  1252. +information. But for the directories, aufs considers all the same named
  1253. +entries on the lower branches. Which means, if one of the lower entry
  1254. +rejects readdir call, then aufs returns an error even if the topmost
  1255. +entry allows it. This behaviour is necessary to respect the branch fs's
  1256. +security, but can make users confused since the user-visible standard
  1257. +attributes don't match the behaviour.
  1258. +To address this issue, aufs has a mount option called dirperm1 which
  1259. +checks the permission for the topmost entry only, and ignores the lower
  1260. +entry's permission.
  1261. +
  1262. +A similar issue can happen around XATTR.
  1263. +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
  1264. +always set. Otherwise these very unpleasant situation would happen.
  1265. +- listxattr(2) may return the duplicated entries.
  1266. +- users may not be able to remove or reset the XATTR forever,
  1267. +
  1268. +
  1269. +XATTR/EA support in the internal (copy,move)-(up,down)
  1270. +----------------------------------------------------------------------
  1271. +Generally the extended attributes of inode are categorized as these.
  1272. +- "security" for LSM and capability.
  1273. +- "system" for posix ACL, 'acl' mount option is required for the branch
  1274. + fs generally.
  1275. +- "trusted" for userspace, CAP_SYS_ADMIN is required.
  1276. +- "user" for userspace, 'user_xattr' mount option is required for the
  1277. + branch fs generally.
  1278. +
  1279. +Moreover there are some other categories. Aufs handles these rather
  1280. +unpopular categories as the ordinary ones, ie. there is no special
  1281. +condition nor exception.
  1282. +
  1283. +In copy-up, the support for XATTR on the dst branch may differ from the
  1284. +src branch. In this case, the copy-up operation will get an error and
  1285. +the original user operation which triggered the copy-up will fail. It
  1286. +can happen that even all copy-up will fail.
  1287. +When both of src and dst branches support XATTR and if an error occurs
  1288. +during copying XATTR, then the copy-up should fail obviously. That is a
  1289. +good reason and aufs should return an error to userspace. But when only
  1290. +the src branch support that XATTR, aufs should not return an error.
  1291. +For example, the src branch supports ACL but the dst branch doesn't
  1292. +because the dst branch may natively un-support it or temporary
  1293. +un-support it due to "noacl" mount option. Of course, the dst branch fs
  1294. +may NOT return an error even if the XATTR is not supported. It is
  1295. +totally up to the branch fs.
  1296. +
  1297. +Anyway when the aufs internal copy-up gets an error from the dst branch
  1298. +fs, then aufs tries removing the just copied entry and returns the error
  1299. +to the userspace. The worst case of this situation will be all copy-up
  1300. +will fail.
  1301. +
  1302. +For the copy-up operation, there two basic approaches.
  1303. +- copy the specified XATTR only (by category above), and return the
  1304. + error unconditionally if it happens.
  1305. +- copy all XATTR, and ignore the error on the specified category only.
  1306. +
  1307. +In order to support XATTR and to implement the correct behaviour, aufs
  1308. +chooses the latter approach and introduces some new branch attributes,
  1309. +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
  1310. +They correspond to the XATTR namespaces (see above). Additionally, to be
  1311. +convenient, "icex" is also provided which means all "icex*" attributes
  1312. +are set (here the word "icex" stands for "ignore copy-error on XATTR").
  1313. +
  1314. +The meaning of these attributes is to ignore the error from setting
  1315. +XATTR on that branch.
  1316. +Note that aufs tries copying all XATTR unconditionally, and ignores the
  1317. +error from the dst branch according to the specified attributes.
  1318. +
  1319. +Some XATTR may have its default value. The default value may come from
  1320. +the parent dir or the environment. If the default value is set at the
  1321. +file creating-time, it will be overwritten by copy-up.
  1322. +Some contradiction may happen I am afraid.
  1323. +Do we need another attribute to stop copying XATTR? I am unsure. For
  1324. +now, aufs implements the branch attributes to ignore the error.
  1325. diff -Naur null/Documentation/filesystems/aufs/design/07export.txt linux-5.1/Documentation/filesystems/aufs/design/07export.txt
  1326. --- /dev/null
  1327. +++ linux-5.1/Documentation/filesystems/aufs/design/07export.txt 2019-05-22 11:23:07.436141226 +0200
  1328. @@ -0,0 +1,58 @@
  1329. +
  1330. +# Copyright (C) 2005-2019 Junjiro R. Okajima
  1331. +#
  1332. +# This program is free software; you can redistribute it and/or modify
  1333. +# it under the terms of the GNU General Public License as published by
  1334. +# the Free Software Foundation; either version 2 of the License, or
  1335. +# (at your option) any later version.
  1336. +#
  1337. +# This program is distributed in the hope that it will be useful,
  1338. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1339. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1340. +# GNU General Public License for more details.
  1341. +#
  1342. +# You should have received a copy of the GNU General Public License
  1343. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  1344. +
  1345. +Export Aufs via NFS
  1346. +----------------------------------------------------------------------
  1347. +Here is an approach.
  1348. +- like xino/xib, add a new file 'xigen' which stores aufs inode
  1349. + generation.
  1350. +- iget_locked(): initialize aufs inode generation for a new inode, and
  1351. + store it in xigen file.
  1352. +- destroy_inode(): increment aufs inode generation and store it in xigen
  1353. + file. it is necessary even if it is not unlinked, because any data of
  1354. + inode may be changed by UDBA.
  1355. +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
  1356. + build file handle by
  1357. + + branch id (4 bytes)
  1358. + + superblock generation (4 bytes)
  1359. + + inode number (4 or 8 bytes)
  1360. + + parent dir inode number (4 or 8 bytes)
  1361. + + inode generation (4 bytes))
  1362. + + return value of exportfs_encode_fh() for the parent on a branch (4
  1363. + bytes)
  1364. + + file handle for a branch (by exportfs_encode_fh())
  1365. +- fh_to_dentry():
  1366. + + find the index of a branch from its id in handle, and check it is
  1367. + still exist in aufs.
  1368. + + 1st level: get the inode number from handle and search it in cache.
  1369. + + 2nd level: if not found in cache, get the parent inode number from
  1370. + the handle and search it in cache. and then open the found parent
  1371. + dir, find the matching inode number by vfs_readdir() and get its
  1372. + name, and call lookup_one_len() for the target dentry.
  1373. + + 3rd level: if the parent dir is not cached, call
  1374. + exportfs_decode_fh() for a branch and get the parent on a branch,
  1375. + build a pathname of it, convert it a pathname in aufs, call
  1376. + path_lookup(). now aufs gets a parent dir dentry, then handle it as
  1377. + the 2nd level.
  1378. + + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
  1379. + for every branch, but not itself. to get this, (currently) aufs
  1380. + searches in current->nsproxy->mnt_ns list. it may not be a good
  1381. + idea, but I didn't get other approach.
  1382. + + test the generation of the gotten inode.
  1383. +- every inode operation: they may get EBUSY due to UDBA. in this case,
  1384. + convert it into ESTALE for NFSD.
  1385. +- readdir(): call lockdep_on/off() because filldir in NFSD calls
  1386. + lookup_one_len(), vfs_getattr(), encode_fh() and others.
  1387. diff -Naur null/Documentation/filesystems/aufs/design/08shwh.txt linux-5.1/Documentation/filesystems/aufs/design/08shwh.txt
  1388. --- /dev/null
  1389. +++ linux-5.1/Documentation/filesystems/aufs/design/08shwh.txt 2019-05-22 11:23:07.436141226 +0200
  1390. @@ -0,0 +1,52 @@
  1391. +
  1392. +# Copyright (C) 2005-2019 Junjiro R. Okajima
  1393. +#
  1394. +# This program is free software; you can redistribute it and/or modify
  1395. +# it under the terms of the GNU General Public License as published by
  1396. +# the Free Software Foundation; either version 2 of the License, or
  1397. +# (at your option) any later version.
  1398. +#
  1399. +# This program is distributed in the hope that it will be useful,
  1400. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1401. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1402. +# GNU General Public License for more details.
  1403. +#
  1404. +# You should have received a copy of the GNU General Public License
  1405. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  1406. +
  1407. +Show Whiteout Mode (shwh)
  1408. +----------------------------------------------------------------------
  1409. +Generally aufs hides the name of whiteouts. But in some cases, to show
  1410. +them is very useful for users. For instance, creating a new middle layer
  1411. +(branch) by merging existing layers.
  1412. +
  1413. +(borrowing aufs1 HOW-TO from a user, Michael Towers)
  1414. +When you have three branches,
  1415. +- Bottom: 'system', squashfs (underlying base system), read-only
  1416. +- Middle: 'mods', squashfs, read-only
  1417. +- Top: 'overlay', ram (tmpfs), read-write
  1418. +
  1419. +The top layer is loaded at boot time and saved at shutdown, to preserve
  1420. +the changes made to the system during the session.
  1421. +When larger changes have been made, or smaller changes have accumulated,
  1422. +the size of the saved top layer data grows. At this point, it would be
  1423. +nice to be able to merge the two overlay branches ('mods' and 'overlay')
  1424. +and rewrite the 'mods' squashfs, clearing the top layer and thus
  1425. +restoring save and load speed.
  1426. +
  1427. +This merging is simplified by the use of another aufs mount, of just the
  1428. +two overlay branches using the 'shwh' option.
  1429. +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
  1430. + aufs /livesys/merge_union
  1431. +
  1432. +A merged view of these two branches is then available at
  1433. +/livesys/merge_union, and the new feature is that the whiteouts are
  1434. +visible!
  1435. +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
  1436. +writing to all branches. Also the default mode for all branches is 'ro'.
  1437. +It is now possible to save the combined contents of the two overlay
  1438. +branches to a new squashfs, e.g.:
  1439. +# mksquashfs /livesys/merge_union /path/to/newmods.squash
  1440. +
  1441. +This new squashfs archive can be stored on the boot device and the
  1442. +initramfs will use it to replace the old one at the next boot.
  1443. diff -Naur null/Documentation/filesystems/aufs/design/10dynop.txt linux-5.1/Documentation/filesystems/aufs/design/10dynop.txt
  1444. --- /dev/null
  1445. +++ linux-5.1/Documentation/filesystems/aufs/design/10dynop.txt 2019-05-22 11:23:07.436141226 +0200
  1446. @@ -0,0 +1,47 @@
  1447. +
  1448. +# Copyright (C) 2010-2019 Junjiro R. Okajima
  1449. +#
  1450. +# This program is free software; you can redistribute it and/or modify
  1451. +# it under the terms of the GNU General Public License as published by
  1452. +# the Free Software Foundation; either version 2 of the License, or
  1453. +# (at your option) any later version.
  1454. +#
  1455. +# This program is distributed in the hope that it will be useful,
  1456. +# but WITHOUT ANY WARRANTY; without even the implied warranty of
  1457. +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1458. +# GNU General Public License for more details.
  1459. +#
  1460. +# You should have received a copy of the GNU General Public License
  1461. +# along with this program. If not, see <http://www.gnu.org/licenses/>.
  1462. +
  1463. +Dynamically customizable FS operations
  1464. +----------------------------------------------------------------------
  1465. +Generally FS operations (struct inode_operations, struct
  1466. +address_space_operations, struct file_operations, etc.) are defined as
  1467. +"static const", but it never means that FS have only one set of
  1468. +operation. Some FS have multiple sets of them. For instance, ext2 has
  1469. +three sets, one for XIP, for NOBH, and for normal.
  1470. +Since aufs overrides and redirects these operations, sometimes aufs has
  1471. +to change its behaviour according to the branch FS type. More importantly
  1472. +VFS acts differently if a function (member in the struct) is set or
  1473. +not. It means aufs should have several sets of operations and select one
  1474. +among them according to the branch FS definition.
  1475. +
  1476. +In order to solve this problem and not to affect the behaviour of VFS,
  1477. +aufs defines these operations dynamically. For instance, aufs defines
  1478. +dummy direct_IO function for struct address_space_operations, but it may
  1479. +not be set to the address_space_operations actually. When the branch FS
  1480. +doesn't have it, aufs doesn't set it to its address_space_operations
  1481. +while the function definition itself is still alive. So the behaviour
  1482. +itself will not change, and it will return an error when direct_IO is
  1483. +not set.
  1484. +
  1485. +The lifetime of these dynamically generated operation object is
  1486. +maintained by aufs branch object. When the branch is removed from aufs,
  1487. +the reference counter of the object is decremented. When it reaches
  1488. +zero, the dynamically generated operation object will be freed.
  1489. +
  1490. +This approach is designed to support AIO (io_submit), Direct I/O and
  1491. +XIP (DAX) mainly.
  1492. +Currently this approach is applied to address_space_operations for
  1493. +regular files only.
  1494. diff -Naur null/Documentation/filesystems/aufs/README linux-5.1/Documentation/filesystems/aufs/README
  1495. --- /dev/null
  1496. +++ linux-5.1/Documentation/filesystems/aufs/README 2019-05-22 11:23:07.436141226 +0200
  1497. @@ -0,0 +1,392 @@
  1498. +
  1499. +Aufs5 -- advanced multi layered unification filesystem version 5.x
  1500. +http://aufs.sf.net
  1501. +Junjiro R. Okajima
  1502. +
  1503. +
  1504. +0. Introduction
  1505. +----------------------------------------
  1506. +In the early days, aufs was entirely re-designed and re-implemented
  1507. +Unionfs Version 1.x series. Adding many original ideas, approaches,
  1508. +improvements and implementations, it became totally different from
  1509. +Unionfs while keeping the basic features.
  1510. +Later, Unionfs Version 2.x series began taking some of the same
  1511. +approaches to aufs1's.
  1512. +Unionfs was being developed by Professor Erez Zadok at Stony Brook
  1513. +University and his team.
  1514. +
  1515. +Aufs5 supports linux-v5.0 and later, If you want older kernel version
  1516. +support,
  1517. +- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
  1518. +- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
  1519. +- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
  1520. + or aufs1 from CVS on SourceForge.
  1521. +
  1522. +Note: it becomes clear that "Aufs was rejected. Let's give it up."
  1523. + According to Christoph Hellwig, linux rejects all union-type
  1524. + filesystems but UnionMount.
  1525. +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
  1526. +
  1527. +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
  1528. + UnionMount, and he pointed out an issue around a directory mutex
  1529. + lock and aufs addressed it. But it is still unsure whether aufs will
  1530. + be merged (or any other union solution).
  1531. +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
  1532. +
  1533. +
  1534. +1. Features
  1535. +----------------------------------------
  1536. +- unite several directories into a single virtual filesystem. The member
  1537. + directory is called as a branch.
  1538. +- you can specify the permission flags to the branch, which are 'readonly',
  1539. + 'readwrite' and 'whiteout-able.'
  1540. +- by upper writable branch, internal copyup and whiteout, files/dirs on
  1541. + readonly branch are modifiable logically.
  1542. +- dynamic branch manipulation, add, del.
  1543. +- etc...
  1544. +
  1545. +Also there are many enhancements in aufs, such as:
  1546. +- test only the highest one for the directory permission (dirperm1)
  1547. +- copyup on open (coo=)
  1548. +- 'move' policy for copy-up between two writable branches, after
  1549. + checking free space.
  1550. +- xattr, acl
  1551. +- readdir(3) in userspace.
  1552. +- keep inode number by external inode number table
  1553. +- keep the timestamps of file/dir in internal copyup operation
  1554. +- seekable directory, supporting NFS readdir.
  1555. +- whiteout is hardlinked in order to reduce the consumption of inodes
  1556. + on branch
  1557. +- do not copyup, nor create a whiteout when it is unnecessary
  1558. +- revert a single systemcall when an error occurs in aufs
  1559. +- remount interface instead of ioctl
  1560. +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
  1561. +- loopback mounted filesystem as a branch
  1562. +- kernel thread for removing the dir who has a plenty of whiteouts
  1563. +- support copyup sparse file (a file which has a 'hole' in it)
  1564. +- default permission flags for branches
  1565. +- selectable permission flags for ro branch, whether whiteout can
  1566. + exist or not
  1567. +- export via NFS.
  1568. +- support <sysfs>/fs/aufs and <debugfs>/aufs.
  1569. +- support multiple writable branches, some policies to select one
  1570. + among multiple writable branches.
  1571. +- a new semantics for link(2) and rename(2) to support multiple
  1572. + writable branches.
  1573. +- no glibc changes are required.
  1574. +- pseudo hardlink (hardlink over branches)
  1575. +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
  1576. + including NFS or remote filesystem branch.
  1577. +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
  1578. +- and more...
  1579. +
  1580. +Currently these features are dropped temporary from aufs5.
  1581. +See design/08plan.txt in detail.
  1582. +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
  1583. + (robr)
  1584. +- statistics of aufs thread (/sys/fs/aufs/stat)
  1585. +
  1586. +Features or just an idea in the future (see also design/*.txt),
  1587. +- reorder the branch index without del/re-add.
  1588. +- permanent xino files for NFSD
  1589. +- an option for refreshing the opened files after add/del branches
  1590. +- light version, without branch manipulation. (unnecessary?)
  1591. +- copyup in userspace
  1592. +- inotify in userspace
  1593. +- readv/writev
  1594. +
  1595. +
  1596. +2. Download
  1597. +----------------------------------------
  1598. +There are three GIT trees for aufs5, aufs5-linux.git,
  1599. +aufs5-standalone.git, and aufs-util.git. Note that there is no "5" in
  1600. +"aufs-util.git."
  1601. +While the aufs-util is always necessary, you need either of aufs5-linux
  1602. +or aufs5-standalone.
  1603. +
  1604. +The aufs5-linux tree includes the whole linux mainline GIT tree,
  1605. +git://git.kernel.org/.../torvalds/linux.git.
  1606. +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
  1607. +build aufs5 as an external kernel module.
  1608. +Several extra patches are not included in this tree. Only
  1609. +aufs5-standalone tree contains them. They are described in the later
  1610. +section "Configuration and Compilation."
  1611. +
  1612. +On the other hand, the aufs5-standalone tree has only aufs source files
  1613. +and necessary patches, and you can select CONFIG_AUFS_FS=m.
  1614. +But you need to apply all aufs patches manually.
  1615. +
  1616. +You will find GIT branches whose name is in form of "aufs5.x" where "x"
  1617. +represents the linux kernel version, "linux-5.x". For instance,
  1618. +"aufs5.0" is for linux-5.0. For latest "linux-5.x-rcN", use
  1619. +"aufs5.x-rcN" branch.
  1620. +
  1621. +o aufs5-linux tree
  1622. +$ git clone --reference /your/linux/git/tree \
  1623. + git://github.com/sfjro/aufs5-linux.git aufs5-linux.git
  1624. +- if you don't have linux GIT tree, then remove "--reference ..."
  1625. +$ cd aufs5-linux.git
  1626. +$ git checkout origin/aufs5.0
  1627. +
  1628. +Or You may want to directly git-pull aufs into your linux GIT tree, and
  1629. +leave the patch-work to GIT.
  1630. +$ cd /your/linux/git/tree
  1631. +$ git remote add aufs5 git://github.com/sfjro/aufs5-linux.git
  1632. +$ git fetch aufs5
  1633. +$ git checkout -b my5.0 v5.0
  1634. +$ (add your local change...)
  1635. +$ git pull aufs5 aufs5.0
  1636. +- now you have v5.0 + your_changes + aufs5.0 in you my5.0 branch.
  1637. +- you may need to solve some conflicts between your_changes and
  1638. + aufs5.0. in this case, git-rerere is recommended so that you can
  1639. + solve the similar conflicts automatically when you upgrade to 5.1 or
  1640. + later in the future.
  1641. +
  1642. +o aufs5-standalone tree
  1643. +$ git clone git://github.com/sfjro/aufs5-standalone.git aufs5-standalone.git
  1644. +$ cd aufs5-standalone.git
  1645. +$ git checkout origin/aufs5.0
  1646. +
  1647. +o aufs-util tree
  1648. +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
  1649. +- note that the public aufs-util.git is on SourceForge instead of
  1650. + GitHUB.
  1651. +$ cd aufs-util.git
  1652. +$ git checkout origin/aufs5.0
  1653. +
  1654. +Note: The 5.x-rcN branch is to be used with `rc' kernel versions ONLY.
  1655. +The minor version number, 'x' in '5.x', of aufs may not always
  1656. +follow the minor version number of the kernel.
  1657. +Because changes in the kernel that cause the use of a new
  1658. +minor version number do not always require changes to aufs-util.
  1659. +
  1660. +Since aufs-util has its own minor version number, you may not be
  1661. +able to find a GIT branch in aufs-util for your kernel's
  1662. +exact minor version number.
  1663. +In this case, you should git-checkout the branch for the
  1664. +nearest lower number.
  1665. +
  1666. +For (an unreleased) example:
  1667. +If you are using "linux-5.10" and the "aufs5.10" branch
  1668. +does not exist in aufs-util repository, then "aufs5.9", "aufs5.8"
  1669. +or something numerically smaller is the branch for your kernel.
  1670. +
  1671. +Also you can view all branches by
  1672. + $ git branch -a
  1673. +
  1674. +
  1675. +3. Configuration and Compilation
  1676. +----------------------------------------
  1677. +Make sure you have git-checkout'ed the correct branch.
  1678. +
  1679. +For aufs5-linux tree,
  1680. +- enable CONFIG_AUFS_FS.
  1681. +- set other aufs configurations if necessary.
  1682. +
  1683. +For aufs5-standalone tree,
  1684. +There are several ways to build.
  1685. +
  1686. +1.
  1687. +- apply ./aufs5-kbuild.patch to your kernel source files.
  1688. +- apply ./aufs5-base.patch too.
  1689. +- apply ./aufs5-mmap.patch too.
  1690. +- apply ./aufs5-standalone.patch too, if you have a plan to set
  1691. + CONFIG_AUFS_FS=m. otherwise you don't need ./aufs5-standalone.patch.
  1692. +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
  1693. + kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
  1694. +- enable CONFIG_AUFS_FS, you can select either
  1695. + =m or =y.
  1696. +- and build your kernel as usual.
  1697. +- install the built kernel.
  1698. +- install the header files too by "make headers_install" to the
  1699. + directory where you specify. By default, it is $PWD/usr.
  1700. + "make help" shows a brief note for headers_install.
  1701. +- and reboot your system.
  1702. +
  1703. +2.
  1704. +- module only (CONFIG_AUFS_FS=m).
  1705. +- apply ./aufs5-base.patch to your kernel source files.
  1706. +- apply ./aufs5-mmap.patch too.
  1707. +- apply ./aufs5-standalone.patch too.
  1708. +- build your kernel, don't forget "make headers_install", and reboot.
  1709. +- edit ./config.mk and set other aufs configurations if necessary.
  1710. + Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
  1711. + every aufs configurations.
  1712. +- build the module by simple "make".
  1713. +- you can specify ${KDIR} make variable which points to your kernel
  1714. + source tree.
  1715. +- install the files
  1716. + + run "make install" to install the aufs module, or copy the built
  1717. + $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
  1718. + + run "make install_headers" (instead of headers_install) to install
  1719. + the modified aufs header file (you can specify DESTDIR which is
  1720. + available in aufs standalone version's Makefile only), or copy
  1721. + $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
  1722. + you like manually. By default, the target directory is $PWD/usr.
  1723. +- no need to apply aufs5-kbuild.patch, nor copying source files to your
  1724. + kernel source tree.
  1725. +
  1726. +Note: The header file aufs_type.h is necessary to build aufs-util
  1727. + as well as "make headers_install" in the kernel source tree.
  1728. + headers_install is subject to be forgotten, but it is essentially
  1729. + necessary, not only for building aufs-util.
  1730. + You may not meet problems without headers_install in some older
  1731. + version though.
  1732. +
  1733. +And then,
  1734. +- read README in aufs-util, build and install it
  1735. +- note that your distribution may contain an obsoleted version of
  1736. + aufs_type.h in /usr/include/linux or something. When you build aufs
  1737. + utilities, make sure that your compiler refers the correct aufs header
  1738. + file which is built by "make headers_install."
  1739. +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
  1740. + then run "make install_ulib" too. And refer to the aufs manual in
  1741. + detail.
  1742. +
  1743. +There several other patches in aufs5-standalone.git. They are all
  1744. +optional. When you meet some problems, they will help you.
  1745. +- aufs5-loopback.patch
  1746. + Supports a nested loopback mount in a branch-fs. This patch is
  1747. + unnecessary until aufs produces a message like "you may want to try
  1748. + another patch for loopback file".
  1749. +- vfs-ino.patch
  1750. + Modifies a system global kernel internal function get_next_ino() in
  1751. + order to stop assigning 0 for an inode-number. Not directly related to
  1752. + aufs, but recommended generally.
  1753. +- tmpfs-idr.patch
  1754. + Keeps the tmpfs inode number as the lowest value. Effective to reduce
  1755. + the size of aufs XINO files for tmpfs branch. Also it prevents the
  1756. + duplication of inode number, which is important for backup tools and
  1757. + other utilities. When you find aufs XINO files for tmpfs branch
  1758. + growing too much, try this patch.
  1759. +- lockdep-debug.patch
  1760. + Because aufs is not only an ordinary filesystem (callee of VFS), but
  1761. + also a caller of VFS functions for branch filesystems, subclassing of
  1762. + the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
  1763. + feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
  1764. + need to apply this debug patch to expand several constant values.
  1765. + If don't know what LOCKDEP is, then you don't have apply this patch.
  1766. +
  1767. +
  1768. +4. Usage
  1769. +----------------------------------------
  1770. +At first, make sure aufs-util are installed, and please read the aufs
  1771. +manual, aufs.5 in aufs-util.git tree.
  1772. +$ man -l aufs.5
  1773. +
  1774. +And then,
  1775. +$ mkdir /tmp/rw /tmp/aufs
  1776. +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
  1777. +
  1778. +Here is another example. The result is equivalent.
  1779. +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
  1780. + Or
  1781. +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
  1782. +# mount -o remount,append:${HOME} /tmp/aufs
  1783. +
  1784. +Then, you can see whole tree of your home dir through /tmp/aufs. If
  1785. +you modify a file under /tmp/aufs, the one on your home directory is
  1786. +not affected, instead the same named file will be newly created under
  1787. +/tmp/rw. And all of your modification to a file will be applied to
  1788. +the one under /tmp/rw. This is called the file based Copy on Write
  1789. +(COW) method.
  1790. +Aufs mount options are described in aufs.5.
  1791. +If you run chroot or something and make your aufs as a root directory,
  1792. +then you need to customize the shutdown script. See the aufs manual in
  1793. +detail.
  1794. +
  1795. +Additionally, there are some sample usages of aufs which are a
  1796. +diskless system with network booting, and LiveCD over NFS.
  1797. +See sample dir in CVS tree on SourceForge.
  1798. +
  1799. +
  1800. +5. Contact
  1801. +----------------------------------------
  1802. +When you have any problems or strange behaviour in aufs, please let me
  1803. +know with:
  1804. +- /proc/mounts (instead of the output of mount(8))
  1805. +- /sys/module/aufs/*
  1806. +- /sys/fs/aufs/* (if you have them)
  1807. +- /debug/aufs/* (if you have them)
  1808. +- linux kernel version
  1809. + if your kernel is not plain, for example modified by distributor,
  1810. + the url where i can download its source is necessary too.
  1811. +- aufs version which was printed at loading the module or booting the
  1812. + system, instead of the date you downloaded.
  1813. +- configuration (define/undefine CONFIG_AUFS_xxx)
  1814. +- kernel configuration or /proc/config.gz (if you have it)
  1815. +- behaviour which you think to be incorrect
  1816. +- actual operation, reproducible one is better
  1817. +- mailto: aufs-users at lists.sourceforge.net
  1818. +
  1819. +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
  1820. +and Feature Requests) on SourceForge. Please join and write to
  1821. +aufs-users ML.
  1822. +
  1823. +
  1824. +6. Acknowledgements
  1825. +----------------------------------------
  1826. +Thanks to everyone who have tried and are using aufs, whoever
  1827. +have reported a bug or any feedback.
  1828. +
  1829. +Especially donators:
  1830. +Tomas Matejicek(slax.org) made a donation (much more than once).
  1831. + Since Apr 2010, Tomas M (the author of Slax and Linux Live
  1832. + scripts) is making "doubling" donations.
  1833. + Unfortunately I cannot list all of the donators, but I really
  1834. + appreciate.
  1835. + It ends Aug 2010, but the ordinary donation URL is still available.
  1836. + <http://sourceforge.net/donate/index.php?group_id=167503>
  1837. +Dai Itasaka made a donation (2007/8).
  1838. +Chuck Smith made a donation (2008/4, 10 and 12).
  1839. +Henk Schoneveld made a donation (2008/9).
  1840. +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
  1841. +Francois Dupoux made a donation (2008/11).
  1842. +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
  1843. + aufs2 GIT tree (2009/2).
  1844. +William Grant made a donation (2009/3).
  1845. +Patrick Lane made a donation (2009/4).
  1846. +The Mail Archive (mail-archive.com) made donations (2009/5).
  1847. +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
  1848. +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
  1849. +Pavel Pronskiy made a donation (2011/2).
  1850. +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
  1851. + Networks (Ed Wildgoose) made a donation for hardware (2011/3).
  1852. +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
  1853. +11).
  1854. +Sam Liddicott made a donation (2011/9).
  1855. +Era Scarecrow made a donation (2013/4).
  1856. +Bor Ratajc made a donation (2013/4).
  1857. +Alessandro Gorreta made a donation (2013/4).
  1858. +POIRETTE Marc made a donation (2013/4).
  1859. +Alessandro Gorreta made a donation (2013/4).
  1860. +lauri kasvandik made a donation (2013/5).
  1861. +"pemasu from Finland" made a donation (2013/7).
  1862. +The Parted Magic Project made a donation (2013/9 and 11).
  1863. +Pavel Barta made a donation (2013/10).
  1864. +Nikolay Pertsev made a donation (2014/5).
  1865. +James B made a donation (2014/7 and 2015/7).
  1866. +Stefano Di Biase made a donation (2014/8).
  1867. +Daniel Epellei made a donation (2015/1).
  1868. +OmegaPhil made a donation (2016/1, 2018/4).
  1869. +Tomasz Szewczyk made a donation (2016/4).
  1870. +James Burry made a donation (2016/12).
  1871. +Carsten Rose made a donation (2018/9).
  1872. +Porteus Kiosk made a donation (2018/10).
  1873. +
  1874. +Thank you very much.
  1875. +Donations are always, including future donations, very important and
  1876. +helpful for me to keep on developing aufs.
  1877. +
  1878. +
  1879. +7.
  1880. +----------------------------------------
  1881. +If you are an experienced user, no explanation is needed. Aufs is
  1882. +just a linux filesystem.
  1883. +
  1884. +
  1885. +Enjoy!
  1886. +
  1887. +# Local variables: ;
  1888. +# mode: text;
  1889. +# End: ;
  1890. diff -Naur null/fs/aufs/aufs.h linux-5.1/fs/aufs/aufs.h
  1891. --- /dev/null
  1892. +++ linux-5.1/fs/aufs/aufs.h 2019-05-22 11:23:07.438141134 +0200
  1893. @@ -0,0 +1,62 @@
  1894. +/* SPDX-License-Identifier: GPL-2.0 */
  1895. +/*
  1896. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  1897. + *
  1898. + * This program, aufs is free software; you can redistribute it and/or modify
  1899. + * it under the terms of the GNU General Public License as published by
  1900. + * the Free Software Foundation; either version 2 of the License, or
  1901. + * (at your option) any later version.
  1902. + *
  1903. + * This program is distributed in the hope that it will be useful,
  1904. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1905. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1906. + * GNU General Public License for more details.
  1907. + *
  1908. + * You should have received a copy of the GNU General Public License
  1909. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  1910. + */
  1911. +
  1912. +/*
  1913. + * all header files
  1914. + */
  1915. +
  1916. +#ifndef __AUFS_H__
  1917. +#define __AUFS_H__
  1918. +
  1919. +#ifdef __KERNEL__
  1920. +
  1921. +#define AuStub(type, name, body, ...) \
  1922. + static inline type name(__VA_ARGS__) { body; }
  1923. +
  1924. +#define AuStubVoid(name, ...) \
  1925. + AuStub(void, name, , __VA_ARGS__)
  1926. +#define AuStubInt0(name, ...) \
  1927. + AuStub(int, name, return 0, __VA_ARGS__)
  1928. +
  1929. +#include "debug.h"
  1930. +
  1931. +#include "branch.h"
  1932. +#include "cpup.h"
  1933. +#include "dcsub.h"
  1934. +#include "dbgaufs.h"
  1935. +#include "dentry.h"
  1936. +#include "dir.h"
  1937. +#include "dirren.h"
  1938. +#include "dynop.h"
  1939. +#include "file.h"
  1940. +#include "fstype.h"
  1941. +#include "hbl.h"
  1942. +#include "inode.h"
  1943. +#include "lcnt.h"
  1944. +#include "loop.h"
  1945. +#include "module.h"
  1946. +#include "opts.h"
  1947. +#include "rwsem.h"
  1948. +#include "super.h"
  1949. +#include "sysaufs.h"
  1950. +#include "vfsub.h"
  1951. +#include "whout.h"
  1952. +#include "wkq.h"
  1953. +
  1954. +#endif /* __KERNEL__ */
  1955. +#endif /* __AUFS_H__ */
  1956. diff -Naur null/fs/aufs/branch.c linux-5.1/fs/aufs/branch.c
  1957. --- /dev/null
  1958. +++ linux-5.1/fs/aufs/branch.c 2019-05-22 11:23:07.438141134 +0200
  1959. @@ -0,0 +1,1423 @@
  1960. +// SPDX-License-Identifier: GPL-2.0
  1961. +/*
  1962. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  1963. + *
  1964. + * This program, aufs is free software; you can redistribute it and/or modify
  1965. + * it under the terms of the GNU General Public License as published by
  1966. + * the Free Software Foundation; either version 2 of the License, or
  1967. + * (at your option) any later version.
  1968. + *
  1969. + * This program is distributed in the hope that it will be useful,
  1970. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1971. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1972. + * GNU General Public License for more details.
  1973. + *
  1974. + * You should have received a copy of the GNU General Public License
  1975. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  1976. + */
  1977. +
  1978. +/*
  1979. + * branch management
  1980. + */
  1981. +
  1982. +#include <linux/compat.h>
  1983. +#include <linux/statfs.h>
  1984. +#include "aufs.h"
  1985. +
  1986. +/*
  1987. + * free a single branch
  1988. + */
  1989. +static void au_br_do_free(struct au_branch *br)
  1990. +{
  1991. + int i;
  1992. + struct au_wbr *wbr;
  1993. + struct au_dykey **key;
  1994. +
  1995. + au_hnotify_fin_br(br);
  1996. + /* always, regardless the mount option */
  1997. + au_dr_hino_free(&br->br_dirren);
  1998. + au_xino_put(br);
  1999. +
  2000. + AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
  2001. + au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
  2002. + AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
  2003. + au_lcnt_fin(&br->br_count, /*do_sync*/0);
  2004. +
  2005. + wbr = br->br_wbr;
  2006. + if (wbr) {
  2007. + for (i = 0; i < AuBrWh_Last; i++)
  2008. + dput(wbr->wbr_wh[i]);
  2009. + AuDebugOn(atomic_read(&wbr->wbr_wh_running));
  2010. + AuRwDestroy(&wbr->wbr_wh_rwsem);
  2011. + }
  2012. +
  2013. + if (br->br_fhsm) {
  2014. + au_br_fhsm_fin(br->br_fhsm);
  2015. + au_kfree_try_rcu(br->br_fhsm);
  2016. + }
  2017. +
  2018. + key = br->br_dykey;
  2019. + for (i = 0; i < AuBrDynOp; i++, key++)
  2020. + if (*key)
  2021. + au_dy_put(*key);
  2022. + else
  2023. + break;
  2024. +
  2025. + /* recursive lock, s_umount of branch's */
  2026. + /* synchronize_rcu(); */ /* why? */
  2027. + lockdep_off();
  2028. + path_put(&br->br_path);
  2029. + lockdep_on();
  2030. + au_kfree_rcu(wbr);
  2031. + au_lcnt_wait_for_fin(&br->br_nfiles);
  2032. + au_lcnt_wait_for_fin(&br->br_count);
  2033. + /* I don't know why, but percpu_refcount requires this */
  2034. + /* synchronize_rcu(); */
  2035. + au_kfree_rcu(br);
  2036. +}
  2037. +
  2038. +/*
  2039. + * frees all branches
  2040. + */
  2041. +void au_br_free(struct au_sbinfo *sbinfo)
  2042. +{
  2043. + aufs_bindex_t bmax;
  2044. + struct au_branch **br;
  2045. +
  2046. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  2047. +
  2048. + bmax = sbinfo->si_bbot + 1;
  2049. + br = sbinfo->si_branch;
  2050. + while (bmax--)
  2051. + au_br_do_free(*br++);
  2052. +}
  2053. +
  2054. +/*
  2055. + * find the index of a branch which is specified by @br_id.
  2056. + */
  2057. +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
  2058. +{
  2059. + aufs_bindex_t bindex, bbot;
  2060. +
  2061. + bbot = au_sbbot(sb);
  2062. + for (bindex = 0; bindex <= bbot; bindex++)
  2063. + if (au_sbr_id(sb, bindex) == br_id)
  2064. + return bindex;
  2065. + return -1;
  2066. +}
  2067. +
  2068. +/* ---------------------------------------------------------------------- */
  2069. +
  2070. +/*
  2071. + * add a branch
  2072. + */
  2073. +
  2074. +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
  2075. + struct dentry *h_root)
  2076. +{
  2077. + if (unlikely(h_adding == h_root
  2078. + || au_test_loopback_overlap(sb, h_adding)))
  2079. + return 1;
  2080. + if (h_adding->d_sb != h_root->d_sb)
  2081. + return 0;
  2082. + return au_test_subdir(h_adding, h_root)
  2083. + || au_test_subdir(h_root, h_adding);
  2084. +}
  2085. +
  2086. +/*
  2087. + * returns a newly allocated branch. @new_nbranch is a number of branches
  2088. + * after adding a branch.
  2089. + */
  2090. +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
  2091. + int perm)
  2092. +{
  2093. + struct au_branch *add_branch;
  2094. + struct dentry *root;
  2095. + struct inode *inode;
  2096. + int err;
  2097. +
  2098. + err = -ENOMEM;
  2099. + add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
  2100. + if (unlikely(!add_branch))
  2101. + goto out;
  2102. + add_branch->br_xino = au_xino_alloc(/*nfile*/1);
  2103. + if (unlikely(!add_branch->br_xino))
  2104. + goto out_br;
  2105. + err = au_hnotify_init_br(add_branch, perm);
  2106. + if (unlikely(err))
  2107. + goto out_xino;
  2108. +
  2109. + if (au_br_writable(perm)) {
  2110. + /* may be freed separately at changing the branch permission */
  2111. + add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
  2112. + GFP_NOFS);
  2113. + if (unlikely(!add_branch->br_wbr))
  2114. + goto out_hnotify;
  2115. + }
  2116. +
  2117. + if (au_br_fhsm(perm)) {
  2118. + err = au_fhsm_br_alloc(add_branch);
  2119. + if (unlikely(err))
  2120. + goto out_wbr;
  2121. + }
  2122. +
  2123. + root = sb->s_root;
  2124. + err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
  2125. + if (!err)
  2126. + err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
  2127. + if (!err) {
  2128. + inode = d_inode(root);
  2129. + err = au_hinode_realloc(au_ii(inode), new_nbranch,
  2130. + /*may_shrink*/0);
  2131. + }
  2132. + if (!err)
  2133. + return add_branch; /* success */
  2134. +
  2135. +out_wbr:
  2136. + au_kfree_rcu(add_branch->br_wbr);
  2137. +out_hnotify:
  2138. + au_hnotify_fin_br(add_branch);
  2139. +out_xino:
  2140. + au_xino_put(add_branch);
  2141. +out_br:
  2142. + au_kfree_rcu(add_branch);
  2143. +out:
  2144. + return ERR_PTR(err);
  2145. +}
  2146. +
  2147. +/*
  2148. + * test if the branch permission is legal or not.
  2149. + */
  2150. +static int test_br(struct inode *inode, int brperm, char *path)
  2151. +{
  2152. + int err;
  2153. +
  2154. + err = (au_br_writable(brperm) && IS_RDONLY(inode));
  2155. + if (!err)
  2156. + goto out;
  2157. +
  2158. + err = -EINVAL;
  2159. + pr_err("write permission for readonly mount or inode, %s\n", path);
  2160. +
  2161. +out:
  2162. + return err;
  2163. +}
  2164. +
  2165. +/*
  2166. + * returns:
  2167. + * 0: success, the caller will add it
  2168. + * plus: success, it is already unified, the caller should ignore it
  2169. + * minus: error
  2170. + */
  2171. +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
  2172. +{
  2173. + int err;
  2174. + aufs_bindex_t bbot, bindex;
  2175. + struct dentry *root, *h_dentry;
  2176. + struct inode *inode, *h_inode;
  2177. +
  2178. + root = sb->s_root;
  2179. + bbot = au_sbbot(sb);
  2180. + if (unlikely(bbot >= 0
  2181. + && au_find_dbindex(root, add->path.dentry) >= 0)) {
  2182. + err = 1;
  2183. + if (!remount) {
  2184. + err = -EINVAL;
  2185. + pr_err("%s duplicated\n", add->pathname);
  2186. + }
  2187. + goto out;
  2188. + }
  2189. +
  2190. + err = -ENOSPC; /* -E2BIG; */
  2191. + if (unlikely(AUFS_BRANCH_MAX <= add->bindex
  2192. + || AUFS_BRANCH_MAX - 1 <= bbot)) {
  2193. + pr_err("number of branches exceeded %s\n", add->pathname);
  2194. + goto out;
  2195. + }
  2196. +
  2197. + err = -EDOM;
  2198. + if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
  2199. + pr_err("bad index %d\n", add->bindex);
  2200. + goto out;
  2201. + }
  2202. +
  2203. + inode = d_inode(add->path.dentry);
  2204. + err = -ENOENT;
  2205. + if (unlikely(!inode->i_nlink)) {
  2206. + pr_err("no existence %s\n", add->pathname);
  2207. + goto out;
  2208. + }
  2209. +
  2210. + err = -EINVAL;
  2211. + if (unlikely(inode->i_sb == sb)) {
  2212. + pr_err("%s must be outside\n", add->pathname);
  2213. + goto out;
  2214. + }
  2215. +
  2216. + if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
  2217. + pr_err("unsupported filesystem, %s (%s)\n",
  2218. + add->pathname, au_sbtype(inode->i_sb));
  2219. + goto out;
  2220. + }
  2221. +
  2222. + if (unlikely(inode->i_sb->s_stack_depth)) {
  2223. + pr_err("already stacked, %s (%s)\n",
  2224. + add->pathname, au_sbtype(inode->i_sb));
  2225. + goto out;
  2226. + }
  2227. +
  2228. + err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
  2229. + if (unlikely(err))
  2230. + goto out;
  2231. +
  2232. + if (bbot < 0)
  2233. + return 0; /* success */
  2234. +
  2235. + err = -EINVAL;
  2236. + for (bindex = 0; bindex <= bbot; bindex++)
  2237. + if (unlikely(test_overlap(sb, add->path.dentry,
  2238. + au_h_dptr(root, bindex)))) {
  2239. + pr_err("%s is overlapped\n", add->pathname);
  2240. + goto out;
  2241. + }
  2242. +
  2243. + err = 0;
  2244. + if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
  2245. + h_dentry = au_h_dptr(root, 0);
  2246. + h_inode = d_inode(h_dentry);
  2247. + if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
  2248. + || !uid_eq(h_inode->i_uid, inode->i_uid)
  2249. + || !gid_eq(h_inode->i_gid, inode->i_gid))
  2250. + pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
  2251. + add->pathname,
  2252. + i_uid_read(inode), i_gid_read(inode),
  2253. + (inode->i_mode & S_IALLUGO),
  2254. + i_uid_read(h_inode), i_gid_read(h_inode),
  2255. + (h_inode->i_mode & S_IALLUGO));
  2256. + }
  2257. +
  2258. +out:
  2259. + return err;
  2260. +}
  2261. +
  2262. +/*
  2263. + * initialize or clean the whiteouts for an adding branch
  2264. + */
  2265. +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
  2266. + int new_perm)
  2267. +{
  2268. + int err, old_perm;
  2269. + aufs_bindex_t bindex;
  2270. + struct inode *h_inode;
  2271. + struct au_wbr *wbr;
  2272. + struct au_hinode *hdir;
  2273. + struct dentry *h_dentry;
  2274. +
  2275. + err = vfsub_mnt_want_write(au_br_mnt(br));
  2276. + if (unlikely(err))
  2277. + goto out;
  2278. +
  2279. + wbr = br->br_wbr;
  2280. + old_perm = br->br_perm;
  2281. + br->br_perm = new_perm;
  2282. + hdir = NULL;
  2283. + h_inode = NULL;
  2284. + bindex = au_br_index(sb, br->br_id);
  2285. + if (0 <= bindex) {
  2286. + hdir = au_hi(d_inode(sb->s_root), bindex);
  2287. + au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
  2288. + } else {
  2289. + h_dentry = au_br_dentry(br);
  2290. + h_inode = d_inode(h_dentry);
  2291. + inode_lock_nested(h_inode, AuLsc_I_PARENT);
  2292. + }
  2293. + if (!wbr)
  2294. + err = au_wh_init(br, sb);
  2295. + else {
  2296. + wbr_wh_write_lock(wbr);
  2297. + err = au_wh_init(br, sb);
  2298. + wbr_wh_write_unlock(wbr);
  2299. + }
  2300. + if (hdir)
  2301. + au_hn_inode_unlock(hdir);
  2302. + else
  2303. + inode_unlock(h_inode);
  2304. + vfsub_mnt_drop_write(au_br_mnt(br));
  2305. + br->br_perm = old_perm;
  2306. +
  2307. + if (!err && wbr && !au_br_writable(new_perm)) {
  2308. + au_kfree_rcu(wbr);
  2309. + br->br_wbr = NULL;
  2310. + }
  2311. +
  2312. +out:
  2313. + return err;
  2314. +}
  2315. +
  2316. +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
  2317. + int perm)
  2318. +{
  2319. + int err;
  2320. + struct kstatfs kst;
  2321. + struct au_wbr *wbr;
  2322. +
  2323. + wbr = br->br_wbr;
  2324. + au_rw_init(&wbr->wbr_wh_rwsem);
  2325. + atomic_set(&wbr->wbr_wh_running, 0);
  2326. +
  2327. + /*
  2328. + * a limit for rmdir/rename a dir
  2329. + * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
  2330. + */
  2331. + err = vfs_statfs(&br->br_path, &kst);
  2332. + if (unlikely(err))
  2333. + goto out;
  2334. + err = -EINVAL;
  2335. + if (kst.f_namelen >= NAME_MAX)
  2336. + err = au_br_init_wh(sb, br, perm);
  2337. + else
  2338. + pr_err("%pd(%s), unsupported namelen %ld\n",
  2339. + au_br_dentry(br),
  2340. + au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
  2341. +
  2342. +out:
  2343. + return err;
  2344. +}
  2345. +
  2346. +/* initialize a new branch */
  2347. +static int au_br_init(struct au_branch *br, struct super_block *sb,
  2348. + struct au_opt_add *add)
  2349. +{
  2350. + int err;
  2351. + struct au_branch *brbase;
  2352. + struct file *xf;
  2353. + struct inode *h_inode;
  2354. +
  2355. + err = 0;
  2356. + br->br_perm = add->perm;
  2357. + br->br_path = add->path; /* set first, path_get() later */
  2358. + spin_lock_init(&br->br_dykey_lock);
  2359. + au_lcnt_init(&br->br_nfiles, /*release*/NULL);
  2360. + au_lcnt_init(&br->br_count, /*release*/NULL);
  2361. + br->br_id = au_new_br_id(sb);
  2362. + AuDebugOn(br->br_id < 0);
  2363. +
  2364. + /* always, regardless the given option */
  2365. + err = au_dr_br_init(sb, br, &add->path);
  2366. + if (unlikely(err))
  2367. + goto out_err;
  2368. +
  2369. + if (au_br_writable(add->perm)) {
  2370. + err = au_wbr_init(br, sb, add->perm);
  2371. + if (unlikely(err))
  2372. + goto out_err;
  2373. + }
  2374. +
  2375. + if (au_opt_test(au_mntflags(sb), XINO)) {
  2376. + brbase = au_sbr(sb, 0);
  2377. + xf = au_xino_file(brbase->br_xino, /*idx*/-1);
  2378. + AuDebugOn(!xf);
  2379. + h_inode = d_inode(add->path.dentry);
  2380. + err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
  2381. + if (unlikely(err)) {
  2382. + AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
  2383. + goto out_err;
  2384. + }
  2385. + }
  2386. +
  2387. + sysaufs_br_init(br);
  2388. + path_get(&br->br_path);
  2389. + goto out; /* success */
  2390. +
  2391. +out_err:
  2392. + memset(&br->br_path, 0, sizeof(br->br_path));
  2393. +out:
  2394. + return err;
  2395. +}
  2396. +
  2397. +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
  2398. + struct au_branch *br, aufs_bindex_t bbot,
  2399. + aufs_bindex_t amount)
  2400. +{
  2401. + struct au_branch **brp;
  2402. +
  2403. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  2404. +
  2405. + brp = sbinfo->si_branch + bindex;
  2406. + memmove(brp + 1, brp, sizeof(*brp) * amount);
  2407. + *brp = br;
  2408. + sbinfo->si_bbot++;
  2409. + if (unlikely(bbot < 0))
  2410. + sbinfo->si_bbot = 0;
  2411. +}
  2412. +
  2413. +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
  2414. + aufs_bindex_t bbot, aufs_bindex_t amount)
  2415. +{
  2416. + struct au_hdentry *hdp;
  2417. +
  2418. + AuRwMustWriteLock(&dinfo->di_rwsem);
  2419. +
  2420. + hdp = au_hdentry(dinfo, bindex);
  2421. + memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
  2422. + au_h_dentry_init(hdp);
  2423. + dinfo->di_bbot++;
  2424. + if (unlikely(bbot < 0))
  2425. + dinfo->di_btop = 0;
  2426. +}
  2427. +
  2428. +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
  2429. + aufs_bindex_t bbot, aufs_bindex_t amount)
  2430. +{
  2431. + struct au_hinode *hip;
  2432. +
  2433. + AuRwMustWriteLock(&iinfo->ii_rwsem);
  2434. +
  2435. + hip = au_hinode(iinfo, bindex);
  2436. + memmove(hip + 1, hip, sizeof(*hip) * amount);
  2437. + au_hinode_init(hip);
  2438. + iinfo->ii_bbot++;
  2439. + if (unlikely(bbot < 0))
  2440. + iinfo->ii_btop = 0;
  2441. +}
  2442. +
  2443. +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
  2444. + aufs_bindex_t bindex)
  2445. +{
  2446. + struct dentry *root, *h_dentry;
  2447. + struct inode *root_inode, *h_inode;
  2448. + aufs_bindex_t bbot, amount;
  2449. +
  2450. + root = sb->s_root;
  2451. + root_inode = d_inode(root);
  2452. + bbot = au_sbbot(sb);
  2453. + amount = bbot + 1 - bindex;
  2454. + h_dentry = au_br_dentry(br);
  2455. + au_sbilist_lock();
  2456. + au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
  2457. + au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
  2458. + au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
  2459. + au_set_h_dptr(root, bindex, dget(h_dentry));
  2460. + h_inode = d_inode(h_dentry);
  2461. + au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
  2462. + au_sbilist_unlock();
  2463. +}
  2464. +
  2465. +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
  2466. +{
  2467. + int err;
  2468. + aufs_bindex_t bbot, add_bindex;
  2469. + struct dentry *root, *h_dentry;
  2470. + struct inode *root_inode;
  2471. + struct au_branch *add_branch;
  2472. +
  2473. + root = sb->s_root;
  2474. + root_inode = d_inode(root);
  2475. + IMustLock(root_inode);
  2476. + IiMustWriteLock(root_inode);
  2477. + err = test_add(sb, add, remount);
  2478. + if (unlikely(err < 0))
  2479. + goto out;
  2480. + if (err) {
  2481. + err = 0;
  2482. + goto out; /* success */
  2483. + }
  2484. +
  2485. + bbot = au_sbbot(sb);
  2486. + add_branch = au_br_alloc(sb, bbot + 2, add->perm);
  2487. + err = PTR_ERR(add_branch);
  2488. + if (IS_ERR(add_branch))
  2489. + goto out;
  2490. +
  2491. + err = au_br_init(add_branch, sb, add);
  2492. + if (unlikely(err)) {
  2493. + au_br_do_free(add_branch);
  2494. + goto out;
  2495. + }
  2496. +
  2497. + add_bindex = add->bindex;
  2498. + sysaufs_brs_del(sb, add_bindex); /* remove successors */
  2499. + au_br_do_add(sb, add_branch, add_bindex);
  2500. + sysaufs_brs_add(sb, add_bindex); /* append successors */
  2501. + dbgaufs_brs_add(sb, add_bindex, /*topdown*/0); /* rename successors */
  2502. +
  2503. + h_dentry = add->path.dentry;
  2504. + if (!add_bindex) {
  2505. + au_cpup_attr_all(root_inode, /*force*/1);
  2506. + sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
  2507. + } else
  2508. + au_add_nlink(root_inode, d_inode(h_dentry));
  2509. +
  2510. +out:
  2511. + return err;
  2512. +}
  2513. +
  2514. +/* ---------------------------------------------------------------------- */
  2515. +
  2516. +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
  2517. + unsigned long long max __maybe_unused,
  2518. + void *arg)
  2519. +{
  2520. + unsigned long long n;
  2521. + struct file **p, *f;
  2522. + struct hlist_bl_head *files;
  2523. + struct hlist_bl_node *pos;
  2524. + struct au_finfo *finfo;
  2525. +
  2526. + n = 0;
  2527. + p = a;
  2528. + files = &au_sbi(sb)->si_files;
  2529. + hlist_bl_lock(files);
  2530. + hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
  2531. + f = finfo->fi_file;
  2532. + if (file_count(f)
  2533. + && !special_file(file_inode(f)->i_mode)) {
  2534. + get_file(f);
  2535. + *p++ = f;
  2536. + n++;
  2537. + AuDebugOn(n > max);
  2538. + }
  2539. + }
  2540. + hlist_bl_unlock(files);
  2541. +
  2542. + return n;
  2543. +}
  2544. +
  2545. +static struct file **au_farray_alloc(struct super_block *sb,
  2546. + unsigned long long *max)
  2547. +{
  2548. + struct au_sbinfo *sbi;
  2549. +
  2550. + sbi = au_sbi(sb);
  2551. + *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
  2552. + return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
  2553. +}
  2554. +
  2555. +static void au_farray_free(struct file **a, unsigned long long max)
  2556. +{
  2557. + unsigned long long ull;
  2558. +
  2559. + for (ull = 0; ull < max; ull++)
  2560. + if (a[ull])
  2561. + fput(a[ull]);
  2562. + kvfree(a);
  2563. +}
  2564. +
  2565. +/* ---------------------------------------------------------------------- */
  2566. +
  2567. +/*
  2568. + * delete a branch
  2569. + */
  2570. +
  2571. +/* to show the line number, do not make it inlined function */
  2572. +#define AuVerbose(do_info, fmt, ...) do { \
  2573. + if (do_info) \
  2574. + pr_info(fmt, ##__VA_ARGS__); \
  2575. +} while (0)
  2576. +
  2577. +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
  2578. + aufs_bindex_t bbot)
  2579. +{
  2580. + return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
  2581. +}
  2582. +
  2583. +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
  2584. + aufs_bindex_t bbot)
  2585. +{
  2586. + return au_test_ibusy(d_inode(dentry), btop, bbot);
  2587. +}
  2588. +
  2589. +/*
  2590. + * test if the branch is deletable or not.
  2591. + */
  2592. +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
  2593. + unsigned int sigen, const unsigned int verbose)
  2594. +{
  2595. + int err, i, j, ndentry;
  2596. + aufs_bindex_t btop, bbot;
  2597. + struct au_dcsub_pages dpages;
  2598. + struct au_dpage *dpage;
  2599. + struct dentry *d;
  2600. +
  2601. + err = au_dpages_init(&dpages, GFP_NOFS);
  2602. + if (unlikely(err))
  2603. + goto out;
  2604. + err = au_dcsub_pages(&dpages, root, NULL, NULL);
  2605. + if (unlikely(err))
  2606. + goto out_dpages;
  2607. +
  2608. + for (i = 0; !err && i < dpages.ndpage; i++) {
  2609. + dpage = dpages.dpages + i;
  2610. + ndentry = dpage->ndentry;
  2611. + for (j = 0; !err && j < ndentry; j++) {
  2612. + d = dpage->dentries[j];
  2613. + AuDebugOn(au_dcount(d) <= 0);
  2614. + if (!au_digen_test(d, sigen)) {
  2615. + di_read_lock_child(d, AuLock_IR);
  2616. + if (unlikely(au_dbrange_test(d))) {
  2617. + di_read_unlock(d, AuLock_IR);
  2618. + continue;
  2619. + }
  2620. + } else {
  2621. + di_write_lock_child(d);
  2622. + if (unlikely(au_dbrange_test(d))) {
  2623. + di_write_unlock(d);
  2624. + continue;
  2625. + }
  2626. + err = au_reval_dpath(d, sigen);
  2627. + if (!err)
  2628. + di_downgrade_lock(d, AuLock_IR);
  2629. + else {
  2630. + di_write_unlock(d);
  2631. + break;
  2632. + }
  2633. + }
  2634. +
  2635. + /* AuDbgDentry(d); */
  2636. + btop = au_dbtop(d);
  2637. + bbot = au_dbbot(d);
  2638. + if (btop <= bindex
  2639. + && bindex <= bbot
  2640. + && au_h_dptr(d, bindex)
  2641. + && au_test_dbusy(d, btop, bbot)) {
  2642. + err = -EBUSY;
  2643. + AuVerbose(verbose, "busy %pd\n", d);
  2644. + AuDbgDentry(d);
  2645. + }
  2646. + di_read_unlock(d, AuLock_IR);
  2647. + }
  2648. + }
  2649. +
  2650. +out_dpages:
  2651. + au_dpages_free(&dpages);
  2652. +out:
  2653. + return err;
  2654. +}
  2655. +
  2656. +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
  2657. + unsigned int sigen, const unsigned int verbose)
  2658. +{
  2659. + int err;
  2660. + unsigned long long max, ull;
  2661. + struct inode *i, **array;
  2662. + aufs_bindex_t btop, bbot;
  2663. +
  2664. + array = au_iarray_alloc(sb, &max);
  2665. + err = PTR_ERR(array);
  2666. + if (IS_ERR(array))
  2667. + goto out;
  2668. +
  2669. + err = 0;
  2670. + AuDbg("b%d\n", bindex);
  2671. + for (ull = 0; !err && ull < max; ull++) {
  2672. + i = array[ull];
  2673. + if (unlikely(!i))
  2674. + break;
  2675. + if (i->i_ino == AUFS_ROOT_INO)
  2676. + continue;
  2677. +
  2678. + /* AuDbgInode(i); */
  2679. + if (au_iigen(i, NULL) == sigen)
  2680. + ii_read_lock_child(i);
  2681. + else {
  2682. + ii_write_lock_child(i);
  2683. + err = au_refresh_hinode_self(i);
  2684. + au_iigen_dec(i);
  2685. + if (!err)
  2686. + ii_downgrade_lock(i);
  2687. + else {
  2688. + ii_write_unlock(i);
  2689. + break;
  2690. + }
  2691. + }
  2692. +
  2693. + btop = au_ibtop(i);
  2694. + bbot = au_ibbot(i);
  2695. + if (btop <= bindex
  2696. + && bindex <= bbot
  2697. + && au_h_iptr(i, bindex)
  2698. + && au_test_ibusy(i, btop, bbot)) {
  2699. + err = -EBUSY;
  2700. + AuVerbose(verbose, "busy i%lu\n", i->i_ino);
  2701. + AuDbgInode(i);
  2702. + }
  2703. + ii_read_unlock(i);
  2704. + }
  2705. + au_iarray_free(array, max);
  2706. +
  2707. +out:
  2708. + return err;
  2709. +}
  2710. +
  2711. +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
  2712. + const unsigned int verbose)
  2713. +{
  2714. + int err;
  2715. + unsigned int sigen;
  2716. +
  2717. + sigen = au_sigen(root->d_sb);
  2718. + DiMustNoWaiters(root);
  2719. + IiMustNoWaiters(d_inode(root));
  2720. + di_write_unlock(root);
  2721. + err = test_dentry_busy(root, bindex, sigen, verbose);
  2722. + if (!err)
  2723. + err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
  2724. + di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
  2725. +
  2726. + return err;
  2727. +}
  2728. +
  2729. +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
  2730. + struct file **to_free, int *idx)
  2731. +{
  2732. + int err;
  2733. + unsigned char matched, root;
  2734. + aufs_bindex_t bindex, bbot;
  2735. + struct au_fidir *fidir;
  2736. + struct au_hfile *hfile;
  2737. +
  2738. + err = 0;
  2739. + root = IS_ROOT(file->f_path.dentry);
  2740. + if (root) {
  2741. + get_file(file);
  2742. + to_free[*idx] = file;
  2743. + (*idx)++;
  2744. + goto out;
  2745. + }
  2746. +
  2747. + matched = 0;
  2748. + fidir = au_fi(file)->fi_hdir;
  2749. + AuDebugOn(!fidir);
  2750. + bbot = au_fbbot_dir(file);
  2751. + for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
  2752. + hfile = fidir->fd_hfile + bindex;
  2753. + if (!hfile->hf_file)
  2754. + continue;
  2755. +
  2756. + if (hfile->hf_br->br_id == br_id) {
  2757. + matched = 1;
  2758. + break;
  2759. + }
  2760. + }
  2761. + if (matched)
  2762. + err = -EBUSY;
  2763. +
  2764. +out:
  2765. + return err;
  2766. +}
  2767. +
  2768. +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
  2769. + struct file **to_free, int opened)
  2770. +{
  2771. + int err, idx;
  2772. + unsigned long long ull, max;
  2773. + aufs_bindex_t btop;
  2774. + struct file *file, **array;
  2775. + struct dentry *root;
  2776. + struct au_hfile *hfile;
  2777. +
  2778. + array = au_farray_alloc(sb, &max);
  2779. + err = PTR_ERR(array);
  2780. + if (IS_ERR(array))
  2781. + goto out;
  2782. +
  2783. + err = 0;
  2784. + idx = 0;
  2785. + root = sb->s_root;
  2786. + di_write_unlock(root);
  2787. + for (ull = 0; ull < max; ull++) {
  2788. + file = array[ull];
  2789. + if (unlikely(!file))
  2790. + break;
  2791. +
  2792. + /* AuDbg("%pD\n", file); */
  2793. + fi_read_lock(file);
  2794. + btop = au_fbtop(file);
  2795. + if (!d_is_dir(file->f_path.dentry)) {
  2796. + hfile = &au_fi(file)->fi_htop;
  2797. + if (hfile->hf_br->br_id == br_id)
  2798. + err = -EBUSY;
  2799. + } else
  2800. + err = test_dir_busy(file, br_id, to_free, &idx);
  2801. + fi_read_unlock(file);
  2802. + if (unlikely(err))
  2803. + break;
  2804. + }
  2805. + di_write_lock_child(root);
  2806. + au_farray_free(array, max);
  2807. + AuDebugOn(idx > opened);
  2808. +
  2809. +out:
  2810. + return err;
  2811. +}
  2812. +
  2813. +static void br_del_file(struct file **to_free, unsigned long long opened,
  2814. + aufs_bindex_t br_id)
  2815. +{
  2816. + unsigned long long ull;
  2817. + aufs_bindex_t bindex, btop, bbot, bfound;
  2818. + struct file *file;
  2819. + struct au_fidir *fidir;
  2820. + struct au_hfile *hfile;
  2821. +
  2822. + for (ull = 0; ull < opened; ull++) {
  2823. + file = to_free[ull];
  2824. + if (unlikely(!file))
  2825. + break;
  2826. +
  2827. + /* AuDbg("%pD\n", file); */
  2828. + AuDebugOn(!d_is_dir(file->f_path.dentry));
  2829. + bfound = -1;
  2830. + fidir = au_fi(file)->fi_hdir;
  2831. + AuDebugOn(!fidir);
  2832. + fi_write_lock(file);
  2833. + btop = au_fbtop(file);
  2834. + bbot = au_fbbot_dir(file);
  2835. + for (bindex = btop; bindex <= bbot; bindex++) {
  2836. + hfile = fidir->fd_hfile + bindex;
  2837. + if (!hfile->hf_file)
  2838. + continue;
  2839. +
  2840. + if (hfile->hf_br->br_id == br_id) {
  2841. + bfound = bindex;
  2842. + break;
  2843. + }
  2844. + }
  2845. + AuDebugOn(bfound < 0);
  2846. + au_set_h_fptr(file, bfound, NULL);
  2847. + if (bfound == btop) {
  2848. + for (btop++; btop <= bbot; btop++)
  2849. + if (au_hf_dir(file, btop)) {
  2850. + au_set_fbtop(file, btop);
  2851. + break;
  2852. + }
  2853. + }
  2854. + fi_write_unlock(file);
  2855. + }
  2856. +}
  2857. +
  2858. +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
  2859. + const aufs_bindex_t bindex,
  2860. + const aufs_bindex_t bbot)
  2861. +{
  2862. + struct au_branch **brp, **p;
  2863. +
  2864. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  2865. +
  2866. + brp = sbinfo->si_branch + bindex;
  2867. + if (bindex < bbot)
  2868. + memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
  2869. + sbinfo->si_branch[0 + bbot] = NULL;
  2870. + sbinfo->si_bbot--;
  2871. +
  2872. + p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
  2873. + /*may_shrink*/1);
  2874. + if (p)
  2875. + sbinfo->si_branch = p;
  2876. + /* harmless error */
  2877. +}
  2878. +
  2879. +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
  2880. + const aufs_bindex_t bbot)
  2881. +{
  2882. + struct au_hdentry *hdp, *p;
  2883. +
  2884. + AuRwMustWriteLock(&dinfo->di_rwsem);
  2885. +
  2886. + hdp = au_hdentry(dinfo, bindex);
  2887. + if (bindex < bbot)
  2888. + memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
  2889. + /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
  2890. + dinfo->di_bbot--;
  2891. +
  2892. + p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
  2893. + /*may_shrink*/1);
  2894. + if (p)
  2895. + dinfo->di_hdentry = p;
  2896. + /* harmless error */
  2897. +}
  2898. +
  2899. +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
  2900. + const aufs_bindex_t bbot)
  2901. +{
  2902. + struct au_hinode *hip, *p;
  2903. +
  2904. + AuRwMustWriteLock(&iinfo->ii_rwsem);
  2905. +
  2906. + hip = au_hinode(iinfo, bindex);
  2907. + if (bindex < bbot)
  2908. + memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
  2909. + /* au_hinode_init(au_hinode(iinfo, bbot)); */
  2910. + iinfo->ii_bbot--;
  2911. +
  2912. + p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
  2913. + /*may_shrink*/1);
  2914. + if (p)
  2915. + iinfo->ii_hinode = p;
  2916. + /* harmless error */
  2917. +}
  2918. +
  2919. +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
  2920. + struct au_branch *br)
  2921. +{
  2922. + aufs_bindex_t bbot;
  2923. + struct au_sbinfo *sbinfo;
  2924. + struct dentry *root, *h_root;
  2925. + struct inode *inode, *h_inode;
  2926. + struct au_hinode *hinode;
  2927. +
  2928. + SiMustWriteLock(sb);
  2929. +
  2930. + root = sb->s_root;
  2931. + inode = d_inode(root);
  2932. + sbinfo = au_sbi(sb);
  2933. + bbot = sbinfo->si_bbot;
  2934. +
  2935. + h_root = au_h_dptr(root, bindex);
  2936. + hinode = au_hi(inode, bindex);
  2937. + h_inode = au_igrab(hinode->hi_inode);
  2938. + au_hiput(hinode);
  2939. +
  2940. + au_sbilist_lock();
  2941. + au_br_do_del_brp(sbinfo, bindex, bbot);
  2942. + au_br_do_del_hdp(au_di(root), bindex, bbot);
  2943. + au_br_do_del_hip(au_ii(inode), bindex, bbot);
  2944. + au_sbilist_unlock();
  2945. +
  2946. + /* ignore an error */
  2947. + au_dr_br_fin(sb, br); /* always, regardless the mount option */
  2948. +
  2949. + dput(h_root);
  2950. + iput(h_inode);
  2951. + au_br_do_free(br);
  2952. +}
  2953. +
  2954. +static unsigned long long empty_cb(struct super_block *sb, void *array,
  2955. + unsigned long long max, void *arg)
  2956. +{
  2957. + return max;
  2958. +}
  2959. +
  2960. +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
  2961. +{
  2962. + int err, rerr, i;
  2963. + unsigned long long opened;
  2964. + unsigned int mnt_flags;
  2965. + aufs_bindex_t bindex, bbot, br_id;
  2966. + unsigned char do_wh, verbose;
  2967. + struct au_branch *br;
  2968. + struct au_wbr *wbr;
  2969. + struct dentry *root;
  2970. + struct file **to_free;
  2971. +
  2972. + err = 0;
  2973. + opened = 0;
  2974. + to_free = NULL;
  2975. + root = sb->s_root;
  2976. + bindex = au_find_dbindex(root, del->h_path.dentry);
  2977. + if (bindex < 0) {
  2978. + if (remount)
  2979. + goto out; /* success */
  2980. + err = -ENOENT;
  2981. + pr_err("%s no such branch\n", del->pathname);
  2982. + goto out;
  2983. + }
  2984. + AuDbg("bindex b%d\n", bindex);
  2985. +
  2986. + err = -EBUSY;
  2987. + mnt_flags = au_mntflags(sb);
  2988. + verbose = !!au_opt_test(mnt_flags, VERBOSE);
  2989. + bbot = au_sbbot(sb);
  2990. + if (unlikely(!bbot)) {
  2991. + AuVerbose(verbose, "no more branches left\n");
  2992. + goto out;
  2993. + }
  2994. +
  2995. + br = au_sbr(sb, bindex);
  2996. + AuDebugOn(!path_equal(&br->br_path, &del->h_path));
  2997. + if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
  2998. + AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
  2999. + goto out;
  3000. + }
  3001. +
  3002. + br_id = br->br_id;
  3003. + opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
  3004. + if (unlikely(opened)) {
  3005. + to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
  3006. + err = PTR_ERR(to_free);
  3007. + if (IS_ERR(to_free))
  3008. + goto out;
  3009. +
  3010. + err = test_file_busy(sb, br_id, to_free, opened);
  3011. + if (unlikely(err)) {
  3012. + AuVerbose(verbose, "%llu file(s) opened\n", opened);
  3013. + goto out;
  3014. + }
  3015. + }
  3016. +
  3017. + wbr = br->br_wbr;
  3018. + do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
  3019. + if (do_wh) {
  3020. + /* instead of WbrWhMustWriteLock(wbr) */
  3021. + SiMustWriteLock(sb);
  3022. + for (i = 0; i < AuBrWh_Last; i++) {
  3023. + dput(wbr->wbr_wh[i]);
  3024. + wbr->wbr_wh[i] = NULL;
  3025. + }
  3026. + }
  3027. +
  3028. + err = test_children_busy(root, bindex, verbose);
  3029. + if (unlikely(err)) {
  3030. + if (do_wh)
  3031. + goto out_wh;
  3032. + goto out;
  3033. + }
  3034. +
  3035. + err = 0;
  3036. + if (to_free) {
  3037. + /*
  3038. + * now we confirmed the branch is deletable.
  3039. + * let's free the remaining opened dirs on the branch.
  3040. + */
  3041. + di_write_unlock(root);
  3042. + br_del_file(to_free, opened, br_id);
  3043. + di_write_lock_child(root);
  3044. + }
  3045. +
  3046. + sysaufs_brs_del(sb, bindex); /* remove successors */
  3047. + dbgaufs_xino_del(br); /* remove one */
  3048. + au_br_do_del(sb, bindex, br);
  3049. + sysaufs_brs_add(sb, bindex); /* append successors */
  3050. + dbgaufs_brs_add(sb, bindex, /*topdown*/1); /* rename successors */
  3051. +
  3052. + if (!bindex) {
  3053. + au_cpup_attr_all(d_inode(root), /*force*/1);
  3054. + sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
  3055. + } else
  3056. + au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
  3057. + if (au_opt_test(mnt_flags, PLINK))
  3058. + au_plink_half_refresh(sb, br_id);
  3059. +
  3060. + goto out; /* success */
  3061. +
  3062. +out_wh:
  3063. + /* revert */
  3064. + rerr = au_br_init_wh(sb, br, br->br_perm);
  3065. + if (rerr)
  3066. + pr_warn("failed re-creating base whiteout, %s. (%d)\n",
  3067. + del->pathname, rerr);
  3068. +out:
  3069. + if (to_free)
  3070. + au_farray_free(to_free, opened);
  3071. + return err;
  3072. +}
  3073. +
  3074. +/* ---------------------------------------------------------------------- */
  3075. +
  3076. +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
  3077. +{
  3078. + int err;
  3079. + aufs_bindex_t btop, bbot;
  3080. + struct aufs_ibusy ibusy;
  3081. + struct inode *inode, *h_inode;
  3082. +
  3083. + err = -EPERM;
  3084. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  3085. + goto out;
  3086. +
  3087. + err = copy_from_user(&ibusy, arg, sizeof(ibusy));
  3088. + if (!err)
  3089. + /* VERIFY_WRITE */
  3090. + err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
  3091. + if (unlikely(err)) {
  3092. + err = -EFAULT;
  3093. + AuTraceErr(err);
  3094. + goto out;
  3095. + }
  3096. +
  3097. + err = -EINVAL;
  3098. + si_read_lock(sb, AuLock_FLUSH);
  3099. + if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
  3100. + goto out_unlock;
  3101. +
  3102. + err = 0;
  3103. + ibusy.h_ino = 0; /* invalid */
  3104. + inode = ilookup(sb, ibusy.ino);
  3105. + if (!inode
  3106. + || inode->i_ino == AUFS_ROOT_INO
  3107. + || au_is_bad_inode(inode))
  3108. + goto out_unlock;
  3109. +
  3110. + ii_read_lock_child(inode);
  3111. + btop = au_ibtop(inode);
  3112. + bbot = au_ibbot(inode);
  3113. + if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
  3114. + h_inode = au_h_iptr(inode, ibusy.bindex);
  3115. + if (h_inode && au_test_ibusy(inode, btop, bbot))
  3116. + ibusy.h_ino = h_inode->i_ino;
  3117. + }
  3118. + ii_read_unlock(inode);
  3119. + iput(inode);
  3120. +
  3121. +out_unlock:
  3122. + si_read_unlock(sb);
  3123. + if (!err) {
  3124. + err = __put_user(ibusy.h_ino, &arg->h_ino);
  3125. + if (unlikely(err)) {
  3126. + err = -EFAULT;
  3127. + AuTraceErr(err);
  3128. + }
  3129. + }
  3130. +out:
  3131. + return err;
  3132. +}
  3133. +
  3134. +long au_ibusy_ioctl(struct file *file, unsigned long arg)
  3135. +{
  3136. + return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
  3137. +}
  3138. +
  3139. +#ifdef CONFIG_COMPAT
  3140. +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
  3141. +{
  3142. + return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
  3143. +}
  3144. +#endif
  3145. +
  3146. +/* ---------------------------------------------------------------------- */
  3147. +
  3148. +/*
  3149. + * change a branch permission
  3150. + */
  3151. +
  3152. +static void au_warn_ima(void)
  3153. +{
  3154. +#ifdef CONFIG_IMA
  3155. + /* since it doesn't support mark_files_ro() */
  3156. + AuWarn1("RW -> RO makes IMA to produce wrong message\n");
  3157. +#endif
  3158. +}
  3159. +
  3160. +static int do_need_sigen_inc(int a, int b)
  3161. +{
  3162. + return au_br_whable(a) && !au_br_whable(b);
  3163. +}
  3164. +
  3165. +static int need_sigen_inc(int old, int new)
  3166. +{
  3167. + return do_need_sigen_inc(old, new)
  3168. + || do_need_sigen_inc(new, old);
  3169. +}
  3170. +
  3171. +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
  3172. +{
  3173. + int err, do_warn;
  3174. + unsigned int mnt_flags;
  3175. + unsigned long long ull, max;
  3176. + aufs_bindex_t br_id;
  3177. + unsigned char verbose, writer;
  3178. + struct file *file, *hf, **array;
  3179. + struct au_hfile *hfile;
  3180. +
  3181. + mnt_flags = au_mntflags(sb);
  3182. + verbose = !!au_opt_test(mnt_flags, VERBOSE);
  3183. +
  3184. + array = au_farray_alloc(sb, &max);
  3185. + err = PTR_ERR(array);
  3186. + if (IS_ERR(array))
  3187. + goto out;
  3188. +
  3189. + do_warn = 0;
  3190. + br_id = au_sbr_id(sb, bindex);
  3191. + for (ull = 0; ull < max; ull++) {
  3192. + file = array[ull];
  3193. + if (unlikely(!file))
  3194. + break;
  3195. +
  3196. + /* AuDbg("%pD\n", file); */
  3197. + fi_read_lock(file);
  3198. + if (unlikely(au_test_mmapped(file))) {
  3199. + err = -EBUSY;
  3200. + AuVerbose(verbose, "mmapped %pD\n", file);
  3201. + AuDbgFile(file);
  3202. + FiMustNoWaiters(file);
  3203. + fi_read_unlock(file);
  3204. + goto out_array;
  3205. + }
  3206. +
  3207. + hfile = &au_fi(file)->fi_htop;
  3208. + hf = hfile->hf_file;
  3209. + if (!d_is_reg(file->f_path.dentry)
  3210. + || !(file->f_mode & FMODE_WRITE)
  3211. + || hfile->hf_br->br_id != br_id
  3212. + || !(hf->f_mode & FMODE_WRITE))
  3213. + array[ull] = NULL;
  3214. + else {
  3215. + do_warn = 1;
  3216. + get_file(file);
  3217. + }
  3218. +
  3219. + FiMustNoWaiters(file);
  3220. + fi_read_unlock(file);
  3221. + fput(file);
  3222. + }
  3223. +
  3224. + err = 0;
  3225. + if (do_warn)
  3226. + au_warn_ima();
  3227. +
  3228. + for (ull = 0; ull < max; ull++) {
  3229. + file = array[ull];
  3230. + if (!file)
  3231. + continue;
  3232. +
  3233. + /* todo: already flushed? */
  3234. + /*
  3235. + * fs/super.c:mark_files_ro() is gone, but aufs keeps its
  3236. + * approach which resets f_mode and calls mnt_drop_write() and
  3237. + * file_release_write() for each file, because the branch
  3238. + * attribute in aufs world is totally different from the native
  3239. + * fs rw/ro mode.
  3240. + */
  3241. + /* fi_read_lock(file); */
  3242. + hfile = &au_fi(file)->fi_htop;
  3243. + hf = hfile->hf_file;
  3244. + /* fi_read_unlock(file); */
  3245. + spin_lock(&hf->f_lock);
  3246. + writer = !!(hf->f_mode & FMODE_WRITER);
  3247. + hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
  3248. + spin_unlock(&hf->f_lock);
  3249. + if (writer) {
  3250. + put_write_access(file_inode(hf));
  3251. + __mnt_drop_write(hf->f_path.mnt);
  3252. + }
  3253. + }
  3254. +
  3255. +out_array:
  3256. + au_farray_free(array, max);
  3257. +out:
  3258. + AuTraceErr(err);
  3259. + return err;
  3260. +}
  3261. +
  3262. +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
  3263. + int *do_refresh)
  3264. +{
  3265. + int err, rerr;
  3266. + aufs_bindex_t bindex;
  3267. + struct dentry *root;
  3268. + struct au_branch *br;
  3269. + struct au_br_fhsm *bf;
  3270. +
  3271. + root = sb->s_root;
  3272. + bindex = au_find_dbindex(root, mod->h_root);
  3273. + if (bindex < 0) {
  3274. + if (remount)
  3275. + return 0; /* success */
  3276. + err = -ENOENT;
  3277. + pr_err("%s no such branch\n", mod->path);
  3278. + goto out;
  3279. + }
  3280. + AuDbg("bindex b%d\n", bindex);
  3281. +
  3282. + err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
  3283. + if (unlikely(err))
  3284. + goto out;
  3285. +
  3286. + br = au_sbr(sb, bindex);
  3287. + AuDebugOn(mod->h_root != au_br_dentry(br));
  3288. + if (br->br_perm == mod->perm)
  3289. + return 0; /* success */
  3290. +
  3291. + /* pre-allocate for non-fhsm --> fhsm */
  3292. + bf = NULL;
  3293. + if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
  3294. + err = au_fhsm_br_alloc(br);
  3295. + if (unlikely(err))
  3296. + goto out;
  3297. + bf = br->br_fhsm;
  3298. + br->br_fhsm = NULL;
  3299. + }
  3300. +
  3301. + if (au_br_writable(br->br_perm)) {
  3302. + /* remove whiteout base */
  3303. + err = au_br_init_wh(sb, br, mod->perm);
  3304. + if (unlikely(err))
  3305. + goto out_bf;
  3306. +
  3307. + if (!au_br_writable(mod->perm)) {
  3308. + /* rw --> ro, file might be mmapped */
  3309. + DiMustNoWaiters(root);
  3310. + IiMustNoWaiters(d_inode(root));
  3311. + di_write_unlock(root);
  3312. + err = au_br_mod_files_ro(sb, bindex);
  3313. + /* aufs_write_lock() calls ..._child() */
  3314. + di_write_lock_child(root);
  3315. +
  3316. + if (unlikely(err)) {
  3317. + rerr = -ENOMEM;
  3318. + br->br_wbr = kzalloc(sizeof(*br->br_wbr),
  3319. + GFP_NOFS);
  3320. + if (br->br_wbr)
  3321. + rerr = au_wbr_init(br, sb, br->br_perm);
  3322. + if (unlikely(rerr)) {
  3323. + AuIOErr("nested error %d (%d)\n",
  3324. + rerr, err);
  3325. + br->br_perm = mod->perm;
  3326. + }
  3327. + }
  3328. + }
  3329. + } else if (au_br_writable(mod->perm)) {
  3330. + /* ro --> rw */
  3331. + err = -ENOMEM;
  3332. + br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
  3333. + if (br->br_wbr) {
  3334. + err = au_wbr_init(br, sb, mod->perm);
  3335. + if (unlikely(err)) {
  3336. + au_kfree_rcu(br->br_wbr);
  3337. + br->br_wbr = NULL;
  3338. + }
  3339. + }
  3340. + }
  3341. + if (unlikely(err))
  3342. + goto out_bf;
  3343. +
  3344. + if (au_br_fhsm(br->br_perm)) {
  3345. + if (!au_br_fhsm(mod->perm)) {
  3346. + /* fhsm --> non-fhsm */
  3347. + au_br_fhsm_fin(br->br_fhsm);
  3348. + au_kfree_rcu(br->br_fhsm);
  3349. + br->br_fhsm = NULL;
  3350. + }
  3351. + } else if (au_br_fhsm(mod->perm))
  3352. + /* non-fhsm --> fhsm */
  3353. + br->br_fhsm = bf;
  3354. +
  3355. + *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
  3356. + br->br_perm = mod->perm;
  3357. + goto out; /* success */
  3358. +
  3359. +out_bf:
  3360. + au_kfree_try_rcu(bf);
  3361. +out:
  3362. + AuTraceErr(err);
  3363. + return err;
  3364. +}
  3365. +
  3366. +/* ---------------------------------------------------------------------- */
  3367. +
  3368. +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
  3369. +{
  3370. + int err;
  3371. + struct kstatfs kstfs;
  3372. +
  3373. + err = vfs_statfs(&br->br_path, &kstfs);
  3374. + if (!err) {
  3375. + stfs->f_blocks = kstfs.f_blocks;
  3376. + stfs->f_bavail = kstfs.f_bavail;
  3377. + stfs->f_files = kstfs.f_files;
  3378. + stfs->f_ffree = kstfs.f_ffree;
  3379. + }
  3380. +
  3381. + return err;
  3382. +}
  3383. diff -Naur null/fs/aufs/branch.h linux-5.1/fs/aufs/branch.h
  3384. --- /dev/null
  3385. +++ linux-5.1/fs/aufs/branch.h 2019-05-22 11:23:07.438141134 +0200
  3386. @@ -0,0 +1,365 @@
  3387. +/* SPDX-License-Identifier: GPL-2.0 */
  3388. +/*
  3389. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  3390. + *
  3391. + * This program, aufs is free software; you can redistribute it and/or modify
  3392. + * it under the terms of the GNU General Public License as published by
  3393. + * the Free Software Foundation; either version 2 of the License, or
  3394. + * (at your option) any later version.
  3395. + *
  3396. + * This program is distributed in the hope that it will be useful,
  3397. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3398. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3399. + * GNU General Public License for more details.
  3400. + *
  3401. + * You should have received a copy of the GNU General Public License
  3402. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  3403. + */
  3404. +
  3405. +/*
  3406. + * branch filesystems and xino for them
  3407. + */
  3408. +
  3409. +#ifndef __AUFS_BRANCH_H__
  3410. +#define __AUFS_BRANCH_H__
  3411. +
  3412. +#ifdef __KERNEL__
  3413. +
  3414. +#include <linux/mount.h>
  3415. +#include "dirren.h"
  3416. +#include "dynop.h"
  3417. +#include "lcnt.h"
  3418. +#include "rwsem.h"
  3419. +#include "super.h"
  3420. +
  3421. +/* ---------------------------------------------------------------------- */
  3422. +
  3423. +/* a xino file */
  3424. +struct au_xino {
  3425. + struct file **xi_file;
  3426. + unsigned int xi_nfile;
  3427. +
  3428. + struct {
  3429. + spinlock_t spin;
  3430. + ino_t *array;
  3431. + int total;
  3432. + /* reserved for future use */
  3433. + /* unsigned long *bitmap; */
  3434. + wait_queue_head_t wqh;
  3435. + } xi_nondir;
  3436. +
  3437. + struct mutex xi_mtx; /* protects xi_file array */
  3438. + struct hlist_bl_head xi_writing;
  3439. +
  3440. + atomic_t xi_truncating;
  3441. +
  3442. + struct kref xi_kref;
  3443. +};
  3444. +
  3445. +/* File-based Hierarchical Storage Management */
  3446. +struct au_br_fhsm {
  3447. +#ifdef CONFIG_AUFS_FHSM
  3448. + struct mutex bf_lock;
  3449. + unsigned long bf_jiffy;
  3450. + struct aufs_stfs bf_stfs;
  3451. + int bf_readable;
  3452. +#endif
  3453. +};
  3454. +
  3455. +/* members for writable branch only */
  3456. +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
  3457. +struct au_wbr {
  3458. + struct au_rwsem wbr_wh_rwsem;
  3459. + struct dentry *wbr_wh[AuBrWh_Last];
  3460. + atomic_t wbr_wh_running;
  3461. +#define wbr_whbase wbr_wh[AuBrWh_BASE] /* whiteout base */
  3462. +#define wbr_plink wbr_wh[AuBrWh_PLINK] /* pseudo-link dir */
  3463. +#define wbr_orph wbr_wh[AuBrWh_ORPH] /* dir for orphans */
  3464. +
  3465. + /* mfs mode */
  3466. + unsigned long long wbr_bytes;
  3467. +};
  3468. +
  3469. +/* ext2 has 3 types of operations at least, ext3 has 4 */
  3470. +#define AuBrDynOp (AuDyLast * 4)
  3471. +
  3472. +#ifdef CONFIG_AUFS_HFSNOTIFY
  3473. +/* support for asynchronous destruction */
  3474. +struct au_br_hfsnotify {
  3475. + struct fsnotify_group *hfsn_group;
  3476. +};
  3477. +#endif
  3478. +
  3479. +/* sysfs entries */
  3480. +struct au_brsysfs {
  3481. + char name[16];
  3482. + struct attribute attr;
  3483. +};
  3484. +
  3485. +enum {
  3486. + AuBrSysfs_BR,
  3487. + AuBrSysfs_BRID,
  3488. + AuBrSysfs_Last
  3489. +};
  3490. +
  3491. +/* protected by superblock rwsem */
  3492. +struct au_branch {
  3493. + struct au_xino *br_xino;
  3494. +
  3495. + aufs_bindex_t br_id;
  3496. +
  3497. + int br_perm;
  3498. + struct path br_path;
  3499. + spinlock_t br_dykey_lock;
  3500. + struct au_dykey *br_dykey[AuBrDynOp];
  3501. + au_lcnt_t br_nfiles; /* opened files */
  3502. + au_lcnt_t br_count; /* in-use for other */
  3503. +
  3504. + struct au_wbr *br_wbr;
  3505. + struct au_br_fhsm *br_fhsm;
  3506. +
  3507. +#ifdef CONFIG_AUFS_HFSNOTIFY
  3508. + struct au_br_hfsnotify *br_hfsn;
  3509. +#endif
  3510. +
  3511. +#ifdef CONFIG_SYSFS
  3512. + /* entries under sysfs per mount-point */
  3513. + struct au_brsysfs br_sysfs[AuBrSysfs_Last];
  3514. +#endif
  3515. +
  3516. +#ifdef CONFIG_DEBUG_FS
  3517. + struct dentry *br_dbgaufs; /* xino */
  3518. +#endif
  3519. +
  3520. + struct au_dr_br br_dirren;
  3521. +};
  3522. +
  3523. +/* ---------------------------------------------------------------------- */
  3524. +
  3525. +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
  3526. +{
  3527. + return br->br_path.mnt;
  3528. +}
  3529. +
  3530. +static inline struct dentry *au_br_dentry(struct au_branch *br)
  3531. +{
  3532. + return br->br_path.dentry;
  3533. +}
  3534. +
  3535. +static inline struct super_block *au_br_sb(struct au_branch *br)
  3536. +{
  3537. + return au_br_mnt(br)->mnt_sb;
  3538. +}
  3539. +
  3540. +static inline int au_br_rdonly(struct au_branch *br)
  3541. +{
  3542. + return (sb_rdonly(au_br_sb(br))
  3543. + || !au_br_writable(br->br_perm))
  3544. + ? -EROFS : 0;
  3545. +}
  3546. +
  3547. +static inline int au_br_hnotifyable(int brperm __maybe_unused)
  3548. +{
  3549. +#ifdef CONFIG_AUFS_HNOTIFY
  3550. + return !(brperm & AuBrPerm_RR);
  3551. +#else
  3552. + return 0;
  3553. +#endif
  3554. +}
  3555. +
  3556. +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
  3557. +{
  3558. + int err, exec_flag;
  3559. +
  3560. + err = 0;
  3561. + exec_flag = oflag & __FMODE_EXEC;
  3562. + if (unlikely(exec_flag && path_noexec(&br->br_path)))
  3563. + err = -EACCES;
  3564. +
  3565. + return err;
  3566. +}
  3567. +
  3568. +static inline void au_xino_get(struct au_branch *br)
  3569. +{
  3570. + struct au_xino *xi;
  3571. +
  3572. + xi = br->br_xino;
  3573. + if (xi)
  3574. + kref_get(&xi->xi_kref);
  3575. +}
  3576. +
  3577. +static inline int au_xino_count(struct au_branch *br)
  3578. +{
  3579. + int v;
  3580. + struct au_xino *xi;
  3581. +
  3582. + v = 0;
  3583. + xi = br->br_xino;
  3584. + if (xi)
  3585. + v = kref_read(&xi->xi_kref);
  3586. +
  3587. + return v;
  3588. +}
  3589. +
  3590. +/* ---------------------------------------------------------------------- */
  3591. +
  3592. +/* branch.c */
  3593. +struct au_sbinfo;
  3594. +void au_br_free(struct au_sbinfo *sinfo);
  3595. +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
  3596. +struct au_opt_add;
  3597. +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
  3598. +struct au_opt_del;
  3599. +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
  3600. +long au_ibusy_ioctl(struct file *file, unsigned long arg);
  3601. +#ifdef CONFIG_COMPAT
  3602. +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
  3603. +#endif
  3604. +struct au_opt_mod;
  3605. +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
  3606. + int *do_refresh);
  3607. +struct aufs_stfs;
  3608. +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
  3609. +
  3610. +/* xino.c */
  3611. +static const loff_t au_loff_max = LLONG_MAX;
  3612. +
  3613. +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
  3614. +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent);
  3615. +struct file *au_xino_create2(struct super_block *sb, struct path *base,
  3616. + struct file *copy_src);
  3617. +struct au_xi_new {
  3618. + struct au_xino *xi; /* switch between xino and xigen */
  3619. + int idx;
  3620. + struct path *base;
  3621. + struct file *copy_src;
  3622. +};
  3623. +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
  3624. +
  3625. +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  3626. + ino_t *ino);
  3627. +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  3628. + ino_t ino);
  3629. +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *buf, size_t size,
  3630. + loff_t *pos);
  3631. +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
  3632. + size_t size, loff_t *pos);
  3633. +
  3634. +int au_xib_trunc(struct super_block *sb);
  3635. +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
  3636. +
  3637. +struct au_xino *au_xino_alloc(unsigned int nfile);
  3638. +int au_xino_put(struct au_branch *br);
  3639. +struct file *au_xino_file1(struct au_xino *xi);
  3640. +
  3641. +struct au_opt_xino;
  3642. +void au_xino_clr(struct super_block *sb);
  3643. +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
  3644. +struct file *au_xino_def(struct super_block *sb);
  3645. +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
  3646. + struct path *base);
  3647. +
  3648. +ino_t au_xino_new_ino(struct super_block *sb);
  3649. +void au_xino_delete_inode(struct inode *inode, const int unlinked);
  3650. +
  3651. +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
  3652. + ino_t h_ino, int idx);
  3653. +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  3654. + int *idx);
  3655. +
  3656. +int au_xino_path(struct seq_file *seq, struct file *file);
  3657. +
  3658. +/* ---------------------------------------------------------------------- */
  3659. +
  3660. +/* @idx is signed to accept -1 meaning the first file */
  3661. +static inline struct file *au_xino_file(struct au_xino *xi, int idx)
  3662. +{
  3663. + struct file *file;
  3664. +
  3665. + file = NULL;
  3666. + if (!xi)
  3667. + goto out;
  3668. +
  3669. + if (idx >= 0) {
  3670. + if (idx < xi->xi_nfile)
  3671. + file = xi->xi_file[idx];
  3672. + } else
  3673. + file = au_xino_file1(xi);
  3674. +
  3675. +out:
  3676. + return file;
  3677. +}
  3678. +
  3679. +/* ---------------------------------------------------------------------- */
  3680. +
  3681. +/* Superblock to branch */
  3682. +static inline
  3683. +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
  3684. +{
  3685. + return au_sbr(sb, bindex)->br_id;
  3686. +}
  3687. +
  3688. +static inline
  3689. +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
  3690. +{
  3691. + return au_br_mnt(au_sbr(sb, bindex));
  3692. +}
  3693. +
  3694. +static inline
  3695. +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
  3696. +{
  3697. + return au_br_sb(au_sbr(sb, bindex));
  3698. +}
  3699. +
  3700. +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
  3701. +{
  3702. + return au_sbr(sb, bindex)->br_perm;
  3703. +}
  3704. +
  3705. +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
  3706. +{
  3707. + return au_br_whable(au_sbr_perm(sb, bindex));
  3708. +}
  3709. +
  3710. +/* ---------------------------------------------------------------------- */
  3711. +
  3712. +#define wbr_wh_read_lock(wbr) au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
  3713. +#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
  3714. +#define wbr_wh_read_trylock(wbr) au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
  3715. +#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
  3716. +/*
  3717. +#define wbr_wh_read_trylock_nested(wbr) \
  3718. + au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
  3719. +#define wbr_wh_write_trylock_nested(wbr) \
  3720. + au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
  3721. +*/
  3722. +
  3723. +#define wbr_wh_read_unlock(wbr) au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
  3724. +#define wbr_wh_write_unlock(wbr) au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
  3725. +#define wbr_wh_downgrade_lock(wbr) au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
  3726. +
  3727. +#define WbrWhMustNoWaiters(wbr) AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
  3728. +#define WbrWhMustAnyLock(wbr) AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
  3729. +#define WbrWhMustWriteLock(wbr) AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
  3730. +
  3731. +/* ---------------------------------------------------------------------- */
  3732. +
  3733. +#ifdef CONFIG_AUFS_FHSM
  3734. +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
  3735. +{
  3736. + mutex_init(&brfhsm->bf_lock);
  3737. + brfhsm->bf_jiffy = 0;
  3738. + brfhsm->bf_readable = 0;
  3739. +}
  3740. +
  3741. +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
  3742. +{
  3743. + mutex_destroy(&brfhsm->bf_lock);
  3744. +}
  3745. +#else
  3746. +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
  3747. +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
  3748. +#endif
  3749. +
  3750. +#endif /* __KERNEL__ */
  3751. +#endif /* __AUFS_BRANCH_H__ */
  3752. diff -Naur null/fs/aufs/conf.mk linux-5.1/fs/aufs/conf.mk
  3753. --- /dev/null
  3754. +++ linux-5.1/fs/aufs/conf.mk 2019-05-22 11:23:07.438141134 +0200
  3755. @@ -0,0 +1,40 @@
  3756. +# SPDX-License-Identifier: GPL-2.0
  3757. +
  3758. +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
  3759. +
  3760. +define AuConf
  3761. +ifdef ${1}
  3762. +AuConfStr += ${1}=${${1}}
  3763. +endif
  3764. +endef
  3765. +
  3766. +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
  3767. + SBILIST \
  3768. + HNOTIFY HFSNOTIFY \
  3769. + EXPORT INO_T_64 \
  3770. + XATTR \
  3771. + FHSM \
  3772. + RDU \
  3773. + DIRREN \
  3774. + SHWH \
  3775. + BR_RAMFS \
  3776. + BR_FUSE POLL \
  3777. + BR_HFSPLUS \
  3778. + BDEV_LOOP \
  3779. + DEBUG MAGIC_SYSRQ
  3780. +$(foreach i, ${AuConfAll}, \
  3781. + $(eval $(call AuConf,CONFIG_AUFS_${i})))
  3782. +
  3783. +AuConfName = ${obj}/conf.str
  3784. +${AuConfName}.tmp: FORCE
  3785. + @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
  3786. +${AuConfName}: ${AuConfName}.tmp
  3787. + @diff -q $< $@ > /dev/null 2>&1 || { \
  3788. + echo ' GEN ' $@; \
  3789. + cp -p $< $@; \
  3790. + }
  3791. +FORCE:
  3792. +clean-files += ${AuConfName} ${AuConfName}.tmp
  3793. +${obj}/sysfs.o: ${AuConfName}
  3794. +
  3795. +-include ${srctree}/${src}/conf_priv.mk
  3796. diff -Naur null/fs/aufs/cpup.c linux-5.1/fs/aufs/cpup.c
  3797. --- /dev/null
  3798. +++ linux-5.1/fs/aufs/cpup.c 2019-05-22 11:23:07.438141134 +0200
  3799. @@ -0,0 +1,1458 @@
  3800. +// SPDX-License-Identifier: GPL-2.0
  3801. +/*
  3802. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  3803. + *
  3804. + * This program, aufs is free software; you can redistribute it and/or modify
  3805. + * it under the terms of the GNU General Public License as published by
  3806. + * the Free Software Foundation; either version 2 of the License, or
  3807. + * (at your option) any later version.
  3808. + *
  3809. + * This program is distributed in the hope that it will be useful,
  3810. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  3811. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  3812. + * GNU General Public License for more details.
  3813. + *
  3814. + * You should have received a copy of the GNU General Public License
  3815. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  3816. + */
  3817. +
  3818. +/*
  3819. + * copy-up functions, see wbr_policy.c for copy-down
  3820. + */
  3821. +
  3822. +#include <linux/fs_stack.h>
  3823. +#include <linux/mm.h>
  3824. +#include <linux/task_work.h>
  3825. +#include "aufs.h"
  3826. +
  3827. +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
  3828. +{
  3829. + const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
  3830. + | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
  3831. +
  3832. + BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
  3833. +
  3834. + dst->i_flags |= iflags & ~mask;
  3835. + if (au_test_fs_notime(dst->i_sb))
  3836. + dst->i_flags |= S_NOATIME | S_NOCMTIME;
  3837. +}
  3838. +
  3839. +void au_cpup_attr_timesizes(struct inode *inode)
  3840. +{
  3841. + struct inode *h_inode;
  3842. +
  3843. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  3844. + fsstack_copy_attr_times(inode, h_inode);
  3845. + fsstack_copy_inode_size(inode, h_inode);
  3846. +}
  3847. +
  3848. +void au_cpup_attr_nlink(struct inode *inode, int force)
  3849. +{
  3850. + struct inode *h_inode;
  3851. + struct super_block *sb;
  3852. + aufs_bindex_t bindex, bbot;
  3853. +
  3854. + sb = inode->i_sb;
  3855. + bindex = au_ibtop(inode);
  3856. + h_inode = au_h_iptr(inode, bindex);
  3857. + if (!force
  3858. + && !S_ISDIR(h_inode->i_mode)
  3859. + && au_opt_test(au_mntflags(sb), PLINK)
  3860. + && au_plink_test(inode))
  3861. + return;
  3862. +
  3863. + /*
  3864. + * 0 can happen in revalidating.
  3865. + * h_inode->i_mutex may not be held here, but it is harmless since once
  3866. + * i_nlink reaches 0, it will never become positive except O_TMPFILE
  3867. + * case.
  3868. + * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
  3869. + * the incorrect link count.
  3870. + */
  3871. + set_nlink(inode, h_inode->i_nlink);
  3872. +
  3873. + /*
  3874. + * fewer nlink makes find(1) noisy, but larger nlink doesn't.
  3875. + * it may includes whplink directory.
  3876. + */
  3877. + if (S_ISDIR(h_inode->i_mode)) {
  3878. + bbot = au_ibbot(inode);
  3879. + for (bindex++; bindex <= bbot; bindex++) {
  3880. + h_inode = au_h_iptr(inode, bindex);
  3881. + if (h_inode)
  3882. + au_add_nlink(inode, h_inode);
  3883. + }
  3884. + }
  3885. +}
  3886. +
  3887. +void au_cpup_attr_changeable(struct inode *inode)
  3888. +{
  3889. + struct inode *h_inode;
  3890. +
  3891. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  3892. + inode->i_mode = h_inode->i_mode;
  3893. + inode->i_uid = h_inode->i_uid;
  3894. + inode->i_gid = h_inode->i_gid;
  3895. + au_cpup_attr_timesizes(inode);
  3896. + au_cpup_attr_flags(inode, h_inode->i_flags);
  3897. +}
  3898. +
  3899. +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
  3900. +{
  3901. + struct au_iinfo *iinfo = au_ii(inode);
  3902. +
  3903. + IiMustWriteLock(inode);
  3904. +
  3905. + iinfo->ii_higen = h_inode->i_generation;
  3906. + iinfo->ii_hsb1 = h_inode->i_sb;
  3907. +}
  3908. +
  3909. +void au_cpup_attr_all(struct inode *inode, int force)
  3910. +{
  3911. + struct inode *h_inode;
  3912. +
  3913. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  3914. + au_cpup_attr_changeable(inode);
  3915. + if (inode->i_nlink > 0)
  3916. + au_cpup_attr_nlink(inode, force);
  3917. + inode->i_rdev = h_inode->i_rdev;
  3918. + inode->i_blkbits = h_inode->i_blkbits;
  3919. + au_cpup_igen(inode, h_inode);
  3920. +}
  3921. +
  3922. +/* ---------------------------------------------------------------------- */
  3923. +
  3924. +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
  3925. +
  3926. +/* keep the timestamps of the parent dir when cpup */
  3927. +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
  3928. + struct path *h_path)
  3929. +{
  3930. + struct inode *h_inode;
  3931. +
  3932. + dt->dt_dentry = dentry;
  3933. + dt->dt_h_path = *h_path;
  3934. + h_inode = d_inode(h_path->dentry);
  3935. + dt->dt_atime = h_inode->i_atime;
  3936. + dt->dt_mtime = h_inode->i_mtime;
  3937. + /* smp_mb(); */
  3938. +}
  3939. +
  3940. +void au_dtime_revert(struct au_dtime *dt)
  3941. +{
  3942. + struct iattr attr;
  3943. + int err;
  3944. +
  3945. + attr.ia_atime = dt->dt_atime;
  3946. + attr.ia_mtime = dt->dt_mtime;
  3947. + attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
  3948. + | ATTR_ATIME | ATTR_ATIME_SET;
  3949. +
  3950. + /* no delegation since this is a directory */
  3951. + err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
  3952. + if (unlikely(err))
  3953. + pr_warn("restoring timestamps failed(%d). ignored\n", err);
  3954. +}
  3955. +
  3956. +/* ---------------------------------------------------------------------- */
  3957. +
  3958. +/* internal use only */
  3959. +struct au_cpup_reg_attr {
  3960. + int valid;
  3961. + struct kstat st;
  3962. + unsigned int iflags; /* inode->i_flags */
  3963. +};
  3964. +
  3965. +static noinline_for_stack
  3966. +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct dentry *h_src,
  3967. + struct au_cpup_reg_attr *h_src_attr)
  3968. +{
  3969. + int err, sbits, icex;
  3970. + unsigned int mnt_flags;
  3971. + unsigned char verbose;
  3972. + struct iattr ia;
  3973. + struct path h_path;
  3974. + struct inode *h_isrc, *h_idst;
  3975. + struct kstat *h_st;
  3976. + struct au_branch *br;
  3977. +
  3978. + h_path.dentry = au_h_dptr(dst, bindex);
  3979. + h_idst = d_inode(h_path.dentry);
  3980. + br = au_sbr(dst->d_sb, bindex);
  3981. + h_path.mnt = au_br_mnt(br);
  3982. + h_isrc = d_inode(h_src);
  3983. + ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
  3984. + | ATTR_ATIME | ATTR_MTIME
  3985. + | ATTR_ATIME_SET | ATTR_MTIME_SET;
  3986. + if (h_src_attr && h_src_attr->valid) {
  3987. + h_st = &h_src_attr->st;
  3988. + ia.ia_uid = h_st->uid;
  3989. + ia.ia_gid = h_st->gid;
  3990. + ia.ia_atime = h_st->atime;
  3991. + ia.ia_mtime = h_st->mtime;
  3992. + if (h_idst->i_mode != h_st->mode
  3993. + && !S_ISLNK(h_idst->i_mode)) {
  3994. + ia.ia_valid |= ATTR_MODE;
  3995. + ia.ia_mode = h_st->mode;
  3996. + }
  3997. + sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
  3998. + au_cpup_attr_flags(h_idst, h_src_attr->iflags);
  3999. + } else {
  4000. + ia.ia_uid = h_isrc->i_uid;
  4001. + ia.ia_gid = h_isrc->i_gid;
  4002. + ia.ia_atime = h_isrc->i_atime;
  4003. + ia.ia_mtime = h_isrc->i_mtime;
  4004. + if (h_idst->i_mode != h_isrc->i_mode
  4005. + && !S_ISLNK(h_idst->i_mode)) {
  4006. + ia.ia_valid |= ATTR_MODE;
  4007. + ia.ia_mode = h_isrc->i_mode;
  4008. + }
  4009. + sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
  4010. + au_cpup_attr_flags(h_idst, h_isrc->i_flags);
  4011. + }
  4012. + /* no delegation since it is just created */
  4013. + err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
  4014. +
  4015. + /* is this nfs only? */
  4016. + if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
  4017. + ia.ia_valid = ATTR_FORCE | ATTR_MODE;
  4018. + ia.ia_mode = h_isrc->i_mode;
  4019. + err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
  4020. + }
  4021. +
  4022. + icex = br->br_perm & AuBrAttr_ICEX;
  4023. + if (!err) {
  4024. + mnt_flags = au_mntflags(dst->d_sb);
  4025. + verbose = !!au_opt_test(mnt_flags, VERBOSE);
  4026. + err = au_cpup_xattr(h_path.dentry, h_src, icex, verbose);
  4027. + }
  4028. +
  4029. + return err;
  4030. +}
  4031. +
  4032. +/* ---------------------------------------------------------------------- */
  4033. +
  4034. +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
  4035. + char *buf, unsigned long blksize)
  4036. +{
  4037. + int err;
  4038. + size_t sz, rbytes, wbytes;
  4039. + unsigned char all_zero;
  4040. + char *p, *zp;
  4041. + struct inode *h_inode;
  4042. + /* reduce stack usage */
  4043. + struct iattr *ia;
  4044. +
  4045. + zp = page_address(ZERO_PAGE(0));
  4046. + if (unlikely(!zp))
  4047. + return -ENOMEM; /* possible? */
  4048. +
  4049. + err = 0;
  4050. + all_zero = 0;
  4051. + while (len) {
  4052. + AuDbg("len %lld\n", len);
  4053. + sz = blksize;
  4054. + if (len < blksize)
  4055. + sz = len;
  4056. +
  4057. + rbytes = 0;
  4058. + /* todo: signal_pending? */
  4059. + while (!rbytes || err == -EAGAIN || err == -EINTR) {
  4060. + rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
  4061. + err = rbytes;
  4062. + }
  4063. + if (unlikely(err < 0))
  4064. + break;
  4065. +
  4066. + all_zero = 0;
  4067. + if (len >= rbytes && rbytes == blksize)
  4068. + all_zero = !memcmp(buf, zp, rbytes);
  4069. + if (!all_zero) {
  4070. + wbytes = rbytes;
  4071. + p = buf;
  4072. + while (wbytes) {
  4073. + size_t b;
  4074. +
  4075. + b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
  4076. + err = b;
  4077. + /* todo: signal_pending? */
  4078. + if (unlikely(err == -EAGAIN || err == -EINTR))
  4079. + continue;
  4080. + if (unlikely(err < 0))
  4081. + break;
  4082. + wbytes -= b;
  4083. + p += b;
  4084. + }
  4085. + if (unlikely(err < 0))
  4086. + break;
  4087. + } else {
  4088. + loff_t res;
  4089. +
  4090. + AuLabel(hole);
  4091. + res = vfsub_llseek(dst, rbytes, SEEK_CUR);
  4092. + err = res;
  4093. + if (unlikely(res < 0))
  4094. + break;
  4095. + }
  4096. + len -= rbytes;
  4097. + err = 0;
  4098. + }
  4099. +
  4100. + /* the last block may be a hole */
  4101. + if (!err && all_zero) {
  4102. + AuLabel(last hole);
  4103. +
  4104. + err = 1;
  4105. + if (au_test_nfs(dst->f_path.dentry->d_sb)) {
  4106. + /* nfs requires this step to make last hole */
  4107. + /* is this only nfs? */
  4108. + do {
  4109. + /* todo: signal_pending? */
  4110. + err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
  4111. + } while (err == -EAGAIN || err == -EINTR);
  4112. + if (err == 1)
  4113. + dst->f_pos--;
  4114. + }
  4115. +
  4116. + if (err == 1) {
  4117. + ia = (void *)buf;
  4118. + ia->ia_size = dst->f_pos;
  4119. + ia->ia_valid = ATTR_SIZE | ATTR_FILE;
  4120. + ia->ia_file = dst;
  4121. + h_inode = file_inode(dst);
  4122. + inode_lock_nested(h_inode, AuLsc_I_CHILD2);
  4123. + /* no delegation since it is just created */
  4124. + err = vfsub_notify_change(&dst->f_path, ia,
  4125. + /*delegated*/NULL);
  4126. + inode_unlock(h_inode);
  4127. + }
  4128. + }
  4129. +
  4130. + return err;
  4131. +}
  4132. +
  4133. +int au_copy_file(struct file *dst, struct file *src, loff_t len)
  4134. +{
  4135. + int err;
  4136. + unsigned long blksize;
  4137. + unsigned char do_kfree;
  4138. + char *buf;
  4139. + struct super_block *h_sb;
  4140. +
  4141. + err = -ENOMEM;
  4142. + h_sb = file_inode(dst)->i_sb;
  4143. + blksize = h_sb->s_blocksize;
  4144. + if (!blksize || PAGE_SIZE < blksize)
  4145. + blksize = PAGE_SIZE;
  4146. + AuDbg("blksize %lu\n", blksize);
  4147. + do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
  4148. + if (do_kfree)
  4149. + buf = kmalloc(blksize, GFP_NOFS);
  4150. + else
  4151. + buf = (void *)__get_free_page(GFP_NOFS);
  4152. + if (unlikely(!buf))
  4153. + goto out;
  4154. +
  4155. + if (len > (1 << 22))
  4156. + AuDbg("copying a large file %lld\n", (long long)len);
  4157. +
  4158. + src->f_pos = 0;
  4159. + dst->f_pos = 0;
  4160. + err = au_do_copy_file(dst, src, len, buf, blksize);
  4161. + if (do_kfree) {
  4162. + AuDebugOn(!au_kfree_do_sz_test(blksize));
  4163. + au_kfree_do_rcu(buf);
  4164. + } else
  4165. + free_page((unsigned long)buf);
  4166. +
  4167. +out:
  4168. + return err;
  4169. +}
  4170. +
  4171. +static int au_do_copy(struct file *dst, struct file *src, loff_t len)
  4172. +{
  4173. + int err;
  4174. + struct super_block *h_src_sb;
  4175. + struct inode *h_src_inode;
  4176. +
  4177. + h_src_inode = file_inode(src);
  4178. + h_src_sb = h_src_inode->i_sb;
  4179. +
  4180. + /* XFS acquires inode_lock */
  4181. + if (!au_test_xfs(h_src_sb))
  4182. + err = au_copy_file(dst, src, len);
  4183. + else {
  4184. + inode_unlock_shared(h_src_inode);
  4185. + err = au_copy_file(dst, src, len);
  4186. + inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
  4187. + }
  4188. +
  4189. + return err;
  4190. +}
  4191. +
  4192. +static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
  4193. +{
  4194. + int err;
  4195. + loff_t lo;
  4196. + struct super_block *h_src_sb;
  4197. + struct inode *h_src_inode;
  4198. +
  4199. + h_src_inode = file_inode(src);
  4200. + h_src_sb = h_src_inode->i_sb;
  4201. + if (h_src_sb != file_inode(dst)->i_sb
  4202. + || !dst->f_op->remap_file_range) {
  4203. + err = au_do_copy(dst, src, len);
  4204. + goto out;
  4205. + }
  4206. +
  4207. + if (!au_test_nfs(h_src_sb)) {
  4208. + inode_unlock_shared(h_src_inode);
  4209. + lo = vfsub_clone_file_range(src, dst, len);
  4210. + inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
  4211. + } else
  4212. + lo = vfsub_clone_file_range(src, dst, len);
  4213. + if (lo == len) {
  4214. + err = 0;
  4215. + goto out; /* success */
  4216. + } else if (lo >= 0)
  4217. + /* todo: possible? */
  4218. + /* paritially succeeded */
  4219. + AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
  4220. + else if (lo != -EOPNOTSUPP) {
  4221. + /* older XFS has a condition in cloning */
  4222. + err = lo;
  4223. + goto out;
  4224. + }
  4225. +
  4226. + /* the backend fs on NFS may not support cloning */
  4227. + err = au_do_copy(dst, src, len);
  4228. +
  4229. +out:
  4230. + AuTraceErr(err);
  4231. + return err;
  4232. +}
  4233. +
  4234. +/*
  4235. + * to support a sparse file which is opened with O_APPEND,
  4236. + * we need to close the file.
  4237. + */
  4238. +static int au_cp_regular(struct au_cp_generic *cpg)
  4239. +{
  4240. + int err, i;
  4241. + enum { SRC, DST };
  4242. + struct {
  4243. + aufs_bindex_t bindex;
  4244. + unsigned int flags;
  4245. + struct dentry *dentry;
  4246. + int force_wr;
  4247. + struct file *file;
  4248. + } *f, file[] = {
  4249. + {
  4250. + .bindex = cpg->bsrc,
  4251. + .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
  4252. + },
  4253. + {
  4254. + .bindex = cpg->bdst,
  4255. + .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
  4256. + .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
  4257. + }
  4258. + };
  4259. + struct au_branch *br;
  4260. + struct super_block *sb, *h_src_sb;
  4261. + struct inode *h_src_inode;
  4262. + struct task_struct *tsk = current;
  4263. +
  4264. + /* bsrc branch can be ro/rw. */
  4265. + sb = cpg->dentry->d_sb;
  4266. + f = file;
  4267. + for (i = 0; i < 2; i++, f++) {
  4268. + f->dentry = au_h_dptr(cpg->dentry, f->bindex);
  4269. + f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
  4270. + /*file*/NULL, f->force_wr);
  4271. + if (IS_ERR(f->file)) {
  4272. + err = PTR_ERR(f->file);
  4273. + if (i == SRC)
  4274. + goto out;
  4275. + else
  4276. + goto out_src;
  4277. + }
  4278. + }
  4279. +
  4280. + /* try stopping to update while we copyup */
  4281. + h_src_inode = d_inode(file[SRC].dentry);
  4282. + h_src_sb = h_src_inode->i_sb;
  4283. + if (!au_test_nfs(h_src_sb))
  4284. + IMustLock(h_src_inode);
  4285. + err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
  4286. +
  4287. + /* i wonder if we had O_NO_DELAY_FPUT flag */
  4288. + if (tsk->flags & PF_KTHREAD)
  4289. + __fput_sync(file[DST].file);
  4290. + else {
  4291. + /* it happened actually */
  4292. + fput(file[DST].file);
  4293. + /*
  4294. + * too bad.
  4295. + * we have to call both since we don't know which place the file
  4296. + * was added to.
  4297. + */
  4298. + task_work_run();
  4299. + flush_delayed_fput();
  4300. + }
  4301. + br = au_sbr(sb, file[DST].bindex);
  4302. + au_lcnt_dec(&br->br_nfiles);
  4303. +
  4304. +out_src:
  4305. + fput(file[SRC].file);
  4306. + br = au_sbr(sb, file[SRC].bindex);
  4307. + au_lcnt_dec(&br->br_nfiles);
  4308. +out:
  4309. + return err;
  4310. +}
  4311. +
  4312. +static int au_do_cpup_regular(struct au_cp_generic *cpg,
  4313. + struct au_cpup_reg_attr *h_src_attr)
  4314. +{
  4315. + int err, rerr;
  4316. + loff_t l;
  4317. + struct path h_path;
  4318. + struct inode *h_src_inode, *h_dst_inode;
  4319. +
  4320. + err = 0;
  4321. + h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
  4322. + l = i_size_read(h_src_inode);
  4323. + if (cpg->len == -1 || l < cpg->len)
  4324. + cpg->len = l;
  4325. + if (cpg->len) {
  4326. + /* try stopping to update while we are referencing */
  4327. + inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
  4328. + au_pin_hdir_unlock(cpg->pin);
  4329. +
  4330. + h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
  4331. + h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
  4332. + h_src_attr->iflags = h_src_inode->i_flags;
  4333. + if (!au_test_nfs(h_src_inode->i_sb))
  4334. + err = vfsub_getattr(&h_path, &h_src_attr->st);
  4335. + else {
  4336. + inode_unlock_shared(h_src_inode);
  4337. + err = vfsub_getattr(&h_path, &h_src_attr->st);
  4338. + inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
  4339. + }
  4340. + if (unlikely(err)) {
  4341. + inode_unlock_shared(h_src_inode);
  4342. + goto out;
  4343. + }
  4344. + h_src_attr->valid = 1;
  4345. + if (!au_test_nfs(h_src_inode->i_sb)) {
  4346. + err = au_cp_regular(cpg);
  4347. + inode_unlock_shared(h_src_inode);
  4348. + } else {
  4349. + inode_unlock_shared(h_src_inode);
  4350. + err = au_cp_regular(cpg);
  4351. + }
  4352. + rerr = au_pin_hdir_relock(cpg->pin);
  4353. + if (!err && rerr)
  4354. + err = rerr;
  4355. + }
  4356. + if (!err && (h_src_inode->i_state & I_LINKABLE)) {
  4357. + h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
  4358. + h_dst_inode = d_inode(h_path.dentry);
  4359. + spin_lock(&h_dst_inode->i_lock);
  4360. + h_dst_inode->i_state |= I_LINKABLE;
  4361. + spin_unlock(&h_dst_inode->i_lock);
  4362. + }
  4363. +
  4364. +out:
  4365. + return err;
  4366. +}
  4367. +
  4368. +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
  4369. + struct inode *h_dir)
  4370. +{
  4371. + int err, symlen;
  4372. + mm_segment_t old_fs;
  4373. + union {
  4374. + char *k;
  4375. + char __user *u;
  4376. + } sym;
  4377. +
  4378. + err = -ENOMEM;
  4379. + sym.k = (void *)__get_free_page(GFP_NOFS);
  4380. + if (unlikely(!sym.k))
  4381. + goto out;
  4382. +
  4383. + /* unnecessary to support mmap_sem since symlink is not mmap-able */
  4384. + old_fs = get_fs();
  4385. + set_fs(KERNEL_DS);
  4386. + symlen = vfs_readlink(h_src, sym.u, PATH_MAX);
  4387. + err = symlen;
  4388. + set_fs(old_fs);
  4389. +
  4390. + if (symlen > 0) {
  4391. + sym.k[symlen] = 0;
  4392. + err = vfsub_symlink(h_dir, h_path, sym.k);
  4393. + }
  4394. + free_page((unsigned long)sym.k);
  4395. +
  4396. +out:
  4397. + return err;
  4398. +}
  4399. +
  4400. +/*
  4401. + * regardless 'acl' option, reset all ACL.
  4402. + * All ACL will be copied up later from the original entry on the lower branch.
  4403. + */
  4404. +static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
  4405. +{
  4406. + int err;
  4407. + struct dentry *h_dentry;
  4408. + struct inode *h_inode;
  4409. +
  4410. + h_dentry = h_path->dentry;
  4411. + h_inode = d_inode(h_dentry);
  4412. + /* forget_all_cached_acls(h_inode)); */
  4413. + err = vfsub_removexattr(h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
  4414. + AuTraceErr(err);
  4415. + if (err == -EOPNOTSUPP)
  4416. + err = 0;
  4417. + if (!err)
  4418. + err = vfsub_acl_chmod(h_inode, mode);
  4419. +
  4420. + AuTraceErr(err);
  4421. + return err;
  4422. +}
  4423. +
  4424. +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
  4425. + struct inode *h_dir, struct path *h_path)
  4426. +{
  4427. + int err;
  4428. + struct inode *dir, *inode;
  4429. +
  4430. + err = vfsub_removexattr(h_path->dentry, XATTR_NAME_POSIX_ACL_DEFAULT);
  4431. + AuTraceErr(err);
  4432. + if (err == -EOPNOTSUPP)
  4433. + err = 0;
  4434. + if (unlikely(err))
  4435. + goto out;
  4436. +
  4437. + /*
  4438. + * strange behaviour from the users view,
  4439. + * particularly setattr case
  4440. + */
  4441. + dir = d_inode(dst_parent);
  4442. + if (au_ibtop(dir) == cpg->bdst)
  4443. + au_cpup_attr_nlink(dir, /*force*/1);
  4444. + inode = d_inode(cpg->dentry);
  4445. + au_cpup_attr_nlink(inode, /*force*/1);
  4446. +
  4447. +out:
  4448. + return err;
  4449. +}
  4450. +
  4451. +static noinline_for_stack
  4452. +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
  4453. + struct au_cpup_reg_attr *h_src_attr)
  4454. +{
  4455. + int err;
  4456. + umode_t mode;
  4457. + unsigned int mnt_flags;
  4458. + unsigned char isdir, isreg, force;
  4459. + const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
  4460. + struct au_dtime dt;
  4461. + struct path h_path;
  4462. + struct dentry *h_src, *h_dst, *h_parent;
  4463. + struct inode *h_inode, *h_dir;
  4464. + struct super_block *sb;
  4465. +
  4466. + /* bsrc branch can be ro/rw. */
  4467. + h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
  4468. + h_inode = d_inode(h_src);
  4469. + AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
  4470. +
  4471. + /* try stopping to be referenced while we are creating */
  4472. + h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
  4473. + if (au_ftest_cpup(cpg->flags, RENAME))
  4474. + AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
  4475. + AUFS_WH_PFX_LEN));
  4476. + h_parent = h_dst->d_parent; /* dir inode is locked */
  4477. + h_dir = d_inode(h_parent);
  4478. + IMustLock(h_dir);
  4479. + AuDebugOn(h_parent != h_dst->d_parent);
  4480. +
  4481. + sb = cpg->dentry->d_sb;
  4482. + h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
  4483. + if (do_dt) {
  4484. + h_path.dentry = h_parent;
  4485. + au_dtime_store(&dt, dst_parent, &h_path);
  4486. + }
  4487. + h_path.dentry = h_dst;
  4488. +
  4489. + isreg = 0;
  4490. + isdir = 0;
  4491. + mode = h_inode->i_mode;
  4492. + switch (mode & S_IFMT) {
  4493. + case S_IFREG:
  4494. + isreg = 1;
  4495. + err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
  4496. + if (!err)
  4497. + err = au_do_cpup_regular(cpg, h_src_attr);
  4498. + break;
  4499. + case S_IFDIR:
  4500. + isdir = 1;
  4501. + err = vfsub_mkdir(h_dir, &h_path, mode);
  4502. + if (!err)
  4503. + err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
  4504. + break;
  4505. + case S_IFLNK:
  4506. + err = au_do_cpup_symlink(&h_path, h_src, h_dir);
  4507. + break;
  4508. + case S_IFCHR:
  4509. + case S_IFBLK:
  4510. + AuDebugOn(!capable(CAP_MKNOD));
  4511. + /*FALLTHROUGH*/
  4512. + case S_IFIFO:
  4513. + case S_IFSOCK:
  4514. + err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
  4515. + break;
  4516. + default:
  4517. + AuIOErr("Unknown inode type 0%o\n", mode);
  4518. + err = -EIO;
  4519. + }
  4520. + if (!err)
  4521. + err = au_reset_acl(h_dir, &h_path, mode);
  4522. +
  4523. + mnt_flags = au_mntflags(sb);
  4524. + if (!au_opt_test(mnt_flags, UDBA_NONE)
  4525. + && !isdir
  4526. + && au_opt_test(mnt_flags, XINO)
  4527. + && (h_inode->i_nlink == 1
  4528. + || (h_inode->i_state & I_LINKABLE))
  4529. + /* todo: unnecessary? */
  4530. + /* && d_inode(cpg->dentry)->i_nlink == 1 */
  4531. + && cpg->bdst < cpg->bsrc
  4532. + && !au_ftest_cpup(cpg->flags, KEEPLINO))
  4533. + au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
  4534. + /* ignore this error */
  4535. +
  4536. + if (!err) {
  4537. + force = 0;
  4538. + if (isreg) {
  4539. + force = !!cpg->len;
  4540. + if (cpg->len == -1)
  4541. + force = !!i_size_read(h_inode);
  4542. + }
  4543. + au_fhsm_wrote(sb, cpg->bdst, force);
  4544. + }
  4545. +
  4546. + if (do_dt)
  4547. + au_dtime_revert(&dt);
  4548. + return err;
  4549. +}
  4550. +
  4551. +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
  4552. +{
  4553. + int err;
  4554. + struct dentry *dentry, *h_dentry, *h_parent, *parent;
  4555. + struct inode *h_dir;
  4556. + aufs_bindex_t bdst;
  4557. +
  4558. + dentry = cpg->dentry;
  4559. + bdst = cpg->bdst;
  4560. + h_dentry = au_h_dptr(dentry, bdst);
  4561. + if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
  4562. + dget(h_dentry);
  4563. + au_set_h_dptr(dentry, bdst, NULL);
  4564. + err = au_lkup_neg(dentry, bdst, /*wh*/0);
  4565. + if (!err)
  4566. + h_path->dentry = dget(au_h_dptr(dentry, bdst));
  4567. + au_set_h_dptr(dentry, bdst, h_dentry);
  4568. + } else {
  4569. + err = 0;
  4570. + parent = dget_parent(dentry);
  4571. + h_parent = au_h_dptr(parent, bdst);
  4572. + dput(parent);
  4573. + h_path->dentry = vfsub_lkup_one(&dentry->d_name, h_parent);
  4574. + if (IS_ERR(h_path->dentry))
  4575. + err = PTR_ERR(h_path->dentry);
  4576. + }
  4577. + if (unlikely(err))
  4578. + goto out;
  4579. +
  4580. + h_parent = h_dentry->d_parent; /* dir inode is locked */
  4581. + h_dir = d_inode(h_parent);
  4582. + IMustLock(h_dir);
  4583. + AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
  4584. + /* no delegation since it is just created */
  4585. + err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
  4586. + /*flags*/0);
  4587. + dput(h_path->dentry);
  4588. +
  4589. +out:
  4590. + return err;
  4591. +}
  4592. +
  4593. +/*
  4594. + * copyup the @dentry from @bsrc to @bdst.
  4595. + * the caller must set the both of lower dentries.
  4596. + * @len is for truncating when it is -1 copyup the entire file.
  4597. + * in link/rename cases, @dst_parent may be different from the real one.
  4598. + * basic->bsrc can be larger than basic->bdst.
  4599. + * aufs doesn't touch the credential so
  4600. + * security_inode_copy_up{,_xattr}() are unnecessary.
  4601. + */
  4602. +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
  4603. +{
  4604. + int err, rerr;
  4605. + aufs_bindex_t old_ibtop;
  4606. + unsigned char isdir, plink;
  4607. + struct dentry *h_src, *h_dst, *h_parent;
  4608. + struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
  4609. + struct super_block *sb;
  4610. + struct au_branch *br;
  4611. + /* to reduce stack size */
  4612. + struct {
  4613. + struct au_dtime dt;
  4614. + struct path h_path;
  4615. + struct au_cpup_reg_attr h_src_attr;
  4616. + } *a;
  4617. +
  4618. + err = -ENOMEM;
  4619. + a = kmalloc(sizeof(*a), GFP_NOFS);
  4620. + if (unlikely(!a))
  4621. + goto out;
  4622. + a->h_src_attr.valid = 0;
  4623. +
  4624. + sb = cpg->dentry->d_sb;
  4625. + br = au_sbr(sb, cpg->bdst);
  4626. + a->h_path.mnt = au_br_mnt(br);
  4627. + h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
  4628. + h_parent = h_dst->d_parent; /* dir inode is locked */
  4629. + h_dir = d_inode(h_parent);
  4630. + IMustLock(h_dir);
  4631. +
  4632. + h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
  4633. + inode = d_inode(cpg->dentry);
  4634. +
  4635. + if (!dst_parent)
  4636. + dst_parent = dget_parent(cpg->dentry);
  4637. + else
  4638. + dget(dst_parent);
  4639. +
  4640. + plink = !!au_opt_test(au_mntflags(sb), PLINK);
  4641. + dst_inode = au_h_iptr(inode, cpg->bdst);
  4642. + if (dst_inode) {
  4643. + if (unlikely(!plink)) {
  4644. + err = -EIO;
  4645. + AuIOErr("hi%lu(i%lu) exists on b%d "
  4646. + "but plink is disabled\n",
  4647. + dst_inode->i_ino, inode->i_ino, cpg->bdst);
  4648. + goto out_parent;
  4649. + }
  4650. +
  4651. + if (dst_inode->i_nlink) {
  4652. + const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
  4653. +
  4654. + h_src = au_plink_lkup(inode, cpg->bdst);
  4655. + err = PTR_ERR(h_src);
  4656. + if (IS_ERR(h_src))
  4657. + goto out_parent;
  4658. + if (unlikely(d_is_negative(h_src))) {
  4659. + err = -EIO;
  4660. + AuIOErr("i%lu exists on b%d "
  4661. + "but not pseudo-linked\n",
  4662. + inode->i_ino, cpg->bdst);
  4663. + dput(h_src);
  4664. + goto out_parent;
  4665. + }
  4666. +
  4667. + if (do_dt) {
  4668. + a->h_path.dentry = h_parent;
  4669. + au_dtime_store(&a->dt, dst_parent, &a->h_path);
  4670. + }
  4671. +
  4672. + a->h_path.dentry = h_dst;
  4673. + delegated = NULL;
  4674. + err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
  4675. + if (!err && au_ftest_cpup(cpg->flags, RENAME))
  4676. + err = au_do_ren_after_cpup(cpg, &a->h_path);
  4677. + if (do_dt)
  4678. + au_dtime_revert(&a->dt);
  4679. + if (unlikely(err == -EWOULDBLOCK)) {
  4680. + pr_warn("cannot retry for NFSv4 delegation"
  4681. + " for an internal link\n");
  4682. + iput(delegated);
  4683. + }
  4684. + dput(h_src);
  4685. + goto out_parent;
  4686. + } else
  4687. + /* todo: cpup_wh_file? */
  4688. + /* udba work */
  4689. + au_update_ibrange(inode, /*do_put_zero*/1);
  4690. + }
  4691. +
  4692. + isdir = S_ISDIR(inode->i_mode);
  4693. + old_ibtop = au_ibtop(inode);
  4694. + err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
  4695. + if (unlikely(err))
  4696. + goto out_rev;
  4697. + dst_inode = d_inode(h_dst);
  4698. + inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
  4699. + /* todo: necessary? */
  4700. + /* au_pin_hdir_unlock(cpg->pin); */
  4701. +
  4702. + err = cpup_iattr(cpg->dentry, cpg->bdst, h_src, &a->h_src_attr);
  4703. + if (unlikely(err)) {
  4704. + /* todo: necessary? */
  4705. + /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
  4706. + inode_unlock(dst_inode);
  4707. + goto out_rev;
  4708. + }
  4709. +
  4710. + if (cpg->bdst < old_ibtop) {
  4711. + if (S_ISREG(inode->i_mode)) {
  4712. + err = au_dy_iaop(inode, cpg->bdst, dst_inode);
  4713. + if (unlikely(err)) {
  4714. + /* ignore an error */
  4715. + /* au_pin_hdir_relock(cpg->pin); */
  4716. + inode_unlock(dst_inode);
  4717. + goto out_rev;
  4718. + }
  4719. + }
  4720. + au_set_ibtop(inode, cpg->bdst);
  4721. + } else
  4722. + au_set_ibbot(inode, cpg->bdst);
  4723. + au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
  4724. + au_hi_flags(inode, isdir));
  4725. +
  4726. + /* todo: necessary? */
  4727. + /* err = au_pin_hdir_relock(cpg->pin); */
  4728. + inode_unlock(dst_inode);
  4729. + if (unlikely(err))
  4730. + goto out_rev;
  4731. +
  4732. + src_inode = d_inode(h_src);
  4733. + if (!isdir
  4734. + && (src_inode->i_nlink > 1
  4735. + || src_inode->i_state & I_LINKABLE)
  4736. + && plink)
  4737. + au_plink_append(inode, cpg->bdst, h_dst);
  4738. +
  4739. + if (au_ftest_cpup(cpg->flags, RENAME)) {
  4740. + a->h_path.dentry = h_dst;
  4741. + err = au_do_ren_after_cpup(cpg, &a->h_path);
  4742. + }
  4743. + if (!err)
  4744. + goto out_parent; /* success */
  4745. +
  4746. + /* revert */
  4747. +out_rev:
  4748. + a->h_path.dentry = h_parent;
  4749. + au_dtime_store(&a->dt, dst_parent, &a->h_path);
  4750. + a->h_path.dentry = h_dst;
  4751. + rerr = 0;
  4752. + if (d_is_positive(h_dst)) {
  4753. + if (!isdir) {
  4754. + /* no delegation since it is just created */
  4755. + rerr = vfsub_unlink(h_dir, &a->h_path,
  4756. + /*delegated*/NULL, /*force*/0);
  4757. + } else
  4758. + rerr = vfsub_rmdir(h_dir, &a->h_path);
  4759. + }
  4760. + au_dtime_revert(&a->dt);
  4761. + if (rerr) {
  4762. + AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
  4763. + err = -EIO;
  4764. + }
  4765. +out_parent:
  4766. + dput(dst_parent);
  4767. + au_kfree_rcu(a);
  4768. +out:
  4769. + return err;
  4770. +}
  4771. +
  4772. +#if 0 /* reserved */
  4773. +struct au_cpup_single_args {
  4774. + int *errp;
  4775. + struct au_cp_generic *cpg;
  4776. + struct dentry *dst_parent;
  4777. +};
  4778. +
  4779. +static void au_call_cpup_single(void *args)
  4780. +{
  4781. + struct au_cpup_single_args *a = args;
  4782. +
  4783. + au_pin_hdir_acquire_nest(a->cpg->pin);
  4784. + *a->errp = au_cpup_single(a->cpg, a->dst_parent);
  4785. + au_pin_hdir_release(a->cpg->pin);
  4786. +}
  4787. +#endif
  4788. +
  4789. +/*
  4790. + * prevent SIGXFSZ in copy-up.
  4791. + * testing CAP_MKNOD is for generic fs,
  4792. + * but CAP_FSETID is for xfs only, currently.
  4793. + */
  4794. +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
  4795. +{
  4796. + int do_sio;
  4797. + struct super_block *sb;
  4798. + struct inode *h_dir;
  4799. +
  4800. + do_sio = 0;
  4801. + sb = au_pinned_parent(pin)->d_sb;
  4802. + if (!au_wkq_test()
  4803. + && (!au_sbi(sb)->si_plink_maint_pid
  4804. + || au_plink_maint(sb, AuLock_NOPLM))) {
  4805. + switch (mode & S_IFMT) {
  4806. + case S_IFREG:
  4807. + /* no condition about RLIMIT_FSIZE and the file size */
  4808. + do_sio = 1;
  4809. + break;
  4810. + case S_IFCHR:
  4811. + case S_IFBLK:
  4812. + do_sio = !capable(CAP_MKNOD);
  4813. + break;
  4814. + }
  4815. + if (!do_sio)
  4816. + do_sio = ((mode & (S_ISUID | S_ISGID))
  4817. + && !capable(CAP_FSETID));
  4818. + /* this workaround may be removed in the future */
  4819. + if (!do_sio) {
  4820. + h_dir = au_pinned_h_dir(pin);
  4821. + do_sio = h_dir->i_mode & S_ISVTX;
  4822. + }
  4823. + }
  4824. +
  4825. + return do_sio;
  4826. +}
  4827. +
  4828. +#if 0 /* reserved */
  4829. +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
  4830. +{
  4831. + int err, wkq_err;
  4832. + struct dentry *h_dentry;
  4833. +
  4834. + h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
  4835. + if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
  4836. + err = au_cpup_single(cpg, dst_parent);
  4837. + else {
  4838. + struct au_cpup_single_args args = {
  4839. + .errp = &err,
  4840. + .cpg = cpg,
  4841. + .dst_parent = dst_parent
  4842. + };
  4843. + wkq_err = au_wkq_wait(au_call_cpup_single, &args);
  4844. + if (unlikely(wkq_err))
  4845. + err = wkq_err;
  4846. + }
  4847. +
  4848. + return err;
  4849. +}
  4850. +#endif
  4851. +
  4852. +/*
  4853. + * copyup the @dentry from the first active lower branch to @bdst,
  4854. + * using au_cpup_single().
  4855. + */
  4856. +static int au_cpup_simple(struct au_cp_generic *cpg)
  4857. +{
  4858. + int err;
  4859. + unsigned int flags_orig;
  4860. + struct dentry *dentry;
  4861. +
  4862. + AuDebugOn(cpg->bsrc < 0);
  4863. +
  4864. + dentry = cpg->dentry;
  4865. + DiMustWriteLock(dentry);
  4866. +
  4867. + err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
  4868. + if (!err) {
  4869. + flags_orig = cpg->flags;
  4870. + au_fset_cpup(cpg->flags, RENAME);
  4871. + err = au_cpup_single(cpg, NULL);
  4872. + cpg->flags = flags_orig;
  4873. + if (!err)
  4874. + return 0; /* success */
  4875. +
  4876. + /* revert */
  4877. + au_set_h_dptr(dentry, cpg->bdst, NULL);
  4878. + au_set_dbtop(dentry, cpg->bsrc);
  4879. + }
  4880. +
  4881. + return err;
  4882. +}
  4883. +
  4884. +struct au_cpup_simple_args {
  4885. + int *errp;
  4886. + struct au_cp_generic *cpg;
  4887. +};
  4888. +
  4889. +static void au_call_cpup_simple(void *args)
  4890. +{
  4891. + struct au_cpup_simple_args *a = args;
  4892. +
  4893. + au_pin_hdir_acquire_nest(a->cpg->pin);
  4894. + *a->errp = au_cpup_simple(a->cpg);
  4895. + au_pin_hdir_release(a->cpg->pin);
  4896. +}
  4897. +
  4898. +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
  4899. +{
  4900. + int err, wkq_err;
  4901. + struct dentry *dentry, *parent;
  4902. + struct file *h_file;
  4903. + struct inode *h_dir;
  4904. +
  4905. + dentry = cpg->dentry;
  4906. + h_file = NULL;
  4907. + if (au_ftest_cpup(cpg->flags, HOPEN)) {
  4908. + AuDebugOn(cpg->bsrc < 0);
  4909. + h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
  4910. + err = PTR_ERR(h_file);
  4911. + if (IS_ERR(h_file))
  4912. + goto out;
  4913. + }
  4914. +
  4915. + parent = dget_parent(dentry);
  4916. + h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
  4917. + if (!au_test_h_perm_sio(h_dir, MAY_EXEC | MAY_WRITE)
  4918. + && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
  4919. + err = au_cpup_simple(cpg);
  4920. + else {
  4921. + struct au_cpup_simple_args args = {
  4922. + .errp = &err,
  4923. + .cpg = cpg
  4924. + };
  4925. + wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
  4926. + if (unlikely(wkq_err))
  4927. + err = wkq_err;
  4928. + }
  4929. +
  4930. + dput(parent);
  4931. + if (h_file)
  4932. + au_h_open_post(dentry, cpg->bsrc, h_file);
  4933. +
  4934. +out:
  4935. + return err;
  4936. +}
  4937. +
  4938. +int au_sio_cpup_simple(struct au_cp_generic *cpg)
  4939. +{
  4940. + aufs_bindex_t bsrc, bbot;
  4941. + struct dentry *dentry, *h_dentry;
  4942. +
  4943. + if (cpg->bsrc < 0) {
  4944. + dentry = cpg->dentry;
  4945. + bbot = au_dbbot(dentry);
  4946. + for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
  4947. + h_dentry = au_h_dptr(dentry, bsrc);
  4948. + if (h_dentry) {
  4949. + AuDebugOn(d_is_negative(h_dentry));
  4950. + break;
  4951. + }
  4952. + }
  4953. + AuDebugOn(bsrc > bbot);
  4954. + cpg->bsrc = bsrc;
  4955. + }
  4956. + AuDebugOn(cpg->bsrc <= cpg->bdst);
  4957. + return au_do_sio_cpup_simple(cpg);
  4958. +}
  4959. +
  4960. +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
  4961. +{
  4962. + AuDebugOn(cpg->bdst <= cpg->bsrc);
  4963. + return au_do_sio_cpup_simple(cpg);
  4964. +}
  4965. +
  4966. +/* ---------------------------------------------------------------------- */
  4967. +
  4968. +/*
  4969. + * copyup the deleted file for writing.
  4970. + */
  4971. +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
  4972. + struct file *file)
  4973. +{
  4974. + int err;
  4975. + unsigned int flags_orig;
  4976. + aufs_bindex_t bsrc_orig;
  4977. + struct au_dinfo *dinfo;
  4978. + struct {
  4979. + struct au_hdentry *hd;
  4980. + struct dentry *h_dentry;
  4981. + } hdst, hsrc;
  4982. +
  4983. + dinfo = au_di(cpg->dentry);
  4984. + AuRwMustWriteLock(&dinfo->di_rwsem);
  4985. +
  4986. + bsrc_orig = cpg->bsrc;
  4987. + cpg->bsrc = dinfo->di_btop;
  4988. + hdst.hd = au_hdentry(dinfo, cpg->bdst);
  4989. + hdst.h_dentry = hdst.hd->hd_dentry;
  4990. + hdst.hd->hd_dentry = wh_dentry;
  4991. + dinfo->di_btop = cpg->bdst;
  4992. +
  4993. + hsrc.h_dentry = NULL;
  4994. + if (file) {
  4995. + hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
  4996. + hsrc.h_dentry = hsrc.hd->hd_dentry;
  4997. + hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
  4998. + }
  4999. + flags_orig = cpg->flags;
  5000. + cpg->flags = !AuCpup_DTIME;
  5001. + err = au_cpup_single(cpg, /*h_parent*/NULL);
  5002. + cpg->flags = flags_orig;
  5003. + if (file) {
  5004. + if (!err)
  5005. + err = au_reopen_nondir(file);
  5006. + hsrc.hd->hd_dentry = hsrc.h_dentry;
  5007. + }
  5008. + hdst.hd->hd_dentry = hdst.h_dentry;
  5009. + dinfo->di_btop = cpg->bsrc;
  5010. + cpg->bsrc = bsrc_orig;
  5011. +
  5012. + return err;
  5013. +}
  5014. +
  5015. +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
  5016. +{
  5017. + int err;
  5018. + aufs_bindex_t bdst;
  5019. + struct au_dtime dt;
  5020. + struct dentry *dentry, *parent, *h_parent, *wh_dentry;
  5021. + struct au_branch *br;
  5022. + struct path h_path;
  5023. +
  5024. + dentry = cpg->dentry;
  5025. + bdst = cpg->bdst;
  5026. + br = au_sbr(dentry->d_sb, bdst);
  5027. + parent = dget_parent(dentry);
  5028. + h_parent = au_h_dptr(parent, bdst);
  5029. + wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
  5030. + err = PTR_ERR(wh_dentry);
  5031. + if (IS_ERR(wh_dentry))
  5032. + goto out;
  5033. +
  5034. + h_path.dentry = h_parent;
  5035. + h_path.mnt = au_br_mnt(br);
  5036. + au_dtime_store(&dt, parent, &h_path);
  5037. + err = au_do_cpup_wh(cpg, wh_dentry, file);
  5038. + if (unlikely(err))
  5039. + goto out_wh;
  5040. +
  5041. + dget(wh_dentry);
  5042. + h_path.dentry = wh_dentry;
  5043. + if (!d_is_dir(wh_dentry)) {
  5044. + /* no delegation since it is just created */
  5045. + err = vfsub_unlink(d_inode(h_parent), &h_path,
  5046. + /*delegated*/NULL, /*force*/0);
  5047. + } else
  5048. + err = vfsub_rmdir(d_inode(h_parent), &h_path);
  5049. + if (unlikely(err)) {
  5050. + AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
  5051. + wh_dentry, err);
  5052. + err = -EIO;
  5053. + }
  5054. + au_dtime_revert(&dt);
  5055. + au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
  5056. +
  5057. +out_wh:
  5058. + dput(wh_dentry);
  5059. +out:
  5060. + dput(parent);
  5061. + return err;
  5062. +}
  5063. +
  5064. +struct au_cpup_wh_args {
  5065. + int *errp;
  5066. + struct au_cp_generic *cpg;
  5067. + struct file *file;
  5068. +};
  5069. +
  5070. +static void au_call_cpup_wh(void *args)
  5071. +{
  5072. + struct au_cpup_wh_args *a = args;
  5073. +
  5074. + au_pin_hdir_acquire_nest(a->cpg->pin);
  5075. + *a->errp = au_cpup_wh(a->cpg, a->file);
  5076. + au_pin_hdir_release(a->cpg->pin);
  5077. +}
  5078. +
  5079. +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
  5080. +{
  5081. + int err, wkq_err;
  5082. + aufs_bindex_t bdst;
  5083. + struct dentry *dentry, *parent, *h_orph, *h_parent;
  5084. + struct inode *dir, *h_dir, *h_tmpdir;
  5085. + struct au_wbr *wbr;
  5086. + struct au_pin wh_pin, *pin_orig;
  5087. +
  5088. + dentry = cpg->dentry;
  5089. + bdst = cpg->bdst;
  5090. + parent = dget_parent(dentry);
  5091. + dir = d_inode(parent);
  5092. + h_orph = NULL;
  5093. + h_parent = NULL;
  5094. + h_dir = au_igrab(au_h_iptr(dir, bdst));
  5095. + h_tmpdir = h_dir;
  5096. + pin_orig = NULL;
  5097. + if (!h_dir->i_nlink) {
  5098. + wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
  5099. + h_orph = wbr->wbr_orph;
  5100. +
  5101. + h_parent = dget(au_h_dptr(parent, bdst));
  5102. + au_set_h_dptr(parent, bdst, dget(h_orph));
  5103. + h_tmpdir = d_inode(h_orph);
  5104. + au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
  5105. +
  5106. + inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
  5107. + /* todo: au_h_open_pre()? */
  5108. +
  5109. + pin_orig = cpg->pin;
  5110. + au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
  5111. + AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
  5112. + cpg->pin = &wh_pin;
  5113. + }
  5114. +
  5115. + if (!au_test_h_perm_sio(h_tmpdir, MAY_EXEC | MAY_WRITE)
  5116. + && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
  5117. + err = au_cpup_wh(cpg, file);
  5118. + else {
  5119. + struct au_cpup_wh_args args = {
  5120. + .errp = &err,
  5121. + .cpg = cpg,
  5122. + .file = file
  5123. + };
  5124. + wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
  5125. + if (unlikely(wkq_err))
  5126. + err = wkq_err;
  5127. + }
  5128. +
  5129. + if (h_orph) {
  5130. + inode_unlock(h_tmpdir);
  5131. + /* todo: au_h_open_post()? */
  5132. + au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
  5133. + au_set_h_dptr(parent, bdst, h_parent);
  5134. + AuDebugOn(!pin_orig);
  5135. + cpg->pin = pin_orig;
  5136. + }
  5137. + iput(h_dir);
  5138. + dput(parent);
  5139. +
  5140. + return err;
  5141. +}
  5142. +
  5143. +/* ---------------------------------------------------------------------- */
  5144. +
  5145. +/*
  5146. + * generic routine for both of copy-up and copy-down.
  5147. + */
  5148. +/* cf. revalidate function in file.c */
  5149. +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
  5150. + int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
  5151. + struct au_pin *pin,
  5152. + struct dentry *h_parent, void *arg),
  5153. + void *arg)
  5154. +{
  5155. + int err;
  5156. + struct au_pin pin;
  5157. + struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
  5158. +
  5159. + err = 0;
  5160. + parent = dget_parent(dentry);
  5161. + if (IS_ROOT(parent))
  5162. + goto out;
  5163. +
  5164. + au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
  5165. + au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
  5166. +
  5167. + /* do not use au_dpage */
  5168. + real_parent = parent;
  5169. + while (1) {
  5170. + dput(parent);
  5171. + parent = dget_parent(dentry);
  5172. + h_parent = au_h_dptr(parent, bdst);
  5173. + if (h_parent)
  5174. + goto out; /* success */
  5175. +
  5176. + /* find top dir which is necessary to cpup */
  5177. + do {
  5178. + d = parent;
  5179. + dput(parent);
  5180. + parent = dget_parent(d);
  5181. + di_read_lock_parent3(parent, !AuLock_IR);
  5182. + h_parent = au_h_dptr(parent, bdst);
  5183. + di_read_unlock(parent, !AuLock_IR);
  5184. + } while (!h_parent);
  5185. +
  5186. + if (d != real_parent)
  5187. + di_write_lock_child3(d);
  5188. +
  5189. + /* somebody else might create while we were sleeping */
  5190. + h_dentry = au_h_dptr(d, bdst);
  5191. + if (!h_dentry || d_is_negative(h_dentry)) {
  5192. + if (h_dentry)
  5193. + au_update_dbtop(d);
  5194. +
  5195. + au_pin_set_dentry(&pin, d);
  5196. + err = au_do_pin(&pin);
  5197. + if (!err) {
  5198. + err = cp(d, bdst, &pin, h_parent, arg);
  5199. + au_unpin(&pin);
  5200. + }
  5201. + }
  5202. +
  5203. + if (d != real_parent)
  5204. + di_write_unlock(d);
  5205. + if (unlikely(err))
  5206. + break;
  5207. + }
  5208. +
  5209. +out:
  5210. + dput(parent);
  5211. + return err;
  5212. +}
  5213. +
  5214. +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
  5215. + struct au_pin *pin,
  5216. + struct dentry *h_parent __maybe_unused,
  5217. + void *arg __maybe_unused)
  5218. +{
  5219. + struct au_cp_generic cpg = {
  5220. + .dentry = dentry,
  5221. + .bdst = bdst,
  5222. + .bsrc = -1,
  5223. + .len = 0,
  5224. + .pin = pin,
  5225. + .flags = AuCpup_DTIME
  5226. + };
  5227. + return au_sio_cpup_simple(&cpg);
  5228. +}
  5229. +
  5230. +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
  5231. +{
  5232. + return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
  5233. +}
  5234. +
  5235. +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
  5236. +{
  5237. + int err;
  5238. + struct dentry *parent;
  5239. + struct inode *dir;
  5240. +
  5241. + parent = dget_parent(dentry);
  5242. + dir = d_inode(parent);
  5243. + err = 0;
  5244. + if (au_h_iptr(dir, bdst))
  5245. + goto out;
  5246. +
  5247. + di_read_unlock(parent, AuLock_IR);
  5248. + di_write_lock_parent(parent);
  5249. + /* someone else might change our inode while we were sleeping */
  5250. + if (!au_h_iptr(dir, bdst))
  5251. + err = au_cpup_dirs(dentry, bdst);
  5252. + di_downgrade_lock(parent, AuLock_IR);
  5253. +
  5254. +out:
  5255. + dput(parent);
  5256. + return err;
  5257. +}
  5258. diff -Naur null/fs/aufs/cpup.h linux-5.1/fs/aufs/cpup.h
  5259. --- /dev/null
  5260. +++ linux-5.1/fs/aufs/cpup.h 2019-05-22 11:23:07.438141134 +0200
  5261. @@ -0,0 +1,100 @@
  5262. +/* SPDX-License-Identifier: GPL-2.0 */
  5263. +/*
  5264. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  5265. + *
  5266. + * This program, aufs is free software; you can redistribute it and/or modify
  5267. + * it under the terms of the GNU General Public License as published by
  5268. + * the Free Software Foundation; either version 2 of the License, or
  5269. + * (at your option) any later version.
  5270. + *
  5271. + * This program is distributed in the hope that it will be useful,
  5272. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5273. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5274. + * GNU General Public License for more details.
  5275. + *
  5276. + * You should have received a copy of the GNU General Public License
  5277. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5278. + */
  5279. +
  5280. +/*
  5281. + * copy-up/down functions
  5282. + */
  5283. +
  5284. +#ifndef __AUFS_CPUP_H__
  5285. +#define __AUFS_CPUP_H__
  5286. +
  5287. +#ifdef __KERNEL__
  5288. +
  5289. +#include <linux/path.h>
  5290. +
  5291. +struct inode;
  5292. +struct file;
  5293. +struct au_pin;
  5294. +
  5295. +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
  5296. +void au_cpup_attr_timesizes(struct inode *inode);
  5297. +void au_cpup_attr_nlink(struct inode *inode, int force);
  5298. +void au_cpup_attr_changeable(struct inode *inode);
  5299. +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
  5300. +void au_cpup_attr_all(struct inode *inode, int force);
  5301. +
  5302. +/* ---------------------------------------------------------------------- */
  5303. +
  5304. +struct au_cp_generic {
  5305. + struct dentry *dentry;
  5306. + aufs_bindex_t bdst, bsrc;
  5307. + loff_t len;
  5308. + struct au_pin *pin;
  5309. + unsigned int flags;
  5310. +};
  5311. +
  5312. +/* cpup flags */
  5313. +#define AuCpup_DTIME 1 /* do dtime_store/revert */
  5314. +#define AuCpup_KEEPLINO (1 << 1) /* do not clear the lower xino,
  5315. + for link(2) */
  5316. +#define AuCpup_RENAME (1 << 2) /* rename after cpup */
  5317. +#define AuCpup_HOPEN (1 << 3) /* call h_open_pre/post() in
  5318. + cpup */
  5319. +#define AuCpup_OVERWRITE (1 << 4) /* allow overwriting the
  5320. + existing entry */
  5321. +#define AuCpup_RWDST (1 << 5) /* force write target even if
  5322. + the branch is marked as RO */
  5323. +
  5324. +#ifndef CONFIG_AUFS_BR_HFSPLUS
  5325. +#undef AuCpup_HOPEN
  5326. +#define AuCpup_HOPEN 0
  5327. +#endif
  5328. +
  5329. +#define au_ftest_cpup(flags, name) ((flags) & AuCpup_##name)
  5330. +#define au_fset_cpup(flags, name) \
  5331. + do { (flags) |= AuCpup_##name; } while (0)
  5332. +#define au_fclr_cpup(flags, name) \
  5333. + do { (flags) &= ~AuCpup_##name; } while (0)
  5334. +
  5335. +int au_copy_file(struct file *dst, struct file *src, loff_t len);
  5336. +int au_sio_cpup_simple(struct au_cp_generic *cpg);
  5337. +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
  5338. +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
  5339. +
  5340. +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
  5341. + int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
  5342. + struct au_pin *pin,
  5343. + struct dentry *h_parent, void *arg),
  5344. + void *arg);
  5345. +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
  5346. +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
  5347. +
  5348. +/* ---------------------------------------------------------------------- */
  5349. +
  5350. +/* keep timestamps when copyup */
  5351. +struct au_dtime {
  5352. + struct dentry *dt_dentry;
  5353. + struct path dt_h_path;
  5354. + struct timespec64 dt_atime, dt_mtime;
  5355. +};
  5356. +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
  5357. + struct path *h_path);
  5358. +void au_dtime_revert(struct au_dtime *dt);
  5359. +
  5360. +#endif /* __KERNEL__ */
  5361. +#endif /* __AUFS_CPUP_H__ */
  5362. diff -Naur null/fs/aufs/dbgaufs.c linux-5.1/fs/aufs/dbgaufs.c
  5363. --- /dev/null
  5364. +++ linux-5.1/fs/aufs/dbgaufs.c 2019-05-22 11:23:07.438141134 +0200
  5365. @@ -0,0 +1,526 @@
  5366. +// SPDX-License-Identifier: GPL-2.0
  5367. +/*
  5368. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  5369. + *
  5370. + * This program, aufs is free software; you can redistribute it and/or modify
  5371. + * it under the terms of the GNU General Public License as published by
  5372. + * the Free Software Foundation; either version 2 of the License, or
  5373. + * (at your option) any later version.
  5374. + *
  5375. + * This program is distributed in the hope that it will be useful,
  5376. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5377. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5378. + * GNU General Public License for more details.
  5379. + *
  5380. + * You should have received a copy of the GNU General Public License
  5381. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5382. + */
  5383. +
  5384. +/*
  5385. + * debugfs interface
  5386. + */
  5387. +
  5388. +#include <linux/debugfs.h>
  5389. +#include "aufs.h"
  5390. +
  5391. +#ifndef CONFIG_SYSFS
  5392. +#error DEBUG_FS depends upon SYSFS
  5393. +#endif
  5394. +
  5395. +static struct dentry *dbgaufs;
  5396. +static const mode_t dbgaufs_mode = 0444;
  5397. +
  5398. +/* 20 is max digits length of ulong 64 */
  5399. +struct dbgaufs_arg {
  5400. + int n;
  5401. + char a[20 * 4];
  5402. +};
  5403. +
  5404. +/*
  5405. + * common function for all XINO files
  5406. + */
  5407. +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
  5408. + struct file *file)
  5409. +{
  5410. + void *p;
  5411. +
  5412. + p = file->private_data;
  5413. + if (p) {
  5414. + /* this is struct dbgaufs_arg */
  5415. + AuDebugOn(!au_kfree_sz_test(p));
  5416. + au_kfree_do_rcu(p);
  5417. + }
  5418. + return 0;
  5419. +}
  5420. +
  5421. +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
  5422. + int cnt)
  5423. +{
  5424. + int err;
  5425. + struct kstat st;
  5426. + struct dbgaufs_arg *p;
  5427. +
  5428. + err = -ENOMEM;
  5429. + p = kmalloc(sizeof(*p), GFP_NOFS);
  5430. + if (unlikely(!p))
  5431. + goto out;
  5432. +
  5433. + err = 0;
  5434. + p->n = 0;
  5435. + file->private_data = p;
  5436. + if (!xf)
  5437. + goto out;
  5438. +
  5439. + err = vfsub_getattr(&xf->f_path, &st);
  5440. + if (!err) {
  5441. + if (do_fcnt)
  5442. + p->n = snprintf
  5443. + (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
  5444. + cnt, st.blocks, st.blksize,
  5445. + (long long)st.size);
  5446. + else
  5447. + p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
  5448. + st.blocks, st.blksize,
  5449. + (long long)st.size);
  5450. + AuDebugOn(p->n >= sizeof(p->a));
  5451. + } else {
  5452. + p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
  5453. + err = 0;
  5454. + }
  5455. +
  5456. +out:
  5457. + return err;
  5458. +}
  5459. +
  5460. +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
  5461. + size_t count, loff_t *ppos)
  5462. +{
  5463. + struct dbgaufs_arg *p;
  5464. +
  5465. + p = file->private_data;
  5466. + return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
  5467. +}
  5468. +
  5469. +/* ---------------------------------------------------------------------- */
  5470. +
  5471. +struct dbgaufs_plink_arg {
  5472. + int n;
  5473. + char a[];
  5474. +};
  5475. +
  5476. +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
  5477. + struct file *file)
  5478. +{
  5479. + free_page((unsigned long)file->private_data);
  5480. + return 0;
  5481. +}
  5482. +
  5483. +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
  5484. +{
  5485. + int err, i, limit;
  5486. + unsigned long n, sum;
  5487. + struct dbgaufs_plink_arg *p;
  5488. + struct au_sbinfo *sbinfo;
  5489. + struct super_block *sb;
  5490. + struct hlist_bl_head *hbl;
  5491. +
  5492. + err = -ENOMEM;
  5493. + p = (void *)get_zeroed_page(GFP_NOFS);
  5494. + if (unlikely(!p))
  5495. + goto out;
  5496. +
  5497. + err = -EFBIG;
  5498. + sbinfo = inode->i_private;
  5499. + sb = sbinfo->si_sb;
  5500. + si_noflush_read_lock(sb);
  5501. + if (au_opt_test(au_mntflags(sb), PLINK)) {
  5502. + limit = PAGE_SIZE - sizeof(p->n);
  5503. +
  5504. + /* the number of buckets */
  5505. + n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
  5506. + p->n += n;
  5507. + limit -= n;
  5508. +
  5509. + sum = 0;
  5510. + for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
  5511. + i++, hbl++) {
  5512. + n = au_hbl_count(hbl);
  5513. + sum += n;
  5514. +
  5515. + n = snprintf(p->a + p->n, limit, "%lu ", n);
  5516. + p->n += n;
  5517. + limit -= n;
  5518. + if (unlikely(limit <= 0))
  5519. + goto out_free;
  5520. + }
  5521. + p->a[p->n - 1] = '\n';
  5522. +
  5523. + /* the sum of plinks */
  5524. + n = snprintf(p->a + p->n, limit, "%lu\n", sum);
  5525. + p->n += n;
  5526. + limit -= n;
  5527. + if (unlikely(limit <= 0))
  5528. + goto out_free;
  5529. + } else {
  5530. +#define str "1\n0\n0\n"
  5531. + p->n = sizeof(str) - 1;
  5532. + strcpy(p->a, str);
  5533. +#undef str
  5534. + }
  5535. + si_read_unlock(sb);
  5536. +
  5537. + err = 0;
  5538. + file->private_data = p;
  5539. + goto out; /* success */
  5540. +
  5541. +out_free:
  5542. + free_page((unsigned long)p);
  5543. +out:
  5544. + return err;
  5545. +}
  5546. +
  5547. +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
  5548. + size_t count, loff_t *ppos)
  5549. +{
  5550. + struct dbgaufs_plink_arg *p;
  5551. +
  5552. + p = file->private_data;
  5553. + return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
  5554. +}
  5555. +
  5556. +static const struct file_operations dbgaufs_plink_fop = {
  5557. + .owner = THIS_MODULE,
  5558. + .open = dbgaufs_plink_open,
  5559. + .release = dbgaufs_plink_release,
  5560. + .read = dbgaufs_plink_read
  5561. +};
  5562. +
  5563. +/* ---------------------------------------------------------------------- */
  5564. +
  5565. +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
  5566. +{
  5567. + int err;
  5568. + struct au_sbinfo *sbinfo;
  5569. + struct super_block *sb;
  5570. +
  5571. + sbinfo = inode->i_private;
  5572. + sb = sbinfo->si_sb;
  5573. + si_noflush_read_lock(sb);
  5574. + err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
  5575. + si_read_unlock(sb);
  5576. + return err;
  5577. +}
  5578. +
  5579. +static const struct file_operations dbgaufs_xib_fop = {
  5580. + .owner = THIS_MODULE,
  5581. + .open = dbgaufs_xib_open,
  5582. + .release = dbgaufs_xi_release,
  5583. + .read = dbgaufs_xi_read
  5584. +};
  5585. +
  5586. +/* ---------------------------------------------------------------------- */
  5587. +
  5588. +#define DbgaufsXi_PREFIX "xi"
  5589. +
  5590. +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
  5591. +{
  5592. + int err, idx;
  5593. + long l;
  5594. + aufs_bindex_t bindex;
  5595. + char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
  5596. + struct au_sbinfo *sbinfo;
  5597. + struct super_block *sb;
  5598. + struct au_xino *xi;
  5599. + struct file *xf;
  5600. + struct qstr *name;
  5601. + struct au_branch *br;
  5602. +
  5603. + err = -ENOENT;
  5604. + name = &file->f_path.dentry->d_name;
  5605. + if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
  5606. + || memcmp(name->name, DbgaufsXi_PREFIX,
  5607. + sizeof(DbgaufsXi_PREFIX) - 1)))
  5608. + goto out;
  5609. +
  5610. + AuDebugOn(name->len >= sizeof(a));
  5611. + memcpy(a, name->name, name->len);
  5612. + a[name->len] = '\0';
  5613. + p = strchr(a, '-');
  5614. + if (p)
  5615. + *p = '\0';
  5616. + err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
  5617. + if (unlikely(err))
  5618. + goto out;
  5619. + bindex = l;
  5620. + idx = 0;
  5621. + if (p) {
  5622. + err = kstrtol(p + 1, 10, &l);
  5623. + if (unlikely(err))
  5624. + goto out;
  5625. + idx = l;
  5626. + }
  5627. +
  5628. + err = -ENOENT;
  5629. + sbinfo = inode->i_private;
  5630. + sb = sbinfo->si_sb;
  5631. + si_noflush_read_lock(sb);
  5632. + if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
  5633. + goto out_si;
  5634. + br = au_sbr(sb, bindex);
  5635. + xi = br->br_xino;
  5636. + if (unlikely(idx >= xi->xi_nfile))
  5637. + goto out_si;
  5638. + xf = au_xino_file(xi, idx);
  5639. + if (xf)
  5640. + err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
  5641. + au_xino_count(br));
  5642. +
  5643. +out_si:
  5644. + si_read_unlock(sb);
  5645. +out:
  5646. + AuTraceErr(err);
  5647. + return err;
  5648. +}
  5649. +
  5650. +static const struct file_operations dbgaufs_xino_fop = {
  5651. + .owner = THIS_MODULE,
  5652. + .open = dbgaufs_xino_open,
  5653. + .release = dbgaufs_xi_release,
  5654. + .read = dbgaufs_xi_read
  5655. +};
  5656. +
  5657. +void dbgaufs_xino_del(struct au_branch *br)
  5658. +{
  5659. + struct dentry *dbgaufs;
  5660. +
  5661. + dbgaufs = br->br_dbgaufs;
  5662. + if (!dbgaufs)
  5663. + return;
  5664. +
  5665. + br->br_dbgaufs = NULL;
  5666. + /* debugfs acquires the parent i_mutex */
  5667. + lockdep_off();
  5668. + debugfs_remove(dbgaufs);
  5669. + lockdep_on();
  5670. +}
  5671. +
  5672. +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
  5673. +{
  5674. + aufs_bindex_t bbot;
  5675. + struct au_branch *br;
  5676. +
  5677. + if (!au_sbi(sb)->si_dbgaufs)
  5678. + return;
  5679. +
  5680. + bbot = au_sbbot(sb);
  5681. + for (; bindex <= bbot; bindex++) {
  5682. + br = au_sbr(sb, bindex);
  5683. + dbgaufs_xino_del(br);
  5684. + }
  5685. +}
  5686. +
  5687. +static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
  5688. + unsigned int idx, struct dentry *parent,
  5689. + struct au_sbinfo *sbinfo)
  5690. +{
  5691. + struct au_branch *br;
  5692. + struct dentry *d;
  5693. + /* "xi" bindex(5) "-" idx(2) NULL */
  5694. + char name[sizeof(DbgaufsXi_PREFIX) + 8];
  5695. +
  5696. + if (!idx)
  5697. + snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
  5698. + else
  5699. + snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
  5700. + bindex, idx);
  5701. + br = au_sbr(sb, bindex);
  5702. + if (br->br_dbgaufs) {
  5703. + struct qstr qstr = QSTR_INIT(name, strlen(name));
  5704. +
  5705. + if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
  5706. + /* debugfs acquires the parent i_mutex */
  5707. + lockdep_off();
  5708. + d = debugfs_rename(parent, br->br_dbgaufs, parent,
  5709. + name);
  5710. + lockdep_on();
  5711. + if (unlikely(!d))
  5712. + pr_warn("failed renaming %pd/%s, ignored.\n",
  5713. + parent, name);
  5714. + }
  5715. + } else {
  5716. + lockdep_off();
  5717. + br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
  5718. + sbinfo, &dbgaufs_xino_fop);
  5719. + lockdep_on();
  5720. + if (unlikely(!br->br_dbgaufs))
  5721. + pr_warn("failed creating %pd/%s, ignored.\n",
  5722. + parent, name);
  5723. + }
  5724. +}
  5725. +
  5726. +static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
  5727. + struct dentry *parent, struct au_sbinfo *sbinfo)
  5728. +{
  5729. + struct au_branch *br;
  5730. + struct au_xino *xi;
  5731. + unsigned int u;
  5732. +
  5733. + br = au_sbr(sb, bindex);
  5734. + xi = br->br_xino;
  5735. + for (u = 0; u < xi->xi_nfile; u++)
  5736. + dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
  5737. +}
  5738. +
  5739. +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
  5740. +{
  5741. + struct au_sbinfo *sbinfo;
  5742. + struct dentry *parent;
  5743. + aufs_bindex_t bbot;
  5744. +
  5745. + if (!au_opt_test(au_mntflags(sb), XINO))
  5746. + return;
  5747. +
  5748. + sbinfo = au_sbi(sb);
  5749. + parent = sbinfo->si_dbgaufs;
  5750. + if (!parent)
  5751. + return;
  5752. +
  5753. + bbot = au_sbbot(sb);
  5754. + if (topdown)
  5755. + for (; bindex <= bbot; bindex++)
  5756. + dbgaufs_br_add(sb, bindex, parent, sbinfo);
  5757. + else
  5758. + for (; bbot >= bindex; bbot--)
  5759. + dbgaufs_br_add(sb, bbot, parent, sbinfo);
  5760. +}
  5761. +
  5762. +/* ---------------------------------------------------------------------- */
  5763. +
  5764. +#ifdef CONFIG_AUFS_EXPORT
  5765. +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
  5766. +{
  5767. + int err;
  5768. + struct au_sbinfo *sbinfo;
  5769. + struct super_block *sb;
  5770. +
  5771. + sbinfo = inode->i_private;
  5772. + sb = sbinfo->si_sb;
  5773. + si_noflush_read_lock(sb);
  5774. + err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
  5775. + si_read_unlock(sb);
  5776. + return err;
  5777. +}
  5778. +
  5779. +static const struct file_operations dbgaufs_xigen_fop = {
  5780. + .owner = THIS_MODULE,
  5781. + .open = dbgaufs_xigen_open,
  5782. + .release = dbgaufs_xi_release,
  5783. + .read = dbgaufs_xi_read
  5784. +};
  5785. +
  5786. +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
  5787. +{
  5788. + int err;
  5789. +
  5790. + /*
  5791. + * This function is a dynamic '__init' function actually,
  5792. + * so the tiny check for si_rwsem is unnecessary.
  5793. + */
  5794. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  5795. +
  5796. + err = -EIO;
  5797. + sbinfo->si_dbgaufs_xigen = debugfs_create_file
  5798. + ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
  5799. + &dbgaufs_xigen_fop);
  5800. + if (sbinfo->si_dbgaufs_xigen)
  5801. + err = 0;
  5802. +
  5803. + return err;
  5804. +}
  5805. +#else
  5806. +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
  5807. +{
  5808. + return 0;
  5809. +}
  5810. +#endif /* CONFIG_AUFS_EXPORT */
  5811. +
  5812. +/* ---------------------------------------------------------------------- */
  5813. +
  5814. +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
  5815. +{
  5816. + /*
  5817. + * This function is a dynamic '__fin' function actually,
  5818. + * so the tiny check for si_rwsem is unnecessary.
  5819. + */
  5820. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  5821. +
  5822. + debugfs_remove_recursive(sbinfo->si_dbgaufs);
  5823. + sbinfo->si_dbgaufs = NULL;
  5824. +}
  5825. +
  5826. +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
  5827. +{
  5828. + int err;
  5829. + char name[SysaufsSiNameLen];
  5830. +
  5831. + /*
  5832. + * This function is a dynamic '__init' function actually,
  5833. + * so the tiny check for si_rwsem is unnecessary.
  5834. + */
  5835. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  5836. +
  5837. + err = -ENOENT;
  5838. + if (!dbgaufs) {
  5839. + AuErr1("/debug/aufs is uninitialized\n");
  5840. + goto out;
  5841. + }
  5842. +
  5843. + err = -EIO;
  5844. + sysaufs_name(sbinfo, name);
  5845. + sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
  5846. + if (unlikely(!sbinfo->si_dbgaufs))
  5847. + goto out;
  5848. +
  5849. + /* regardless plink/noplink option */
  5850. + sbinfo->si_dbgaufs_plink = debugfs_create_file
  5851. + ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
  5852. + &dbgaufs_plink_fop);
  5853. + if (unlikely(!sbinfo->si_dbgaufs_plink))
  5854. + goto out_dir;
  5855. +
  5856. + /* regardless xino/noxino option */
  5857. + sbinfo->si_dbgaufs_xib = debugfs_create_file
  5858. + ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
  5859. + &dbgaufs_xib_fop);
  5860. + if (unlikely(!sbinfo->si_dbgaufs_xib))
  5861. + goto out_dir;
  5862. +
  5863. + err = dbgaufs_xigen_init(sbinfo);
  5864. + if (!err)
  5865. + goto out; /* success */
  5866. +
  5867. +out_dir:
  5868. + dbgaufs_si_fin(sbinfo);
  5869. +out:
  5870. + if (unlikely(err))
  5871. + pr_err("debugfs/aufs failed\n");
  5872. + return err;
  5873. +}
  5874. +
  5875. +/* ---------------------------------------------------------------------- */
  5876. +
  5877. +void dbgaufs_fin(void)
  5878. +{
  5879. + debugfs_remove(dbgaufs);
  5880. +}
  5881. +
  5882. +int __init dbgaufs_init(void)
  5883. +{
  5884. + int err;
  5885. +
  5886. + err = -EIO;
  5887. + dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
  5888. + if (dbgaufs)
  5889. + err = 0;
  5890. + return err;
  5891. +}
  5892. diff -Naur null/fs/aufs/dbgaufs.h linux-5.1/fs/aufs/dbgaufs.h
  5893. --- /dev/null
  5894. +++ linux-5.1/fs/aufs/dbgaufs.h 2019-05-22 11:23:07.438141134 +0200
  5895. @@ -0,0 +1,53 @@
  5896. +/* SPDX-License-Identifier: GPL-2.0 */
  5897. +/*
  5898. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  5899. + *
  5900. + * This program, aufs is free software; you can redistribute it and/or modify
  5901. + * it under the terms of the GNU General Public License as published by
  5902. + * the Free Software Foundation; either version 2 of the License, or
  5903. + * (at your option) any later version.
  5904. + *
  5905. + * This program is distributed in the hope that it will be useful,
  5906. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5907. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5908. + * GNU General Public License for more details.
  5909. + *
  5910. + * You should have received a copy of the GNU General Public License
  5911. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5912. + */
  5913. +
  5914. +/*
  5915. + * debugfs interface
  5916. + */
  5917. +
  5918. +#ifndef __DBGAUFS_H__
  5919. +#define __DBGAUFS_H__
  5920. +
  5921. +#ifdef __KERNEL__
  5922. +
  5923. +struct super_block;
  5924. +struct au_sbinfo;
  5925. +struct au_branch;
  5926. +
  5927. +#ifdef CONFIG_DEBUG_FS
  5928. +/* dbgaufs.c */
  5929. +void dbgaufs_xino_del(struct au_branch *br);
  5930. +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
  5931. +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
  5932. +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
  5933. +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
  5934. +void dbgaufs_fin(void);
  5935. +int __init dbgaufs_init(void);
  5936. +#else
  5937. +AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
  5938. +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
  5939. +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
  5940. + int topdown)
  5941. +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
  5942. +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
  5943. +AuStubVoid(dbgaufs_fin, void)
  5944. +AuStubInt0(__init dbgaufs_init, void)
  5945. +#endif /* CONFIG_DEBUG_FS */
  5946. +
  5947. +#endif /* __KERNEL__ */
  5948. +#endif /* __DBGAUFS_H__ */
  5949. diff -Naur null/fs/aufs/dcsub.c linux-5.1/fs/aufs/dcsub.c
  5950. --- /dev/null
  5951. +++ linux-5.1/fs/aufs/dcsub.c 2019-05-22 11:23:07.438141134 +0200
  5952. @@ -0,0 +1,225 @@
  5953. +// SPDX-License-Identifier: GPL-2.0
  5954. +/*
  5955. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  5956. + *
  5957. + * This program, aufs is free software; you can redistribute it and/or modify
  5958. + * it under the terms of the GNU General Public License as published by
  5959. + * the Free Software Foundation; either version 2 of the License, or
  5960. + * (at your option) any later version.
  5961. + *
  5962. + * This program is distributed in the hope that it will be useful,
  5963. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  5964. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  5965. + * GNU General Public License for more details.
  5966. + *
  5967. + * You should have received a copy of the GNU General Public License
  5968. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  5969. + */
  5970. +
  5971. +/*
  5972. + * sub-routines for dentry cache
  5973. + */
  5974. +
  5975. +#include "aufs.h"
  5976. +
  5977. +static void au_dpage_free(struct au_dpage *dpage)
  5978. +{
  5979. + int i;
  5980. + struct dentry **p;
  5981. +
  5982. + p = dpage->dentries;
  5983. + for (i = 0; i < dpage->ndentry; i++)
  5984. + dput(*p++);
  5985. + free_page((unsigned long)dpage->dentries);
  5986. +}
  5987. +
  5988. +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
  5989. +{
  5990. + int err;
  5991. + void *p;
  5992. +
  5993. + err = -ENOMEM;
  5994. + dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
  5995. + if (unlikely(!dpages->dpages))
  5996. + goto out;
  5997. +
  5998. + p = (void *)__get_free_page(gfp);
  5999. + if (unlikely(!p))
  6000. + goto out_dpages;
  6001. +
  6002. + dpages->dpages[0].ndentry = 0;
  6003. + dpages->dpages[0].dentries = p;
  6004. + dpages->ndpage = 1;
  6005. + return 0; /* success */
  6006. +
  6007. +out_dpages:
  6008. + au_kfree_try_rcu(dpages->dpages);
  6009. +out:
  6010. + return err;
  6011. +}
  6012. +
  6013. +void au_dpages_free(struct au_dcsub_pages *dpages)
  6014. +{
  6015. + int i;
  6016. + struct au_dpage *p;
  6017. +
  6018. + p = dpages->dpages;
  6019. + for (i = 0; i < dpages->ndpage; i++)
  6020. + au_dpage_free(p++);
  6021. + au_kfree_try_rcu(dpages->dpages);
  6022. +}
  6023. +
  6024. +static int au_dpages_append(struct au_dcsub_pages *dpages,
  6025. + struct dentry *dentry, gfp_t gfp)
  6026. +{
  6027. + int err, sz;
  6028. + struct au_dpage *dpage;
  6029. + void *p;
  6030. +
  6031. + dpage = dpages->dpages + dpages->ndpage - 1;
  6032. + sz = PAGE_SIZE / sizeof(dentry);
  6033. + if (unlikely(dpage->ndentry >= sz)) {
  6034. + AuLabel(new dpage);
  6035. + err = -ENOMEM;
  6036. + sz = dpages->ndpage * sizeof(*dpages->dpages);
  6037. + p = au_kzrealloc(dpages->dpages, sz,
  6038. + sz + sizeof(*dpages->dpages), gfp,
  6039. + /*may_shrink*/0);
  6040. + if (unlikely(!p))
  6041. + goto out;
  6042. +
  6043. + dpages->dpages = p;
  6044. + dpage = dpages->dpages + dpages->ndpage;
  6045. + p = (void *)__get_free_page(gfp);
  6046. + if (unlikely(!p))
  6047. + goto out;
  6048. +
  6049. + dpage->ndentry = 0;
  6050. + dpage->dentries = p;
  6051. + dpages->ndpage++;
  6052. + }
  6053. +
  6054. + AuDebugOn(au_dcount(dentry) <= 0);
  6055. + dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
  6056. + return 0; /* success */
  6057. +
  6058. +out:
  6059. + return err;
  6060. +}
  6061. +
  6062. +/* todo: BAD approach */
  6063. +/* copied from linux/fs/dcache.c */
  6064. +enum d_walk_ret {
  6065. + D_WALK_CONTINUE,
  6066. + D_WALK_QUIT,
  6067. + D_WALK_NORETRY,
  6068. + D_WALK_SKIP,
  6069. +};
  6070. +
  6071. +extern void d_walk(struct dentry *parent, void *data,
  6072. + enum d_walk_ret (*enter)(void *, struct dentry *));
  6073. +
  6074. +struct ac_dpages_arg {
  6075. + int err;
  6076. + struct au_dcsub_pages *dpages;
  6077. + struct super_block *sb;
  6078. + au_dpages_test test;
  6079. + void *arg;
  6080. +};
  6081. +
  6082. +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
  6083. +{
  6084. + enum d_walk_ret ret;
  6085. + struct ac_dpages_arg *arg = _arg;
  6086. +
  6087. + ret = D_WALK_CONTINUE;
  6088. + if (dentry->d_sb == arg->sb
  6089. + && !IS_ROOT(dentry)
  6090. + && au_dcount(dentry) > 0
  6091. + && au_di(dentry)
  6092. + && (!arg->test || arg->test(dentry, arg->arg))) {
  6093. + arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
  6094. + if (unlikely(arg->err))
  6095. + ret = D_WALK_QUIT;
  6096. + }
  6097. +
  6098. + return ret;
  6099. +}
  6100. +
  6101. +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
  6102. + au_dpages_test test, void *arg)
  6103. +{
  6104. + struct ac_dpages_arg args = {
  6105. + .err = 0,
  6106. + .dpages = dpages,
  6107. + .sb = root->d_sb,
  6108. + .test = test,
  6109. + .arg = arg
  6110. + };
  6111. +
  6112. + d_walk(root, &args, au_call_dpages_append);
  6113. +
  6114. + return args.err;
  6115. +}
  6116. +
  6117. +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
  6118. + int do_include, au_dpages_test test, void *arg)
  6119. +{
  6120. + int err;
  6121. +
  6122. + err = 0;
  6123. + write_seqlock(&rename_lock);
  6124. + spin_lock(&dentry->d_lock);
  6125. + if (do_include
  6126. + && au_dcount(dentry) > 0
  6127. + && (!test || test(dentry, arg)))
  6128. + err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
  6129. + spin_unlock(&dentry->d_lock);
  6130. + if (unlikely(err))
  6131. + goto out;
  6132. +
  6133. + /*
  6134. + * RCU for vfsmount is unnecessary since this is a traverse in a single
  6135. + * mount
  6136. + */
  6137. + while (!IS_ROOT(dentry)) {
  6138. + dentry = dentry->d_parent; /* rename_lock is locked */
  6139. + spin_lock(&dentry->d_lock);
  6140. + if (au_dcount(dentry) > 0
  6141. + && (!test || test(dentry, arg)))
  6142. + err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
  6143. + spin_unlock(&dentry->d_lock);
  6144. + if (unlikely(err))
  6145. + break;
  6146. + }
  6147. +
  6148. +out:
  6149. + write_sequnlock(&rename_lock);
  6150. + return err;
  6151. +}
  6152. +
  6153. +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
  6154. +{
  6155. + return au_di(dentry) && dentry->d_sb == arg;
  6156. +}
  6157. +
  6158. +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
  6159. + struct dentry *dentry, int do_include)
  6160. +{
  6161. + return au_dcsub_pages_rev(dpages, dentry, do_include,
  6162. + au_dcsub_dpages_aufs, dentry->d_sb);
  6163. +}
  6164. +
  6165. +int au_test_subdir(struct dentry *d1, struct dentry *d2)
  6166. +{
  6167. + struct path path[2] = {
  6168. + {
  6169. + .dentry = d1
  6170. + },
  6171. + {
  6172. + .dentry = d2
  6173. + }
  6174. + };
  6175. +
  6176. + return path_is_under(path + 0, path + 1);
  6177. +}
  6178. diff -Naur null/fs/aufs/dcsub.h linux-5.1/fs/aufs/dcsub.h
  6179. --- /dev/null
  6180. +++ linux-5.1/fs/aufs/dcsub.h 2019-05-22 11:23:07.438141134 +0200
  6181. @@ -0,0 +1,137 @@
  6182. +/* SPDX-License-Identifier: GPL-2.0 */
  6183. +/*
  6184. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  6185. + *
  6186. + * This program, aufs is free software; you can redistribute it and/or modify
  6187. + * it under the terms of the GNU General Public License as published by
  6188. + * the Free Software Foundation; either version 2 of the License, or
  6189. + * (at your option) any later version.
  6190. + *
  6191. + * This program is distributed in the hope that it will be useful,
  6192. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6193. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6194. + * GNU General Public License for more details.
  6195. + *
  6196. + * You should have received a copy of the GNU General Public License
  6197. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  6198. + */
  6199. +
  6200. +/*
  6201. + * sub-routines for dentry cache
  6202. + */
  6203. +
  6204. +#ifndef __AUFS_DCSUB_H__
  6205. +#define __AUFS_DCSUB_H__
  6206. +
  6207. +#ifdef __KERNEL__
  6208. +
  6209. +#include <linux/dcache.h>
  6210. +#include <linux/fs.h>
  6211. +
  6212. +struct au_dpage {
  6213. + int ndentry;
  6214. + struct dentry **dentries;
  6215. +};
  6216. +
  6217. +struct au_dcsub_pages {
  6218. + int ndpage;
  6219. + struct au_dpage *dpages;
  6220. +};
  6221. +
  6222. +/* ---------------------------------------------------------------------- */
  6223. +
  6224. +/* dcsub.c */
  6225. +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
  6226. +void au_dpages_free(struct au_dcsub_pages *dpages);
  6227. +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
  6228. +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
  6229. + au_dpages_test test, void *arg);
  6230. +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
  6231. + int do_include, au_dpages_test test, void *arg);
  6232. +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
  6233. + struct dentry *dentry, int do_include);
  6234. +int au_test_subdir(struct dentry *d1, struct dentry *d2);
  6235. +
  6236. +/* ---------------------------------------------------------------------- */
  6237. +
  6238. +/*
  6239. + * todo: in linux-3.13, several similar (but faster) helpers are added to
  6240. + * include/linux/dcache.h. Try them (in the future).
  6241. + */
  6242. +
  6243. +static inline int au_d_hashed_positive(struct dentry *d)
  6244. +{
  6245. + int err;
  6246. + struct inode *inode = d_inode(d);
  6247. +
  6248. + err = 0;
  6249. + if (unlikely(d_unhashed(d)
  6250. + || d_is_negative(d)
  6251. + || !inode->i_nlink))
  6252. + err = -ENOENT;
  6253. + return err;
  6254. +}
  6255. +
  6256. +static inline int au_d_linkable(struct dentry *d)
  6257. +{
  6258. + int err;
  6259. + struct inode *inode = d_inode(d);
  6260. +
  6261. + err = au_d_hashed_positive(d);
  6262. + if (err
  6263. + && d_is_positive(d)
  6264. + && (inode->i_state & I_LINKABLE))
  6265. + err = 0;
  6266. + return err;
  6267. +}
  6268. +
  6269. +static inline int au_d_alive(struct dentry *d)
  6270. +{
  6271. + int err;
  6272. + struct inode *inode;
  6273. +
  6274. + err = 0;
  6275. + if (!IS_ROOT(d))
  6276. + err = au_d_hashed_positive(d);
  6277. + else {
  6278. + inode = d_inode(d);
  6279. + if (unlikely(d_unlinked(d)
  6280. + || d_is_negative(d)
  6281. + || !inode->i_nlink))
  6282. + err = -ENOENT;
  6283. + }
  6284. + return err;
  6285. +}
  6286. +
  6287. +static inline int au_alive_dir(struct dentry *d)
  6288. +{
  6289. + int err;
  6290. +
  6291. + err = au_d_alive(d);
  6292. + if (unlikely(err || IS_DEADDIR(d_inode(d))))
  6293. + err = -ENOENT;
  6294. + return err;
  6295. +}
  6296. +
  6297. +static inline int au_qstreq(struct qstr *a, struct qstr *b)
  6298. +{
  6299. + return a->len == b->len
  6300. + && !memcmp(a->name, b->name, a->len);
  6301. +}
  6302. +
  6303. +/*
  6304. + * by the commit
  6305. + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
  6306. + * taking d_lock
  6307. + * the type of d_lockref.count became int, but the inlined function d_count()
  6308. + * still returns unsigned int.
  6309. + * I don't know why. Maybe it is for every d_count() users?
  6310. + * Anyway au_dcount() lives on.
  6311. + */
  6312. +static inline int au_dcount(struct dentry *d)
  6313. +{
  6314. + return (int)d_count(d);
  6315. +}
  6316. +
  6317. +#endif /* __KERNEL__ */
  6318. +#endif /* __AUFS_DCSUB_H__ */
  6319. diff -Naur null/fs/aufs/debug.c linux-5.1/fs/aufs/debug.c
  6320. --- /dev/null
  6321. +++ linux-5.1/fs/aufs/debug.c 2019-05-22 11:23:07.438141134 +0200
  6322. @@ -0,0 +1,441 @@
  6323. +// SPDX-License-Identifier: GPL-2.0
  6324. +/*
  6325. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  6326. + *
  6327. + * This program, aufs is free software; you can redistribute it and/or modify
  6328. + * it under the terms of the GNU General Public License as published by
  6329. + * the Free Software Foundation; either version 2 of the License, or
  6330. + * (at your option) any later version.
  6331. + *
  6332. + * This program is distributed in the hope that it will be useful,
  6333. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6334. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6335. + * GNU General Public License for more details.
  6336. + *
  6337. + * You should have received a copy of the GNU General Public License
  6338. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  6339. + */
  6340. +
  6341. +/*
  6342. + * debug print functions
  6343. + */
  6344. +
  6345. +#include <linux/iversion.h>
  6346. +#include "aufs.h"
  6347. +
  6348. +/* Returns 0, or -errno. arg is in kp->arg. */
  6349. +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
  6350. +{
  6351. + int err, n;
  6352. +
  6353. + err = kstrtoint(val, 0, &n);
  6354. + if (!err) {
  6355. + if (n > 0)
  6356. + au_debug_on();
  6357. + else
  6358. + au_debug_off();
  6359. + }
  6360. + return err;
  6361. +}
  6362. +
  6363. +/* Returns length written or -errno. Buffer is 4k (ie. be short!) */
  6364. +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
  6365. +{
  6366. + atomic_t *a;
  6367. +
  6368. + a = kp->arg;
  6369. + return sprintf(buffer, "%d", atomic_read(a));
  6370. +}
  6371. +
  6372. +static struct kernel_param_ops param_ops_atomic_t = {
  6373. + .set = param_atomic_t_set,
  6374. + .get = param_atomic_t_get
  6375. + /* void (*free)(void *arg) */
  6376. +};
  6377. +
  6378. +atomic_t aufs_debug = ATOMIC_INIT(0);
  6379. +MODULE_PARM_DESC(debug, "debug print");
  6380. +module_param_named(debug, aufs_debug, atomic_t, 0664);
  6381. +
  6382. +DEFINE_MUTEX(au_dbg_mtx); /* just to serialize the dbg msgs */
  6383. +char *au_plevel = KERN_DEBUG;
  6384. +#define dpri(fmt, ...) do { \
  6385. + if ((au_plevel \
  6386. + && strcmp(au_plevel, KERN_DEBUG)) \
  6387. + || au_debug_test()) \
  6388. + printk("%s" fmt, au_plevel, ##__VA_ARGS__); \
  6389. +} while (0)
  6390. +
  6391. +/* ---------------------------------------------------------------------- */
  6392. +
  6393. +void au_dpri_whlist(struct au_nhash *whlist)
  6394. +{
  6395. + unsigned long ul, n;
  6396. + struct hlist_head *head;
  6397. + struct au_vdir_wh *pos;
  6398. +
  6399. + n = whlist->nh_num;
  6400. + head = whlist->nh_head;
  6401. + for (ul = 0; ul < n; ul++) {
  6402. + hlist_for_each_entry(pos, head, wh_hash)
  6403. + dpri("b%d, %.*s, %d\n",
  6404. + pos->wh_bindex,
  6405. + pos->wh_str.len, pos->wh_str.name,
  6406. + pos->wh_str.len);
  6407. + head++;
  6408. + }
  6409. +}
  6410. +
  6411. +void au_dpri_vdir(struct au_vdir *vdir)
  6412. +{
  6413. + unsigned long ul;
  6414. + union au_vdir_deblk_p p;
  6415. + unsigned char *o;
  6416. +
  6417. + if (!vdir || IS_ERR(vdir)) {
  6418. + dpri("err %ld\n", PTR_ERR(vdir));
  6419. + return;
  6420. + }
  6421. +
  6422. + dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
  6423. + vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
  6424. + vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
  6425. + for (ul = 0; ul < vdir->vd_nblk; ul++) {
  6426. + p.deblk = vdir->vd_deblk[ul];
  6427. + o = p.deblk;
  6428. + dpri("[%lu]: %p\n", ul, o);
  6429. + }
  6430. +}
  6431. +
  6432. +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
  6433. + struct dentry *wh)
  6434. +{
  6435. + char *n = NULL;
  6436. + int l = 0;
  6437. +
  6438. + if (!inode || IS_ERR(inode)) {
  6439. + dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
  6440. + return -1;
  6441. + }
  6442. +
  6443. + /* the type of i_blocks depends upon CONFIG_LBDAF */
  6444. + BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
  6445. + && sizeof(inode->i_blocks) != sizeof(u64));
  6446. + if (wh) {
  6447. + n = (void *)wh->d_name.name;
  6448. + l = wh->d_name.len;
  6449. + }
  6450. +
  6451. + dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
  6452. + " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
  6453. + bindex, inode,
  6454. + inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
  6455. + atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
  6456. + i_size_read(inode), (unsigned long long)inode->i_blocks,
  6457. + hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
  6458. + inode->i_mapping ? inode->i_mapping->nrpages : 0,
  6459. + inode->i_state, inode->i_flags, inode_peek_iversion(inode),
  6460. + inode->i_generation,
  6461. + l ? ", wh " : "", l, n);
  6462. + return 0;
  6463. +}
  6464. +
  6465. +void au_dpri_inode(struct inode *inode)
  6466. +{
  6467. + struct au_iinfo *iinfo;
  6468. + struct au_hinode *hi;
  6469. + aufs_bindex_t bindex;
  6470. + int err, hn;
  6471. +
  6472. + err = do_pri_inode(-1, inode, -1, NULL);
  6473. + if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
  6474. + return;
  6475. +
  6476. + iinfo = au_ii(inode);
  6477. + dpri("i-1: btop %d, bbot %d, gen %d\n",
  6478. + iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
  6479. + if (iinfo->ii_btop < 0)
  6480. + return;
  6481. + hn = 0;
  6482. + for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
  6483. + hi = au_hinode(iinfo, bindex);
  6484. + hn = !!au_hn(hi);
  6485. + do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
  6486. + }
  6487. +}
  6488. +
  6489. +void au_dpri_dalias(struct inode *inode)
  6490. +{
  6491. + struct dentry *d;
  6492. +
  6493. + spin_lock(&inode->i_lock);
  6494. + hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
  6495. + au_dpri_dentry(d);
  6496. + spin_unlock(&inode->i_lock);
  6497. +}
  6498. +
  6499. +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
  6500. +{
  6501. + struct dentry *wh = NULL;
  6502. + int hn;
  6503. + struct inode *inode;
  6504. + struct au_iinfo *iinfo;
  6505. + struct au_hinode *hi;
  6506. +
  6507. + if (!dentry || IS_ERR(dentry)) {
  6508. + dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
  6509. + return -1;
  6510. + }
  6511. + /* do not call dget_parent() here */
  6512. + /* note: access d_xxx without d_lock */
  6513. + dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
  6514. + bindex, dentry, dentry,
  6515. + dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
  6516. + au_dcount(dentry), dentry->d_flags,
  6517. + d_unhashed(dentry) ? "un" : "");
  6518. + hn = -1;
  6519. + inode = NULL;
  6520. + if (d_is_positive(dentry))
  6521. + inode = d_inode(dentry);
  6522. + if (inode
  6523. + && au_test_aufs(dentry->d_sb)
  6524. + && bindex >= 0
  6525. + && !au_is_bad_inode(inode)) {
  6526. + iinfo = au_ii(inode);
  6527. + hi = au_hinode(iinfo, bindex);
  6528. + hn = !!au_hn(hi);
  6529. + wh = hi->hi_whdentry;
  6530. + }
  6531. + do_pri_inode(bindex, inode, hn, wh);
  6532. + return 0;
  6533. +}
  6534. +
  6535. +void au_dpri_dentry(struct dentry *dentry)
  6536. +{
  6537. + struct au_dinfo *dinfo;
  6538. + aufs_bindex_t bindex;
  6539. + int err;
  6540. +
  6541. + err = do_pri_dentry(-1, dentry);
  6542. + if (err || !au_test_aufs(dentry->d_sb))
  6543. + return;
  6544. +
  6545. + dinfo = au_di(dentry);
  6546. + if (!dinfo)
  6547. + return;
  6548. + dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
  6549. + dinfo->di_btop, dinfo->di_bbot,
  6550. + dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
  6551. + dinfo->di_tmpfile);
  6552. + if (dinfo->di_btop < 0)
  6553. + return;
  6554. + for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
  6555. + do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
  6556. +}
  6557. +
  6558. +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
  6559. +{
  6560. + char a[32];
  6561. +
  6562. + if (!file || IS_ERR(file)) {
  6563. + dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
  6564. + return -1;
  6565. + }
  6566. + a[0] = 0;
  6567. + if (bindex < 0
  6568. + && !IS_ERR_OR_NULL(file->f_path.dentry)
  6569. + && au_test_aufs(file->f_path.dentry->d_sb)
  6570. + && au_fi(file))
  6571. + snprintf(a, sizeof(a), ", gen %d, mmapped %d",
  6572. + au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
  6573. + dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
  6574. + bindex, file->f_mode, file->f_flags, (long)file_count(file),
  6575. + file->f_version, file->f_pos, a);
  6576. + if (!IS_ERR_OR_NULL(file->f_path.dentry))
  6577. + do_pri_dentry(bindex, file->f_path.dentry);
  6578. + return 0;
  6579. +}
  6580. +
  6581. +void au_dpri_file(struct file *file)
  6582. +{
  6583. + struct au_finfo *finfo;
  6584. + struct au_fidir *fidir;
  6585. + struct au_hfile *hfile;
  6586. + aufs_bindex_t bindex;
  6587. + int err;
  6588. +
  6589. + err = do_pri_file(-1, file);
  6590. + if (err
  6591. + || IS_ERR_OR_NULL(file->f_path.dentry)
  6592. + || !au_test_aufs(file->f_path.dentry->d_sb))
  6593. + return;
  6594. +
  6595. + finfo = au_fi(file);
  6596. + if (!finfo)
  6597. + return;
  6598. + if (finfo->fi_btop < 0)
  6599. + return;
  6600. + fidir = finfo->fi_hdir;
  6601. + if (!fidir)
  6602. + do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
  6603. + else
  6604. + for (bindex = finfo->fi_btop;
  6605. + bindex >= 0 && bindex <= fidir->fd_bbot;
  6606. + bindex++) {
  6607. + hfile = fidir->fd_hfile + bindex;
  6608. + do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
  6609. + }
  6610. +}
  6611. +
  6612. +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
  6613. +{
  6614. + struct vfsmount *mnt;
  6615. + struct super_block *sb;
  6616. +
  6617. + if (!br || IS_ERR(br))
  6618. + goto out;
  6619. + mnt = au_br_mnt(br);
  6620. + if (!mnt || IS_ERR(mnt))
  6621. + goto out;
  6622. + sb = mnt->mnt_sb;
  6623. + if (!sb || IS_ERR(sb))
  6624. + goto out;
  6625. +
  6626. + dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
  6627. + "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
  6628. + "xino %d\n",
  6629. + bindex, br->br_perm, br->br_id, br->br_wbr,
  6630. + au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
  6631. + sb->s_flags, sb->s_count,
  6632. + atomic_read(&sb->s_active),
  6633. + !!au_xino_file(br->br_xino, /*idx*/-1));
  6634. + return 0;
  6635. +
  6636. +out:
  6637. + dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
  6638. + return -1;
  6639. +}
  6640. +
  6641. +void au_dpri_sb(struct super_block *sb)
  6642. +{
  6643. + struct au_sbinfo *sbinfo;
  6644. + aufs_bindex_t bindex;
  6645. + int err;
  6646. + /* to reduce stack size */
  6647. + struct {
  6648. + struct vfsmount mnt;
  6649. + struct au_branch fake;
  6650. + } *a;
  6651. +
  6652. + /* this function can be called from magic sysrq */
  6653. + a = kzalloc(sizeof(*a), GFP_ATOMIC);
  6654. + if (unlikely(!a)) {
  6655. + dpri("no memory\n");
  6656. + return;
  6657. + }
  6658. +
  6659. + a->mnt.mnt_sb = sb;
  6660. + a->fake.br_path.mnt = &a->mnt;
  6661. + err = do_pri_br(-1, &a->fake);
  6662. + au_kfree_rcu(a);
  6663. + dpri("dev 0x%x\n", sb->s_dev);
  6664. + if (err || !au_test_aufs(sb))
  6665. + return;
  6666. +
  6667. + sbinfo = au_sbi(sb);
  6668. + if (!sbinfo)
  6669. + return;
  6670. + dpri("nw %d, gen %u, kobj %d\n",
  6671. + atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
  6672. + kref_read(&sbinfo->si_kobj.kref));
  6673. + for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
  6674. + do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
  6675. +}
  6676. +
  6677. +/* ---------------------------------------------------------------------- */
  6678. +
  6679. +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
  6680. +{
  6681. + struct inode *h_inode, *inode = d_inode(dentry);
  6682. + struct dentry *h_dentry;
  6683. + aufs_bindex_t bindex, bbot, bi;
  6684. +
  6685. + if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
  6686. + return;
  6687. +
  6688. + bbot = au_dbbot(dentry);
  6689. + bi = au_ibbot(inode);
  6690. + if (bi < bbot)
  6691. + bbot = bi;
  6692. + bindex = au_dbtop(dentry);
  6693. + bi = au_ibtop(inode);
  6694. + if (bi > bindex)
  6695. + bindex = bi;
  6696. +
  6697. + for (; bindex <= bbot; bindex++) {
  6698. + h_dentry = au_h_dptr(dentry, bindex);
  6699. + if (!h_dentry)
  6700. + continue;
  6701. + h_inode = au_h_iptr(inode, bindex);
  6702. + if (unlikely(h_inode != d_inode(h_dentry))) {
  6703. + au_debug_on();
  6704. + AuDbg("b%d, %s:%d\n", bindex, func, line);
  6705. + AuDbgDentry(dentry);
  6706. + AuDbgInode(inode);
  6707. + au_debug_off();
  6708. + BUG();
  6709. + }
  6710. + }
  6711. +}
  6712. +
  6713. +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
  6714. +{
  6715. + int err, i, j;
  6716. + struct au_dcsub_pages dpages;
  6717. + struct au_dpage *dpage;
  6718. + struct dentry **dentries;
  6719. +
  6720. + err = au_dpages_init(&dpages, GFP_NOFS);
  6721. + AuDebugOn(err);
  6722. + err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
  6723. + AuDebugOn(err);
  6724. + for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
  6725. + dpage = dpages.dpages + i;
  6726. + dentries = dpage->dentries;
  6727. + for (j = dpage->ndentry - 1; !err && j >= 0; j--)
  6728. + AuDebugOn(au_digen_test(dentries[j], sigen));
  6729. + }
  6730. + au_dpages_free(&dpages);
  6731. +}
  6732. +
  6733. +void au_dbg_verify_kthread(void)
  6734. +{
  6735. + if (au_wkq_test()) {
  6736. + au_dbg_blocked();
  6737. + /*
  6738. + * It may be recursive, but udba=notify between two aufs mounts,
  6739. + * where a single ro branch is shared, is not a problem.
  6740. + */
  6741. + /* WARN_ON(1); */
  6742. + }
  6743. +}
  6744. +
  6745. +/* ---------------------------------------------------------------------- */
  6746. +
  6747. +int __init au_debug_init(void)
  6748. +{
  6749. + aufs_bindex_t bindex;
  6750. + struct au_vdir_destr destr;
  6751. +
  6752. + bindex = -1;
  6753. + AuDebugOn(bindex >= 0);
  6754. +
  6755. + destr.len = -1;
  6756. + AuDebugOn(destr.len < NAME_MAX);
  6757. +
  6758. +#ifdef CONFIG_4KSTACKS
  6759. + pr_warn("CONFIG_4KSTACKS is defined.\n");
  6760. +#endif
  6761. +
  6762. + return 0;
  6763. +}
  6764. diff -Naur null/fs/aufs/debug.h linux-5.1/fs/aufs/debug.h
  6765. --- /dev/null
  6766. +++ linux-5.1/fs/aufs/debug.h 2019-05-22 11:23:07.438141134 +0200
  6767. @@ -0,0 +1,226 @@
  6768. +/* SPDX-License-Identifier: GPL-2.0 */
  6769. +/*
  6770. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  6771. + *
  6772. + * This program, aufs is free software; you can redistribute it and/or modify
  6773. + * it under the terms of the GNU General Public License as published by
  6774. + * the Free Software Foundation; either version 2 of the License, or
  6775. + * (at your option) any later version.
  6776. + *
  6777. + * This program is distributed in the hope that it will be useful,
  6778. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6779. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6780. + * GNU General Public License for more details.
  6781. + *
  6782. + * You should have received a copy of the GNU General Public License
  6783. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  6784. + */
  6785. +
  6786. +/*
  6787. + * debug print functions
  6788. + */
  6789. +
  6790. +#ifndef __AUFS_DEBUG_H__
  6791. +#define __AUFS_DEBUG_H__
  6792. +
  6793. +#ifdef __KERNEL__
  6794. +
  6795. +#include <linux/atomic.h>
  6796. +#include <linux/module.h>
  6797. +#include <linux/kallsyms.h>
  6798. +#include <linux/sysrq.h>
  6799. +
  6800. +#ifdef CONFIG_AUFS_DEBUG
  6801. +#define AuDebugOn(a) BUG_ON(a)
  6802. +
  6803. +/* module parameter */
  6804. +extern atomic_t aufs_debug;
  6805. +static inline void au_debug_on(void)
  6806. +{
  6807. + atomic_inc(&aufs_debug);
  6808. +}
  6809. +static inline void au_debug_off(void)
  6810. +{
  6811. + atomic_dec_if_positive(&aufs_debug);
  6812. +}
  6813. +
  6814. +static inline int au_debug_test(void)
  6815. +{
  6816. + return atomic_read(&aufs_debug) > 0;
  6817. +}
  6818. +#else
  6819. +#define AuDebugOn(a) do {} while (0)
  6820. +AuStubVoid(au_debug_on, void)
  6821. +AuStubVoid(au_debug_off, void)
  6822. +AuStubInt0(au_debug_test, void)
  6823. +#endif /* CONFIG_AUFS_DEBUG */
  6824. +
  6825. +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
  6826. +
  6827. +/* ---------------------------------------------------------------------- */
  6828. +
  6829. +/* debug print */
  6830. +
  6831. +#define AuDbg(fmt, ...) do { \
  6832. + if (au_debug_test()) \
  6833. + pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
  6834. +} while (0)
  6835. +#define AuLabel(l) AuDbg(#l "\n")
  6836. +#define AuIOErr(fmt, ...) pr_err("I/O Error, " fmt, ##__VA_ARGS__)
  6837. +#define AuWarn1(fmt, ...) do { \
  6838. + static unsigned char _c; \
  6839. + if (!_c++) \
  6840. + pr_warn(fmt, ##__VA_ARGS__); \
  6841. +} while (0)
  6842. +
  6843. +#define AuErr1(fmt, ...) do { \
  6844. + static unsigned char _c; \
  6845. + if (!_c++) \
  6846. + pr_err(fmt, ##__VA_ARGS__); \
  6847. +} while (0)
  6848. +
  6849. +#define AuIOErr1(fmt, ...) do { \
  6850. + static unsigned char _c; \
  6851. + if (!_c++) \
  6852. + AuIOErr(fmt, ##__VA_ARGS__); \
  6853. +} while (0)
  6854. +
  6855. +#define AuUnsupportMsg "This operation is not supported." \
  6856. + " Please report this application to aufs-users ML."
  6857. +#define AuUnsupport(fmt, ...) do { \
  6858. + pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
  6859. + dump_stack(); \
  6860. +} while (0)
  6861. +
  6862. +#define AuTraceErr(e) do { \
  6863. + if (unlikely((e) < 0)) \
  6864. + AuDbg("err %d\n", (int)(e)); \
  6865. +} while (0)
  6866. +
  6867. +#define AuTraceErrPtr(p) do { \
  6868. + if (IS_ERR(p)) \
  6869. + AuDbg("err %ld\n", PTR_ERR(p)); \
  6870. +} while (0)
  6871. +
  6872. +/* dirty macros for debug print, use with "%.*s" and caution */
  6873. +#define AuLNPair(qstr) (qstr)->len, (qstr)->name
  6874. +
  6875. +/* ---------------------------------------------------------------------- */
  6876. +
  6877. +struct dentry;
  6878. +#ifdef CONFIG_AUFS_DEBUG
  6879. +extern struct mutex au_dbg_mtx;
  6880. +extern char *au_plevel;
  6881. +struct au_nhash;
  6882. +void au_dpri_whlist(struct au_nhash *whlist);
  6883. +struct au_vdir;
  6884. +void au_dpri_vdir(struct au_vdir *vdir);
  6885. +struct inode;
  6886. +void au_dpri_inode(struct inode *inode);
  6887. +void au_dpri_dalias(struct inode *inode);
  6888. +void au_dpri_dentry(struct dentry *dentry);
  6889. +struct file;
  6890. +void au_dpri_file(struct file *filp);
  6891. +struct super_block;
  6892. +void au_dpri_sb(struct super_block *sb);
  6893. +
  6894. +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
  6895. +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
  6896. +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
  6897. +void au_dbg_verify_kthread(void);
  6898. +
  6899. +int __init au_debug_init(void);
  6900. +
  6901. +#define AuDbgWhlist(w) do { \
  6902. + mutex_lock(&au_dbg_mtx); \
  6903. + AuDbg(#w "\n"); \
  6904. + au_dpri_whlist(w); \
  6905. + mutex_unlock(&au_dbg_mtx); \
  6906. +} while (0)
  6907. +
  6908. +#define AuDbgVdir(v) do { \
  6909. + mutex_lock(&au_dbg_mtx); \
  6910. + AuDbg(#v "\n"); \
  6911. + au_dpri_vdir(v); \
  6912. + mutex_unlock(&au_dbg_mtx); \
  6913. +} while (0)
  6914. +
  6915. +#define AuDbgInode(i) do { \
  6916. + mutex_lock(&au_dbg_mtx); \
  6917. + AuDbg(#i "\n"); \
  6918. + au_dpri_inode(i); \
  6919. + mutex_unlock(&au_dbg_mtx); \
  6920. +} while (0)
  6921. +
  6922. +#define AuDbgDAlias(i) do { \
  6923. + mutex_lock(&au_dbg_mtx); \
  6924. + AuDbg(#i "\n"); \
  6925. + au_dpri_dalias(i); \
  6926. + mutex_unlock(&au_dbg_mtx); \
  6927. +} while (0)
  6928. +
  6929. +#define AuDbgDentry(d) do { \
  6930. + mutex_lock(&au_dbg_mtx); \
  6931. + AuDbg(#d "\n"); \
  6932. + au_dpri_dentry(d); \
  6933. + mutex_unlock(&au_dbg_mtx); \
  6934. +} while (0)
  6935. +
  6936. +#define AuDbgFile(f) do { \
  6937. + mutex_lock(&au_dbg_mtx); \
  6938. + AuDbg(#f "\n"); \
  6939. + au_dpri_file(f); \
  6940. + mutex_unlock(&au_dbg_mtx); \
  6941. +} while (0)
  6942. +
  6943. +#define AuDbgSb(sb) do { \
  6944. + mutex_lock(&au_dbg_mtx); \
  6945. + AuDbg(#sb "\n"); \
  6946. + au_dpri_sb(sb); \
  6947. + mutex_unlock(&au_dbg_mtx); \
  6948. +} while (0)
  6949. +
  6950. +#define AuDbgSym(addr) do { \
  6951. + char sym[KSYM_SYMBOL_LEN]; \
  6952. + sprint_symbol(sym, (unsigned long)addr); \
  6953. + AuDbg("%s\n", sym); \
  6954. +} while (0)
  6955. +#else
  6956. +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
  6957. +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
  6958. +AuStubVoid(au_dbg_verify_kthread, void)
  6959. +AuStubInt0(__init au_debug_init, void)
  6960. +
  6961. +#define AuDbgWhlist(w) do {} while (0)
  6962. +#define AuDbgVdir(v) do {} while (0)
  6963. +#define AuDbgInode(i) do {} while (0)
  6964. +#define AuDbgDAlias(i) do {} while (0)
  6965. +#define AuDbgDentry(d) do {} while (0)
  6966. +#define AuDbgFile(f) do {} while (0)
  6967. +#define AuDbgSb(sb) do {} while (0)
  6968. +#define AuDbgSym(addr) do {} while (0)
  6969. +#endif /* CONFIG_AUFS_DEBUG */
  6970. +
  6971. +/* ---------------------------------------------------------------------- */
  6972. +
  6973. +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
  6974. +int __init au_sysrq_init(void);
  6975. +void au_sysrq_fin(void);
  6976. +
  6977. +#ifdef CONFIG_HW_CONSOLE
  6978. +#define au_dbg_blocked() do { \
  6979. + WARN_ON(1); \
  6980. + handle_sysrq('w'); \
  6981. +} while (0)
  6982. +#else
  6983. +AuStubVoid(au_dbg_blocked, void)
  6984. +#endif
  6985. +
  6986. +#else
  6987. +AuStubInt0(__init au_sysrq_init, void)
  6988. +AuStubVoid(au_sysrq_fin, void)
  6989. +AuStubVoid(au_dbg_blocked, void)
  6990. +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
  6991. +
  6992. +#endif /* __KERNEL__ */
  6993. +#endif /* __AUFS_DEBUG_H__ */
  6994. diff -Naur null/fs/aufs/dentry.c linux-5.1/fs/aufs/dentry.c
  6995. --- /dev/null
  6996. +++ linux-5.1/fs/aufs/dentry.c 2019-05-22 11:23:07.439141088 +0200
  6997. @@ -0,0 +1,1154 @@
  6998. +// SPDX-License-Identifier: GPL-2.0
  6999. +/*
  7000. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  7001. + *
  7002. + * This program, aufs is free software; you can redistribute it and/or modify
  7003. + * it under the terms of the GNU General Public License as published by
  7004. + * the Free Software Foundation; either version 2 of the License, or
  7005. + * (at your option) any later version.
  7006. + *
  7007. + * This program is distributed in the hope that it will be useful,
  7008. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  7009. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  7010. + * GNU General Public License for more details.
  7011. + *
  7012. + * You should have received a copy of the GNU General Public License
  7013. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  7014. + */
  7015. +
  7016. +/*
  7017. + * lookup and dentry operations
  7018. + */
  7019. +
  7020. +#include <linux/iversion.h>
  7021. +#include <linux/namei.h>
  7022. +#include "aufs.h"
  7023. +
  7024. +/*
  7025. + * returns positive/negative dentry, NULL or an error.
  7026. + * NULL means whiteout-ed or not-found.
  7027. + */
  7028. +static struct dentry*
  7029. +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
  7030. + aufs_bindex_t bindex, struct au_do_lookup_args *args)
  7031. +{
  7032. + struct dentry *h_dentry;
  7033. + struct inode *h_inode;
  7034. + struct au_branch *br;
  7035. + int wh_found, opq;
  7036. + unsigned char wh_able;
  7037. + const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
  7038. + const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
  7039. + IGNORE_PERM);
  7040. +
  7041. + wh_found = 0;
  7042. + br = au_sbr(dentry->d_sb, bindex);
  7043. + wh_able = !!au_br_whable(br->br_perm);
  7044. + if (wh_able)
  7045. + wh_found = au_wh_test(h_parent, &args->whname, ignore_perm);
  7046. + h_dentry = ERR_PTR(wh_found);
  7047. + if (!wh_found)
  7048. + goto real_lookup;
  7049. + if (unlikely(wh_found < 0))
  7050. + goto out;
  7051. +
  7052. + /* We found a whiteout */
  7053. + /* au_set_dbbot(dentry, bindex); */
  7054. + au_set_dbwh(dentry, bindex);
  7055. + if (!allow_neg)
  7056. + return NULL; /* success */
  7057. +
  7058. +real_lookup:
  7059. + if (!ignore_perm)
  7060. + h_dentry = vfsub_lkup_one(args->name, h_parent);
  7061. + else
  7062. + h_dentry = au_sio_lkup_one(args->name, h_parent);
  7063. + if (IS_ERR(h_dentry)) {
  7064. + if (PTR_ERR(h_dentry) == -ENAMETOOLONG
  7065. + && !allow_neg)
  7066. + h_dentry = NULL;
  7067. + goto out;
  7068. + }
  7069. +
  7070. + h_inode = d_inode(h_dentry);
  7071. + if (d_is_negative(h_dentry)) {
  7072. + if (!allow_neg)
  7073. + goto out_neg;
  7074. + } else if (wh_found
  7075. + || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
  7076. + goto out_neg;
  7077. + else if (au_ftest_lkup(args->flags, DIRREN)
  7078. + /* && h_inode */
  7079. + && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
  7080. + AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
  7081. + (unsigned long long)h_inode->i_ino);
  7082. + goto out_neg;
  7083. + }
  7084. +
  7085. + if (au_dbbot(dentry) <= bindex)
  7086. + au_set_dbbot(dentry, bindex);
  7087. + if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
  7088. + au_set_dbtop(dentry, bindex);
  7089. + au_set_h_dptr(dentry, bindex, h_dentry);
  7090. +
  7091. + if (!d_is_dir(h_dentry)
  7092. + || !wh_able
  7093. + || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
  7094. + goto out; /* success */
  7095. +
  7096. + inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
  7097. + opq = au_diropq_test(h_dentry);
  7098. + inode_unlock_shared(h_inode);
  7099. + if (opq > 0)
  7100. + au_set_dbdiropq(dentry, bindex);
  7101. + else if (unlikely(opq < 0)) {
  7102. + au_set_h_dptr(dentry, bindex, NULL);
  7103. + h_dentry = ERR_PTR(opq);
  7104. + }
  7105. + goto out;
  7106. +
  7107. +out_neg:
  7108. + dput(h_dentry);
  7109. + h_dentry = NULL;
  7110. +out:
  7111. + return h_dentry;
  7112. +}
  7113. +
  7114. +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
  7115. +{
  7116. + if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
  7117. + && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
  7118. + return -EPERM;
  7119. + return 0;
  7120. +}
  7121. +
  7122. +/*
  7123. + * returns the number of lower positive dentries,
  7124. + * otherwise an error.
  7125. + * can be called at unlinking with @type is zero.
  7126. + */
  7127. +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
  7128. + unsigned int flags)
  7129. +{
  7130. + int npositive, err;
  7131. + aufs_bindex_t bindex, btail, bdiropq;
  7132. + unsigned char isdir, dirperm1, dirren;
  7133. + struct au_do_lookup_args args = {
  7134. + .flags = flags,
  7135. + .name = &dentry->d_name
  7136. + };
  7137. + struct dentry *parent;
  7138. + struct super_block *sb;
  7139. +
  7140. + sb = dentry->d_sb;
  7141. + err = au_test_shwh(sb, args.name);
  7142. + if (unlikely(err))
  7143. + goto out;
  7144. +
  7145. + err = au_wh_name_alloc(&args.whname, args.name);
  7146. + if (unlikely(err))
  7147. + goto out;
  7148. +
  7149. + isdir = !!d_is_dir(dentry);
  7150. + dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
  7151. + dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
  7152. + if (dirren)
  7153. + au_fset_lkup(args.flags, DIRREN);
  7154. +
  7155. + npositive = 0;
  7156. + parent = dget_parent(dentry);
  7157. + btail = au_dbtaildir(parent);
  7158. + for (bindex = btop; bindex <= btail; bindex++) {
  7159. + struct dentry *h_parent, *h_dentry;
  7160. + struct inode *h_inode, *h_dir;
  7161. + struct au_branch *br;
  7162. +
  7163. + h_dentry = au_h_dptr(dentry, bindex);
  7164. + if (h_dentry) {
  7165. + if (d_is_positive(h_dentry))
  7166. + npositive++;
  7167. + break;
  7168. + }
  7169. + h_parent = au_h_dptr(parent, bindex);
  7170. + if (!h_parent || !d_is_dir(h_parent))
  7171. + continue;
  7172. +
  7173. + if (dirren) {
  7174. + /* if the inum matches, then use the prepared name */
  7175. + err = au_dr_lkup_name(&args, bindex);
  7176. + if (unlikely(err))
  7177. + goto out_parent;
  7178. + }
  7179. +
  7180. + h_dir = d_inode(h_parent);
  7181. + inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
  7182. + h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
  7183. + inode_unlock_shared(h_dir);
  7184. + err = PTR_ERR(h_dentry);
  7185. + if (IS_ERR(h_dentry))
  7186. + goto out_parent;
  7187. + if (h_dentry)
  7188. + au_fclr_lkup(args.flags, ALLOW_NEG);
  7189. + if (dirperm1)
  7190. + au_fset_lkup(args.flags, IGNORE_PERM);
  7191. +
  7192. + if (au_dbwh(dentry) == bindex)
  7193. + break;
  7194. + if (!h_dentry)
  7195. + continue;
  7196. + if (d_is_negative(h_dentry))
  7197. + continue;
  7198. + h_inode = d_inode(h_dentry);
  7199. + npositive++;
  7200. + if (!args.type)
  7201. + args.type = h_inode->i_mode & S_IFMT;
  7202. + if (args.type != S_IFDIR)
  7203. + break;
  7204. + else if (isdir) {
  7205. + /* the type of lower may be different */
  7206. + bdiropq = au_dbdiropq(dentry);
  7207. + if (bdiropq >= 0 && bdiropq <= bindex)
  7208. + break;
  7209. + }
  7210. + br = au_sbr(sb, bindex);
  7211. + if (dirren
  7212. + && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
  7213. + /*add_ent*/NULL)) {
  7214. + /* prepare next name to lookup */
  7215. + err = au_dr_lkup(&args, dentry, bindex);
  7216. + if (unlikely(err))
  7217. + goto out_parent;
  7218. + }
  7219. + }
  7220. +
  7221. + if (npositive) {
  7222. + AuLabel(positive);
  7223. + au_update_dbtop(dentry);
  7224. + }
  7225. + err = npositive;
  7226. + if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
  7227. + && au_dbtop(dentry) < 0)) {
  7228. + err = -EIO;
  7229. + AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
  7230. + dentry, err);
  7231. + }
  7232. +
  7233. +out_parent:
  7234. + dput(parent);
  7235. + au_kfree_try_rcu(args.whname.name);
  7236. + if (dirren)
  7237. + au_dr_lkup_fin(&args);
  7238. +out:
  7239. + return err;
  7240. +}
  7241. +
  7242. +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent)
  7243. +{
  7244. + struct dentry *dentry;
  7245. + int wkq_err;
  7246. +
  7247. + if (!au_test_h_perm_sio(d_inode(parent), MAY_EXEC))
  7248. + dentry = vfsub_lkup_one(name, parent);
  7249. + else {
  7250. + struct vfsub_lkup_one_args args = {
  7251. + .errp = &dentry,
  7252. + .name = name,
  7253. + .parent = parent
  7254. + };
  7255. +
  7256. + wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
  7257. + if (unlikely(wkq_err))
  7258. + dentry = ERR_PTR(wkq_err);
  7259. + }
  7260. +
  7261. + return dentry;
  7262. +}
  7263. +
  7264. +/*
  7265. + * lookup @dentry on @bindex which should be negative.
  7266. + */
  7267. +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
  7268. +{
  7269. + int err;
  7270. + struct dentry *parent, *h_parent, *h_dentry;
  7271. + struct au_branch *br;
  7272. +
  7273. + parent = dget_parent(dentry);
  7274. + h_parent = au_h_dptr(parent, bindex);
  7275. + br = au_sbr(dentry->d_sb, bindex);
  7276. + if (wh)
  7277. + h_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
  7278. + else
  7279. + h_dentry = au_sio_lkup_one(&dentry->d_name, h_parent);
  7280. + err = PTR_ERR(h_dentry);
  7281. + if (IS_ERR(h_dentry))
  7282. + goto out;
  7283. + if (unlikely(d_is_positive(h_dentry))) {
  7284. + err = -EIO;
  7285. + AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
  7286. + dput(h_dentry);
  7287. + goto out;
  7288. + }
  7289. +
  7290. + err = 0;
  7291. + if (bindex < au_dbtop(dentry))
  7292. + au_set_dbtop(dentry, bindex);
  7293. + if (au_dbbot(dentry) < bindex)
  7294. + au_set_dbbot(dentry, bindex);
  7295. + au_set_h_dptr(dentry, bindex, h_dentry);
  7296. +
  7297. +out:
  7298. + dput(parent);
  7299. + return err;
  7300. +}
  7301. +
  7302. +/* ---------------------------------------------------------------------- */
  7303. +
  7304. +/* subset of struct inode */
  7305. +struct au_iattr {
  7306. + unsigned long i_ino;
  7307. + /* unsigned int i_nlink; */
  7308. + kuid_t i_uid;
  7309. + kgid_t i_gid;
  7310. + u64 i_version;
  7311. +/*
  7312. + loff_t i_size;
  7313. + blkcnt_t i_blocks;
  7314. +*/
  7315. + umode_t i_mode;
  7316. +};
  7317. +
  7318. +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
  7319. +{
  7320. + ia->i_ino = h_inode->i_ino;
  7321. + /* ia->i_nlink = h_inode->i_nlink; */
  7322. + ia->i_uid = h_inode->i_uid;
  7323. + ia->i_gid = h_inode->i_gid;
  7324. + ia->i_version = inode_query_iversion(h_inode);
  7325. +/*
  7326. + ia->i_size = h_inode->i_size;
  7327. + ia->i_blocks = h_inode->i_blocks;
  7328. +*/
  7329. + ia->i_mode = (h_inode->i_mode & S_IFMT);
  7330. +}
  7331. +
  7332. +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
  7333. +{
  7334. + return ia->i_ino != h_inode->i_ino
  7335. + /* || ia->i_nlink != h_inode->i_nlink */
  7336. + || !uid_eq(ia->i_uid, h_inode->i_uid)
  7337. + || !gid_eq(ia->i_gid, h_inode->i_gid)
  7338. + || !inode_eq_iversion(h_inode, ia->i_version)
  7339. +/*
  7340. + || ia->i_size != h_inode->i_size
  7341. + || ia->i_blocks != h_inode->i_blocks
  7342. +*/
  7343. + || ia->i_mode != (h_inode->i_mode & S_IFMT);
  7344. +}
  7345. +
  7346. +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
  7347. + struct au_branch *br)
  7348. +{
  7349. + int err;
  7350. + struct au_iattr ia;
  7351. + struct inode *h_inode;
  7352. + struct dentry *h_d;
  7353. + struct super_block *h_sb;
  7354. +
  7355. + err = 0;
  7356. + memset(&ia, -1, sizeof(ia));
  7357. + h_sb = h_dentry->d_sb;
  7358. + h_inode = NULL;
  7359. + if (d_is_positive(h_dentry)) {
  7360. + h_inode = d_inode(h_dentry);
  7361. + au_iattr_save(&ia, h_inode);
  7362. + } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
  7363. + /* nfs d_revalidate may return 0 for negative dentry */
  7364. + /* fuse d_revalidate always return 0 for negative dentry */
  7365. + goto out;
  7366. +
  7367. + /* main purpose is namei.c:cached_lookup() and d_revalidate */
  7368. + h_d = vfsub_lkup_one(&h_dentry->d_name, h_parent);
  7369. + err = PTR_ERR(h_d);
  7370. + if (IS_ERR(h_d))
  7371. + goto out;
  7372. +
  7373. + err = 0;
  7374. + if (unlikely(h_d != h_dentry
  7375. + || d_inode(h_d) != h_inode
  7376. + || (h_inode && au_iattr_test(&ia, h_inode))))
  7377. + err = au_busy_or_stale();
  7378. + dput(h_d);
  7379. +
  7380. +out:
  7381. + AuTraceErr(err);
  7382. + return err;
  7383. +}
  7384. +
  7385. +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
  7386. + struct dentry *h_parent, struct au_branch *br)
  7387. +{
  7388. + int err;
  7389. +
  7390. + err = 0;
  7391. + if (udba == AuOpt_UDBA_REVAL
  7392. + && !au_test_fs_remote(h_dentry->d_sb)) {
  7393. + IMustLock(h_dir);
  7394. + err = (d_inode(h_dentry->d_parent) != h_dir);
  7395. + } else if (udba != AuOpt_UDBA_NONE)
  7396. + err = au_h_verify_dentry(h_dentry, h_parent, br);
  7397. +
  7398. + return err;
  7399. +}
  7400. +
  7401. +/* ---------------------------------------------------------------------- */
  7402. +
  7403. +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
  7404. +{
  7405. + int err;
  7406. + aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
  7407. + struct au_hdentry tmp, *p, *q;
  7408. + struct au_dinfo *dinfo;
  7409. + struct super_block *sb;
  7410. +
  7411. + DiMustWriteLock(dentry);
  7412. +
  7413. + sb = dentry->d_sb;
  7414. + dinfo = au_di(dentry);
  7415. + bbot = dinfo->di_bbot;
  7416. + bwh = dinfo->di_bwh;
  7417. + bdiropq = dinfo->di_bdiropq;
  7418. + bindex = dinfo->di_btop;
  7419. + p = au_hdentry(dinfo, bindex);
  7420. + for (; bindex <= bbot; bindex++, p++) {
  7421. + if (!p->hd_dentry)
  7422. + continue;
  7423. +
  7424. + new_bindex = au_br_index(sb, p->hd_id);
  7425. + if (new_bindex == bindex)
  7426. + continue;
  7427. +
  7428. + if (dinfo->di_bwh == bindex)
  7429. + bwh = new_bindex;
  7430. + if (dinfo->di_bdiropq == bindex)
  7431. + bdiropq = new_bindex;
  7432. + if (new_bindex < 0) {
  7433. + au_hdput(p);
  7434. + p->hd_dentry = NULL;
  7435. + continue;
  7436. + }
  7437. +
  7438. + /* swap two lower dentries, and loop again */
  7439. + q = au_hdentry(dinfo, new_bindex);
  7440. + tmp = *q;
  7441. + *q = *p;
  7442. + *p = tmp;
  7443. + if (tmp.hd_dentry) {
  7444. + bindex--;
  7445. + p--;
  7446. + }
  7447. + }
  7448. +
  7449. + dinfo->di_bwh = -1;
  7450. + if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
  7451. + dinfo->di_bwh = bwh;
  7452. +
  7453. + dinfo->di_bdiropq = -1;
  7454. + if (bdiropq >= 0
  7455. + && bdiropq <= au_sbbot(sb)
  7456. + && au_sbr_whable(sb, bdiropq))
  7457. + dinfo->di_bdiropq = bdiropq;
  7458. +
  7459. + err = -EIO;
  7460. + dinfo->di_btop = -1;
  7461. + dinfo->di_bbot = -1;
  7462. + bbot = au_dbbot(parent);
  7463. + bindex = 0;
  7464. + p = au_hdentry(dinfo, bindex);
  7465. + for (; bindex <= bbot; bindex++, p++)
  7466. + if (p->hd_dentry) {
  7467. + dinfo->di_btop = bindex;
  7468. + break;
  7469. + }
  7470. +
  7471. + if (dinfo->di_btop >= 0) {
  7472. + bindex = bbot;
  7473. + p = au_hdentry(dinfo, bindex);
  7474. + for (; bindex >= 0; bindex--, p--)
  7475. + if (p->hd_dentry) {
  7476. + dinfo->di_bbot = bindex;
  7477. + err = 0;
  7478. + break;
  7479. + }
  7480. + }
  7481. +
  7482. + return err;
  7483. +}
  7484. +
  7485. +static void au_do_hide(struct dentry *dentry)
  7486. +{
  7487. + struct inode *inode;
  7488. +
  7489. + if (d_really_is_positive(dentry)) {
  7490. + inode = d_inode(dentry);
  7491. + if (!d_is_dir(dentry)) {
  7492. + if (inode->i_nlink && !d_unhashed(dentry))
  7493. + drop_nlink(inode);
  7494. + } else {
  7495. + clear_nlink(inode);
  7496. + /* stop next lookup */
  7497. + inode->i_flags |= S_DEAD;
  7498. + }
  7499. + smp_mb(); /* necessary? */
  7500. + }
  7501. + d_drop(dentry);
  7502. +}
  7503. +
  7504. +static int au_hide_children(struct dentry *parent)
  7505. +{
  7506. + int err, i, j, ndentry;
  7507. + struct au_dcsub_pages dpages;
  7508. + struct au_dpage *dpage;
  7509. + struct dentry *dentry;
  7510. +
  7511. + err = au_dpages_init(&dpages, GFP_NOFS);
  7512. + if (unlikely(err))
  7513. + goto out;
  7514. + err = au_dcsub_pages(&dpages, parent, NULL, NULL);
  7515. + if (unlikely(err))
  7516. + goto out_dpages;
  7517. +
  7518. + /* in reverse order */
  7519. + for (i = dpages.ndpage - 1; i >= 0; i--) {
  7520. + dpage = dpages.dpages + i;
  7521. + ndentry = dpage->ndentry;
  7522. + for (j = ndentry - 1; j >= 0; j--) {
  7523. + dentry = dpage->dentries[j];
  7524. + if (dentry != parent)
  7525. + au_do_hide(dentry);
  7526. + }
  7527. + }
  7528. +
  7529. +out_dpages:
  7530. + au_dpages_free(&dpages);
  7531. +out:
  7532. + return err;
  7533. +}
  7534. +
  7535. +static void au_hide(struct dentry *dentry)
  7536. +{
  7537. + int err;
  7538. +
  7539. + AuDbgDentry(dentry);
  7540. + if (d_is_dir(dentry)) {
  7541. + /* shrink_dcache_parent(dentry); */
  7542. + err = au_hide_children(dentry);
  7543. + if (unlikely(err))
  7544. + AuIOErr("%pd, failed hiding children, ignored %d\n",
  7545. + dentry, err);
  7546. + }
  7547. + au_do_hide(dentry);
  7548. +}
  7549. +
  7550. +/*
  7551. + * By adding a dirty branch, a cached dentry may be affected in various ways.
  7552. + *
  7553. + * a dirty branch is added
  7554. + * - on the top of layers
  7555. + * - in the middle of layers
  7556. + * - to the bottom of layers
  7557. + *
  7558. + * on the added branch there exists
  7559. + * - a whiteout
  7560. + * - a diropq
  7561. + * - a same named entry
  7562. + * + exist
  7563. + * * negative --> positive
  7564. + * * positive --> positive
  7565. + * - type is unchanged
  7566. + * - type is changed
  7567. + * + doesn't exist
  7568. + * * negative --> negative
  7569. + * * positive --> negative (rejected by au_br_del() for non-dir case)
  7570. + * - none
  7571. + */
  7572. +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
  7573. + struct au_dinfo *tmp)
  7574. +{
  7575. + int err;
  7576. + aufs_bindex_t bindex, bbot;
  7577. + struct {
  7578. + struct dentry *dentry;
  7579. + struct inode *inode;
  7580. + mode_t mode;
  7581. + } orig_h, tmp_h = {
  7582. + .dentry = NULL
  7583. + };
  7584. + struct au_hdentry *hd;
  7585. + struct inode *inode, *h_inode;
  7586. + struct dentry *h_dentry;
  7587. +
  7588. + err = 0;
  7589. + AuDebugOn(dinfo->di_btop < 0);
  7590. + orig_h.mode = 0;
  7591. + orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
  7592. + orig_h.inode = NULL;
  7593. + if (d_is_positive(orig_h.dentry)) {
  7594. + orig_h.inode = d_inode(orig_h.dentry);
  7595. + orig_h.mode = orig_h.inode->i_mode & S_IFMT;
  7596. + }
  7597. + if (tmp->di_btop >= 0) {
  7598. + tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
  7599. + if (d_is_positive(tmp_h.dentry)) {
  7600. + tmp_h.inode = d_inode(tmp_h.dentry);
  7601. + tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
  7602. + }
  7603. + }
  7604. +
  7605. + inode = NULL;
  7606. + if (d_really_is_positive(dentry))
  7607. + inode = d_inode(dentry);
  7608. + if (!orig_h.inode) {
  7609. + AuDbg("negative originally\n");
  7610. + if (inode) {
  7611. + au_hide(dentry);
  7612. + goto out;
  7613. + }
  7614. + AuDebugOn(inode);
  7615. + AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
  7616. + AuDebugOn(dinfo->di_bdiropq != -1);
  7617. +
  7618. + if (!tmp_h.inode) {
  7619. + AuDbg("negative --> negative\n");
  7620. + /* should have only one negative lower */
  7621. + if (tmp->di_btop >= 0
  7622. + && tmp->di_btop < dinfo->di_btop) {
  7623. + AuDebugOn(tmp->di_btop != tmp->di_bbot);
  7624. + AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
  7625. + au_set_h_dptr(dentry, dinfo->di_btop, NULL);
  7626. + au_di_cp(dinfo, tmp);
  7627. + hd = au_hdentry(tmp, tmp->di_btop);
  7628. + au_set_h_dptr(dentry, tmp->di_btop,
  7629. + dget(hd->hd_dentry));
  7630. + }
  7631. + au_dbg_verify_dinode(dentry);
  7632. + } else {
  7633. + AuDbg("negative --> positive\n");
  7634. + /*
  7635. + * similar to the behaviour of creating with bypassing
  7636. + * aufs.
  7637. + * unhash it in order to force an error in the
  7638. + * succeeding create operation.
  7639. + * we should not set S_DEAD here.
  7640. + */
  7641. + d_drop(dentry);
  7642. + /* au_di_swap(tmp, dinfo); */
  7643. + au_dbg_verify_dinode(dentry);
  7644. + }
  7645. + } else {
  7646. + AuDbg("positive originally\n");
  7647. + /* inode may be NULL */
  7648. + AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
  7649. + if (!tmp_h.inode) {
  7650. + AuDbg("positive --> negative\n");
  7651. + /* or bypassing aufs */
  7652. + au_hide(dentry);
  7653. + if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
  7654. + dinfo->di_bwh = tmp->di_bwh;
  7655. + if (inode)
  7656. + err = au_refresh_hinode_self(inode);
  7657. + au_dbg_verify_dinode(dentry);
  7658. + } else if (orig_h.mode == tmp_h.mode) {
  7659. + AuDbg("positive --> positive, same type\n");
  7660. + if (!S_ISDIR(orig_h.mode)
  7661. + && dinfo->di_btop > tmp->di_btop) {
  7662. + /*
  7663. + * similar to the behaviour of removing and
  7664. + * creating.
  7665. + */
  7666. + au_hide(dentry);
  7667. + if (inode)
  7668. + err = au_refresh_hinode_self(inode);
  7669. + au_dbg_verify_dinode(dentry);
  7670. + } else {
  7671. + /* fill empty slots */
  7672. + if (dinfo->di_btop > tmp->di_btop)
  7673. + dinfo->di_btop = tmp->di_btop;
  7674. + if (dinfo->di_bbot < tmp->di_bbot)
  7675. + dinfo->di_bbot = tmp->di_bbot;
  7676. + dinfo->di_bwh = tmp->di_bwh;
  7677. + dinfo->di_bdiropq = tmp->di_bdiropq;
  7678. + bbot = dinfo->di_bbot;
  7679. + bindex = tmp->di_btop;
  7680. + hd = au_hdentry(tmp, bindex);
  7681. + for (; bindex <= bbot; bindex++, hd++) {
  7682. + if (au_h_dptr(dentry, bindex))
  7683. + continue;
  7684. + h_dentry = hd->hd_dentry;
  7685. + if (!h_dentry)
  7686. + continue;
  7687. + AuDebugOn(d_is_negative(h_dentry));
  7688. + h_inode = d_inode(h_dentry);
  7689. + AuDebugOn(orig_h.mode
  7690. + != (h_inode->i_mode
  7691. + & S_IFMT));
  7692. + au_set_h_dptr(dentry, bindex,
  7693. + dget(h_dentry));
  7694. + }
  7695. + if (inode)
  7696. + err = au_refresh_hinode(inode, dentry);
  7697. + au_dbg_verify_dinode(dentry);
  7698. + }
  7699. + } else {
  7700. + AuDbg("positive --> positive, different type\n");
  7701. + /* similar to the behaviour of removing and creating */
  7702. + au_hide(dentry);
  7703. + if (inode)
  7704. + err = au_refresh_hinode_self(inode);
  7705. + au_dbg_verify_dinode(dentry);
  7706. + }
  7707. + }
  7708. +
  7709. +out:
  7710. + return err;
  7711. +}
  7712. +
  7713. +void au_refresh_dop(struct dentry *dentry, int force_reval)
  7714. +{
  7715. + const struct dentry_operations *dop
  7716. + = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
  7717. + static const unsigned int mask
  7718. + = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
  7719. +
  7720. + BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
  7721. +
  7722. + if (dentry->d_op == dop)
  7723. + return;
  7724. +
  7725. + AuDbg("%pd\n", dentry);
  7726. + spin_lock(&dentry->d_lock);
  7727. + if (dop == &aufs_dop)
  7728. + dentry->d_flags |= mask;
  7729. + else
  7730. + dentry->d_flags &= ~mask;
  7731. + dentry->d_op = dop;
  7732. + spin_unlock(&dentry->d_lock);
  7733. +}
  7734. +
  7735. +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
  7736. +{
  7737. + int err, ebrange, nbr;
  7738. + unsigned int sigen;
  7739. + struct au_dinfo *dinfo, *tmp;
  7740. + struct super_block *sb;
  7741. + struct inode *inode;
  7742. +
  7743. + DiMustWriteLock(dentry);
  7744. + AuDebugOn(IS_ROOT(dentry));
  7745. + AuDebugOn(d_really_is_negative(parent));
  7746. +
  7747. + sb = dentry->d_sb;
  7748. + sigen = au_sigen(sb);
  7749. + err = au_digen_test(parent, sigen);
  7750. + if (unlikely(err))
  7751. + goto out;
  7752. +
  7753. + nbr = au_sbbot(sb) + 1;
  7754. + dinfo = au_di(dentry);
  7755. + err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
  7756. + if (unlikely(err))
  7757. + goto out;
  7758. + ebrange = au_dbrange_test(dentry);
  7759. + if (!ebrange)
  7760. + ebrange = au_do_refresh_hdentry(dentry, parent);
  7761. +
  7762. + if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
  7763. + AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
  7764. + if (d_really_is_positive(dentry)) {
  7765. + inode = d_inode(dentry);
  7766. + err = au_refresh_hinode_self(inode);
  7767. + }
  7768. + au_dbg_verify_dinode(dentry);
  7769. + if (!err)
  7770. + goto out_dgen; /* success */
  7771. + goto out;
  7772. + }
  7773. +
  7774. + /* temporary dinfo */
  7775. + AuDbgDentry(dentry);
  7776. + err = -ENOMEM;
  7777. + tmp = au_di_alloc(sb, AuLsc_DI_TMP);
  7778. + if (unlikely(!tmp))
  7779. + goto out;
  7780. + au_di_swap(tmp, dinfo);
  7781. + /* returns the number of positive dentries */
  7782. + /*
  7783. + * if current working dir is removed, it returns an error.
  7784. + * but the dentry is legal.
  7785. + */
  7786. + err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
  7787. + AuDbgDentry(dentry);
  7788. + au_di_swap(tmp, dinfo);
  7789. + if (err == -ENOENT)
  7790. + err = 0;
  7791. + if (err >= 0) {
  7792. + /* compare/refresh by dinfo */
  7793. + AuDbgDentry(dentry);
  7794. + err = au_refresh_by_dinfo(dentry, dinfo, tmp);
  7795. + au_dbg_verify_dinode(dentry);
  7796. + AuTraceErr(err);
  7797. + }
  7798. + au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
  7799. + au_rw_write_unlock(&tmp->di_rwsem);
  7800. + au_di_free(tmp);
  7801. + if (unlikely(err))
  7802. + goto out;
  7803. +
  7804. +out_dgen:
  7805. + au_update_digen(dentry);
  7806. +out:
  7807. + if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
  7808. + AuIOErr("failed refreshing %pd, %d\n", dentry, err);
  7809. + AuDbgDentry(dentry);
  7810. + }
  7811. + AuTraceErr(err);
  7812. + return err;
  7813. +}
  7814. +
  7815. +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
  7816. + struct dentry *dentry, aufs_bindex_t bindex)
  7817. +{
  7818. + int err, valid;
  7819. +
  7820. + err = 0;
  7821. + if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
  7822. + goto out;
  7823. +
  7824. + AuDbg("b%d\n", bindex);
  7825. + /*
  7826. + * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
  7827. + * due to whiteout and branch permission.
  7828. + */
  7829. + flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
  7830. + | LOOKUP_FOLLOW | LOOKUP_EXCL);
  7831. + /* it may return tri-state */
  7832. + valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
  7833. +
  7834. + if (unlikely(valid < 0))
  7835. + err = valid;
  7836. + else if (!valid)
  7837. + err = -EINVAL;
  7838. +
  7839. +out:
  7840. + AuTraceErr(err);
  7841. + return err;
  7842. +}
  7843. +
  7844. +/* todo: remove this */
  7845. +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
  7846. + unsigned int flags, int do_udba, int dirren)
  7847. +{
  7848. + int err;
  7849. + umode_t mode, h_mode;
  7850. + aufs_bindex_t bindex, btail, btop, ibs, ibe;
  7851. + unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
  7852. + struct inode *h_inode, *h_cached_inode;
  7853. + struct dentry *h_dentry;
  7854. + struct qstr *name, *h_name;
  7855. +
  7856. + err = 0;
  7857. + plus = 0;
  7858. + mode = 0;
  7859. + ibs = -1;
  7860. + ibe = -1;
  7861. + unhashed = !!d_unhashed(dentry);
  7862. + is_root = !!IS_ROOT(dentry);
  7863. + name = &dentry->d_name;
  7864. + tmpfile = au_di(dentry)->di_tmpfile;
  7865. +
  7866. + /*
  7867. + * Theoretically, REVAL test should be unnecessary in case of
  7868. + * {FS,I}NOTIFY.
  7869. + * But {fs,i}notify doesn't fire some necessary events,
  7870. + * IN_ATTRIB for atime/nlink/pageio
  7871. + * Let's do REVAL test too.
  7872. + */
  7873. + if (do_udba && inode) {
  7874. + mode = (inode->i_mode & S_IFMT);
  7875. + plus = (inode->i_nlink > 0);
  7876. + ibs = au_ibtop(inode);
  7877. + ibe = au_ibbot(inode);
  7878. + }
  7879. +
  7880. + btop = au_dbtop(dentry);
  7881. + btail = btop;
  7882. + if (inode && S_ISDIR(inode->i_mode))
  7883. + btail = au_dbtaildir(dentry);
  7884. + for (bindex = btop; bindex <= btail; bindex++) {
  7885. + h_dentry = au_h_dptr(dentry, bindex);
  7886. + if (!h_dentry)
  7887. + continue;
  7888. +
  7889. + AuDbg("b%d, %pd\n", bindex, h_dentry);
  7890. + h_nfs = !!au_test_nfs(h_dentry->d_sb);
  7891. + spin_lock(&h_dentry->d_lock);
  7892. + h_name = &h_dentry->d_name;
  7893. + if (unlikely(do_udba
  7894. + && !is_root
  7895. + && ((!h_nfs
  7896. + && (unhashed != !!d_unhashed(h_dentry)
  7897. + || (!tmpfile && !dirren
  7898. + && !au_qstreq(name, h_name))
  7899. + ))
  7900. + || (h_nfs
  7901. + && !(flags & LOOKUP_OPEN)
  7902. + && (h_dentry->d_flags
  7903. + & DCACHE_NFSFS_RENAMED)))
  7904. + )) {
  7905. + int h_unhashed;
  7906. +
  7907. + h_unhashed = d_unhashed(h_dentry);
  7908. + spin_unlock(&h_dentry->d_lock);
  7909. + AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
  7910. + unhashed, h_unhashed, dentry, h_dentry);
  7911. + goto err;
  7912. + }
  7913. + spin_unlock(&h_dentry->d_lock);
  7914. +
  7915. + err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
  7916. + if (unlikely(err))
  7917. + /* do not goto err, to keep the errno */
  7918. + break;
  7919. +
  7920. + /* todo: plink too? */
  7921. + if (!do_udba)
  7922. + continue;
  7923. +
  7924. + /* UDBA tests */
  7925. + if (unlikely(!!inode != d_is_positive(h_dentry)))
  7926. + goto err;
  7927. +
  7928. + h_inode = NULL;
  7929. + if (d_is_positive(h_dentry))
  7930. + h_inode = d_inode(h_dentry);
  7931. + h_plus = plus;
  7932. + h_mode = mode;
  7933. + h_cached_inode = h_inode;
  7934. + if (h_inode) {
  7935. + h_mode = (h_inode->i_mode & S_IFMT);
  7936. + h_plus = (h_inode->i_nlink > 0);
  7937. + }
  7938. + if (inode && ibs <= bindex && bindex <= ibe)
  7939. + h_cached_inode = au_h_iptr(inode, bindex);
  7940. +
  7941. + if (!h_nfs) {
  7942. + if (unlikely(plus != h_plus && !tmpfile))
  7943. + goto err;
  7944. + } else {
  7945. + if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
  7946. + && !is_root
  7947. + && !IS_ROOT(h_dentry)
  7948. + && unhashed != d_unhashed(h_dentry)))
  7949. + goto err;
  7950. + }
  7951. + if (unlikely(mode != h_mode
  7952. + || h_cached_inode != h_inode))
  7953. + goto err;
  7954. + continue;
  7955. +
  7956. +err:
  7957. + err = -EINVAL;
  7958. + break;
  7959. + }
  7960. +
  7961. + AuTraceErr(err);
  7962. + return err;
  7963. +}
  7964. +
  7965. +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
  7966. +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
  7967. +{
  7968. + int err;
  7969. + struct dentry *parent;
  7970. +
  7971. + if (!au_digen_test(dentry, sigen))
  7972. + return 0;
  7973. +
  7974. + parent = dget_parent(dentry);
  7975. + di_read_lock_parent(parent, AuLock_IR);
  7976. + AuDebugOn(au_digen_test(parent, sigen));
  7977. + au_dbg_verify_gen(parent, sigen);
  7978. + err = au_refresh_dentry(dentry, parent);
  7979. + di_read_unlock(parent, AuLock_IR);
  7980. + dput(parent);
  7981. + AuTraceErr(err);
  7982. + return err;
  7983. +}
  7984. +
  7985. +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
  7986. +{
  7987. + int err;
  7988. + struct dentry *d, *parent;
  7989. +
  7990. + if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
  7991. + return simple_reval_dpath(dentry, sigen);
  7992. +
  7993. + /* slow loop, keep it simple and stupid */
  7994. + /* cf: au_cpup_dirs() */
  7995. + err = 0;
  7996. + parent = NULL;
  7997. + while (au_digen_test(dentry, sigen)) {
  7998. + d = dentry;
  7999. + while (1) {
  8000. + dput(parent);
  8001. + parent = dget_parent(d);
  8002. + if (!au_digen_test(parent, sigen))
  8003. + break;
  8004. + d = parent;
  8005. + }
  8006. +
  8007. + if (d != dentry)
  8008. + di_write_lock_child2(d);
  8009. +
  8010. + /* someone might update our dentry while we were sleeping */
  8011. + if (au_digen_test(d, sigen)) {
  8012. + /*
  8013. + * todo: consolidate with simple_reval_dpath(),
  8014. + * do_refresh() and au_reval_for_attr().
  8015. + */
  8016. + di_read_lock_parent(parent, AuLock_IR);
  8017. + err = au_refresh_dentry(d, parent);
  8018. + di_read_unlock(parent, AuLock_IR);
  8019. + }
  8020. +
  8021. + if (d != dentry)
  8022. + di_write_unlock(d);
  8023. + dput(parent);
  8024. + if (unlikely(err))
  8025. + break;
  8026. + }
  8027. +
  8028. + return err;
  8029. +}
  8030. +
  8031. +/*
  8032. + * if valid returns 1, otherwise 0.
  8033. + */
  8034. +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
  8035. +{
  8036. + int valid, err;
  8037. + unsigned int sigen;
  8038. + unsigned char do_udba, dirren;
  8039. + struct super_block *sb;
  8040. + struct inode *inode;
  8041. +
  8042. + /* todo: support rcu-walk? */
  8043. + if (flags & LOOKUP_RCU)
  8044. + return -ECHILD;
  8045. +
  8046. + valid = 0;
  8047. + if (unlikely(!au_di(dentry)))
  8048. + goto out;
  8049. +
  8050. + valid = 1;
  8051. + sb = dentry->d_sb;
  8052. + /*
  8053. + * todo: very ugly
  8054. + * i_mutex of parent dir may be held,
  8055. + * but we should not return 'invalid' due to busy.
  8056. + */
  8057. + err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
  8058. + if (unlikely(err)) {
  8059. + valid = err;
  8060. + AuTraceErr(err);
  8061. + goto out;
  8062. + }
  8063. + inode = NULL;
  8064. + if (d_really_is_positive(dentry))
  8065. + inode = d_inode(dentry);
  8066. + if (unlikely(inode && au_is_bad_inode(inode))) {
  8067. + err = -EINVAL;
  8068. + AuTraceErr(err);
  8069. + goto out_dgrade;
  8070. + }
  8071. + if (unlikely(au_dbrange_test(dentry))) {
  8072. + err = -EINVAL;
  8073. + AuTraceErr(err);
  8074. + goto out_dgrade;
  8075. + }
  8076. +
  8077. + sigen = au_sigen(sb);
  8078. + if (au_digen_test(dentry, sigen)) {
  8079. + AuDebugOn(IS_ROOT(dentry));
  8080. + err = au_reval_dpath(dentry, sigen);
  8081. + if (unlikely(err)) {
  8082. + AuTraceErr(err);
  8083. + goto out_dgrade;
  8084. + }
  8085. + }
  8086. + di_downgrade_lock(dentry, AuLock_IR);
  8087. +
  8088. + err = -EINVAL;
  8089. + if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
  8090. + && inode
  8091. + && !(inode->i_state && I_LINKABLE)
  8092. + && (IS_DEADDIR(inode) || !inode->i_nlink)) {
  8093. + AuTraceErr(err);
  8094. + goto out_inval;
  8095. + }
  8096. +
  8097. + do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
  8098. + if (do_udba && inode) {
  8099. + aufs_bindex_t btop = au_ibtop(inode);
  8100. + struct inode *h_inode;
  8101. +
  8102. + if (btop >= 0) {
  8103. + h_inode = au_h_iptr(inode, btop);
  8104. + if (h_inode && au_test_higen(inode, h_inode)) {
  8105. + AuTraceErr(err);
  8106. + goto out_inval;
  8107. + }
  8108. + }
  8109. + }
  8110. +
  8111. + dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
  8112. + err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
  8113. + if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
  8114. + err = -EIO;
  8115. + AuDbg("both of real entry and whiteout found, %p, err %d\n",
  8116. + dentry, err);
  8117. + }
  8118. + goto out_inval;
  8119. +
  8120. +out_dgrade:
  8121. + di_downgrade_lock(dentry, AuLock_IR);
  8122. +out_inval:
  8123. + aufs_read_unlock(dentry, AuLock_IR);
  8124. + AuTraceErr(err);
  8125. + valid = !err;
  8126. +out:
  8127. + if (!valid) {
  8128. + AuDbg("%pd invalid, %d\n", dentry, valid);
  8129. + d_drop(dentry);
  8130. + }
  8131. + return valid;
  8132. +}
  8133. +
  8134. +static void aufs_d_release(struct dentry *dentry)
  8135. +{
  8136. + if (au_di(dentry)) {
  8137. + au_di_fin(dentry);
  8138. + au_hn_di_reinit(dentry);
  8139. + }
  8140. +}
  8141. +
  8142. +const struct dentry_operations aufs_dop = {
  8143. + .d_revalidate = aufs_d_revalidate,
  8144. + .d_weak_revalidate = aufs_d_revalidate,
  8145. + .d_release = aufs_d_release
  8146. +};
  8147. +
  8148. +/* aufs_dop without d_revalidate */
  8149. +const struct dentry_operations aufs_dop_noreval = {
  8150. + .d_release = aufs_d_release
  8151. +};
  8152. diff -Naur null/fs/aufs/dentry.h linux-5.1/fs/aufs/dentry.h
  8153. --- /dev/null
  8154. +++ linux-5.1/fs/aufs/dentry.h 2019-05-22 11:23:07.439141088 +0200
  8155. @@ -0,0 +1,268 @@
  8156. +/* SPDX-License-Identifier: GPL-2.0 */
  8157. +/*
  8158. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  8159. + *
  8160. + * This program, aufs is free software; you can redistribute it and/or modify
  8161. + * it under the terms of the GNU General Public License as published by
  8162. + * the Free Software Foundation; either version 2 of the License, or
  8163. + * (at your option) any later version.
  8164. + *
  8165. + * This program is distributed in the hope that it will be useful,
  8166. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8167. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8168. + * GNU General Public License for more details.
  8169. + *
  8170. + * You should have received a copy of the GNU General Public License
  8171. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  8172. + */
  8173. +
  8174. +/*
  8175. + * lookup and dentry operations
  8176. + */
  8177. +
  8178. +#ifndef __AUFS_DENTRY_H__
  8179. +#define __AUFS_DENTRY_H__
  8180. +
  8181. +#ifdef __KERNEL__
  8182. +
  8183. +#include <linux/dcache.h>
  8184. +#include "dirren.h"
  8185. +#include "rwsem.h"
  8186. +
  8187. +struct au_hdentry {
  8188. + struct dentry *hd_dentry;
  8189. + aufs_bindex_t hd_id;
  8190. +};
  8191. +
  8192. +struct au_dinfo {
  8193. + atomic_t di_generation;
  8194. +
  8195. + struct au_rwsem di_rwsem;
  8196. + aufs_bindex_t di_btop, di_bbot, di_bwh, di_bdiropq;
  8197. + unsigned char di_tmpfile; /* to allow the different name */
  8198. + struct au_hdentry *di_hdentry;
  8199. + struct rcu_head rcu;
  8200. +} ____cacheline_aligned_in_smp;
  8201. +
  8202. +/* ---------------------------------------------------------------------- */
  8203. +
  8204. +/* flags for au_lkup_dentry() */
  8205. +#define AuLkup_ALLOW_NEG 1
  8206. +#define AuLkup_IGNORE_PERM (1 << 1)
  8207. +#define AuLkup_DIRREN (1 << 2)
  8208. +#define au_ftest_lkup(flags, name) ((flags) & AuLkup_##name)
  8209. +#define au_fset_lkup(flags, name) \
  8210. + do { (flags) |= AuLkup_##name; } while (0)
  8211. +#define au_fclr_lkup(flags, name) \
  8212. + do { (flags) &= ~AuLkup_##name; } while (0)
  8213. +
  8214. +#ifndef CONFIG_AUFS_DIRREN
  8215. +#undef AuLkup_DIRREN
  8216. +#define AuLkup_DIRREN 0
  8217. +#endif
  8218. +
  8219. +struct au_do_lookup_args {
  8220. + unsigned int flags;
  8221. + mode_t type;
  8222. + struct qstr whname, *name;
  8223. + struct au_dr_lookup dirren;
  8224. +};
  8225. +
  8226. +/* ---------------------------------------------------------------------- */
  8227. +
  8228. +/* dentry.c */
  8229. +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
  8230. +struct au_branch;
  8231. +struct dentry *au_sio_lkup_one(struct qstr *name, struct dentry *parent);
  8232. +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
  8233. + struct dentry *h_parent, struct au_branch *br);
  8234. +
  8235. +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
  8236. + unsigned int flags);
  8237. +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
  8238. +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
  8239. +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
  8240. +void au_refresh_dop(struct dentry *dentry, int force_reval);
  8241. +
  8242. +/* dinfo.c */
  8243. +void au_di_init_once(void *_di);
  8244. +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
  8245. +void au_di_free(struct au_dinfo *dinfo);
  8246. +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
  8247. +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
  8248. +int au_di_init(struct dentry *dentry);
  8249. +void au_di_fin(struct dentry *dentry);
  8250. +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
  8251. +
  8252. +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
  8253. +void di_read_unlock(struct dentry *d, int flags);
  8254. +void di_downgrade_lock(struct dentry *d, int flags);
  8255. +void di_write_lock(struct dentry *d, unsigned int lsc);
  8256. +void di_write_unlock(struct dentry *d);
  8257. +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
  8258. +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
  8259. +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
  8260. +
  8261. +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
  8262. +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
  8263. +aufs_bindex_t au_dbtail(struct dentry *dentry);
  8264. +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
  8265. +
  8266. +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
  8267. + struct dentry *h_dentry);
  8268. +int au_digen_test(struct dentry *dentry, unsigned int sigen);
  8269. +int au_dbrange_test(struct dentry *dentry);
  8270. +void au_update_digen(struct dentry *dentry);
  8271. +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
  8272. +void au_update_dbtop(struct dentry *dentry);
  8273. +void au_update_dbbot(struct dentry *dentry);
  8274. +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
  8275. +
  8276. +/* ---------------------------------------------------------------------- */
  8277. +
  8278. +static inline struct au_dinfo *au_di(struct dentry *dentry)
  8279. +{
  8280. + return dentry->d_fsdata;
  8281. +}
  8282. +
  8283. +/* ---------------------------------------------------------------------- */
  8284. +
  8285. +/* lock subclass for dinfo */
  8286. +enum {
  8287. + AuLsc_DI_CHILD, /* child first */
  8288. + AuLsc_DI_CHILD2, /* rename(2), link(2), and cpup at hnotify */
  8289. + AuLsc_DI_CHILD3, /* copyup dirs */
  8290. + AuLsc_DI_PARENT,
  8291. + AuLsc_DI_PARENT2,
  8292. + AuLsc_DI_PARENT3,
  8293. + AuLsc_DI_TMP /* temp for replacing dinfo */
  8294. +};
  8295. +
  8296. +/*
  8297. + * di_read_lock_child, di_write_lock_child,
  8298. + * di_read_lock_child2, di_write_lock_child2,
  8299. + * di_read_lock_child3, di_write_lock_child3,
  8300. + * di_read_lock_parent, di_write_lock_parent,
  8301. + * di_read_lock_parent2, di_write_lock_parent2,
  8302. + * di_read_lock_parent3, di_write_lock_parent3,
  8303. + */
  8304. +#define AuReadLockFunc(name, lsc) \
  8305. +static inline void di_read_lock_##name(struct dentry *d, int flags) \
  8306. +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
  8307. +
  8308. +#define AuWriteLockFunc(name, lsc) \
  8309. +static inline void di_write_lock_##name(struct dentry *d) \
  8310. +{ di_write_lock(d, AuLsc_DI_##lsc); }
  8311. +
  8312. +#define AuRWLockFuncs(name, lsc) \
  8313. + AuReadLockFunc(name, lsc) \
  8314. + AuWriteLockFunc(name, lsc)
  8315. +
  8316. +AuRWLockFuncs(child, CHILD);
  8317. +AuRWLockFuncs(child2, CHILD2);
  8318. +AuRWLockFuncs(child3, CHILD3);
  8319. +AuRWLockFuncs(parent, PARENT);
  8320. +AuRWLockFuncs(parent2, PARENT2);
  8321. +AuRWLockFuncs(parent3, PARENT3);
  8322. +
  8323. +#undef AuReadLockFunc
  8324. +#undef AuWriteLockFunc
  8325. +#undef AuRWLockFuncs
  8326. +
  8327. +#define DiMustNoWaiters(d) AuRwMustNoWaiters(&au_di(d)->di_rwsem)
  8328. +#define DiMustAnyLock(d) AuRwMustAnyLock(&au_di(d)->di_rwsem)
  8329. +#define DiMustWriteLock(d) AuRwMustWriteLock(&au_di(d)->di_rwsem)
  8330. +
  8331. +/* ---------------------------------------------------------------------- */
  8332. +
  8333. +/* todo: memory barrier? */
  8334. +static inline unsigned int au_digen(struct dentry *d)
  8335. +{
  8336. + return atomic_read(&au_di(d)->di_generation);
  8337. +}
  8338. +
  8339. +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
  8340. +{
  8341. + hdentry->hd_dentry = NULL;
  8342. +}
  8343. +
  8344. +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
  8345. + aufs_bindex_t bindex)
  8346. +{
  8347. + return di->di_hdentry + bindex;
  8348. +}
  8349. +
  8350. +static inline void au_hdput(struct au_hdentry *hd)
  8351. +{
  8352. + if (hd)
  8353. + dput(hd->hd_dentry);
  8354. +}
  8355. +
  8356. +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
  8357. +{
  8358. + DiMustAnyLock(dentry);
  8359. + return au_di(dentry)->di_btop;
  8360. +}
  8361. +
  8362. +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
  8363. +{
  8364. + DiMustAnyLock(dentry);
  8365. + return au_di(dentry)->di_bbot;
  8366. +}
  8367. +
  8368. +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
  8369. +{
  8370. + DiMustAnyLock(dentry);
  8371. + return au_di(dentry)->di_bwh;
  8372. +}
  8373. +
  8374. +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
  8375. +{
  8376. + DiMustAnyLock(dentry);
  8377. + return au_di(dentry)->di_bdiropq;
  8378. +}
  8379. +
  8380. +/* todo: hard/soft set? */
  8381. +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
  8382. +{
  8383. + DiMustWriteLock(dentry);
  8384. + au_di(dentry)->di_btop = bindex;
  8385. +}
  8386. +
  8387. +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
  8388. +{
  8389. + DiMustWriteLock(dentry);
  8390. + au_di(dentry)->di_bbot = bindex;
  8391. +}
  8392. +
  8393. +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
  8394. +{
  8395. + DiMustWriteLock(dentry);
  8396. + /* dbwh can be outside of btop - bbot range */
  8397. + au_di(dentry)->di_bwh = bindex;
  8398. +}
  8399. +
  8400. +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
  8401. +{
  8402. + DiMustWriteLock(dentry);
  8403. + au_di(dentry)->di_bdiropq = bindex;
  8404. +}
  8405. +
  8406. +/* ---------------------------------------------------------------------- */
  8407. +
  8408. +#ifdef CONFIG_AUFS_HNOTIFY
  8409. +static inline void au_digen_dec(struct dentry *d)
  8410. +{
  8411. + atomic_dec(&au_di(d)->di_generation);
  8412. +}
  8413. +
  8414. +static inline void au_hn_di_reinit(struct dentry *dentry)
  8415. +{
  8416. + dentry->d_fsdata = NULL;
  8417. +}
  8418. +#else
  8419. +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
  8420. +#endif /* CONFIG_AUFS_HNOTIFY */
  8421. +
  8422. +#endif /* __KERNEL__ */
  8423. +#endif /* __AUFS_DENTRY_H__ */
  8424. diff -Naur null/fs/aufs/dinfo.c linux-5.1/fs/aufs/dinfo.c
  8425. --- /dev/null
  8426. +++ linux-5.1/fs/aufs/dinfo.c 2019-05-22 11:23:07.439141088 +0200
  8427. @@ -0,0 +1,554 @@
  8428. +// SPDX-License-Identifier: GPL-2.0
  8429. +/*
  8430. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  8431. + *
  8432. + * This program, aufs is free software; you can redistribute it and/or modify
  8433. + * it under the terms of the GNU General Public License as published by
  8434. + * the Free Software Foundation; either version 2 of the License, or
  8435. + * (at your option) any later version.
  8436. + *
  8437. + * This program is distributed in the hope that it will be useful,
  8438. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8439. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8440. + * GNU General Public License for more details.
  8441. + *
  8442. + * You should have received a copy of the GNU General Public License
  8443. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  8444. + */
  8445. +
  8446. +/*
  8447. + * dentry private data
  8448. + */
  8449. +
  8450. +#include "aufs.h"
  8451. +
  8452. +void au_di_init_once(void *_dinfo)
  8453. +{
  8454. + struct au_dinfo *dinfo = _dinfo;
  8455. +
  8456. + au_rw_init(&dinfo->di_rwsem);
  8457. +}
  8458. +
  8459. +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
  8460. +{
  8461. + struct au_dinfo *dinfo;
  8462. + int nbr, i;
  8463. +
  8464. + dinfo = au_cache_alloc_dinfo();
  8465. + if (unlikely(!dinfo))
  8466. + goto out;
  8467. +
  8468. + nbr = au_sbbot(sb) + 1;
  8469. + if (nbr <= 0)
  8470. + nbr = 1;
  8471. + dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
  8472. + if (dinfo->di_hdentry) {
  8473. + au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
  8474. + dinfo->di_btop = -1;
  8475. + dinfo->di_bbot = -1;
  8476. + dinfo->di_bwh = -1;
  8477. + dinfo->di_bdiropq = -1;
  8478. + dinfo->di_tmpfile = 0;
  8479. + for (i = 0; i < nbr; i++)
  8480. + dinfo->di_hdentry[i].hd_id = -1;
  8481. + goto out;
  8482. + }
  8483. +
  8484. + au_cache_free_dinfo(dinfo);
  8485. + dinfo = NULL;
  8486. +
  8487. +out:
  8488. + return dinfo;
  8489. +}
  8490. +
  8491. +void au_di_free(struct au_dinfo *dinfo)
  8492. +{
  8493. + struct au_hdentry *p;
  8494. + aufs_bindex_t bbot, bindex;
  8495. +
  8496. + /* dentry may not be revalidated */
  8497. + bindex = dinfo->di_btop;
  8498. + if (bindex >= 0) {
  8499. + bbot = dinfo->di_bbot;
  8500. + p = au_hdentry(dinfo, bindex);
  8501. + while (bindex++ <= bbot)
  8502. + au_hdput(p++);
  8503. + }
  8504. + au_kfree_try_rcu(dinfo->di_hdentry);
  8505. + au_cache_free_dinfo(dinfo);
  8506. +}
  8507. +
  8508. +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
  8509. +{
  8510. + struct au_hdentry *p;
  8511. + aufs_bindex_t bi;
  8512. +
  8513. + AuRwMustWriteLock(&a->di_rwsem);
  8514. + AuRwMustWriteLock(&b->di_rwsem);
  8515. +
  8516. +#define DiSwap(v, name) \
  8517. + do { \
  8518. + v = a->di_##name; \
  8519. + a->di_##name = b->di_##name; \
  8520. + b->di_##name = v; \
  8521. + } while (0)
  8522. +
  8523. + DiSwap(p, hdentry);
  8524. + DiSwap(bi, btop);
  8525. + DiSwap(bi, bbot);
  8526. + DiSwap(bi, bwh);
  8527. + DiSwap(bi, bdiropq);
  8528. + /* smp_mb(); */
  8529. +
  8530. +#undef DiSwap
  8531. +}
  8532. +
  8533. +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
  8534. +{
  8535. + AuRwMustWriteLock(&dst->di_rwsem);
  8536. + AuRwMustWriteLock(&src->di_rwsem);
  8537. +
  8538. + dst->di_btop = src->di_btop;
  8539. + dst->di_bbot = src->di_bbot;
  8540. + dst->di_bwh = src->di_bwh;
  8541. + dst->di_bdiropq = src->di_bdiropq;
  8542. + /* smp_mb(); */
  8543. +}
  8544. +
  8545. +int au_di_init(struct dentry *dentry)
  8546. +{
  8547. + int err;
  8548. + struct super_block *sb;
  8549. + struct au_dinfo *dinfo;
  8550. +
  8551. + err = 0;
  8552. + sb = dentry->d_sb;
  8553. + dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
  8554. + if (dinfo) {
  8555. + atomic_set(&dinfo->di_generation, au_sigen(sb));
  8556. + /* smp_mb(); */ /* atomic_set */
  8557. + dentry->d_fsdata = dinfo;
  8558. + } else
  8559. + err = -ENOMEM;
  8560. +
  8561. + return err;
  8562. +}
  8563. +
  8564. +void au_di_fin(struct dentry *dentry)
  8565. +{
  8566. + struct au_dinfo *dinfo;
  8567. +
  8568. + dinfo = au_di(dentry);
  8569. + AuRwDestroy(&dinfo->di_rwsem);
  8570. + au_di_free(dinfo);
  8571. +}
  8572. +
  8573. +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
  8574. +{
  8575. + int err, sz;
  8576. + struct au_hdentry *hdp;
  8577. +
  8578. + AuRwMustWriteLock(&dinfo->di_rwsem);
  8579. +
  8580. + err = -ENOMEM;
  8581. + sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
  8582. + if (!sz)
  8583. + sz = sizeof(*hdp);
  8584. + hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
  8585. + may_shrink);
  8586. + if (hdp) {
  8587. + dinfo->di_hdentry = hdp;
  8588. + err = 0;
  8589. + }
  8590. +
  8591. + return err;
  8592. +}
  8593. +
  8594. +/* ---------------------------------------------------------------------- */
  8595. +
  8596. +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
  8597. +{
  8598. + switch (lsc) {
  8599. + case AuLsc_DI_CHILD:
  8600. + ii_write_lock_child(inode);
  8601. + break;
  8602. + case AuLsc_DI_CHILD2:
  8603. + ii_write_lock_child2(inode);
  8604. + break;
  8605. + case AuLsc_DI_CHILD3:
  8606. + ii_write_lock_child3(inode);
  8607. + break;
  8608. + case AuLsc_DI_PARENT:
  8609. + ii_write_lock_parent(inode);
  8610. + break;
  8611. + case AuLsc_DI_PARENT2:
  8612. + ii_write_lock_parent2(inode);
  8613. + break;
  8614. + case AuLsc_DI_PARENT3:
  8615. + ii_write_lock_parent3(inode);
  8616. + break;
  8617. + default:
  8618. + BUG();
  8619. + }
  8620. +}
  8621. +
  8622. +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
  8623. +{
  8624. + switch (lsc) {
  8625. + case AuLsc_DI_CHILD:
  8626. + ii_read_lock_child(inode);
  8627. + break;
  8628. + case AuLsc_DI_CHILD2:
  8629. + ii_read_lock_child2(inode);
  8630. + break;
  8631. + case AuLsc_DI_CHILD3:
  8632. + ii_read_lock_child3(inode);
  8633. + break;
  8634. + case AuLsc_DI_PARENT:
  8635. + ii_read_lock_parent(inode);
  8636. + break;
  8637. + case AuLsc_DI_PARENT2:
  8638. + ii_read_lock_parent2(inode);
  8639. + break;
  8640. + case AuLsc_DI_PARENT3:
  8641. + ii_read_lock_parent3(inode);
  8642. + break;
  8643. + default:
  8644. + BUG();
  8645. + }
  8646. +}
  8647. +
  8648. +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
  8649. +{
  8650. + struct inode *inode;
  8651. +
  8652. + au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
  8653. + if (d_really_is_positive(d)) {
  8654. + inode = d_inode(d);
  8655. + if (au_ftest_lock(flags, IW))
  8656. + do_ii_write_lock(inode, lsc);
  8657. + else if (au_ftest_lock(flags, IR))
  8658. + do_ii_read_lock(inode, lsc);
  8659. + }
  8660. +}
  8661. +
  8662. +void di_read_unlock(struct dentry *d, int flags)
  8663. +{
  8664. + struct inode *inode;
  8665. +
  8666. + if (d_really_is_positive(d)) {
  8667. + inode = d_inode(d);
  8668. + if (au_ftest_lock(flags, IW)) {
  8669. + au_dbg_verify_dinode(d);
  8670. + ii_write_unlock(inode);
  8671. + } else if (au_ftest_lock(flags, IR)) {
  8672. + au_dbg_verify_dinode(d);
  8673. + ii_read_unlock(inode);
  8674. + }
  8675. + }
  8676. + au_rw_read_unlock(&au_di(d)->di_rwsem);
  8677. +}
  8678. +
  8679. +void di_downgrade_lock(struct dentry *d, int flags)
  8680. +{
  8681. + if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
  8682. + ii_downgrade_lock(d_inode(d));
  8683. + au_rw_dgrade_lock(&au_di(d)->di_rwsem);
  8684. +}
  8685. +
  8686. +void di_write_lock(struct dentry *d, unsigned int lsc)
  8687. +{
  8688. + au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
  8689. + if (d_really_is_positive(d))
  8690. + do_ii_write_lock(d_inode(d), lsc);
  8691. +}
  8692. +
  8693. +void di_write_unlock(struct dentry *d)
  8694. +{
  8695. + au_dbg_verify_dinode(d);
  8696. + if (d_really_is_positive(d))
  8697. + ii_write_unlock(d_inode(d));
  8698. + au_rw_write_unlock(&au_di(d)->di_rwsem);
  8699. +}
  8700. +
  8701. +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
  8702. +{
  8703. + AuDebugOn(d1 == d2
  8704. + || d_inode(d1) == d_inode(d2)
  8705. + || d1->d_sb != d2->d_sb);
  8706. +
  8707. + if ((isdir && au_test_subdir(d1, d2))
  8708. + || d1 < d2) {
  8709. + di_write_lock_child(d1);
  8710. + di_write_lock_child2(d2);
  8711. + } else {
  8712. + di_write_lock_child(d2);
  8713. + di_write_lock_child2(d1);
  8714. + }
  8715. +}
  8716. +
  8717. +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
  8718. +{
  8719. + AuDebugOn(d1 == d2
  8720. + || d_inode(d1) == d_inode(d2)
  8721. + || d1->d_sb != d2->d_sb);
  8722. +
  8723. + if ((isdir && au_test_subdir(d1, d2))
  8724. + || d1 < d2) {
  8725. + di_write_lock_parent(d1);
  8726. + di_write_lock_parent2(d2);
  8727. + } else {
  8728. + di_write_lock_parent(d2);
  8729. + di_write_lock_parent2(d1);
  8730. + }
  8731. +}
  8732. +
  8733. +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
  8734. +{
  8735. + di_write_unlock(d1);
  8736. + if (d_inode(d1) == d_inode(d2))
  8737. + au_rw_write_unlock(&au_di(d2)->di_rwsem);
  8738. + else
  8739. + di_write_unlock(d2);
  8740. +}
  8741. +
  8742. +/* ---------------------------------------------------------------------- */
  8743. +
  8744. +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
  8745. +{
  8746. + struct dentry *d;
  8747. +
  8748. + DiMustAnyLock(dentry);
  8749. +
  8750. + if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
  8751. + return NULL;
  8752. + AuDebugOn(bindex < 0);
  8753. + d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
  8754. + AuDebugOn(d && au_dcount(d) <= 0);
  8755. + return d;
  8756. +}
  8757. +
  8758. +/*
  8759. + * extended version of au_h_dptr().
  8760. + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
  8761. + * error.
  8762. + */
  8763. +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
  8764. +{
  8765. + struct dentry *h_dentry;
  8766. + struct inode *inode, *h_inode;
  8767. +
  8768. + AuDebugOn(d_really_is_negative(dentry));
  8769. +
  8770. + h_dentry = NULL;
  8771. + if (au_dbtop(dentry) <= bindex
  8772. + && bindex <= au_dbbot(dentry))
  8773. + h_dentry = au_h_dptr(dentry, bindex);
  8774. + if (h_dentry && !au_d_linkable(h_dentry)) {
  8775. + dget(h_dentry);
  8776. + goto out; /* success */
  8777. + }
  8778. +
  8779. + inode = d_inode(dentry);
  8780. + AuDebugOn(bindex < au_ibtop(inode));
  8781. + AuDebugOn(au_ibbot(inode) < bindex);
  8782. + h_inode = au_h_iptr(inode, bindex);
  8783. + h_dentry = d_find_alias(h_inode);
  8784. + if (h_dentry) {
  8785. + if (!IS_ERR(h_dentry)) {
  8786. + if (!au_d_linkable(h_dentry))
  8787. + goto out; /* success */
  8788. + dput(h_dentry);
  8789. + } else
  8790. + goto out;
  8791. + }
  8792. +
  8793. + if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
  8794. + h_dentry = au_plink_lkup(inode, bindex);
  8795. + AuDebugOn(!h_dentry);
  8796. + if (!IS_ERR(h_dentry)) {
  8797. + if (!au_d_hashed_positive(h_dentry))
  8798. + goto out; /* success */
  8799. + dput(h_dentry);
  8800. + h_dentry = NULL;
  8801. + }
  8802. + }
  8803. +
  8804. +out:
  8805. + AuDbgDentry(h_dentry);
  8806. + return h_dentry;
  8807. +}
  8808. +
  8809. +aufs_bindex_t au_dbtail(struct dentry *dentry)
  8810. +{
  8811. + aufs_bindex_t bbot, bwh;
  8812. +
  8813. + bbot = au_dbbot(dentry);
  8814. + if (0 <= bbot) {
  8815. + bwh = au_dbwh(dentry);
  8816. + if (!bwh)
  8817. + return bwh;
  8818. + if (0 < bwh && bwh < bbot)
  8819. + return bwh - 1;
  8820. + }
  8821. + return bbot;
  8822. +}
  8823. +
  8824. +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
  8825. +{
  8826. + aufs_bindex_t bbot, bopq;
  8827. +
  8828. + bbot = au_dbtail(dentry);
  8829. + if (0 <= bbot) {
  8830. + bopq = au_dbdiropq(dentry);
  8831. + if (0 <= bopq && bopq < bbot)
  8832. + bbot = bopq;
  8833. + }
  8834. + return bbot;
  8835. +}
  8836. +
  8837. +/* ---------------------------------------------------------------------- */
  8838. +
  8839. +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
  8840. + struct dentry *h_dentry)
  8841. +{
  8842. + struct au_dinfo *dinfo;
  8843. + struct au_hdentry *hd;
  8844. + struct au_branch *br;
  8845. +
  8846. + DiMustWriteLock(dentry);
  8847. +
  8848. + dinfo = au_di(dentry);
  8849. + hd = au_hdentry(dinfo, bindex);
  8850. + au_hdput(hd);
  8851. + hd->hd_dentry = h_dentry;
  8852. + if (h_dentry) {
  8853. + br = au_sbr(dentry->d_sb, bindex);
  8854. + hd->hd_id = br->br_id;
  8855. + }
  8856. +}
  8857. +
  8858. +int au_dbrange_test(struct dentry *dentry)
  8859. +{
  8860. + int err;
  8861. + aufs_bindex_t btop, bbot;
  8862. +
  8863. + err = 0;
  8864. + btop = au_dbtop(dentry);
  8865. + bbot = au_dbbot(dentry);
  8866. + if (btop >= 0)
  8867. + AuDebugOn(bbot < 0 && btop > bbot);
  8868. + else {
  8869. + err = -EIO;
  8870. + AuDebugOn(bbot >= 0);
  8871. + }
  8872. +
  8873. + return err;
  8874. +}
  8875. +
  8876. +int au_digen_test(struct dentry *dentry, unsigned int sigen)
  8877. +{
  8878. + int err;
  8879. +
  8880. + err = 0;
  8881. + if (unlikely(au_digen(dentry) != sigen
  8882. + || au_iigen_test(d_inode(dentry), sigen)))
  8883. + err = -EIO;
  8884. +
  8885. + return err;
  8886. +}
  8887. +
  8888. +void au_update_digen(struct dentry *dentry)
  8889. +{
  8890. + atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
  8891. + /* smp_mb(); */ /* atomic_set */
  8892. +}
  8893. +
  8894. +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
  8895. +{
  8896. + struct au_dinfo *dinfo;
  8897. + struct dentry *h_d;
  8898. + struct au_hdentry *hdp;
  8899. + aufs_bindex_t bindex, bbot;
  8900. +
  8901. + DiMustWriteLock(dentry);
  8902. +
  8903. + dinfo = au_di(dentry);
  8904. + if (!dinfo || dinfo->di_btop < 0)
  8905. + return;
  8906. +
  8907. + if (do_put_zero) {
  8908. + bbot = dinfo->di_bbot;
  8909. + bindex = dinfo->di_btop;
  8910. + hdp = au_hdentry(dinfo, bindex);
  8911. + for (; bindex <= bbot; bindex++, hdp++) {
  8912. + h_d = hdp->hd_dentry;
  8913. + if (h_d && d_is_negative(h_d))
  8914. + au_set_h_dptr(dentry, bindex, NULL);
  8915. + }
  8916. + }
  8917. +
  8918. + dinfo->di_btop = 0;
  8919. + hdp = au_hdentry(dinfo, dinfo->di_btop);
  8920. + for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
  8921. + if (hdp->hd_dentry)
  8922. + break;
  8923. + if (dinfo->di_btop > dinfo->di_bbot) {
  8924. + dinfo->di_btop = -1;
  8925. + dinfo->di_bbot = -1;
  8926. + return;
  8927. + }
  8928. +
  8929. + hdp = au_hdentry(dinfo, dinfo->di_bbot);
  8930. + for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
  8931. + if (hdp->hd_dentry)
  8932. + break;
  8933. + AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
  8934. +}
  8935. +
  8936. +void au_update_dbtop(struct dentry *dentry)
  8937. +{
  8938. + aufs_bindex_t bindex, bbot;
  8939. + struct dentry *h_dentry;
  8940. +
  8941. + bbot = au_dbbot(dentry);
  8942. + for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
  8943. + h_dentry = au_h_dptr(dentry, bindex);
  8944. + if (!h_dentry)
  8945. + continue;
  8946. + if (d_is_positive(h_dentry)) {
  8947. + au_set_dbtop(dentry, bindex);
  8948. + return;
  8949. + }
  8950. + au_set_h_dptr(dentry, bindex, NULL);
  8951. + }
  8952. +}
  8953. +
  8954. +void au_update_dbbot(struct dentry *dentry)
  8955. +{
  8956. + aufs_bindex_t bindex, btop;
  8957. + struct dentry *h_dentry;
  8958. +
  8959. + btop = au_dbtop(dentry);
  8960. + for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
  8961. + h_dentry = au_h_dptr(dentry, bindex);
  8962. + if (!h_dentry)
  8963. + continue;
  8964. + if (d_is_positive(h_dentry)) {
  8965. + au_set_dbbot(dentry, bindex);
  8966. + return;
  8967. + }
  8968. + au_set_h_dptr(dentry, bindex, NULL);
  8969. + }
  8970. +}
  8971. +
  8972. +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
  8973. +{
  8974. + aufs_bindex_t bindex, bbot;
  8975. +
  8976. + bbot = au_dbbot(dentry);
  8977. + for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
  8978. + if (au_h_dptr(dentry, bindex) == h_dentry)
  8979. + return bindex;
  8980. + return -1;
  8981. +}
  8982. diff -Naur null/fs/aufs/dir.c linux-5.1/fs/aufs/dir.c
  8983. --- /dev/null
  8984. +++ linux-5.1/fs/aufs/dir.c 2019-05-22 11:23:07.439141088 +0200
  8985. @@ -0,0 +1,763 @@
  8986. +// SPDX-License-Identifier: GPL-2.0
  8987. +/*
  8988. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  8989. + *
  8990. + * This program, aufs is free software; you can redistribute it and/or modify
  8991. + * it under the terms of the GNU General Public License as published by
  8992. + * the Free Software Foundation; either version 2 of the License, or
  8993. + * (at your option) any later version.
  8994. + *
  8995. + * This program is distributed in the hope that it will be useful,
  8996. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  8997. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  8998. + * GNU General Public License for more details.
  8999. + *
  9000. + * You should have received a copy of the GNU General Public License
  9001. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  9002. + */
  9003. +
  9004. +/*
  9005. + * directory operations
  9006. + */
  9007. +
  9008. +#include <linux/fs_stack.h>
  9009. +#include <linux/iversion.h>
  9010. +#include "aufs.h"
  9011. +
  9012. +void au_add_nlink(struct inode *dir, struct inode *h_dir)
  9013. +{
  9014. + unsigned int nlink;
  9015. +
  9016. + AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
  9017. +
  9018. + nlink = dir->i_nlink;
  9019. + nlink += h_dir->i_nlink - 2;
  9020. + if (h_dir->i_nlink < 2)
  9021. + nlink += 2;
  9022. + smp_mb(); /* for i_nlink */
  9023. + /* 0 can happen in revaliding */
  9024. + set_nlink(dir, nlink);
  9025. +}
  9026. +
  9027. +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
  9028. +{
  9029. + unsigned int nlink;
  9030. +
  9031. + AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
  9032. +
  9033. + nlink = dir->i_nlink;
  9034. + nlink -= h_dir->i_nlink - 2;
  9035. + if (h_dir->i_nlink < 2)
  9036. + nlink -= 2;
  9037. + smp_mb(); /* for i_nlink */
  9038. + /* nlink == 0 means the branch-fs is broken */
  9039. + set_nlink(dir, nlink);
  9040. +}
  9041. +
  9042. +loff_t au_dir_size(struct file *file, struct dentry *dentry)
  9043. +{
  9044. + loff_t sz;
  9045. + aufs_bindex_t bindex, bbot;
  9046. + struct file *h_file;
  9047. + struct dentry *h_dentry;
  9048. +
  9049. + sz = 0;
  9050. + if (file) {
  9051. + AuDebugOn(!d_is_dir(file->f_path.dentry));
  9052. +
  9053. + bbot = au_fbbot_dir(file);
  9054. + for (bindex = au_fbtop(file);
  9055. + bindex <= bbot && sz < KMALLOC_MAX_SIZE;
  9056. + bindex++) {
  9057. + h_file = au_hf_dir(file, bindex);
  9058. + if (h_file && file_inode(h_file))
  9059. + sz += vfsub_f_size_read(h_file);
  9060. + }
  9061. + } else {
  9062. + AuDebugOn(!dentry);
  9063. + AuDebugOn(!d_is_dir(dentry));
  9064. +
  9065. + bbot = au_dbtaildir(dentry);
  9066. + for (bindex = au_dbtop(dentry);
  9067. + bindex <= bbot && sz < KMALLOC_MAX_SIZE;
  9068. + bindex++) {
  9069. + h_dentry = au_h_dptr(dentry, bindex);
  9070. + if (h_dentry && d_is_positive(h_dentry))
  9071. + sz += i_size_read(d_inode(h_dentry));
  9072. + }
  9073. + }
  9074. + if (sz < KMALLOC_MAX_SIZE)
  9075. + sz = roundup_pow_of_two(sz);
  9076. + if (sz > KMALLOC_MAX_SIZE)
  9077. + sz = KMALLOC_MAX_SIZE;
  9078. + else if (sz < NAME_MAX) {
  9079. + BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
  9080. + sz = AUFS_RDBLK_DEF;
  9081. + }
  9082. + return sz;
  9083. +}
  9084. +
  9085. +struct au_dir_ts_arg {
  9086. + struct dentry *dentry;
  9087. + aufs_bindex_t brid;
  9088. +};
  9089. +
  9090. +static void au_do_dir_ts(void *arg)
  9091. +{
  9092. + struct au_dir_ts_arg *a = arg;
  9093. + struct au_dtime dt;
  9094. + struct path h_path;
  9095. + struct inode *dir, *h_dir;
  9096. + struct super_block *sb;
  9097. + struct au_branch *br;
  9098. + struct au_hinode *hdir;
  9099. + int err;
  9100. + aufs_bindex_t btop, bindex;
  9101. +
  9102. + sb = a->dentry->d_sb;
  9103. + if (d_really_is_negative(a->dentry))
  9104. + goto out;
  9105. + /* no dir->i_mutex lock */
  9106. + aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
  9107. +
  9108. + dir = d_inode(a->dentry);
  9109. + btop = au_ibtop(dir);
  9110. + bindex = au_br_index(sb, a->brid);
  9111. + if (bindex < btop)
  9112. + goto out_unlock;
  9113. +
  9114. + br = au_sbr(sb, bindex);
  9115. + h_path.dentry = au_h_dptr(a->dentry, bindex);
  9116. + if (!h_path.dentry)
  9117. + goto out_unlock;
  9118. + h_path.mnt = au_br_mnt(br);
  9119. + au_dtime_store(&dt, a->dentry, &h_path);
  9120. +
  9121. + br = au_sbr(sb, btop);
  9122. + if (!au_br_writable(br->br_perm))
  9123. + goto out_unlock;
  9124. + h_path.dentry = au_h_dptr(a->dentry, btop);
  9125. + h_path.mnt = au_br_mnt(br);
  9126. + err = vfsub_mnt_want_write(h_path.mnt);
  9127. + if (err)
  9128. + goto out_unlock;
  9129. + hdir = au_hi(dir, btop);
  9130. + au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
  9131. + h_dir = au_h_iptr(dir, btop);
  9132. + if (h_dir->i_nlink
  9133. + && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
  9134. + dt.dt_h_path = h_path;
  9135. + au_dtime_revert(&dt);
  9136. + }
  9137. + au_hn_inode_unlock(hdir);
  9138. + vfsub_mnt_drop_write(h_path.mnt);
  9139. + au_cpup_attr_timesizes(dir);
  9140. +
  9141. +out_unlock:
  9142. + aufs_read_unlock(a->dentry, AuLock_DW);
  9143. +out:
  9144. + dput(a->dentry);
  9145. + au_nwt_done(&au_sbi(sb)->si_nowait);
  9146. + au_kfree_try_rcu(arg);
  9147. +}
  9148. +
  9149. +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
  9150. +{
  9151. + int perm, wkq_err;
  9152. + aufs_bindex_t btop;
  9153. + struct au_dir_ts_arg *arg;
  9154. + struct dentry *dentry;
  9155. + struct super_block *sb;
  9156. +
  9157. + IMustLock(dir);
  9158. +
  9159. + dentry = d_find_any_alias(dir);
  9160. + AuDebugOn(!dentry);
  9161. + sb = dentry->d_sb;
  9162. + btop = au_ibtop(dir);
  9163. + if (btop == bindex) {
  9164. + au_cpup_attr_timesizes(dir);
  9165. + goto out;
  9166. + }
  9167. +
  9168. + perm = au_sbr_perm(sb, btop);
  9169. + if (!au_br_writable(perm))
  9170. + goto out;
  9171. +
  9172. + arg = kmalloc(sizeof(*arg), GFP_NOFS);
  9173. + if (!arg)
  9174. + goto out;
  9175. +
  9176. + arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
  9177. + arg->brid = au_sbr_id(sb, bindex);
  9178. + wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
  9179. + if (unlikely(wkq_err)) {
  9180. + pr_err("wkq %d\n", wkq_err);
  9181. + dput(dentry);
  9182. + au_kfree_try_rcu(arg);
  9183. + }
  9184. +
  9185. +out:
  9186. + dput(dentry);
  9187. +}
  9188. +
  9189. +/* ---------------------------------------------------------------------- */
  9190. +
  9191. +static int reopen_dir(struct file *file)
  9192. +{
  9193. + int err;
  9194. + unsigned int flags;
  9195. + aufs_bindex_t bindex, btail, btop;
  9196. + struct dentry *dentry, *h_dentry;
  9197. + struct file *h_file;
  9198. +
  9199. + /* open all lower dirs */
  9200. + dentry = file->f_path.dentry;
  9201. + btop = au_dbtop(dentry);
  9202. + for (bindex = au_fbtop(file); bindex < btop; bindex++)
  9203. + au_set_h_fptr(file, bindex, NULL);
  9204. + au_set_fbtop(file, btop);
  9205. +
  9206. + btail = au_dbtaildir(dentry);
  9207. + for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
  9208. + au_set_h_fptr(file, bindex, NULL);
  9209. + au_set_fbbot_dir(file, btail);
  9210. +
  9211. + flags = vfsub_file_flags(file);
  9212. + for (bindex = btop; bindex <= btail; bindex++) {
  9213. + h_dentry = au_h_dptr(dentry, bindex);
  9214. + if (!h_dentry)
  9215. + continue;
  9216. + h_file = au_hf_dir(file, bindex);
  9217. + if (h_file)
  9218. + continue;
  9219. +
  9220. + h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
  9221. + err = PTR_ERR(h_file);
  9222. + if (IS_ERR(h_file))
  9223. + goto out; /* close all? */
  9224. + au_set_h_fptr(file, bindex, h_file);
  9225. + }
  9226. + au_update_figen(file);
  9227. + /* todo: necessary? */
  9228. + /* file->f_ra = h_file->f_ra; */
  9229. + err = 0;
  9230. +
  9231. +out:
  9232. + return err;
  9233. +}
  9234. +
  9235. +static int do_open_dir(struct file *file, int flags, struct file *h_file)
  9236. +{
  9237. + int err;
  9238. + aufs_bindex_t bindex, btail;
  9239. + struct dentry *dentry, *h_dentry;
  9240. + struct vfsmount *mnt;
  9241. +
  9242. + FiMustWriteLock(file);
  9243. + AuDebugOn(h_file);
  9244. +
  9245. + err = 0;
  9246. + mnt = file->f_path.mnt;
  9247. + dentry = file->f_path.dentry;
  9248. + file->f_version = inode_query_iversion(d_inode(dentry));
  9249. + bindex = au_dbtop(dentry);
  9250. + au_set_fbtop(file, bindex);
  9251. + btail = au_dbtaildir(dentry);
  9252. + au_set_fbbot_dir(file, btail);
  9253. + for (; !err && bindex <= btail; bindex++) {
  9254. + h_dentry = au_h_dptr(dentry, bindex);
  9255. + if (!h_dentry)
  9256. + continue;
  9257. +
  9258. + err = vfsub_test_mntns(mnt, h_dentry->d_sb);
  9259. + if (unlikely(err))
  9260. + break;
  9261. + h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
  9262. + if (IS_ERR(h_file)) {
  9263. + err = PTR_ERR(h_file);
  9264. + break;
  9265. + }
  9266. + au_set_h_fptr(file, bindex, h_file);
  9267. + }
  9268. + au_update_figen(file);
  9269. + /* todo: necessary? */
  9270. + /* file->f_ra = h_file->f_ra; */
  9271. + if (!err)
  9272. + return 0; /* success */
  9273. +
  9274. + /* close all */
  9275. + for (bindex = au_fbtop(file); bindex <= btail; bindex++)
  9276. + au_set_h_fptr(file, bindex, NULL);
  9277. + au_set_fbtop(file, -1);
  9278. + au_set_fbbot_dir(file, -1);
  9279. +
  9280. + return err;
  9281. +}
  9282. +
  9283. +static int aufs_open_dir(struct inode *inode __maybe_unused,
  9284. + struct file *file)
  9285. +{
  9286. + int err;
  9287. + struct super_block *sb;
  9288. + struct au_fidir *fidir;
  9289. +
  9290. + err = -ENOMEM;
  9291. + sb = file->f_path.dentry->d_sb;
  9292. + si_read_lock(sb, AuLock_FLUSH);
  9293. + fidir = au_fidir_alloc(sb);
  9294. + if (fidir) {
  9295. + struct au_do_open_args args = {
  9296. + .open = do_open_dir,
  9297. + .fidir = fidir
  9298. + };
  9299. + err = au_do_open(file, &args);
  9300. + if (unlikely(err))
  9301. + au_kfree_rcu(fidir);
  9302. + }
  9303. + si_read_unlock(sb);
  9304. + return err;
  9305. +}
  9306. +
  9307. +static int aufs_release_dir(struct inode *inode __maybe_unused,
  9308. + struct file *file)
  9309. +{
  9310. + struct au_vdir *vdir_cache;
  9311. + struct au_finfo *finfo;
  9312. + struct au_fidir *fidir;
  9313. + struct au_hfile *hf;
  9314. + aufs_bindex_t bindex, bbot;
  9315. +
  9316. + finfo = au_fi(file);
  9317. + fidir = finfo->fi_hdir;
  9318. + if (fidir) {
  9319. + au_hbl_del(&finfo->fi_hlist,
  9320. + &au_sbi(file->f_path.dentry->d_sb)->si_files);
  9321. + vdir_cache = fidir->fd_vdir_cache; /* lock-free */
  9322. + if (vdir_cache)
  9323. + au_vdir_free(vdir_cache);
  9324. +
  9325. + bindex = finfo->fi_btop;
  9326. + if (bindex >= 0) {
  9327. + hf = fidir->fd_hfile + bindex;
  9328. + /*
  9329. + * calls fput() instead of filp_close(),
  9330. + * since no dnotify or lock for the lower file.
  9331. + */
  9332. + bbot = fidir->fd_bbot;
  9333. + for (; bindex <= bbot; bindex++, hf++)
  9334. + if (hf->hf_file)
  9335. + au_hfput(hf, /*execed*/0);
  9336. + }
  9337. + au_kfree_rcu(fidir);
  9338. + finfo->fi_hdir = NULL;
  9339. + }
  9340. + au_finfo_fin(file);
  9341. + return 0;
  9342. +}
  9343. +
  9344. +/* ---------------------------------------------------------------------- */
  9345. +
  9346. +static int au_do_flush_dir(struct file *file, fl_owner_t id)
  9347. +{
  9348. + int err;
  9349. + aufs_bindex_t bindex, bbot;
  9350. + struct file *h_file;
  9351. +
  9352. + err = 0;
  9353. + bbot = au_fbbot_dir(file);
  9354. + for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
  9355. + h_file = au_hf_dir(file, bindex);
  9356. + if (h_file)
  9357. + err = vfsub_flush(h_file, id);
  9358. + }
  9359. + return err;
  9360. +}
  9361. +
  9362. +static int aufs_flush_dir(struct file *file, fl_owner_t id)
  9363. +{
  9364. + return au_do_flush(file, id, au_do_flush_dir);
  9365. +}
  9366. +
  9367. +/* ---------------------------------------------------------------------- */
  9368. +
  9369. +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
  9370. +{
  9371. + int err;
  9372. + aufs_bindex_t bbot, bindex;
  9373. + struct inode *inode;
  9374. + struct super_block *sb;
  9375. +
  9376. + err = 0;
  9377. + sb = dentry->d_sb;
  9378. + inode = d_inode(dentry);
  9379. + IMustLock(inode);
  9380. + bbot = au_dbbot(dentry);
  9381. + for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
  9382. + struct path h_path;
  9383. +
  9384. + if (au_test_ro(sb, bindex, inode))
  9385. + continue;
  9386. + h_path.dentry = au_h_dptr(dentry, bindex);
  9387. + if (!h_path.dentry)
  9388. + continue;
  9389. +
  9390. + h_path.mnt = au_sbr_mnt(sb, bindex);
  9391. + err = vfsub_fsync(NULL, &h_path, datasync);
  9392. + }
  9393. +
  9394. + return err;
  9395. +}
  9396. +
  9397. +static int au_do_fsync_dir(struct file *file, int datasync)
  9398. +{
  9399. + int err;
  9400. + aufs_bindex_t bbot, bindex;
  9401. + struct file *h_file;
  9402. + struct super_block *sb;
  9403. + struct inode *inode;
  9404. +
  9405. + err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
  9406. + if (unlikely(err))
  9407. + goto out;
  9408. +
  9409. + inode = file_inode(file);
  9410. + sb = inode->i_sb;
  9411. + bbot = au_fbbot_dir(file);
  9412. + for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
  9413. + h_file = au_hf_dir(file, bindex);
  9414. + if (!h_file || au_test_ro(sb, bindex, inode))
  9415. + continue;
  9416. +
  9417. + err = vfsub_fsync(h_file, &h_file->f_path, datasync);
  9418. + }
  9419. +
  9420. +out:
  9421. + return err;
  9422. +}
  9423. +
  9424. +/*
  9425. + * @file may be NULL
  9426. + */
  9427. +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
  9428. + int datasync)
  9429. +{
  9430. + int err;
  9431. + struct dentry *dentry;
  9432. + struct inode *inode;
  9433. + struct super_block *sb;
  9434. +
  9435. + err = 0;
  9436. + dentry = file->f_path.dentry;
  9437. + inode = d_inode(dentry);
  9438. + inode_lock(inode);
  9439. + sb = dentry->d_sb;
  9440. + si_noflush_read_lock(sb);
  9441. + if (file)
  9442. + err = au_do_fsync_dir(file, datasync);
  9443. + else {
  9444. + di_write_lock_child(dentry);
  9445. + err = au_do_fsync_dir_no_file(dentry, datasync);
  9446. + }
  9447. + au_cpup_attr_timesizes(inode);
  9448. + di_write_unlock(dentry);
  9449. + if (file)
  9450. + fi_write_unlock(file);
  9451. +
  9452. + si_read_unlock(sb);
  9453. + inode_unlock(inode);
  9454. + return err;
  9455. +}
  9456. +
  9457. +/* ---------------------------------------------------------------------- */
  9458. +
  9459. +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
  9460. +{
  9461. + int err;
  9462. + struct dentry *dentry;
  9463. + struct inode *inode, *h_inode;
  9464. + struct super_block *sb;
  9465. +
  9466. + AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
  9467. +
  9468. + dentry = file->f_path.dentry;
  9469. + inode = d_inode(dentry);
  9470. + IMustLock(inode);
  9471. +
  9472. + sb = dentry->d_sb;
  9473. + si_read_lock(sb, AuLock_FLUSH);
  9474. + err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
  9475. + if (unlikely(err))
  9476. + goto out;
  9477. + err = au_alive_dir(dentry);
  9478. + if (!err)
  9479. + err = au_vdir_init(file);
  9480. + di_downgrade_lock(dentry, AuLock_IR);
  9481. + if (unlikely(err))
  9482. + goto out_unlock;
  9483. +
  9484. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  9485. + if (!au_test_nfsd()) {
  9486. + err = au_vdir_fill_de(file, ctx);
  9487. + fsstack_copy_attr_atime(inode, h_inode);
  9488. + } else {
  9489. + /*
  9490. + * nfsd filldir may call lookup_one_len(), vfs_getattr(),
  9491. + * encode_fh() and others.
  9492. + */
  9493. + atomic_inc(&h_inode->i_count);
  9494. + di_read_unlock(dentry, AuLock_IR);
  9495. + si_read_unlock(sb);
  9496. + err = au_vdir_fill_de(file, ctx);
  9497. + fsstack_copy_attr_atime(inode, h_inode);
  9498. + fi_write_unlock(file);
  9499. + iput(h_inode);
  9500. +
  9501. + AuTraceErr(err);
  9502. + return err;
  9503. + }
  9504. +
  9505. +out_unlock:
  9506. + di_read_unlock(dentry, AuLock_IR);
  9507. + fi_write_unlock(file);
  9508. +out:
  9509. + si_read_unlock(sb);
  9510. + return err;
  9511. +}
  9512. +
  9513. +/* ---------------------------------------------------------------------- */
  9514. +
  9515. +#define AuTestEmpty_WHONLY 1
  9516. +#define AuTestEmpty_CALLED (1 << 1)
  9517. +#define AuTestEmpty_SHWH (1 << 2)
  9518. +#define au_ftest_testempty(flags, name) ((flags) & AuTestEmpty_##name)
  9519. +#define au_fset_testempty(flags, name) \
  9520. + do { (flags) |= AuTestEmpty_##name; } while (0)
  9521. +#define au_fclr_testempty(flags, name) \
  9522. + do { (flags) &= ~AuTestEmpty_##name; } while (0)
  9523. +
  9524. +#ifndef CONFIG_AUFS_SHWH
  9525. +#undef AuTestEmpty_SHWH
  9526. +#define AuTestEmpty_SHWH 0
  9527. +#endif
  9528. +
  9529. +struct test_empty_arg {
  9530. + struct dir_context ctx;
  9531. + struct au_nhash *whlist;
  9532. + unsigned int flags;
  9533. + int err;
  9534. + aufs_bindex_t bindex;
  9535. +};
  9536. +
  9537. +static int test_empty_cb(struct dir_context *ctx, const char *__name,
  9538. + int namelen, loff_t offset __maybe_unused, u64 ino,
  9539. + unsigned int d_type)
  9540. +{
  9541. + struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
  9542. + ctx);
  9543. + char *name = (void *)__name;
  9544. +
  9545. + arg->err = 0;
  9546. + au_fset_testempty(arg->flags, CALLED);
  9547. + /* smp_mb(); */
  9548. + if (name[0] == '.'
  9549. + && (namelen == 1 || (name[1] == '.' && namelen == 2)))
  9550. + goto out; /* success */
  9551. +
  9552. + if (namelen <= AUFS_WH_PFX_LEN
  9553. + || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
  9554. + if (au_ftest_testempty(arg->flags, WHONLY)
  9555. + && !au_nhash_test_known_wh(arg->whlist, name, namelen))
  9556. + arg->err = -ENOTEMPTY;
  9557. + goto out;
  9558. + }
  9559. +
  9560. + name += AUFS_WH_PFX_LEN;
  9561. + namelen -= AUFS_WH_PFX_LEN;
  9562. + if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
  9563. + arg->err = au_nhash_append_wh
  9564. + (arg->whlist, name, namelen, ino, d_type, arg->bindex,
  9565. + au_ftest_testempty(arg->flags, SHWH));
  9566. +
  9567. +out:
  9568. + /* smp_mb(); */
  9569. + AuTraceErr(arg->err);
  9570. + return arg->err;
  9571. +}
  9572. +
  9573. +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
  9574. +{
  9575. + int err;
  9576. + struct file *h_file;
  9577. + struct au_branch *br;
  9578. +
  9579. + h_file = au_h_open(dentry, arg->bindex,
  9580. + O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
  9581. + /*file*/NULL, /*force_wr*/0);
  9582. + err = PTR_ERR(h_file);
  9583. + if (IS_ERR(h_file))
  9584. + goto out;
  9585. +
  9586. + err = 0;
  9587. + if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
  9588. + && !file_inode(h_file)->i_nlink)
  9589. + goto out_put;
  9590. +
  9591. + do {
  9592. + arg->err = 0;
  9593. + au_fclr_testempty(arg->flags, CALLED);
  9594. + /* smp_mb(); */
  9595. + err = vfsub_iterate_dir(h_file, &arg->ctx);
  9596. + if (err >= 0)
  9597. + err = arg->err;
  9598. + } while (!err && au_ftest_testempty(arg->flags, CALLED));
  9599. +
  9600. +out_put:
  9601. + fput(h_file);
  9602. + br = au_sbr(dentry->d_sb, arg->bindex);
  9603. + au_lcnt_dec(&br->br_nfiles);
  9604. +out:
  9605. + return err;
  9606. +}
  9607. +
  9608. +struct do_test_empty_args {
  9609. + int *errp;
  9610. + struct dentry *dentry;
  9611. + struct test_empty_arg *arg;
  9612. +};
  9613. +
  9614. +static void call_do_test_empty(void *args)
  9615. +{
  9616. + struct do_test_empty_args *a = args;
  9617. + *a->errp = do_test_empty(a->dentry, a->arg);
  9618. +}
  9619. +
  9620. +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
  9621. +{
  9622. + int err, wkq_err;
  9623. + struct dentry *h_dentry;
  9624. + struct inode *h_inode;
  9625. +
  9626. + h_dentry = au_h_dptr(dentry, arg->bindex);
  9627. + h_inode = d_inode(h_dentry);
  9628. + /* todo: i_mode changes anytime? */
  9629. + inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
  9630. + err = au_test_h_perm_sio(h_inode, MAY_EXEC | MAY_READ);
  9631. + inode_unlock_shared(h_inode);
  9632. + if (!err)
  9633. + err = do_test_empty(dentry, arg);
  9634. + else {
  9635. + struct do_test_empty_args args = {
  9636. + .errp = &err,
  9637. + .dentry = dentry,
  9638. + .arg = arg
  9639. + };
  9640. + unsigned int flags = arg->flags;
  9641. +
  9642. + wkq_err = au_wkq_wait(call_do_test_empty, &args);
  9643. + if (unlikely(wkq_err))
  9644. + err = wkq_err;
  9645. + arg->flags = flags;
  9646. + }
  9647. +
  9648. + return err;
  9649. +}
  9650. +
  9651. +int au_test_empty_lower(struct dentry *dentry)
  9652. +{
  9653. + int err;
  9654. + unsigned int rdhash;
  9655. + aufs_bindex_t bindex, btop, btail;
  9656. + struct au_nhash whlist;
  9657. + struct test_empty_arg arg = {
  9658. + .ctx = {
  9659. + .actor = test_empty_cb
  9660. + }
  9661. + };
  9662. + int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
  9663. +
  9664. + SiMustAnyLock(dentry->d_sb);
  9665. +
  9666. + rdhash = au_sbi(dentry->d_sb)->si_rdhash;
  9667. + if (!rdhash)
  9668. + rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
  9669. + err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
  9670. + if (unlikely(err))
  9671. + goto out;
  9672. +
  9673. + arg.flags = 0;
  9674. + arg.whlist = &whlist;
  9675. + btop = au_dbtop(dentry);
  9676. + if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
  9677. + au_fset_testempty(arg.flags, SHWH);
  9678. + test_empty = do_test_empty;
  9679. + if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
  9680. + test_empty = sio_test_empty;
  9681. + arg.bindex = btop;
  9682. + err = test_empty(dentry, &arg);
  9683. + if (unlikely(err))
  9684. + goto out_whlist;
  9685. +
  9686. + au_fset_testempty(arg.flags, WHONLY);
  9687. + btail = au_dbtaildir(dentry);
  9688. + for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
  9689. + struct dentry *h_dentry;
  9690. +
  9691. + h_dentry = au_h_dptr(dentry, bindex);
  9692. + if (h_dentry && d_is_positive(h_dentry)) {
  9693. + arg.bindex = bindex;
  9694. + err = test_empty(dentry, &arg);
  9695. + }
  9696. + }
  9697. +
  9698. +out_whlist:
  9699. + au_nhash_wh_free(&whlist);
  9700. +out:
  9701. + return err;
  9702. +}
  9703. +
  9704. +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
  9705. +{
  9706. + int err;
  9707. + struct test_empty_arg arg = {
  9708. + .ctx = {
  9709. + .actor = test_empty_cb
  9710. + }
  9711. + };
  9712. + aufs_bindex_t bindex, btail;
  9713. +
  9714. + err = 0;
  9715. + arg.whlist = whlist;
  9716. + arg.flags = AuTestEmpty_WHONLY;
  9717. + if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
  9718. + au_fset_testempty(arg.flags, SHWH);
  9719. + btail = au_dbtaildir(dentry);
  9720. + for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
  9721. + struct dentry *h_dentry;
  9722. +
  9723. + h_dentry = au_h_dptr(dentry, bindex);
  9724. + if (h_dentry && d_is_positive(h_dentry)) {
  9725. + arg.bindex = bindex;
  9726. + err = sio_test_empty(dentry, &arg);
  9727. + }
  9728. + }
  9729. +
  9730. + return err;
  9731. +}
  9732. +
  9733. +/* ---------------------------------------------------------------------- */
  9734. +
  9735. +const struct file_operations aufs_dir_fop = {
  9736. + .owner = THIS_MODULE,
  9737. + .llseek = default_llseek,
  9738. + .read = generic_read_dir,
  9739. + .iterate_shared = aufs_iterate_shared,
  9740. + .unlocked_ioctl = aufs_ioctl_dir,
  9741. +#ifdef CONFIG_COMPAT
  9742. + .compat_ioctl = aufs_compat_ioctl_dir,
  9743. +#endif
  9744. + .open = aufs_open_dir,
  9745. + .release = aufs_release_dir,
  9746. + .flush = aufs_flush_dir,
  9747. + .fsync = aufs_fsync_dir
  9748. +};
  9749. diff -Naur null/fs/aufs/dir.h linux-5.1/fs/aufs/dir.h
  9750. --- /dev/null
  9751. +++ linux-5.1/fs/aufs/dir.h 2019-05-22 11:23:07.439141088 +0200
  9752. @@ -0,0 +1,134 @@
  9753. +/* SPDX-License-Identifier: GPL-2.0 */
  9754. +/*
  9755. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  9756. + *
  9757. + * This program, aufs is free software; you can redistribute it and/or modify
  9758. + * it under the terms of the GNU General Public License as published by
  9759. + * the Free Software Foundation; either version 2 of the License, or
  9760. + * (at your option) any later version.
  9761. + *
  9762. + * This program is distributed in the hope that it will be useful,
  9763. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9764. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9765. + * GNU General Public License for more details.
  9766. + *
  9767. + * You should have received a copy of the GNU General Public License
  9768. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  9769. + */
  9770. +
  9771. +/*
  9772. + * directory operations
  9773. + */
  9774. +
  9775. +#ifndef __AUFS_DIR_H__
  9776. +#define __AUFS_DIR_H__
  9777. +
  9778. +#ifdef __KERNEL__
  9779. +
  9780. +#include <linux/fs.h>
  9781. +
  9782. +/* ---------------------------------------------------------------------- */
  9783. +
  9784. +/* need to be faster and smaller */
  9785. +
  9786. +struct au_nhash {
  9787. + unsigned int nh_num;
  9788. + struct hlist_head *nh_head;
  9789. +};
  9790. +
  9791. +struct au_vdir_destr {
  9792. + unsigned char len;
  9793. + unsigned char name[0];
  9794. +} __packed;
  9795. +
  9796. +struct au_vdir_dehstr {
  9797. + struct hlist_node hash;
  9798. + struct au_vdir_destr *str;
  9799. + struct rcu_head rcu;
  9800. +} ____cacheline_aligned_in_smp;
  9801. +
  9802. +struct au_vdir_de {
  9803. + ino_t de_ino;
  9804. + unsigned char de_type;
  9805. + /* caution: packed */
  9806. + struct au_vdir_destr de_str;
  9807. +} __packed;
  9808. +
  9809. +struct au_vdir_wh {
  9810. + struct hlist_node wh_hash;
  9811. +#ifdef CONFIG_AUFS_SHWH
  9812. + ino_t wh_ino;
  9813. + aufs_bindex_t wh_bindex;
  9814. + unsigned char wh_type;
  9815. +#else
  9816. + aufs_bindex_t wh_bindex;
  9817. +#endif
  9818. + /* caution: packed */
  9819. + struct au_vdir_destr wh_str;
  9820. +} __packed;
  9821. +
  9822. +union au_vdir_deblk_p {
  9823. + unsigned char *deblk;
  9824. + struct au_vdir_de *de;
  9825. +};
  9826. +
  9827. +struct au_vdir {
  9828. + unsigned char **vd_deblk;
  9829. + unsigned long vd_nblk;
  9830. + struct {
  9831. + unsigned long ul;
  9832. + union au_vdir_deblk_p p;
  9833. + } vd_last;
  9834. +
  9835. + u64 vd_version;
  9836. + unsigned int vd_deblk_sz;
  9837. + unsigned long vd_jiffy;
  9838. + struct rcu_head rcu;
  9839. +} ____cacheline_aligned_in_smp;
  9840. +
  9841. +/* ---------------------------------------------------------------------- */
  9842. +
  9843. +/* dir.c */
  9844. +extern const struct file_operations aufs_dir_fop;
  9845. +void au_add_nlink(struct inode *dir, struct inode *h_dir);
  9846. +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
  9847. +loff_t au_dir_size(struct file *file, struct dentry *dentry);
  9848. +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
  9849. +int au_test_empty_lower(struct dentry *dentry);
  9850. +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
  9851. +
  9852. +/* vdir.c */
  9853. +unsigned int au_rdhash_est(loff_t sz);
  9854. +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
  9855. +void au_nhash_wh_free(struct au_nhash *whlist);
  9856. +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
  9857. + int limit);
  9858. +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
  9859. +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
  9860. + unsigned int d_type, aufs_bindex_t bindex,
  9861. + unsigned char shwh);
  9862. +void au_vdir_free(struct au_vdir *vdir);
  9863. +int au_vdir_init(struct file *file);
  9864. +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
  9865. +
  9866. +/* ioctl.c */
  9867. +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
  9868. +
  9869. +#ifdef CONFIG_AUFS_RDU
  9870. +/* rdu.c */
  9871. +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  9872. +#ifdef CONFIG_COMPAT
  9873. +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
  9874. + unsigned long arg);
  9875. +#endif
  9876. +#else
  9877. +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
  9878. + unsigned int cmd, unsigned long arg)
  9879. +#ifdef CONFIG_COMPAT
  9880. +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
  9881. + unsigned int cmd, unsigned long arg)
  9882. +#endif
  9883. +#endif
  9884. +
  9885. +#endif /* __KERNEL__ */
  9886. +#endif /* __AUFS_DIR_H__ */
  9887. diff -Naur null/fs/aufs/dirren.c linux-5.1/fs/aufs/dirren.c
  9888. --- /dev/null
  9889. +++ linux-5.1/fs/aufs/dirren.c 2019-05-22 11:23:07.439141088 +0200
  9890. @@ -0,0 +1,1316 @@
  9891. +// SPDX-License-Identifier: GPL-2.0
  9892. +/*
  9893. + * Copyright (C) 2017-2019 Junjiro R. Okajima
  9894. + *
  9895. + * This program, aufs is free software; you can redistribute it and/or modify
  9896. + * it under the terms of the GNU General Public License as published by
  9897. + * the Free Software Foundation; either version 2 of the License, or
  9898. + * (at your option) any later version.
  9899. + *
  9900. + * This program is distributed in the hope that it will be useful,
  9901. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9902. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9903. + * GNU General Public License for more details.
  9904. + *
  9905. + * You should have received a copy of the GNU General Public License
  9906. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  9907. + */
  9908. +
  9909. +/*
  9910. + * special handling in renaming a directory
  9911. + * in order to support looking-up the before-renamed name on the lower readonly
  9912. + * branches
  9913. + */
  9914. +
  9915. +#include <linux/byteorder/generic.h>
  9916. +#include "aufs.h"
  9917. +
  9918. +static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
  9919. +{
  9920. + int idx;
  9921. +
  9922. + idx = au_dr_ihash(ent->dr_h_ino);
  9923. + au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
  9924. +}
  9925. +
  9926. +static int au_dr_hino_test_empty(struct au_dr_br *dr)
  9927. +{
  9928. + int ret, i;
  9929. + struct hlist_bl_head *hbl;
  9930. +
  9931. + ret = 1;
  9932. + for (i = 0; ret && i < AuDirren_NHASH; i++) {
  9933. + hbl = dr->dr_h_ino + i;
  9934. + hlist_bl_lock(hbl);
  9935. + ret &= hlist_bl_empty(hbl);
  9936. + hlist_bl_unlock(hbl);
  9937. + }
  9938. +
  9939. + return ret;
  9940. +}
  9941. +
  9942. +static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
  9943. +{
  9944. + struct au_dr_hino *found, *ent;
  9945. + struct hlist_bl_head *hbl;
  9946. + struct hlist_bl_node *pos;
  9947. + int idx;
  9948. +
  9949. + found = NULL;
  9950. + idx = au_dr_ihash(ino);
  9951. + hbl = dr->dr_h_ino + idx;
  9952. + hlist_bl_lock(hbl);
  9953. + hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
  9954. + if (ent->dr_h_ino == ino) {
  9955. + found = ent;
  9956. + break;
  9957. + }
  9958. + hlist_bl_unlock(hbl);
  9959. +
  9960. + return found;
  9961. +}
  9962. +
  9963. +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
  9964. + struct au_dr_hino *add_ent)
  9965. +{
  9966. + int found, idx;
  9967. + struct hlist_bl_head *hbl;
  9968. + struct hlist_bl_node *pos;
  9969. + struct au_dr_hino *ent;
  9970. +
  9971. + found = 0;
  9972. + idx = au_dr_ihash(ino);
  9973. + hbl = dr->dr_h_ino + idx;
  9974. +#if 0
  9975. + {
  9976. + struct hlist_bl_node *tmp;
  9977. +
  9978. + hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
  9979. + AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
  9980. + }
  9981. +#endif
  9982. + hlist_bl_lock(hbl);
  9983. + hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
  9984. + if (ent->dr_h_ino == ino) {
  9985. + found = 1;
  9986. + break;
  9987. + }
  9988. + if (!found && add_ent)
  9989. + hlist_bl_add_head(&add_ent->dr_hnode, hbl);
  9990. + hlist_bl_unlock(hbl);
  9991. +
  9992. + if (!found && add_ent)
  9993. + AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
  9994. +
  9995. + return found;
  9996. +}
  9997. +
  9998. +void au_dr_hino_free(struct au_dr_br *dr)
  9999. +{
  10000. + int i;
  10001. + struct hlist_bl_head *hbl;
  10002. + struct hlist_bl_node *pos, *tmp;
  10003. + struct au_dr_hino *ent;
  10004. +
  10005. + /* SiMustWriteLock(sb); */
  10006. +
  10007. + for (i = 0; i < AuDirren_NHASH; i++) {
  10008. + hbl = dr->dr_h_ino + i;
  10009. + /* no spinlock since sbinfo must be write-locked */
  10010. + hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
  10011. + au_kfree_rcu(ent);
  10012. + INIT_HLIST_BL_HEAD(hbl);
  10013. + }
  10014. +}
  10015. +
  10016. +/* returns the number of inodes or an error */
  10017. +static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
  10018. + struct file *hinofile)
  10019. +{
  10020. + int err, i;
  10021. + ssize_t ssz;
  10022. + loff_t pos, oldsize;
  10023. + __be64 u64;
  10024. + struct inode *hinoinode;
  10025. + struct hlist_bl_head *hbl;
  10026. + struct hlist_bl_node *n1, *n2;
  10027. + struct au_dr_hino *ent;
  10028. +
  10029. + SiMustWriteLock(sb);
  10030. + AuDebugOn(!au_br_writable(br->br_perm));
  10031. +
  10032. + hinoinode = file_inode(hinofile);
  10033. + oldsize = i_size_read(hinoinode);
  10034. +
  10035. + err = 0;
  10036. + pos = 0;
  10037. + hbl = br->br_dirren.dr_h_ino;
  10038. + for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
  10039. + /* no bit-lock since sbinfo must be write-locked */
  10040. + hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
  10041. + AuDbg("hi%llu, %pD2\n",
  10042. + (unsigned long long)ent->dr_h_ino, hinofile);
  10043. + u64 = cpu_to_be64(ent->dr_h_ino);
  10044. + ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
  10045. + if (ssz == sizeof(u64))
  10046. + continue;
  10047. +
  10048. + /* write error */
  10049. + pr_err("ssz %zd, %pD2\n", ssz, hinofile);
  10050. + err = -ENOSPC;
  10051. + if (ssz < 0)
  10052. + err = ssz;
  10053. + break;
  10054. + }
  10055. + }
  10056. + /* regardless the error */
  10057. + if (pos < oldsize) {
  10058. + err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
  10059. + AuTraceErr(err);
  10060. + }
  10061. +
  10062. + AuTraceErr(err);
  10063. + return err;
  10064. +}
  10065. +
  10066. +static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
  10067. +{
  10068. + int err, hidx;
  10069. + ssize_t ssz;
  10070. + size_t sz, n;
  10071. + loff_t pos;
  10072. + uint64_t u64;
  10073. + struct au_dr_hino *ent;
  10074. + struct inode *hinoinode;
  10075. + struct hlist_bl_head *hbl;
  10076. +
  10077. + err = 0;
  10078. + pos = 0;
  10079. + hbl = dr->dr_h_ino;
  10080. + hinoinode = file_inode(hinofile);
  10081. + sz = i_size_read(hinoinode);
  10082. + AuDebugOn(sz % sizeof(u64));
  10083. + n = sz / sizeof(u64);
  10084. + while (n--) {
  10085. + ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
  10086. + if (unlikely(ssz != sizeof(u64))) {
  10087. + pr_err("ssz %zd, %pD2\n", ssz, hinofile);
  10088. + err = -EINVAL;
  10089. + if (ssz < 0)
  10090. + err = ssz;
  10091. + goto out_free;
  10092. + }
  10093. +
  10094. + ent = kmalloc(sizeof(*ent), GFP_NOFS);
  10095. + if (!ent) {
  10096. + err = -ENOMEM;
  10097. + AuTraceErr(err);
  10098. + goto out_free;
  10099. + }
  10100. + ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
  10101. + AuDbg("hi%llu, %pD2\n",
  10102. + (unsigned long long)ent->dr_h_ino, hinofile);
  10103. + hidx = au_dr_ihash(ent->dr_h_ino);
  10104. + au_hbl_add(&ent->dr_hnode, hbl + hidx);
  10105. + }
  10106. + goto out; /* success */
  10107. +
  10108. +out_free:
  10109. + au_dr_hino_free(dr);
  10110. +out:
  10111. + AuTraceErr(err);
  10112. + return err;
  10113. +}
  10114. +
  10115. +/*
  10116. + * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
  10117. + * @path is a switch to distinguish load and store.
  10118. + */
  10119. +static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
  10120. + struct au_branch *br, const struct path *path)
  10121. +{
  10122. + int err, flags;
  10123. + unsigned char load, suspend;
  10124. + struct file *hinofile;
  10125. + struct au_hinode *hdir;
  10126. + struct inode *dir, *delegated;
  10127. + struct path hinopath;
  10128. + struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
  10129. + sizeof(AUFS_WH_DR_BRHINO) - 1);
  10130. +
  10131. + AuDebugOn(bindex < 0 && !br);
  10132. + AuDebugOn(bindex >= 0 && br);
  10133. +
  10134. + err = -EINVAL;
  10135. + suspend = !br;
  10136. + if (suspend)
  10137. + br = au_sbr(sb, bindex);
  10138. + load = !!path;
  10139. + if (!load) {
  10140. + path = &br->br_path;
  10141. + AuDebugOn(!au_br_writable(br->br_perm));
  10142. + if (unlikely(!au_br_writable(br->br_perm)))
  10143. + goto out;
  10144. + }
  10145. +
  10146. + hdir = NULL;
  10147. + if (suspend) {
  10148. + dir = d_inode(sb->s_root);
  10149. + hdir = au_hinode(au_ii(dir), bindex);
  10150. + dir = hdir->hi_inode;
  10151. + au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
  10152. + } else {
  10153. + dir = d_inode(path->dentry);
  10154. + inode_lock_nested(dir, AuLsc_I_CHILD);
  10155. + }
  10156. + hinopath.dentry = vfsub_lkup_one(&hinoname, path->dentry);
  10157. + err = PTR_ERR(hinopath.dentry);
  10158. + if (IS_ERR(hinopath.dentry))
  10159. + goto out_unlock;
  10160. +
  10161. + err = 0;
  10162. + flags = O_RDONLY;
  10163. + if (load) {
  10164. + if (d_is_negative(hinopath.dentry))
  10165. + goto out_dput; /* success */
  10166. + } else {
  10167. + if (au_dr_hino_test_empty(&br->br_dirren)) {
  10168. + if (d_is_positive(hinopath.dentry)) {
  10169. + delegated = NULL;
  10170. + err = vfsub_unlink(dir, &hinopath, &delegated,
  10171. + /*force*/0);
  10172. + AuTraceErr(err);
  10173. + if (unlikely(err))
  10174. + pr_err("ignored err %d, %pd2\n",
  10175. + err, hinopath.dentry);
  10176. + if (unlikely(err == -EWOULDBLOCK))
  10177. + iput(delegated);
  10178. + err = 0;
  10179. + }
  10180. + goto out_dput;
  10181. + } else if (!d_is_positive(hinopath.dentry)) {
  10182. + err = vfsub_create(dir, &hinopath, 0600,
  10183. + /*want_excl*/false);
  10184. + AuTraceErr(err);
  10185. + if (unlikely(err))
  10186. + goto out_dput;
  10187. + }
  10188. + flags = O_WRONLY;
  10189. + }
  10190. + hinopath.mnt = path->mnt;
  10191. + hinofile = vfsub_dentry_open(&hinopath, flags);
  10192. + if (suspend)
  10193. + au_hn_inode_unlock(hdir);
  10194. + else
  10195. + inode_unlock(dir);
  10196. + dput(hinopath.dentry);
  10197. + AuTraceErrPtr(hinofile);
  10198. + if (IS_ERR(hinofile)) {
  10199. + err = PTR_ERR(hinofile);
  10200. + goto out;
  10201. + }
  10202. +
  10203. + if (load)
  10204. + err = au_dr_hino_load(&br->br_dirren, hinofile);
  10205. + else
  10206. + err = au_dr_hino_store(sb, br, hinofile);
  10207. + fput(hinofile);
  10208. + goto out;
  10209. +
  10210. +out_dput:
  10211. + dput(hinopath.dentry);
  10212. +out_unlock:
  10213. + if (suspend)
  10214. + au_hn_inode_unlock(hdir);
  10215. + else
  10216. + inode_unlock(dir);
  10217. +out:
  10218. + AuTraceErr(err);
  10219. + return err;
  10220. +}
  10221. +
  10222. +/* ---------------------------------------------------------------------- */
  10223. +
  10224. +static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
  10225. +{
  10226. + int err;
  10227. + struct kstatfs kstfs;
  10228. + dev_t dev;
  10229. + struct dentry *dentry;
  10230. + struct super_block *sb;
  10231. +
  10232. + err = vfs_statfs((void *)path, &kstfs);
  10233. + AuTraceErr(err);
  10234. + if (unlikely(err))
  10235. + goto out;
  10236. +
  10237. + /* todo: support for UUID */
  10238. +
  10239. + if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
  10240. + brid->type = AuBrid_FSID;
  10241. + brid->fsid = kstfs.f_fsid;
  10242. + } else {
  10243. + dentry = path->dentry;
  10244. + sb = dentry->d_sb;
  10245. + dev = sb->s_dev;
  10246. + if (dev) {
  10247. + brid->type = AuBrid_DEV;
  10248. + brid->dev = dev;
  10249. + }
  10250. + }
  10251. +
  10252. +out:
  10253. + return err;
  10254. +}
  10255. +
  10256. +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
  10257. + const struct path *path)
  10258. +{
  10259. + int err, i;
  10260. + struct au_dr_br *dr;
  10261. + struct hlist_bl_head *hbl;
  10262. +
  10263. + dr = &br->br_dirren;
  10264. + hbl = dr->dr_h_ino;
  10265. + for (i = 0; i < AuDirren_NHASH; i++, hbl++)
  10266. + INIT_HLIST_BL_HEAD(hbl);
  10267. +
  10268. + err = au_dr_brid_init(&dr->dr_brid, path);
  10269. + if (unlikely(err))
  10270. + goto out;
  10271. +
  10272. + if (au_opt_test(au_mntflags(sb), DIRREN))
  10273. + err = au_dr_hino(sb, /*bindex*/-1, br, path);
  10274. +
  10275. +out:
  10276. + AuTraceErr(err);
  10277. + return err;
  10278. +}
  10279. +
  10280. +int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
  10281. +{
  10282. + int err;
  10283. +
  10284. + err = 0;
  10285. + if (au_br_writable(br->br_perm))
  10286. + err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
  10287. + if (!err)
  10288. + au_dr_hino_free(&br->br_dirren);
  10289. +
  10290. + return err;
  10291. +}
  10292. +
  10293. +/* ---------------------------------------------------------------------- */
  10294. +
  10295. +static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
  10296. + char *buf, size_t sz)
  10297. +{
  10298. + int err;
  10299. + unsigned int major, minor;
  10300. + char *p;
  10301. +
  10302. + p = buf;
  10303. + err = snprintf(p, sz, "%d_", brid->type);
  10304. + AuDebugOn(err > sz);
  10305. + p += err;
  10306. + sz -= err;
  10307. + switch (brid->type) {
  10308. + case AuBrid_Unset:
  10309. + return -EINVAL;
  10310. + case AuBrid_UUID:
  10311. + err = snprintf(p, sz, "%pU", brid->uuid.b);
  10312. + break;
  10313. + case AuBrid_FSID:
  10314. + err = snprintf(p, sz, "%08x-%08x",
  10315. + brid->fsid.val[0], brid->fsid.val[1]);
  10316. + break;
  10317. + case AuBrid_DEV:
  10318. + major = MAJOR(brid->dev);
  10319. + minor = MINOR(brid->dev);
  10320. + if (major <= 0xff && minor <= 0xff)
  10321. + err = snprintf(p, sz, "%02x%02x", major, minor);
  10322. + else
  10323. + err = snprintf(p, sz, "%03x:%05x", major, minor);
  10324. + break;
  10325. + }
  10326. + AuDebugOn(err > sz);
  10327. + p += err;
  10328. + sz -= err;
  10329. + err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
  10330. + AuDebugOn(err > sz);
  10331. + p += err;
  10332. + sz -= err;
  10333. +
  10334. + return p - buf;
  10335. +}
  10336. +
  10337. +static int au_drinfo_name(struct au_branch *br, char *name, int len)
  10338. +{
  10339. + int rlen;
  10340. + struct dentry *br_dentry;
  10341. + struct inode *br_inode;
  10342. +
  10343. + br_dentry = au_br_dentry(br);
  10344. + br_inode = d_inode(br_dentry);
  10345. + rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
  10346. + AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
  10347. + AuDebugOn(rlen > len);
  10348. +
  10349. + return rlen;
  10350. +}
  10351. +
  10352. +/* ---------------------------------------------------------------------- */
  10353. +
  10354. +/*
  10355. + * from the given @h_dentry, construct drinfo at @*fdata.
  10356. + * when the size of @*fdata is not enough, reallocate and return new @fdata and
  10357. + * @allocated.
  10358. + */
  10359. +static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
  10360. + struct dentry *h_dentry,
  10361. + unsigned char *allocated)
  10362. +{
  10363. + int err, v;
  10364. + struct au_drinfo_fdata *f, *p;
  10365. + struct au_drinfo *drinfo;
  10366. + struct inode *h_inode;
  10367. + struct qstr *qname;
  10368. +
  10369. + err = 0;
  10370. + f = *fdata;
  10371. + h_inode = d_inode(h_dentry);
  10372. + qname = &h_dentry->d_name;
  10373. + drinfo = &f->drinfo;
  10374. + drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
  10375. + drinfo->oldnamelen = qname->len;
  10376. + if (*allocated < sizeof(*f) + qname->len) {
  10377. + v = roundup_pow_of_two(*allocated + qname->len);
  10378. + p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
  10379. + if (unlikely(!p)) {
  10380. + err = -ENOMEM;
  10381. + AuTraceErr(err);
  10382. + goto out;
  10383. + }
  10384. + f = p;
  10385. + *fdata = f;
  10386. + *allocated = v;
  10387. + drinfo = &f->drinfo;
  10388. + }
  10389. + memcpy(drinfo->oldname, qname->name, qname->len);
  10390. + AuDbg("i%llu, %.*s\n",
  10391. + be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
  10392. + drinfo->oldname);
  10393. +
  10394. +out:
  10395. + AuTraceErr(err);
  10396. + return err;
  10397. +}
  10398. +
  10399. +/* callers have to free the return value */
  10400. +static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
  10401. +{
  10402. + struct au_drinfo *ret, *drinfo;
  10403. + struct au_drinfo_fdata fdata;
  10404. + int len;
  10405. + loff_t pos;
  10406. + ssize_t ssz;
  10407. +
  10408. + ret = ERR_PTR(-EIO);
  10409. + pos = 0;
  10410. + ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
  10411. + if (unlikely(ssz != sizeof(fdata))) {
  10412. + AuIOErr("ssz %zd, %u, %pD2\n",
  10413. + ssz, (unsigned int)sizeof(fdata), file);
  10414. + goto out;
  10415. + }
  10416. +
  10417. + fdata.magic = ntohl((__force __be32)fdata.magic);
  10418. + switch (fdata.magic) {
  10419. + case AUFS_DRINFO_MAGIC_V1:
  10420. + break;
  10421. + default:
  10422. + AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
  10423. + fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
  10424. + goto out;
  10425. + }
  10426. +
  10427. + drinfo = &fdata.drinfo;
  10428. + len = drinfo->oldnamelen;
  10429. + if (!len) {
  10430. + AuIOErr("broken drinfo %pD2\n", file);
  10431. + goto out;
  10432. + }
  10433. +
  10434. + ret = NULL;
  10435. + drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
  10436. + if (unlikely(h_ino && drinfo->ino != h_ino)) {
  10437. + AuDbg("ignored i%llu, i%llu, %pD2\n",
  10438. + (unsigned long long)drinfo->ino,
  10439. + (unsigned long long)h_ino, file);
  10440. + goto out; /* success */
  10441. + }
  10442. +
  10443. + ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
  10444. + if (unlikely(!ret)) {
  10445. + ret = ERR_PTR(-ENOMEM);
  10446. + AuTraceErrPtr(ret);
  10447. + goto out;
  10448. + }
  10449. +
  10450. + *ret = *drinfo;
  10451. + ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
  10452. + if (unlikely(ssz != len)) {
  10453. + au_kfree_rcu(ret);
  10454. + ret = ERR_PTR(-EIO);
  10455. + AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
  10456. + goto out;
  10457. + }
  10458. +
  10459. + AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
  10460. +
  10461. +out:
  10462. + return ret;
  10463. +}
  10464. +
  10465. +/* ---------------------------------------------------------------------- */
  10466. +
  10467. +/* in order to be revertible */
  10468. +struct au_drinfo_rev_elm {
  10469. + int created;
  10470. + struct dentry *info_dentry;
  10471. + struct au_drinfo *info_last;
  10472. +};
  10473. +
  10474. +struct au_drinfo_rev {
  10475. + unsigned char already;
  10476. + aufs_bindex_t nelm;
  10477. + struct au_drinfo_rev_elm elm[0];
  10478. +};
  10479. +
  10480. +/* todo: isn't it too large? */
  10481. +struct au_drinfo_store {
  10482. + struct path h_ppath;
  10483. + struct dentry *h_dentry;
  10484. + struct au_drinfo_fdata *fdata;
  10485. + char *infoname; /* inside of whname, just after PFX */
  10486. + char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
  10487. + aufs_bindex_t btgt, btail;
  10488. + unsigned char no_sio,
  10489. + allocated, /* current size of *fdata */
  10490. + infonamelen, /* room size for p */
  10491. + whnamelen, /* length of the generated name */
  10492. + renameback; /* renamed back */
  10493. +};
  10494. +
  10495. +/* on rename(2) error, the caller should revert it using @elm */
  10496. +static int au_drinfo_do_store(struct au_drinfo_store *w,
  10497. + struct au_drinfo_rev_elm *elm)
  10498. +{
  10499. + int err, len;
  10500. + ssize_t ssz;
  10501. + loff_t pos;
  10502. + struct path infopath = {
  10503. + .mnt = w->h_ppath.mnt
  10504. + };
  10505. + struct inode *h_dir, *h_inode, *delegated;
  10506. + struct file *infofile;
  10507. + struct qstr *qname;
  10508. +
  10509. + AuDebugOn(elm
  10510. + && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
  10511. +
  10512. + infopath.dentry = vfsub_lookup_one_len(w->whname, w->h_ppath.dentry,
  10513. + w->whnamelen);
  10514. + AuTraceErrPtr(infopath.dentry);
  10515. + if (IS_ERR(infopath.dentry)) {
  10516. + err = PTR_ERR(infopath.dentry);
  10517. + goto out;
  10518. + }
  10519. +
  10520. + err = 0;
  10521. + h_dir = d_inode(w->h_ppath.dentry);
  10522. + if (elm && d_is_negative(infopath.dentry)) {
  10523. + err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
  10524. + AuTraceErr(err);
  10525. + if (unlikely(err))
  10526. + goto out_dput;
  10527. + elm->created = 1;
  10528. + elm->info_dentry = dget(infopath.dentry);
  10529. + }
  10530. +
  10531. + infofile = vfsub_dentry_open(&infopath, O_RDWR);
  10532. + AuTraceErrPtr(infofile);
  10533. + if (IS_ERR(infofile)) {
  10534. + err = PTR_ERR(infofile);
  10535. + goto out_dput;
  10536. + }
  10537. +
  10538. + h_inode = d_inode(infopath.dentry);
  10539. + if (elm && i_size_read(h_inode)) {
  10540. + h_inode = d_inode(w->h_dentry);
  10541. + elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
  10542. + AuTraceErrPtr(elm->info_last);
  10543. + if (IS_ERR(elm->info_last)) {
  10544. + err = PTR_ERR(elm->info_last);
  10545. + elm->info_last = NULL;
  10546. + AuDebugOn(elm->info_dentry);
  10547. + goto out_fput;
  10548. + }
  10549. + }
  10550. +
  10551. + if (elm && w->renameback) {
  10552. + delegated = NULL;
  10553. + err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
  10554. + AuTraceErr(err);
  10555. + if (unlikely(err == -EWOULDBLOCK))
  10556. + iput(delegated);
  10557. + goto out_fput;
  10558. + }
  10559. +
  10560. + pos = 0;
  10561. + qname = &w->h_dentry->d_name;
  10562. + len = sizeof(*w->fdata) + qname->len;
  10563. + if (!elm)
  10564. + len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
  10565. + ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
  10566. + if (ssz == len) {
  10567. + AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
  10568. + w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
  10569. + goto out_fput; /* success */
  10570. + } else {
  10571. + err = -EIO;
  10572. + if (ssz < 0)
  10573. + err = ssz;
  10574. + /* the caller should revert it using @elm */
  10575. + }
  10576. +
  10577. +out_fput:
  10578. + fput(infofile);
  10579. +out_dput:
  10580. + dput(infopath.dentry);
  10581. +out:
  10582. + AuTraceErr(err);
  10583. + return err;
  10584. +}
  10585. +
  10586. +struct au_call_drinfo_do_store_args {
  10587. + int *errp;
  10588. + struct au_drinfo_store *w;
  10589. + struct au_drinfo_rev_elm *elm;
  10590. +};
  10591. +
  10592. +static void au_call_drinfo_do_store(void *args)
  10593. +{
  10594. + struct au_call_drinfo_do_store_args *a = args;
  10595. +
  10596. + *a->errp = au_drinfo_do_store(a->w, a->elm);
  10597. +}
  10598. +
  10599. +static int au_drinfo_store_sio(struct au_drinfo_store *w,
  10600. + struct au_drinfo_rev_elm *elm)
  10601. +{
  10602. + int err, wkq_err;
  10603. +
  10604. + if (w->no_sio)
  10605. + err = au_drinfo_do_store(w, elm);
  10606. + else {
  10607. + struct au_call_drinfo_do_store_args a = {
  10608. + .errp = &err,
  10609. + .w = w,
  10610. + .elm = elm
  10611. + };
  10612. + wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
  10613. + if (unlikely(wkq_err))
  10614. + err = wkq_err;
  10615. + }
  10616. + AuTraceErr(err);
  10617. +
  10618. + return err;
  10619. +}
  10620. +
  10621. +static int au_drinfo_store_work_init(struct au_drinfo_store *w,
  10622. + aufs_bindex_t btgt)
  10623. +{
  10624. + int err;
  10625. +
  10626. + memset(w, 0, sizeof(*w));
  10627. + w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
  10628. + strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
  10629. + w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
  10630. + w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
  10631. + w->btgt = btgt;
  10632. + w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
  10633. +
  10634. + err = -ENOMEM;
  10635. + w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
  10636. + if (unlikely(!w->fdata)) {
  10637. + AuTraceErr(err);
  10638. + goto out;
  10639. + }
  10640. + w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
  10641. + err = 0;
  10642. +
  10643. +out:
  10644. + return err;
  10645. +}
  10646. +
  10647. +static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
  10648. +{
  10649. + au_kfree_rcu(w->fdata);
  10650. +}
  10651. +
  10652. +static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
  10653. + struct au_drinfo_store *w)
  10654. +{
  10655. + struct au_drinfo_rev_elm *elm;
  10656. + struct inode *h_dir, *delegated;
  10657. + int err, nelm;
  10658. + struct path infopath = {
  10659. + .mnt = w->h_ppath.mnt
  10660. + };
  10661. +
  10662. + h_dir = d_inode(w->h_ppath.dentry);
  10663. + IMustLock(h_dir);
  10664. +
  10665. + err = 0;
  10666. + elm = rev->elm;
  10667. + for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
  10668. + AuDebugOn(elm->created && elm->info_last);
  10669. + if (elm->created) {
  10670. + AuDbg("here\n");
  10671. + delegated = NULL;
  10672. + infopath.dentry = elm->info_dentry;
  10673. + err = vfsub_unlink(h_dir, &infopath, &delegated,
  10674. + !w->no_sio);
  10675. + AuTraceErr(err);
  10676. + if (unlikely(err == -EWOULDBLOCK))
  10677. + iput(delegated);
  10678. + dput(elm->info_dentry);
  10679. + } else if (elm->info_last) {
  10680. + AuDbg("here\n");
  10681. + w->fdata->drinfo = *elm->info_last;
  10682. + memcpy(w->fdata->drinfo.oldname,
  10683. + elm->info_last->oldname,
  10684. + elm->info_last->oldnamelen);
  10685. + err = au_drinfo_store_sio(w, /*elm*/NULL);
  10686. + au_kfree_rcu(elm->info_last);
  10687. + }
  10688. + if (unlikely(err))
  10689. + AuIOErr("%d, %s\n", err, w->whname);
  10690. + /* go on even if err */
  10691. + }
  10692. +}
  10693. +
  10694. +/* caller has to call au_dr_rename_fin() later */
  10695. +static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
  10696. + struct qstr *dst_name, void *_rev)
  10697. +{
  10698. + int err, sz, nelm;
  10699. + aufs_bindex_t bindex, btail;
  10700. + struct au_drinfo_store work;
  10701. + struct au_drinfo_rev *rev, **p;
  10702. + struct au_drinfo_rev_elm *elm;
  10703. + struct super_block *sb;
  10704. + struct au_branch *br;
  10705. + struct au_hinode *hdir;
  10706. +
  10707. + err = au_drinfo_store_work_init(&work, btgt);
  10708. + AuTraceErr(err);
  10709. + if (unlikely(err))
  10710. + goto out;
  10711. +
  10712. + err = -ENOMEM;
  10713. + btail = au_dbtaildir(dentry);
  10714. + nelm = btail - btgt;
  10715. + sz = sizeof(*rev) + sizeof(*elm) * nelm;
  10716. + rev = kcalloc(1, sz, GFP_NOFS);
  10717. + if (unlikely(!rev)) {
  10718. + AuTraceErr(err);
  10719. + goto out_args;
  10720. + }
  10721. + rev->nelm = nelm;
  10722. + elm = rev->elm;
  10723. + p = _rev;
  10724. + *p = rev;
  10725. +
  10726. + err = 0;
  10727. + sb = dentry->d_sb;
  10728. + work.h_ppath.dentry = au_h_dptr(dentry, btgt);
  10729. + work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
  10730. + hdir = au_hi(d_inode(dentry), btgt);
  10731. + au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
  10732. + for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
  10733. + work.h_dentry = au_h_dptr(dentry, bindex);
  10734. + if (!work.h_dentry)
  10735. + continue;
  10736. +
  10737. + err = au_drinfo_construct(&work.fdata, work.h_dentry,
  10738. + &work.allocated);
  10739. + AuTraceErr(err);
  10740. + if (unlikely(err))
  10741. + break;
  10742. +
  10743. + work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
  10744. + br = au_sbr(sb, bindex);
  10745. + work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
  10746. + work.whnamelen += au_drinfo_name(br, work.infoname,
  10747. + work.infonamelen);
  10748. + AuDbg("whname %.*s, i%llu, %.*s\n",
  10749. + work.whnamelen, work.whname,
  10750. + be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
  10751. + work.fdata->drinfo.oldnamelen,
  10752. + work.fdata->drinfo.oldname);
  10753. +
  10754. + err = au_drinfo_store_sio(&work, elm);
  10755. + AuTraceErr(err);
  10756. + if (unlikely(err))
  10757. + break;
  10758. + }
  10759. + if (unlikely(err)) {
  10760. + /* revert all drinfo */
  10761. + au_drinfo_store_rev(rev, &work);
  10762. + au_kfree_try_rcu(rev);
  10763. + *p = NULL;
  10764. + }
  10765. + au_hn_inode_unlock(hdir);
  10766. +
  10767. +out_args:
  10768. + au_drinfo_store_work_fin(&work);
  10769. +out:
  10770. + return err;
  10771. +}
  10772. +
  10773. +/* ---------------------------------------------------------------------- */
  10774. +
  10775. +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
  10776. + struct qstr *dst_name, void *_rev)
  10777. +{
  10778. + int err, already;
  10779. + ino_t ino;
  10780. + struct super_block *sb;
  10781. + struct au_branch *br;
  10782. + struct au_dr_br *dr;
  10783. + struct dentry *h_dentry;
  10784. + struct inode *h_inode;
  10785. + struct au_dr_hino *ent;
  10786. + struct au_drinfo_rev *rev, **p;
  10787. +
  10788. + AuDbg("bindex %d\n", bindex);
  10789. +
  10790. + err = -ENOMEM;
  10791. + ent = kmalloc(sizeof(*ent), GFP_NOFS);
  10792. + if (unlikely(!ent))
  10793. + goto out;
  10794. +
  10795. + sb = src->d_sb;
  10796. + br = au_sbr(sb, bindex);
  10797. + dr = &br->br_dirren;
  10798. + h_dentry = au_h_dptr(src, bindex);
  10799. + h_inode = d_inode(h_dentry);
  10800. + ino = h_inode->i_ino;
  10801. + ent->dr_h_ino = ino;
  10802. + already = au_dr_hino_test_add(dr, ino, ent);
  10803. + AuDbg("b%d, hi%llu, already %d\n",
  10804. + bindex, (unsigned long long)ino, already);
  10805. +
  10806. + err = au_drinfo_store(src, bindex, dst_name, _rev);
  10807. + AuTraceErr(err);
  10808. + if (!err) {
  10809. + p = _rev;
  10810. + rev = *p;
  10811. + rev->already = already;
  10812. + goto out; /* success */
  10813. + }
  10814. +
  10815. + /* revert */
  10816. + if (!already)
  10817. + au_dr_hino_del(dr, ent);
  10818. + au_kfree_rcu(ent);
  10819. +
  10820. +out:
  10821. + AuTraceErr(err);
  10822. + return err;
  10823. +}
  10824. +
  10825. +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
  10826. +{
  10827. + struct au_drinfo_rev *rev;
  10828. + struct au_drinfo_rev_elm *elm;
  10829. + int nelm;
  10830. +
  10831. + rev = _rev;
  10832. + elm = rev->elm;
  10833. + for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
  10834. + dput(elm->info_dentry);
  10835. + au_kfree_rcu(elm->info_last);
  10836. + }
  10837. + au_kfree_try_rcu(rev);
  10838. +}
  10839. +
  10840. +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
  10841. +{
  10842. + int err;
  10843. + struct au_drinfo_store work;
  10844. + struct au_drinfo_rev *rev = _rev;
  10845. + struct super_block *sb;
  10846. + struct au_branch *br;
  10847. + struct inode *h_inode;
  10848. + struct au_dr_br *dr;
  10849. + struct au_dr_hino *ent;
  10850. +
  10851. + err = au_drinfo_store_work_init(&work, btgt);
  10852. + if (unlikely(err))
  10853. + goto out;
  10854. +
  10855. + sb = src->d_sb;
  10856. + br = au_sbr(sb, btgt);
  10857. + work.h_ppath.dentry = au_h_dptr(src, btgt);
  10858. + work.h_ppath.mnt = au_br_mnt(br);
  10859. + au_drinfo_store_rev(rev, &work);
  10860. + au_drinfo_store_work_fin(&work);
  10861. + if (rev->already)
  10862. + goto out;
  10863. +
  10864. + dr = &br->br_dirren;
  10865. + h_inode = d_inode(work.h_ppath.dentry);
  10866. + ent = au_dr_hino_find(dr, h_inode->i_ino);
  10867. + BUG_ON(!ent);
  10868. + au_dr_hino_del(dr, ent);
  10869. + au_kfree_rcu(ent);
  10870. +
  10871. +out:
  10872. + au_kfree_try_rcu(rev);
  10873. + if (unlikely(err))
  10874. + pr_err("failed to remove dirren info\n");
  10875. +}
  10876. +
  10877. +/* ---------------------------------------------------------------------- */
  10878. +
  10879. +static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
  10880. + char *whname, int whnamelen,
  10881. + struct dentry **info_dentry)
  10882. +{
  10883. + struct au_drinfo *drinfo;
  10884. + struct file *f;
  10885. + struct inode *h_dir;
  10886. + struct path infopath;
  10887. + int unlocked;
  10888. +
  10889. + AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
  10890. +
  10891. + *info_dentry = NULL;
  10892. + drinfo = NULL;
  10893. + unlocked = 0;
  10894. + h_dir = d_inode(h_ppath->dentry);
  10895. + inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
  10896. + infopath.dentry = vfsub_lookup_one_len(whname, h_ppath->dentry,
  10897. + whnamelen);
  10898. + if (IS_ERR(infopath.dentry)) {
  10899. + drinfo = (void *)infopath.dentry;
  10900. + goto out;
  10901. + }
  10902. +
  10903. + if (d_is_negative(infopath.dentry))
  10904. + goto out_dput; /* success */
  10905. +
  10906. + infopath.mnt = h_ppath->mnt;
  10907. + f = vfsub_dentry_open(&infopath, O_RDONLY);
  10908. + inode_unlock_shared(h_dir);
  10909. + unlocked = 1;
  10910. + if (IS_ERR(f)) {
  10911. + drinfo = (void *)f;
  10912. + goto out_dput;
  10913. + }
  10914. +
  10915. + drinfo = au_drinfo_read_k(f, /*h_ino*/0);
  10916. + if (IS_ERR_OR_NULL(drinfo))
  10917. + goto out_fput;
  10918. +
  10919. + AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
  10920. + *info_dentry = dget(infopath.dentry); /* keep it alive */
  10921. +
  10922. +out_fput:
  10923. + fput(f);
  10924. +out_dput:
  10925. + dput(infopath.dentry);
  10926. +out:
  10927. + if (!unlocked)
  10928. + inode_unlock_shared(h_dir);
  10929. + AuTraceErrPtr(drinfo);
  10930. + return drinfo;
  10931. +}
  10932. +
  10933. +struct au_drinfo_do_load_args {
  10934. + struct au_drinfo **drinfop;
  10935. + struct path *h_ppath;
  10936. + char *whname;
  10937. + int whnamelen;
  10938. + struct dentry **info_dentry;
  10939. +};
  10940. +
  10941. +static void au_call_drinfo_do_load(void *args)
  10942. +{
  10943. + struct au_drinfo_do_load_args *a = args;
  10944. +
  10945. + *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
  10946. + a->info_dentry);
  10947. +}
  10948. +
  10949. +struct au_drinfo_load {
  10950. + struct path h_ppath;
  10951. + struct qstr *qname;
  10952. + unsigned char no_sio;
  10953. +
  10954. + aufs_bindex_t ninfo;
  10955. + struct au_drinfo **drinfo;
  10956. +};
  10957. +
  10958. +static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
  10959. + struct au_branch *br)
  10960. +{
  10961. + int err, wkq_err, whnamelen, e;
  10962. + char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
  10963. + = AUFS_WH_DR_INFO_PFX;
  10964. + struct au_drinfo *drinfo;
  10965. + struct qstr oldname;
  10966. + struct inode *h_dir, *delegated;
  10967. + struct dentry *info_dentry;
  10968. + struct path infopath;
  10969. +
  10970. + whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
  10971. + whnamelen += au_drinfo_name(br, whname + whnamelen,
  10972. + sizeof(whname) - whnamelen);
  10973. + if (w->no_sio)
  10974. + drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
  10975. + &info_dentry);
  10976. + else {
  10977. + struct au_drinfo_do_load_args args = {
  10978. + .drinfop = &drinfo,
  10979. + .h_ppath = &w->h_ppath,
  10980. + .whname = whname,
  10981. + .whnamelen = whnamelen,
  10982. + .info_dentry = &info_dentry
  10983. + };
  10984. + wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
  10985. + if (unlikely(wkq_err))
  10986. + drinfo = ERR_PTR(wkq_err);
  10987. + }
  10988. + err = PTR_ERR(drinfo);
  10989. + if (IS_ERR_OR_NULL(drinfo))
  10990. + goto out;
  10991. +
  10992. + err = 0;
  10993. + oldname.len = drinfo->oldnamelen;
  10994. + oldname.name = drinfo->oldname;
  10995. + if (au_qstreq(w->qname, &oldname)) {
  10996. + /* the name is renamed back */
  10997. + au_kfree_rcu(drinfo);
  10998. + drinfo = NULL;
  10999. +
  11000. + infopath.dentry = info_dentry;
  11001. + infopath.mnt = w->h_ppath.mnt;
  11002. + h_dir = d_inode(w->h_ppath.dentry);
  11003. + delegated = NULL;
  11004. + inode_lock_nested(h_dir, AuLsc_I_PARENT);
  11005. + e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
  11006. + inode_unlock(h_dir);
  11007. + if (unlikely(e))
  11008. + AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
  11009. + if (unlikely(e == -EWOULDBLOCK))
  11010. + iput(delegated);
  11011. + }
  11012. + au_kfree_rcu(w->drinfo[bindex]);
  11013. + w->drinfo[bindex] = drinfo;
  11014. + dput(info_dentry);
  11015. +
  11016. +out:
  11017. + AuTraceErr(err);
  11018. + return err;
  11019. +}
  11020. +
  11021. +/* ---------------------------------------------------------------------- */
  11022. +
  11023. +static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
  11024. +{
  11025. + struct au_drinfo **p = drinfo;
  11026. +
  11027. + while (n-- > 0)
  11028. + au_kfree_rcu(*drinfo++);
  11029. + au_kfree_try_rcu(p);
  11030. +}
  11031. +
  11032. +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
  11033. + aufs_bindex_t btgt)
  11034. +{
  11035. + int err, ninfo;
  11036. + struct au_drinfo_load w;
  11037. + aufs_bindex_t bindex, bbot;
  11038. + struct au_branch *br;
  11039. + struct inode *h_dir;
  11040. + struct au_dr_hino *ent;
  11041. + struct super_block *sb;
  11042. +
  11043. + AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
  11044. + AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
  11045. + AuLNPair(&lkup->whname), btgt);
  11046. +
  11047. + sb = dentry->d_sb;
  11048. + bbot = au_sbbot(sb);
  11049. + w.ninfo = bbot + 1;
  11050. + if (!lkup->dirren.drinfo) {
  11051. + lkup->dirren.drinfo = kcalloc(w.ninfo,
  11052. + sizeof(*lkup->dirren.drinfo),
  11053. + GFP_NOFS);
  11054. + if (unlikely(!lkup->dirren.drinfo)) {
  11055. + err = -ENOMEM;
  11056. + goto out;
  11057. + }
  11058. + lkup->dirren.ninfo = w.ninfo;
  11059. + }
  11060. + w.drinfo = lkup->dirren.drinfo;
  11061. + w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
  11062. + w.h_ppath.dentry = au_h_dptr(dentry, btgt);
  11063. + AuDebugOn(!w.h_ppath.dentry);
  11064. + w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
  11065. + w.qname = &dentry->d_name;
  11066. +
  11067. + ninfo = 0;
  11068. + for (bindex = btgt + 1; bindex <= bbot; bindex++) {
  11069. + br = au_sbr(sb, bindex);
  11070. + err = au_drinfo_load(&w, bindex, br);
  11071. + if (unlikely(err))
  11072. + goto out_free;
  11073. + if (w.drinfo[bindex])
  11074. + ninfo++;
  11075. + }
  11076. + if (!ninfo) {
  11077. + br = au_sbr(sb, btgt);
  11078. + h_dir = d_inode(w.h_ppath.dentry);
  11079. + ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
  11080. + AuDebugOn(!ent);
  11081. + au_dr_hino_del(&br->br_dirren, ent);
  11082. + au_kfree_rcu(ent);
  11083. + }
  11084. + goto out; /* success */
  11085. +
  11086. +out_free:
  11087. + au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
  11088. + lkup->dirren.ninfo = 0;
  11089. + lkup->dirren.drinfo = NULL;
  11090. +out:
  11091. + AuTraceErr(err);
  11092. + return err;
  11093. +}
  11094. +
  11095. +void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
  11096. +{
  11097. + au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
  11098. +}
  11099. +
  11100. +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
  11101. +{
  11102. + int err;
  11103. + struct au_drinfo *drinfo;
  11104. +
  11105. + err = 0;
  11106. + if (!lkup->dirren.drinfo)
  11107. + goto out;
  11108. + AuDebugOn(lkup->dirren.ninfo < btgt + 1);
  11109. + drinfo = lkup->dirren.drinfo[btgt + 1];
  11110. + if (!drinfo)
  11111. + goto out;
  11112. +
  11113. + au_kfree_try_rcu(lkup->whname.name);
  11114. + lkup->whname.name = NULL;
  11115. + lkup->dirren.dr_name.len = drinfo->oldnamelen;
  11116. + lkup->dirren.dr_name.name = drinfo->oldname;
  11117. + lkup->name = &lkup->dirren.dr_name;
  11118. + err = au_wh_name_alloc(&lkup->whname, lkup->name);
  11119. + if (!err)
  11120. + AuDbg("name %.*s, whname %.*s, b%d\n",
  11121. + AuLNPair(lkup->name), AuLNPair(&lkup->whname),
  11122. + btgt);
  11123. +
  11124. +out:
  11125. + AuTraceErr(err);
  11126. + return err;
  11127. +}
  11128. +
  11129. +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
  11130. + ino_t h_ino)
  11131. +{
  11132. + int match;
  11133. + struct au_drinfo *drinfo;
  11134. +
  11135. + match = 1;
  11136. + if (!lkup->dirren.drinfo)
  11137. + goto out;
  11138. + AuDebugOn(lkup->dirren.ninfo < bindex + 1);
  11139. + drinfo = lkup->dirren.drinfo[bindex + 1];
  11140. + if (!drinfo)
  11141. + goto out;
  11142. +
  11143. + match = (drinfo->ino == h_ino);
  11144. + AuDbg("match %d\n", match);
  11145. +
  11146. +out:
  11147. + return match;
  11148. +}
  11149. +
  11150. +/* ---------------------------------------------------------------------- */
  11151. +
  11152. +int au_dr_opt_set(struct super_block *sb)
  11153. +{
  11154. + int err;
  11155. + aufs_bindex_t bindex, bbot;
  11156. + struct au_branch *br;
  11157. +
  11158. + err = 0;
  11159. + bbot = au_sbbot(sb);
  11160. + for (bindex = 0; !err && bindex <= bbot; bindex++) {
  11161. + br = au_sbr(sb, bindex);
  11162. + err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
  11163. + }
  11164. +
  11165. + return err;
  11166. +}
  11167. +
  11168. +int au_dr_opt_flush(struct super_block *sb)
  11169. +{
  11170. + int err;
  11171. + aufs_bindex_t bindex, bbot;
  11172. + struct au_branch *br;
  11173. +
  11174. + err = 0;
  11175. + bbot = au_sbbot(sb);
  11176. + for (bindex = 0; !err && bindex <= bbot; bindex++) {
  11177. + br = au_sbr(sb, bindex);
  11178. + if (au_br_writable(br->br_perm))
  11179. + err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
  11180. + }
  11181. +
  11182. + return err;
  11183. +}
  11184. +
  11185. +int au_dr_opt_clr(struct super_block *sb, int no_flush)
  11186. +{
  11187. + int err;
  11188. + aufs_bindex_t bindex, bbot;
  11189. + struct au_branch *br;
  11190. +
  11191. + err = 0;
  11192. + if (!no_flush) {
  11193. + err = au_dr_opt_flush(sb);
  11194. + if (unlikely(err))
  11195. + goto out;
  11196. + }
  11197. +
  11198. + bbot = au_sbbot(sb);
  11199. + for (bindex = 0; bindex <= bbot; bindex++) {
  11200. + br = au_sbr(sb, bindex);
  11201. + au_dr_hino_free(&br->br_dirren);
  11202. + }
  11203. +
  11204. +out:
  11205. + return err;
  11206. +}
  11207. diff -Naur null/fs/aufs/dirren.h linux-5.1/fs/aufs/dirren.h
  11208. --- /dev/null
  11209. +++ linux-5.1/fs/aufs/dirren.h 2019-05-22 11:23:07.439141088 +0200
  11210. @@ -0,0 +1,140 @@
  11211. +/* SPDX-License-Identifier: GPL-2.0 */
  11212. +/*
  11213. + * Copyright (C) 2017-2019 Junjiro R. Okajima
  11214. + *
  11215. + * This program, aufs is free software; you can redistribute it and/or modify
  11216. + * it under the terms of the GNU General Public License as published by
  11217. + * the Free Software Foundation; either version 2 of the License, or
  11218. + * (at your option) any later version.
  11219. + *
  11220. + * This program is distributed in the hope that it will be useful,
  11221. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11222. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11223. + * GNU General Public License for more details.
  11224. + *
  11225. + * You should have received a copy of the GNU General Public License
  11226. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  11227. + */
  11228. +
  11229. +/*
  11230. + * renamed dir info
  11231. + */
  11232. +
  11233. +#ifndef __AUFS_DIRREN_H__
  11234. +#define __AUFS_DIRREN_H__
  11235. +
  11236. +#ifdef __KERNEL__
  11237. +
  11238. +#include <linux/dcache.h>
  11239. +#include <linux/statfs.h>
  11240. +#include <linux/uuid.h>
  11241. +#include "hbl.h"
  11242. +
  11243. +#define AuDirren_NHASH 100
  11244. +
  11245. +#ifdef CONFIG_AUFS_DIRREN
  11246. +enum au_brid_type {
  11247. + AuBrid_Unset,
  11248. + AuBrid_UUID,
  11249. + AuBrid_FSID,
  11250. + AuBrid_DEV
  11251. +};
  11252. +
  11253. +struct au_dr_brid {
  11254. + enum au_brid_type type;
  11255. + union {
  11256. + uuid_t uuid; /* unimplemented yet */
  11257. + fsid_t fsid;
  11258. + dev_t dev;
  11259. + };
  11260. +};
  11261. +
  11262. +/* 20 is the max digits length of ulong 64 */
  11263. +/* brid-type "_" uuid "_" inum */
  11264. +#define AUFS_DIRREN_FNAME_SZ (1 + 1 + UUID_STRING_LEN + 20)
  11265. +#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
  11266. +
  11267. +struct au_dr_hino {
  11268. + struct hlist_bl_node dr_hnode;
  11269. + ino_t dr_h_ino;
  11270. +};
  11271. +
  11272. +struct au_dr_br {
  11273. + struct hlist_bl_head dr_h_ino[AuDirren_NHASH];
  11274. + struct au_dr_brid dr_brid;
  11275. +};
  11276. +
  11277. +struct au_dr_lookup {
  11278. + /* dr_name is pointed by struct au_do_lookup_args.name */
  11279. + struct qstr dr_name; /* subset of dr_info */
  11280. + aufs_bindex_t ninfo;
  11281. + struct au_drinfo **drinfo;
  11282. +};
  11283. +#else
  11284. +struct au_dr_hino;
  11285. +/* empty */
  11286. +struct au_dr_br { };
  11287. +struct au_dr_lookup { };
  11288. +#endif
  11289. +
  11290. +/* ---------------------------------------------------------------------- */
  11291. +
  11292. +struct au_branch;
  11293. +struct au_do_lookup_args;
  11294. +struct au_hinode;
  11295. +#ifdef CONFIG_AUFS_DIRREN
  11296. +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
  11297. + struct au_dr_hino *add_ent);
  11298. +void au_dr_hino_free(struct au_dr_br *dr);
  11299. +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
  11300. + const struct path *path);
  11301. +int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
  11302. +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
  11303. + struct qstr *dst_name, void *_rev);
  11304. +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
  11305. +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
  11306. +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
  11307. + aufs_bindex_t bindex);
  11308. +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
  11309. +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
  11310. + ino_t h_ino);
  11311. +void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
  11312. +int au_dr_opt_set(struct super_block *sb);
  11313. +int au_dr_opt_flush(struct super_block *sb);
  11314. +int au_dr_opt_clr(struct super_block *sb, int no_flush);
  11315. +#else
  11316. +AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
  11317. + struct au_dr_hino *add_ent);
  11318. +AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
  11319. +AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
  11320. + const struct path *path);
  11321. +AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
  11322. +AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
  11323. + struct qstr *dst_name, void *_rev);
  11324. +AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
  11325. +AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
  11326. + void *rev);
  11327. +AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
  11328. + aufs_bindex_t bindex);
  11329. +AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
  11330. +AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
  11331. + aufs_bindex_t bindex, ino_t h_ino);
  11332. +AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
  11333. +AuStubInt0(au_dr_opt_set, struct super_block *sb);
  11334. +AuStubInt0(au_dr_opt_flush, struct super_block *sb);
  11335. +AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
  11336. +#endif
  11337. +
  11338. +/* ---------------------------------------------------------------------- */
  11339. +
  11340. +#ifdef CONFIG_AUFS_DIRREN
  11341. +static inline int au_dr_ihash(ino_t h_ino)
  11342. +{
  11343. + return h_ino % AuDirren_NHASH;
  11344. +}
  11345. +#else
  11346. +AuStubInt0(au_dr_ihash, ino_t h_ino);
  11347. +#endif
  11348. +
  11349. +#endif /* __KERNEL__ */
  11350. +#endif /* __AUFS_DIRREN_H__ */
  11351. diff -Naur null/fs/aufs/dynop.c linux-5.1/fs/aufs/dynop.c
  11352. --- /dev/null
  11353. +++ linux-5.1/fs/aufs/dynop.c 2019-05-22 11:23:07.439141088 +0200
  11354. @@ -0,0 +1,367 @@
  11355. +// SPDX-License-Identifier: GPL-2.0
  11356. +/*
  11357. + * Copyright (C) 2010-2019 Junjiro R. Okajima
  11358. + *
  11359. + * This program, aufs is free software; you can redistribute it and/or modify
  11360. + * it under the terms of the GNU General Public License as published by
  11361. + * the Free Software Foundation; either version 2 of the License, or
  11362. + * (at your option) any later version.
  11363. + *
  11364. + * This program is distributed in the hope that it will be useful,
  11365. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11366. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11367. + * GNU General Public License for more details.
  11368. + *
  11369. + * You should have received a copy of the GNU General Public License
  11370. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  11371. + */
  11372. +
  11373. +/*
  11374. + * dynamically customizable operations for regular files
  11375. + */
  11376. +
  11377. +#include "aufs.h"
  11378. +
  11379. +#define DyPrSym(key) AuDbgSym(key->dk_op.dy_hop)
  11380. +
  11381. +/*
  11382. + * How large will these lists be?
  11383. + * Usually just a few elements, 20-30 at most for each, I guess.
  11384. + */
  11385. +static struct hlist_bl_head dynop[AuDyLast];
  11386. +
  11387. +static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
  11388. + const void *h_op)
  11389. +{
  11390. + struct au_dykey *key, *tmp;
  11391. + struct hlist_bl_node *pos;
  11392. +
  11393. + key = NULL;
  11394. + hlist_bl_lock(hbl);
  11395. + hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
  11396. + if (tmp->dk_op.dy_hop == h_op) {
  11397. + key = tmp;
  11398. + kref_get(&key->dk_kref);
  11399. + break;
  11400. + }
  11401. + hlist_bl_unlock(hbl);
  11402. +
  11403. + return key;
  11404. +}
  11405. +
  11406. +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
  11407. +{
  11408. + struct au_dykey **k, *found;
  11409. + const void *h_op = key->dk_op.dy_hop;
  11410. + int i;
  11411. +
  11412. + found = NULL;
  11413. + k = br->br_dykey;
  11414. + for (i = 0; i < AuBrDynOp; i++)
  11415. + if (k[i]) {
  11416. + if (k[i]->dk_op.dy_hop == h_op) {
  11417. + found = k[i];
  11418. + break;
  11419. + }
  11420. + } else
  11421. + break;
  11422. + if (!found) {
  11423. + spin_lock(&br->br_dykey_lock);
  11424. + for (; i < AuBrDynOp; i++)
  11425. + if (k[i]) {
  11426. + if (k[i]->dk_op.dy_hop == h_op) {
  11427. + found = k[i];
  11428. + break;
  11429. + }
  11430. + } else {
  11431. + k[i] = key;
  11432. + break;
  11433. + }
  11434. + spin_unlock(&br->br_dykey_lock);
  11435. + BUG_ON(i == AuBrDynOp); /* expand the array */
  11436. + }
  11437. +
  11438. + return found;
  11439. +}
  11440. +
  11441. +/* kref_get() if @key is already added */
  11442. +static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
  11443. +{
  11444. + struct au_dykey *tmp, *found;
  11445. + struct hlist_bl_node *pos;
  11446. + const void *h_op = key->dk_op.dy_hop;
  11447. +
  11448. + found = NULL;
  11449. + hlist_bl_lock(hbl);
  11450. + hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
  11451. + if (tmp->dk_op.dy_hop == h_op) {
  11452. + kref_get(&tmp->dk_kref);
  11453. + found = tmp;
  11454. + break;
  11455. + }
  11456. + if (!found)
  11457. + hlist_bl_add_head(&key->dk_hnode, hbl);
  11458. + hlist_bl_unlock(hbl);
  11459. +
  11460. + if (!found)
  11461. + DyPrSym(key);
  11462. + return found;
  11463. +}
  11464. +
  11465. +static void dy_free_rcu(struct rcu_head *rcu)
  11466. +{
  11467. + struct au_dykey *key;
  11468. +
  11469. + key = container_of(rcu, struct au_dykey, dk_rcu);
  11470. + DyPrSym(key);
  11471. + au_kfree_rcu(key);
  11472. +}
  11473. +
  11474. +static void dy_free(struct kref *kref)
  11475. +{
  11476. + struct au_dykey *key;
  11477. + struct hlist_bl_head *hbl;
  11478. +
  11479. + key = container_of(kref, struct au_dykey, dk_kref);
  11480. + hbl = dynop + key->dk_op.dy_type;
  11481. + au_hbl_del(&key->dk_hnode, hbl);
  11482. + call_rcu(&key->dk_rcu, dy_free_rcu);
  11483. +}
  11484. +
  11485. +void au_dy_put(struct au_dykey *key)
  11486. +{
  11487. + kref_put(&key->dk_kref, dy_free);
  11488. +}
  11489. +
  11490. +/* ---------------------------------------------------------------------- */
  11491. +
  11492. +#define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
  11493. +
  11494. +#ifdef CONFIG_AUFS_DEBUG
  11495. +#define DyDbgDeclare(cnt) unsigned int cnt = 0
  11496. +#define DyDbgInc(cnt) do { cnt++; } while (0)
  11497. +#else
  11498. +#define DyDbgDeclare(cnt) do {} while (0)
  11499. +#define DyDbgInc(cnt) do {} while (0)
  11500. +#endif
  11501. +
  11502. +#define DySet(func, dst, src, h_op, h_sb) do { \
  11503. + DyDbgInc(cnt); \
  11504. + if (h_op->func) { \
  11505. + if (src.func) \
  11506. + dst.func = src.func; \
  11507. + else \
  11508. + AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
  11509. + } \
  11510. +} while (0)
  11511. +
  11512. +#define DySetForce(func, dst, src) do { \
  11513. + AuDebugOn(!src.func); \
  11514. + DyDbgInc(cnt); \
  11515. + dst.func = src.func; \
  11516. +} while (0)
  11517. +
  11518. +#define DySetAop(func) \
  11519. + DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
  11520. +#define DySetAopForce(func) \
  11521. + DySetForce(func, dyaop->da_op, aufs_aop)
  11522. +
  11523. +static void dy_aop(struct au_dykey *key, const void *h_op,
  11524. + struct super_block *h_sb __maybe_unused)
  11525. +{
  11526. + struct au_dyaop *dyaop = (void *)key;
  11527. + const struct address_space_operations *h_aop = h_op;
  11528. + DyDbgDeclare(cnt);
  11529. +
  11530. + AuDbg("%s\n", au_sbtype(h_sb));
  11531. +
  11532. + DySetAop(writepage);
  11533. + DySetAopForce(readpage); /* force */
  11534. + DySetAop(writepages);
  11535. + DySetAop(set_page_dirty);
  11536. + DySetAop(readpages);
  11537. + DySetAop(write_begin);
  11538. + DySetAop(write_end);
  11539. + DySetAop(bmap);
  11540. + DySetAop(invalidatepage);
  11541. + DySetAop(releasepage);
  11542. + DySetAop(freepage);
  11543. + /* this one will be changed according to an aufs mount option */
  11544. + DySetAop(direct_IO);
  11545. + DySetAop(migratepage);
  11546. + DySetAop(isolate_page);
  11547. + DySetAop(putback_page);
  11548. + DySetAop(launder_page);
  11549. + DySetAop(is_partially_uptodate);
  11550. + DySetAop(is_dirty_writeback);
  11551. + DySetAop(error_remove_page);
  11552. + DySetAop(swap_activate);
  11553. + DySetAop(swap_deactivate);
  11554. +
  11555. + DyDbgSize(cnt, *h_aop);
  11556. +}
  11557. +
  11558. +/* ---------------------------------------------------------------------- */
  11559. +
  11560. +static void dy_bug(struct kref *kref)
  11561. +{
  11562. + BUG();
  11563. +}
  11564. +
  11565. +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
  11566. +{
  11567. + struct au_dykey *key, *old;
  11568. + struct hlist_bl_head *hbl;
  11569. + struct op {
  11570. + unsigned int sz;
  11571. + void (*set)(struct au_dykey *key, const void *h_op,
  11572. + struct super_block *h_sb __maybe_unused);
  11573. + };
  11574. + static const struct op a[] = {
  11575. + [AuDy_AOP] = {
  11576. + .sz = sizeof(struct au_dyaop),
  11577. + .set = dy_aop
  11578. + }
  11579. + };
  11580. + const struct op *p;
  11581. +
  11582. + hbl = dynop + op->dy_type;
  11583. + key = dy_gfind_get(hbl, op->dy_hop);
  11584. + if (key)
  11585. + goto out_add; /* success */
  11586. +
  11587. + p = a + op->dy_type;
  11588. + key = kzalloc(p->sz, GFP_NOFS);
  11589. + if (unlikely(!key)) {
  11590. + key = ERR_PTR(-ENOMEM);
  11591. + goto out;
  11592. + }
  11593. +
  11594. + key->dk_op.dy_hop = op->dy_hop;
  11595. + kref_init(&key->dk_kref);
  11596. + p->set(key, op->dy_hop, au_br_sb(br));
  11597. + old = dy_gadd(hbl, key);
  11598. + if (old) {
  11599. + au_kfree_rcu(key);
  11600. + key = old;
  11601. + }
  11602. +
  11603. +out_add:
  11604. + old = dy_bradd(br, key);
  11605. + if (old)
  11606. + /* its ref-count should never be zero here */
  11607. + kref_put(&key->dk_kref, dy_bug);
  11608. +out:
  11609. + return key;
  11610. +}
  11611. +
  11612. +/* ---------------------------------------------------------------------- */
  11613. +/*
  11614. + * Aufs prohibits O_DIRECT by default even if the branch supports it.
  11615. + * This behaviour is necessary to return an error from open(O_DIRECT) instead
  11616. + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
  11617. + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
  11618. + * See the aufs manual in detail.
  11619. + */
  11620. +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
  11621. +{
  11622. + if (!do_dx)
  11623. + dyaop->da_op.direct_IO = NULL;
  11624. + else
  11625. + dyaop->da_op.direct_IO = aufs_aop.direct_IO;
  11626. +}
  11627. +
  11628. +static struct au_dyaop *dy_aget(struct au_branch *br,
  11629. + const struct address_space_operations *h_aop,
  11630. + int do_dx)
  11631. +{
  11632. + struct au_dyaop *dyaop;
  11633. + struct au_dynop op;
  11634. +
  11635. + op.dy_type = AuDy_AOP;
  11636. + op.dy_haop = h_aop;
  11637. + dyaop = (void *)dy_get(&op, br);
  11638. + if (IS_ERR(dyaop))
  11639. + goto out;
  11640. + dy_adx(dyaop, do_dx);
  11641. +
  11642. +out:
  11643. + return dyaop;
  11644. +}
  11645. +
  11646. +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
  11647. + struct inode *h_inode)
  11648. +{
  11649. + int err, do_dx;
  11650. + struct super_block *sb;
  11651. + struct au_branch *br;
  11652. + struct au_dyaop *dyaop;
  11653. +
  11654. + AuDebugOn(!S_ISREG(h_inode->i_mode));
  11655. + IiMustWriteLock(inode);
  11656. +
  11657. + sb = inode->i_sb;
  11658. + br = au_sbr(sb, bindex);
  11659. + do_dx = !!au_opt_test(au_mntflags(sb), DIO);
  11660. + dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
  11661. + err = PTR_ERR(dyaop);
  11662. + if (IS_ERR(dyaop))
  11663. + /* unnecessary to call dy_fput() */
  11664. + goto out;
  11665. +
  11666. + err = 0;
  11667. + inode->i_mapping->a_ops = &dyaop->da_op;
  11668. +
  11669. +out:
  11670. + return err;
  11671. +}
  11672. +
  11673. +/*
  11674. + * Is it safe to replace a_ops during the inode/file is in operation?
  11675. + * Yes, I hope so.
  11676. + */
  11677. +int au_dy_irefresh(struct inode *inode)
  11678. +{
  11679. + int err;
  11680. + aufs_bindex_t btop;
  11681. + struct inode *h_inode;
  11682. +
  11683. + err = 0;
  11684. + if (S_ISREG(inode->i_mode)) {
  11685. + btop = au_ibtop(inode);
  11686. + h_inode = au_h_iptr(inode, btop);
  11687. + err = au_dy_iaop(inode, btop, h_inode);
  11688. + }
  11689. + return err;
  11690. +}
  11691. +
  11692. +void au_dy_arefresh(int do_dx)
  11693. +{
  11694. + struct hlist_bl_head *hbl;
  11695. + struct hlist_bl_node *pos;
  11696. + struct au_dykey *key;
  11697. +
  11698. + hbl = dynop + AuDy_AOP;
  11699. + hlist_bl_lock(hbl);
  11700. + hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
  11701. + dy_adx((void *)key, do_dx);
  11702. + hlist_bl_unlock(hbl);
  11703. +}
  11704. +
  11705. +/* ---------------------------------------------------------------------- */
  11706. +
  11707. +void __init au_dy_init(void)
  11708. +{
  11709. + int i;
  11710. +
  11711. + for (i = 0; i < AuDyLast; i++)
  11712. + INIT_HLIST_BL_HEAD(dynop + i);
  11713. +}
  11714. +
  11715. +void au_dy_fin(void)
  11716. +{
  11717. + int i;
  11718. +
  11719. + for (i = 0; i < AuDyLast; i++)
  11720. + WARN_ON(!hlist_bl_empty(dynop + i));
  11721. +}
  11722. diff -Naur null/fs/aufs/dynop.h linux-5.1/fs/aufs/dynop.h
  11723. --- /dev/null
  11724. +++ linux-5.1/fs/aufs/dynop.h 2019-05-22 11:23:07.439141088 +0200
  11725. @@ -0,0 +1,77 @@
  11726. +/* SPDX-License-Identifier: GPL-2.0 */
  11727. +/*
  11728. + * Copyright (C) 2010-2019 Junjiro R. Okajima
  11729. + *
  11730. + * This program, aufs is free software; you can redistribute it and/or modify
  11731. + * it under the terms of the GNU General Public License as published by
  11732. + * the Free Software Foundation; either version 2 of the License, or
  11733. + * (at your option) any later version.
  11734. + *
  11735. + * This program is distributed in the hope that it will be useful,
  11736. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11737. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11738. + * GNU General Public License for more details.
  11739. + *
  11740. + * You should have received a copy of the GNU General Public License
  11741. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  11742. + */
  11743. +
  11744. +/*
  11745. + * dynamically customizable operations (for regular files only)
  11746. + */
  11747. +
  11748. +#ifndef __AUFS_DYNOP_H__
  11749. +#define __AUFS_DYNOP_H__
  11750. +
  11751. +#ifdef __KERNEL__
  11752. +
  11753. +#include <linux/fs.h>
  11754. +#include <linux/kref.h>
  11755. +
  11756. +enum {AuDy_AOP, AuDyLast};
  11757. +
  11758. +struct au_dynop {
  11759. + int dy_type;
  11760. + union {
  11761. + const void *dy_hop;
  11762. + const struct address_space_operations *dy_haop;
  11763. + };
  11764. +};
  11765. +
  11766. +struct au_dykey {
  11767. + union {
  11768. + struct hlist_bl_node dk_hnode;
  11769. + struct rcu_head dk_rcu;
  11770. + };
  11771. + struct au_dynop dk_op;
  11772. +
  11773. + /*
  11774. + * during I am in the branch local array, kref is gotten. when the
  11775. + * branch is removed, kref is put.
  11776. + */
  11777. + struct kref dk_kref;
  11778. +};
  11779. +
  11780. +/* stop unioning since their sizes are very different from each other */
  11781. +struct au_dyaop {
  11782. + struct au_dykey da_key;
  11783. + struct address_space_operations da_op; /* not const */
  11784. +};
  11785. +/* make sure that 'struct au_dykey *' can be any type */
  11786. +static_assert(!offsetof(struct au_dyaop, da_key));
  11787. +
  11788. +/* ---------------------------------------------------------------------- */
  11789. +
  11790. +/* dynop.c */
  11791. +struct au_branch;
  11792. +void au_dy_put(struct au_dykey *key);
  11793. +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
  11794. + struct inode *h_inode);
  11795. +int au_dy_irefresh(struct inode *inode);
  11796. +void au_dy_arefresh(int do_dio);
  11797. +
  11798. +void __init au_dy_init(void);
  11799. +void au_dy_fin(void);
  11800. +
  11801. +#endif /* __KERNEL__ */
  11802. +#endif /* __AUFS_DYNOP_H__ */
  11803. diff -Naur null/fs/aufs/export.c linux-5.1/fs/aufs/export.c
  11804. --- /dev/null
  11805. +++ linux-5.1/fs/aufs/export.c 2019-05-22 11:23:07.439141088 +0200
  11806. @@ -0,0 +1,838 @@
  11807. +// SPDX-License-Identifier: GPL-2.0
  11808. +/*
  11809. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  11810. + *
  11811. + * This program, aufs is free software; you can redistribute it and/or modify
  11812. + * it under the terms of the GNU General Public License as published by
  11813. + * the Free Software Foundation; either version 2 of the License, or
  11814. + * (at your option) any later version.
  11815. + *
  11816. + * This program is distributed in the hope that it will be useful,
  11817. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11818. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11819. + * GNU General Public License for more details.
  11820. + *
  11821. + * You should have received a copy of the GNU General Public License
  11822. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  11823. + */
  11824. +
  11825. +/*
  11826. + * export via nfs
  11827. + */
  11828. +
  11829. +#include <linux/exportfs.h>
  11830. +#include <linux/fs_struct.h>
  11831. +#include <linux/namei.h>
  11832. +#include <linux/nsproxy.h>
  11833. +#include <linux/random.h>
  11834. +#include <linux/writeback.h>
  11835. +#include "aufs.h"
  11836. +
  11837. +union conv {
  11838. +#ifdef CONFIG_AUFS_INO_T_64
  11839. + __u32 a[2];
  11840. +#else
  11841. + __u32 a[1];
  11842. +#endif
  11843. + ino_t ino;
  11844. +};
  11845. +
  11846. +static ino_t decode_ino(__u32 *a)
  11847. +{
  11848. + union conv u;
  11849. +
  11850. + BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
  11851. + u.a[0] = a[0];
  11852. +#ifdef CONFIG_AUFS_INO_T_64
  11853. + u.a[1] = a[1];
  11854. +#endif
  11855. + return u.ino;
  11856. +}
  11857. +
  11858. +static void encode_ino(__u32 *a, ino_t ino)
  11859. +{
  11860. + union conv u;
  11861. +
  11862. + u.ino = ino;
  11863. + a[0] = u.a[0];
  11864. +#ifdef CONFIG_AUFS_INO_T_64
  11865. + a[1] = u.a[1];
  11866. +#endif
  11867. +}
  11868. +
  11869. +/* NFS file handle */
  11870. +enum {
  11871. + Fh_br_id,
  11872. + Fh_sigen,
  11873. +#ifdef CONFIG_AUFS_INO_T_64
  11874. + /* support 64bit inode number */
  11875. + Fh_ino1,
  11876. + Fh_ino2,
  11877. + Fh_dir_ino1,
  11878. + Fh_dir_ino2,
  11879. +#else
  11880. + Fh_ino1,
  11881. + Fh_dir_ino1,
  11882. +#endif
  11883. + Fh_igen,
  11884. + Fh_h_type,
  11885. + Fh_tail,
  11886. +
  11887. + Fh_ino = Fh_ino1,
  11888. + Fh_dir_ino = Fh_dir_ino1
  11889. +};
  11890. +
  11891. +static int au_test_anon(struct dentry *dentry)
  11892. +{
  11893. + /* note: read d_flags without d_lock */
  11894. + return !!(dentry->d_flags & DCACHE_DISCONNECTED);
  11895. +}
  11896. +
  11897. +int au_test_nfsd(void)
  11898. +{
  11899. + int ret;
  11900. + struct task_struct *tsk = current;
  11901. + char comm[sizeof(tsk->comm)];
  11902. +
  11903. + ret = 0;
  11904. + if (tsk->flags & PF_KTHREAD) {
  11905. + get_task_comm(comm, tsk);
  11906. + ret = !strcmp(comm, "nfsd");
  11907. + }
  11908. +
  11909. + return ret;
  11910. +}
  11911. +
  11912. +/* ---------------------------------------------------------------------- */
  11913. +/* inode generation external table */
  11914. +
  11915. +void au_xigen_inc(struct inode *inode)
  11916. +{
  11917. + loff_t pos;
  11918. + ssize_t sz;
  11919. + __u32 igen;
  11920. + struct super_block *sb;
  11921. + struct au_sbinfo *sbinfo;
  11922. +
  11923. + sb = inode->i_sb;
  11924. + AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
  11925. +
  11926. + sbinfo = au_sbi(sb);
  11927. + pos = inode->i_ino;
  11928. + pos *= sizeof(igen);
  11929. + igen = inode->i_generation + 1;
  11930. + sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xigen, &igen,
  11931. + sizeof(igen), &pos);
  11932. + if (sz == sizeof(igen))
  11933. + return; /* success */
  11934. +
  11935. + if (unlikely(sz >= 0))
  11936. + AuIOErr("xigen error (%zd)\n", sz);
  11937. +}
  11938. +
  11939. +int au_xigen_new(struct inode *inode)
  11940. +{
  11941. + int err;
  11942. + loff_t pos;
  11943. + ssize_t sz;
  11944. + struct super_block *sb;
  11945. + struct au_sbinfo *sbinfo;
  11946. + struct file *file;
  11947. +
  11948. + err = 0;
  11949. + /* todo: dirty, at mount time */
  11950. + if (inode->i_ino == AUFS_ROOT_INO)
  11951. + goto out;
  11952. + sb = inode->i_sb;
  11953. + SiMustAnyLock(sb);
  11954. + if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
  11955. + goto out;
  11956. +
  11957. + err = -EFBIG;
  11958. + pos = inode->i_ino;
  11959. + if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
  11960. + AuIOErr1("too large i%lld\n", pos);
  11961. + goto out;
  11962. + }
  11963. + pos *= sizeof(inode->i_generation);
  11964. +
  11965. + err = 0;
  11966. + sbinfo = au_sbi(sb);
  11967. + file = sbinfo->si_xigen;
  11968. + BUG_ON(!file);
  11969. +
  11970. + if (vfsub_f_size_read(file)
  11971. + < pos + sizeof(inode->i_generation)) {
  11972. + inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
  11973. + sz = xino_fwrite(sbinfo->si_xwrite, file, &inode->i_generation,
  11974. + sizeof(inode->i_generation), &pos);
  11975. + } else
  11976. + sz = xino_fread(sbinfo->si_xread, file, &inode->i_generation,
  11977. + sizeof(inode->i_generation), &pos);
  11978. + if (sz == sizeof(inode->i_generation))
  11979. + goto out; /* success */
  11980. +
  11981. + err = sz;
  11982. + if (unlikely(sz >= 0)) {
  11983. + err = -EIO;
  11984. + AuIOErr("xigen error (%zd)\n", sz);
  11985. + }
  11986. +
  11987. +out:
  11988. + return err;
  11989. +}
  11990. +
  11991. +int au_xigen_set(struct super_block *sb, struct path *path)
  11992. +{
  11993. + int err;
  11994. + struct au_sbinfo *sbinfo;
  11995. + struct file *file;
  11996. +
  11997. + SiMustWriteLock(sb);
  11998. +
  11999. + sbinfo = au_sbi(sb);
  12000. + file = au_xino_create2(sb, path, sbinfo->si_xigen);
  12001. + err = PTR_ERR(file);
  12002. + if (IS_ERR(file))
  12003. + goto out;
  12004. + err = 0;
  12005. + if (sbinfo->si_xigen)
  12006. + fput(sbinfo->si_xigen);
  12007. + sbinfo->si_xigen = file;
  12008. +
  12009. +out:
  12010. + AuTraceErr(err);
  12011. + return err;
  12012. +}
  12013. +
  12014. +void au_xigen_clr(struct super_block *sb)
  12015. +{
  12016. + struct au_sbinfo *sbinfo;
  12017. +
  12018. + SiMustWriteLock(sb);
  12019. +
  12020. + sbinfo = au_sbi(sb);
  12021. + if (sbinfo->si_xigen) {
  12022. + fput(sbinfo->si_xigen);
  12023. + sbinfo->si_xigen = NULL;
  12024. + }
  12025. +}
  12026. +
  12027. +/* ---------------------------------------------------------------------- */
  12028. +
  12029. +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
  12030. + ino_t dir_ino)
  12031. +{
  12032. + struct dentry *dentry, *d;
  12033. + struct inode *inode;
  12034. + unsigned int sigen;
  12035. +
  12036. + dentry = NULL;
  12037. + inode = ilookup(sb, ino);
  12038. + if (!inode)
  12039. + goto out;
  12040. +
  12041. + dentry = ERR_PTR(-ESTALE);
  12042. + sigen = au_sigen(sb);
  12043. + if (unlikely(au_is_bad_inode(inode)
  12044. + || IS_DEADDIR(inode)
  12045. + || sigen != au_iigen(inode, NULL)))
  12046. + goto out_iput;
  12047. +
  12048. + dentry = NULL;
  12049. + if (!dir_ino || S_ISDIR(inode->i_mode))
  12050. + dentry = d_find_alias(inode);
  12051. + else {
  12052. + spin_lock(&inode->i_lock);
  12053. + hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
  12054. + spin_lock(&d->d_lock);
  12055. + if (!au_test_anon(d)
  12056. + && d_inode(d->d_parent)->i_ino == dir_ino) {
  12057. + dentry = dget_dlock(d);
  12058. + spin_unlock(&d->d_lock);
  12059. + break;
  12060. + }
  12061. + spin_unlock(&d->d_lock);
  12062. + }
  12063. + spin_unlock(&inode->i_lock);
  12064. + }
  12065. + if (unlikely(dentry && au_digen_test(dentry, sigen))) {
  12066. + /* need to refresh */
  12067. + dput(dentry);
  12068. + dentry = NULL;
  12069. + }
  12070. +
  12071. +out_iput:
  12072. + iput(inode);
  12073. +out:
  12074. + AuTraceErrPtr(dentry);
  12075. + return dentry;
  12076. +}
  12077. +
  12078. +/* ---------------------------------------------------------------------- */
  12079. +
  12080. +/* todo: dirty? */
  12081. +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
  12082. +
  12083. +struct au_compare_mnt_args {
  12084. + /* input */
  12085. + struct super_block *sb;
  12086. +
  12087. + /* output */
  12088. + struct vfsmount *mnt;
  12089. +};
  12090. +
  12091. +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
  12092. +{
  12093. + struct au_compare_mnt_args *a = arg;
  12094. +
  12095. + if (mnt->mnt_sb != a->sb)
  12096. + return 0;
  12097. + a->mnt = mntget(mnt);
  12098. + return 1;
  12099. +}
  12100. +
  12101. +static struct vfsmount *au_mnt_get(struct super_block *sb)
  12102. +{
  12103. + int err;
  12104. + struct path root;
  12105. + struct au_compare_mnt_args args = {
  12106. + .sb = sb
  12107. + };
  12108. +
  12109. + get_fs_root(current->fs, &root);
  12110. + rcu_read_lock();
  12111. + err = iterate_mounts(au_compare_mnt, &args, root.mnt);
  12112. + rcu_read_unlock();
  12113. + path_put(&root);
  12114. + AuDebugOn(!err);
  12115. + AuDebugOn(!args.mnt);
  12116. + return args.mnt;
  12117. +}
  12118. +
  12119. +struct au_nfsd_si_lock {
  12120. + unsigned int sigen;
  12121. + aufs_bindex_t bindex, br_id;
  12122. + unsigned char force_lock;
  12123. +};
  12124. +
  12125. +static int si_nfsd_read_lock(struct super_block *sb,
  12126. + struct au_nfsd_si_lock *nsi_lock)
  12127. +{
  12128. + int err;
  12129. + aufs_bindex_t bindex;
  12130. +
  12131. + si_read_lock(sb, AuLock_FLUSH);
  12132. +
  12133. + /* branch id may be wrapped around */
  12134. + err = 0;
  12135. + bindex = au_br_index(sb, nsi_lock->br_id);
  12136. + if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
  12137. + goto out; /* success */
  12138. +
  12139. + err = -ESTALE;
  12140. + bindex = -1;
  12141. + if (!nsi_lock->force_lock)
  12142. + si_read_unlock(sb);
  12143. +
  12144. +out:
  12145. + nsi_lock->bindex = bindex;
  12146. + return err;
  12147. +}
  12148. +
  12149. +struct find_name_by_ino {
  12150. + struct dir_context ctx;
  12151. + int called, found;
  12152. + ino_t ino;
  12153. + char *name;
  12154. + int namelen;
  12155. +};
  12156. +
  12157. +static int
  12158. +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
  12159. + loff_t offset, u64 ino, unsigned int d_type)
  12160. +{
  12161. + struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
  12162. + ctx);
  12163. +
  12164. + a->called++;
  12165. + if (a->ino != ino)
  12166. + return 0;
  12167. +
  12168. + memcpy(a->name, name, namelen);
  12169. + a->namelen = namelen;
  12170. + a->found = 1;
  12171. + return 1;
  12172. +}
  12173. +
  12174. +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
  12175. + struct au_nfsd_si_lock *nsi_lock)
  12176. +{
  12177. + struct dentry *dentry, *parent;
  12178. + struct file *file;
  12179. + struct inode *dir;
  12180. + struct find_name_by_ino arg = {
  12181. + .ctx = {
  12182. + .actor = find_name_by_ino
  12183. + }
  12184. + };
  12185. + int err;
  12186. +
  12187. + parent = path->dentry;
  12188. + if (nsi_lock)
  12189. + si_read_unlock(parent->d_sb);
  12190. + file = vfsub_dentry_open(path, au_dir_roflags);
  12191. + dentry = (void *)file;
  12192. + if (IS_ERR(file))
  12193. + goto out;
  12194. +
  12195. + dentry = ERR_PTR(-ENOMEM);
  12196. + arg.name = (void *)__get_free_page(GFP_NOFS);
  12197. + if (unlikely(!arg.name))
  12198. + goto out_file;
  12199. + arg.ino = ino;
  12200. + arg.found = 0;
  12201. + do {
  12202. + arg.called = 0;
  12203. + /* smp_mb(); */
  12204. + err = vfsub_iterate_dir(file, &arg.ctx);
  12205. + } while (!err && !arg.found && arg.called);
  12206. + dentry = ERR_PTR(err);
  12207. + if (unlikely(err))
  12208. + goto out_name;
  12209. + /* instead of ENOENT */
  12210. + dentry = ERR_PTR(-ESTALE);
  12211. + if (!arg.found)
  12212. + goto out_name;
  12213. +
  12214. + /* do not call vfsub_lkup_one() */
  12215. + dir = d_inode(parent);
  12216. + dentry = vfsub_lookup_one_len_unlocked(arg.name, parent, arg.namelen);
  12217. + AuTraceErrPtr(dentry);
  12218. + if (IS_ERR(dentry))
  12219. + goto out_name;
  12220. + AuDebugOn(au_test_anon(dentry));
  12221. + if (unlikely(d_really_is_negative(dentry))) {
  12222. + dput(dentry);
  12223. + dentry = ERR_PTR(-ENOENT);
  12224. + }
  12225. +
  12226. +out_name:
  12227. + free_page((unsigned long)arg.name);
  12228. +out_file:
  12229. + fput(file);
  12230. +out:
  12231. + if (unlikely(nsi_lock
  12232. + && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
  12233. + if (!IS_ERR(dentry)) {
  12234. + dput(dentry);
  12235. + dentry = ERR_PTR(-ESTALE);
  12236. + }
  12237. + AuTraceErrPtr(dentry);
  12238. + return dentry;
  12239. +}
  12240. +
  12241. +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
  12242. + ino_t dir_ino,
  12243. + struct au_nfsd_si_lock *nsi_lock)
  12244. +{
  12245. + struct dentry *dentry;
  12246. + struct path path;
  12247. +
  12248. + if (dir_ino != AUFS_ROOT_INO) {
  12249. + path.dentry = decode_by_ino(sb, dir_ino, 0);
  12250. + dentry = path.dentry;
  12251. + if (!path.dentry || IS_ERR(path.dentry))
  12252. + goto out;
  12253. + AuDebugOn(au_test_anon(path.dentry));
  12254. + } else
  12255. + path.dentry = dget(sb->s_root);
  12256. +
  12257. + path.mnt = au_mnt_get(sb);
  12258. + dentry = au_lkup_by_ino(&path, ino, nsi_lock);
  12259. + path_put(&path);
  12260. +
  12261. +out:
  12262. + AuTraceErrPtr(dentry);
  12263. + return dentry;
  12264. +}
  12265. +
  12266. +/* ---------------------------------------------------------------------- */
  12267. +
  12268. +static int h_acceptable(void *expv, struct dentry *dentry)
  12269. +{
  12270. + return 1;
  12271. +}
  12272. +
  12273. +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
  12274. + char *buf, int len, struct super_block *sb)
  12275. +{
  12276. + char *p;
  12277. + int n;
  12278. + struct path path;
  12279. +
  12280. + p = d_path(h_rootpath, buf, len);
  12281. + if (IS_ERR(p))
  12282. + goto out;
  12283. + n = strlen(p);
  12284. +
  12285. + path.mnt = h_rootpath->mnt;
  12286. + path.dentry = h_parent;
  12287. + p = d_path(&path, buf, len);
  12288. + if (IS_ERR(p))
  12289. + goto out;
  12290. + if (n != 1)
  12291. + p += n;
  12292. +
  12293. + path.mnt = au_mnt_get(sb);
  12294. + path.dentry = sb->s_root;
  12295. + p = d_path(&path, buf, len - strlen(p));
  12296. + mntput(path.mnt);
  12297. + if (IS_ERR(p))
  12298. + goto out;
  12299. + if (n != 1)
  12300. + p[strlen(p)] = '/';
  12301. +
  12302. +out:
  12303. + AuTraceErrPtr(p);
  12304. + return p;
  12305. +}
  12306. +
  12307. +static
  12308. +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
  12309. + int fh_len, struct au_nfsd_si_lock *nsi_lock)
  12310. +{
  12311. + struct dentry *dentry, *h_parent, *root;
  12312. + struct super_block *h_sb;
  12313. + char *pathname, *p;
  12314. + struct vfsmount *h_mnt;
  12315. + struct au_branch *br;
  12316. + int err;
  12317. + struct path path;
  12318. +
  12319. + br = au_sbr(sb, nsi_lock->bindex);
  12320. + h_mnt = au_br_mnt(br);
  12321. + h_sb = h_mnt->mnt_sb;
  12322. + /* todo: call lower fh_to_dentry()? fh_to_parent()? */
  12323. + lockdep_off();
  12324. + h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
  12325. + fh_len - Fh_tail, fh[Fh_h_type],
  12326. + h_acceptable, /*context*/NULL);
  12327. + lockdep_on();
  12328. + dentry = h_parent;
  12329. + if (unlikely(!h_parent || IS_ERR(h_parent))) {
  12330. + AuWarn1("%s decode_fh failed, %ld\n",
  12331. + au_sbtype(h_sb), PTR_ERR(h_parent));
  12332. + goto out;
  12333. + }
  12334. + dentry = NULL;
  12335. + if (unlikely(au_test_anon(h_parent))) {
  12336. + AuWarn1("%s decode_fh returned a disconnected dentry\n",
  12337. + au_sbtype(h_sb));
  12338. + goto out_h_parent;
  12339. + }
  12340. +
  12341. + dentry = ERR_PTR(-ENOMEM);
  12342. + pathname = (void *)__get_free_page(GFP_NOFS);
  12343. + if (unlikely(!pathname))
  12344. + goto out_h_parent;
  12345. +
  12346. + root = sb->s_root;
  12347. + path.mnt = h_mnt;
  12348. + di_read_lock_parent(root, !AuLock_IR);
  12349. + path.dentry = au_h_dptr(root, nsi_lock->bindex);
  12350. + di_read_unlock(root, !AuLock_IR);
  12351. + p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
  12352. + dentry = (void *)p;
  12353. + if (IS_ERR(p))
  12354. + goto out_pathname;
  12355. +
  12356. + si_read_unlock(sb);
  12357. + err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
  12358. + dentry = ERR_PTR(err);
  12359. + if (unlikely(err))
  12360. + goto out_relock;
  12361. +
  12362. + dentry = ERR_PTR(-ENOENT);
  12363. + AuDebugOn(au_test_anon(path.dentry));
  12364. + if (unlikely(d_really_is_negative(path.dentry)))
  12365. + goto out_path;
  12366. +
  12367. + if (ino != d_inode(path.dentry)->i_ino)
  12368. + dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
  12369. + else
  12370. + dentry = dget(path.dentry);
  12371. +
  12372. +out_path:
  12373. + path_put(&path);
  12374. +out_relock:
  12375. + if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
  12376. + if (!IS_ERR(dentry)) {
  12377. + dput(dentry);
  12378. + dentry = ERR_PTR(-ESTALE);
  12379. + }
  12380. +out_pathname:
  12381. + free_page((unsigned long)pathname);
  12382. +out_h_parent:
  12383. + dput(h_parent);
  12384. +out:
  12385. + AuTraceErrPtr(dentry);
  12386. + return dentry;
  12387. +}
  12388. +
  12389. +/* ---------------------------------------------------------------------- */
  12390. +
  12391. +static struct dentry *
  12392. +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
  12393. + int fh_type)
  12394. +{
  12395. + struct dentry *dentry;
  12396. + __u32 *fh = fid->raw;
  12397. + struct au_branch *br;
  12398. + ino_t ino, dir_ino;
  12399. + struct au_nfsd_si_lock nsi_lock = {
  12400. + .force_lock = 0
  12401. + };
  12402. +
  12403. + dentry = ERR_PTR(-ESTALE);
  12404. + /* it should never happen, but the file handle is unreliable */
  12405. + if (unlikely(fh_len < Fh_tail))
  12406. + goto out;
  12407. + nsi_lock.sigen = fh[Fh_sigen];
  12408. + nsi_lock.br_id = fh[Fh_br_id];
  12409. +
  12410. + /* branch id may be wrapped around */
  12411. + br = NULL;
  12412. + if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
  12413. + goto out;
  12414. + nsi_lock.force_lock = 1;
  12415. +
  12416. + /* is this inode still cached? */
  12417. + ino = decode_ino(fh + Fh_ino);
  12418. + /* it should never happen */
  12419. + if (unlikely(ino == AUFS_ROOT_INO))
  12420. + goto out_unlock;
  12421. +
  12422. + dir_ino = decode_ino(fh + Fh_dir_ino);
  12423. + dentry = decode_by_ino(sb, ino, dir_ino);
  12424. + if (IS_ERR(dentry))
  12425. + goto out_unlock;
  12426. + if (dentry)
  12427. + goto accept;
  12428. +
  12429. + /* is the parent dir cached? */
  12430. + br = au_sbr(sb, nsi_lock.bindex);
  12431. + au_lcnt_inc(&br->br_nfiles);
  12432. + dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
  12433. + if (IS_ERR(dentry))
  12434. + goto out_unlock;
  12435. + if (dentry)
  12436. + goto accept;
  12437. +
  12438. + /* lookup path */
  12439. + dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
  12440. + if (IS_ERR(dentry))
  12441. + goto out_unlock;
  12442. + if (unlikely(!dentry))
  12443. + /* todo?: make it ESTALE */
  12444. + goto out_unlock;
  12445. +
  12446. +accept:
  12447. + if (!au_digen_test(dentry, au_sigen(sb))
  12448. + && d_inode(dentry)->i_generation == fh[Fh_igen])
  12449. + goto out_unlock; /* success */
  12450. +
  12451. + dput(dentry);
  12452. + dentry = ERR_PTR(-ESTALE);
  12453. +out_unlock:
  12454. + if (br)
  12455. + au_lcnt_dec(&br->br_nfiles);
  12456. + si_read_unlock(sb);
  12457. +out:
  12458. + AuTraceErrPtr(dentry);
  12459. + return dentry;
  12460. +}
  12461. +
  12462. +#if 0 /* reserved for future use */
  12463. +/* support subtreecheck option */
  12464. +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
  12465. + int fh_len, int fh_type)
  12466. +{
  12467. + struct dentry *parent;
  12468. + __u32 *fh = fid->raw;
  12469. + ino_t dir_ino;
  12470. +
  12471. + dir_ino = decode_ino(fh + Fh_dir_ino);
  12472. + parent = decode_by_ino(sb, dir_ino, 0);
  12473. + if (IS_ERR(parent))
  12474. + goto out;
  12475. + if (!parent)
  12476. + parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
  12477. + dir_ino, fh, fh_len);
  12478. +
  12479. +out:
  12480. + AuTraceErrPtr(parent);
  12481. + return parent;
  12482. +}
  12483. +#endif
  12484. +
  12485. +/* ---------------------------------------------------------------------- */
  12486. +
  12487. +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
  12488. + struct inode *dir)
  12489. +{
  12490. + int err;
  12491. + aufs_bindex_t bindex;
  12492. + struct super_block *sb, *h_sb;
  12493. + struct dentry *dentry, *parent, *h_parent;
  12494. + struct inode *h_dir;
  12495. + struct au_branch *br;
  12496. +
  12497. + err = -ENOSPC;
  12498. + if (unlikely(*max_len <= Fh_tail)) {
  12499. + AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
  12500. + goto out;
  12501. + }
  12502. +
  12503. + err = FILEID_ROOT;
  12504. + if (inode->i_ino == AUFS_ROOT_INO) {
  12505. + AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
  12506. + goto out;
  12507. + }
  12508. +
  12509. + h_parent = NULL;
  12510. + sb = inode->i_sb;
  12511. + err = si_read_lock(sb, AuLock_FLUSH);
  12512. + if (unlikely(err))
  12513. + goto out;
  12514. +
  12515. +#ifdef CONFIG_AUFS_DEBUG
  12516. + if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
  12517. + AuWarn1("NFS-exporting requires xino\n");
  12518. +#endif
  12519. + err = -EIO;
  12520. + parent = NULL;
  12521. + ii_read_lock_child(inode);
  12522. + bindex = au_ibtop(inode);
  12523. + if (!dir) {
  12524. + dentry = d_find_any_alias(inode);
  12525. + if (unlikely(!dentry))
  12526. + goto out_unlock;
  12527. + AuDebugOn(au_test_anon(dentry));
  12528. + parent = dget_parent(dentry);
  12529. + dput(dentry);
  12530. + if (unlikely(!parent))
  12531. + goto out_unlock;
  12532. + if (d_really_is_positive(parent))
  12533. + dir = d_inode(parent);
  12534. + }
  12535. +
  12536. + ii_read_lock_parent(dir);
  12537. + h_dir = au_h_iptr(dir, bindex);
  12538. + ii_read_unlock(dir);
  12539. + if (unlikely(!h_dir))
  12540. + goto out_parent;
  12541. + h_parent = d_find_any_alias(h_dir);
  12542. + if (unlikely(!h_parent))
  12543. + goto out_hparent;
  12544. +
  12545. + err = -EPERM;
  12546. + br = au_sbr(sb, bindex);
  12547. + h_sb = au_br_sb(br);
  12548. + if (unlikely(!h_sb->s_export_op)) {
  12549. + AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
  12550. + goto out_hparent;
  12551. + }
  12552. +
  12553. + fh[Fh_br_id] = br->br_id;
  12554. + fh[Fh_sigen] = au_sigen(sb);
  12555. + encode_ino(fh + Fh_ino, inode->i_ino);
  12556. + encode_ino(fh + Fh_dir_ino, dir->i_ino);
  12557. + fh[Fh_igen] = inode->i_generation;
  12558. +
  12559. + *max_len -= Fh_tail;
  12560. + fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
  12561. + max_len,
  12562. + /*connectable or subtreecheck*/0);
  12563. + err = fh[Fh_h_type];
  12564. + *max_len += Fh_tail;
  12565. + /* todo: macros? */
  12566. + if (err != FILEID_INVALID)
  12567. + err = 99;
  12568. + else
  12569. + AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
  12570. +
  12571. +out_hparent:
  12572. + dput(h_parent);
  12573. +out_parent:
  12574. + dput(parent);
  12575. +out_unlock:
  12576. + ii_read_unlock(inode);
  12577. + si_read_unlock(sb);
  12578. +out:
  12579. + if (unlikely(err < 0))
  12580. + err = FILEID_INVALID;
  12581. + return err;
  12582. +}
  12583. +
  12584. +/* ---------------------------------------------------------------------- */
  12585. +
  12586. +static int aufs_commit_metadata(struct inode *inode)
  12587. +{
  12588. + int err;
  12589. + aufs_bindex_t bindex;
  12590. + struct super_block *sb;
  12591. + struct inode *h_inode;
  12592. + int (*f)(struct inode *inode);
  12593. +
  12594. + sb = inode->i_sb;
  12595. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  12596. + ii_write_lock_child(inode);
  12597. + bindex = au_ibtop(inode);
  12598. + AuDebugOn(bindex < 0);
  12599. + h_inode = au_h_iptr(inode, bindex);
  12600. +
  12601. + f = h_inode->i_sb->s_export_op->commit_metadata;
  12602. + if (f)
  12603. + err = f(h_inode);
  12604. + else {
  12605. + struct writeback_control wbc = {
  12606. + .sync_mode = WB_SYNC_ALL,
  12607. + .nr_to_write = 0 /* metadata only */
  12608. + };
  12609. +
  12610. + err = sync_inode(h_inode, &wbc);
  12611. + }
  12612. +
  12613. + au_cpup_attr_timesizes(inode);
  12614. + ii_write_unlock(inode);
  12615. + si_read_unlock(sb);
  12616. + return err;
  12617. +}
  12618. +
  12619. +/* ---------------------------------------------------------------------- */
  12620. +
  12621. +static struct export_operations aufs_export_op = {
  12622. + .fh_to_dentry = aufs_fh_to_dentry,
  12623. + /* .fh_to_parent = aufs_fh_to_parent, */
  12624. + .encode_fh = aufs_encode_fh,
  12625. + .commit_metadata = aufs_commit_metadata
  12626. +};
  12627. +
  12628. +void au_export_init(struct super_block *sb)
  12629. +{
  12630. + struct au_sbinfo *sbinfo;
  12631. + __u32 u;
  12632. +
  12633. + BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
  12634. + && IS_MODULE(CONFIG_EXPORTFS),
  12635. + AUFS_NAME ": unsupported configuration "
  12636. + "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
  12637. +
  12638. + sb->s_export_op = &aufs_export_op;
  12639. + sbinfo = au_sbi(sb);
  12640. + sbinfo->si_xigen = NULL;
  12641. + get_random_bytes(&u, sizeof(u));
  12642. + BUILD_BUG_ON(sizeof(u) != sizeof(int));
  12643. + atomic_set(&sbinfo->si_xigen_next, u);
  12644. +}
  12645. diff -Naur null/fs/aufs/fhsm.c linux-5.1/fs/aufs/fhsm.c
  12646. --- /dev/null
  12647. +++ linux-5.1/fs/aufs/fhsm.c 2019-05-22 11:23:07.440141042 +0200
  12648. @@ -0,0 +1,427 @@
  12649. +// SPDX-License-Identifier: GPL-2.0
  12650. +/*
  12651. + * Copyright (C) 2011-2019 Junjiro R. Okajima
  12652. + *
  12653. + * This program, aufs is free software; you can redistribute it and/or modify
  12654. + * it under the terms of the GNU General Public License as published by
  12655. + * the Free Software Foundation; either version 2 of the License, or
  12656. + * (at your option) any later version.
  12657. + *
  12658. + * This program is distributed in the hope that it will be useful,
  12659. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12660. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12661. + * GNU General Public License for more details.
  12662. + *
  12663. + * You should have received a copy of the GNU General Public License
  12664. + * along with this program; if not, write to the Free Software
  12665. + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  12666. + */
  12667. +
  12668. +/*
  12669. + * File-based Hierarchy Storage Management
  12670. + */
  12671. +
  12672. +#include <linux/anon_inodes.h>
  12673. +#include <linux/poll.h>
  12674. +#include <linux/seq_file.h>
  12675. +#include <linux/statfs.h>
  12676. +#include "aufs.h"
  12677. +
  12678. +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
  12679. +{
  12680. + struct au_sbinfo *sbinfo;
  12681. + struct au_fhsm *fhsm;
  12682. +
  12683. + SiMustAnyLock(sb);
  12684. +
  12685. + sbinfo = au_sbi(sb);
  12686. + fhsm = &sbinfo->si_fhsm;
  12687. + AuDebugOn(!fhsm);
  12688. + return fhsm->fhsm_bottom;
  12689. +}
  12690. +
  12691. +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
  12692. +{
  12693. + struct au_sbinfo *sbinfo;
  12694. + struct au_fhsm *fhsm;
  12695. +
  12696. + SiMustWriteLock(sb);
  12697. +
  12698. + sbinfo = au_sbi(sb);
  12699. + fhsm = &sbinfo->si_fhsm;
  12700. + AuDebugOn(!fhsm);
  12701. + fhsm->fhsm_bottom = bindex;
  12702. +}
  12703. +
  12704. +/* ---------------------------------------------------------------------- */
  12705. +
  12706. +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
  12707. +{
  12708. + struct au_br_fhsm *bf;
  12709. +
  12710. + bf = br->br_fhsm;
  12711. + MtxMustLock(&bf->bf_lock);
  12712. +
  12713. + return !bf->bf_readable
  12714. + || time_after(jiffies,
  12715. + bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
  12716. +}
  12717. +
  12718. +/* ---------------------------------------------------------------------- */
  12719. +
  12720. +static void au_fhsm_notify(struct super_block *sb, int val)
  12721. +{
  12722. + struct au_sbinfo *sbinfo;
  12723. + struct au_fhsm *fhsm;
  12724. +
  12725. + SiMustAnyLock(sb);
  12726. +
  12727. + sbinfo = au_sbi(sb);
  12728. + fhsm = &sbinfo->si_fhsm;
  12729. + if (au_fhsm_pid(fhsm)
  12730. + && atomic_read(&fhsm->fhsm_readable) != -1) {
  12731. + atomic_set(&fhsm->fhsm_readable, val);
  12732. + if (val)
  12733. + wake_up(&fhsm->fhsm_wqh);
  12734. + }
  12735. +}
  12736. +
  12737. +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
  12738. + struct aufs_stfs *rstfs, int do_lock, int do_notify)
  12739. +{
  12740. + int err;
  12741. + struct au_branch *br;
  12742. + struct au_br_fhsm *bf;
  12743. +
  12744. + br = au_sbr(sb, bindex);
  12745. + AuDebugOn(au_br_rdonly(br));
  12746. + bf = br->br_fhsm;
  12747. + AuDebugOn(!bf);
  12748. +
  12749. + if (do_lock)
  12750. + mutex_lock(&bf->bf_lock);
  12751. + else
  12752. + MtxMustLock(&bf->bf_lock);
  12753. +
  12754. + /* sb->s_root for NFS is unreliable */
  12755. + err = au_br_stfs(br, &bf->bf_stfs);
  12756. + if (unlikely(err)) {
  12757. + AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
  12758. + goto out;
  12759. + }
  12760. +
  12761. + bf->bf_jiffy = jiffies;
  12762. + bf->bf_readable = 1;
  12763. + if (do_notify)
  12764. + au_fhsm_notify(sb, /*val*/1);
  12765. + if (rstfs)
  12766. + *rstfs = bf->bf_stfs;
  12767. +
  12768. +out:
  12769. + if (do_lock)
  12770. + mutex_unlock(&bf->bf_lock);
  12771. + au_fhsm_notify(sb, /*val*/1);
  12772. +
  12773. + return err;
  12774. +}
  12775. +
  12776. +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
  12777. +{
  12778. + int err;
  12779. + struct au_sbinfo *sbinfo;
  12780. + struct au_fhsm *fhsm;
  12781. + struct au_branch *br;
  12782. + struct au_br_fhsm *bf;
  12783. +
  12784. + AuDbg("b%d, force %d\n", bindex, force);
  12785. + SiMustAnyLock(sb);
  12786. +
  12787. + sbinfo = au_sbi(sb);
  12788. + fhsm = &sbinfo->si_fhsm;
  12789. + if (!au_ftest_si(sbinfo, FHSM)
  12790. + || fhsm->fhsm_bottom == bindex)
  12791. + return;
  12792. +
  12793. + br = au_sbr(sb, bindex);
  12794. + bf = br->br_fhsm;
  12795. + AuDebugOn(!bf);
  12796. + mutex_lock(&bf->bf_lock);
  12797. + if (force
  12798. + || au_fhsm_pid(fhsm)
  12799. + || au_fhsm_test_jiffy(sbinfo, br))
  12800. + err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
  12801. + /*do_notify*/1);
  12802. + mutex_unlock(&bf->bf_lock);
  12803. +}
  12804. +
  12805. +void au_fhsm_wrote_all(struct super_block *sb, int force)
  12806. +{
  12807. + aufs_bindex_t bindex, bbot;
  12808. + struct au_branch *br;
  12809. +
  12810. + /* exclude the bottom */
  12811. + bbot = au_fhsm_bottom(sb);
  12812. + for (bindex = 0; bindex < bbot; bindex++) {
  12813. + br = au_sbr(sb, bindex);
  12814. + if (au_br_fhsm(br->br_perm))
  12815. + au_fhsm_wrote(sb, bindex, force);
  12816. + }
  12817. +}
  12818. +
  12819. +/* ---------------------------------------------------------------------- */
  12820. +
  12821. +static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
  12822. +{
  12823. + __poll_t mask;
  12824. + struct au_sbinfo *sbinfo;
  12825. + struct au_fhsm *fhsm;
  12826. +
  12827. + mask = 0;
  12828. + sbinfo = file->private_data;
  12829. + fhsm = &sbinfo->si_fhsm;
  12830. + poll_wait(file, &fhsm->fhsm_wqh, wait);
  12831. + if (atomic_read(&fhsm->fhsm_readable))
  12832. + mask = EPOLLIN /* | EPOLLRDNORM */;
  12833. +
  12834. + if (!mask)
  12835. + AuDbg("mask 0x%x\n", mask);
  12836. + return mask;
  12837. +}
  12838. +
  12839. +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
  12840. + struct aufs_stfs *stfs, __s16 brid)
  12841. +{
  12842. + int err;
  12843. +
  12844. + err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
  12845. + if (!err)
  12846. + err = __put_user(brid, &stbr->brid);
  12847. + if (unlikely(err))
  12848. + err = -EFAULT;
  12849. +
  12850. + return err;
  12851. +}
  12852. +
  12853. +static ssize_t au_fhsm_do_read(struct super_block *sb,
  12854. + struct aufs_stbr __user *stbr, size_t count)
  12855. +{
  12856. + ssize_t err;
  12857. + int nstbr;
  12858. + aufs_bindex_t bindex, bbot;
  12859. + struct au_branch *br;
  12860. + struct au_br_fhsm *bf;
  12861. +
  12862. + /* except the bottom branch */
  12863. + err = 0;
  12864. + nstbr = 0;
  12865. + bbot = au_fhsm_bottom(sb);
  12866. + for (bindex = 0; !err && bindex < bbot; bindex++) {
  12867. + br = au_sbr(sb, bindex);
  12868. + if (!au_br_fhsm(br->br_perm))
  12869. + continue;
  12870. +
  12871. + bf = br->br_fhsm;
  12872. + mutex_lock(&bf->bf_lock);
  12873. + if (bf->bf_readable) {
  12874. + err = -EFAULT;
  12875. + if (count >= sizeof(*stbr))
  12876. + err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
  12877. + br->br_id);
  12878. + if (!err) {
  12879. + bf->bf_readable = 0;
  12880. + count -= sizeof(*stbr);
  12881. + nstbr++;
  12882. + }
  12883. + }
  12884. + mutex_unlock(&bf->bf_lock);
  12885. + }
  12886. + if (!err)
  12887. + err = sizeof(*stbr) * nstbr;
  12888. +
  12889. + return err;
  12890. +}
  12891. +
  12892. +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
  12893. + loff_t *pos)
  12894. +{
  12895. + ssize_t err;
  12896. + int readable;
  12897. + aufs_bindex_t nfhsm, bindex, bbot;
  12898. + struct au_sbinfo *sbinfo;
  12899. + struct au_fhsm *fhsm;
  12900. + struct au_branch *br;
  12901. + struct super_block *sb;
  12902. +
  12903. + err = 0;
  12904. + sbinfo = file->private_data;
  12905. + fhsm = &sbinfo->si_fhsm;
  12906. +need_data:
  12907. + spin_lock_irq(&fhsm->fhsm_wqh.lock);
  12908. + if (!atomic_read(&fhsm->fhsm_readable)) {
  12909. + if (vfsub_file_flags(file) & O_NONBLOCK)
  12910. + err = -EAGAIN;
  12911. + else
  12912. + err = wait_event_interruptible_locked_irq
  12913. + (fhsm->fhsm_wqh,
  12914. + atomic_read(&fhsm->fhsm_readable));
  12915. + }
  12916. + spin_unlock_irq(&fhsm->fhsm_wqh.lock);
  12917. + if (unlikely(err))
  12918. + goto out;
  12919. +
  12920. + /* sb may already be dead */
  12921. + au_rw_read_lock(&sbinfo->si_rwsem);
  12922. + readable = atomic_read(&fhsm->fhsm_readable);
  12923. + if (readable > 0) {
  12924. + sb = sbinfo->si_sb;
  12925. + AuDebugOn(!sb);
  12926. + /* exclude the bottom branch */
  12927. + nfhsm = 0;
  12928. + bbot = au_fhsm_bottom(sb);
  12929. + for (bindex = 0; bindex < bbot; bindex++) {
  12930. + br = au_sbr(sb, bindex);
  12931. + if (au_br_fhsm(br->br_perm))
  12932. + nfhsm++;
  12933. + }
  12934. + err = -EMSGSIZE;
  12935. + if (nfhsm * sizeof(struct aufs_stbr) <= count) {
  12936. + atomic_set(&fhsm->fhsm_readable, 0);
  12937. + err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
  12938. + count);
  12939. + }
  12940. + }
  12941. + au_rw_read_unlock(&sbinfo->si_rwsem);
  12942. + if (!readable)
  12943. + goto need_data;
  12944. +
  12945. +out:
  12946. + return err;
  12947. +}
  12948. +
  12949. +static int au_fhsm_release(struct inode *inode, struct file *file)
  12950. +{
  12951. + struct au_sbinfo *sbinfo;
  12952. + struct au_fhsm *fhsm;
  12953. +
  12954. + /* sb may already be dead */
  12955. + sbinfo = file->private_data;
  12956. + fhsm = &sbinfo->si_fhsm;
  12957. + spin_lock(&fhsm->fhsm_spin);
  12958. + fhsm->fhsm_pid = 0;
  12959. + spin_unlock(&fhsm->fhsm_spin);
  12960. + kobject_put(&sbinfo->si_kobj);
  12961. +
  12962. + return 0;
  12963. +}
  12964. +
  12965. +static const struct file_operations au_fhsm_fops = {
  12966. + .owner = THIS_MODULE,
  12967. + .llseek = noop_llseek,
  12968. + .read = au_fhsm_read,
  12969. + .poll = au_fhsm_poll,
  12970. + .release = au_fhsm_release
  12971. +};
  12972. +
  12973. +int au_fhsm_fd(struct super_block *sb, int oflags)
  12974. +{
  12975. + int err, fd;
  12976. + struct au_sbinfo *sbinfo;
  12977. + struct au_fhsm *fhsm;
  12978. +
  12979. + err = -EPERM;
  12980. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  12981. + goto out;
  12982. +
  12983. + err = -EINVAL;
  12984. + if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
  12985. + goto out;
  12986. +
  12987. + err = 0;
  12988. + sbinfo = au_sbi(sb);
  12989. + fhsm = &sbinfo->si_fhsm;
  12990. + spin_lock(&fhsm->fhsm_spin);
  12991. + if (!fhsm->fhsm_pid)
  12992. + fhsm->fhsm_pid = current->pid;
  12993. + else
  12994. + err = -EBUSY;
  12995. + spin_unlock(&fhsm->fhsm_spin);
  12996. + if (unlikely(err))
  12997. + goto out;
  12998. +
  12999. + oflags |= O_RDONLY;
  13000. + /* oflags |= FMODE_NONOTIFY; */
  13001. + fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
  13002. + err = fd;
  13003. + if (unlikely(fd < 0))
  13004. + goto out_pid;
  13005. +
  13006. + /* succeed regardless 'fhsm' status */
  13007. + kobject_get(&sbinfo->si_kobj);
  13008. + si_noflush_read_lock(sb);
  13009. + if (au_ftest_si(sbinfo, FHSM))
  13010. + au_fhsm_wrote_all(sb, /*force*/0);
  13011. + si_read_unlock(sb);
  13012. + goto out; /* success */
  13013. +
  13014. +out_pid:
  13015. + spin_lock(&fhsm->fhsm_spin);
  13016. + fhsm->fhsm_pid = 0;
  13017. + spin_unlock(&fhsm->fhsm_spin);
  13018. +out:
  13019. + AuTraceErr(err);
  13020. + return err;
  13021. +}
  13022. +
  13023. +/* ---------------------------------------------------------------------- */
  13024. +
  13025. +int au_fhsm_br_alloc(struct au_branch *br)
  13026. +{
  13027. + int err;
  13028. +
  13029. + err = 0;
  13030. + br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
  13031. + if (br->br_fhsm)
  13032. + au_br_fhsm_init(br->br_fhsm);
  13033. + else
  13034. + err = -ENOMEM;
  13035. +
  13036. + return err;
  13037. +}
  13038. +
  13039. +/* ---------------------------------------------------------------------- */
  13040. +
  13041. +void au_fhsm_fin(struct super_block *sb)
  13042. +{
  13043. + au_fhsm_notify(sb, /*val*/-1);
  13044. +}
  13045. +
  13046. +void au_fhsm_init(struct au_sbinfo *sbinfo)
  13047. +{
  13048. + struct au_fhsm *fhsm;
  13049. +
  13050. + fhsm = &sbinfo->si_fhsm;
  13051. + spin_lock_init(&fhsm->fhsm_spin);
  13052. + init_waitqueue_head(&fhsm->fhsm_wqh);
  13053. + atomic_set(&fhsm->fhsm_readable, 0);
  13054. + fhsm->fhsm_expire
  13055. + = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
  13056. + fhsm->fhsm_bottom = -1;
  13057. +}
  13058. +
  13059. +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
  13060. +{
  13061. + sbinfo->si_fhsm.fhsm_expire
  13062. + = msecs_to_jiffies(sec * MSEC_PER_SEC);
  13063. +}
  13064. +
  13065. +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
  13066. +{
  13067. + unsigned int u;
  13068. +
  13069. + if (!au_ftest_si(sbinfo, FHSM))
  13070. + return;
  13071. +
  13072. + u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
  13073. + if (u != AUFS_FHSM_CACHE_DEF_SEC)
  13074. + seq_printf(seq, ",fhsm_sec=%u", u);
  13075. +}
  13076. diff -Naur null/fs/aufs/file.c linux-5.1/fs/aufs/file.c
  13077. --- /dev/null
  13078. +++ linux-5.1/fs/aufs/file.c 2019-05-22 11:23:07.440141042 +0200
  13079. @@ -0,0 +1,863 @@
  13080. +// SPDX-License-Identifier: GPL-2.0
  13081. +/*
  13082. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  13083. + *
  13084. + * This program, aufs is free software; you can redistribute it and/or modify
  13085. + * it under the terms of the GNU General Public License as published by
  13086. + * the Free Software Foundation; either version 2 of the License, or
  13087. + * (at your option) any later version.
  13088. + *
  13089. + * This program is distributed in the hope that it will be useful,
  13090. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13091. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13092. + * GNU General Public License for more details.
  13093. + *
  13094. + * You should have received a copy of the GNU General Public License
  13095. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  13096. + */
  13097. +
  13098. +/*
  13099. + * handling file/dir, and address_space operation
  13100. + */
  13101. +
  13102. +#ifdef CONFIG_AUFS_DEBUG
  13103. +#include <linux/migrate.h>
  13104. +#endif
  13105. +#include <linux/pagemap.h>
  13106. +#include "aufs.h"
  13107. +
  13108. +/* drop flags for writing */
  13109. +unsigned int au_file_roflags(unsigned int flags)
  13110. +{
  13111. + flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
  13112. + flags |= O_RDONLY | O_NOATIME;
  13113. + return flags;
  13114. +}
  13115. +
  13116. +/* common functions to regular file and dir */
  13117. +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
  13118. + struct file *file, int force_wr)
  13119. +{
  13120. + struct file *h_file;
  13121. + struct dentry *h_dentry;
  13122. + struct inode *h_inode;
  13123. + struct super_block *sb;
  13124. + struct au_branch *br;
  13125. + struct path h_path;
  13126. + int err;
  13127. +
  13128. + /* a race condition can happen between open and unlink/rmdir */
  13129. + h_file = ERR_PTR(-ENOENT);
  13130. + h_dentry = au_h_dptr(dentry, bindex);
  13131. + if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
  13132. + goto out;
  13133. + h_inode = d_inode(h_dentry);
  13134. + spin_lock(&h_dentry->d_lock);
  13135. + err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
  13136. + /* || !d_inode(dentry)->i_nlink */
  13137. + ;
  13138. + spin_unlock(&h_dentry->d_lock);
  13139. + if (unlikely(err))
  13140. + goto out;
  13141. +
  13142. + sb = dentry->d_sb;
  13143. + br = au_sbr(sb, bindex);
  13144. + err = au_br_test_oflag(flags, br);
  13145. + h_file = ERR_PTR(err);
  13146. + if (unlikely(err))
  13147. + goto out;
  13148. +
  13149. + /* drop flags for writing */
  13150. + if (au_test_ro(sb, bindex, d_inode(dentry))) {
  13151. + if (force_wr && !(flags & O_WRONLY))
  13152. + force_wr = 0;
  13153. + flags = au_file_roflags(flags);
  13154. + if (force_wr) {
  13155. + h_file = ERR_PTR(-EROFS);
  13156. + flags = au_file_roflags(flags);
  13157. + if (unlikely(vfsub_native_ro(h_inode)
  13158. + || IS_APPEND(h_inode)))
  13159. + goto out;
  13160. + flags &= ~O_ACCMODE;
  13161. + flags |= O_WRONLY;
  13162. + }
  13163. + }
  13164. + flags &= ~O_CREAT;
  13165. + au_lcnt_inc(&br->br_nfiles);
  13166. + h_path.dentry = h_dentry;
  13167. + h_path.mnt = au_br_mnt(br);
  13168. + h_file = vfsub_dentry_open(&h_path, flags);
  13169. + if (IS_ERR(h_file))
  13170. + goto out_br;
  13171. +
  13172. + if (flags & __FMODE_EXEC) {
  13173. + err = deny_write_access(h_file);
  13174. + if (unlikely(err)) {
  13175. + fput(h_file);
  13176. + h_file = ERR_PTR(err);
  13177. + goto out_br;
  13178. + }
  13179. + }
  13180. + fsnotify_open(h_file);
  13181. + goto out; /* success */
  13182. +
  13183. +out_br:
  13184. + au_lcnt_dec(&br->br_nfiles);
  13185. +out:
  13186. + return h_file;
  13187. +}
  13188. +
  13189. +static int au_cmoo(struct dentry *dentry)
  13190. +{
  13191. + int err, cmoo, matched;
  13192. + unsigned int udba;
  13193. + struct path h_path;
  13194. + struct au_pin pin;
  13195. + struct au_cp_generic cpg = {
  13196. + .dentry = dentry,
  13197. + .bdst = -1,
  13198. + .bsrc = -1,
  13199. + .len = -1,
  13200. + .pin = &pin,
  13201. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  13202. + };
  13203. + struct inode *delegated;
  13204. + struct super_block *sb;
  13205. + struct au_sbinfo *sbinfo;
  13206. + struct au_fhsm *fhsm;
  13207. + pid_t pid;
  13208. + struct au_branch *br;
  13209. + struct dentry *parent;
  13210. + struct au_hinode *hdir;
  13211. +
  13212. + DiMustWriteLock(dentry);
  13213. + IiMustWriteLock(d_inode(dentry));
  13214. +
  13215. + err = 0;
  13216. + if (IS_ROOT(dentry))
  13217. + goto out;
  13218. + cpg.bsrc = au_dbtop(dentry);
  13219. + if (!cpg.bsrc)
  13220. + goto out;
  13221. +
  13222. + sb = dentry->d_sb;
  13223. + sbinfo = au_sbi(sb);
  13224. + fhsm = &sbinfo->si_fhsm;
  13225. + pid = au_fhsm_pid(fhsm);
  13226. + rcu_read_lock();
  13227. + matched = (pid
  13228. + && (current->pid == pid
  13229. + || rcu_dereference(current->real_parent)->pid == pid));
  13230. + rcu_read_unlock();
  13231. + if (matched)
  13232. + goto out;
  13233. +
  13234. + br = au_sbr(sb, cpg.bsrc);
  13235. + cmoo = au_br_cmoo(br->br_perm);
  13236. + if (!cmoo)
  13237. + goto out;
  13238. + if (!d_is_reg(dentry))
  13239. + cmoo &= AuBrAttr_COO_ALL;
  13240. + if (!cmoo)
  13241. + goto out;
  13242. +
  13243. + parent = dget_parent(dentry);
  13244. + di_write_lock_parent(parent);
  13245. + err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
  13246. + cpg.bdst = err;
  13247. + if (unlikely(err < 0)) {
  13248. + err = 0; /* there is no upper writable branch */
  13249. + goto out_dgrade;
  13250. + }
  13251. + AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
  13252. +
  13253. + /* do not respect the coo attrib for the target branch */
  13254. + err = au_cpup_dirs(dentry, cpg.bdst);
  13255. + if (unlikely(err))
  13256. + goto out_dgrade;
  13257. +
  13258. + di_downgrade_lock(parent, AuLock_IR);
  13259. + udba = au_opt_udba(sb);
  13260. + err = au_pin(&pin, dentry, cpg.bdst, udba,
  13261. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  13262. + if (unlikely(err))
  13263. + goto out_parent;
  13264. +
  13265. + err = au_sio_cpup_simple(&cpg);
  13266. + au_unpin(&pin);
  13267. + if (unlikely(err))
  13268. + goto out_parent;
  13269. + if (!(cmoo & AuBrWAttr_MOO))
  13270. + goto out_parent; /* success */
  13271. +
  13272. + err = au_pin(&pin, dentry, cpg.bsrc, udba,
  13273. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  13274. + if (unlikely(err))
  13275. + goto out_parent;
  13276. +
  13277. + h_path.mnt = au_br_mnt(br);
  13278. + h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
  13279. + hdir = au_hi(d_inode(parent), cpg.bsrc);
  13280. + delegated = NULL;
  13281. + err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
  13282. + au_unpin(&pin);
  13283. + /* todo: keep h_dentry or not? */
  13284. + if (unlikely(err == -EWOULDBLOCK)) {
  13285. + pr_warn("cannot retry for NFSv4 delegation"
  13286. + " for an internal unlink\n");
  13287. + iput(delegated);
  13288. + }
  13289. + if (unlikely(err)) {
  13290. + pr_err("unlink %pd after coo failed (%d), ignored\n",
  13291. + dentry, err);
  13292. + err = 0;
  13293. + }
  13294. + goto out_parent; /* success */
  13295. +
  13296. +out_dgrade:
  13297. + di_downgrade_lock(parent, AuLock_IR);
  13298. +out_parent:
  13299. + di_read_unlock(parent, AuLock_IR);
  13300. + dput(parent);
  13301. +out:
  13302. + AuTraceErr(err);
  13303. + return err;
  13304. +}
  13305. +
  13306. +int au_do_open(struct file *file, struct au_do_open_args *args)
  13307. +{
  13308. + int err, aopen = args->aopen;
  13309. + struct dentry *dentry;
  13310. + struct au_finfo *finfo;
  13311. +
  13312. + if (!aopen)
  13313. + err = au_finfo_init(file, args->fidir);
  13314. + else {
  13315. + lockdep_off();
  13316. + err = au_finfo_init(file, args->fidir);
  13317. + lockdep_on();
  13318. + }
  13319. + if (unlikely(err))
  13320. + goto out;
  13321. +
  13322. + dentry = file->f_path.dentry;
  13323. + AuDebugOn(IS_ERR_OR_NULL(dentry));
  13324. + di_write_lock_child(dentry);
  13325. + err = au_cmoo(dentry);
  13326. + di_downgrade_lock(dentry, AuLock_IR);
  13327. + if (!err) {
  13328. + if (!aopen)
  13329. + err = args->open(file, vfsub_file_flags(file), NULL);
  13330. + else {
  13331. + lockdep_off();
  13332. + err = args->open(file, vfsub_file_flags(file),
  13333. + args->h_file);
  13334. + lockdep_on();
  13335. + }
  13336. + }
  13337. + di_read_unlock(dentry, AuLock_IR);
  13338. +
  13339. + finfo = au_fi(file);
  13340. + if (!err) {
  13341. + finfo->fi_file = file;
  13342. + au_hbl_add(&finfo->fi_hlist,
  13343. + &au_sbi(file->f_path.dentry->d_sb)->si_files);
  13344. + }
  13345. + if (!aopen)
  13346. + fi_write_unlock(file);
  13347. + else {
  13348. + lockdep_off();
  13349. + fi_write_unlock(file);
  13350. + lockdep_on();
  13351. + }
  13352. + if (unlikely(err)) {
  13353. + finfo->fi_hdir = NULL;
  13354. + au_finfo_fin(file);
  13355. + }
  13356. +
  13357. +out:
  13358. + AuTraceErr(err);
  13359. + return err;
  13360. +}
  13361. +
  13362. +int au_reopen_nondir(struct file *file)
  13363. +{
  13364. + int err;
  13365. + aufs_bindex_t btop;
  13366. + struct dentry *dentry;
  13367. + struct au_branch *br;
  13368. + struct file *h_file, *h_file_tmp;
  13369. +
  13370. + dentry = file->f_path.dentry;
  13371. + btop = au_dbtop(dentry);
  13372. + br = au_sbr(dentry->d_sb, btop);
  13373. + h_file_tmp = NULL;
  13374. + if (au_fbtop(file) == btop) {
  13375. + h_file = au_hf_top(file);
  13376. + if (file->f_mode == h_file->f_mode)
  13377. + return 0; /* success */
  13378. + h_file_tmp = h_file;
  13379. + get_file(h_file_tmp);
  13380. + au_lcnt_inc(&br->br_nfiles);
  13381. + au_set_h_fptr(file, btop, NULL);
  13382. + }
  13383. + AuDebugOn(au_fi(file)->fi_hdir);
  13384. + /*
  13385. + * it can happen
  13386. + * file exists on both of rw and ro
  13387. + * open --> dbtop and fbtop are both 0
  13388. + * prepend a branch as rw, "rw" become ro
  13389. + * remove rw/file
  13390. + * delete the top branch, "rw" becomes rw again
  13391. + * --> dbtop is 1, fbtop is still 0
  13392. + * write --> fbtop is 0 but dbtop is 1
  13393. + */
  13394. + /* AuDebugOn(au_fbtop(file) < btop); */
  13395. +
  13396. + h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
  13397. + file, /*force_wr*/0);
  13398. + err = PTR_ERR(h_file);
  13399. + if (IS_ERR(h_file)) {
  13400. + if (h_file_tmp) {
  13401. + /* revert */
  13402. + au_set_h_fptr(file, btop, h_file_tmp);
  13403. + h_file_tmp = NULL;
  13404. + }
  13405. + goto out; /* todo: close all? */
  13406. + }
  13407. +
  13408. + err = 0;
  13409. + au_set_fbtop(file, btop);
  13410. + au_set_h_fptr(file, btop, h_file);
  13411. + au_update_figen(file);
  13412. + /* todo: necessary? */
  13413. + /* file->f_ra = h_file->f_ra; */
  13414. +
  13415. +out:
  13416. + if (h_file_tmp) {
  13417. + fput(h_file_tmp);
  13418. + au_lcnt_dec(&br->br_nfiles);
  13419. + }
  13420. + return err;
  13421. +}
  13422. +
  13423. +/* ---------------------------------------------------------------------- */
  13424. +
  13425. +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
  13426. + struct dentry *hi_wh)
  13427. +{
  13428. + int err;
  13429. + aufs_bindex_t btop;
  13430. + struct au_dinfo *dinfo;
  13431. + struct dentry *h_dentry;
  13432. + struct au_hdentry *hdp;
  13433. +
  13434. + dinfo = au_di(file->f_path.dentry);
  13435. + AuRwMustWriteLock(&dinfo->di_rwsem);
  13436. +
  13437. + btop = dinfo->di_btop;
  13438. + dinfo->di_btop = btgt;
  13439. + hdp = au_hdentry(dinfo, btgt);
  13440. + h_dentry = hdp->hd_dentry;
  13441. + hdp->hd_dentry = hi_wh;
  13442. + err = au_reopen_nondir(file);
  13443. + hdp->hd_dentry = h_dentry;
  13444. + dinfo->di_btop = btop;
  13445. +
  13446. + return err;
  13447. +}
  13448. +
  13449. +static int au_ready_to_write_wh(struct file *file, loff_t len,
  13450. + aufs_bindex_t bcpup, struct au_pin *pin)
  13451. +{
  13452. + int err;
  13453. + struct inode *inode, *h_inode;
  13454. + struct dentry *h_dentry, *hi_wh;
  13455. + struct au_cp_generic cpg = {
  13456. + .dentry = file->f_path.dentry,
  13457. + .bdst = bcpup,
  13458. + .bsrc = -1,
  13459. + .len = len,
  13460. + .pin = pin
  13461. + };
  13462. +
  13463. + au_update_dbtop(cpg.dentry);
  13464. + inode = d_inode(cpg.dentry);
  13465. + h_inode = NULL;
  13466. + if (au_dbtop(cpg.dentry) <= bcpup
  13467. + && au_dbbot(cpg.dentry) >= bcpup) {
  13468. + h_dentry = au_h_dptr(cpg.dentry, bcpup);
  13469. + if (h_dentry && d_is_positive(h_dentry))
  13470. + h_inode = d_inode(h_dentry);
  13471. + }
  13472. + hi_wh = au_hi_wh(inode, bcpup);
  13473. + if (!hi_wh && !h_inode)
  13474. + err = au_sio_cpup_wh(&cpg, file);
  13475. + else
  13476. + /* already copied-up after unlink */
  13477. + err = au_reopen_wh(file, bcpup, hi_wh);
  13478. +
  13479. + if (!err
  13480. + && (inode->i_nlink > 1
  13481. + || (inode->i_state & I_LINKABLE))
  13482. + && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
  13483. + au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
  13484. +
  13485. + return err;
  13486. +}
  13487. +
  13488. +/*
  13489. + * prepare the @file for writing.
  13490. + */
  13491. +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
  13492. +{
  13493. + int err;
  13494. + aufs_bindex_t dbtop;
  13495. + struct dentry *parent;
  13496. + struct inode *inode;
  13497. + struct super_block *sb;
  13498. + struct file *h_file;
  13499. + struct au_cp_generic cpg = {
  13500. + .dentry = file->f_path.dentry,
  13501. + .bdst = -1,
  13502. + .bsrc = -1,
  13503. + .len = len,
  13504. + .pin = pin,
  13505. + .flags = AuCpup_DTIME
  13506. + };
  13507. +
  13508. + sb = cpg.dentry->d_sb;
  13509. + inode = d_inode(cpg.dentry);
  13510. + cpg.bsrc = au_fbtop(file);
  13511. + err = au_test_ro(sb, cpg.bsrc, inode);
  13512. + if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
  13513. + err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
  13514. + /*flags*/0);
  13515. + goto out;
  13516. + }
  13517. +
  13518. + /* need to cpup or reopen */
  13519. + parent = dget_parent(cpg.dentry);
  13520. + di_write_lock_parent(parent);
  13521. + err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
  13522. + cpg.bdst = err;
  13523. + if (unlikely(err < 0))
  13524. + goto out_dgrade;
  13525. + err = 0;
  13526. +
  13527. + if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
  13528. + err = au_cpup_dirs(cpg.dentry, cpg.bdst);
  13529. + if (unlikely(err))
  13530. + goto out_dgrade;
  13531. + }
  13532. +
  13533. + err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
  13534. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  13535. + if (unlikely(err))
  13536. + goto out_dgrade;
  13537. +
  13538. + dbtop = au_dbtop(cpg.dentry);
  13539. + if (dbtop <= cpg.bdst)
  13540. + cpg.bsrc = cpg.bdst;
  13541. +
  13542. + if (dbtop <= cpg.bdst /* just reopen */
  13543. + || !d_unhashed(cpg.dentry) /* copyup and reopen */
  13544. + ) {
  13545. + h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
  13546. + if (IS_ERR(h_file))
  13547. + err = PTR_ERR(h_file);
  13548. + else {
  13549. + di_downgrade_lock(parent, AuLock_IR);
  13550. + if (dbtop > cpg.bdst)
  13551. + err = au_sio_cpup_simple(&cpg);
  13552. + if (!err)
  13553. + err = au_reopen_nondir(file);
  13554. + au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
  13555. + }
  13556. + } else { /* copyup as wh and reopen */
  13557. + /*
  13558. + * since writable hfsplus branch is not supported,
  13559. + * h_open_pre/post() are unnecessary.
  13560. + */
  13561. + err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
  13562. + di_downgrade_lock(parent, AuLock_IR);
  13563. + }
  13564. +
  13565. + if (!err) {
  13566. + au_pin_set_parent_lflag(pin, /*lflag*/0);
  13567. + goto out_dput; /* success */
  13568. + }
  13569. + au_unpin(pin);
  13570. + goto out_unlock;
  13571. +
  13572. +out_dgrade:
  13573. + di_downgrade_lock(parent, AuLock_IR);
  13574. +out_unlock:
  13575. + di_read_unlock(parent, AuLock_IR);
  13576. +out_dput:
  13577. + dput(parent);
  13578. +out:
  13579. + return err;
  13580. +}
  13581. +
  13582. +/* ---------------------------------------------------------------------- */
  13583. +
  13584. +int au_do_flush(struct file *file, fl_owner_t id,
  13585. + int (*flush)(struct file *file, fl_owner_t id))
  13586. +{
  13587. + int err;
  13588. + struct super_block *sb;
  13589. + struct inode *inode;
  13590. +
  13591. + inode = file_inode(file);
  13592. + sb = inode->i_sb;
  13593. + si_noflush_read_lock(sb);
  13594. + fi_read_lock(file);
  13595. + ii_read_lock_child(inode);
  13596. +
  13597. + err = flush(file, id);
  13598. + au_cpup_attr_timesizes(inode);
  13599. +
  13600. + ii_read_unlock(inode);
  13601. + fi_read_unlock(file);
  13602. + si_read_unlock(sb);
  13603. + return err;
  13604. +}
  13605. +
  13606. +/* ---------------------------------------------------------------------- */
  13607. +
  13608. +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
  13609. +{
  13610. + int err;
  13611. + struct au_pin pin;
  13612. + struct au_finfo *finfo;
  13613. + struct dentry *parent, *hi_wh;
  13614. + struct inode *inode;
  13615. + struct super_block *sb;
  13616. + struct au_cp_generic cpg = {
  13617. + .dentry = file->f_path.dentry,
  13618. + .bdst = -1,
  13619. + .bsrc = -1,
  13620. + .len = -1,
  13621. + .pin = &pin,
  13622. + .flags = AuCpup_DTIME
  13623. + };
  13624. +
  13625. + FiMustWriteLock(file);
  13626. +
  13627. + err = 0;
  13628. + finfo = au_fi(file);
  13629. + sb = cpg.dentry->d_sb;
  13630. + inode = d_inode(cpg.dentry);
  13631. + cpg.bdst = au_ibtop(inode);
  13632. + if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
  13633. + goto out;
  13634. +
  13635. + parent = dget_parent(cpg.dentry);
  13636. + if (au_test_ro(sb, cpg.bdst, inode)) {
  13637. + di_read_lock_parent(parent, !AuLock_IR);
  13638. + err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
  13639. + cpg.bdst = err;
  13640. + di_read_unlock(parent, !AuLock_IR);
  13641. + if (unlikely(err < 0))
  13642. + goto out_parent;
  13643. + err = 0;
  13644. + }
  13645. +
  13646. + di_read_lock_parent(parent, AuLock_IR);
  13647. + hi_wh = au_hi_wh(inode, cpg.bdst);
  13648. + if (!S_ISDIR(inode->i_mode)
  13649. + && au_opt_test(au_mntflags(sb), PLINK)
  13650. + && au_plink_test(inode)
  13651. + && !d_unhashed(cpg.dentry)
  13652. + && cpg.bdst < au_dbtop(cpg.dentry)) {
  13653. + err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
  13654. + if (unlikely(err))
  13655. + goto out_unlock;
  13656. +
  13657. + /* always superio. */
  13658. + err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
  13659. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  13660. + if (!err) {
  13661. + err = au_sio_cpup_simple(&cpg);
  13662. + au_unpin(&pin);
  13663. + }
  13664. + } else if (hi_wh) {
  13665. + /* already copied-up after unlink */
  13666. + err = au_reopen_wh(file, cpg.bdst, hi_wh);
  13667. + *need_reopen = 0;
  13668. + }
  13669. +
  13670. +out_unlock:
  13671. + di_read_unlock(parent, AuLock_IR);
  13672. +out_parent:
  13673. + dput(parent);
  13674. +out:
  13675. + return err;
  13676. +}
  13677. +
  13678. +static void au_do_refresh_dir(struct file *file)
  13679. +{
  13680. + aufs_bindex_t bindex, bbot, new_bindex, brid;
  13681. + struct au_hfile *p, tmp, *q;
  13682. + struct au_finfo *finfo;
  13683. + struct super_block *sb;
  13684. + struct au_fidir *fidir;
  13685. +
  13686. + FiMustWriteLock(file);
  13687. +
  13688. + sb = file->f_path.dentry->d_sb;
  13689. + finfo = au_fi(file);
  13690. + fidir = finfo->fi_hdir;
  13691. + AuDebugOn(!fidir);
  13692. + p = fidir->fd_hfile + finfo->fi_btop;
  13693. + brid = p->hf_br->br_id;
  13694. + bbot = fidir->fd_bbot;
  13695. + for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
  13696. + if (!p->hf_file)
  13697. + continue;
  13698. +
  13699. + new_bindex = au_br_index(sb, p->hf_br->br_id);
  13700. + if (new_bindex == bindex)
  13701. + continue;
  13702. + if (new_bindex < 0) {
  13703. + au_set_h_fptr(file, bindex, NULL);
  13704. + continue;
  13705. + }
  13706. +
  13707. + /* swap two lower inode, and loop again */
  13708. + q = fidir->fd_hfile + new_bindex;
  13709. + tmp = *q;
  13710. + *q = *p;
  13711. + *p = tmp;
  13712. + if (tmp.hf_file) {
  13713. + bindex--;
  13714. + p--;
  13715. + }
  13716. + }
  13717. +
  13718. + p = fidir->fd_hfile;
  13719. + if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
  13720. + bbot = au_sbbot(sb);
  13721. + for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
  13722. + finfo->fi_btop++, p++)
  13723. + if (p->hf_file) {
  13724. + if (file_inode(p->hf_file))
  13725. + break;
  13726. + au_hfput(p, /*execed*/0);
  13727. + }
  13728. + } else {
  13729. + bbot = au_br_index(sb, brid);
  13730. + for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
  13731. + finfo->fi_btop++, p++)
  13732. + if (p->hf_file)
  13733. + au_hfput(p, /*execed*/0);
  13734. + bbot = au_sbbot(sb);
  13735. + }
  13736. +
  13737. + p = fidir->fd_hfile + bbot;
  13738. + for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
  13739. + fidir->fd_bbot--, p--)
  13740. + if (p->hf_file) {
  13741. + if (file_inode(p->hf_file))
  13742. + break;
  13743. + au_hfput(p, /*execed*/0);
  13744. + }
  13745. + AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
  13746. +}
  13747. +
  13748. +/*
  13749. + * after branch manipulating, refresh the file.
  13750. + */
  13751. +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
  13752. +{
  13753. + int err, need_reopen, nbr;
  13754. + aufs_bindex_t bbot, bindex;
  13755. + struct dentry *dentry;
  13756. + struct super_block *sb;
  13757. + struct au_finfo *finfo;
  13758. + struct au_hfile *hfile;
  13759. +
  13760. + dentry = file->f_path.dentry;
  13761. + sb = dentry->d_sb;
  13762. + nbr = au_sbbot(sb) + 1;
  13763. + finfo = au_fi(file);
  13764. + if (!finfo->fi_hdir) {
  13765. + hfile = &finfo->fi_htop;
  13766. + AuDebugOn(!hfile->hf_file);
  13767. + bindex = au_br_index(sb, hfile->hf_br->br_id);
  13768. + AuDebugOn(bindex < 0);
  13769. + if (bindex != finfo->fi_btop)
  13770. + au_set_fbtop(file, bindex);
  13771. + } else {
  13772. + err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
  13773. + if (unlikely(err))
  13774. + goto out;
  13775. + au_do_refresh_dir(file);
  13776. + }
  13777. +
  13778. + err = 0;
  13779. + need_reopen = 1;
  13780. + if (!au_test_mmapped(file))
  13781. + err = au_file_refresh_by_inode(file, &need_reopen);
  13782. + if (finfo->fi_hdir)
  13783. + /* harmless if err */
  13784. + au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
  13785. + if (!err && need_reopen && !d_unlinked(dentry))
  13786. + err = reopen(file);
  13787. + if (!err) {
  13788. + au_update_figen(file);
  13789. + goto out; /* success */
  13790. + }
  13791. +
  13792. + /* error, close all lower files */
  13793. + if (finfo->fi_hdir) {
  13794. + bbot = au_fbbot_dir(file);
  13795. + for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
  13796. + au_set_h_fptr(file, bindex, NULL);
  13797. + }
  13798. +
  13799. +out:
  13800. + return err;
  13801. +}
  13802. +
  13803. +/* common function to regular file and dir */
  13804. +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
  13805. + int wlock, unsigned int fi_lsc)
  13806. +{
  13807. + int err;
  13808. + unsigned int sigen, figen;
  13809. + aufs_bindex_t btop;
  13810. + unsigned char pseudo_link;
  13811. + struct dentry *dentry;
  13812. + struct inode *inode;
  13813. +
  13814. + err = 0;
  13815. + dentry = file->f_path.dentry;
  13816. + inode = d_inode(dentry);
  13817. + sigen = au_sigen(dentry->d_sb);
  13818. + fi_write_lock_nested(file, fi_lsc);
  13819. + figen = au_figen(file);
  13820. + if (!fi_lsc)
  13821. + di_write_lock_child(dentry);
  13822. + else
  13823. + di_write_lock_child2(dentry);
  13824. + btop = au_dbtop(dentry);
  13825. + pseudo_link = (btop != au_ibtop(inode));
  13826. + if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
  13827. + if (!wlock) {
  13828. + di_downgrade_lock(dentry, AuLock_IR);
  13829. + fi_downgrade_lock(file);
  13830. + }
  13831. + goto out; /* success */
  13832. + }
  13833. +
  13834. + AuDbg("sigen %d, figen %d\n", sigen, figen);
  13835. + if (au_digen_test(dentry, sigen)) {
  13836. + err = au_reval_dpath(dentry, sigen);
  13837. + AuDebugOn(!err && au_digen_test(dentry, sigen));
  13838. + }
  13839. +
  13840. + if (!err)
  13841. + err = refresh_file(file, reopen);
  13842. + if (!err) {
  13843. + if (!wlock) {
  13844. + di_downgrade_lock(dentry, AuLock_IR);
  13845. + fi_downgrade_lock(file);
  13846. + }
  13847. + } else {
  13848. + di_write_unlock(dentry);
  13849. + fi_write_unlock(file);
  13850. + }
  13851. +
  13852. +out:
  13853. + return err;
  13854. +}
  13855. +
  13856. +/* ---------------------------------------------------------------------- */
  13857. +
  13858. +/* cf. aufs_nopage() */
  13859. +/* for madvise(2) */
  13860. +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
  13861. +{
  13862. + unlock_page(page);
  13863. + return 0;
  13864. +}
  13865. +
  13866. +/* it will never be called, but necessary to support O_DIRECT */
  13867. +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  13868. +{ BUG(); return 0; }
  13869. +
  13870. +/* they will never be called. */
  13871. +#ifdef CONFIG_AUFS_DEBUG
  13872. +static int aufs_write_begin(struct file *file, struct address_space *mapping,
  13873. + loff_t pos, unsigned len, unsigned flags,
  13874. + struct page **pagep, void **fsdata)
  13875. +{ AuUnsupport(); return 0; }
  13876. +static int aufs_write_end(struct file *file, struct address_space *mapping,
  13877. + loff_t pos, unsigned len, unsigned copied,
  13878. + struct page *page, void *fsdata)
  13879. +{ AuUnsupport(); return 0; }
  13880. +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
  13881. +{ AuUnsupport(); return 0; }
  13882. +
  13883. +static int aufs_set_page_dirty(struct page *page)
  13884. +{ AuUnsupport(); return 0; }
  13885. +static void aufs_invalidatepage(struct page *page, unsigned int offset,
  13886. + unsigned int length)
  13887. +{ AuUnsupport(); }
  13888. +static int aufs_releasepage(struct page *page, gfp_t gfp)
  13889. +{ AuUnsupport(); return 0; }
  13890. +#if 0 /* called by memory compaction regardless file */
  13891. +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
  13892. + struct page *page, enum migrate_mode mode)
  13893. +{ AuUnsupport(); return 0; }
  13894. +#endif
  13895. +static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
  13896. +{ AuUnsupport(); return true; }
  13897. +static void aufs_putback_page(struct page *page)
  13898. +{ AuUnsupport(); }
  13899. +static int aufs_launder_page(struct page *page)
  13900. +{ AuUnsupport(); return 0; }
  13901. +static int aufs_is_partially_uptodate(struct page *page,
  13902. + unsigned long from,
  13903. + unsigned long count)
  13904. +{ AuUnsupport(); return 0; }
  13905. +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
  13906. + bool *writeback)
  13907. +{ AuUnsupport(); }
  13908. +static int aufs_error_remove_page(struct address_space *mapping,
  13909. + struct page *page)
  13910. +{ AuUnsupport(); return 0; }
  13911. +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
  13912. + sector_t *span)
  13913. +{ AuUnsupport(); return 0; }
  13914. +static void aufs_swap_deactivate(struct file *file)
  13915. +{ AuUnsupport(); }
  13916. +#endif /* CONFIG_AUFS_DEBUG */
  13917. +
  13918. +const struct address_space_operations aufs_aop = {
  13919. + .readpage = aufs_readpage,
  13920. + .direct_IO = aufs_direct_IO,
  13921. +#ifdef CONFIG_AUFS_DEBUG
  13922. + .writepage = aufs_writepage,
  13923. + /* no writepages, because of writepage */
  13924. + .set_page_dirty = aufs_set_page_dirty,
  13925. + /* no readpages, because of readpage */
  13926. + .write_begin = aufs_write_begin,
  13927. + .write_end = aufs_write_end,
  13928. + /* no bmap, no block device */
  13929. + .invalidatepage = aufs_invalidatepage,
  13930. + .releasepage = aufs_releasepage,
  13931. + /* is fallback_migrate_page ok? */
  13932. + /* .migratepage = aufs_migratepage, */
  13933. + .isolate_page = aufs_isolate_page,
  13934. + .putback_page = aufs_putback_page,
  13935. + .launder_page = aufs_launder_page,
  13936. + .is_partially_uptodate = aufs_is_partially_uptodate,
  13937. + .is_dirty_writeback = aufs_is_dirty_writeback,
  13938. + .error_remove_page = aufs_error_remove_page,
  13939. + .swap_activate = aufs_swap_activate,
  13940. + .swap_deactivate = aufs_swap_deactivate
  13941. +#endif /* CONFIG_AUFS_DEBUG */
  13942. +};
  13943. diff -Naur null/fs/aufs/file.h linux-5.1/fs/aufs/file.h
  13944. --- /dev/null
  13945. +++ linux-5.1/fs/aufs/file.h 2019-05-22 11:23:07.440141042 +0200
  13946. @@ -0,0 +1,342 @@
  13947. +/* SPDX-License-Identifier: GPL-2.0 */
  13948. +/*
  13949. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  13950. + *
  13951. + * This program, aufs is free software; you can redistribute it and/or modify
  13952. + * it under the terms of the GNU General Public License as published by
  13953. + * the Free Software Foundation; either version 2 of the License, or
  13954. + * (at your option) any later version.
  13955. + *
  13956. + * This program is distributed in the hope that it will be useful,
  13957. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13958. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13959. + * GNU General Public License for more details.
  13960. + *
  13961. + * You should have received a copy of the GNU General Public License
  13962. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  13963. + */
  13964. +
  13965. +/*
  13966. + * file operations
  13967. + */
  13968. +
  13969. +#ifndef __AUFS_FILE_H__
  13970. +#define __AUFS_FILE_H__
  13971. +
  13972. +#ifdef __KERNEL__
  13973. +
  13974. +#include <linux/file.h>
  13975. +#include <linux/fs.h>
  13976. +#include <linux/mm_types.h>
  13977. +#include <linux/poll.h>
  13978. +#include "rwsem.h"
  13979. +
  13980. +struct au_branch;
  13981. +struct au_hfile {
  13982. + struct file *hf_file;
  13983. + struct au_branch *hf_br;
  13984. +};
  13985. +
  13986. +struct au_vdir;
  13987. +struct au_fidir {
  13988. + aufs_bindex_t fd_bbot;
  13989. + aufs_bindex_t fd_nent;
  13990. + struct au_vdir *fd_vdir_cache;
  13991. + struct au_hfile fd_hfile[];
  13992. +};
  13993. +
  13994. +static inline int au_fidir_sz(int nent)
  13995. +{
  13996. + AuDebugOn(nent < 0);
  13997. + return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
  13998. +}
  13999. +
  14000. +struct au_finfo {
  14001. + atomic_t fi_generation;
  14002. +
  14003. + struct au_rwsem fi_rwsem;
  14004. + aufs_bindex_t fi_btop;
  14005. +
  14006. + /* do not union them */
  14007. + struct { /* for non-dir */
  14008. + struct au_hfile fi_htop;
  14009. + atomic_t fi_mmapped;
  14010. + };
  14011. + struct au_fidir *fi_hdir; /* for dir only */
  14012. +
  14013. + struct hlist_bl_node fi_hlist;
  14014. + struct file *fi_file; /* very ugly */
  14015. + struct rcu_head rcu;
  14016. +} ____cacheline_aligned_in_smp;
  14017. +
  14018. +/* ---------------------------------------------------------------------- */
  14019. +
  14020. +/* file.c */
  14021. +extern const struct address_space_operations aufs_aop;
  14022. +unsigned int au_file_roflags(unsigned int flags);
  14023. +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
  14024. + struct file *file, int force_wr);
  14025. +struct au_do_open_args {
  14026. + int aopen;
  14027. + int (*open)(struct file *file, int flags,
  14028. + struct file *h_file);
  14029. + struct au_fidir *fidir;
  14030. + struct file *h_file;
  14031. +};
  14032. +int au_do_open(struct file *file, struct au_do_open_args *args);
  14033. +int au_reopen_nondir(struct file *file);
  14034. +struct au_pin;
  14035. +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
  14036. +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
  14037. + int wlock, unsigned int fi_lsc);
  14038. +int au_do_flush(struct file *file, fl_owner_t id,
  14039. + int (*flush)(struct file *file, fl_owner_t id));
  14040. +
  14041. +/* poll.c */
  14042. +#ifdef CONFIG_AUFS_POLL
  14043. +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
  14044. +#endif
  14045. +
  14046. +#ifdef CONFIG_AUFS_BR_HFSPLUS
  14047. +/* hfsplus.c */
  14048. +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
  14049. + int force_wr);
  14050. +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
  14051. + struct file *h_file);
  14052. +#else
  14053. +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
  14054. + aufs_bindex_t bindex, int force_wr)
  14055. +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
  14056. + struct file *h_file);
  14057. +#endif
  14058. +
  14059. +/* f_op.c */
  14060. +extern const struct file_operations aufs_file_fop;
  14061. +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
  14062. +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
  14063. +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
  14064. +
  14065. +/* finfo.c */
  14066. +void au_hfput(struct au_hfile *hf, int execed);
  14067. +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
  14068. + struct file *h_file);
  14069. +
  14070. +void au_update_figen(struct file *file);
  14071. +struct au_fidir *au_fidir_alloc(struct super_block *sb);
  14072. +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
  14073. +
  14074. +void au_fi_init_once(void *_fi);
  14075. +void au_finfo_fin(struct file *file);
  14076. +int au_finfo_init(struct file *file, struct au_fidir *fidir);
  14077. +
  14078. +/* ioctl.c */
  14079. +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
  14080. +#ifdef CONFIG_COMPAT
  14081. +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
  14082. + unsigned long arg);
  14083. +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
  14084. + unsigned long arg);
  14085. +#endif
  14086. +
  14087. +/* ---------------------------------------------------------------------- */
  14088. +
  14089. +static inline struct au_finfo *au_fi(struct file *file)
  14090. +{
  14091. + return file->private_data;
  14092. +}
  14093. +
  14094. +/* ---------------------------------------------------------------------- */
  14095. +
  14096. +#define fi_read_lock(f) au_rw_read_lock(&au_fi(f)->fi_rwsem)
  14097. +#define fi_write_lock(f) au_rw_write_lock(&au_fi(f)->fi_rwsem)
  14098. +#define fi_read_trylock(f) au_rw_read_trylock(&au_fi(f)->fi_rwsem)
  14099. +#define fi_write_trylock(f) au_rw_write_trylock(&au_fi(f)->fi_rwsem)
  14100. +/*
  14101. +#define fi_read_trylock_nested(f) \
  14102. + au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
  14103. +#define fi_write_trylock_nested(f) \
  14104. + au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
  14105. +*/
  14106. +
  14107. +#define fi_read_unlock(f) au_rw_read_unlock(&au_fi(f)->fi_rwsem)
  14108. +#define fi_write_unlock(f) au_rw_write_unlock(&au_fi(f)->fi_rwsem)
  14109. +#define fi_downgrade_lock(f) au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
  14110. +
  14111. +/* lock subclass for finfo */
  14112. +enum {
  14113. + AuLsc_FI_1,
  14114. + AuLsc_FI_2
  14115. +};
  14116. +
  14117. +static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
  14118. +{
  14119. + au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
  14120. +}
  14121. +
  14122. +static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
  14123. +{
  14124. + au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
  14125. +}
  14126. +
  14127. +/*
  14128. + * fi_read_lock_1, fi_write_lock_1,
  14129. + * fi_read_lock_2, fi_write_lock_2
  14130. + */
  14131. +#define AuReadLockFunc(name) \
  14132. +static inline void fi_read_lock_##name(struct file *f) \
  14133. +{ fi_read_lock_nested(f, AuLsc_FI_##name); }
  14134. +
  14135. +#define AuWriteLockFunc(name) \
  14136. +static inline void fi_write_lock_##name(struct file *f) \
  14137. +{ fi_write_lock_nested(f, AuLsc_FI_##name); }
  14138. +
  14139. +#define AuRWLockFuncs(name) \
  14140. + AuReadLockFunc(name) \
  14141. + AuWriteLockFunc(name)
  14142. +
  14143. +AuRWLockFuncs(1);
  14144. +AuRWLockFuncs(2);
  14145. +
  14146. +#undef AuReadLockFunc
  14147. +#undef AuWriteLockFunc
  14148. +#undef AuRWLockFuncs
  14149. +
  14150. +#define FiMustNoWaiters(f) AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
  14151. +#define FiMustAnyLock(f) AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
  14152. +#define FiMustWriteLock(f) AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
  14153. +
  14154. +/* ---------------------------------------------------------------------- */
  14155. +
  14156. +/* todo: hard/soft set? */
  14157. +static inline aufs_bindex_t au_fbtop(struct file *file)
  14158. +{
  14159. + FiMustAnyLock(file);
  14160. + return au_fi(file)->fi_btop;
  14161. +}
  14162. +
  14163. +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
  14164. +{
  14165. + FiMustAnyLock(file);
  14166. + AuDebugOn(!au_fi(file)->fi_hdir);
  14167. + return au_fi(file)->fi_hdir->fd_bbot;
  14168. +}
  14169. +
  14170. +static inline struct au_vdir *au_fvdir_cache(struct file *file)
  14171. +{
  14172. + FiMustAnyLock(file);
  14173. + AuDebugOn(!au_fi(file)->fi_hdir);
  14174. + return au_fi(file)->fi_hdir->fd_vdir_cache;
  14175. +}
  14176. +
  14177. +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
  14178. +{
  14179. + FiMustWriteLock(file);
  14180. + au_fi(file)->fi_btop = bindex;
  14181. +}
  14182. +
  14183. +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
  14184. +{
  14185. + FiMustWriteLock(file);
  14186. + AuDebugOn(!au_fi(file)->fi_hdir);
  14187. + au_fi(file)->fi_hdir->fd_bbot = bindex;
  14188. +}
  14189. +
  14190. +static inline void au_set_fvdir_cache(struct file *file,
  14191. + struct au_vdir *vdir_cache)
  14192. +{
  14193. + FiMustWriteLock(file);
  14194. + AuDebugOn(!au_fi(file)->fi_hdir);
  14195. + au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
  14196. +}
  14197. +
  14198. +static inline struct file *au_hf_top(struct file *file)
  14199. +{
  14200. + FiMustAnyLock(file);
  14201. + AuDebugOn(au_fi(file)->fi_hdir);
  14202. + return au_fi(file)->fi_htop.hf_file;
  14203. +}
  14204. +
  14205. +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
  14206. +{
  14207. + FiMustAnyLock(file);
  14208. + AuDebugOn(!au_fi(file)->fi_hdir);
  14209. + return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
  14210. +}
  14211. +
  14212. +/* todo: memory barrier? */
  14213. +static inline unsigned int au_figen(struct file *f)
  14214. +{
  14215. + return atomic_read(&au_fi(f)->fi_generation);
  14216. +}
  14217. +
  14218. +static inline void au_set_mmapped(struct file *f)
  14219. +{
  14220. + if (atomic_inc_return(&au_fi(f)->fi_mmapped))
  14221. + return;
  14222. + pr_warn("fi_mmapped wrapped around\n");
  14223. + while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
  14224. + ;
  14225. +}
  14226. +
  14227. +static inline void au_unset_mmapped(struct file *f)
  14228. +{
  14229. + atomic_dec(&au_fi(f)->fi_mmapped);
  14230. +}
  14231. +
  14232. +static inline int au_test_mmapped(struct file *f)
  14233. +{
  14234. + return atomic_read(&au_fi(f)->fi_mmapped);
  14235. +}
  14236. +
  14237. +/* customize vma->vm_file */
  14238. +
  14239. +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
  14240. + struct file *file)
  14241. +{
  14242. + struct file *f;
  14243. +
  14244. + f = vma->vm_file;
  14245. + get_file(file);
  14246. + vma->vm_file = file;
  14247. + fput(f);
  14248. +}
  14249. +
  14250. +#ifdef CONFIG_MMU
  14251. +#define AuDbgVmRegion(file, vma) do {} while (0)
  14252. +
  14253. +static inline void au_vm_file_reset(struct vm_area_struct *vma,
  14254. + struct file *file)
  14255. +{
  14256. + au_do_vm_file_reset(vma, file);
  14257. +}
  14258. +#else
  14259. +#define AuDbgVmRegion(file, vma) \
  14260. + AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
  14261. +
  14262. +static inline void au_vm_file_reset(struct vm_area_struct *vma,
  14263. + struct file *file)
  14264. +{
  14265. + struct file *f;
  14266. +
  14267. + au_do_vm_file_reset(vma, file);
  14268. + f = vma->vm_region->vm_file;
  14269. + get_file(file);
  14270. + vma->vm_region->vm_file = file;
  14271. + fput(f);
  14272. +}
  14273. +#endif /* CONFIG_MMU */
  14274. +
  14275. +/* handle vma->vm_prfile */
  14276. +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
  14277. + struct file *file)
  14278. +{
  14279. + get_file(file);
  14280. + vma->vm_prfile = file;
  14281. +#ifndef CONFIG_MMU
  14282. + get_file(file);
  14283. + vma->vm_region->vm_prfile = file;
  14284. +#endif
  14285. +}
  14286. +
  14287. +#endif /* __KERNEL__ */
  14288. +#endif /* __AUFS_FILE_H__ */
  14289. diff -Naur null/fs/aufs/finfo.c linux-5.1/fs/aufs/finfo.c
  14290. --- /dev/null
  14291. +++ linux-5.1/fs/aufs/finfo.c 2019-05-22 11:23:07.440141042 +0200
  14292. @@ -0,0 +1,149 @@
  14293. +// SPDX-License-Identifier: GPL-2.0
  14294. +/*
  14295. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  14296. + *
  14297. + * This program, aufs is free software; you can redistribute it and/or modify
  14298. + * it under the terms of the GNU General Public License as published by
  14299. + * the Free Software Foundation; either version 2 of the License, or
  14300. + * (at your option) any later version.
  14301. + *
  14302. + * This program is distributed in the hope that it will be useful,
  14303. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14304. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14305. + * GNU General Public License for more details.
  14306. + *
  14307. + * You should have received a copy of the GNU General Public License
  14308. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14309. + */
  14310. +
  14311. +/*
  14312. + * file private data
  14313. + */
  14314. +
  14315. +#include "aufs.h"
  14316. +
  14317. +void au_hfput(struct au_hfile *hf, int execed)
  14318. +{
  14319. + if (execed)
  14320. + allow_write_access(hf->hf_file);
  14321. + fput(hf->hf_file);
  14322. + hf->hf_file = NULL;
  14323. + au_lcnt_dec(&hf->hf_br->br_nfiles);
  14324. + hf->hf_br = NULL;
  14325. +}
  14326. +
  14327. +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
  14328. +{
  14329. + struct au_finfo *finfo = au_fi(file);
  14330. + struct au_hfile *hf;
  14331. + struct au_fidir *fidir;
  14332. +
  14333. + fidir = finfo->fi_hdir;
  14334. + if (!fidir) {
  14335. + AuDebugOn(finfo->fi_btop != bindex);
  14336. + hf = &finfo->fi_htop;
  14337. + } else
  14338. + hf = fidir->fd_hfile + bindex;
  14339. +
  14340. + if (hf && hf->hf_file)
  14341. + au_hfput(hf, vfsub_file_execed(file));
  14342. + if (val) {
  14343. + FiMustWriteLock(file);
  14344. + AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
  14345. + hf->hf_file = val;
  14346. + hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
  14347. + }
  14348. +}
  14349. +
  14350. +void au_update_figen(struct file *file)
  14351. +{
  14352. + atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
  14353. + /* smp_mb(); */ /* atomic_set */
  14354. +}
  14355. +
  14356. +/* ---------------------------------------------------------------------- */
  14357. +
  14358. +struct au_fidir *au_fidir_alloc(struct super_block *sb)
  14359. +{
  14360. + struct au_fidir *fidir;
  14361. + int nbr;
  14362. +
  14363. + nbr = au_sbbot(sb) + 1;
  14364. + if (nbr < 2)
  14365. + nbr = 2; /* initial allocate for 2 branches */
  14366. + fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
  14367. + if (fidir) {
  14368. + fidir->fd_bbot = -1;
  14369. + fidir->fd_nent = nbr;
  14370. + }
  14371. +
  14372. + return fidir;
  14373. +}
  14374. +
  14375. +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
  14376. +{
  14377. + int err;
  14378. + struct au_fidir *fidir, *p;
  14379. +
  14380. + AuRwMustWriteLock(&finfo->fi_rwsem);
  14381. + fidir = finfo->fi_hdir;
  14382. + AuDebugOn(!fidir);
  14383. +
  14384. + err = -ENOMEM;
  14385. + p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
  14386. + GFP_NOFS, may_shrink);
  14387. + if (p) {
  14388. + p->fd_nent = nbr;
  14389. + finfo->fi_hdir = p;
  14390. + err = 0;
  14391. + }
  14392. +
  14393. + return err;
  14394. +}
  14395. +
  14396. +/* ---------------------------------------------------------------------- */
  14397. +
  14398. +void au_finfo_fin(struct file *file)
  14399. +{
  14400. + struct au_finfo *finfo;
  14401. +
  14402. + au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
  14403. +
  14404. + finfo = au_fi(file);
  14405. + AuDebugOn(finfo->fi_hdir);
  14406. + AuRwDestroy(&finfo->fi_rwsem);
  14407. + au_cache_free_finfo(finfo);
  14408. +}
  14409. +
  14410. +void au_fi_init_once(void *_finfo)
  14411. +{
  14412. + struct au_finfo *finfo = _finfo;
  14413. +
  14414. + au_rw_init(&finfo->fi_rwsem);
  14415. +}
  14416. +
  14417. +int au_finfo_init(struct file *file, struct au_fidir *fidir)
  14418. +{
  14419. + int err;
  14420. + struct au_finfo *finfo;
  14421. + struct dentry *dentry;
  14422. +
  14423. + err = -ENOMEM;
  14424. + dentry = file->f_path.dentry;
  14425. + finfo = au_cache_alloc_finfo();
  14426. + if (unlikely(!finfo))
  14427. + goto out;
  14428. +
  14429. + err = 0;
  14430. + au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
  14431. + au_rw_write_lock(&finfo->fi_rwsem);
  14432. + finfo->fi_btop = -1;
  14433. + finfo->fi_hdir = fidir;
  14434. + atomic_set(&finfo->fi_generation, au_digen(dentry));
  14435. + /* smp_mb(); */ /* atomic_set */
  14436. +
  14437. + file->private_data = finfo;
  14438. +
  14439. +out:
  14440. + return err;
  14441. +}
  14442. diff -Naur null/fs/aufs/f_op.c linux-5.1/fs/aufs/f_op.c
  14443. --- /dev/null
  14444. +++ linux-5.1/fs/aufs/f_op.c 2019-05-22 11:23:07.440141042 +0200
  14445. @@ -0,0 +1,819 @@
  14446. +// SPDX-License-Identifier: GPL-2.0
  14447. +/*
  14448. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  14449. + *
  14450. + * This program, aufs is free software; you can redistribute it and/or modify
  14451. + * it under the terms of the GNU General Public License as published by
  14452. + * the Free Software Foundation; either version 2 of the License, or
  14453. + * (at your option) any later version.
  14454. + *
  14455. + * This program is distributed in the hope that it will be useful,
  14456. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14457. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14458. + * GNU General Public License for more details.
  14459. + *
  14460. + * You should have received a copy of the GNU General Public License
  14461. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14462. + */
  14463. +
  14464. +/*
  14465. + * file and vm operations
  14466. + */
  14467. +
  14468. +#include <linux/aio.h>
  14469. +#include <linux/fs_stack.h>
  14470. +#include <linux/mman.h>
  14471. +#include <linux/security.h>
  14472. +#include "aufs.h"
  14473. +
  14474. +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
  14475. +{
  14476. + int err;
  14477. + aufs_bindex_t bindex;
  14478. + struct dentry *dentry, *h_dentry;
  14479. + struct au_finfo *finfo;
  14480. + struct inode *h_inode;
  14481. +
  14482. + FiMustWriteLock(file);
  14483. +
  14484. + err = 0;
  14485. + dentry = file->f_path.dentry;
  14486. + AuDebugOn(IS_ERR_OR_NULL(dentry));
  14487. + finfo = au_fi(file);
  14488. + memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
  14489. + atomic_set(&finfo->fi_mmapped, 0);
  14490. + bindex = au_dbtop(dentry);
  14491. + if (!h_file) {
  14492. + h_dentry = au_h_dptr(dentry, bindex);
  14493. + err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
  14494. + if (unlikely(err))
  14495. + goto out;
  14496. + h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
  14497. + if (IS_ERR(h_file)) {
  14498. + err = PTR_ERR(h_file);
  14499. + goto out;
  14500. + }
  14501. + } else {
  14502. + h_dentry = h_file->f_path.dentry;
  14503. + err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
  14504. + if (unlikely(err))
  14505. + goto out;
  14506. + /* br ref is already inc-ed */
  14507. + }
  14508. +
  14509. + if ((flags & __O_TMPFILE)
  14510. + && !(flags & O_EXCL)) {
  14511. + h_inode = file_inode(h_file);
  14512. + spin_lock(&h_inode->i_lock);
  14513. + h_inode->i_state |= I_LINKABLE;
  14514. + spin_unlock(&h_inode->i_lock);
  14515. + }
  14516. + au_set_fbtop(file, bindex);
  14517. + au_set_h_fptr(file, bindex, h_file);
  14518. + au_update_figen(file);
  14519. + /* todo: necessary? */
  14520. + /* file->f_ra = h_file->f_ra; */
  14521. +
  14522. +out:
  14523. + return err;
  14524. +}
  14525. +
  14526. +static int aufs_open_nondir(struct inode *inode __maybe_unused,
  14527. + struct file *file)
  14528. +{
  14529. + int err;
  14530. + struct super_block *sb;
  14531. + struct au_do_open_args args = {
  14532. + .open = au_do_open_nondir
  14533. + };
  14534. +
  14535. + AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
  14536. + file, vfsub_file_flags(file), file->f_mode);
  14537. +
  14538. + sb = file->f_path.dentry->d_sb;
  14539. + si_read_lock(sb, AuLock_FLUSH);
  14540. + err = au_do_open(file, &args);
  14541. + si_read_unlock(sb);
  14542. + return err;
  14543. +}
  14544. +
  14545. +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
  14546. +{
  14547. + struct au_finfo *finfo;
  14548. + aufs_bindex_t bindex;
  14549. +
  14550. + finfo = au_fi(file);
  14551. + au_hbl_del(&finfo->fi_hlist,
  14552. + &au_sbi(file->f_path.dentry->d_sb)->si_files);
  14553. + bindex = finfo->fi_btop;
  14554. + if (bindex >= 0)
  14555. + au_set_h_fptr(file, bindex, NULL);
  14556. +
  14557. + au_finfo_fin(file);
  14558. + return 0;
  14559. +}
  14560. +
  14561. +/* ---------------------------------------------------------------------- */
  14562. +
  14563. +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
  14564. +{
  14565. + int err;
  14566. + struct file *h_file;
  14567. +
  14568. + err = 0;
  14569. + h_file = au_hf_top(file);
  14570. + if (h_file)
  14571. + err = vfsub_flush(h_file, id);
  14572. + return err;
  14573. +}
  14574. +
  14575. +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
  14576. +{
  14577. + return au_do_flush(file, id, au_do_flush_nondir);
  14578. +}
  14579. +
  14580. +/* ---------------------------------------------------------------------- */
  14581. +/*
  14582. + * read and write functions acquire [fdi]_rwsem once, but release before
  14583. + * mmap_sem. This is because to stop a race condition between mmap(2).
  14584. + * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
  14585. + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
  14586. + * read functions after [fdi]_rwsem are released, but it should be harmless.
  14587. + */
  14588. +
  14589. +/* Callers should call au_read_post() or fput() in the end */
  14590. +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
  14591. +{
  14592. + struct file *h_file;
  14593. + int err;
  14594. +
  14595. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
  14596. + if (!err) {
  14597. + di_read_unlock(file->f_path.dentry, AuLock_IR);
  14598. + h_file = au_hf_top(file);
  14599. + get_file(h_file);
  14600. + if (!keep_fi)
  14601. + fi_read_unlock(file);
  14602. + } else
  14603. + h_file = ERR_PTR(err);
  14604. +
  14605. + return h_file;
  14606. +}
  14607. +
  14608. +static void au_read_post(struct inode *inode, struct file *h_file)
  14609. +{
  14610. + /* update without lock, I don't think it a problem */
  14611. + fsstack_copy_attr_atime(inode, file_inode(h_file));
  14612. + fput(h_file);
  14613. +}
  14614. +
  14615. +struct au_write_pre {
  14616. + /* input */
  14617. + unsigned int lsc;
  14618. +
  14619. + /* output */
  14620. + blkcnt_t blks;
  14621. + aufs_bindex_t btop;
  14622. +};
  14623. +
  14624. +/*
  14625. + * return with iinfo is write-locked
  14626. + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
  14627. + * end
  14628. + */
  14629. +static struct file *au_write_pre(struct file *file, int do_ready,
  14630. + struct au_write_pre *wpre)
  14631. +{
  14632. + struct file *h_file;
  14633. + struct dentry *dentry;
  14634. + int err;
  14635. + unsigned int lsc;
  14636. + struct au_pin pin;
  14637. +
  14638. + lsc = 0;
  14639. + if (wpre)
  14640. + lsc = wpre->lsc;
  14641. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
  14642. + h_file = ERR_PTR(err);
  14643. + if (unlikely(err))
  14644. + goto out;
  14645. +
  14646. + dentry = file->f_path.dentry;
  14647. + if (do_ready) {
  14648. + err = au_ready_to_write(file, -1, &pin);
  14649. + if (unlikely(err)) {
  14650. + h_file = ERR_PTR(err);
  14651. + di_write_unlock(dentry);
  14652. + goto out_fi;
  14653. + }
  14654. + }
  14655. +
  14656. + di_downgrade_lock(dentry, /*flags*/0);
  14657. + if (wpre)
  14658. + wpre->btop = au_fbtop(file);
  14659. + h_file = au_hf_top(file);
  14660. + get_file(h_file);
  14661. + if (wpre)
  14662. + wpre->blks = file_inode(h_file)->i_blocks;
  14663. + if (do_ready)
  14664. + au_unpin(&pin);
  14665. + di_read_unlock(dentry, /*flags*/0);
  14666. +
  14667. +out_fi:
  14668. + fi_write_unlock(file);
  14669. +out:
  14670. + return h_file;
  14671. +}
  14672. +
  14673. +static void au_write_post(struct inode *inode, struct file *h_file,
  14674. + struct au_write_pre *wpre, ssize_t written)
  14675. +{
  14676. + struct inode *h_inode;
  14677. +
  14678. + au_cpup_attr_timesizes(inode);
  14679. + AuDebugOn(au_ibtop(inode) != wpre->btop);
  14680. + h_inode = file_inode(h_file);
  14681. + inode->i_mode = h_inode->i_mode;
  14682. + ii_write_unlock(inode);
  14683. + /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
  14684. + if (written > 0)
  14685. + au_fhsm_wrote(inode->i_sb, wpre->btop,
  14686. + /*force*/h_inode->i_blocks > wpre->blks);
  14687. + fput(h_file);
  14688. +}
  14689. +
  14690. +static ssize_t aufs_read(struct file *file, char __user *buf, size_t count,
  14691. + loff_t *ppos)
  14692. +{
  14693. + ssize_t err;
  14694. + struct inode *inode;
  14695. + struct file *h_file;
  14696. + struct super_block *sb;
  14697. +
  14698. + inode = file_inode(file);
  14699. + sb = inode->i_sb;
  14700. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  14701. +
  14702. + h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
  14703. + err = PTR_ERR(h_file);
  14704. + if (IS_ERR(h_file))
  14705. + goto out;
  14706. +
  14707. + /* filedata may be obsoleted by concurrent copyup, but no problem */
  14708. + err = vfsub_read_u(h_file, buf, count, ppos);
  14709. + /* todo: necessary? */
  14710. + /* file->f_ra = h_file->f_ra; */
  14711. + au_read_post(inode, h_file);
  14712. +
  14713. +out:
  14714. + si_read_unlock(sb);
  14715. + return err;
  14716. +}
  14717. +
  14718. +/*
  14719. + * todo: very ugly
  14720. + * it locks both of i_mutex and si_rwsem for read in safe.
  14721. + * if the plink maintenance mode continues forever (that is the problem),
  14722. + * may loop forever.
  14723. + */
  14724. +static void au_mtx_and_read_lock(struct inode *inode)
  14725. +{
  14726. + int err;
  14727. + struct super_block *sb = inode->i_sb;
  14728. +
  14729. + while (1) {
  14730. + inode_lock(inode);
  14731. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  14732. + if (!err)
  14733. + break;
  14734. + inode_unlock(inode);
  14735. + si_read_lock(sb, AuLock_NOPLMW);
  14736. + si_read_unlock(sb);
  14737. + }
  14738. +}
  14739. +
  14740. +static ssize_t aufs_write(struct file *file, const char __user *ubuf,
  14741. + size_t count, loff_t *ppos)
  14742. +{
  14743. + ssize_t err;
  14744. + struct au_write_pre wpre;
  14745. + struct inode *inode;
  14746. + struct file *h_file;
  14747. + char __user *buf = (char __user *)ubuf;
  14748. +
  14749. + inode = file_inode(file);
  14750. + au_mtx_and_read_lock(inode);
  14751. +
  14752. + wpre.lsc = 0;
  14753. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  14754. + err = PTR_ERR(h_file);
  14755. + if (IS_ERR(h_file))
  14756. + goto out;
  14757. +
  14758. + err = vfsub_write_u(h_file, buf, count, ppos);
  14759. + au_write_post(inode, h_file, &wpre, err);
  14760. +
  14761. +out:
  14762. + si_read_unlock(inode->i_sb);
  14763. + inode_unlock(inode);
  14764. + return err;
  14765. +}
  14766. +
  14767. +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
  14768. + struct iov_iter *iov_iter)
  14769. +{
  14770. + ssize_t err;
  14771. + struct file *file;
  14772. + ssize_t (*iter)(struct kiocb *, struct iov_iter *);
  14773. +
  14774. + err = security_file_permission(h_file, rw);
  14775. + if (unlikely(err))
  14776. + goto out;
  14777. +
  14778. + err = -ENOSYS;
  14779. + iter = NULL;
  14780. + if (rw == MAY_READ)
  14781. + iter = h_file->f_op->read_iter;
  14782. + else if (rw == MAY_WRITE)
  14783. + iter = h_file->f_op->write_iter;
  14784. +
  14785. + file = kio->ki_filp;
  14786. + kio->ki_filp = h_file;
  14787. + if (iter) {
  14788. + lockdep_off();
  14789. + err = iter(kio, iov_iter);
  14790. + lockdep_on();
  14791. + } else
  14792. + /* currently there is no such fs */
  14793. + WARN_ON_ONCE(1);
  14794. + kio->ki_filp = file;
  14795. +
  14796. +out:
  14797. + return err;
  14798. +}
  14799. +
  14800. +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
  14801. +{
  14802. + ssize_t err;
  14803. + struct file *file, *h_file;
  14804. + struct inode *inode;
  14805. + struct super_block *sb;
  14806. +
  14807. + file = kio->ki_filp;
  14808. + inode = file_inode(file);
  14809. + sb = inode->i_sb;
  14810. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  14811. +
  14812. + h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
  14813. + err = PTR_ERR(h_file);
  14814. + if (IS_ERR(h_file))
  14815. + goto out;
  14816. +
  14817. + if (au_test_loopback_kthread()) {
  14818. + au_warn_loopback(h_file->f_path.dentry->d_sb);
  14819. + if (file->f_mapping != h_file->f_mapping) {
  14820. + file->f_mapping = h_file->f_mapping;
  14821. + smp_mb(); /* unnecessary? */
  14822. + }
  14823. + }
  14824. + fi_read_unlock(file);
  14825. +
  14826. + err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
  14827. + /* todo: necessary? */
  14828. + /* file->f_ra = h_file->f_ra; */
  14829. + au_read_post(inode, h_file);
  14830. +
  14831. +out:
  14832. + si_read_unlock(sb);
  14833. + return err;
  14834. +}
  14835. +
  14836. +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
  14837. +{
  14838. + ssize_t err;
  14839. + struct au_write_pre wpre;
  14840. + struct inode *inode;
  14841. + struct file *file, *h_file;
  14842. +
  14843. + file = kio->ki_filp;
  14844. + inode = file_inode(file);
  14845. + au_mtx_and_read_lock(inode);
  14846. +
  14847. + wpre.lsc = 0;
  14848. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  14849. + err = PTR_ERR(h_file);
  14850. + if (IS_ERR(h_file))
  14851. + goto out;
  14852. +
  14853. + err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
  14854. + au_write_post(inode, h_file, &wpre, err);
  14855. +
  14856. +out:
  14857. + si_read_unlock(inode->i_sb);
  14858. + inode_unlock(inode);
  14859. + return err;
  14860. +}
  14861. +
  14862. +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
  14863. + struct pipe_inode_info *pipe, size_t len,
  14864. + unsigned int flags)
  14865. +{
  14866. + ssize_t err;
  14867. + struct file *h_file;
  14868. + struct inode *inode;
  14869. + struct super_block *sb;
  14870. +
  14871. + inode = file_inode(file);
  14872. + sb = inode->i_sb;
  14873. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  14874. +
  14875. + h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
  14876. + err = PTR_ERR(h_file);
  14877. + if (IS_ERR(h_file))
  14878. + goto out;
  14879. +
  14880. + err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
  14881. + /* todo: necessary? */
  14882. + /* file->f_ra = h_file->f_ra; */
  14883. + au_read_post(inode, h_file);
  14884. +
  14885. +out:
  14886. + si_read_unlock(sb);
  14887. + return err;
  14888. +}
  14889. +
  14890. +static ssize_t
  14891. +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
  14892. + size_t len, unsigned int flags)
  14893. +{
  14894. + ssize_t err;
  14895. + struct au_write_pre wpre;
  14896. + struct inode *inode;
  14897. + struct file *h_file;
  14898. +
  14899. + inode = file_inode(file);
  14900. + au_mtx_and_read_lock(inode);
  14901. +
  14902. + wpre.lsc = 0;
  14903. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  14904. + err = PTR_ERR(h_file);
  14905. + if (IS_ERR(h_file))
  14906. + goto out;
  14907. +
  14908. + err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
  14909. + au_write_post(inode, h_file, &wpre, err);
  14910. +
  14911. +out:
  14912. + si_read_unlock(inode->i_sb);
  14913. + inode_unlock(inode);
  14914. + return err;
  14915. +}
  14916. +
  14917. +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
  14918. + loff_t len)
  14919. +{
  14920. + long err;
  14921. + struct au_write_pre wpre;
  14922. + struct inode *inode;
  14923. + struct file *h_file;
  14924. +
  14925. + inode = file_inode(file);
  14926. + au_mtx_and_read_lock(inode);
  14927. +
  14928. + wpre.lsc = 0;
  14929. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  14930. + err = PTR_ERR(h_file);
  14931. + if (IS_ERR(h_file))
  14932. + goto out;
  14933. +
  14934. + lockdep_off();
  14935. + err = vfs_fallocate(h_file, mode, offset, len);
  14936. + lockdep_on();
  14937. + au_write_post(inode, h_file, &wpre, /*written*/1);
  14938. +
  14939. +out:
  14940. + si_read_unlock(inode->i_sb);
  14941. + inode_unlock(inode);
  14942. + return err;
  14943. +}
  14944. +
  14945. +static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
  14946. + struct file *dst, loff_t dst_pos,
  14947. + size_t len, unsigned int flags)
  14948. +{
  14949. + ssize_t err;
  14950. + struct au_write_pre wpre;
  14951. + enum { SRC, DST };
  14952. + struct {
  14953. + struct inode *inode;
  14954. + struct file *h_file;
  14955. + struct super_block *h_sb;
  14956. + } a[2];
  14957. +#define a_src a[SRC]
  14958. +#define a_dst a[DST]
  14959. +
  14960. + err = -EINVAL;
  14961. + a_src.inode = file_inode(src);
  14962. + if (unlikely(!S_ISREG(a_src.inode->i_mode)))
  14963. + goto out;
  14964. + a_dst.inode = file_inode(dst);
  14965. + if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
  14966. + goto out;
  14967. +
  14968. + au_mtx_and_read_lock(a_dst.inode);
  14969. + /*
  14970. + * in order to match the order in di_write_lock2_{child,parent}(),
  14971. + * use f_path.dentry for this comparison.
  14972. + */
  14973. + if (src->f_path.dentry < dst->f_path.dentry) {
  14974. + a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
  14975. + err = PTR_ERR(a_src.h_file);
  14976. + if (IS_ERR(a_src.h_file))
  14977. + goto out_si;
  14978. +
  14979. + wpre.lsc = AuLsc_FI_2;
  14980. + a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
  14981. + err = PTR_ERR(a_dst.h_file);
  14982. + if (IS_ERR(a_dst.h_file)) {
  14983. + au_read_post(a_src.inode, a_src.h_file);
  14984. + goto out_si;
  14985. + }
  14986. + } else {
  14987. + wpre.lsc = AuLsc_FI_1;
  14988. + a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
  14989. + err = PTR_ERR(a_dst.h_file);
  14990. + if (IS_ERR(a_dst.h_file))
  14991. + goto out_si;
  14992. +
  14993. + a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
  14994. + err = PTR_ERR(a_src.h_file);
  14995. + if (IS_ERR(a_src.h_file)) {
  14996. + au_write_post(a_dst.inode, a_dst.h_file, &wpre,
  14997. + /*written*/0);
  14998. + goto out_si;
  14999. + }
  15000. + }
  15001. +
  15002. + err = -EXDEV;
  15003. + a_src.h_sb = file_inode(a_src.h_file)->i_sb;
  15004. + a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
  15005. + if (unlikely(a_src.h_sb != a_dst.h_sb)) {
  15006. + AuDbgFile(src);
  15007. + AuDbgFile(dst);
  15008. + goto out_file;
  15009. + }
  15010. +
  15011. + err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
  15012. + dst_pos, len, flags);
  15013. +
  15014. +out_file:
  15015. + au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
  15016. + fi_read_unlock(src);
  15017. + au_read_post(a_src.inode, a_src.h_file);
  15018. +out_si:
  15019. + si_read_unlock(a_dst.inode->i_sb);
  15020. + inode_unlock(a_dst.inode);
  15021. +out:
  15022. + return err;
  15023. +#undef a_src
  15024. +#undef a_dst
  15025. +}
  15026. +
  15027. +/* ---------------------------------------------------------------------- */
  15028. +
  15029. +/*
  15030. + * The locking order around current->mmap_sem.
  15031. + * - in most and regular cases
  15032. + * file I/O syscall -- aufs_read() or something
  15033. + * -- si_rwsem for read -- mmap_sem
  15034. + * (Note that [fdi]i_rwsem are released before mmap_sem).
  15035. + * - in mmap case
  15036. + * mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
  15037. + * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
  15038. + * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
  15039. + * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
  15040. + * It means that when aufs acquires si_rwsem for write, the process should never
  15041. + * acquire mmap_sem.
  15042. + *
  15043. + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
  15044. + * problem either since any directory is not able to be mmap-ed.
  15045. + * The similar scenario is applied to aufs_readlink() too.
  15046. + */
  15047. +
  15048. +#if 0 /* stop calling security_file_mmap() */
  15049. +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
  15050. +#define AuConv_VM_PROT(f, b) _calc_vm_trans(f, VM_##b, PROT_##b)
  15051. +
  15052. +static unsigned long au_arch_prot_conv(unsigned long flags)
  15053. +{
  15054. + /* currently ppc64 only */
  15055. +#ifdef CONFIG_PPC64
  15056. + /* cf. linux/arch/powerpc/include/asm/mman.h */
  15057. + AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
  15058. + return AuConv_VM_PROT(flags, SAO);
  15059. +#else
  15060. + AuDebugOn(arch_calc_vm_prot_bits(-1));
  15061. + return 0;
  15062. +#endif
  15063. +}
  15064. +
  15065. +static unsigned long au_prot_conv(unsigned long flags)
  15066. +{
  15067. + return AuConv_VM_PROT(flags, READ)
  15068. + | AuConv_VM_PROT(flags, WRITE)
  15069. + | AuConv_VM_PROT(flags, EXEC)
  15070. + | au_arch_prot_conv(flags);
  15071. +}
  15072. +
  15073. +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
  15074. +#define AuConv_VM_MAP(f, b) _calc_vm_trans(f, VM_##b, MAP_##b)
  15075. +
  15076. +static unsigned long au_flag_conv(unsigned long flags)
  15077. +{
  15078. + return AuConv_VM_MAP(flags, GROWSDOWN)
  15079. + | AuConv_VM_MAP(flags, DENYWRITE)
  15080. + | AuConv_VM_MAP(flags, LOCKED);
  15081. +}
  15082. +#endif
  15083. +
  15084. +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
  15085. +{
  15086. + int err;
  15087. + const unsigned char wlock
  15088. + = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
  15089. + struct super_block *sb;
  15090. + struct file *h_file;
  15091. + struct inode *inode;
  15092. +
  15093. + AuDbgVmRegion(file, vma);
  15094. +
  15095. + inode = file_inode(file);
  15096. + sb = inode->i_sb;
  15097. + lockdep_off();
  15098. + si_read_lock(sb, AuLock_NOPLMW);
  15099. +
  15100. + h_file = au_write_pre(file, wlock, /*wpre*/NULL);
  15101. + lockdep_on();
  15102. + err = PTR_ERR(h_file);
  15103. + if (IS_ERR(h_file))
  15104. + goto out;
  15105. +
  15106. + err = 0;
  15107. + au_set_mmapped(file);
  15108. + au_vm_file_reset(vma, h_file);
  15109. + /*
  15110. + * we cannot call security_mmap_file() here since it may acquire
  15111. + * mmap_sem or i_mutex.
  15112. + *
  15113. + * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
  15114. + * au_flag_conv(vma->vm_flags));
  15115. + */
  15116. + if (!err)
  15117. + err = call_mmap(h_file, vma);
  15118. + if (!err) {
  15119. + au_vm_prfile_set(vma, file);
  15120. + fsstack_copy_attr_atime(inode, file_inode(h_file));
  15121. + goto out_fput; /* success */
  15122. + }
  15123. + au_unset_mmapped(file);
  15124. + au_vm_file_reset(vma, file);
  15125. +
  15126. +out_fput:
  15127. + lockdep_off();
  15128. + ii_write_unlock(inode);
  15129. + lockdep_on();
  15130. + fput(h_file);
  15131. +out:
  15132. + lockdep_off();
  15133. + si_read_unlock(sb);
  15134. + lockdep_on();
  15135. + AuTraceErr(err);
  15136. + return err;
  15137. +}
  15138. +
  15139. +/* ---------------------------------------------------------------------- */
  15140. +
  15141. +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
  15142. + int datasync)
  15143. +{
  15144. + int err;
  15145. + struct au_write_pre wpre;
  15146. + struct inode *inode;
  15147. + struct file *h_file;
  15148. +
  15149. + err = 0; /* -EBADF; */ /* posix? */
  15150. + if (unlikely(!(file->f_mode & FMODE_WRITE)))
  15151. + goto out;
  15152. +
  15153. + inode = file_inode(file);
  15154. + au_mtx_and_read_lock(inode);
  15155. +
  15156. + wpre.lsc = 0;
  15157. + h_file = au_write_pre(file, /*do_ready*/1, &wpre);
  15158. + err = PTR_ERR(h_file);
  15159. + if (IS_ERR(h_file))
  15160. + goto out_unlock;
  15161. +
  15162. + err = vfsub_fsync(h_file, &h_file->f_path, datasync);
  15163. + au_write_post(inode, h_file, &wpre, /*written*/0);
  15164. +
  15165. +out_unlock:
  15166. + si_read_unlock(inode->i_sb);
  15167. + inode_unlock(inode);
  15168. +out:
  15169. + return err;
  15170. +}
  15171. +
  15172. +static int aufs_fasync(int fd, struct file *file, int flag)
  15173. +{
  15174. + int err;
  15175. + struct file *h_file;
  15176. + struct super_block *sb;
  15177. +
  15178. + sb = file->f_path.dentry->d_sb;
  15179. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  15180. +
  15181. + h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
  15182. + err = PTR_ERR(h_file);
  15183. + if (IS_ERR(h_file))
  15184. + goto out;
  15185. +
  15186. + if (h_file->f_op->fasync)
  15187. + err = h_file->f_op->fasync(fd, h_file, flag);
  15188. + fput(h_file); /* instead of au_read_post() */
  15189. +
  15190. +out:
  15191. + si_read_unlock(sb);
  15192. + return err;
  15193. +}
  15194. +
  15195. +static int aufs_setfl(struct file *file, unsigned long arg)
  15196. +{
  15197. + int err;
  15198. + struct file *h_file;
  15199. + struct super_block *sb;
  15200. +
  15201. + sb = file->f_path.dentry->d_sb;
  15202. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  15203. +
  15204. + h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
  15205. + err = PTR_ERR(h_file);
  15206. + if (IS_ERR(h_file))
  15207. + goto out;
  15208. +
  15209. + /* stop calling h_file->fasync */
  15210. + arg |= vfsub_file_flags(file) & FASYNC;
  15211. + err = setfl(/*unused fd*/-1, h_file, arg);
  15212. + fput(h_file); /* instead of au_read_post() */
  15213. +
  15214. +out:
  15215. + si_read_unlock(sb);
  15216. + return err;
  15217. +}
  15218. +
  15219. +/* ---------------------------------------------------------------------- */
  15220. +
  15221. +/* no one supports this operation, currently */
  15222. +#if 0
  15223. +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
  15224. + size_t len, loff_t *pos, int more)
  15225. +{
  15226. +}
  15227. +#endif
  15228. +
  15229. +/* ---------------------------------------------------------------------- */
  15230. +
  15231. +const struct file_operations aufs_file_fop = {
  15232. + .owner = THIS_MODULE,
  15233. +
  15234. + .llseek = default_llseek,
  15235. +
  15236. + .read = aufs_read,
  15237. + .write = aufs_write,
  15238. + .read_iter = aufs_read_iter,
  15239. + .write_iter = aufs_write_iter,
  15240. +
  15241. +#ifdef CONFIG_AUFS_POLL
  15242. + .poll = aufs_poll,
  15243. +#endif
  15244. + .unlocked_ioctl = aufs_ioctl_nondir,
  15245. +#ifdef CONFIG_COMPAT
  15246. + .compat_ioctl = aufs_compat_ioctl_nondir,
  15247. +#endif
  15248. + .mmap = aufs_mmap,
  15249. + .open = aufs_open_nondir,
  15250. + .flush = aufs_flush_nondir,
  15251. + .release = aufs_release_nondir,
  15252. + .fsync = aufs_fsync_nondir,
  15253. + .fasync = aufs_fasync,
  15254. + /* .sendpage = aufs_sendpage, */
  15255. + .setfl = aufs_setfl,
  15256. + .splice_write = aufs_splice_write,
  15257. + .splice_read = aufs_splice_read,
  15258. +#if 0
  15259. + .aio_splice_write = aufs_aio_splice_write,
  15260. + .aio_splice_read = aufs_aio_splice_read,
  15261. +#endif
  15262. + .fallocate = aufs_fallocate,
  15263. + .copy_file_range = aufs_copy_file_range
  15264. +};
  15265. diff -Naur null/fs/aufs/fstype.h linux-5.1/fs/aufs/fstype.h
  15266. --- /dev/null
  15267. +++ linux-5.1/fs/aufs/fstype.h 2019-05-22 11:23:07.440141042 +0200
  15268. @@ -0,0 +1,401 @@
  15269. +/* SPDX-License-Identifier: GPL-2.0 */
  15270. +/*
  15271. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  15272. + *
  15273. + * This program, aufs is free software; you can redistribute it and/or modify
  15274. + * it under the terms of the GNU General Public License as published by
  15275. + * the Free Software Foundation; either version 2 of the License, or
  15276. + * (at your option) any later version.
  15277. + *
  15278. + * This program is distributed in the hope that it will be useful,
  15279. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15280. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15281. + * GNU General Public License for more details.
  15282. + *
  15283. + * You should have received a copy of the GNU General Public License
  15284. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15285. + */
  15286. +
  15287. +/*
  15288. + * judging filesystem type
  15289. + */
  15290. +
  15291. +#ifndef __AUFS_FSTYPE_H__
  15292. +#define __AUFS_FSTYPE_H__
  15293. +
  15294. +#ifdef __KERNEL__
  15295. +
  15296. +#include <linux/fs.h>
  15297. +#include <linux/magic.h>
  15298. +#include <linux/nfs_fs.h>
  15299. +#include <linux/romfs_fs.h>
  15300. +
  15301. +static inline int au_test_aufs(struct super_block *sb)
  15302. +{
  15303. + return sb->s_magic == AUFS_SUPER_MAGIC;
  15304. +}
  15305. +
  15306. +static inline const char *au_sbtype(struct super_block *sb)
  15307. +{
  15308. + return sb->s_type->name;
  15309. +}
  15310. +
  15311. +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
  15312. +{
  15313. +#if IS_ENABLED(CONFIG_ISO9660_FS)
  15314. + return sb->s_magic == ISOFS_SUPER_MAGIC;
  15315. +#else
  15316. + return 0;
  15317. +#endif
  15318. +}
  15319. +
  15320. +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
  15321. +{
  15322. +#if IS_ENABLED(CONFIG_ROMFS_FS)
  15323. + return sb->s_magic == ROMFS_MAGIC;
  15324. +#else
  15325. + return 0;
  15326. +#endif
  15327. +}
  15328. +
  15329. +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
  15330. +{
  15331. +#if IS_ENABLED(CONFIG_CRAMFS)
  15332. + return sb->s_magic == CRAMFS_MAGIC;
  15333. +#endif
  15334. + return 0;
  15335. +}
  15336. +
  15337. +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
  15338. +{
  15339. +#if IS_ENABLED(CONFIG_NFS_FS)
  15340. + return sb->s_magic == NFS_SUPER_MAGIC;
  15341. +#else
  15342. + return 0;
  15343. +#endif
  15344. +}
  15345. +
  15346. +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
  15347. +{
  15348. +#if IS_ENABLED(CONFIG_FUSE_FS)
  15349. + return sb->s_magic == FUSE_SUPER_MAGIC;
  15350. +#else
  15351. + return 0;
  15352. +#endif
  15353. +}
  15354. +
  15355. +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
  15356. +{
  15357. +#if IS_ENABLED(CONFIG_XFS_FS)
  15358. + return sb->s_magic == XFS_SB_MAGIC;
  15359. +#else
  15360. + return 0;
  15361. +#endif
  15362. +}
  15363. +
  15364. +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
  15365. +{
  15366. +#ifdef CONFIG_TMPFS
  15367. + return sb->s_magic == TMPFS_MAGIC;
  15368. +#else
  15369. + return 0;
  15370. +#endif
  15371. +}
  15372. +
  15373. +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
  15374. +{
  15375. +#if IS_ENABLED(CONFIG_ECRYPT_FS)
  15376. + return !strcmp(au_sbtype(sb), "ecryptfs");
  15377. +#else
  15378. + return 0;
  15379. +#endif
  15380. +}
  15381. +
  15382. +static inline int au_test_ramfs(struct super_block *sb)
  15383. +{
  15384. + return sb->s_magic == RAMFS_MAGIC;
  15385. +}
  15386. +
  15387. +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
  15388. +{
  15389. +#if IS_ENABLED(CONFIG_UBIFS_FS)
  15390. + return sb->s_magic == UBIFS_SUPER_MAGIC;
  15391. +#else
  15392. + return 0;
  15393. +#endif
  15394. +}
  15395. +
  15396. +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
  15397. +{
  15398. +#ifdef CONFIG_PROC_FS
  15399. + return sb->s_magic == PROC_SUPER_MAGIC;
  15400. +#else
  15401. + return 0;
  15402. +#endif
  15403. +}
  15404. +
  15405. +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
  15406. +{
  15407. +#ifdef CONFIG_SYSFS
  15408. + return sb->s_magic == SYSFS_MAGIC;
  15409. +#else
  15410. + return 0;
  15411. +#endif
  15412. +}
  15413. +
  15414. +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
  15415. +{
  15416. +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
  15417. + return sb->s_magic == CONFIGFS_MAGIC;
  15418. +#else
  15419. + return 0;
  15420. +#endif
  15421. +}
  15422. +
  15423. +static inline int au_test_minix(struct super_block *sb __maybe_unused)
  15424. +{
  15425. +#if IS_ENABLED(CONFIG_MINIX_FS)
  15426. + return sb->s_magic == MINIX3_SUPER_MAGIC
  15427. + || sb->s_magic == MINIX2_SUPER_MAGIC
  15428. + || sb->s_magic == MINIX2_SUPER_MAGIC2
  15429. + || sb->s_magic == MINIX_SUPER_MAGIC
  15430. + || sb->s_magic == MINIX_SUPER_MAGIC2;
  15431. +#else
  15432. + return 0;
  15433. +#endif
  15434. +}
  15435. +
  15436. +static inline int au_test_fat(struct super_block *sb __maybe_unused)
  15437. +{
  15438. +#if IS_ENABLED(CONFIG_FAT_FS)
  15439. + return sb->s_magic == MSDOS_SUPER_MAGIC;
  15440. +#else
  15441. + return 0;
  15442. +#endif
  15443. +}
  15444. +
  15445. +static inline int au_test_msdos(struct super_block *sb)
  15446. +{
  15447. + return au_test_fat(sb);
  15448. +}
  15449. +
  15450. +static inline int au_test_vfat(struct super_block *sb)
  15451. +{
  15452. + return au_test_fat(sb);
  15453. +}
  15454. +
  15455. +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
  15456. +{
  15457. +#ifdef CONFIG_SECURITYFS
  15458. + return sb->s_magic == SECURITYFS_MAGIC;
  15459. +#else
  15460. + return 0;
  15461. +#endif
  15462. +}
  15463. +
  15464. +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
  15465. +{
  15466. +#if IS_ENABLED(CONFIG_SQUASHFS)
  15467. + return sb->s_magic == SQUASHFS_MAGIC;
  15468. +#else
  15469. + return 0;
  15470. +#endif
  15471. +}
  15472. +
  15473. +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
  15474. +{
  15475. +#if IS_ENABLED(CONFIG_BTRFS_FS)
  15476. + return sb->s_magic == BTRFS_SUPER_MAGIC;
  15477. +#else
  15478. + return 0;
  15479. +#endif
  15480. +}
  15481. +
  15482. +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
  15483. +{
  15484. +#if IS_ENABLED(CONFIG_XENFS)
  15485. + return sb->s_magic == XENFS_SUPER_MAGIC;
  15486. +#else
  15487. + return 0;
  15488. +#endif
  15489. +}
  15490. +
  15491. +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
  15492. +{
  15493. +#ifdef CONFIG_DEBUG_FS
  15494. + return sb->s_magic == DEBUGFS_MAGIC;
  15495. +#else
  15496. + return 0;
  15497. +#endif
  15498. +}
  15499. +
  15500. +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
  15501. +{
  15502. +#if IS_ENABLED(CONFIG_NILFS)
  15503. + return sb->s_magic == NILFS_SUPER_MAGIC;
  15504. +#else
  15505. + return 0;
  15506. +#endif
  15507. +}
  15508. +
  15509. +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
  15510. +{
  15511. +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
  15512. + return sb->s_magic == HFSPLUS_SUPER_MAGIC;
  15513. +#else
  15514. + return 0;
  15515. +#endif
  15516. +}
  15517. +
  15518. +/* ---------------------------------------------------------------------- */
  15519. +/*
  15520. + * they can't be an aufs branch.
  15521. + */
  15522. +static inline int au_test_fs_unsuppoted(struct super_block *sb)
  15523. +{
  15524. + return
  15525. +#ifndef CONFIG_AUFS_BR_RAMFS
  15526. + au_test_ramfs(sb) ||
  15527. +#endif
  15528. + au_test_procfs(sb)
  15529. + || au_test_sysfs(sb)
  15530. + || au_test_configfs(sb)
  15531. + || au_test_debugfs(sb)
  15532. + || au_test_securityfs(sb)
  15533. + || au_test_xenfs(sb)
  15534. + || au_test_ecryptfs(sb)
  15535. + /* || !strcmp(au_sbtype(sb), "unionfs") */
  15536. + || au_test_aufs(sb); /* will be supported in next version */
  15537. +}
  15538. +
  15539. +static inline int au_test_fs_remote(struct super_block *sb)
  15540. +{
  15541. + return !au_test_tmpfs(sb)
  15542. +#ifdef CONFIG_AUFS_BR_RAMFS
  15543. + && !au_test_ramfs(sb)
  15544. +#endif
  15545. + && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
  15546. +}
  15547. +
  15548. +/* ---------------------------------------------------------------------- */
  15549. +
  15550. +/*
  15551. + * Note: these functions (below) are created after reading ->getattr() in all
  15552. + * filesystems under linux/fs. it means we have to do so in every update...
  15553. + */
  15554. +
  15555. +/*
  15556. + * some filesystems require getattr to refresh the inode attributes before
  15557. + * referencing.
  15558. + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
  15559. + * and leave the work for d_revalidate()
  15560. + */
  15561. +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
  15562. +{
  15563. + return au_test_nfs(sb)
  15564. + || au_test_fuse(sb)
  15565. + /* || au_test_btrfs(sb) */ /* untested */
  15566. + ;
  15567. +}
  15568. +
  15569. +/*
  15570. + * filesystems which don't maintain i_size or i_blocks.
  15571. + */
  15572. +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
  15573. +{
  15574. + return au_test_xfs(sb)
  15575. + || au_test_btrfs(sb)
  15576. + || au_test_ubifs(sb)
  15577. + || au_test_hfsplus(sb) /* maintained, but incorrect */
  15578. + /* || au_test_minix(sb) */ /* untested */
  15579. + ;
  15580. +}
  15581. +
  15582. +/*
  15583. + * filesystems which don't store the correct value in some of their inode
  15584. + * attributes.
  15585. + */
  15586. +static inline int au_test_fs_bad_iattr(struct super_block *sb)
  15587. +{
  15588. + return au_test_fs_bad_iattr_size(sb)
  15589. + || au_test_fat(sb)
  15590. + || au_test_msdos(sb)
  15591. + || au_test_vfat(sb);
  15592. +}
  15593. +
  15594. +/* they don't check i_nlink in link(2) */
  15595. +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
  15596. +{
  15597. + return au_test_tmpfs(sb)
  15598. +#ifdef CONFIG_AUFS_BR_RAMFS
  15599. + || au_test_ramfs(sb)
  15600. +#endif
  15601. + || au_test_ubifs(sb)
  15602. + || au_test_hfsplus(sb);
  15603. +}
  15604. +
  15605. +/*
  15606. + * filesystems which sets S_NOATIME and S_NOCMTIME.
  15607. + */
  15608. +static inline int au_test_fs_notime(struct super_block *sb)
  15609. +{
  15610. + return au_test_nfs(sb)
  15611. + || au_test_fuse(sb)
  15612. + || au_test_ubifs(sb)
  15613. + ;
  15614. +}
  15615. +
  15616. +/* temporary support for i#1 in cramfs */
  15617. +static inline int au_test_fs_unique_ino(struct inode *inode)
  15618. +{
  15619. + if (au_test_cramfs(inode->i_sb))
  15620. + return inode->i_ino != 1;
  15621. + return 1;
  15622. +}
  15623. +
  15624. +/* ---------------------------------------------------------------------- */
  15625. +
  15626. +/*
  15627. + * the filesystem where the xino files placed must support i/o after unlink and
  15628. + * maintain i_size and i_blocks.
  15629. + */
  15630. +static inline int au_test_fs_bad_xino(struct super_block *sb)
  15631. +{
  15632. + return au_test_fs_remote(sb)
  15633. + || au_test_fs_bad_iattr_size(sb)
  15634. + /* don't want unnecessary work for xino */
  15635. + || au_test_aufs(sb)
  15636. + || au_test_ecryptfs(sb)
  15637. + || au_test_nilfs(sb);
  15638. +}
  15639. +
  15640. +static inline int au_test_fs_trunc_xino(struct super_block *sb)
  15641. +{
  15642. + return au_test_tmpfs(sb)
  15643. + || au_test_ramfs(sb);
  15644. +}
  15645. +
  15646. +/*
  15647. + * test if the @sb is real-readonly.
  15648. + */
  15649. +static inline int au_test_fs_rr(struct super_block *sb)
  15650. +{
  15651. + return au_test_squashfs(sb)
  15652. + || au_test_iso9660(sb)
  15653. + || au_test_cramfs(sb)
  15654. + || au_test_romfs(sb);
  15655. +}
  15656. +
  15657. +/*
  15658. + * test if the @inode is nfs with 'noacl' option
  15659. + * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
  15660. + */
  15661. +static inline int au_test_nfs_noacl(struct inode *inode)
  15662. +{
  15663. + return au_test_nfs(inode->i_sb)
  15664. + /* && IS_POSIXACL(inode) */
  15665. + && !nfs_server_capable(inode, NFS_CAP_ACLS);
  15666. +}
  15667. +
  15668. +#endif /* __KERNEL__ */
  15669. +#endif /* __AUFS_FSTYPE_H__ */
  15670. diff -Naur null/fs/aufs/hbl.h linux-5.1/fs/aufs/hbl.h
  15671. --- /dev/null
  15672. +++ linux-5.1/fs/aufs/hbl.h 2019-05-22 11:23:07.440141042 +0200
  15673. @@ -0,0 +1,65 @@
  15674. +/* SPDX-License-Identifier: GPL-2.0 */
  15675. +/*
  15676. + * Copyright (C) 2017-2019 Junjiro R. Okajima
  15677. + *
  15678. + * This program, aufs is free software; you can redistribute it and/or modify
  15679. + * it under the terms of the GNU General Public License as published by
  15680. + * the Free Software Foundation; either version 2 of the License, or
  15681. + * (at your option) any later version.
  15682. + *
  15683. + * This program is distributed in the hope that it will be useful,
  15684. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15685. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15686. + * GNU General Public License for more details.
  15687. + *
  15688. + * You should have received a copy of the GNU General Public License
  15689. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15690. + */
  15691. +
  15692. +/*
  15693. + * helpers for hlist_bl.h
  15694. + */
  15695. +
  15696. +#ifndef __AUFS_HBL_H__
  15697. +#define __AUFS_HBL_H__
  15698. +
  15699. +#ifdef __KERNEL__
  15700. +
  15701. +#include <linux/list_bl.h>
  15702. +
  15703. +static inline void au_hbl_add(struct hlist_bl_node *node,
  15704. + struct hlist_bl_head *hbl)
  15705. +{
  15706. + hlist_bl_lock(hbl);
  15707. + hlist_bl_add_head(node, hbl);
  15708. + hlist_bl_unlock(hbl);
  15709. +}
  15710. +
  15711. +static inline void au_hbl_del(struct hlist_bl_node *node,
  15712. + struct hlist_bl_head *hbl)
  15713. +{
  15714. + hlist_bl_lock(hbl);
  15715. + hlist_bl_del(node);
  15716. + hlist_bl_unlock(hbl);
  15717. +}
  15718. +
  15719. +#define au_hbl_for_each(pos, head) \
  15720. + for (pos = hlist_bl_first(head); \
  15721. + pos; \
  15722. + pos = pos->next)
  15723. +
  15724. +static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
  15725. +{
  15726. + unsigned long cnt;
  15727. + struct hlist_bl_node *pos;
  15728. +
  15729. + cnt = 0;
  15730. + hlist_bl_lock(hbl);
  15731. + au_hbl_for_each(pos, hbl)
  15732. + cnt++;
  15733. + hlist_bl_unlock(hbl);
  15734. + return cnt;
  15735. +}
  15736. +
  15737. +#endif /* __KERNEL__ */
  15738. +#endif /* __AUFS_HBL_H__ */
  15739. diff -Naur null/fs/aufs/hfsnotify.c linux-5.1/fs/aufs/hfsnotify.c
  15740. --- /dev/null
  15741. +++ linux-5.1/fs/aufs/hfsnotify.c 2019-05-22 11:23:07.440141042 +0200
  15742. @@ -0,0 +1,289 @@
  15743. +// SPDX-License-Identifier: GPL-2.0
  15744. +/*
  15745. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  15746. + *
  15747. + * This program, aufs is free software; you can redistribute it and/or modify
  15748. + * it under the terms of the GNU General Public License as published by
  15749. + * the Free Software Foundation; either version 2 of the License, or
  15750. + * (at your option) any later version.
  15751. + *
  15752. + * This program is distributed in the hope that it will be useful,
  15753. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15754. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15755. + * GNU General Public License for more details.
  15756. + *
  15757. + * You should have received a copy of the GNU General Public License
  15758. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15759. + */
  15760. +
  15761. +/*
  15762. + * fsnotify for the lower directories
  15763. + */
  15764. +
  15765. +#include "aufs.h"
  15766. +
  15767. +/* FS_IN_IGNORED is unnecessary */
  15768. +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
  15769. + | FS_CREATE | FS_EVENT_ON_CHILD);
  15770. +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
  15771. +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
  15772. +
  15773. +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
  15774. +{
  15775. + struct au_hnotify *hn = container_of(mark, struct au_hnotify,
  15776. + hn_mark);
  15777. + /* AuDbg("here\n"); */
  15778. + au_cache_free_hnotify(hn);
  15779. + smp_mb__before_atomic(); /* for atomic64_dec */
  15780. + if (atomic64_dec_and_test(&au_hfsn_ifree))
  15781. + wake_up(&au_hfsn_wq);
  15782. +}
  15783. +
  15784. +static int au_hfsn_alloc(struct au_hinode *hinode)
  15785. +{
  15786. + int err;
  15787. + struct au_hnotify *hn;
  15788. + struct super_block *sb;
  15789. + struct au_branch *br;
  15790. + struct fsnotify_mark *mark;
  15791. + aufs_bindex_t bindex;
  15792. +
  15793. + hn = hinode->hi_notify;
  15794. + sb = hn->hn_aufs_inode->i_sb;
  15795. + bindex = au_br_index(sb, hinode->hi_id);
  15796. + br = au_sbr(sb, bindex);
  15797. + AuDebugOn(!br->br_hfsn);
  15798. +
  15799. + mark = &hn->hn_mark;
  15800. + fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
  15801. + mark->mask = AuHfsnMask;
  15802. + /*
  15803. + * by udba rename or rmdir, aufs assign a new inode to the known
  15804. + * h_inode, so specify 1 to allow dups.
  15805. + */
  15806. + lockdep_off();
  15807. + err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
  15808. + lockdep_on();
  15809. +
  15810. + return err;
  15811. +}
  15812. +
  15813. +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
  15814. +{
  15815. + struct fsnotify_mark *mark;
  15816. + unsigned long long ull;
  15817. + struct fsnotify_group *group;
  15818. +
  15819. + ull = atomic64_inc_return(&au_hfsn_ifree);
  15820. + BUG_ON(!ull);
  15821. +
  15822. + mark = &hn->hn_mark;
  15823. + spin_lock(&mark->lock);
  15824. + group = mark->group;
  15825. + fsnotify_get_group(group);
  15826. + spin_unlock(&mark->lock);
  15827. + lockdep_off();
  15828. + fsnotify_destroy_mark(mark, group);
  15829. + fsnotify_put_mark(mark);
  15830. + fsnotify_put_group(group);
  15831. + lockdep_on();
  15832. +
  15833. + /* free hn by myself */
  15834. + return 0;
  15835. +}
  15836. +
  15837. +/* ---------------------------------------------------------------------- */
  15838. +
  15839. +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
  15840. +{
  15841. + struct fsnotify_mark *mark;
  15842. +
  15843. + mark = &hinode->hi_notify->hn_mark;
  15844. + spin_lock(&mark->lock);
  15845. + if (do_set) {
  15846. + AuDebugOn(mark->mask & AuHfsnMask);
  15847. + mark->mask |= AuHfsnMask;
  15848. + } else {
  15849. + AuDebugOn(!(mark->mask & AuHfsnMask));
  15850. + mark->mask &= ~AuHfsnMask;
  15851. + }
  15852. + spin_unlock(&mark->lock);
  15853. + /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
  15854. +}
  15855. +
  15856. +/* ---------------------------------------------------------------------- */
  15857. +
  15858. +/* #define AuDbgHnotify */
  15859. +#ifdef AuDbgHnotify
  15860. +static char *au_hfsn_name(u32 mask)
  15861. +{
  15862. +#ifdef CONFIG_AUFS_DEBUG
  15863. +#define test_ret(flag) \
  15864. + do { \
  15865. + if (mask & flag) \
  15866. + return #flag; \
  15867. + } while (0)
  15868. + test_ret(FS_ACCESS);
  15869. + test_ret(FS_MODIFY);
  15870. + test_ret(FS_ATTRIB);
  15871. + test_ret(FS_CLOSE_WRITE);
  15872. + test_ret(FS_CLOSE_NOWRITE);
  15873. + test_ret(FS_OPEN);
  15874. + test_ret(FS_MOVED_FROM);
  15875. + test_ret(FS_MOVED_TO);
  15876. + test_ret(FS_CREATE);
  15877. + test_ret(FS_DELETE);
  15878. + test_ret(FS_DELETE_SELF);
  15879. + test_ret(FS_MOVE_SELF);
  15880. + test_ret(FS_UNMOUNT);
  15881. + test_ret(FS_Q_OVERFLOW);
  15882. + test_ret(FS_IN_IGNORED);
  15883. + test_ret(FS_ISDIR);
  15884. + test_ret(FS_IN_ONESHOT);
  15885. + test_ret(FS_EVENT_ON_CHILD);
  15886. + return "";
  15887. +#undef test_ret
  15888. +#else
  15889. + return "??";
  15890. +#endif
  15891. +}
  15892. +#endif
  15893. +
  15894. +/* ---------------------------------------------------------------------- */
  15895. +
  15896. +static void au_hfsn_free_group(struct fsnotify_group *group)
  15897. +{
  15898. + struct au_br_hfsnotify *hfsn = group->private;
  15899. +
  15900. + /* AuDbg("here\n"); */
  15901. + au_kfree_try_rcu(hfsn);
  15902. +}
  15903. +
  15904. +static int au_hfsn_handle_event(struct fsnotify_group *group,
  15905. + struct inode *inode,
  15906. + u32 mask, const void *data, int data_type,
  15907. + const unsigned char *file_name, u32 cookie,
  15908. + struct fsnotify_iter_info *iter_info)
  15909. +{
  15910. + int err;
  15911. + struct au_hnotify *hnotify;
  15912. + struct inode *h_dir, *h_inode;
  15913. + struct qstr h_child_qstr = QSTR_INIT(file_name, strlen(file_name));
  15914. + struct fsnotify_mark *inode_mark;
  15915. +
  15916. + AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
  15917. +
  15918. + err = 0;
  15919. + /* if FS_UNMOUNT happens, there must be another bug */
  15920. + AuDebugOn(mask & FS_UNMOUNT);
  15921. + if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
  15922. + goto out;
  15923. +
  15924. + h_dir = inode;
  15925. + h_inode = NULL;
  15926. +#ifdef AuDbgHnotify
  15927. + au_debug_on();
  15928. + if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
  15929. + || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
  15930. + AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
  15931. + h_dir->i_ino, mask, au_hfsn_name(mask),
  15932. + AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
  15933. + /* WARN_ON(1); */
  15934. + }
  15935. + au_debug_off();
  15936. +#endif
  15937. +
  15938. + inode_mark = fsnotify_iter_inode_mark(iter_info);
  15939. + AuDebugOn(!inode_mark);
  15940. + hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
  15941. + err = au_hnotify(h_dir, hnotify, mask, &h_child_qstr, h_inode);
  15942. +
  15943. +out:
  15944. + return err;
  15945. +}
  15946. +
  15947. +static struct fsnotify_ops au_hfsn_ops = {
  15948. + .handle_event = au_hfsn_handle_event,
  15949. + .free_group_priv = au_hfsn_free_group,
  15950. + .free_mark = au_hfsn_free_mark
  15951. +};
  15952. +
  15953. +/* ---------------------------------------------------------------------- */
  15954. +
  15955. +static void au_hfsn_fin_br(struct au_branch *br)
  15956. +{
  15957. + struct au_br_hfsnotify *hfsn;
  15958. +
  15959. + hfsn = br->br_hfsn;
  15960. + if (hfsn) {
  15961. + lockdep_off();
  15962. + fsnotify_put_group(hfsn->hfsn_group);
  15963. + lockdep_on();
  15964. + }
  15965. +}
  15966. +
  15967. +static int au_hfsn_init_br(struct au_branch *br, int perm)
  15968. +{
  15969. + int err;
  15970. + struct fsnotify_group *group;
  15971. + struct au_br_hfsnotify *hfsn;
  15972. +
  15973. + err = 0;
  15974. + br->br_hfsn = NULL;
  15975. + if (!au_br_hnotifyable(perm))
  15976. + goto out;
  15977. +
  15978. + err = -ENOMEM;
  15979. + hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
  15980. + if (unlikely(!hfsn))
  15981. + goto out;
  15982. +
  15983. + err = 0;
  15984. + group = fsnotify_alloc_group(&au_hfsn_ops);
  15985. + if (IS_ERR(group)) {
  15986. + err = PTR_ERR(group);
  15987. + pr_err("fsnotify_alloc_group() failed, %d\n", err);
  15988. + goto out_hfsn;
  15989. + }
  15990. +
  15991. + group->private = hfsn;
  15992. + hfsn->hfsn_group = group;
  15993. + br->br_hfsn = hfsn;
  15994. + goto out; /* success */
  15995. +
  15996. +out_hfsn:
  15997. + au_kfree_try_rcu(hfsn);
  15998. +out:
  15999. + return err;
  16000. +}
  16001. +
  16002. +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
  16003. +{
  16004. + int err;
  16005. +
  16006. + err = 0;
  16007. + if (!br->br_hfsn)
  16008. + err = au_hfsn_init_br(br, perm);
  16009. +
  16010. + return err;
  16011. +}
  16012. +
  16013. +/* ---------------------------------------------------------------------- */
  16014. +
  16015. +static void au_hfsn_fin(void)
  16016. +{
  16017. + AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
  16018. + wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
  16019. +}
  16020. +
  16021. +const struct au_hnotify_op au_hnotify_op = {
  16022. + .ctl = au_hfsn_ctl,
  16023. + .alloc = au_hfsn_alloc,
  16024. + .free = au_hfsn_free,
  16025. +
  16026. + .fin = au_hfsn_fin,
  16027. +
  16028. + .reset_br = au_hfsn_reset_br,
  16029. + .fin_br = au_hfsn_fin_br,
  16030. + .init_br = au_hfsn_init_br
  16031. +};
  16032. diff -Naur null/fs/aufs/hfsplus.c linux-5.1/fs/aufs/hfsplus.c
  16033. --- /dev/null
  16034. +++ linux-5.1/fs/aufs/hfsplus.c 2019-05-22 11:23:07.440141042 +0200
  16035. @@ -0,0 +1,60 @@
  16036. +// SPDX-License-Identifier: GPL-2.0
  16037. +/*
  16038. + * Copyright (C) 2010-2019 Junjiro R. Okajima
  16039. + *
  16040. + * This program, aufs is free software; you can redistribute it and/or modify
  16041. + * it under the terms of the GNU General Public License as published by
  16042. + * the Free Software Foundation; either version 2 of the License, or
  16043. + * (at your option) any later version.
  16044. + *
  16045. + * This program is distributed in the hope that it will be useful,
  16046. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16047. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16048. + * GNU General Public License for more details.
  16049. + *
  16050. + * You should have received a copy of the GNU General Public License
  16051. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16052. + */
  16053. +
  16054. +/*
  16055. + * special support for filesystems which acquires an inode mutex
  16056. + * at final closing a file, eg, hfsplus.
  16057. + *
  16058. + * This trick is very simple and stupid, just to open the file before really
  16059. + * necessary open to tell hfsplus that this is not the final closing.
  16060. + * The caller should call au_h_open_pre() after acquiring the inode mutex,
  16061. + * and au_h_open_post() after releasing it.
  16062. + */
  16063. +
  16064. +#include "aufs.h"
  16065. +
  16066. +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
  16067. + int force_wr)
  16068. +{
  16069. + struct file *h_file;
  16070. + struct dentry *h_dentry;
  16071. +
  16072. + h_dentry = au_h_dptr(dentry, bindex);
  16073. + AuDebugOn(!h_dentry);
  16074. + AuDebugOn(d_is_negative(h_dentry));
  16075. +
  16076. + h_file = NULL;
  16077. + if (au_test_hfsplus(h_dentry->d_sb)
  16078. + && d_is_reg(h_dentry))
  16079. + h_file = au_h_open(dentry, bindex,
  16080. + O_RDONLY | O_NOATIME | O_LARGEFILE,
  16081. + /*file*/NULL, force_wr);
  16082. + return h_file;
  16083. +}
  16084. +
  16085. +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
  16086. + struct file *h_file)
  16087. +{
  16088. + struct au_branch *br;
  16089. +
  16090. + if (h_file) {
  16091. + fput(h_file);
  16092. + br = au_sbr(dentry->d_sb, bindex);
  16093. + au_lcnt_dec(&br->br_nfiles);
  16094. + }
  16095. +}
  16096. diff -Naur null/fs/aufs/hnotify.c linux-5.1/fs/aufs/hnotify.c
  16097. --- /dev/null
  16098. +++ linux-5.1/fs/aufs/hnotify.c 2019-05-22 11:23:07.440141042 +0200
  16099. @@ -0,0 +1,721 @@
  16100. +// SPDX-License-Identifier: GPL-2.0
  16101. +/*
  16102. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  16103. + *
  16104. + * This program, aufs is free software; you can redistribute it and/or modify
  16105. + * it under the terms of the GNU General Public License as published by
  16106. + * the Free Software Foundation; either version 2 of the License, or
  16107. + * (at your option) any later version.
  16108. + *
  16109. + * This program is distributed in the hope that it will be useful,
  16110. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16111. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16112. + * GNU General Public License for more details.
  16113. + *
  16114. + * You should have received a copy of the GNU General Public License
  16115. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16116. + */
  16117. +
  16118. +/*
  16119. + * abstraction to notify the direct changes on lower directories
  16120. + */
  16121. +
  16122. +/* #include <linux/iversion.h> */
  16123. +#include "aufs.h"
  16124. +
  16125. +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
  16126. +{
  16127. + int err;
  16128. + struct au_hnotify *hn;
  16129. +
  16130. + err = -ENOMEM;
  16131. + hn = au_cache_alloc_hnotify();
  16132. + if (hn) {
  16133. + hn->hn_aufs_inode = inode;
  16134. + hinode->hi_notify = hn;
  16135. + err = au_hnotify_op.alloc(hinode);
  16136. + AuTraceErr(err);
  16137. + if (unlikely(err)) {
  16138. + hinode->hi_notify = NULL;
  16139. + au_cache_free_hnotify(hn);
  16140. + /*
  16141. + * The upper dir was removed by udba, but the same named
  16142. + * dir left. In this case, aufs assigns a new inode
  16143. + * number and set the monitor again.
  16144. + * For the lower dir, the old monitor is still left.
  16145. + */
  16146. + if (err == -EEXIST)
  16147. + err = 0;
  16148. + }
  16149. + }
  16150. +
  16151. + AuTraceErr(err);
  16152. + return err;
  16153. +}
  16154. +
  16155. +void au_hn_free(struct au_hinode *hinode)
  16156. +{
  16157. + struct au_hnotify *hn;
  16158. +
  16159. + hn = hinode->hi_notify;
  16160. + if (hn) {
  16161. + hinode->hi_notify = NULL;
  16162. + if (au_hnotify_op.free(hinode, hn))
  16163. + au_cache_free_hnotify(hn);
  16164. + }
  16165. +}
  16166. +
  16167. +/* ---------------------------------------------------------------------- */
  16168. +
  16169. +void au_hn_ctl(struct au_hinode *hinode, int do_set)
  16170. +{
  16171. + if (hinode->hi_notify)
  16172. + au_hnotify_op.ctl(hinode, do_set);
  16173. +}
  16174. +
  16175. +void au_hn_reset(struct inode *inode, unsigned int flags)
  16176. +{
  16177. + aufs_bindex_t bindex, bbot;
  16178. + struct inode *hi;
  16179. + struct dentry *iwhdentry;
  16180. +
  16181. + bbot = au_ibbot(inode);
  16182. + for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
  16183. + hi = au_h_iptr(inode, bindex);
  16184. + if (!hi)
  16185. + continue;
  16186. +
  16187. + /* inode_lock_nested(hi, AuLsc_I_CHILD); */
  16188. + iwhdentry = au_hi_wh(inode, bindex);
  16189. + if (iwhdentry)
  16190. + dget(iwhdentry);
  16191. + au_igrab(hi);
  16192. + au_set_h_iptr(inode, bindex, NULL, 0);
  16193. + au_set_h_iptr(inode, bindex, au_igrab(hi),
  16194. + flags & ~AuHi_XINO);
  16195. + iput(hi);
  16196. + dput(iwhdentry);
  16197. + /* inode_unlock(hi); */
  16198. + }
  16199. +}
  16200. +
  16201. +/* ---------------------------------------------------------------------- */
  16202. +
  16203. +static int hn_xino(struct inode *inode, struct inode *h_inode)
  16204. +{
  16205. + int err;
  16206. + aufs_bindex_t bindex, bbot, bfound, btop;
  16207. + struct inode *h_i;
  16208. +
  16209. + err = 0;
  16210. + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
  16211. + pr_warn("branch root dir was changed\n");
  16212. + goto out;
  16213. + }
  16214. +
  16215. + bfound = -1;
  16216. + bbot = au_ibbot(inode);
  16217. + btop = au_ibtop(inode);
  16218. +#if 0 /* reserved for future use */
  16219. + if (bindex == bbot) {
  16220. + /* keep this ino in rename case */
  16221. + goto out;
  16222. + }
  16223. +#endif
  16224. + for (bindex = btop; bindex <= bbot; bindex++)
  16225. + if (au_h_iptr(inode, bindex) == h_inode) {
  16226. + bfound = bindex;
  16227. + break;
  16228. + }
  16229. + if (bfound < 0)
  16230. + goto out;
  16231. +
  16232. + for (bindex = btop; bindex <= bbot; bindex++) {
  16233. + h_i = au_h_iptr(inode, bindex);
  16234. + if (!h_i)
  16235. + continue;
  16236. +
  16237. + err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
  16238. + /* ignore this error */
  16239. + /* bad action? */
  16240. + }
  16241. +
  16242. + /* children inode number will be broken */
  16243. +
  16244. +out:
  16245. + AuTraceErr(err);
  16246. + return err;
  16247. +}
  16248. +
  16249. +static int hn_gen_tree(struct dentry *dentry)
  16250. +{
  16251. + int err, i, j, ndentry;
  16252. + struct au_dcsub_pages dpages;
  16253. + struct au_dpage *dpage;
  16254. + struct dentry **dentries;
  16255. +
  16256. + err = au_dpages_init(&dpages, GFP_NOFS);
  16257. + if (unlikely(err))
  16258. + goto out;
  16259. + err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
  16260. + if (unlikely(err))
  16261. + goto out_dpages;
  16262. +
  16263. + for (i = 0; i < dpages.ndpage; i++) {
  16264. + dpage = dpages.dpages + i;
  16265. + dentries = dpage->dentries;
  16266. + ndentry = dpage->ndentry;
  16267. + for (j = 0; j < ndentry; j++) {
  16268. + struct dentry *d;
  16269. +
  16270. + d = dentries[j];
  16271. + if (IS_ROOT(d))
  16272. + continue;
  16273. +
  16274. + au_digen_dec(d);
  16275. + if (d_really_is_positive(d))
  16276. + /* todo: reset children xino?
  16277. + cached children only? */
  16278. + au_iigen_dec(d_inode(d));
  16279. + }
  16280. + }
  16281. +
  16282. +out_dpages:
  16283. + au_dpages_free(&dpages);
  16284. +
  16285. +#if 0
  16286. + /* discard children */
  16287. + dentry_unhash(dentry);
  16288. + dput(dentry);
  16289. +#endif
  16290. +out:
  16291. + return err;
  16292. +}
  16293. +
  16294. +/*
  16295. + * return 0 if processed.
  16296. + */
  16297. +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
  16298. + const unsigned int isdir)
  16299. +{
  16300. + int err;
  16301. + struct dentry *d;
  16302. + struct qstr *dname;
  16303. +
  16304. + err = 1;
  16305. + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
  16306. + pr_warn("branch root dir was changed\n");
  16307. + err = 0;
  16308. + goto out;
  16309. + }
  16310. +
  16311. + if (!isdir) {
  16312. + AuDebugOn(!name);
  16313. + au_iigen_dec(inode);
  16314. + spin_lock(&inode->i_lock);
  16315. + hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
  16316. + spin_lock(&d->d_lock);
  16317. + dname = &d->d_name;
  16318. + if (dname->len != nlen
  16319. + && memcmp(dname->name, name, nlen)) {
  16320. + spin_unlock(&d->d_lock);
  16321. + continue;
  16322. + }
  16323. + err = 0;
  16324. + au_digen_dec(d);
  16325. + spin_unlock(&d->d_lock);
  16326. + break;
  16327. + }
  16328. + spin_unlock(&inode->i_lock);
  16329. + } else {
  16330. + au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
  16331. + d = d_find_any_alias(inode);
  16332. + if (!d) {
  16333. + au_iigen_dec(inode);
  16334. + goto out;
  16335. + }
  16336. +
  16337. + spin_lock(&d->d_lock);
  16338. + dname = &d->d_name;
  16339. + if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
  16340. + spin_unlock(&d->d_lock);
  16341. + err = hn_gen_tree(d);
  16342. + spin_lock(&d->d_lock);
  16343. + }
  16344. + spin_unlock(&d->d_lock);
  16345. + dput(d);
  16346. + }
  16347. +
  16348. +out:
  16349. + AuTraceErr(err);
  16350. + return err;
  16351. +}
  16352. +
  16353. +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
  16354. +{
  16355. + int err;
  16356. +
  16357. + if (IS_ROOT(dentry)) {
  16358. + pr_warn("branch root dir was changed\n");
  16359. + return 0;
  16360. + }
  16361. +
  16362. + err = 0;
  16363. + if (!isdir) {
  16364. + au_digen_dec(dentry);
  16365. + if (d_really_is_positive(dentry))
  16366. + au_iigen_dec(d_inode(dentry));
  16367. + } else {
  16368. + au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
  16369. + if (d_really_is_positive(dentry))
  16370. + err = hn_gen_tree(dentry);
  16371. + }
  16372. +
  16373. + AuTraceErr(err);
  16374. + return err;
  16375. +}
  16376. +
  16377. +/* ---------------------------------------------------------------------- */
  16378. +
  16379. +/* hnotify job flags */
  16380. +#define AuHnJob_XINO0 1
  16381. +#define AuHnJob_GEN (1 << 1)
  16382. +#define AuHnJob_DIRENT (1 << 2)
  16383. +#define AuHnJob_ISDIR (1 << 3)
  16384. +#define AuHnJob_TRYXINO0 (1 << 4)
  16385. +#define AuHnJob_MNTPNT (1 << 5)
  16386. +#define au_ftest_hnjob(flags, name) ((flags) & AuHnJob_##name)
  16387. +#define au_fset_hnjob(flags, name) \
  16388. + do { (flags) |= AuHnJob_##name; } while (0)
  16389. +#define au_fclr_hnjob(flags, name) \
  16390. + do { (flags) &= ~AuHnJob_##name; } while (0)
  16391. +
  16392. +enum {
  16393. + AuHn_CHILD,
  16394. + AuHn_PARENT,
  16395. + AuHnLast
  16396. +};
  16397. +
  16398. +struct au_hnotify_args {
  16399. + struct inode *h_dir, *dir, *h_child_inode;
  16400. + u32 mask;
  16401. + unsigned int flags[AuHnLast];
  16402. + unsigned int h_child_nlen;
  16403. + char h_child_name[];
  16404. +};
  16405. +
  16406. +struct hn_job_args {
  16407. + unsigned int flags;
  16408. + struct inode *inode, *h_inode, *dir, *h_dir;
  16409. + struct dentry *dentry;
  16410. + char *h_name;
  16411. + int h_nlen;
  16412. +};
  16413. +
  16414. +static int hn_job(struct hn_job_args *a)
  16415. +{
  16416. + const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
  16417. + int e;
  16418. +
  16419. + /* reset xino */
  16420. + if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
  16421. + hn_xino(a->inode, a->h_inode); /* ignore this error */
  16422. +
  16423. + if (au_ftest_hnjob(a->flags, TRYXINO0)
  16424. + && a->inode
  16425. + && a->h_inode) {
  16426. + inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
  16427. + if (!a->h_inode->i_nlink
  16428. + && !(a->h_inode->i_state & I_LINKABLE))
  16429. + hn_xino(a->inode, a->h_inode); /* ignore this error */
  16430. + inode_unlock_shared(a->h_inode);
  16431. + }
  16432. +
  16433. + /* make the generation obsolete */
  16434. + if (au_ftest_hnjob(a->flags, GEN)) {
  16435. + e = -1;
  16436. + if (a->inode)
  16437. + e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
  16438. + isdir);
  16439. + if (e && a->dentry)
  16440. + hn_gen_by_name(a->dentry, isdir);
  16441. + /* ignore this error */
  16442. + }
  16443. +
  16444. + /* make dir entries obsolete */
  16445. + if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
  16446. + struct au_vdir *vdir;
  16447. +
  16448. + vdir = au_ivdir(a->inode);
  16449. + if (vdir)
  16450. + vdir->vd_jiffy = 0;
  16451. + /* IMustLock(a->inode); */
  16452. + /* inode_inc_iversion(a->inode); */
  16453. + }
  16454. +
  16455. + /* can do nothing but warn */
  16456. + if (au_ftest_hnjob(a->flags, MNTPNT)
  16457. + && a->dentry
  16458. + && d_mountpoint(a->dentry))
  16459. + pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
  16460. +
  16461. + return 0;
  16462. +}
  16463. +
  16464. +/* ---------------------------------------------------------------------- */
  16465. +
  16466. +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
  16467. + struct inode *dir)
  16468. +{
  16469. + struct dentry *dentry, *d, *parent;
  16470. + struct qstr *dname;
  16471. +
  16472. + parent = d_find_any_alias(dir);
  16473. + if (!parent)
  16474. + return NULL;
  16475. +
  16476. + dentry = NULL;
  16477. + spin_lock(&parent->d_lock);
  16478. + list_for_each_entry(d, &parent->d_subdirs, d_child) {
  16479. + /* AuDbg("%pd\n", d); */
  16480. + spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
  16481. + dname = &d->d_name;
  16482. + if (dname->len != nlen || memcmp(dname->name, name, nlen))
  16483. + goto cont_unlock;
  16484. + if (au_di(d))
  16485. + au_digen_dec(d);
  16486. + else
  16487. + goto cont_unlock;
  16488. + if (au_dcount(d) > 0) {
  16489. + dentry = dget_dlock(d);
  16490. + spin_unlock(&d->d_lock);
  16491. + break;
  16492. + }
  16493. +
  16494. +cont_unlock:
  16495. + spin_unlock(&d->d_lock);
  16496. + }
  16497. + spin_unlock(&parent->d_lock);
  16498. + dput(parent);
  16499. +
  16500. + if (dentry)
  16501. + di_write_lock_child(dentry);
  16502. +
  16503. + return dentry;
  16504. +}
  16505. +
  16506. +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
  16507. + aufs_bindex_t bindex, ino_t h_ino)
  16508. +{
  16509. + struct inode *inode;
  16510. + ino_t ino;
  16511. + int err;
  16512. +
  16513. + inode = NULL;
  16514. + err = au_xino_read(sb, bindex, h_ino, &ino);
  16515. + if (!err && ino)
  16516. + inode = ilookup(sb, ino);
  16517. + if (!inode)
  16518. + goto out;
  16519. +
  16520. + if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
  16521. + pr_warn("wrong root branch\n");
  16522. + iput(inode);
  16523. + inode = NULL;
  16524. + goto out;
  16525. + }
  16526. +
  16527. + ii_write_lock_child(inode);
  16528. +
  16529. +out:
  16530. + return inode;
  16531. +}
  16532. +
  16533. +static void au_hn_bh(void *_args)
  16534. +{
  16535. + struct au_hnotify_args *a = _args;
  16536. + struct super_block *sb;
  16537. + aufs_bindex_t bindex, bbot, bfound;
  16538. + unsigned char xino, try_iput;
  16539. + int err;
  16540. + struct inode *inode;
  16541. + ino_t h_ino;
  16542. + struct hn_job_args args;
  16543. + struct dentry *dentry;
  16544. + struct au_sbinfo *sbinfo;
  16545. +
  16546. + AuDebugOn(!_args);
  16547. + AuDebugOn(!a->h_dir);
  16548. + AuDebugOn(!a->dir);
  16549. + AuDebugOn(!a->mask);
  16550. + AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
  16551. + a->mask, a->dir->i_ino, a->h_dir->i_ino,
  16552. + a->h_child_inode ? a->h_child_inode->i_ino : 0);
  16553. +
  16554. + inode = NULL;
  16555. + dentry = NULL;
  16556. + /*
  16557. + * do not lock a->dir->i_mutex here
  16558. + * because of d_revalidate() may cause a deadlock.
  16559. + */
  16560. + sb = a->dir->i_sb;
  16561. + AuDebugOn(!sb);
  16562. + sbinfo = au_sbi(sb);
  16563. + AuDebugOn(!sbinfo);
  16564. + si_write_lock(sb, AuLock_NOPLMW);
  16565. +
  16566. + if (au_opt_test(sbinfo->si_mntflags, DIRREN))
  16567. + switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
  16568. + case FS_MOVED_FROM:
  16569. + case FS_MOVED_TO:
  16570. + AuWarn1("DIRREN with UDBA may not work correctly "
  16571. + "for the direct rename(2)\n");
  16572. + }
  16573. +
  16574. + ii_read_lock_parent(a->dir);
  16575. + bfound = -1;
  16576. + bbot = au_ibbot(a->dir);
  16577. + for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
  16578. + if (au_h_iptr(a->dir, bindex) == a->h_dir) {
  16579. + bfound = bindex;
  16580. + break;
  16581. + }
  16582. + ii_read_unlock(a->dir);
  16583. + if (unlikely(bfound < 0))
  16584. + goto out;
  16585. +
  16586. + xino = !!au_opt_test(au_mntflags(sb), XINO);
  16587. + h_ino = 0;
  16588. + if (a->h_child_inode)
  16589. + h_ino = a->h_child_inode->i_ino;
  16590. +
  16591. + if (a->h_child_nlen
  16592. + && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
  16593. + || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
  16594. + dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
  16595. + a->dir);
  16596. + try_iput = 0;
  16597. + if (dentry && d_really_is_positive(dentry))
  16598. + inode = d_inode(dentry);
  16599. + if (xino && !inode && h_ino
  16600. + && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
  16601. + || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
  16602. + || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
  16603. + inode = lookup_wlock_by_ino(sb, bfound, h_ino);
  16604. + try_iput = 1;
  16605. + }
  16606. +
  16607. + args.flags = a->flags[AuHn_CHILD];
  16608. + args.dentry = dentry;
  16609. + args.inode = inode;
  16610. + args.h_inode = a->h_child_inode;
  16611. + args.dir = a->dir;
  16612. + args.h_dir = a->h_dir;
  16613. + args.h_name = a->h_child_name;
  16614. + args.h_nlen = a->h_child_nlen;
  16615. + err = hn_job(&args);
  16616. + if (dentry) {
  16617. + if (au_di(dentry))
  16618. + di_write_unlock(dentry);
  16619. + dput(dentry);
  16620. + }
  16621. + if (inode && try_iput) {
  16622. + ii_write_unlock(inode);
  16623. + iput(inode);
  16624. + }
  16625. +
  16626. + ii_write_lock_parent(a->dir);
  16627. + args.flags = a->flags[AuHn_PARENT];
  16628. + args.dentry = NULL;
  16629. + args.inode = a->dir;
  16630. + args.h_inode = a->h_dir;
  16631. + args.dir = NULL;
  16632. + args.h_dir = NULL;
  16633. + args.h_name = NULL;
  16634. + args.h_nlen = 0;
  16635. + err = hn_job(&args);
  16636. + ii_write_unlock(a->dir);
  16637. +
  16638. +out:
  16639. + iput(a->h_child_inode);
  16640. + iput(a->h_dir);
  16641. + iput(a->dir);
  16642. + si_write_unlock(sb);
  16643. + au_nwt_done(&sbinfo->si_nowait);
  16644. + au_kfree_rcu(a);
  16645. +}
  16646. +
  16647. +/* ---------------------------------------------------------------------- */
  16648. +
  16649. +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
  16650. + struct qstr *h_child_qstr, struct inode *h_child_inode)
  16651. +{
  16652. + int err, len;
  16653. + unsigned int flags[AuHnLast], f;
  16654. + unsigned char isdir, isroot, wh;
  16655. + struct inode *dir;
  16656. + struct au_hnotify_args *args;
  16657. + char *p, *h_child_name;
  16658. +
  16659. + err = 0;
  16660. + AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
  16661. + dir = igrab(hnotify->hn_aufs_inode);
  16662. + if (!dir)
  16663. + goto out;
  16664. +
  16665. + isroot = (dir->i_ino == AUFS_ROOT_INO);
  16666. + wh = 0;
  16667. + h_child_name = (void *)h_child_qstr->name;
  16668. + len = h_child_qstr->len;
  16669. + if (h_child_name) {
  16670. + if (len > AUFS_WH_PFX_LEN
  16671. + && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
  16672. + h_child_name += AUFS_WH_PFX_LEN;
  16673. + len -= AUFS_WH_PFX_LEN;
  16674. + wh = 1;
  16675. + }
  16676. + }
  16677. +
  16678. + isdir = 0;
  16679. + if (h_child_inode)
  16680. + isdir = !!S_ISDIR(h_child_inode->i_mode);
  16681. + flags[AuHn_PARENT] = AuHnJob_ISDIR;
  16682. + flags[AuHn_CHILD] = 0;
  16683. + if (isdir)
  16684. + flags[AuHn_CHILD] = AuHnJob_ISDIR;
  16685. + au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
  16686. + au_fset_hnjob(flags[AuHn_CHILD], GEN);
  16687. + switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
  16688. + case FS_MOVED_FROM:
  16689. + case FS_MOVED_TO:
  16690. + au_fset_hnjob(flags[AuHn_CHILD], XINO0);
  16691. + au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
  16692. + /*FALLTHROUGH*/
  16693. + case FS_CREATE:
  16694. + AuDebugOn(!h_child_name);
  16695. + break;
  16696. +
  16697. + case FS_DELETE:
  16698. + /*
  16699. + * aufs never be able to get this child inode.
  16700. + * revalidation should be in d_revalidate()
  16701. + * by checking i_nlink, i_generation or d_unhashed().
  16702. + */
  16703. + AuDebugOn(!h_child_name);
  16704. + au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
  16705. + au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
  16706. + break;
  16707. +
  16708. + default:
  16709. + AuDebugOn(1);
  16710. + }
  16711. +
  16712. + if (wh)
  16713. + h_child_inode = NULL;
  16714. +
  16715. + err = -ENOMEM;
  16716. + /* iput() and kfree() will be called in au_hnotify() */
  16717. + args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
  16718. + if (unlikely(!args)) {
  16719. + AuErr1("no memory\n");
  16720. + iput(dir);
  16721. + goto out;
  16722. + }
  16723. + args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
  16724. + args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
  16725. + args->mask = mask;
  16726. + args->dir = dir;
  16727. + args->h_dir = igrab(h_dir);
  16728. + if (h_child_inode)
  16729. + h_child_inode = igrab(h_child_inode); /* can be NULL */
  16730. + args->h_child_inode = h_child_inode;
  16731. + args->h_child_nlen = len;
  16732. + if (len) {
  16733. + p = (void *)args;
  16734. + p += sizeof(*args);
  16735. + memcpy(p, h_child_name, len);
  16736. + p[len] = 0;
  16737. + }
  16738. +
  16739. + /* NFS fires the event for silly-renamed one from kworker */
  16740. + f = 0;
  16741. + if (!dir->i_nlink
  16742. + || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
  16743. + f = AuWkq_NEST;
  16744. + err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
  16745. + if (unlikely(err)) {
  16746. + pr_err("wkq %d\n", err);
  16747. + iput(args->h_child_inode);
  16748. + iput(args->h_dir);
  16749. + iput(args->dir);
  16750. + au_kfree_rcu(args);
  16751. + }
  16752. +
  16753. +out:
  16754. + return err;
  16755. +}
  16756. +
  16757. +/* ---------------------------------------------------------------------- */
  16758. +
  16759. +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
  16760. +{
  16761. + int err;
  16762. +
  16763. + AuDebugOn(!(udba & AuOptMask_UDBA));
  16764. +
  16765. + err = 0;
  16766. + if (au_hnotify_op.reset_br)
  16767. + err = au_hnotify_op.reset_br(udba, br, perm);
  16768. +
  16769. + return err;
  16770. +}
  16771. +
  16772. +int au_hnotify_init_br(struct au_branch *br, int perm)
  16773. +{
  16774. + int err;
  16775. +
  16776. + err = 0;
  16777. + if (au_hnotify_op.init_br)
  16778. + err = au_hnotify_op.init_br(br, perm);
  16779. +
  16780. + return err;
  16781. +}
  16782. +
  16783. +void au_hnotify_fin_br(struct au_branch *br)
  16784. +{
  16785. + if (au_hnotify_op.fin_br)
  16786. + au_hnotify_op.fin_br(br);
  16787. +}
  16788. +
  16789. +static void au_hn_destroy_cache(void)
  16790. +{
  16791. + kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
  16792. + au_cache[AuCache_HNOTIFY] = NULL;
  16793. +}
  16794. +
  16795. +int __init au_hnotify_init(void)
  16796. +{
  16797. + int err;
  16798. +
  16799. + err = -ENOMEM;
  16800. + au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
  16801. + if (au_cache[AuCache_HNOTIFY]) {
  16802. + err = 0;
  16803. + if (au_hnotify_op.init)
  16804. + err = au_hnotify_op.init();
  16805. + if (unlikely(err))
  16806. + au_hn_destroy_cache();
  16807. + }
  16808. + AuTraceErr(err);
  16809. + return err;
  16810. +}
  16811. +
  16812. +void au_hnotify_fin(void)
  16813. +{
  16814. + if (au_hnotify_op.fin)
  16815. + au_hnotify_op.fin();
  16816. +
  16817. + /* cf. au_cache_fin() */
  16818. + if (au_cache[AuCache_HNOTIFY])
  16819. + au_hn_destroy_cache();
  16820. +}
  16821. diff -Naur null/fs/aufs/iinfo.c linux-5.1/fs/aufs/iinfo.c
  16822. --- /dev/null
  16823. +++ linux-5.1/fs/aufs/iinfo.c 2019-05-22 11:23:07.441140996 +0200
  16824. @@ -0,0 +1,286 @@
  16825. +// SPDX-License-Identifier: GPL-2.0
  16826. +/*
  16827. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  16828. + *
  16829. + * This program, aufs is free software; you can redistribute it and/or modify
  16830. + * it under the terms of the GNU General Public License as published by
  16831. + * the Free Software Foundation; either version 2 of the License, or
  16832. + * (at your option) any later version.
  16833. + *
  16834. + * This program is distributed in the hope that it will be useful,
  16835. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16836. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16837. + * GNU General Public License for more details.
  16838. + *
  16839. + * You should have received a copy of the GNU General Public License
  16840. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16841. + */
  16842. +
  16843. +/*
  16844. + * inode private data
  16845. + */
  16846. +
  16847. +#include "aufs.h"
  16848. +
  16849. +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
  16850. +{
  16851. + struct inode *h_inode;
  16852. + struct au_hinode *hinode;
  16853. +
  16854. + IiMustAnyLock(inode);
  16855. +
  16856. + hinode = au_hinode(au_ii(inode), bindex);
  16857. + h_inode = hinode->hi_inode;
  16858. + AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
  16859. + return h_inode;
  16860. +}
  16861. +
  16862. +/* todo: hard/soft set? */
  16863. +void au_hiput(struct au_hinode *hinode)
  16864. +{
  16865. + au_hn_free(hinode);
  16866. + dput(hinode->hi_whdentry);
  16867. + iput(hinode->hi_inode);
  16868. +}
  16869. +
  16870. +unsigned int au_hi_flags(struct inode *inode, int isdir)
  16871. +{
  16872. + unsigned int flags;
  16873. + const unsigned int mnt_flags = au_mntflags(inode->i_sb);
  16874. +
  16875. + flags = 0;
  16876. + if (au_opt_test(mnt_flags, XINO))
  16877. + au_fset_hi(flags, XINO);
  16878. + if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
  16879. + au_fset_hi(flags, HNOTIFY);
  16880. + return flags;
  16881. +}
  16882. +
  16883. +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
  16884. + struct inode *h_inode, unsigned int flags)
  16885. +{
  16886. + struct au_hinode *hinode;
  16887. + struct inode *hi;
  16888. + struct au_iinfo *iinfo = au_ii(inode);
  16889. +
  16890. + IiMustWriteLock(inode);
  16891. +
  16892. + hinode = au_hinode(iinfo, bindex);
  16893. + hi = hinode->hi_inode;
  16894. + AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
  16895. +
  16896. + if (hi)
  16897. + au_hiput(hinode);
  16898. + hinode->hi_inode = h_inode;
  16899. + if (h_inode) {
  16900. + int err;
  16901. + struct super_block *sb = inode->i_sb;
  16902. + struct au_branch *br;
  16903. +
  16904. + AuDebugOn(inode->i_mode
  16905. + && (h_inode->i_mode & S_IFMT)
  16906. + != (inode->i_mode & S_IFMT));
  16907. + if (bindex == iinfo->ii_btop)
  16908. + au_cpup_igen(inode, h_inode);
  16909. + br = au_sbr(sb, bindex);
  16910. + hinode->hi_id = br->br_id;
  16911. + if (au_ftest_hi(flags, XINO)) {
  16912. + err = au_xino_write(sb, bindex, h_inode->i_ino,
  16913. + inode->i_ino);
  16914. + if (unlikely(err))
  16915. + AuIOErr1("failed au_xino_write() %d\n", err);
  16916. + }
  16917. +
  16918. + if (au_ftest_hi(flags, HNOTIFY)
  16919. + && au_br_hnotifyable(br->br_perm)) {
  16920. + err = au_hn_alloc(hinode, inode);
  16921. + if (unlikely(err))
  16922. + AuIOErr1("au_hn_alloc() %d\n", err);
  16923. + }
  16924. + }
  16925. +}
  16926. +
  16927. +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
  16928. + struct dentry *h_wh)
  16929. +{
  16930. + struct au_hinode *hinode;
  16931. +
  16932. + IiMustWriteLock(inode);
  16933. +
  16934. + hinode = au_hinode(au_ii(inode), bindex);
  16935. + AuDebugOn(hinode->hi_whdentry);
  16936. + hinode->hi_whdentry = h_wh;
  16937. +}
  16938. +
  16939. +void au_update_iigen(struct inode *inode, int half)
  16940. +{
  16941. + struct au_iinfo *iinfo;
  16942. + struct au_iigen *iigen;
  16943. + unsigned int sigen;
  16944. +
  16945. + sigen = au_sigen(inode->i_sb);
  16946. + iinfo = au_ii(inode);
  16947. + iigen = &iinfo->ii_generation;
  16948. + spin_lock(&iigen->ig_spin);
  16949. + iigen->ig_generation = sigen;
  16950. + if (half)
  16951. + au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
  16952. + else
  16953. + au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
  16954. + spin_unlock(&iigen->ig_spin);
  16955. +}
  16956. +
  16957. +/* it may be called at remount time, too */
  16958. +void au_update_ibrange(struct inode *inode, int do_put_zero)
  16959. +{
  16960. + struct au_iinfo *iinfo;
  16961. + aufs_bindex_t bindex, bbot;
  16962. +
  16963. + AuDebugOn(au_is_bad_inode(inode));
  16964. + IiMustWriteLock(inode);
  16965. +
  16966. + iinfo = au_ii(inode);
  16967. + if (do_put_zero && iinfo->ii_btop >= 0) {
  16968. + for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
  16969. + bindex++) {
  16970. + struct inode *h_i;
  16971. +
  16972. + h_i = au_hinode(iinfo, bindex)->hi_inode;
  16973. + if (h_i
  16974. + && !h_i->i_nlink
  16975. + && !(h_i->i_state & I_LINKABLE))
  16976. + au_set_h_iptr(inode, bindex, NULL, 0);
  16977. + }
  16978. + }
  16979. +
  16980. + iinfo->ii_btop = -1;
  16981. + iinfo->ii_bbot = -1;
  16982. + bbot = au_sbbot(inode->i_sb);
  16983. + for (bindex = 0; bindex <= bbot; bindex++)
  16984. + if (au_hinode(iinfo, bindex)->hi_inode) {
  16985. + iinfo->ii_btop = bindex;
  16986. + break;
  16987. + }
  16988. + if (iinfo->ii_btop >= 0)
  16989. + for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
  16990. + if (au_hinode(iinfo, bindex)->hi_inode) {
  16991. + iinfo->ii_bbot = bindex;
  16992. + break;
  16993. + }
  16994. + AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
  16995. +}
  16996. +
  16997. +/* ---------------------------------------------------------------------- */
  16998. +
  16999. +void au_icntnr_init_once(void *_c)
  17000. +{
  17001. + struct au_icntnr *c = _c;
  17002. + struct au_iinfo *iinfo = &c->iinfo;
  17003. +
  17004. + spin_lock_init(&iinfo->ii_generation.ig_spin);
  17005. + au_rw_init(&iinfo->ii_rwsem);
  17006. + inode_init_once(&c->vfs_inode);
  17007. +}
  17008. +
  17009. +void au_hinode_init(struct au_hinode *hinode)
  17010. +{
  17011. + hinode->hi_inode = NULL;
  17012. + hinode->hi_id = -1;
  17013. + au_hn_init(hinode);
  17014. + hinode->hi_whdentry = NULL;
  17015. +}
  17016. +
  17017. +int au_iinfo_init(struct inode *inode)
  17018. +{
  17019. + struct au_iinfo *iinfo;
  17020. + struct super_block *sb;
  17021. + struct au_hinode *hi;
  17022. + int nbr, i;
  17023. +
  17024. + sb = inode->i_sb;
  17025. + iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
  17026. + nbr = au_sbbot(sb) + 1;
  17027. + if (unlikely(nbr <= 0))
  17028. + nbr = 1;
  17029. + hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
  17030. + if (hi) {
  17031. + au_lcnt_inc(&au_sbi(sb)->si_ninodes);
  17032. +
  17033. + iinfo->ii_hinode = hi;
  17034. + for (i = 0; i < nbr; i++, hi++)
  17035. + au_hinode_init(hi);
  17036. +
  17037. + iinfo->ii_generation.ig_generation = au_sigen(sb);
  17038. + iinfo->ii_btop = -1;
  17039. + iinfo->ii_bbot = -1;
  17040. + iinfo->ii_vdir = NULL;
  17041. + return 0;
  17042. + }
  17043. + return -ENOMEM;
  17044. +}
  17045. +
  17046. +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
  17047. +{
  17048. + int err, i;
  17049. + struct au_hinode *hip;
  17050. +
  17051. + AuRwMustWriteLock(&iinfo->ii_rwsem);
  17052. +
  17053. + err = -ENOMEM;
  17054. + hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
  17055. + may_shrink);
  17056. + if (hip) {
  17057. + iinfo->ii_hinode = hip;
  17058. + i = iinfo->ii_bbot + 1;
  17059. + hip += i;
  17060. + for (; i < nbr; i++, hip++)
  17061. + au_hinode_init(hip);
  17062. + err = 0;
  17063. + }
  17064. +
  17065. + return err;
  17066. +}
  17067. +
  17068. +void au_iinfo_fin(struct inode *inode)
  17069. +{
  17070. + struct au_iinfo *iinfo;
  17071. + struct au_hinode *hi;
  17072. + struct super_block *sb;
  17073. + aufs_bindex_t bindex, bbot;
  17074. + const unsigned char unlinked = !inode->i_nlink;
  17075. +
  17076. + AuDebugOn(au_is_bad_inode(inode));
  17077. +
  17078. + sb = inode->i_sb;
  17079. + au_lcnt_dec(&au_sbi(sb)->si_ninodes);
  17080. + if (si_pid_test(sb))
  17081. + au_xino_delete_inode(inode, unlinked);
  17082. + else {
  17083. + /*
  17084. + * it is safe to hide the dependency between sbinfo and
  17085. + * sb->s_umount.
  17086. + */
  17087. + lockdep_off();
  17088. + si_noflush_read_lock(sb);
  17089. + au_xino_delete_inode(inode, unlinked);
  17090. + si_read_unlock(sb);
  17091. + lockdep_on();
  17092. + }
  17093. +
  17094. + iinfo = au_ii(inode);
  17095. + if (iinfo->ii_vdir)
  17096. + au_vdir_free(iinfo->ii_vdir);
  17097. +
  17098. + bindex = iinfo->ii_btop;
  17099. + if (bindex >= 0) {
  17100. + hi = au_hinode(iinfo, bindex);
  17101. + bbot = iinfo->ii_bbot;
  17102. + while (bindex++ <= bbot) {
  17103. + if (hi->hi_inode)
  17104. + au_hiput(hi);
  17105. + hi++;
  17106. + }
  17107. + }
  17108. + au_kfree_rcu(iinfo->ii_hinode);
  17109. + AuRwDestroy(&iinfo->ii_rwsem);
  17110. +}
  17111. diff -Naur null/fs/aufs/inode.c linux-5.1/fs/aufs/inode.c
  17112. --- /dev/null
  17113. +++ linux-5.1/fs/aufs/inode.c 2019-05-22 11:23:07.441140996 +0200
  17114. @@ -0,0 +1,529 @@
  17115. +// SPDX-License-Identifier: GPL-2.0
  17116. +/*
  17117. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  17118. + *
  17119. + * This program, aufs is free software; you can redistribute it and/or modify
  17120. + * it under the terms of the GNU General Public License as published by
  17121. + * the Free Software Foundation; either version 2 of the License, or
  17122. + * (at your option) any later version.
  17123. + *
  17124. + * This program is distributed in the hope that it will be useful,
  17125. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17126. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17127. + * GNU General Public License for more details.
  17128. + *
  17129. + * You should have received a copy of the GNU General Public License
  17130. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17131. + */
  17132. +
  17133. +/*
  17134. + * inode functions
  17135. + */
  17136. +
  17137. +#include <linux/iversion.h>
  17138. +#include "aufs.h"
  17139. +
  17140. +struct inode *au_igrab(struct inode *inode)
  17141. +{
  17142. + if (inode) {
  17143. + AuDebugOn(!atomic_read(&inode->i_count));
  17144. + ihold(inode);
  17145. + }
  17146. + return inode;
  17147. +}
  17148. +
  17149. +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
  17150. +{
  17151. + au_cpup_attr_all(inode, /*force*/0);
  17152. + au_update_iigen(inode, /*half*/1);
  17153. + if (do_version)
  17154. + inode_inc_iversion(inode);
  17155. +}
  17156. +
  17157. +static int au_ii_refresh(struct inode *inode, int *update)
  17158. +{
  17159. + int err, e, nbr;
  17160. + umode_t type;
  17161. + aufs_bindex_t bindex, new_bindex;
  17162. + struct super_block *sb;
  17163. + struct au_iinfo *iinfo;
  17164. + struct au_hinode *p, *q, tmp;
  17165. +
  17166. + AuDebugOn(au_is_bad_inode(inode));
  17167. + IiMustWriteLock(inode);
  17168. +
  17169. + *update = 0;
  17170. + sb = inode->i_sb;
  17171. + nbr = au_sbbot(sb) + 1;
  17172. + type = inode->i_mode & S_IFMT;
  17173. + iinfo = au_ii(inode);
  17174. + err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
  17175. + if (unlikely(err))
  17176. + goto out;
  17177. +
  17178. + AuDebugOn(iinfo->ii_btop < 0);
  17179. + p = au_hinode(iinfo, iinfo->ii_btop);
  17180. + for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
  17181. + bindex++, p++) {
  17182. + if (!p->hi_inode)
  17183. + continue;
  17184. +
  17185. + AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
  17186. + new_bindex = au_br_index(sb, p->hi_id);
  17187. + if (new_bindex == bindex)
  17188. + continue;
  17189. +
  17190. + if (new_bindex < 0) {
  17191. + *update = 1;
  17192. + au_hiput(p);
  17193. + p->hi_inode = NULL;
  17194. + continue;
  17195. + }
  17196. +
  17197. + if (new_bindex < iinfo->ii_btop)
  17198. + iinfo->ii_btop = new_bindex;
  17199. + if (iinfo->ii_bbot < new_bindex)
  17200. + iinfo->ii_bbot = new_bindex;
  17201. + /* swap two lower inode, and loop again */
  17202. + q = au_hinode(iinfo, new_bindex);
  17203. + tmp = *q;
  17204. + *q = *p;
  17205. + *p = tmp;
  17206. + if (tmp.hi_inode) {
  17207. + bindex--;
  17208. + p--;
  17209. + }
  17210. + }
  17211. + au_update_ibrange(inode, /*do_put_zero*/0);
  17212. + au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
  17213. + e = au_dy_irefresh(inode);
  17214. + if (unlikely(e && !err))
  17215. + err = e;
  17216. +
  17217. +out:
  17218. + AuTraceErr(err);
  17219. + return err;
  17220. +}
  17221. +
  17222. +void au_refresh_iop(struct inode *inode, int force_getattr)
  17223. +{
  17224. + int type;
  17225. + struct au_sbinfo *sbi = au_sbi(inode->i_sb);
  17226. + const struct inode_operations *iop
  17227. + = force_getattr ? aufs_iop : sbi->si_iop_array;
  17228. +
  17229. + if (inode->i_op == iop)
  17230. + return;
  17231. +
  17232. + switch (inode->i_mode & S_IFMT) {
  17233. + case S_IFDIR:
  17234. + type = AuIop_DIR;
  17235. + break;
  17236. + case S_IFLNK:
  17237. + type = AuIop_SYMLINK;
  17238. + break;
  17239. + default:
  17240. + type = AuIop_OTHER;
  17241. + break;
  17242. + }
  17243. +
  17244. + inode->i_op = iop + type;
  17245. + /* unnecessary smp_wmb() */
  17246. +}
  17247. +
  17248. +int au_refresh_hinode_self(struct inode *inode)
  17249. +{
  17250. + int err, update;
  17251. +
  17252. + err = au_ii_refresh(inode, &update);
  17253. + if (!err)
  17254. + au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
  17255. +
  17256. + AuTraceErr(err);
  17257. + return err;
  17258. +}
  17259. +
  17260. +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
  17261. +{
  17262. + int err, e, update;
  17263. + unsigned int flags;
  17264. + umode_t mode;
  17265. + aufs_bindex_t bindex, bbot;
  17266. + unsigned char isdir;
  17267. + struct au_hinode *p;
  17268. + struct au_iinfo *iinfo;
  17269. +
  17270. + err = au_ii_refresh(inode, &update);
  17271. + if (unlikely(err))
  17272. + goto out;
  17273. +
  17274. + update = 0;
  17275. + iinfo = au_ii(inode);
  17276. + p = au_hinode(iinfo, iinfo->ii_btop);
  17277. + mode = (inode->i_mode & S_IFMT);
  17278. + isdir = S_ISDIR(mode);
  17279. + flags = au_hi_flags(inode, isdir);
  17280. + bbot = au_dbbot(dentry);
  17281. + for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
  17282. + struct inode *h_i, *h_inode;
  17283. + struct dentry *h_d;
  17284. +
  17285. + h_d = au_h_dptr(dentry, bindex);
  17286. + if (!h_d || d_is_negative(h_d))
  17287. + continue;
  17288. +
  17289. + h_inode = d_inode(h_d);
  17290. + AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
  17291. + if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
  17292. + h_i = au_h_iptr(inode, bindex);
  17293. + if (h_i) {
  17294. + if (h_i == h_inode)
  17295. + continue;
  17296. + err = -EIO;
  17297. + break;
  17298. + }
  17299. + }
  17300. + if (bindex < iinfo->ii_btop)
  17301. + iinfo->ii_btop = bindex;
  17302. + if (iinfo->ii_bbot < bindex)
  17303. + iinfo->ii_bbot = bindex;
  17304. + au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
  17305. + update = 1;
  17306. + }
  17307. + au_update_ibrange(inode, /*do_put_zero*/0);
  17308. + e = au_dy_irefresh(inode);
  17309. + if (unlikely(e && !err))
  17310. + err = e;
  17311. + if (!err)
  17312. + au_refresh_hinode_attr(inode, update && isdir);
  17313. +
  17314. +out:
  17315. + AuTraceErr(err);
  17316. + return err;
  17317. +}
  17318. +
  17319. +static int set_inode(struct inode *inode, struct dentry *dentry)
  17320. +{
  17321. + int err;
  17322. + unsigned int flags;
  17323. + umode_t mode;
  17324. + aufs_bindex_t bindex, btop, btail;
  17325. + unsigned char isdir;
  17326. + struct dentry *h_dentry;
  17327. + struct inode *h_inode;
  17328. + struct au_iinfo *iinfo;
  17329. + struct inode_operations *iop;
  17330. +
  17331. + IiMustWriteLock(inode);
  17332. +
  17333. + err = 0;
  17334. + isdir = 0;
  17335. + iop = au_sbi(inode->i_sb)->si_iop_array;
  17336. + btop = au_dbtop(dentry);
  17337. + h_dentry = au_h_dptr(dentry, btop);
  17338. + h_inode = d_inode(h_dentry);
  17339. + mode = h_inode->i_mode;
  17340. + switch (mode & S_IFMT) {
  17341. + case S_IFREG:
  17342. + btail = au_dbtail(dentry);
  17343. + inode->i_op = iop + AuIop_OTHER;
  17344. + inode->i_fop = &aufs_file_fop;
  17345. + err = au_dy_iaop(inode, btop, h_inode);
  17346. + if (unlikely(err))
  17347. + goto out;
  17348. + break;
  17349. + case S_IFDIR:
  17350. + isdir = 1;
  17351. + btail = au_dbtaildir(dentry);
  17352. + inode->i_op = iop + AuIop_DIR;
  17353. + inode->i_fop = &aufs_dir_fop;
  17354. + break;
  17355. + case S_IFLNK:
  17356. + btail = au_dbtail(dentry);
  17357. + inode->i_op = iop + AuIop_SYMLINK;
  17358. + break;
  17359. + case S_IFBLK:
  17360. + case S_IFCHR:
  17361. + case S_IFIFO:
  17362. + case S_IFSOCK:
  17363. + btail = au_dbtail(dentry);
  17364. + inode->i_op = iop + AuIop_OTHER;
  17365. + init_special_inode(inode, mode, h_inode->i_rdev);
  17366. + break;
  17367. + default:
  17368. + AuIOErr("Unknown file type 0%o\n", mode);
  17369. + err = -EIO;
  17370. + goto out;
  17371. + }
  17372. +
  17373. + /* do not set hnotify for whiteouted dirs (SHWH mode) */
  17374. + flags = au_hi_flags(inode, isdir);
  17375. + if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
  17376. + && au_ftest_hi(flags, HNOTIFY)
  17377. + && dentry->d_name.len > AUFS_WH_PFX_LEN
  17378. + && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
  17379. + au_fclr_hi(flags, HNOTIFY);
  17380. + iinfo = au_ii(inode);
  17381. + iinfo->ii_btop = btop;
  17382. + iinfo->ii_bbot = btail;
  17383. + for (bindex = btop; bindex <= btail; bindex++) {
  17384. + h_dentry = au_h_dptr(dentry, bindex);
  17385. + if (h_dentry)
  17386. + au_set_h_iptr(inode, bindex,
  17387. + au_igrab(d_inode(h_dentry)), flags);
  17388. + }
  17389. + au_cpup_attr_all(inode, /*force*/1);
  17390. + /*
  17391. + * to force calling aufs_get_acl() every time,
  17392. + * do not call cache_no_acl() for aufs inode.
  17393. + */
  17394. +
  17395. +out:
  17396. + return err;
  17397. +}
  17398. +
  17399. +/*
  17400. + * successful returns with iinfo write_locked
  17401. + * minus: errno
  17402. + * zero: success, matched
  17403. + * plus: no error, but unmatched
  17404. + */
  17405. +static int reval_inode(struct inode *inode, struct dentry *dentry)
  17406. +{
  17407. + int err;
  17408. + unsigned int gen, igflags;
  17409. + aufs_bindex_t bindex, bbot;
  17410. + struct inode *h_inode, *h_dinode;
  17411. + struct dentry *h_dentry;
  17412. +
  17413. + /*
  17414. + * before this function, if aufs got any iinfo lock, it must be only
  17415. + * one, the parent dir.
  17416. + * it can happen by UDBA and the obsoleted inode number.
  17417. + */
  17418. + err = -EIO;
  17419. + if (unlikely(inode->i_ino == parent_ino(dentry)))
  17420. + goto out;
  17421. +
  17422. + err = 1;
  17423. + ii_write_lock_new_child(inode);
  17424. + h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
  17425. + h_dinode = d_inode(h_dentry);
  17426. + bbot = au_ibbot(inode);
  17427. + for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
  17428. + h_inode = au_h_iptr(inode, bindex);
  17429. + if (!h_inode || h_inode != h_dinode)
  17430. + continue;
  17431. +
  17432. + err = 0;
  17433. + gen = au_iigen(inode, &igflags);
  17434. + if (gen == au_digen(dentry)
  17435. + && !au_ig_ftest(igflags, HALF_REFRESHED))
  17436. + break;
  17437. +
  17438. + /* fully refresh inode using dentry */
  17439. + err = au_refresh_hinode(inode, dentry);
  17440. + if (!err)
  17441. + au_update_iigen(inode, /*half*/0);
  17442. + break;
  17443. + }
  17444. +
  17445. + if (unlikely(err))
  17446. + ii_write_unlock(inode);
  17447. +out:
  17448. + return err;
  17449. +}
  17450. +
  17451. +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  17452. + unsigned int d_type, ino_t *ino)
  17453. +{
  17454. + int err, idx;
  17455. + const int isnondir = d_type != DT_DIR;
  17456. +
  17457. + /* prevent hardlinked inode number from race condition */
  17458. + if (isnondir) {
  17459. + err = au_xinondir_enter(sb, bindex, h_ino, &idx);
  17460. + if (unlikely(err))
  17461. + goto out;
  17462. + }
  17463. +
  17464. + err = au_xino_read(sb, bindex, h_ino, ino);
  17465. + if (unlikely(err))
  17466. + goto out_xinondir;
  17467. +
  17468. + if (!*ino) {
  17469. + err = -EIO;
  17470. + *ino = au_xino_new_ino(sb);
  17471. + if (unlikely(!*ino))
  17472. + goto out_xinondir;
  17473. + err = au_xino_write(sb, bindex, h_ino, *ino);
  17474. + if (unlikely(err))
  17475. + goto out_xinondir;
  17476. + }
  17477. +
  17478. +out_xinondir:
  17479. + if (isnondir && idx >= 0)
  17480. + au_xinondir_leave(sb, bindex, h_ino, idx);
  17481. +out:
  17482. + return err;
  17483. +}
  17484. +
  17485. +/* successful returns with iinfo write_locked */
  17486. +/* todo: return with unlocked? */
  17487. +struct inode *au_new_inode(struct dentry *dentry, int must_new)
  17488. +{
  17489. + struct inode *inode, *h_inode;
  17490. + struct dentry *h_dentry;
  17491. + struct super_block *sb;
  17492. + ino_t h_ino, ino;
  17493. + int err, idx, hlinked;
  17494. + aufs_bindex_t btop;
  17495. +
  17496. + sb = dentry->d_sb;
  17497. + btop = au_dbtop(dentry);
  17498. + h_dentry = au_h_dptr(dentry, btop);
  17499. + h_inode = d_inode(h_dentry);
  17500. + h_ino = h_inode->i_ino;
  17501. + hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
  17502. +
  17503. +new_ino:
  17504. + /*
  17505. + * stop 'race'-ing between hardlinks under different
  17506. + * parents.
  17507. + */
  17508. + if (hlinked) {
  17509. + err = au_xinondir_enter(sb, btop, h_ino, &idx);
  17510. + inode = ERR_PTR(err);
  17511. + if (unlikely(err))
  17512. + goto out;
  17513. + }
  17514. +
  17515. + err = au_xino_read(sb, btop, h_ino, &ino);
  17516. + inode = ERR_PTR(err);
  17517. + if (unlikely(err))
  17518. + goto out_xinondir;
  17519. +
  17520. + if (!ino) {
  17521. + ino = au_xino_new_ino(sb);
  17522. + if (unlikely(!ino)) {
  17523. + inode = ERR_PTR(-EIO);
  17524. + goto out_xinondir;
  17525. + }
  17526. + }
  17527. +
  17528. + AuDbg("i%lu\n", (unsigned long)ino);
  17529. + inode = au_iget_locked(sb, ino);
  17530. + err = PTR_ERR(inode);
  17531. + if (IS_ERR(inode))
  17532. + goto out_xinondir;
  17533. +
  17534. + AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
  17535. + if (inode->i_state & I_NEW) {
  17536. + ii_write_lock_new_child(inode);
  17537. + err = set_inode(inode, dentry);
  17538. + if (!err) {
  17539. + unlock_new_inode(inode);
  17540. + goto out_xinondir; /* success */
  17541. + }
  17542. +
  17543. + /*
  17544. + * iget_failed() calls iput(), but we need to call
  17545. + * ii_write_unlock() after iget_failed(). so dirty hack for
  17546. + * i_count.
  17547. + */
  17548. + atomic_inc(&inode->i_count);
  17549. + iget_failed(inode);
  17550. + ii_write_unlock(inode);
  17551. + au_xino_write(sb, btop, h_ino, /*ino*/0);
  17552. + /* ignore this error */
  17553. + goto out_iput;
  17554. + } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
  17555. + /*
  17556. + * horrible race condition between lookup, readdir and copyup
  17557. + * (or something).
  17558. + */
  17559. + if (hlinked && idx >= 0)
  17560. + au_xinondir_leave(sb, btop, h_ino, idx);
  17561. + err = reval_inode(inode, dentry);
  17562. + if (unlikely(err < 0)) {
  17563. + hlinked = 0;
  17564. + goto out_iput;
  17565. + }
  17566. + if (!err)
  17567. + goto out; /* success */
  17568. + else if (hlinked && idx >= 0) {
  17569. + err = au_xinondir_enter(sb, btop, h_ino, &idx);
  17570. + if (unlikely(err)) {
  17571. + iput(inode);
  17572. + inode = ERR_PTR(err);
  17573. + goto out;
  17574. + }
  17575. + }
  17576. + }
  17577. +
  17578. + if (unlikely(au_test_fs_unique_ino(h_inode)))
  17579. + AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
  17580. + " b%d, %s, %pd, hi%lu, i%lu.\n",
  17581. + btop, au_sbtype(h_dentry->d_sb), dentry,
  17582. + (unsigned long)h_ino, (unsigned long)ino);
  17583. + ino = 0;
  17584. + err = au_xino_write(sb, btop, h_ino, /*ino*/0);
  17585. + if (!err) {
  17586. + iput(inode);
  17587. + if (hlinked && idx >= 0)
  17588. + au_xinondir_leave(sb, btop, h_ino, idx);
  17589. + goto new_ino;
  17590. + }
  17591. +
  17592. +out_iput:
  17593. + iput(inode);
  17594. + inode = ERR_PTR(err);
  17595. +out_xinondir:
  17596. + if (hlinked && idx >= 0)
  17597. + au_xinondir_leave(sb, btop, h_ino, idx);
  17598. +out:
  17599. + return inode;
  17600. +}
  17601. +
  17602. +/* ---------------------------------------------------------------------- */
  17603. +
  17604. +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
  17605. + struct inode *inode)
  17606. +{
  17607. + int err;
  17608. + struct inode *hi;
  17609. +
  17610. + err = au_br_rdonly(au_sbr(sb, bindex));
  17611. +
  17612. + /* pseudo-link after flushed may happen out of bounds */
  17613. + if (!err
  17614. + && inode
  17615. + && au_ibtop(inode) <= bindex
  17616. + && bindex <= au_ibbot(inode)) {
  17617. + /*
  17618. + * permission check is unnecessary since vfsub routine
  17619. + * will be called later
  17620. + */
  17621. + hi = au_h_iptr(inode, bindex);
  17622. + if (hi)
  17623. + err = IS_IMMUTABLE(hi) ? -EROFS : 0;
  17624. + }
  17625. +
  17626. + return err;
  17627. +}
  17628. +
  17629. +int au_test_h_perm(struct inode *h_inode, int mask)
  17630. +{
  17631. + if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
  17632. + return 0;
  17633. + return inode_permission(h_inode, mask);
  17634. +}
  17635. +
  17636. +int au_test_h_perm_sio(struct inode *h_inode, int mask)
  17637. +{
  17638. + if (au_test_nfs(h_inode->i_sb)
  17639. + && (mask & MAY_WRITE)
  17640. + && S_ISDIR(h_inode->i_mode))
  17641. + mask |= MAY_READ; /* force permission check */
  17642. + return au_test_h_perm(h_inode, mask);
  17643. +}
  17644. diff -Naur null/fs/aufs/inode.h linux-5.1/fs/aufs/inode.h
  17645. --- /dev/null
  17646. +++ linux-5.1/fs/aufs/inode.h 2019-05-22 11:23:07.441140996 +0200
  17647. @@ -0,0 +1,698 @@
  17648. +/* SPDX-License-Identifier: GPL-2.0 */
  17649. +/*
  17650. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  17651. + *
  17652. + * This program, aufs is free software; you can redistribute it and/or modify
  17653. + * it under the terms of the GNU General Public License as published by
  17654. + * the Free Software Foundation; either version 2 of the License, or
  17655. + * (at your option) any later version.
  17656. + *
  17657. + * This program is distributed in the hope that it will be useful,
  17658. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17659. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17660. + * GNU General Public License for more details.
  17661. + *
  17662. + * You should have received a copy of the GNU General Public License
  17663. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17664. + */
  17665. +
  17666. +/*
  17667. + * inode operations
  17668. + */
  17669. +
  17670. +#ifndef __AUFS_INODE_H__
  17671. +#define __AUFS_INODE_H__
  17672. +
  17673. +#ifdef __KERNEL__
  17674. +
  17675. +#include <linux/fsnotify.h>
  17676. +#include "rwsem.h"
  17677. +
  17678. +struct vfsmount;
  17679. +
  17680. +struct au_hnotify {
  17681. +#ifdef CONFIG_AUFS_HNOTIFY
  17682. +#ifdef CONFIG_AUFS_HFSNOTIFY
  17683. + /* never use fsnotify_add_vfsmount_mark() */
  17684. + struct fsnotify_mark hn_mark;
  17685. +#endif
  17686. + struct inode *hn_aufs_inode; /* no get/put */
  17687. + struct rcu_head rcu;
  17688. +#endif
  17689. +} ____cacheline_aligned_in_smp;
  17690. +
  17691. +struct au_hinode {
  17692. + struct inode *hi_inode;
  17693. + aufs_bindex_t hi_id;
  17694. +#ifdef CONFIG_AUFS_HNOTIFY
  17695. + struct au_hnotify *hi_notify;
  17696. +#endif
  17697. +
  17698. + /* reference to the copied-up whiteout with get/put */
  17699. + struct dentry *hi_whdentry;
  17700. +};
  17701. +
  17702. +/* ig_flags */
  17703. +#define AuIG_HALF_REFRESHED 1
  17704. +#define au_ig_ftest(flags, name) ((flags) & AuIG_##name)
  17705. +#define au_ig_fset(flags, name) \
  17706. + do { (flags) |= AuIG_##name; } while (0)
  17707. +#define au_ig_fclr(flags, name) \
  17708. + do { (flags) &= ~AuIG_##name; } while (0)
  17709. +
  17710. +struct au_iigen {
  17711. + spinlock_t ig_spin;
  17712. + __u32 ig_generation, ig_flags;
  17713. +};
  17714. +
  17715. +struct au_vdir;
  17716. +struct au_iinfo {
  17717. + struct au_iigen ii_generation;
  17718. + struct super_block *ii_hsb1; /* no get/put */
  17719. +
  17720. + struct au_rwsem ii_rwsem;
  17721. + aufs_bindex_t ii_btop, ii_bbot;
  17722. + __u32 ii_higen;
  17723. + struct au_hinode *ii_hinode;
  17724. + struct au_vdir *ii_vdir;
  17725. +};
  17726. +
  17727. +struct au_icntnr {
  17728. + struct au_iinfo iinfo;
  17729. + struct inode vfs_inode;
  17730. + struct hlist_bl_node plink;
  17731. + struct rcu_head rcu;
  17732. +} ____cacheline_aligned_in_smp;
  17733. +
  17734. +/* au_pin flags */
  17735. +#define AuPin_DI_LOCKED 1
  17736. +#define AuPin_MNT_WRITE (1 << 1)
  17737. +#define au_ftest_pin(flags, name) ((flags) & AuPin_##name)
  17738. +#define au_fset_pin(flags, name) \
  17739. + do { (flags) |= AuPin_##name; } while (0)
  17740. +#define au_fclr_pin(flags, name) \
  17741. + do { (flags) &= ~AuPin_##name; } while (0)
  17742. +
  17743. +struct au_pin {
  17744. + /* input */
  17745. + struct dentry *dentry;
  17746. + unsigned int udba;
  17747. + unsigned char lsc_di, lsc_hi, flags;
  17748. + aufs_bindex_t bindex;
  17749. +
  17750. + /* output */
  17751. + struct dentry *parent;
  17752. + struct au_hinode *hdir;
  17753. + struct vfsmount *h_mnt;
  17754. +
  17755. + /* temporary unlock/relock for copyup */
  17756. + struct dentry *h_dentry, *h_parent;
  17757. + struct au_branch *br;
  17758. + struct task_struct *task;
  17759. +};
  17760. +
  17761. +void au_pin_hdir_unlock(struct au_pin *p);
  17762. +int au_pin_hdir_lock(struct au_pin *p);
  17763. +int au_pin_hdir_relock(struct au_pin *p);
  17764. +void au_pin_hdir_acquire_nest(struct au_pin *p);
  17765. +void au_pin_hdir_release(struct au_pin *p);
  17766. +
  17767. +/* ---------------------------------------------------------------------- */
  17768. +
  17769. +static inline struct au_iinfo *au_ii(struct inode *inode)
  17770. +{
  17771. + BUG_ON(is_bad_inode(inode));
  17772. + return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
  17773. +}
  17774. +
  17775. +/* ---------------------------------------------------------------------- */
  17776. +
  17777. +/* inode.c */
  17778. +struct inode *au_igrab(struct inode *inode);
  17779. +void au_refresh_iop(struct inode *inode, int force_getattr);
  17780. +int au_refresh_hinode_self(struct inode *inode);
  17781. +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
  17782. +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  17783. + unsigned int d_type, ino_t *ino);
  17784. +struct inode *au_new_inode(struct dentry *dentry, int must_new);
  17785. +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
  17786. + struct inode *inode);
  17787. +int au_test_h_perm(struct inode *h_inode, int mask);
  17788. +int au_test_h_perm_sio(struct inode *h_inode, int mask);
  17789. +
  17790. +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
  17791. + ino_t h_ino, unsigned int d_type, ino_t *ino)
  17792. +{
  17793. +#ifdef CONFIG_AUFS_SHWH
  17794. + return au_ino(sb, bindex, h_ino, d_type, ino);
  17795. +#else
  17796. + return 0;
  17797. +#endif
  17798. +}
  17799. +
  17800. +/* i_op.c */
  17801. +enum {
  17802. + AuIop_SYMLINK,
  17803. + AuIop_DIR,
  17804. + AuIop_OTHER,
  17805. + AuIop_Last
  17806. +};
  17807. +extern struct inode_operations aufs_iop[AuIop_Last],
  17808. + aufs_iop_nogetattr[AuIop_Last];
  17809. +
  17810. +/* au_wr_dir flags */
  17811. +#define AuWrDir_ADD_ENTRY 1
  17812. +#define AuWrDir_ISDIR (1 << 1)
  17813. +#define AuWrDir_TMPFILE (1 << 2)
  17814. +#define au_ftest_wrdir(flags, name) ((flags) & AuWrDir_##name)
  17815. +#define au_fset_wrdir(flags, name) \
  17816. + do { (flags) |= AuWrDir_##name; } while (0)
  17817. +#define au_fclr_wrdir(flags, name) \
  17818. + do { (flags) &= ~AuWrDir_##name; } while (0)
  17819. +
  17820. +struct au_wr_dir_args {
  17821. + aufs_bindex_t force_btgt;
  17822. + unsigned char flags;
  17823. +};
  17824. +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
  17825. + struct au_wr_dir_args *args);
  17826. +
  17827. +struct dentry *au_pinned_h_parent(struct au_pin *pin);
  17828. +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
  17829. + aufs_bindex_t bindex, int lsc_di, int lsc_hi,
  17830. + unsigned int udba, unsigned char flags);
  17831. +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
  17832. + unsigned int udba, unsigned char flags) __must_check;
  17833. +int au_do_pin(struct au_pin *pin) __must_check;
  17834. +void au_unpin(struct au_pin *pin);
  17835. +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
  17836. +
  17837. +#define AuIcpup_DID_CPUP 1
  17838. +#define au_ftest_icpup(flags, name) ((flags) & AuIcpup_##name)
  17839. +#define au_fset_icpup(flags, name) \
  17840. + do { (flags) |= AuIcpup_##name; } while (0)
  17841. +#define au_fclr_icpup(flags, name) \
  17842. + do { (flags) &= ~AuIcpup_##name; } while (0)
  17843. +
  17844. +struct au_icpup_args {
  17845. + unsigned char flags;
  17846. + unsigned char pin_flags;
  17847. + aufs_bindex_t btgt;
  17848. + unsigned int udba;
  17849. + struct au_pin pin;
  17850. + struct path h_path;
  17851. + struct inode *h_inode;
  17852. +};
  17853. +
  17854. +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
  17855. + struct au_icpup_args *a);
  17856. +
  17857. +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
  17858. + int locked);
  17859. +
  17860. +/* i_op_add.c */
  17861. +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
  17862. + struct dentry *h_parent, int isdir);
  17863. +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  17864. + dev_t dev);
  17865. +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
  17866. +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  17867. + bool want_excl);
  17868. +struct vfsub_aopen_args;
  17869. +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
  17870. + struct vfsub_aopen_args *args);
  17871. +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode);
  17872. +int aufs_link(struct dentry *src_dentry, struct inode *dir,
  17873. + struct dentry *dentry);
  17874. +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode);
  17875. +
  17876. +/* i_op_del.c */
  17877. +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
  17878. +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
  17879. + struct dentry *h_parent, int isdir);
  17880. +int aufs_unlink(struct inode *dir, struct dentry *dentry);
  17881. +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
  17882. +
  17883. +/* i_op_ren.c */
  17884. +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
  17885. +int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
  17886. + struct inode *dir, struct dentry *dentry,
  17887. + unsigned int flags);
  17888. +
  17889. +/* iinfo.c */
  17890. +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
  17891. +void au_hiput(struct au_hinode *hinode);
  17892. +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
  17893. + struct dentry *h_wh);
  17894. +unsigned int au_hi_flags(struct inode *inode, int isdir);
  17895. +
  17896. +/* hinode flags */
  17897. +#define AuHi_XINO 1
  17898. +#define AuHi_HNOTIFY (1 << 1)
  17899. +#define au_ftest_hi(flags, name) ((flags) & AuHi_##name)
  17900. +#define au_fset_hi(flags, name) \
  17901. + do { (flags) |= AuHi_##name; } while (0)
  17902. +#define au_fclr_hi(flags, name) \
  17903. + do { (flags) &= ~AuHi_##name; } while (0)
  17904. +
  17905. +#ifndef CONFIG_AUFS_HNOTIFY
  17906. +#undef AuHi_HNOTIFY
  17907. +#define AuHi_HNOTIFY 0
  17908. +#endif
  17909. +
  17910. +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
  17911. + struct inode *h_inode, unsigned int flags);
  17912. +
  17913. +void au_update_iigen(struct inode *inode, int half);
  17914. +void au_update_ibrange(struct inode *inode, int do_put_zero);
  17915. +
  17916. +void au_icntnr_init_once(void *_c);
  17917. +void au_hinode_init(struct au_hinode *hinode);
  17918. +int au_iinfo_init(struct inode *inode);
  17919. +void au_iinfo_fin(struct inode *inode);
  17920. +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
  17921. +
  17922. +#ifdef CONFIG_PROC_FS
  17923. +/* plink.c */
  17924. +int au_plink_maint(struct super_block *sb, int flags);
  17925. +struct au_sbinfo;
  17926. +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
  17927. +int au_plink_maint_enter(struct super_block *sb);
  17928. +#ifdef CONFIG_AUFS_DEBUG
  17929. +void au_plink_list(struct super_block *sb);
  17930. +#else
  17931. +AuStubVoid(au_plink_list, struct super_block *sb)
  17932. +#endif
  17933. +int au_plink_test(struct inode *inode);
  17934. +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
  17935. +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
  17936. + struct dentry *h_dentry);
  17937. +void au_plink_put(struct super_block *sb, int verbose);
  17938. +void au_plink_clean(struct super_block *sb, int verbose);
  17939. +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
  17940. +#else
  17941. +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
  17942. +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
  17943. +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
  17944. +AuStubVoid(au_plink_list, struct super_block *sb);
  17945. +AuStubInt0(au_plink_test, struct inode *inode);
  17946. +AuStub(struct dentry *, au_plink_lkup, return NULL,
  17947. + struct inode *inode, aufs_bindex_t bindex);
  17948. +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
  17949. + struct dentry *h_dentry);
  17950. +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
  17951. +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
  17952. +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
  17953. +#endif /* CONFIG_PROC_FS */
  17954. +
  17955. +#ifdef CONFIG_AUFS_XATTR
  17956. +/* xattr.c */
  17957. +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
  17958. + unsigned int verbose);
  17959. +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
  17960. +void au_xattr_init(struct super_block *sb);
  17961. +#else
  17962. +AuStubInt0(au_cpup_xattr, struct dentry *h_dst, struct dentry *h_src,
  17963. + int ignore_flags, unsigned int verbose);
  17964. +AuStubVoid(au_xattr_init, struct super_block *sb);
  17965. +#endif
  17966. +
  17967. +#ifdef CONFIG_FS_POSIX_ACL
  17968. +struct posix_acl *aufs_get_acl(struct inode *inode, int type);
  17969. +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
  17970. +#endif
  17971. +
  17972. +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
  17973. +enum {
  17974. + AU_XATTR_SET,
  17975. + AU_ACL_SET
  17976. +};
  17977. +
  17978. +struct au_sxattr {
  17979. + int type;
  17980. + union {
  17981. + struct {
  17982. + const char *name;
  17983. + const void *value;
  17984. + size_t size;
  17985. + int flags;
  17986. + } set;
  17987. + struct {
  17988. + struct posix_acl *acl;
  17989. + int type;
  17990. + } acl_set;
  17991. + } u;
  17992. +};
  17993. +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
  17994. + struct au_sxattr *arg);
  17995. +#endif
  17996. +
  17997. +/* ---------------------------------------------------------------------- */
  17998. +
  17999. +/* lock subclass for iinfo */
  18000. +enum {
  18001. + AuLsc_II_CHILD, /* child first */
  18002. + AuLsc_II_CHILD2, /* rename(2), link(2), and cpup at hnotify */
  18003. + AuLsc_II_CHILD3, /* copyup dirs */
  18004. + AuLsc_II_PARENT, /* see AuLsc_I_PARENT in vfsub.h */
  18005. + AuLsc_II_PARENT2,
  18006. + AuLsc_II_PARENT3, /* copyup dirs */
  18007. + AuLsc_II_NEW_CHILD
  18008. +};
  18009. +
  18010. +/*
  18011. + * ii_read_lock_child, ii_write_lock_child,
  18012. + * ii_read_lock_child2, ii_write_lock_child2,
  18013. + * ii_read_lock_child3, ii_write_lock_child3,
  18014. + * ii_read_lock_parent, ii_write_lock_parent,
  18015. + * ii_read_lock_parent2, ii_write_lock_parent2,
  18016. + * ii_read_lock_parent3, ii_write_lock_parent3,
  18017. + * ii_read_lock_new_child, ii_write_lock_new_child,
  18018. + */
  18019. +#define AuReadLockFunc(name, lsc) \
  18020. +static inline void ii_read_lock_##name(struct inode *i) \
  18021. +{ \
  18022. + au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
  18023. +}
  18024. +
  18025. +#define AuWriteLockFunc(name, lsc) \
  18026. +static inline void ii_write_lock_##name(struct inode *i) \
  18027. +{ \
  18028. + au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
  18029. +}
  18030. +
  18031. +#define AuRWLockFuncs(name, lsc) \
  18032. + AuReadLockFunc(name, lsc) \
  18033. + AuWriteLockFunc(name, lsc)
  18034. +
  18035. +AuRWLockFuncs(child, CHILD);
  18036. +AuRWLockFuncs(child2, CHILD2);
  18037. +AuRWLockFuncs(child3, CHILD3);
  18038. +AuRWLockFuncs(parent, PARENT);
  18039. +AuRWLockFuncs(parent2, PARENT2);
  18040. +AuRWLockFuncs(parent3, PARENT3);
  18041. +AuRWLockFuncs(new_child, NEW_CHILD);
  18042. +
  18043. +#undef AuReadLockFunc
  18044. +#undef AuWriteLockFunc
  18045. +#undef AuRWLockFuncs
  18046. +
  18047. +#define ii_read_unlock(i) au_rw_read_unlock(&au_ii(i)->ii_rwsem)
  18048. +#define ii_write_unlock(i) au_rw_write_unlock(&au_ii(i)->ii_rwsem)
  18049. +#define ii_downgrade_lock(i) au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
  18050. +
  18051. +#define IiMustNoWaiters(i) AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
  18052. +#define IiMustAnyLock(i) AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
  18053. +#define IiMustWriteLock(i) AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
  18054. +
  18055. +/* ---------------------------------------------------------------------- */
  18056. +
  18057. +static inline void au_icntnr_init(struct au_icntnr *c)
  18058. +{
  18059. +#ifdef CONFIG_AUFS_DEBUG
  18060. + c->vfs_inode.i_mode = 0;
  18061. +#endif
  18062. +}
  18063. +
  18064. +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
  18065. +{
  18066. + unsigned int gen;
  18067. + struct au_iinfo *iinfo;
  18068. + struct au_iigen *iigen;
  18069. +
  18070. + iinfo = au_ii(inode);
  18071. + iigen = &iinfo->ii_generation;
  18072. + spin_lock(&iigen->ig_spin);
  18073. + if (igflags)
  18074. + *igflags = iigen->ig_flags;
  18075. + gen = iigen->ig_generation;
  18076. + spin_unlock(&iigen->ig_spin);
  18077. +
  18078. + return gen;
  18079. +}
  18080. +
  18081. +/* tiny test for inode number */
  18082. +/* tmpfs generation is too rough */
  18083. +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
  18084. +{
  18085. + struct au_iinfo *iinfo;
  18086. +
  18087. + iinfo = au_ii(inode);
  18088. + AuRwMustAnyLock(&iinfo->ii_rwsem);
  18089. + return !(iinfo->ii_hsb1 == h_inode->i_sb
  18090. + && iinfo->ii_higen == h_inode->i_generation);
  18091. +}
  18092. +
  18093. +static inline void au_iigen_dec(struct inode *inode)
  18094. +{
  18095. + struct au_iinfo *iinfo;
  18096. + struct au_iigen *iigen;
  18097. +
  18098. + iinfo = au_ii(inode);
  18099. + iigen = &iinfo->ii_generation;
  18100. + spin_lock(&iigen->ig_spin);
  18101. + iigen->ig_generation--;
  18102. + spin_unlock(&iigen->ig_spin);
  18103. +}
  18104. +
  18105. +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
  18106. +{
  18107. + int err;
  18108. +
  18109. + err = 0;
  18110. + if (unlikely(inode && au_iigen(inode, NULL) != sigen))
  18111. + err = -EIO;
  18112. +
  18113. + return err;
  18114. +}
  18115. +
  18116. +/* ---------------------------------------------------------------------- */
  18117. +
  18118. +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
  18119. + aufs_bindex_t bindex)
  18120. +{
  18121. + return iinfo->ii_hinode + bindex;
  18122. +}
  18123. +
  18124. +static inline int au_is_bad_inode(struct inode *inode)
  18125. +{
  18126. + return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
  18127. +}
  18128. +
  18129. +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
  18130. + aufs_bindex_t bindex)
  18131. +{
  18132. + IiMustAnyLock(inode);
  18133. + return au_hinode(au_ii(inode), bindex)->hi_id;
  18134. +}
  18135. +
  18136. +static inline aufs_bindex_t au_ibtop(struct inode *inode)
  18137. +{
  18138. + IiMustAnyLock(inode);
  18139. + return au_ii(inode)->ii_btop;
  18140. +}
  18141. +
  18142. +static inline aufs_bindex_t au_ibbot(struct inode *inode)
  18143. +{
  18144. + IiMustAnyLock(inode);
  18145. + return au_ii(inode)->ii_bbot;
  18146. +}
  18147. +
  18148. +static inline struct au_vdir *au_ivdir(struct inode *inode)
  18149. +{
  18150. + IiMustAnyLock(inode);
  18151. + return au_ii(inode)->ii_vdir;
  18152. +}
  18153. +
  18154. +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
  18155. +{
  18156. + IiMustAnyLock(inode);
  18157. + return au_hinode(au_ii(inode), bindex)->hi_whdentry;
  18158. +}
  18159. +
  18160. +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
  18161. +{
  18162. + IiMustWriteLock(inode);
  18163. + au_ii(inode)->ii_btop = bindex;
  18164. +}
  18165. +
  18166. +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
  18167. +{
  18168. + IiMustWriteLock(inode);
  18169. + au_ii(inode)->ii_bbot = bindex;
  18170. +}
  18171. +
  18172. +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
  18173. +{
  18174. + IiMustWriteLock(inode);
  18175. + au_ii(inode)->ii_vdir = vdir;
  18176. +}
  18177. +
  18178. +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
  18179. +{
  18180. + IiMustAnyLock(inode);
  18181. + return au_hinode(au_ii(inode), bindex);
  18182. +}
  18183. +
  18184. +/* ---------------------------------------------------------------------- */
  18185. +
  18186. +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
  18187. +{
  18188. + if (pin)
  18189. + return pin->parent;
  18190. + return NULL;
  18191. +}
  18192. +
  18193. +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
  18194. +{
  18195. + if (pin && pin->hdir)
  18196. + return pin->hdir->hi_inode;
  18197. + return NULL;
  18198. +}
  18199. +
  18200. +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
  18201. +{
  18202. + if (pin)
  18203. + return pin->hdir;
  18204. + return NULL;
  18205. +}
  18206. +
  18207. +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
  18208. +{
  18209. + if (pin)
  18210. + pin->dentry = dentry;
  18211. +}
  18212. +
  18213. +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
  18214. + unsigned char lflag)
  18215. +{
  18216. + if (pin) {
  18217. + if (lflag)
  18218. + au_fset_pin(pin->flags, DI_LOCKED);
  18219. + else
  18220. + au_fclr_pin(pin->flags, DI_LOCKED);
  18221. + }
  18222. +}
  18223. +
  18224. +#if 0 /* reserved */
  18225. +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
  18226. +{
  18227. + if (pin) {
  18228. + dput(pin->parent);
  18229. + pin->parent = dget(parent);
  18230. + }
  18231. +}
  18232. +#endif
  18233. +
  18234. +/* ---------------------------------------------------------------------- */
  18235. +
  18236. +struct au_branch;
  18237. +#ifdef CONFIG_AUFS_HNOTIFY
  18238. +struct au_hnotify_op {
  18239. + void (*ctl)(struct au_hinode *hinode, int do_set);
  18240. + int (*alloc)(struct au_hinode *hinode);
  18241. +
  18242. + /*
  18243. + * if it returns true, the the caller should free hinode->hi_notify,
  18244. + * otherwise ->free() frees it.
  18245. + */
  18246. + int (*free)(struct au_hinode *hinode,
  18247. + struct au_hnotify *hn) __must_check;
  18248. +
  18249. + void (*fin)(void);
  18250. + int (*init)(void);
  18251. +
  18252. + int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
  18253. + void (*fin_br)(struct au_branch *br);
  18254. + int (*init_br)(struct au_branch *br, int perm);
  18255. +};
  18256. +
  18257. +/* hnotify.c */
  18258. +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
  18259. +void au_hn_free(struct au_hinode *hinode);
  18260. +void au_hn_ctl(struct au_hinode *hinode, int do_set);
  18261. +void au_hn_reset(struct inode *inode, unsigned int flags);
  18262. +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
  18263. + struct qstr *h_child_qstr, struct inode *h_child_inode);
  18264. +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
  18265. +int au_hnotify_init_br(struct au_branch *br, int perm);
  18266. +void au_hnotify_fin_br(struct au_branch *br);
  18267. +int __init au_hnotify_init(void);
  18268. +void au_hnotify_fin(void);
  18269. +
  18270. +/* hfsnotify.c */
  18271. +extern const struct au_hnotify_op au_hnotify_op;
  18272. +
  18273. +static inline
  18274. +void au_hn_init(struct au_hinode *hinode)
  18275. +{
  18276. + hinode->hi_notify = NULL;
  18277. +}
  18278. +
  18279. +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
  18280. +{
  18281. + return hinode->hi_notify;
  18282. +}
  18283. +
  18284. +#else
  18285. +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
  18286. + struct au_hinode *hinode __maybe_unused,
  18287. + struct inode *inode __maybe_unused)
  18288. +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
  18289. +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
  18290. +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
  18291. + int do_set __maybe_unused)
  18292. +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
  18293. + unsigned int flags __maybe_unused)
  18294. +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
  18295. + struct au_branch *br __maybe_unused,
  18296. + int perm __maybe_unused)
  18297. +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
  18298. + int perm __maybe_unused)
  18299. +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
  18300. +AuStubInt0(__init au_hnotify_init, void)
  18301. +AuStubVoid(au_hnotify_fin, void)
  18302. +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
  18303. +#endif /* CONFIG_AUFS_HNOTIFY */
  18304. +
  18305. +static inline void au_hn_suspend(struct au_hinode *hdir)
  18306. +{
  18307. + au_hn_ctl(hdir, /*do_set*/0);
  18308. +}
  18309. +
  18310. +static inline void au_hn_resume(struct au_hinode *hdir)
  18311. +{
  18312. + au_hn_ctl(hdir, /*do_set*/1);
  18313. +}
  18314. +
  18315. +static inline void au_hn_inode_lock(struct au_hinode *hdir)
  18316. +{
  18317. + inode_lock(hdir->hi_inode);
  18318. + au_hn_suspend(hdir);
  18319. +}
  18320. +
  18321. +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
  18322. + unsigned int sc __maybe_unused)
  18323. +{
  18324. + inode_lock_nested(hdir->hi_inode, sc);
  18325. + au_hn_suspend(hdir);
  18326. +}
  18327. +
  18328. +#if 0 /* unused */
  18329. +#include "vfsub.h"
  18330. +static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
  18331. + unsigned int sc)
  18332. +{
  18333. + inode_lock_shared_nested(hdir->hi_inode, sc);
  18334. + au_hn_suspend(hdir);
  18335. +}
  18336. +#endif
  18337. +
  18338. +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
  18339. +{
  18340. + au_hn_resume(hdir);
  18341. + inode_unlock(hdir->hi_inode);
  18342. +}
  18343. +
  18344. +#endif /* __KERNEL__ */
  18345. +#endif /* __AUFS_INODE_H__ */
  18346. diff -Naur null/fs/aufs/ioctl.c linux-5.1/fs/aufs/ioctl.c
  18347. --- /dev/null
  18348. +++ linux-5.1/fs/aufs/ioctl.c 2019-05-22 11:23:07.441140996 +0200
  18349. @@ -0,0 +1,220 @@
  18350. +// SPDX-License-Identifier: GPL-2.0
  18351. +/*
  18352. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  18353. + *
  18354. + * This program, aufs is free software; you can redistribute it and/or modify
  18355. + * it under the terms of the GNU General Public License as published by
  18356. + * the Free Software Foundation; either version 2 of the License, or
  18357. + * (at your option) any later version.
  18358. + *
  18359. + * This program is distributed in the hope that it will be useful,
  18360. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18361. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18362. + * GNU General Public License for more details.
  18363. + *
  18364. + * You should have received a copy of the GNU General Public License
  18365. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18366. + */
  18367. +
  18368. +/*
  18369. + * ioctl
  18370. + * plink-management and readdir in userspace.
  18371. + * assist the pathconf(3) wrapper library.
  18372. + * move-down
  18373. + * File-based Hierarchical Storage Management.
  18374. + */
  18375. +
  18376. +#include <linux/compat.h>
  18377. +#include <linux/file.h>
  18378. +#include "aufs.h"
  18379. +
  18380. +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
  18381. +{
  18382. + int err, fd;
  18383. + aufs_bindex_t wbi, bindex, bbot;
  18384. + struct file *h_file;
  18385. + struct super_block *sb;
  18386. + struct dentry *root;
  18387. + struct au_branch *br;
  18388. + struct aufs_wbr_fd wbrfd = {
  18389. + .oflags = au_dir_roflags,
  18390. + .brid = -1
  18391. + };
  18392. + const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
  18393. + | O_NOATIME | O_CLOEXEC;
  18394. +
  18395. + AuDebugOn(wbrfd.oflags & ~valid);
  18396. +
  18397. + if (arg) {
  18398. + err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
  18399. + if (unlikely(err)) {
  18400. + err = -EFAULT;
  18401. + goto out;
  18402. + }
  18403. +
  18404. + err = -EINVAL;
  18405. + AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
  18406. + wbrfd.oflags |= au_dir_roflags;
  18407. + AuDbg("0%o\n", wbrfd.oflags);
  18408. + if (unlikely(wbrfd.oflags & ~valid))
  18409. + goto out;
  18410. + }
  18411. +
  18412. + fd = get_unused_fd_flags(0);
  18413. + err = fd;
  18414. + if (unlikely(fd < 0))
  18415. + goto out;
  18416. +
  18417. + h_file = ERR_PTR(-EINVAL);
  18418. + wbi = 0;
  18419. + br = NULL;
  18420. + sb = path->dentry->d_sb;
  18421. + root = sb->s_root;
  18422. + aufs_read_lock(root, AuLock_IR);
  18423. + bbot = au_sbbot(sb);
  18424. + if (wbrfd.brid >= 0) {
  18425. + wbi = au_br_index(sb, wbrfd.brid);
  18426. + if (unlikely(wbi < 0 || wbi > bbot))
  18427. + goto out_unlock;
  18428. + }
  18429. +
  18430. + h_file = ERR_PTR(-ENOENT);
  18431. + br = au_sbr(sb, wbi);
  18432. + if (!au_br_writable(br->br_perm)) {
  18433. + if (arg)
  18434. + goto out_unlock;
  18435. +
  18436. + bindex = wbi + 1;
  18437. + wbi = -1;
  18438. + for (; bindex <= bbot; bindex++) {
  18439. + br = au_sbr(sb, bindex);
  18440. + if (au_br_writable(br->br_perm)) {
  18441. + wbi = bindex;
  18442. + br = au_sbr(sb, wbi);
  18443. + break;
  18444. + }
  18445. + }
  18446. + }
  18447. + AuDbg("wbi %d\n", wbi);
  18448. + if (wbi >= 0)
  18449. + h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
  18450. + /*force_wr*/0);
  18451. +
  18452. +out_unlock:
  18453. + aufs_read_unlock(root, AuLock_IR);
  18454. + err = PTR_ERR(h_file);
  18455. + if (IS_ERR(h_file))
  18456. + goto out_fd;
  18457. +
  18458. + au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
  18459. + fd_install(fd, h_file);
  18460. + err = fd;
  18461. + goto out; /* success */
  18462. +
  18463. +out_fd:
  18464. + put_unused_fd(fd);
  18465. +out:
  18466. + AuTraceErr(err);
  18467. + return err;
  18468. +}
  18469. +
  18470. +/* ---------------------------------------------------------------------- */
  18471. +
  18472. +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
  18473. +{
  18474. + long err;
  18475. + struct dentry *dentry;
  18476. +
  18477. + switch (cmd) {
  18478. + case AUFS_CTL_RDU:
  18479. + case AUFS_CTL_RDU_INO:
  18480. + err = au_rdu_ioctl(file, cmd, arg);
  18481. + break;
  18482. +
  18483. + case AUFS_CTL_WBR_FD:
  18484. + err = au_wbr_fd(&file->f_path, (void __user *)arg);
  18485. + break;
  18486. +
  18487. + case AUFS_CTL_IBUSY:
  18488. + err = au_ibusy_ioctl(file, arg);
  18489. + break;
  18490. +
  18491. + case AUFS_CTL_BRINFO:
  18492. + err = au_brinfo_ioctl(file, arg);
  18493. + break;
  18494. +
  18495. + case AUFS_CTL_FHSM_FD:
  18496. + dentry = file->f_path.dentry;
  18497. + if (IS_ROOT(dentry))
  18498. + err = au_fhsm_fd(dentry->d_sb, arg);
  18499. + else
  18500. + err = -ENOTTY;
  18501. + break;
  18502. +
  18503. + default:
  18504. + /* do not call the lower */
  18505. + AuDbg("0x%x\n", cmd);
  18506. + err = -ENOTTY;
  18507. + }
  18508. +
  18509. + AuTraceErr(err);
  18510. + return err;
  18511. +}
  18512. +
  18513. +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
  18514. +{
  18515. + long err;
  18516. +
  18517. + switch (cmd) {
  18518. + case AUFS_CTL_MVDOWN:
  18519. + err = au_mvdown(file->f_path.dentry, (void __user *)arg);
  18520. + break;
  18521. +
  18522. + case AUFS_CTL_WBR_FD:
  18523. + err = au_wbr_fd(&file->f_path, (void __user *)arg);
  18524. + break;
  18525. +
  18526. + default:
  18527. + /* do not call the lower */
  18528. + AuDbg("0x%x\n", cmd);
  18529. + err = -ENOTTY;
  18530. + }
  18531. +
  18532. + AuTraceErr(err);
  18533. + return err;
  18534. +}
  18535. +
  18536. +#ifdef CONFIG_COMPAT
  18537. +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
  18538. + unsigned long arg)
  18539. +{
  18540. + long err;
  18541. +
  18542. + switch (cmd) {
  18543. + case AUFS_CTL_RDU:
  18544. + case AUFS_CTL_RDU_INO:
  18545. + err = au_rdu_compat_ioctl(file, cmd, arg);
  18546. + break;
  18547. +
  18548. + case AUFS_CTL_IBUSY:
  18549. + err = au_ibusy_compat_ioctl(file, arg);
  18550. + break;
  18551. +
  18552. + case AUFS_CTL_BRINFO:
  18553. + err = au_brinfo_compat_ioctl(file, arg);
  18554. + break;
  18555. +
  18556. + default:
  18557. + err = aufs_ioctl_dir(file, cmd, arg);
  18558. + }
  18559. +
  18560. + AuTraceErr(err);
  18561. + return err;
  18562. +}
  18563. +
  18564. +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
  18565. + unsigned long arg)
  18566. +{
  18567. + return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
  18568. +}
  18569. +#endif
  18570. diff -Naur null/fs/aufs/i_op_add.c linux-5.1/fs/aufs/i_op_add.c
  18571. --- /dev/null
  18572. +++ linux-5.1/fs/aufs/i_op_add.c 2019-05-22 11:23:07.441140996 +0200
  18573. @@ -0,0 +1,936 @@
  18574. +// SPDX-License-Identifier: GPL-2.0
  18575. +/*
  18576. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  18577. + *
  18578. + * This program, aufs is free software; you can redistribute it and/or modify
  18579. + * it under the terms of the GNU General Public License as published by
  18580. + * the Free Software Foundation; either version 2 of the License, or
  18581. + * (at your option) any later version.
  18582. + *
  18583. + * This program is distributed in the hope that it will be useful,
  18584. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18585. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18586. + * GNU General Public License for more details.
  18587. + *
  18588. + * You should have received a copy of the GNU General Public License
  18589. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18590. + */
  18591. +
  18592. +/*
  18593. + * inode operations (add entry)
  18594. + */
  18595. +
  18596. +#include <linux/iversion.h>
  18597. +#include "aufs.h"
  18598. +
  18599. +/*
  18600. + * final procedure of adding a new entry, except link(2).
  18601. + * remove whiteout, instantiate, copyup the parent dir's times and size
  18602. + * and update version.
  18603. + * if it failed, re-create the removed whiteout.
  18604. + */
  18605. +static int epilog(struct inode *dir, aufs_bindex_t bindex,
  18606. + struct dentry *wh_dentry, struct dentry *dentry)
  18607. +{
  18608. + int err, rerr;
  18609. + aufs_bindex_t bwh;
  18610. + struct path h_path;
  18611. + struct super_block *sb;
  18612. + struct inode *inode, *h_dir;
  18613. + struct dentry *wh;
  18614. +
  18615. + bwh = -1;
  18616. + sb = dir->i_sb;
  18617. + if (wh_dentry) {
  18618. + h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
  18619. + IMustLock(h_dir);
  18620. + AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
  18621. + bwh = au_dbwh(dentry);
  18622. + h_path.dentry = wh_dentry;
  18623. + h_path.mnt = au_sbr_mnt(sb, bindex);
  18624. + err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
  18625. + dentry);
  18626. + if (unlikely(err))
  18627. + goto out;
  18628. + }
  18629. +
  18630. + inode = au_new_inode(dentry, /*must_new*/1);
  18631. + if (!IS_ERR(inode)) {
  18632. + d_instantiate(dentry, inode);
  18633. + dir = d_inode(dentry->d_parent); /* dir inode is locked */
  18634. + IMustLock(dir);
  18635. + au_dir_ts(dir, bindex);
  18636. + inode_inc_iversion(dir);
  18637. + au_fhsm_wrote(sb, bindex, /*force*/0);
  18638. + return 0; /* success */
  18639. + }
  18640. +
  18641. + err = PTR_ERR(inode);
  18642. + if (!wh_dentry)
  18643. + goto out;
  18644. +
  18645. + /* revert */
  18646. + /* dir inode is locked */
  18647. + wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
  18648. + rerr = PTR_ERR(wh);
  18649. + if (IS_ERR(wh)) {
  18650. + AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
  18651. + dentry, err, rerr);
  18652. + err = -EIO;
  18653. + } else
  18654. + dput(wh);
  18655. +
  18656. +out:
  18657. + return err;
  18658. +}
  18659. +
  18660. +static int au_d_may_add(struct dentry *dentry)
  18661. +{
  18662. + int err;
  18663. +
  18664. + err = 0;
  18665. + if (unlikely(d_unhashed(dentry)))
  18666. + err = -ENOENT;
  18667. + if (unlikely(d_really_is_positive(dentry)))
  18668. + err = -EEXIST;
  18669. + return err;
  18670. +}
  18671. +
  18672. +/*
  18673. + * simple tests for the adding inode operations.
  18674. + * following the checks in vfs, plus the parent-child relationship.
  18675. + */
  18676. +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
  18677. + struct dentry *h_parent, int isdir)
  18678. +{
  18679. + int err;
  18680. + umode_t h_mode;
  18681. + struct dentry *h_dentry;
  18682. + struct inode *h_inode;
  18683. +
  18684. + err = -ENAMETOOLONG;
  18685. + if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
  18686. + goto out;
  18687. +
  18688. + h_dentry = au_h_dptr(dentry, bindex);
  18689. + if (d_really_is_negative(dentry)) {
  18690. + err = -EEXIST;
  18691. + if (unlikely(d_is_positive(h_dentry)))
  18692. + goto out;
  18693. + } else {
  18694. + /* rename(2) case */
  18695. + err = -EIO;
  18696. + if (unlikely(d_is_negative(h_dentry)))
  18697. + goto out;
  18698. + h_inode = d_inode(h_dentry);
  18699. + if (unlikely(!h_inode->i_nlink))
  18700. + goto out;
  18701. +
  18702. + h_mode = h_inode->i_mode;
  18703. + if (!isdir) {
  18704. + err = -EISDIR;
  18705. + if (unlikely(S_ISDIR(h_mode)))
  18706. + goto out;
  18707. + } else if (unlikely(!S_ISDIR(h_mode))) {
  18708. + err = -ENOTDIR;
  18709. + goto out;
  18710. + }
  18711. + }
  18712. +
  18713. + err = 0;
  18714. + /* expected parent dir is locked */
  18715. + if (unlikely(h_parent != h_dentry->d_parent))
  18716. + err = -EIO;
  18717. +
  18718. +out:
  18719. + AuTraceErr(err);
  18720. + return err;
  18721. +}
  18722. +
  18723. +/*
  18724. + * initial procedure of adding a new entry.
  18725. + * prepare writable branch and the parent dir, lock it,
  18726. + * and lookup whiteout for the new entry.
  18727. + */
  18728. +static struct dentry*
  18729. +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
  18730. + struct dentry *src_dentry, struct au_pin *pin,
  18731. + struct au_wr_dir_args *wr_dir_args)
  18732. +{
  18733. + struct dentry *wh_dentry, *h_parent;
  18734. + struct super_block *sb;
  18735. + struct au_branch *br;
  18736. + int err;
  18737. + unsigned int udba;
  18738. + aufs_bindex_t bcpup;
  18739. +
  18740. + AuDbg("%pd\n", dentry);
  18741. +
  18742. + err = au_wr_dir(dentry, src_dentry, wr_dir_args);
  18743. + bcpup = err;
  18744. + wh_dentry = ERR_PTR(err);
  18745. + if (unlikely(err < 0))
  18746. + goto out;
  18747. +
  18748. + sb = dentry->d_sb;
  18749. + udba = au_opt_udba(sb);
  18750. + err = au_pin(pin, dentry, bcpup, udba,
  18751. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  18752. + wh_dentry = ERR_PTR(err);
  18753. + if (unlikely(err))
  18754. + goto out;
  18755. +
  18756. + h_parent = au_pinned_h_parent(pin);
  18757. + if (udba != AuOpt_UDBA_NONE
  18758. + && au_dbtop(dentry) == bcpup)
  18759. + err = au_may_add(dentry, bcpup, h_parent,
  18760. + au_ftest_wrdir(wr_dir_args->flags, ISDIR));
  18761. + else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
  18762. + err = -ENAMETOOLONG;
  18763. + wh_dentry = ERR_PTR(err);
  18764. + if (unlikely(err))
  18765. + goto out_unpin;
  18766. +
  18767. + br = au_sbr(sb, bcpup);
  18768. + if (dt) {
  18769. + struct path tmp = {
  18770. + .dentry = h_parent,
  18771. + .mnt = au_br_mnt(br)
  18772. + };
  18773. + au_dtime_store(dt, au_pinned_parent(pin), &tmp);
  18774. + }
  18775. +
  18776. + wh_dentry = NULL;
  18777. + if (bcpup != au_dbwh(dentry))
  18778. + goto out; /* success */
  18779. +
  18780. + /*
  18781. + * ENAMETOOLONG here means that if we allowed create such name, then it
  18782. + * would not be able to removed in the future. So we don't allow such
  18783. + * name here and we don't handle ENAMETOOLONG differently here.
  18784. + */
  18785. + wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
  18786. +
  18787. +out_unpin:
  18788. + if (IS_ERR(wh_dentry))
  18789. + au_unpin(pin);
  18790. +out:
  18791. + return wh_dentry;
  18792. +}
  18793. +
  18794. +/* ---------------------------------------------------------------------- */
  18795. +
  18796. +enum { Mknod, Symlink, Creat };
  18797. +struct simple_arg {
  18798. + int type;
  18799. + union {
  18800. + struct {
  18801. + umode_t mode;
  18802. + bool want_excl;
  18803. + bool try_aopen;
  18804. + struct vfsub_aopen_args *aopen;
  18805. + } c;
  18806. + struct {
  18807. + const char *symname;
  18808. + } s;
  18809. + struct {
  18810. + umode_t mode;
  18811. + dev_t dev;
  18812. + } m;
  18813. + } u;
  18814. +};
  18815. +
  18816. +static int add_simple(struct inode *dir, struct dentry *dentry,
  18817. + struct simple_arg *arg)
  18818. +{
  18819. + int err, rerr;
  18820. + aufs_bindex_t btop;
  18821. + unsigned char created;
  18822. + const unsigned char try_aopen
  18823. + = (arg->type == Creat && arg->u.c.try_aopen);
  18824. + struct vfsub_aopen_args *aopen = arg->u.c.aopen;
  18825. + struct dentry *wh_dentry, *parent;
  18826. + struct inode *h_dir;
  18827. + struct super_block *sb;
  18828. + struct au_branch *br;
  18829. + /* to reduce stack size */
  18830. + struct {
  18831. + struct au_dtime dt;
  18832. + struct au_pin pin;
  18833. + struct path h_path;
  18834. + struct au_wr_dir_args wr_dir_args;
  18835. + } *a;
  18836. +
  18837. + AuDbg("%pd\n", dentry);
  18838. + IMustLock(dir);
  18839. +
  18840. + err = -ENOMEM;
  18841. + a = kmalloc(sizeof(*a), GFP_NOFS);
  18842. + if (unlikely(!a))
  18843. + goto out;
  18844. + a->wr_dir_args.force_btgt = -1;
  18845. + a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
  18846. +
  18847. + parent = dentry->d_parent; /* dir inode is locked */
  18848. + if (!try_aopen) {
  18849. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
  18850. + if (unlikely(err))
  18851. + goto out_free;
  18852. + }
  18853. + err = au_d_may_add(dentry);
  18854. + if (unlikely(err))
  18855. + goto out_unlock;
  18856. + if (!try_aopen)
  18857. + di_write_lock_parent(parent);
  18858. + wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
  18859. + &a->pin, &a->wr_dir_args);
  18860. + err = PTR_ERR(wh_dentry);
  18861. + if (IS_ERR(wh_dentry))
  18862. + goto out_parent;
  18863. +
  18864. + btop = au_dbtop(dentry);
  18865. + sb = dentry->d_sb;
  18866. + br = au_sbr(sb, btop);
  18867. + a->h_path.dentry = au_h_dptr(dentry, btop);
  18868. + a->h_path.mnt = au_br_mnt(br);
  18869. + h_dir = au_pinned_h_dir(&a->pin);
  18870. + switch (arg->type) {
  18871. + case Creat:
  18872. + if (!try_aopen || !h_dir->i_op->atomic_open) {
  18873. + err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
  18874. + arg->u.c.want_excl);
  18875. + created = !err;
  18876. + if (!err && try_aopen)
  18877. + aopen->file->f_mode |= FMODE_CREATED;
  18878. + } else {
  18879. + aopen->br = br;
  18880. + err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
  18881. + AuDbg("err %d\n", err);
  18882. + AuDbgFile(aopen->file);
  18883. + created = err >= 0
  18884. + && !!(aopen->file->f_mode & FMODE_CREATED);
  18885. + }
  18886. + break;
  18887. + case Symlink:
  18888. + err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
  18889. + created = !err;
  18890. + break;
  18891. + case Mknod:
  18892. + err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
  18893. + arg->u.m.dev);
  18894. + created = !err;
  18895. + break;
  18896. + default:
  18897. + BUG();
  18898. + }
  18899. + if (unlikely(err < 0))
  18900. + goto out_unpin;
  18901. +
  18902. + err = epilog(dir, btop, wh_dentry, dentry);
  18903. + if (!err)
  18904. + goto out_unpin; /* success */
  18905. +
  18906. + /* revert */
  18907. + if (created /* && d_is_positive(a->h_path.dentry) */) {
  18908. + /* no delegation since it is just created */
  18909. + rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
  18910. + /*force*/0);
  18911. + if (rerr) {
  18912. + AuIOErr("%pd revert failure(%d, %d)\n",
  18913. + dentry, err, rerr);
  18914. + err = -EIO;
  18915. + }
  18916. + au_dtime_revert(&a->dt);
  18917. + }
  18918. + if (try_aopen && h_dir->i_op->atomic_open
  18919. + && (aopen->file->f_mode & FMODE_OPENED))
  18920. + /* aopen->file is still opened */
  18921. + au_lcnt_dec(&aopen->br->br_nfiles);
  18922. +
  18923. +out_unpin:
  18924. + au_unpin(&a->pin);
  18925. + dput(wh_dentry);
  18926. +out_parent:
  18927. + if (!try_aopen)
  18928. + di_write_unlock(parent);
  18929. +out_unlock:
  18930. + if (unlikely(err)) {
  18931. + au_update_dbtop(dentry);
  18932. + d_drop(dentry);
  18933. + }
  18934. + if (!try_aopen)
  18935. + aufs_read_unlock(dentry, AuLock_DW);
  18936. +out_free:
  18937. + au_kfree_rcu(a);
  18938. +out:
  18939. + return err;
  18940. +}
  18941. +
  18942. +int aufs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  18943. + dev_t dev)
  18944. +{
  18945. + struct simple_arg arg = {
  18946. + .type = Mknod,
  18947. + .u.m = {
  18948. + .mode = mode,
  18949. + .dev = dev
  18950. + }
  18951. + };
  18952. + return add_simple(dir, dentry, &arg);
  18953. +}
  18954. +
  18955. +int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
  18956. +{
  18957. + struct simple_arg arg = {
  18958. + .type = Symlink,
  18959. + .u.s.symname = symname
  18960. + };
  18961. + return add_simple(dir, dentry, &arg);
  18962. +}
  18963. +
  18964. +int aufs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  18965. + bool want_excl)
  18966. +{
  18967. + struct simple_arg arg = {
  18968. + .type = Creat,
  18969. + .u.c = {
  18970. + .mode = mode,
  18971. + .want_excl = want_excl
  18972. + }
  18973. + };
  18974. + return add_simple(dir, dentry, &arg);
  18975. +}
  18976. +
  18977. +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
  18978. + struct vfsub_aopen_args *aopen_args)
  18979. +{
  18980. + struct simple_arg arg = {
  18981. + .type = Creat,
  18982. + .u.c = {
  18983. + .mode = aopen_args->create_mode,
  18984. + .want_excl = aopen_args->open_flag & O_EXCL,
  18985. + .try_aopen = true,
  18986. + .aopen = aopen_args
  18987. + }
  18988. + };
  18989. + return add_simple(dir, dentry, &arg);
  18990. +}
  18991. +
  18992. +int aufs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
  18993. +{
  18994. + int err;
  18995. + aufs_bindex_t bindex;
  18996. + struct super_block *sb;
  18997. + struct dentry *parent, *h_parent, *h_dentry;
  18998. + struct inode *h_dir, *inode;
  18999. + struct vfsmount *h_mnt;
  19000. + struct au_wr_dir_args wr_dir_args = {
  19001. + .force_btgt = -1,
  19002. + .flags = AuWrDir_TMPFILE
  19003. + };
  19004. +
  19005. + /* copy-up may happen */
  19006. + inode_lock(dir);
  19007. +
  19008. + sb = dir->i_sb;
  19009. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  19010. + if (unlikely(err))
  19011. + goto out;
  19012. +
  19013. + err = au_di_init(dentry);
  19014. + if (unlikely(err))
  19015. + goto out_si;
  19016. +
  19017. + err = -EBUSY;
  19018. + parent = d_find_any_alias(dir);
  19019. + AuDebugOn(!parent);
  19020. + di_write_lock_parent(parent);
  19021. + if (unlikely(d_inode(parent) != dir))
  19022. + goto out_parent;
  19023. +
  19024. + err = au_digen_test(parent, au_sigen(sb));
  19025. + if (unlikely(err))
  19026. + goto out_parent;
  19027. +
  19028. + bindex = au_dbtop(parent);
  19029. + au_set_dbtop(dentry, bindex);
  19030. + au_set_dbbot(dentry, bindex);
  19031. + err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
  19032. + bindex = err;
  19033. + if (unlikely(err < 0))
  19034. + goto out_parent;
  19035. +
  19036. + err = -EOPNOTSUPP;
  19037. + h_dir = au_h_iptr(dir, bindex);
  19038. + if (unlikely(!h_dir->i_op->tmpfile))
  19039. + goto out_parent;
  19040. +
  19041. + h_mnt = au_sbr_mnt(sb, bindex);
  19042. + err = vfsub_mnt_want_write(h_mnt);
  19043. + if (unlikely(err))
  19044. + goto out_parent;
  19045. +
  19046. + h_parent = au_h_dptr(parent, bindex);
  19047. + h_dentry = vfs_tmpfile(h_parent, mode, /*open_flag*/0);
  19048. + if (IS_ERR(h_dentry)) {
  19049. + err = PTR_ERR(h_dentry);
  19050. + goto out_mnt;
  19051. + }
  19052. +
  19053. + au_set_dbtop(dentry, bindex);
  19054. + au_set_dbbot(dentry, bindex);
  19055. + au_set_h_dptr(dentry, bindex, dget(h_dentry));
  19056. + inode = au_new_inode(dentry, /*must_new*/1);
  19057. + if (IS_ERR(inode)) {
  19058. + err = PTR_ERR(inode);
  19059. + au_set_h_dptr(dentry, bindex, NULL);
  19060. + au_set_dbtop(dentry, -1);
  19061. + au_set_dbbot(dentry, -1);
  19062. + } else {
  19063. + if (!inode->i_nlink)
  19064. + set_nlink(inode, 1);
  19065. + d_tmpfile(dentry, inode);
  19066. + au_di(dentry)->di_tmpfile = 1;
  19067. +
  19068. + /* update without i_mutex */
  19069. + if (au_ibtop(dir) == au_dbtop(dentry))
  19070. + au_cpup_attr_timesizes(dir);
  19071. + }
  19072. + dput(h_dentry);
  19073. +
  19074. +out_mnt:
  19075. + vfsub_mnt_drop_write(h_mnt);
  19076. +out_parent:
  19077. + di_write_unlock(parent);
  19078. + dput(parent);
  19079. + di_write_unlock(dentry);
  19080. + if (unlikely(err)) {
  19081. + au_di_fin(dentry);
  19082. + dentry->d_fsdata = NULL;
  19083. + }
  19084. +out_si:
  19085. + si_read_unlock(sb);
  19086. +out:
  19087. + inode_unlock(dir);
  19088. + return err;
  19089. +}
  19090. +
  19091. +/* ---------------------------------------------------------------------- */
  19092. +
  19093. +struct au_link_args {
  19094. + aufs_bindex_t bdst, bsrc;
  19095. + struct au_pin pin;
  19096. + struct path h_path;
  19097. + struct dentry *src_parent, *parent;
  19098. +};
  19099. +
  19100. +static int au_cpup_before_link(struct dentry *src_dentry,
  19101. + struct au_link_args *a)
  19102. +{
  19103. + int err;
  19104. + struct dentry *h_src_dentry;
  19105. + struct au_cp_generic cpg = {
  19106. + .dentry = src_dentry,
  19107. + .bdst = a->bdst,
  19108. + .bsrc = a->bsrc,
  19109. + .len = -1,
  19110. + .pin = &a->pin,
  19111. + .flags = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
  19112. + };
  19113. +
  19114. + di_read_lock_parent(a->src_parent, AuLock_IR);
  19115. + err = au_test_and_cpup_dirs(src_dentry, a->bdst);
  19116. + if (unlikely(err))
  19117. + goto out;
  19118. +
  19119. + h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
  19120. + err = au_pin(&a->pin, src_dentry, a->bdst,
  19121. + au_opt_udba(src_dentry->d_sb),
  19122. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  19123. + if (unlikely(err))
  19124. + goto out;
  19125. +
  19126. + err = au_sio_cpup_simple(&cpg);
  19127. + au_unpin(&a->pin);
  19128. +
  19129. +out:
  19130. + di_read_unlock(a->src_parent, AuLock_IR);
  19131. + return err;
  19132. +}
  19133. +
  19134. +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
  19135. + struct au_link_args *a)
  19136. +{
  19137. + int err;
  19138. + unsigned char plink;
  19139. + aufs_bindex_t bbot;
  19140. + struct dentry *h_src_dentry;
  19141. + struct inode *h_inode, *inode, *delegated;
  19142. + struct super_block *sb;
  19143. + struct file *h_file;
  19144. +
  19145. + plink = 0;
  19146. + h_inode = NULL;
  19147. + sb = src_dentry->d_sb;
  19148. + inode = d_inode(src_dentry);
  19149. + if (au_ibtop(inode) <= a->bdst)
  19150. + h_inode = au_h_iptr(inode, a->bdst);
  19151. + if (!h_inode || !h_inode->i_nlink) {
  19152. + /* copyup src_dentry as the name of dentry. */
  19153. + bbot = au_dbbot(dentry);
  19154. + if (bbot < a->bsrc)
  19155. + au_set_dbbot(dentry, a->bsrc);
  19156. + au_set_h_dptr(dentry, a->bsrc,
  19157. + dget(au_h_dptr(src_dentry, a->bsrc)));
  19158. + dget(a->h_path.dentry);
  19159. + au_set_h_dptr(dentry, a->bdst, NULL);
  19160. + AuDbg("temporary d_inode...\n");
  19161. + spin_lock(&dentry->d_lock);
  19162. + dentry->d_inode = d_inode(src_dentry); /* tmp */
  19163. + spin_unlock(&dentry->d_lock);
  19164. + h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
  19165. + if (IS_ERR(h_file))
  19166. + err = PTR_ERR(h_file);
  19167. + else {
  19168. + struct au_cp_generic cpg = {
  19169. + .dentry = dentry,
  19170. + .bdst = a->bdst,
  19171. + .bsrc = -1,
  19172. + .len = -1,
  19173. + .pin = &a->pin,
  19174. + .flags = AuCpup_KEEPLINO
  19175. + };
  19176. + err = au_sio_cpup_simple(&cpg);
  19177. + au_h_open_post(dentry, a->bsrc, h_file);
  19178. + if (!err) {
  19179. + dput(a->h_path.dentry);
  19180. + a->h_path.dentry = au_h_dptr(dentry, a->bdst);
  19181. + } else
  19182. + au_set_h_dptr(dentry, a->bdst,
  19183. + a->h_path.dentry);
  19184. + }
  19185. + spin_lock(&dentry->d_lock);
  19186. + dentry->d_inode = NULL; /* restore */
  19187. + spin_unlock(&dentry->d_lock);
  19188. + AuDbg("temporary d_inode...done\n");
  19189. + au_set_h_dptr(dentry, a->bsrc, NULL);
  19190. + au_set_dbbot(dentry, bbot);
  19191. + } else {
  19192. + /* the inode of src_dentry already exists on a.bdst branch */
  19193. + h_src_dentry = d_find_alias(h_inode);
  19194. + if (!h_src_dentry && au_plink_test(inode)) {
  19195. + plink = 1;
  19196. + h_src_dentry = au_plink_lkup(inode, a->bdst);
  19197. + err = PTR_ERR(h_src_dentry);
  19198. + if (IS_ERR(h_src_dentry))
  19199. + goto out;
  19200. +
  19201. + if (unlikely(d_is_negative(h_src_dentry))) {
  19202. + dput(h_src_dentry);
  19203. + h_src_dentry = NULL;
  19204. + }
  19205. +
  19206. + }
  19207. + if (h_src_dentry) {
  19208. + delegated = NULL;
  19209. + err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
  19210. + &a->h_path, &delegated);
  19211. + if (unlikely(err == -EWOULDBLOCK)) {
  19212. + pr_warn("cannot retry for NFSv4 delegation"
  19213. + " for an internal link\n");
  19214. + iput(delegated);
  19215. + }
  19216. + dput(h_src_dentry);
  19217. + } else {
  19218. + AuIOErr("no dentry found for hi%lu on b%d\n",
  19219. + h_inode->i_ino, a->bdst);
  19220. + err = -EIO;
  19221. + }
  19222. + }
  19223. +
  19224. + if (!err && !plink)
  19225. + au_plink_append(inode, a->bdst, a->h_path.dentry);
  19226. +
  19227. +out:
  19228. + AuTraceErr(err);
  19229. + return err;
  19230. +}
  19231. +
  19232. +int aufs_link(struct dentry *src_dentry, struct inode *dir,
  19233. + struct dentry *dentry)
  19234. +{
  19235. + int err, rerr;
  19236. + struct au_dtime dt;
  19237. + struct au_link_args *a;
  19238. + struct dentry *wh_dentry, *h_src_dentry;
  19239. + struct inode *inode, *delegated;
  19240. + struct super_block *sb;
  19241. + struct au_wr_dir_args wr_dir_args = {
  19242. + /* .force_btgt = -1, */
  19243. + .flags = AuWrDir_ADD_ENTRY
  19244. + };
  19245. +
  19246. + IMustLock(dir);
  19247. + inode = d_inode(src_dentry);
  19248. + IMustLock(inode);
  19249. +
  19250. + err = -ENOMEM;
  19251. + a = kzalloc(sizeof(*a), GFP_NOFS);
  19252. + if (unlikely(!a))
  19253. + goto out;
  19254. +
  19255. + a->parent = dentry->d_parent; /* dir inode is locked */
  19256. + err = aufs_read_and_write_lock2(dentry, src_dentry,
  19257. + AuLock_NOPLM | AuLock_GEN);
  19258. + if (unlikely(err))
  19259. + goto out_kfree;
  19260. + err = au_d_linkable(src_dentry);
  19261. + if (unlikely(err))
  19262. + goto out_unlock;
  19263. + err = au_d_may_add(dentry);
  19264. + if (unlikely(err))
  19265. + goto out_unlock;
  19266. +
  19267. + a->src_parent = dget_parent(src_dentry);
  19268. + wr_dir_args.force_btgt = au_ibtop(inode);
  19269. +
  19270. + di_write_lock_parent(a->parent);
  19271. + wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
  19272. + wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
  19273. + &wr_dir_args);
  19274. + err = PTR_ERR(wh_dentry);
  19275. + if (IS_ERR(wh_dentry))
  19276. + goto out_parent;
  19277. +
  19278. + err = 0;
  19279. + sb = dentry->d_sb;
  19280. + a->bdst = au_dbtop(dentry);
  19281. + a->h_path.dentry = au_h_dptr(dentry, a->bdst);
  19282. + a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
  19283. + a->bsrc = au_ibtop(inode);
  19284. + h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
  19285. + if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
  19286. + h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
  19287. + if (!h_src_dentry) {
  19288. + a->bsrc = au_dbtop(src_dentry);
  19289. + h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
  19290. + AuDebugOn(!h_src_dentry);
  19291. + } else if (IS_ERR(h_src_dentry)) {
  19292. + err = PTR_ERR(h_src_dentry);
  19293. + goto out_parent;
  19294. + }
  19295. +
  19296. + /*
  19297. + * aufs doesn't touch the credential so
  19298. + * security_dentry_create_files_as() is unnecessary.
  19299. + */
  19300. + if (au_opt_test(au_mntflags(sb), PLINK)) {
  19301. + if (a->bdst < a->bsrc
  19302. + /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
  19303. + err = au_cpup_or_link(src_dentry, dentry, a);
  19304. + else {
  19305. + delegated = NULL;
  19306. + err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
  19307. + &a->h_path, &delegated);
  19308. + if (unlikely(err == -EWOULDBLOCK)) {
  19309. + pr_warn("cannot retry for NFSv4 delegation"
  19310. + " for an internal link\n");
  19311. + iput(delegated);
  19312. + }
  19313. + }
  19314. + dput(h_src_dentry);
  19315. + } else {
  19316. + /*
  19317. + * copyup src_dentry to the branch we process,
  19318. + * and then link(2) to it.
  19319. + */
  19320. + dput(h_src_dentry);
  19321. + if (a->bdst < a->bsrc
  19322. + /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
  19323. + au_unpin(&a->pin);
  19324. + di_write_unlock(a->parent);
  19325. + err = au_cpup_before_link(src_dentry, a);
  19326. + di_write_lock_parent(a->parent);
  19327. + if (!err)
  19328. + err = au_pin(&a->pin, dentry, a->bdst,
  19329. + au_opt_udba(sb),
  19330. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  19331. + if (unlikely(err))
  19332. + goto out_wh;
  19333. + }
  19334. + if (!err) {
  19335. + h_src_dentry = au_h_dptr(src_dentry, a->bdst);
  19336. + err = -ENOENT;
  19337. + if (h_src_dentry && d_is_positive(h_src_dentry)) {
  19338. + delegated = NULL;
  19339. + err = vfsub_link(h_src_dentry,
  19340. + au_pinned_h_dir(&a->pin),
  19341. + &a->h_path, &delegated);
  19342. + if (unlikely(err == -EWOULDBLOCK)) {
  19343. + pr_warn("cannot retry"
  19344. + " for NFSv4 delegation"
  19345. + " for an internal link\n");
  19346. + iput(delegated);
  19347. + }
  19348. + }
  19349. + }
  19350. + }
  19351. + if (unlikely(err))
  19352. + goto out_unpin;
  19353. +
  19354. + if (wh_dentry) {
  19355. + a->h_path.dentry = wh_dentry;
  19356. + err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
  19357. + dentry);
  19358. + if (unlikely(err))
  19359. + goto out_revert;
  19360. + }
  19361. +
  19362. + au_dir_ts(dir, a->bdst);
  19363. + inode_inc_iversion(dir);
  19364. + inc_nlink(inode);
  19365. + inode->i_ctime = dir->i_ctime;
  19366. + d_instantiate(dentry, au_igrab(inode));
  19367. + if (d_unhashed(a->h_path.dentry))
  19368. + /* some filesystem calls d_drop() */
  19369. + d_drop(dentry);
  19370. + /* some filesystems consume an inode even hardlink */
  19371. + au_fhsm_wrote(sb, a->bdst, /*force*/0);
  19372. + goto out_unpin; /* success */
  19373. +
  19374. +out_revert:
  19375. + /* no delegation since it is just created */
  19376. + rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
  19377. + /*delegated*/NULL, /*force*/0);
  19378. + if (unlikely(rerr)) {
  19379. + AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
  19380. + err = -EIO;
  19381. + }
  19382. + au_dtime_revert(&dt);
  19383. +out_unpin:
  19384. + au_unpin(&a->pin);
  19385. +out_wh:
  19386. + dput(wh_dentry);
  19387. +out_parent:
  19388. + di_write_unlock(a->parent);
  19389. + dput(a->src_parent);
  19390. +out_unlock:
  19391. + if (unlikely(err)) {
  19392. + au_update_dbtop(dentry);
  19393. + d_drop(dentry);
  19394. + }
  19395. + aufs_read_and_write_unlock2(dentry, src_dentry);
  19396. +out_kfree:
  19397. + au_kfree_rcu(a);
  19398. +out:
  19399. + AuTraceErr(err);
  19400. + return err;
  19401. +}
  19402. +
  19403. +int aufs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  19404. +{
  19405. + int err, rerr;
  19406. + aufs_bindex_t bindex;
  19407. + unsigned char diropq;
  19408. + struct path h_path;
  19409. + struct dentry *wh_dentry, *parent, *opq_dentry;
  19410. + struct inode *h_inode;
  19411. + struct super_block *sb;
  19412. + struct {
  19413. + struct au_pin pin;
  19414. + struct au_dtime dt;
  19415. + } *a; /* reduce the stack usage */
  19416. + struct au_wr_dir_args wr_dir_args = {
  19417. + .force_btgt = -1,
  19418. + .flags = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
  19419. + };
  19420. +
  19421. + IMustLock(dir);
  19422. +
  19423. + err = -ENOMEM;
  19424. + a = kmalloc(sizeof(*a), GFP_NOFS);
  19425. + if (unlikely(!a))
  19426. + goto out;
  19427. +
  19428. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
  19429. + if (unlikely(err))
  19430. + goto out_free;
  19431. + err = au_d_may_add(dentry);
  19432. + if (unlikely(err))
  19433. + goto out_unlock;
  19434. +
  19435. + parent = dentry->d_parent; /* dir inode is locked */
  19436. + di_write_lock_parent(parent);
  19437. + wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
  19438. + &a->pin, &wr_dir_args);
  19439. + err = PTR_ERR(wh_dentry);
  19440. + if (IS_ERR(wh_dentry))
  19441. + goto out_parent;
  19442. +
  19443. + sb = dentry->d_sb;
  19444. + bindex = au_dbtop(dentry);
  19445. + h_path.dentry = au_h_dptr(dentry, bindex);
  19446. + h_path.mnt = au_sbr_mnt(sb, bindex);
  19447. + err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
  19448. + if (unlikely(err))
  19449. + goto out_unpin;
  19450. +
  19451. + /* make the dir opaque */
  19452. + diropq = 0;
  19453. + h_inode = d_inode(h_path.dentry);
  19454. + if (wh_dentry
  19455. + || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
  19456. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  19457. + opq_dentry = au_diropq_create(dentry, bindex);
  19458. + inode_unlock(h_inode);
  19459. + err = PTR_ERR(opq_dentry);
  19460. + if (IS_ERR(opq_dentry))
  19461. + goto out_dir;
  19462. + dput(opq_dentry);
  19463. + diropq = 1;
  19464. + }
  19465. +
  19466. + err = epilog(dir, bindex, wh_dentry, dentry);
  19467. + if (!err) {
  19468. + inc_nlink(dir);
  19469. + goto out_unpin; /* success */
  19470. + }
  19471. +
  19472. + /* revert */
  19473. + if (diropq) {
  19474. + AuLabel(revert opq);
  19475. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  19476. + rerr = au_diropq_remove(dentry, bindex);
  19477. + inode_unlock(h_inode);
  19478. + if (rerr) {
  19479. + AuIOErr("%pd reverting diropq failed(%d, %d)\n",
  19480. + dentry, err, rerr);
  19481. + err = -EIO;
  19482. + }
  19483. + }
  19484. +
  19485. +out_dir:
  19486. + AuLabel(revert dir);
  19487. + rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
  19488. + if (rerr) {
  19489. + AuIOErr("%pd reverting dir failed(%d, %d)\n",
  19490. + dentry, err, rerr);
  19491. + err = -EIO;
  19492. + }
  19493. + au_dtime_revert(&a->dt);
  19494. +out_unpin:
  19495. + au_unpin(&a->pin);
  19496. + dput(wh_dentry);
  19497. +out_parent:
  19498. + di_write_unlock(parent);
  19499. +out_unlock:
  19500. + if (unlikely(err)) {
  19501. + au_update_dbtop(dentry);
  19502. + d_drop(dentry);
  19503. + }
  19504. + aufs_read_unlock(dentry, AuLock_DW);
  19505. +out_free:
  19506. + au_kfree_rcu(a);
  19507. +out:
  19508. + return err;
  19509. +}
  19510. diff -Naur null/fs/aufs/i_op.c linux-5.1/fs/aufs/i_op.c
  19511. --- /dev/null
  19512. +++ linux-5.1/fs/aufs/i_op.c 2019-05-22 11:23:07.440141042 +0200
  19513. @@ -0,0 +1,1507 @@
  19514. +// SPDX-License-Identifier: GPL-2.0
  19515. +/*
  19516. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  19517. + *
  19518. + * This program, aufs is free software; you can redistribute it and/or modify
  19519. + * it under the terms of the GNU General Public License as published by
  19520. + * the Free Software Foundation; either version 2 of the License, or
  19521. + * (at your option) any later version.
  19522. + *
  19523. + * This program is distributed in the hope that it will be useful,
  19524. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19525. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19526. + * GNU General Public License for more details.
  19527. + *
  19528. + * You should have received a copy of the GNU General Public License
  19529. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19530. + */
  19531. +
  19532. +/*
  19533. + * inode operations (except add/del/rename)
  19534. + */
  19535. +
  19536. +#include <linux/device_cgroup.h>
  19537. +#include <linux/fs_stack.h>
  19538. +#include <linux/iversion.h>
  19539. +#include <linux/namei.h>
  19540. +#include <linux/security.h>
  19541. +#include "aufs.h"
  19542. +
  19543. +static int h_permission(struct inode *h_inode, int mask,
  19544. + struct path *h_path, int brperm)
  19545. +{
  19546. + int err;
  19547. + const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
  19548. +
  19549. + err = -EPERM;
  19550. + if (write_mask && IS_IMMUTABLE(h_inode))
  19551. + goto out;
  19552. +
  19553. + err = -EACCES;
  19554. + if (((mask & MAY_EXEC)
  19555. + && S_ISREG(h_inode->i_mode)
  19556. + && (path_noexec(h_path)
  19557. + || !(h_inode->i_mode & 0111))))
  19558. + goto out;
  19559. +
  19560. + /*
  19561. + * - skip the lower fs test in the case of write to ro branch.
  19562. + * - nfs dir permission write check is optimized, but a policy for
  19563. + * link/rename requires a real check.
  19564. + * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
  19565. + * in this case, generic_permission() returns -EOPNOTSUPP.
  19566. + */
  19567. + if ((write_mask && !au_br_writable(brperm))
  19568. + || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
  19569. + && write_mask && !(mask & MAY_READ))
  19570. + || !h_inode->i_op->permission) {
  19571. + /* AuLabel(generic_permission); */
  19572. + /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
  19573. + err = generic_permission(h_inode, mask);
  19574. + if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
  19575. + err = h_inode->i_op->permission(h_inode, mask);
  19576. + AuTraceErr(err);
  19577. + } else {
  19578. + /* AuLabel(h_inode->permission); */
  19579. + err = h_inode->i_op->permission(h_inode, mask);
  19580. + AuTraceErr(err);
  19581. + }
  19582. +
  19583. + if (!err)
  19584. + err = devcgroup_inode_permission(h_inode, mask);
  19585. + if (!err)
  19586. + err = security_inode_permission(h_inode, mask);
  19587. +
  19588. +#if 0
  19589. + if (!err) {
  19590. + /* todo: do we need to call ima_path_check()? */
  19591. + struct path h_path = {
  19592. + .dentry =
  19593. + .mnt = h_mnt
  19594. + };
  19595. + err = ima_path_check(&h_path,
  19596. + mask & (MAY_READ | MAY_WRITE | MAY_EXEC),
  19597. + IMA_COUNT_LEAVE);
  19598. + }
  19599. +#endif
  19600. +
  19601. +out:
  19602. + return err;
  19603. +}
  19604. +
  19605. +static int aufs_permission(struct inode *inode, int mask)
  19606. +{
  19607. + int err;
  19608. + aufs_bindex_t bindex, bbot;
  19609. + const unsigned char isdir = !!S_ISDIR(inode->i_mode),
  19610. + write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
  19611. + struct inode *h_inode;
  19612. + struct super_block *sb;
  19613. + struct au_branch *br;
  19614. +
  19615. + /* todo: support rcu-walk? */
  19616. + if (mask & MAY_NOT_BLOCK)
  19617. + return -ECHILD;
  19618. +
  19619. + sb = inode->i_sb;
  19620. + si_read_lock(sb, AuLock_FLUSH);
  19621. + ii_read_lock_child(inode);
  19622. +#if 0
  19623. + err = au_iigen_test(inode, au_sigen(sb));
  19624. + if (unlikely(err))
  19625. + goto out;
  19626. +#endif
  19627. +
  19628. + if (!isdir
  19629. + || write_mask
  19630. + || au_opt_test(au_mntflags(sb), DIRPERM1)) {
  19631. + err = au_busy_or_stale();
  19632. + h_inode = au_h_iptr(inode, au_ibtop(inode));
  19633. + if (unlikely(!h_inode
  19634. + || (h_inode->i_mode & S_IFMT)
  19635. + != (inode->i_mode & S_IFMT)))
  19636. + goto out;
  19637. +
  19638. + err = 0;
  19639. + bindex = au_ibtop(inode);
  19640. + br = au_sbr(sb, bindex);
  19641. + err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
  19642. + if (write_mask
  19643. + && !err
  19644. + && !special_file(h_inode->i_mode)) {
  19645. + /* test whether the upper writable branch exists */
  19646. + err = -EROFS;
  19647. + for (; bindex >= 0; bindex--)
  19648. + if (!au_br_rdonly(au_sbr(sb, bindex))) {
  19649. + err = 0;
  19650. + break;
  19651. + }
  19652. + }
  19653. + goto out;
  19654. + }
  19655. +
  19656. + /* non-write to dir */
  19657. + err = 0;
  19658. + bbot = au_ibbot(inode);
  19659. + for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
  19660. + h_inode = au_h_iptr(inode, bindex);
  19661. + if (h_inode) {
  19662. + err = au_busy_or_stale();
  19663. + if (unlikely(!S_ISDIR(h_inode->i_mode)))
  19664. + break;
  19665. +
  19666. + br = au_sbr(sb, bindex);
  19667. + err = h_permission(h_inode, mask, &br->br_path,
  19668. + br->br_perm);
  19669. + }
  19670. + }
  19671. +
  19672. +out:
  19673. + ii_read_unlock(inode);
  19674. + si_read_unlock(sb);
  19675. + return err;
  19676. +}
  19677. +
  19678. +/* ---------------------------------------------------------------------- */
  19679. +
  19680. +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
  19681. + unsigned int flags)
  19682. +{
  19683. + struct dentry *ret, *parent;
  19684. + struct inode *inode;
  19685. + struct super_block *sb;
  19686. + int err, npositive;
  19687. +
  19688. + IMustLock(dir);
  19689. +
  19690. + /* todo: support rcu-walk? */
  19691. + ret = ERR_PTR(-ECHILD);
  19692. + if (flags & LOOKUP_RCU)
  19693. + goto out;
  19694. +
  19695. + ret = ERR_PTR(-ENAMETOOLONG);
  19696. + if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
  19697. + goto out;
  19698. +
  19699. + sb = dir->i_sb;
  19700. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  19701. + ret = ERR_PTR(err);
  19702. + if (unlikely(err))
  19703. + goto out;
  19704. +
  19705. + err = au_di_init(dentry);
  19706. + ret = ERR_PTR(err);
  19707. + if (unlikely(err))
  19708. + goto out_si;
  19709. +
  19710. + inode = NULL;
  19711. + npositive = 0; /* suppress a warning */
  19712. + parent = dentry->d_parent; /* dir inode is locked */
  19713. + di_read_lock_parent(parent, AuLock_IR);
  19714. + err = au_alive_dir(parent);
  19715. + if (!err)
  19716. + err = au_digen_test(parent, au_sigen(sb));
  19717. + if (!err) {
  19718. + /* regardless LOOKUP_CREATE, always ALLOW_NEG */
  19719. + npositive = au_lkup_dentry(dentry, au_dbtop(parent),
  19720. + AuLkup_ALLOW_NEG);
  19721. + err = npositive;
  19722. + }
  19723. + di_read_unlock(parent, AuLock_IR);
  19724. + ret = ERR_PTR(err);
  19725. + if (unlikely(err < 0))
  19726. + goto out_unlock;
  19727. +
  19728. + if (npositive) {
  19729. + inode = au_new_inode(dentry, /*must_new*/0);
  19730. + if (IS_ERR(inode)) {
  19731. + ret = (void *)inode;
  19732. + inode = NULL;
  19733. + goto out_unlock;
  19734. + }
  19735. + }
  19736. +
  19737. + if (inode)
  19738. + atomic_inc(&inode->i_count);
  19739. + ret = d_splice_alias(inode, dentry);
  19740. +#if 0
  19741. + if (unlikely(d_need_lookup(dentry))) {
  19742. + spin_lock(&dentry->d_lock);
  19743. + dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
  19744. + spin_unlock(&dentry->d_lock);
  19745. + } else
  19746. +#endif
  19747. + if (inode) {
  19748. + if (!IS_ERR(ret)) {
  19749. + iput(inode);
  19750. + if (ret && ret != dentry)
  19751. + ii_write_unlock(inode);
  19752. + } else {
  19753. + ii_write_unlock(inode);
  19754. + iput(inode);
  19755. + inode = NULL;
  19756. + }
  19757. + }
  19758. +
  19759. +out_unlock:
  19760. + di_write_unlock(dentry);
  19761. +out_si:
  19762. + si_read_unlock(sb);
  19763. +out:
  19764. + return ret;
  19765. +}
  19766. +
  19767. +/* ---------------------------------------------------------------------- */
  19768. +
  19769. +/*
  19770. + * very dirty and complicated aufs ->atomic_open().
  19771. + * aufs_atomic_open()
  19772. + * + au_aopen_or_create()
  19773. + * + add_simple()
  19774. + * + vfsub_atomic_open()
  19775. + * + branch fs ->atomic_open()
  19776. + * may call the actual 'open' for h_file
  19777. + * + inc br_nfiles only if opened
  19778. + * + au_aopen_no_open() or au_aopen_do_open()
  19779. + *
  19780. + * au_aopen_do_open()
  19781. + * + finish_open()
  19782. + * + au_do_aopen()
  19783. + * + au_do_open() the body of all 'open'
  19784. + * + au_do_open_nondir()
  19785. + * set the passed h_file
  19786. + *
  19787. + * au_aopen_no_open()
  19788. + * + finish_no_open()
  19789. + */
  19790. +
  19791. +struct aopen_node {
  19792. + struct hlist_bl_node hblist;
  19793. + struct file *file, *h_file;
  19794. +};
  19795. +
  19796. +static int au_do_aopen(struct inode *inode, struct file *file)
  19797. +{
  19798. + struct hlist_bl_head *aopen;
  19799. + struct hlist_bl_node *pos;
  19800. + struct aopen_node *node;
  19801. + struct au_do_open_args args = {
  19802. + .aopen = 1,
  19803. + .open = au_do_open_nondir
  19804. + };
  19805. +
  19806. + aopen = &au_sbi(inode->i_sb)->si_aopen;
  19807. + hlist_bl_lock(aopen);
  19808. + hlist_bl_for_each_entry(node, pos, aopen, hblist)
  19809. + if (node->file == file) {
  19810. + args.h_file = node->h_file;
  19811. + break;
  19812. + }
  19813. + hlist_bl_unlock(aopen);
  19814. + /* AuDebugOn(!args.h_file); */
  19815. +
  19816. + return au_do_open(file, &args);
  19817. +}
  19818. +
  19819. +static int au_aopen_do_open(struct file *file, struct dentry *dentry,
  19820. + struct aopen_node *aopen_node)
  19821. +{
  19822. + int err;
  19823. + struct hlist_bl_head *aopen;
  19824. +
  19825. + AuLabel(here);
  19826. + aopen = &au_sbi(dentry->d_sb)->si_aopen;
  19827. + au_hbl_add(&aopen_node->hblist, aopen);
  19828. + err = finish_open(file, dentry, au_do_aopen);
  19829. + au_hbl_del(&aopen_node->hblist, aopen);
  19830. + /* AuDbgFile(file); */
  19831. + AuDbg("%pd%s%s\n", dentry,
  19832. + (file->f_mode & FMODE_CREATED) ? " created" : "",
  19833. + (file->f_mode & FMODE_OPENED) ? " opened" : "");
  19834. +
  19835. + AuTraceErr(err);
  19836. + return err;
  19837. +}
  19838. +
  19839. +static int au_aopen_no_open(struct file *file, struct dentry *dentry)
  19840. +{
  19841. + int err;
  19842. +
  19843. + AuLabel(here);
  19844. + dget(dentry);
  19845. + err = finish_no_open(file, dentry);
  19846. +
  19847. + AuTraceErr(err);
  19848. + return err;
  19849. +}
  19850. +
  19851. +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
  19852. + struct file *file, unsigned int open_flag,
  19853. + umode_t create_mode)
  19854. +{
  19855. + int err, did_open;
  19856. + unsigned int lkup_flags;
  19857. + aufs_bindex_t bindex;
  19858. + struct super_block *sb;
  19859. + struct dentry *parent, *d;
  19860. + struct vfsub_aopen_args args = {
  19861. + .open_flag = open_flag,
  19862. + .create_mode = create_mode
  19863. + };
  19864. + struct aopen_node aopen_node = {
  19865. + .file = file
  19866. + };
  19867. +
  19868. + IMustLock(dir);
  19869. + AuDbg("open_flag 0%o\n", open_flag);
  19870. + AuDbgDentry(dentry);
  19871. +
  19872. + err = 0;
  19873. + if (!au_di(dentry)) {
  19874. + lkup_flags = LOOKUP_OPEN;
  19875. + if (open_flag & O_CREAT)
  19876. + lkup_flags |= LOOKUP_CREATE;
  19877. + d = aufs_lookup(dir, dentry, lkup_flags);
  19878. + if (IS_ERR(d)) {
  19879. + err = PTR_ERR(d);
  19880. + AuTraceErr(err);
  19881. + goto out;
  19882. + } else if (d) {
  19883. + /*
  19884. + * obsoleted dentry found.
  19885. + * another error will be returned later.
  19886. + */
  19887. + d_drop(d);
  19888. + AuDbgDentry(d);
  19889. + dput(d);
  19890. + }
  19891. + AuDbgDentry(dentry);
  19892. + }
  19893. +
  19894. + if (d_is_positive(dentry)
  19895. + || d_unhashed(dentry)
  19896. + || d_unlinked(dentry)
  19897. + || !(open_flag & O_CREAT)) {
  19898. + err = au_aopen_no_open(file, dentry);
  19899. + goto out; /* success */
  19900. + }
  19901. +
  19902. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
  19903. + if (unlikely(err))
  19904. + goto out;
  19905. +
  19906. + sb = dentry->d_sb;
  19907. + parent = dentry->d_parent; /* dir is locked */
  19908. + di_write_lock_parent(parent);
  19909. + err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
  19910. + if (unlikely(err < 0))
  19911. + goto out_parent;
  19912. +
  19913. + AuDbgDentry(dentry);
  19914. + if (d_is_positive(dentry)) {
  19915. + err = au_aopen_no_open(file, dentry);
  19916. + goto out_parent; /* success */
  19917. + }
  19918. +
  19919. + args.file = alloc_empty_file(file->f_flags, current_cred());
  19920. + err = PTR_ERR(args.file);
  19921. + if (IS_ERR(args.file))
  19922. + goto out_parent;
  19923. +
  19924. + bindex = au_dbtop(dentry);
  19925. + err = au_aopen_or_create(dir, dentry, &args);
  19926. + AuTraceErr(err);
  19927. + AuDbgFile(args.file);
  19928. + file->f_mode = args.file->f_mode & ~FMODE_OPENED;
  19929. + did_open = !!(args.file->f_mode & FMODE_OPENED);
  19930. + if (!did_open) {
  19931. + fput(args.file);
  19932. + args.file = NULL;
  19933. + }
  19934. + di_write_unlock(parent);
  19935. + di_write_unlock(dentry);
  19936. + if (unlikely(err < 0)) {
  19937. + if (args.file)
  19938. + fput(args.file);
  19939. + goto out_sb;
  19940. + }
  19941. +
  19942. + if (!did_open)
  19943. + err = au_aopen_no_open(file, dentry);
  19944. + else {
  19945. + aopen_node.h_file = args.file;
  19946. + err = au_aopen_do_open(file, dentry, &aopen_node);
  19947. + }
  19948. + if (unlikely(err < 0)) {
  19949. + if (args.file)
  19950. + fput(args.file);
  19951. + if (did_open)
  19952. + au_lcnt_dec(&args.br->br_nfiles);
  19953. + }
  19954. + goto out_sb; /* success */
  19955. +
  19956. +out_parent:
  19957. + di_write_unlock(parent);
  19958. + di_write_unlock(dentry);
  19959. +out_sb:
  19960. + si_read_unlock(sb);
  19961. +out:
  19962. + AuTraceErr(err);
  19963. + AuDbgFile(file);
  19964. + return err;
  19965. +}
  19966. +
  19967. +
  19968. +/* ---------------------------------------------------------------------- */
  19969. +
  19970. +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
  19971. + const unsigned char add_entry, aufs_bindex_t bcpup,
  19972. + aufs_bindex_t btop)
  19973. +{
  19974. + int err;
  19975. + struct dentry *h_parent;
  19976. + struct inode *h_dir;
  19977. +
  19978. + if (add_entry)
  19979. + IMustLock(d_inode(parent));
  19980. + else
  19981. + di_write_lock_parent(parent);
  19982. +
  19983. + err = 0;
  19984. + if (!au_h_dptr(parent, bcpup)) {
  19985. + if (btop > bcpup)
  19986. + err = au_cpup_dirs(dentry, bcpup);
  19987. + else if (btop < bcpup)
  19988. + err = au_cpdown_dirs(dentry, bcpup);
  19989. + else
  19990. + BUG();
  19991. + }
  19992. + if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
  19993. + h_parent = au_h_dptr(parent, bcpup);
  19994. + h_dir = d_inode(h_parent);
  19995. + inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
  19996. + err = au_lkup_neg(dentry, bcpup, /*wh*/0);
  19997. + /* todo: no unlock here */
  19998. + inode_unlock_shared(h_dir);
  19999. +
  20000. + AuDbg("bcpup %d\n", bcpup);
  20001. + if (!err) {
  20002. + if (d_really_is_negative(dentry))
  20003. + au_set_h_dptr(dentry, btop, NULL);
  20004. + au_update_dbrange(dentry, /*do_put_zero*/0);
  20005. + }
  20006. + }
  20007. +
  20008. + if (!add_entry)
  20009. + di_write_unlock(parent);
  20010. + if (!err)
  20011. + err = bcpup; /* success */
  20012. +
  20013. + AuTraceErr(err);
  20014. + return err;
  20015. +}
  20016. +
  20017. +/*
  20018. + * decide the branch and the parent dir where we will create a new entry.
  20019. + * returns new bindex or an error.
  20020. + * copyup the parent dir if needed.
  20021. + */
  20022. +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
  20023. + struct au_wr_dir_args *args)
  20024. +{
  20025. + int err;
  20026. + unsigned int flags;
  20027. + aufs_bindex_t bcpup, btop, src_btop;
  20028. + const unsigned char add_entry
  20029. + = au_ftest_wrdir(args->flags, ADD_ENTRY)
  20030. + | au_ftest_wrdir(args->flags, TMPFILE);
  20031. + struct super_block *sb;
  20032. + struct dentry *parent;
  20033. + struct au_sbinfo *sbinfo;
  20034. +
  20035. + sb = dentry->d_sb;
  20036. + sbinfo = au_sbi(sb);
  20037. + parent = dget_parent(dentry);
  20038. + btop = au_dbtop(dentry);
  20039. + bcpup = btop;
  20040. + if (args->force_btgt < 0) {
  20041. + if (src_dentry) {
  20042. + src_btop = au_dbtop(src_dentry);
  20043. + if (src_btop < btop)
  20044. + bcpup = src_btop;
  20045. + } else if (add_entry) {
  20046. + flags = 0;
  20047. + if (au_ftest_wrdir(args->flags, ISDIR))
  20048. + au_fset_wbr(flags, DIR);
  20049. + err = AuWbrCreate(sbinfo, dentry, flags);
  20050. + bcpup = err;
  20051. + }
  20052. +
  20053. + if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
  20054. + if (add_entry)
  20055. + err = AuWbrCopyup(sbinfo, dentry);
  20056. + else {
  20057. + if (!IS_ROOT(dentry)) {
  20058. + di_read_lock_parent(parent, !AuLock_IR);
  20059. + err = AuWbrCopyup(sbinfo, dentry);
  20060. + di_read_unlock(parent, !AuLock_IR);
  20061. + } else
  20062. + err = AuWbrCopyup(sbinfo, dentry);
  20063. + }
  20064. + bcpup = err;
  20065. + if (unlikely(err < 0))
  20066. + goto out;
  20067. + }
  20068. + } else {
  20069. + bcpup = args->force_btgt;
  20070. + AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
  20071. + }
  20072. +
  20073. + AuDbg("btop %d, bcpup %d\n", btop, bcpup);
  20074. + err = bcpup;
  20075. + if (bcpup == btop)
  20076. + goto out; /* success */
  20077. +
  20078. + /* copyup the new parent into the branch we process */
  20079. + err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
  20080. + if (err >= 0) {
  20081. + if (d_really_is_negative(dentry)) {
  20082. + au_set_h_dptr(dentry, btop, NULL);
  20083. + au_set_dbtop(dentry, bcpup);
  20084. + au_set_dbbot(dentry, bcpup);
  20085. + }
  20086. + AuDebugOn(add_entry
  20087. + && !au_ftest_wrdir(args->flags, TMPFILE)
  20088. + && !au_h_dptr(dentry, bcpup));
  20089. + }
  20090. +
  20091. +out:
  20092. + dput(parent);
  20093. + return err;
  20094. +}
  20095. +
  20096. +/* ---------------------------------------------------------------------- */
  20097. +
  20098. +void au_pin_hdir_unlock(struct au_pin *p)
  20099. +{
  20100. + if (p->hdir)
  20101. + au_hn_inode_unlock(p->hdir);
  20102. +}
  20103. +
  20104. +int au_pin_hdir_lock(struct au_pin *p)
  20105. +{
  20106. + int err;
  20107. +
  20108. + err = 0;
  20109. + if (!p->hdir)
  20110. + goto out;
  20111. +
  20112. + /* even if an error happens later, keep this lock */
  20113. + au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
  20114. +
  20115. + err = -EBUSY;
  20116. + if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
  20117. + goto out;
  20118. +
  20119. + err = 0;
  20120. + if (p->h_dentry)
  20121. + err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
  20122. + p->h_parent, p->br);
  20123. +
  20124. +out:
  20125. + return err;
  20126. +}
  20127. +
  20128. +int au_pin_hdir_relock(struct au_pin *p)
  20129. +{
  20130. + int err, i;
  20131. + struct inode *h_i;
  20132. + struct dentry *h_d[] = {
  20133. + p->h_dentry,
  20134. + p->h_parent
  20135. + };
  20136. +
  20137. + err = au_pin_hdir_lock(p);
  20138. + if (unlikely(err))
  20139. + goto out;
  20140. +
  20141. + for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
  20142. + if (!h_d[i])
  20143. + continue;
  20144. + if (d_is_positive(h_d[i])) {
  20145. + h_i = d_inode(h_d[i]);
  20146. + err = !h_i->i_nlink;
  20147. + }
  20148. + }
  20149. +
  20150. +out:
  20151. + return err;
  20152. +}
  20153. +
  20154. +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
  20155. +{
  20156. +#if !defined(CONFIG_RWSEM_GENERIC_SPINLOCK) && defined(CONFIG_RWSEM_SPIN_ON_OWNER)
  20157. + p->hdir->hi_inode->i_rwsem.owner = task;
  20158. +#endif
  20159. +}
  20160. +
  20161. +void au_pin_hdir_acquire_nest(struct au_pin *p)
  20162. +{
  20163. + if (p->hdir) {
  20164. + rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
  20165. + p->lsc_hi, 0, NULL, _RET_IP_);
  20166. + au_pin_hdir_set_owner(p, current);
  20167. + }
  20168. +}
  20169. +
  20170. +void au_pin_hdir_release(struct au_pin *p)
  20171. +{
  20172. + if (p->hdir) {
  20173. + au_pin_hdir_set_owner(p, p->task);
  20174. + rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, 1, _RET_IP_);
  20175. + }
  20176. +}
  20177. +
  20178. +struct dentry *au_pinned_h_parent(struct au_pin *pin)
  20179. +{
  20180. + if (pin && pin->parent)
  20181. + return au_h_dptr(pin->parent, pin->bindex);
  20182. + return NULL;
  20183. +}
  20184. +
  20185. +void au_unpin(struct au_pin *p)
  20186. +{
  20187. + if (p->hdir)
  20188. + au_pin_hdir_unlock(p);
  20189. + if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
  20190. + vfsub_mnt_drop_write(p->h_mnt);
  20191. + if (!p->hdir)
  20192. + return;
  20193. +
  20194. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  20195. + di_read_unlock(p->parent, AuLock_IR);
  20196. + iput(p->hdir->hi_inode);
  20197. + dput(p->parent);
  20198. + p->parent = NULL;
  20199. + p->hdir = NULL;
  20200. + p->h_mnt = NULL;
  20201. + /* do not clear p->task */
  20202. +}
  20203. +
  20204. +int au_do_pin(struct au_pin *p)
  20205. +{
  20206. + int err;
  20207. + struct super_block *sb;
  20208. + struct inode *h_dir;
  20209. +
  20210. + err = 0;
  20211. + sb = p->dentry->d_sb;
  20212. + p->br = au_sbr(sb, p->bindex);
  20213. + if (IS_ROOT(p->dentry)) {
  20214. + if (au_ftest_pin(p->flags, MNT_WRITE)) {
  20215. + p->h_mnt = au_br_mnt(p->br);
  20216. + err = vfsub_mnt_want_write(p->h_mnt);
  20217. + if (unlikely(err)) {
  20218. + au_fclr_pin(p->flags, MNT_WRITE);
  20219. + goto out_err;
  20220. + }
  20221. + }
  20222. + goto out;
  20223. + }
  20224. +
  20225. + p->h_dentry = NULL;
  20226. + if (p->bindex <= au_dbbot(p->dentry))
  20227. + p->h_dentry = au_h_dptr(p->dentry, p->bindex);
  20228. +
  20229. + p->parent = dget_parent(p->dentry);
  20230. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  20231. + di_read_lock(p->parent, AuLock_IR, p->lsc_di);
  20232. +
  20233. + h_dir = NULL;
  20234. + p->h_parent = au_h_dptr(p->parent, p->bindex);
  20235. + p->hdir = au_hi(d_inode(p->parent), p->bindex);
  20236. + if (p->hdir)
  20237. + h_dir = p->hdir->hi_inode;
  20238. +
  20239. + /*
  20240. + * udba case, or
  20241. + * if DI_LOCKED is not set, then p->parent may be different
  20242. + * and h_parent can be NULL.
  20243. + */
  20244. + if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
  20245. + err = -EBUSY;
  20246. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  20247. + di_read_unlock(p->parent, AuLock_IR);
  20248. + dput(p->parent);
  20249. + p->parent = NULL;
  20250. + goto out_err;
  20251. + }
  20252. +
  20253. + if (au_ftest_pin(p->flags, MNT_WRITE)) {
  20254. + p->h_mnt = au_br_mnt(p->br);
  20255. + err = vfsub_mnt_want_write(p->h_mnt);
  20256. + if (unlikely(err)) {
  20257. + au_fclr_pin(p->flags, MNT_WRITE);
  20258. + if (!au_ftest_pin(p->flags, DI_LOCKED))
  20259. + di_read_unlock(p->parent, AuLock_IR);
  20260. + dput(p->parent);
  20261. + p->parent = NULL;
  20262. + goto out_err;
  20263. + }
  20264. + }
  20265. +
  20266. + au_igrab(h_dir);
  20267. + err = au_pin_hdir_lock(p);
  20268. + if (!err)
  20269. + goto out; /* success */
  20270. +
  20271. + au_unpin(p);
  20272. +
  20273. +out_err:
  20274. + pr_err("err %d\n", err);
  20275. + err = au_busy_or_stale();
  20276. +out:
  20277. + return err;
  20278. +}
  20279. +
  20280. +void au_pin_init(struct au_pin *p, struct dentry *dentry,
  20281. + aufs_bindex_t bindex, int lsc_di, int lsc_hi,
  20282. + unsigned int udba, unsigned char flags)
  20283. +{
  20284. + p->dentry = dentry;
  20285. + p->udba = udba;
  20286. + p->lsc_di = lsc_di;
  20287. + p->lsc_hi = lsc_hi;
  20288. + p->flags = flags;
  20289. + p->bindex = bindex;
  20290. +
  20291. + p->parent = NULL;
  20292. + p->hdir = NULL;
  20293. + p->h_mnt = NULL;
  20294. +
  20295. + p->h_dentry = NULL;
  20296. + p->h_parent = NULL;
  20297. + p->br = NULL;
  20298. + p->task = current;
  20299. +}
  20300. +
  20301. +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
  20302. + unsigned int udba, unsigned char flags)
  20303. +{
  20304. + au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
  20305. + udba, flags);
  20306. + return au_do_pin(pin);
  20307. +}
  20308. +
  20309. +/* ---------------------------------------------------------------------- */
  20310. +
  20311. +/*
  20312. + * ->setattr() and ->getattr() are called in various cases.
  20313. + * chmod, stat: dentry is revalidated.
  20314. + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
  20315. + * unhashed.
  20316. + * for ->setattr(), ia->ia_file is passed from ftruncate only.
  20317. + */
  20318. +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
  20319. +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
  20320. +{
  20321. + int err;
  20322. + struct dentry *parent;
  20323. +
  20324. + err = 0;
  20325. + if (au_digen_test(dentry, sigen)) {
  20326. + parent = dget_parent(dentry);
  20327. + di_read_lock_parent(parent, AuLock_IR);
  20328. + err = au_refresh_dentry(dentry, parent);
  20329. + di_read_unlock(parent, AuLock_IR);
  20330. + dput(parent);
  20331. + }
  20332. +
  20333. + AuTraceErr(err);
  20334. + return err;
  20335. +}
  20336. +
  20337. +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
  20338. + struct au_icpup_args *a)
  20339. +{
  20340. + int err;
  20341. + loff_t sz;
  20342. + aufs_bindex_t btop, ibtop;
  20343. + struct dentry *hi_wh, *parent;
  20344. + struct inode *inode;
  20345. + struct au_wr_dir_args wr_dir_args = {
  20346. + .force_btgt = -1,
  20347. + .flags = 0
  20348. + };
  20349. +
  20350. + if (d_is_dir(dentry))
  20351. + au_fset_wrdir(wr_dir_args.flags, ISDIR);
  20352. + /* plink or hi_wh() case */
  20353. + btop = au_dbtop(dentry);
  20354. + inode = d_inode(dentry);
  20355. + ibtop = au_ibtop(inode);
  20356. + if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
  20357. + wr_dir_args.force_btgt = ibtop;
  20358. + err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
  20359. + if (unlikely(err < 0))
  20360. + goto out;
  20361. + a->btgt = err;
  20362. + if (err != btop)
  20363. + au_fset_icpup(a->flags, DID_CPUP);
  20364. +
  20365. + err = 0;
  20366. + a->pin_flags = AuPin_MNT_WRITE;
  20367. + parent = NULL;
  20368. + if (!IS_ROOT(dentry)) {
  20369. + au_fset_pin(a->pin_flags, DI_LOCKED);
  20370. + parent = dget_parent(dentry);
  20371. + di_write_lock_parent(parent);
  20372. + }
  20373. +
  20374. + err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
  20375. + if (unlikely(err))
  20376. + goto out_parent;
  20377. +
  20378. + sz = -1;
  20379. + a->h_path.dentry = au_h_dptr(dentry, btop);
  20380. + a->h_inode = d_inode(a->h_path.dentry);
  20381. + if (ia && (ia->ia_valid & ATTR_SIZE)) {
  20382. + inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
  20383. + if (ia->ia_size < i_size_read(a->h_inode))
  20384. + sz = ia->ia_size;
  20385. + inode_unlock_shared(a->h_inode);
  20386. + }
  20387. +
  20388. + hi_wh = NULL;
  20389. + if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
  20390. + hi_wh = au_hi_wh(inode, a->btgt);
  20391. + if (!hi_wh) {
  20392. + struct au_cp_generic cpg = {
  20393. + .dentry = dentry,
  20394. + .bdst = a->btgt,
  20395. + .bsrc = -1,
  20396. + .len = sz,
  20397. + .pin = &a->pin
  20398. + };
  20399. + err = au_sio_cpup_wh(&cpg, /*file*/NULL);
  20400. + if (unlikely(err))
  20401. + goto out_unlock;
  20402. + hi_wh = au_hi_wh(inode, a->btgt);
  20403. + /* todo: revalidate hi_wh? */
  20404. + }
  20405. + }
  20406. +
  20407. + if (parent) {
  20408. + au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
  20409. + di_downgrade_lock(parent, AuLock_IR);
  20410. + dput(parent);
  20411. + parent = NULL;
  20412. + }
  20413. + if (!au_ftest_icpup(a->flags, DID_CPUP))
  20414. + goto out; /* success */
  20415. +
  20416. + if (!d_unhashed(dentry)) {
  20417. + struct au_cp_generic cpg = {
  20418. + .dentry = dentry,
  20419. + .bdst = a->btgt,
  20420. + .bsrc = btop,
  20421. + .len = sz,
  20422. + .pin = &a->pin,
  20423. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  20424. + };
  20425. + err = au_sio_cpup_simple(&cpg);
  20426. + if (!err)
  20427. + a->h_path.dentry = au_h_dptr(dentry, a->btgt);
  20428. + } else if (!hi_wh)
  20429. + a->h_path.dentry = au_h_dptr(dentry, a->btgt);
  20430. + else
  20431. + a->h_path.dentry = hi_wh; /* do not dget here */
  20432. +
  20433. +out_unlock:
  20434. + a->h_inode = d_inode(a->h_path.dentry);
  20435. + if (!err)
  20436. + goto out; /* success */
  20437. + au_unpin(&a->pin);
  20438. +out_parent:
  20439. + if (parent) {
  20440. + di_write_unlock(parent);
  20441. + dput(parent);
  20442. + }
  20443. +out:
  20444. + if (!err)
  20445. + inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
  20446. + return err;
  20447. +}
  20448. +
  20449. +static int aufs_setattr(struct dentry *dentry, struct iattr *ia)
  20450. +{
  20451. + int err;
  20452. + struct inode *inode, *delegated;
  20453. + struct super_block *sb;
  20454. + struct file *file;
  20455. + struct au_icpup_args *a;
  20456. +
  20457. + inode = d_inode(dentry);
  20458. + IMustLock(inode);
  20459. +
  20460. + err = setattr_prepare(dentry, ia);
  20461. + if (unlikely(err))
  20462. + goto out;
  20463. +
  20464. + err = -ENOMEM;
  20465. + a = kzalloc(sizeof(*a), GFP_NOFS);
  20466. + if (unlikely(!a))
  20467. + goto out;
  20468. +
  20469. + if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
  20470. + ia->ia_valid &= ~ATTR_MODE;
  20471. +
  20472. + file = NULL;
  20473. + sb = dentry->d_sb;
  20474. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  20475. + if (unlikely(err))
  20476. + goto out_kfree;
  20477. +
  20478. + if (ia->ia_valid & ATTR_FILE) {
  20479. + /* currently ftruncate(2) only */
  20480. + AuDebugOn(!d_is_reg(dentry));
  20481. + file = ia->ia_file;
  20482. + err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
  20483. + /*fi_lsc*/0);
  20484. + if (unlikely(err))
  20485. + goto out_si;
  20486. + ia->ia_file = au_hf_top(file);
  20487. + a->udba = AuOpt_UDBA_NONE;
  20488. + } else {
  20489. + /* fchmod() doesn't pass ia_file */
  20490. + a->udba = au_opt_udba(sb);
  20491. + di_write_lock_child(dentry);
  20492. + /* no d_unlinked(), to set UDBA_NONE for root */
  20493. + if (d_unhashed(dentry))
  20494. + a->udba = AuOpt_UDBA_NONE;
  20495. + if (a->udba != AuOpt_UDBA_NONE) {
  20496. + AuDebugOn(IS_ROOT(dentry));
  20497. + err = au_reval_for_attr(dentry, au_sigen(sb));
  20498. + if (unlikely(err))
  20499. + goto out_dentry;
  20500. + }
  20501. + }
  20502. +
  20503. + err = au_pin_and_icpup(dentry, ia, a);
  20504. + if (unlikely(err < 0))
  20505. + goto out_dentry;
  20506. + if (au_ftest_icpup(a->flags, DID_CPUP)) {
  20507. + ia->ia_file = NULL;
  20508. + ia->ia_valid &= ~ATTR_FILE;
  20509. + }
  20510. +
  20511. + a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
  20512. + if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
  20513. + == (ATTR_MODE | ATTR_CTIME)) {
  20514. + err = security_path_chmod(&a->h_path, ia->ia_mode);
  20515. + if (unlikely(err))
  20516. + goto out_unlock;
  20517. + } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
  20518. + && (ia->ia_valid & ATTR_CTIME)) {
  20519. + err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
  20520. + if (unlikely(err))
  20521. + goto out_unlock;
  20522. + }
  20523. +
  20524. + if (ia->ia_valid & ATTR_SIZE) {
  20525. + struct file *f;
  20526. +
  20527. + if (ia->ia_size < i_size_read(inode))
  20528. + /* unmap only */
  20529. + truncate_setsize(inode, ia->ia_size);
  20530. +
  20531. + f = NULL;
  20532. + if (ia->ia_valid & ATTR_FILE)
  20533. + f = ia->ia_file;
  20534. + inode_unlock(a->h_inode);
  20535. + err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
  20536. + inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
  20537. + } else {
  20538. + delegated = NULL;
  20539. + while (1) {
  20540. + err = vfsub_notify_change(&a->h_path, ia, &delegated);
  20541. + if (delegated) {
  20542. + err = break_deleg_wait(&delegated);
  20543. + if (!err)
  20544. + continue;
  20545. + }
  20546. + break;
  20547. + }
  20548. + }
  20549. + /*
  20550. + * regardless aufs 'acl' option setting.
  20551. + * why don't all acl-aware fs call this func from their ->setattr()?
  20552. + */
  20553. + if (!err && (ia->ia_valid & ATTR_MODE))
  20554. + err = vfsub_acl_chmod(a->h_inode, ia->ia_mode);
  20555. + if (!err)
  20556. + au_cpup_attr_changeable(inode);
  20557. +
  20558. +out_unlock:
  20559. + inode_unlock(a->h_inode);
  20560. + au_unpin(&a->pin);
  20561. + if (unlikely(err))
  20562. + au_update_dbtop(dentry);
  20563. +out_dentry:
  20564. + di_write_unlock(dentry);
  20565. + if (file) {
  20566. + fi_write_unlock(file);
  20567. + ia->ia_file = file;
  20568. + ia->ia_valid |= ATTR_FILE;
  20569. + }
  20570. +out_si:
  20571. + si_read_unlock(sb);
  20572. +out_kfree:
  20573. + au_kfree_rcu(a);
  20574. +out:
  20575. + AuTraceErr(err);
  20576. + return err;
  20577. +}
  20578. +
  20579. +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
  20580. +static int au_h_path_to_set_attr(struct dentry *dentry,
  20581. + struct au_icpup_args *a, struct path *h_path)
  20582. +{
  20583. + int err;
  20584. + struct super_block *sb;
  20585. +
  20586. + sb = dentry->d_sb;
  20587. + a->udba = au_opt_udba(sb);
  20588. + /* no d_unlinked(), to set UDBA_NONE for root */
  20589. + if (d_unhashed(dentry))
  20590. + a->udba = AuOpt_UDBA_NONE;
  20591. + if (a->udba != AuOpt_UDBA_NONE) {
  20592. + AuDebugOn(IS_ROOT(dentry));
  20593. + err = au_reval_for_attr(dentry, au_sigen(sb));
  20594. + if (unlikely(err))
  20595. + goto out;
  20596. + }
  20597. + err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
  20598. + if (unlikely(err < 0))
  20599. + goto out;
  20600. +
  20601. + h_path->dentry = a->h_path.dentry;
  20602. + h_path->mnt = au_sbr_mnt(sb, a->btgt);
  20603. +
  20604. +out:
  20605. + return err;
  20606. +}
  20607. +
  20608. +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
  20609. + struct au_sxattr *arg)
  20610. +{
  20611. + int err;
  20612. + struct path h_path;
  20613. + struct super_block *sb;
  20614. + struct au_icpup_args *a;
  20615. + struct inode *h_inode;
  20616. +
  20617. + IMustLock(inode);
  20618. +
  20619. + err = -ENOMEM;
  20620. + a = kzalloc(sizeof(*a), GFP_NOFS);
  20621. + if (unlikely(!a))
  20622. + goto out;
  20623. +
  20624. + sb = dentry->d_sb;
  20625. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  20626. + if (unlikely(err))
  20627. + goto out_kfree;
  20628. +
  20629. + h_path.dentry = NULL; /* silence gcc */
  20630. + di_write_lock_child(dentry);
  20631. + err = au_h_path_to_set_attr(dentry, a, &h_path);
  20632. + if (unlikely(err))
  20633. + goto out_di;
  20634. +
  20635. + inode_unlock(a->h_inode);
  20636. + switch (arg->type) {
  20637. + case AU_XATTR_SET:
  20638. + AuDebugOn(d_is_negative(h_path.dentry));
  20639. + err = vfsub_setxattr(h_path.dentry,
  20640. + arg->u.set.name, arg->u.set.value,
  20641. + arg->u.set.size, arg->u.set.flags);
  20642. + break;
  20643. + case AU_ACL_SET:
  20644. + err = -EOPNOTSUPP;
  20645. + h_inode = d_inode(h_path.dentry);
  20646. + if (h_inode->i_op->set_acl)
  20647. + /* this will call posix_acl_update_mode */
  20648. + err = h_inode->i_op->set_acl(h_inode,
  20649. + arg->u.acl_set.acl,
  20650. + arg->u.acl_set.type);
  20651. + break;
  20652. + }
  20653. + if (!err)
  20654. + au_cpup_attr_timesizes(inode);
  20655. +
  20656. + au_unpin(&a->pin);
  20657. + if (unlikely(err))
  20658. + au_update_dbtop(dentry);
  20659. +
  20660. +out_di:
  20661. + di_write_unlock(dentry);
  20662. + si_read_unlock(sb);
  20663. +out_kfree:
  20664. + au_kfree_rcu(a);
  20665. +out:
  20666. + AuTraceErr(err);
  20667. + return err;
  20668. +}
  20669. +#endif
  20670. +
  20671. +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
  20672. + unsigned int nlink)
  20673. +{
  20674. + unsigned int n;
  20675. +
  20676. + inode->i_mode = st->mode;
  20677. + /* don't i_[ug]id_write() here */
  20678. + inode->i_uid = st->uid;
  20679. + inode->i_gid = st->gid;
  20680. + inode->i_atime = st->atime;
  20681. + inode->i_mtime = st->mtime;
  20682. + inode->i_ctime = st->ctime;
  20683. +
  20684. + au_cpup_attr_nlink(inode, /*force*/0);
  20685. + if (S_ISDIR(inode->i_mode)) {
  20686. + n = inode->i_nlink;
  20687. + n -= nlink;
  20688. + n += st->nlink;
  20689. + smp_mb(); /* for i_nlink */
  20690. + /* 0 can happen */
  20691. + set_nlink(inode, n);
  20692. + }
  20693. +
  20694. + spin_lock(&inode->i_lock);
  20695. + inode->i_blocks = st->blocks;
  20696. + i_size_write(inode, st->size);
  20697. + spin_unlock(&inode->i_lock);
  20698. +}
  20699. +
  20700. +/*
  20701. + * common routine for aufs_getattr() and au_getxattr().
  20702. + * returns zero or negative (an error).
  20703. + * @dentry will be read-locked in success.
  20704. + */
  20705. +int au_h_path_getattr(struct dentry *dentry, int force, struct path *h_path,
  20706. + int locked)
  20707. +{
  20708. + int err;
  20709. + unsigned int mnt_flags, sigen;
  20710. + unsigned char udba_none;
  20711. + aufs_bindex_t bindex;
  20712. + struct super_block *sb, *h_sb;
  20713. + struct inode *inode;
  20714. +
  20715. + h_path->mnt = NULL;
  20716. + h_path->dentry = NULL;
  20717. +
  20718. + err = 0;
  20719. + sb = dentry->d_sb;
  20720. + mnt_flags = au_mntflags(sb);
  20721. + udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
  20722. +
  20723. + if (unlikely(locked))
  20724. + goto body; /* skip locking dinfo */
  20725. +
  20726. + /* support fstat(2) */
  20727. + if (!d_unlinked(dentry) && !udba_none) {
  20728. + sigen = au_sigen(sb);
  20729. + err = au_digen_test(dentry, sigen);
  20730. + if (!err) {
  20731. + di_read_lock_child(dentry, AuLock_IR);
  20732. + err = au_dbrange_test(dentry);
  20733. + if (unlikely(err)) {
  20734. + di_read_unlock(dentry, AuLock_IR);
  20735. + goto out;
  20736. + }
  20737. + } else {
  20738. + AuDebugOn(IS_ROOT(dentry));
  20739. + di_write_lock_child(dentry);
  20740. + err = au_dbrange_test(dentry);
  20741. + if (!err)
  20742. + err = au_reval_for_attr(dentry, sigen);
  20743. + if (!err)
  20744. + di_downgrade_lock(dentry, AuLock_IR);
  20745. + else {
  20746. + di_write_unlock(dentry);
  20747. + goto out;
  20748. + }
  20749. + }
  20750. + } else
  20751. + di_read_lock_child(dentry, AuLock_IR);
  20752. +
  20753. +body:
  20754. + inode = d_inode(dentry);
  20755. + bindex = au_ibtop(inode);
  20756. + h_path->mnt = au_sbr_mnt(sb, bindex);
  20757. + h_sb = h_path->mnt->mnt_sb;
  20758. + if (!force
  20759. + && !au_test_fs_bad_iattr(h_sb)
  20760. + && udba_none)
  20761. + goto out; /* success */
  20762. +
  20763. + if (au_dbtop(dentry) == bindex)
  20764. + h_path->dentry = au_h_dptr(dentry, bindex);
  20765. + else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
  20766. + h_path->dentry = au_plink_lkup(inode, bindex);
  20767. + if (IS_ERR(h_path->dentry))
  20768. + /* pretending success */
  20769. + h_path->dentry = NULL;
  20770. + else
  20771. + dput(h_path->dentry);
  20772. + }
  20773. +
  20774. +out:
  20775. + return err;
  20776. +}
  20777. +
  20778. +static int aufs_getattr(const struct path *path, struct kstat *st,
  20779. + u32 request, unsigned int query)
  20780. +{
  20781. + int err;
  20782. + unsigned char positive;
  20783. + struct path h_path;
  20784. + struct dentry *dentry;
  20785. + struct inode *inode;
  20786. + struct super_block *sb;
  20787. +
  20788. + dentry = path->dentry;
  20789. + inode = d_inode(dentry);
  20790. + sb = dentry->d_sb;
  20791. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  20792. + if (unlikely(err))
  20793. + goto out;
  20794. + err = au_h_path_getattr(dentry, /*force*/0, &h_path, /*locked*/0);
  20795. + if (unlikely(err))
  20796. + goto out_si;
  20797. + if (unlikely(!h_path.dentry))
  20798. + /* illegally overlapped or something */
  20799. + goto out_fill; /* pretending success */
  20800. +
  20801. + positive = d_is_positive(h_path.dentry);
  20802. + if (positive)
  20803. + /* no vfsub version */
  20804. + err = vfs_getattr(&h_path, st, request, query);
  20805. + if (!err) {
  20806. + if (positive)
  20807. + au_refresh_iattr(inode, st,
  20808. + d_inode(h_path.dentry)->i_nlink);
  20809. + goto out_fill; /* success */
  20810. + }
  20811. + AuTraceErr(err);
  20812. + goto out_di;
  20813. +
  20814. +out_fill:
  20815. + generic_fillattr(inode, st);
  20816. +out_di:
  20817. + di_read_unlock(dentry, AuLock_IR);
  20818. +out_si:
  20819. + si_read_unlock(sb);
  20820. +out:
  20821. + AuTraceErr(err);
  20822. + return err;
  20823. +}
  20824. +
  20825. +/* ---------------------------------------------------------------------- */
  20826. +
  20827. +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
  20828. + struct delayed_call *done)
  20829. +{
  20830. + const char *ret;
  20831. + struct dentry *h_dentry;
  20832. + struct inode *h_inode;
  20833. + int err;
  20834. + aufs_bindex_t bindex;
  20835. +
  20836. + ret = NULL; /* suppress a warning */
  20837. + err = -ECHILD;
  20838. + if (!dentry)
  20839. + goto out;
  20840. +
  20841. + err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
  20842. + if (unlikely(err))
  20843. + goto out;
  20844. +
  20845. + err = au_d_hashed_positive(dentry);
  20846. + if (unlikely(err))
  20847. + goto out_unlock;
  20848. +
  20849. + err = -EINVAL;
  20850. + inode = d_inode(dentry);
  20851. + bindex = au_ibtop(inode);
  20852. + h_inode = au_h_iptr(inode, bindex);
  20853. + if (unlikely(!h_inode->i_op->get_link))
  20854. + goto out_unlock;
  20855. +
  20856. + err = -EBUSY;
  20857. + h_dentry = NULL;
  20858. + if (au_dbtop(dentry) <= bindex) {
  20859. + h_dentry = au_h_dptr(dentry, bindex);
  20860. + if (h_dentry)
  20861. + dget(h_dentry);
  20862. + }
  20863. + if (!h_dentry) {
  20864. + h_dentry = d_find_any_alias(h_inode);
  20865. + if (IS_ERR(h_dentry)) {
  20866. + err = PTR_ERR(h_dentry);
  20867. + goto out_unlock;
  20868. + }
  20869. + }
  20870. + if (unlikely(!h_dentry))
  20871. + goto out_unlock;
  20872. +
  20873. + err = 0;
  20874. + AuDbg("%ps\n", h_inode->i_op->get_link);
  20875. + AuDbgDentry(h_dentry);
  20876. + ret = vfs_get_link(h_dentry, done);
  20877. + dput(h_dentry);
  20878. + if (IS_ERR(ret))
  20879. + err = PTR_ERR(ret);
  20880. +
  20881. +out_unlock:
  20882. + aufs_read_unlock(dentry, AuLock_IR);
  20883. +out:
  20884. + if (unlikely(err))
  20885. + ret = ERR_PTR(err);
  20886. + AuTraceErrPtr(ret);
  20887. + return ret;
  20888. +}
  20889. +
  20890. +/* ---------------------------------------------------------------------- */
  20891. +
  20892. +static int au_is_special(struct inode *inode)
  20893. +{
  20894. + return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
  20895. +}
  20896. +
  20897. +static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
  20898. + int flags)
  20899. +{
  20900. + int err;
  20901. + aufs_bindex_t bindex;
  20902. + struct super_block *sb;
  20903. + struct inode *h_inode;
  20904. + struct vfsmount *h_mnt;
  20905. +
  20906. + sb = inode->i_sb;
  20907. + WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
  20908. + "unexpected s_flags 0x%lx", sb->s_flags);
  20909. +
  20910. + /* mmap_sem might be acquired already, cf. aufs_mmap() */
  20911. + lockdep_off();
  20912. + si_read_lock(sb, AuLock_FLUSH);
  20913. + ii_write_lock_child(inode);
  20914. +
  20915. + err = 0;
  20916. + bindex = au_ibtop(inode);
  20917. + h_inode = au_h_iptr(inode, bindex);
  20918. + if (!au_test_ro(sb, bindex, inode)) {
  20919. + h_mnt = au_sbr_mnt(sb, bindex);
  20920. + err = vfsub_mnt_want_write(h_mnt);
  20921. + if (!err) {
  20922. + err = vfsub_update_time(h_inode, ts, flags);
  20923. + vfsub_mnt_drop_write(h_mnt);
  20924. + }
  20925. + } else if (au_is_special(h_inode)) {
  20926. + /*
  20927. + * Never copy-up here.
  20928. + * These special files may already be opened and used for
  20929. + * communicating. If we copied it up, then the communication
  20930. + * would be corrupted.
  20931. + */
  20932. + AuWarn1("timestamps for i%lu are ignored "
  20933. + "since it is on readonly branch (hi%lu).\n",
  20934. + inode->i_ino, h_inode->i_ino);
  20935. + } else if (flags & ~S_ATIME) {
  20936. + err = -EIO;
  20937. + AuIOErr1("unexpected flags 0x%x\n", flags);
  20938. + AuDebugOn(1);
  20939. + }
  20940. +
  20941. + if (!err)
  20942. + au_cpup_attr_timesizes(inode);
  20943. + ii_write_unlock(inode);
  20944. + si_read_unlock(sb);
  20945. + lockdep_on();
  20946. +
  20947. + if (!err && (flags & S_VERSION))
  20948. + inode_inc_iversion(inode);
  20949. +
  20950. + return err;
  20951. +}
  20952. +
  20953. +/* ---------------------------------------------------------------------- */
  20954. +
  20955. +/* no getattr version will be set by module.c:aufs_init() */
  20956. +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
  20957. + aufs_iop[] = {
  20958. + [AuIop_SYMLINK] = {
  20959. + .permission = aufs_permission,
  20960. +#ifdef CONFIG_FS_POSIX_ACL
  20961. + .get_acl = aufs_get_acl,
  20962. + .set_acl = aufs_set_acl, /* unsupport for symlink? */
  20963. +#endif
  20964. +
  20965. + .setattr = aufs_setattr,
  20966. + .getattr = aufs_getattr,
  20967. +
  20968. +#ifdef CONFIG_AUFS_XATTR
  20969. + .listxattr = aufs_listxattr,
  20970. +#endif
  20971. +
  20972. + .get_link = aufs_get_link,
  20973. +
  20974. + /* .update_time = aufs_update_time */
  20975. + },
  20976. + [AuIop_DIR] = {
  20977. + .create = aufs_create,
  20978. + .lookup = aufs_lookup,
  20979. + .link = aufs_link,
  20980. + .unlink = aufs_unlink,
  20981. + .symlink = aufs_symlink,
  20982. + .mkdir = aufs_mkdir,
  20983. + .rmdir = aufs_rmdir,
  20984. + .mknod = aufs_mknod,
  20985. + .rename = aufs_rename,
  20986. +
  20987. + .permission = aufs_permission,
  20988. +#ifdef CONFIG_FS_POSIX_ACL
  20989. + .get_acl = aufs_get_acl,
  20990. + .set_acl = aufs_set_acl,
  20991. +#endif
  20992. +
  20993. + .setattr = aufs_setattr,
  20994. + .getattr = aufs_getattr,
  20995. +
  20996. +#ifdef CONFIG_AUFS_XATTR
  20997. + .listxattr = aufs_listxattr,
  20998. +#endif
  20999. +
  21000. + .update_time = aufs_update_time,
  21001. + .atomic_open = aufs_atomic_open,
  21002. + .tmpfile = aufs_tmpfile
  21003. + },
  21004. + [AuIop_OTHER] = {
  21005. + .permission = aufs_permission,
  21006. +#ifdef CONFIG_FS_POSIX_ACL
  21007. + .get_acl = aufs_get_acl,
  21008. + .set_acl = aufs_set_acl,
  21009. +#endif
  21010. +
  21011. + .setattr = aufs_setattr,
  21012. + .getattr = aufs_getattr,
  21013. +
  21014. +#ifdef CONFIG_AUFS_XATTR
  21015. + .listxattr = aufs_listxattr,
  21016. +#endif
  21017. +
  21018. + .update_time = aufs_update_time
  21019. + }
  21020. +};
  21021. diff -Naur null/fs/aufs/i_op_del.c linux-5.1/fs/aufs/i_op_del.c
  21022. --- /dev/null
  21023. +++ linux-5.1/fs/aufs/i_op_del.c 2019-05-22 11:23:07.441140996 +0200
  21024. @@ -0,0 +1,513 @@
  21025. +// SPDX-License-Identifier: GPL-2.0
  21026. +/*
  21027. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  21028. + *
  21029. + * This program, aufs is free software; you can redistribute it and/or modify
  21030. + * it under the terms of the GNU General Public License as published by
  21031. + * the Free Software Foundation; either version 2 of the License, or
  21032. + * (at your option) any later version.
  21033. + *
  21034. + * This program is distributed in the hope that it will be useful,
  21035. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21036. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21037. + * GNU General Public License for more details.
  21038. + *
  21039. + * You should have received a copy of the GNU General Public License
  21040. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21041. + */
  21042. +
  21043. +/*
  21044. + * inode operations (del entry)
  21045. + */
  21046. +
  21047. +#include <linux/iversion.h>
  21048. +#include "aufs.h"
  21049. +
  21050. +/*
  21051. + * decide if a new whiteout for @dentry is necessary or not.
  21052. + * when it is necessary, prepare the parent dir for the upper branch whose
  21053. + * branch index is @bcpup for creation. the actual creation of the whiteout will
  21054. + * be done by caller.
  21055. + * return value:
  21056. + * 0: wh is unnecessary
  21057. + * plus: wh is necessary
  21058. + * minus: error
  21059. + */
  21060. +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
  21061. +{
  21062. + int need_wh, err;
  21063. + aufs_bindex_t btop;
  21064. + struct super_block *sb;
  21065. +
  21066. + sb = dentry->d_sb;
  21067. + btop = au_dbtop(dentry);
  21068. + if (*bcpup < 0) {
  21069. + *bcpup = btop;
  21070. + if (au_test_ro(sb, btop, d_inode(dentry))) {
  21071. + err = AuWbrCopyup(au_sbi(sb), dentry);
  21072. + *bcpup = err;
  21073. + if (unlikely(err < 0))
  21074. + goto out;
  21075. + }
  21076. + } else
  21077. + AuDebugOn(btop < *bcpup
  21078. + || au_test_ro(sb, *bcpup, d_inode(dentry)));
  21079. + AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
  21080. +
  21081. + if (*bcpup != btop) {
  21082. + err = au_cpup_dirs(dentry, *bcpup);
  21083. + if (unlikely(err))
  21084. + goto out;
  21085. + need_wh = 1;
  21086. + } else {
  21087. + struct au_dinfo *dinfo, *tmp;
  21088. +
  21089. + need_wh = -ENOMEM;
  21090. + dinfo = au_di(dentry);
  21091. + tmp = au_di_alloc(sb, AuLsc_DI_TMP);
  21092. + if (tmp) {
  21093. + au_di_cp(tmp, dinfo);
  21094. + au_di_swap(tmp, dinfo);
  21095. + /* returns the number of positive dentries */
  21096. + need_wh = au_lkup_dentry(dentry, btop + 1,
  21097. + /* AuLkup_IGNORE_PERM */ 0);
  21098. + au_di_swap(tmp, dinfo);
  21099. + au_rw_write_unlock(&tmp->di_rwsem);
  21100. + au_di_free(tmp);
  21101. + }
  21102. + }
  21103. + AuDbg("need_wh %d\n", need_wh);
  21104. + err = need_wh;
  21105. +
  21106. +out:
  21107. + return err;
  21108. +}
  21109. +
  21110. +/*
  21111. + * simple tests for the del-entry operations.
  21112. + * following the checks in vfs, plus the parent-child relationship.
  21113. + */
  21114. +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
  21115. + struct dentry *h_parent, int isdir)
  21116. +{
  21117. + int err;
  21118. + umode_t h_mode;
  21119. + struct dentry *h_dentry, *h_latest;
  21120. + struct inode *h_inode;
  21121. +
  21122. + h_dentry = au_h_dptr(dentry, bindex);
  21123. + if (d_really_is_positive(dentry)) {
  21124. + err = -ENOENT;
  21125. + if (unlikely(d_is_negative(h_dentry)))
  21126. + goto out;
  21127. + h_inode = d_inode(h_dentry);
  21128. + if (unlikely(!h_inode->i_nlink))
  21129. + goto out;
  21130. +
  21131. + h_mode = h_inode->i_mode;
  21132. + if (!isdir) {
  21133. + err = -EISDIR;
  21134. + if (unlikely(S_ISDIR(h_mode)))
  21135. + goto out;
  21136. + } else if (unlikely(!S_ISDIR(h_mode))) {
  21137. + err = -ENOTDIR;
  21138. + goto out;
  21139. + }
  21140. + } else {
  21141. + /* rename(2) case */
  21142. + err = -EIO;
  21143. + if (unlikely(d_is_positive(h_dentry)))
  21144. + goto out;
  21145. + }
  21146. +
  21147. + err = -ENOENT;
  21148. + /* expected parent dir is locked */
  21149. + if (unlikely(h_parent != h_dentry->d_parent))
  21150. + goto out;
  21151. + err = 0;
  21152. +
  21153. + /*
  21154. + * rmdir a dir may break the consistency on some filesystem.
  21155. + * let's try heavy test.
  21156. + */
  21157. + err = -EACCES;
  21158. + if (unlikely(!au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1)
  21159. + && au_test_h_perm(d_inode(h_parent),
  21160. + MAY_EXEC | MAY_WRITE)))
  21161. + goto out;
  21162. +
  21163. + h_latest = au_sio_lkup_one(&dentry->d_name, h_parent);
  21164. + err = -EIO;
  21165. + if (IS_ERR(h_latest))
  21166. + goto out;
  21167. + if (h_latest == h_dentry)
  21168. + err = 0;
  21169. + dput(h_latest);
  21170. +
  21171. +out:
  21172. + return err;
  21173. +}
  21174. +
  21175. +/*
  21176. + * decide the branch where we operate for @dentry. the branch index will be set
  21177. + * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
  21178. + * dir for reverting.
  21179. + * when a new whiteout is necessary, create it.
  21180. + */
  21181. +static struct dentry*
  21182. +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
  21183. + struct au_dtime *dt, struct au_pin *pin)
  21184. +{
  21185. + struct dentry *wh_dentry;
  21186. + struct super_block *sb;
  21187. + struct path h_path;
  21188. + int err, need_wh;
  21189. + unsigned int udba;
  21190. + aufs_bindex_t bcpup;
  21191. +
  21192. + need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
  21193. + wh_dentry = ERR_PTR(need_wh);
  21194. + if (unlikely(need_wh < 0))
  21195. + goto out;
  21196. +
  21197. + sb = dentry->d_sb;
  21198. + udba = au_opt_udba(sb);
  21199. + bcpup = *rbcpup;
  21200. + err = au_pin(pin, dentry, bcpup, udba,
  21201. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  21202. + wh_dentry = ERR_PTR(err);
  21203. + if (unlikely(err))
  21204. + goto out;
  21205. +
  21206. + h_path.dentry = au_pinned_h_parent(pin);
  21207. + if (udba != AuOpt_UDBA_NONE
  21208. + && au_dbtop(dentry) == bcpup) {
  21209. + err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
  21210. + wh_dentry = ERR_PTR(err);
  21211. + if (unlikely(err))
  21212. + goto out_unpin;
  21213. + }
  21214. +
  21215. + h_path.mnt = au_sbr_mnt(sb, bcpup);
  21216. + au_dtime_store(dt, au_pinned_parent(pin), &h_path);
  21217. + wh_dentry = NULL;
  21218. + if (!need_wh)
  21219. + goto out; /* success, no need to create whiteout */
  21220. +
  21221. + wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
  21222. + if (IS_ERR(wh_dentry))
  21223. + goto out_unpin;
  21224. +
  21225. + /* returns with the parent is locked and wh_dentry is dget-ed */
  21226. + goto out; /* success */
  21227. +
  21228. +out_unpin:
  21229. + au_unpin(pin);
  21230. +out:
  21231. + return wh_dentry;
  21232. +}
  21233. +
  21234. +/*
  21235. + * when removing a dir, rename it to a unique temporary whiteout-ed name first
  21236. + * in order to be revertible and save time for removing many child whiteouts
  21237. + * under the dir.
  21238. + * returns 1 when there are too many child whiteout and caller should remove
  21239. + * them asynchronously. returns 0 when the number of children is enough small to
  21240. + * remove now or the branch fs is a remote fs.
  21241. + * otherwise return an error.
  21242. + */
  21243. +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
  21244. + struct au_nhash *whlist, struct inode *dir)
  21245. +{
  21246. + int rmdir_later, err, dirwh;
  21247. + struct dentry *h_dentry;
  21248. + struct super_block *sb;
  21249. + struct inode *inode;
  21250. +
  21251. + sb = dentry->d_sb;
  21252. + SiMustAnyLock(sb);
  21253. + h_dentry = au_h_dptr(dentry, bindex);
  21254. + err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
  21255. + if (unlikely(err))
  21256. + goto out;
  21257. +
  21258. + /* stop monitoring */
  21259. + inode = d_inode(dentry);
  21260. + au_hn_free(au_hi(inode, bindex));
  21261. +
  21262. + if (!au_test_fs_remote(h_dentry->d_sb)) {
  21263. + dirwh = au_sbi(sb)->si_dirwh;
  21264. + rmdir_later = (dirwh <= 1);
  21265. + if (!rmdir_later)
  21266. + rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
  21267. + dirwh);
  21268. + if (rmdir_later)
  21269. + return rmdir_later;
  21270. + }
  21271. +
  21272. + err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
  21273. + if (unlikely(err)) {
  21274. + AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
  21275. + h_dentry, bindex, err);
  21276. + err = 0;
  21277. + }
  21278. +
  21279. +out:
  21280. + AuTraceErr(err);
  21281. + return err;
  21282. +}
  21283. +
  21284. +/*
  21285. + * final procedure for deleting a entry.
  21286. + * maintain dentry and iattr.
  21287. + */
  21288. +static void epilog(struct inode *dir, struct dentry *dentry,
  21289. + aufs_bindex_t bindex)
  21290. +{
  21291. + struct inode *inode;
  21292. +
  21293. + inode = d_inode(dentry);
  21294. + d_drop(dentry);
  21295. + inode->i_ctime = dir->i_ctime;
  21296. +
  21297. + au_dir_ts(dir, bindex);
  21298. + inode_inc_iversion(dir);
  21299. +}
  21300. +
  21301. +/*
  21302. + * when an error happened, remove the created whiteout and revert everything.
  21303. + */
  21304. +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
  21305. + aufs_bindex_t bwh, struct dentry *wh_dentry,
  21306. + struct dentry *dentry, struct au_dtime *dt)
  21307. +{
  21308. + int rerr;
  21309. + struct path h_path = {
  21310. + .dentry = wh_dentry,
  21311. + .mnt = au_sbr_mnt(dir->i_sb, bindex)
  21312. + };
  21313. +
  21314. + rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
  21315. + if (!rerr) {
  21316. + au_set_dbwh(dentry, bwh);
  21317. + au_dtime_revert(dt);
  21318. + return 0;
  21319. + }
  21320. +
  21321. + AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
  21322. + return -EIO;
  21323. +}
  21324. +
  21325. +/* ---------------------------------------------------------------------- */
  21326. +
  21327. +int aufs_unlink(struct inode *dir, struct dentry *dentry)
  21328. +{
  21329. + int err;
  21330. + aufs_bindex_t bwh, bindex, btop;
  21331. + struct inode *inode, *h_dir, *delegated;
  21332. + struct dentry *parent, *wh_dentry;
  21333. + /* to reduce stack size */
  21334. + struct {
  21335. + struct au_dtime dt;
  21336. + struct au_pin pin;
  21337. + struct path h_path;
  21338. + } *a;
  21339. +
  21340. + IMustLock(dir);
  21341. +
  21342. + err = -ENOMEM;
  21343. + a = kmalloc(sizeof(*a), GFP_NOFS);
  21344. + if (unlikely(!a))
  21345. + goto out;
  21346. +
  21347. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
  21348. + if (unlikely(err))
  21349. + goto out_free;
  21350. + err = au_d_hashed_positive(dentry);
  21351. + if (unlikely(err))
  21352. + goto out_unlock;
  21353. + inode = d_inode(dentry);
  21354. + IMustLock(inode);
  21355. + err = -EISDIR;
  21356. + if (unlikely(d_is_dir(dentry)))
  21357. + goto out_unlock; /* possible? */
  21358. +
  21359. + btop = au_dbtop(dentry);
  21360. + bwh = au_dbwh(dentry);
  21361. + bindex = -1;
  21362. + parent = dentry->d_parent; /* dir inode is locked */
  21363. + di_write_lock_parent(parent);
  21364. + wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
  21365. + &a->pin);
  21366. + err = PTR_ERR(wh_dentry);
  21367. + if (IS_ERR(wh_dentry))
  21368. + goto out_parent;
  21369. +
  21370. + a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
  21371. + a->h_path.dentry = au_h_dptr(dentry, btop);
  21372. + dget(a->h_path.dentry);
  21373. + if (bindex == btop) {
  21374. + h_dir = au_pinned_h_dir(&a->pin);
  21375. + delegated = NULL;
  21376. + err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
  21377. + if (unlikely(err == -EWOULDBLOCK)) {
  21378. + pr_warn("cannot retry for NFSv4 delegation"
  21379. + " for an internal unlink\n");
  21380. + iput(delegated);
  21381. + }
  21382. + } else {
  21383. + /* dir inode is locked */
  21384. + h_dir = d_inode(wh_dentry->d_parent);
  21385. + IMustLock(h_dir);
  21386. + err = 0;
  21387. + }
  21388. +
  21389. + if (!err) {
  21390. + vfsub_drop_nlink(inode);
  21391. + epilog(dir, dentry, bindex);
  21392. +
  21393. + /* update target timestamps */
  21394. + if (bindex == btop) {
  21395. + vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
  21396. + /*ignore*/
  21397. + inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
  21398. + } else
  21399. + /* todo: this timestamp may be reverted later */
  21400. + inode->i_ctime = h_dir->i_ctime;
  21401. + goto out_unpin; /* success */
  21402. + }
  21403. +
  21404. + /* revert */
  21405. + if (wh_dentry) {
  21406. + int rerr;
  21407. +
  21408. + rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
  21409. + &a->dt);
  21410. + if (rerr)
  21411. + err = rerr;
  21412. + }
  21413. +
  21414. +out_unpin:
  21415. + au_unpin(&a->pin);
  21416. + dput(wh_dentry);
  21417. + dput(a->h_path.dentry);
  21418. +out_parent:
  21419. + di_write_unlock(parent);
  21420. +out_unlock:
  21421. + aufs_read_unlock(dentry, AuLock_DW);
  21422. +out_free:
  21423. + au_kfree_rcu(a);
  21424. +out:
  21425. + return err;
  21426. +}
  21427. +
  21428. +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
  21429. +{
  21430. + int err, rmdir_later;
  21431. + aufs_bindex_t bwh, bindex, btop;
  21432. + struct inode *inode;
  21433. + struct dentry *parent, *wh_dentry, *h_dentry;
  21434. + struct au_whtmp_rmdir *args;
  21435. + /* to reduce stack size */
  21436. + struct {
  21437. + struct au_dtime dt;
  21438. + struct au_pin pin;
  21439. + } *a;
  21440. +
  21441. + IMustLock(dir);
  21442. +
  21443. + err = -ENOMEM;
  21444. + a = kmalloc(sizeof(*a), GFP_NOFS);
  21445. + if (unlikely(!a))
  21446. + goto out;
  21447. +
  21448. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
  21449. + if (unlikely(err))
  21450. + goto out_free;
  21451. + err = au_alive_dir(dentry);
  21452. + if (unlikely(err))
  21453. + goto out_unlock;
  21454. + inode = d_inode(dentry);
  21455. + IMustLock(inode);
  21456. + err = -ENOTDIR;
  21457. + if (unlikely(!d_is_dir(dentry)))
  21458. + goto out_unlock; /* possible? */
  21459. +
  21460. + err = -ENOMEM;
  21461. + args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
  21462. + if (unlikely(!args))
  21463. + goto out_unlock;
  21464. +
  21465. + parent = dentry->d_parent; /* dir inode is locked */
  21466. + di_write_lock_parent(parent);
  21467. + err = au_test_empty(dentry, &args->whlist);
  21468. + if (unlikely(err))
  21469. + goto out_parent;
  21470. +
  21471. + btop = au_dbtop(dentry);
  21472. + bwh = au_dbwh(dentry);
  21473. + bindex = -1;
  21474. + wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
  21475. + &a->pin);
  21476. + err = PTR_ERR(wh_dentry);
  21477. + if (IS_ERR(wh_dentry))
  21478. + goto out_parent;
  21479. +
  21480. + h_dentry = au_h_dptr(dentry, btop);
  21481. + dget(h_dentry);
  21482. + rmdir_later = 0;
  21483. + if (bindex == btop) {
  21484. + err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
  21485. + if (err > 0) {
  21486. + rmdir_later = err;
  21487. + err = 0;
  21488. + }
  21489. + } else {
  21490. + /* stop monitoring */
  21491. + au_hn_free(au_hi(inode, btop));
  21492. +
  21493. + /* dir inode is locked */
  21494. + IMustLock(d_inode(wh_dentry->d_parent));
  21495. + err = 0;
  21496. + }
  21497. +
  21498. + if (!err) {
  21499. + vfsub_dead_dir(inode);
  21500. + au_set_dbdiropq(dentry, -1);
  21501. + epilog(dir, dentry, bindex);
  21502. +
  21503. + if (rmdir_later) {
  21504. + au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
  21505. + args = NULL;
  21506. + }
  21507. +
  21508. + goto out_unpin; /* success */
  21509. + }
  21510. +
  21511. + /* revert */
  21512. + AuLabel(revert);
  21513. + if (wh_dentry) {
  21514. + int rerr;
  21515. +
  21516. + rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
  21517. + &a->dt);
  21518. + if (rerr)
  21519. + err = rerr;
  21520. + }
  21521. +
  21522. +out_unpin:
  21523. + au_unpin(&a->pin);
  21524. + dput(wh_dentry);
  21525. + dput(h_dentry);
  21526. +out_parent:
  21527. + di_write_unlock(parent);
  21528. + if (args)
  21529. + au_whtmp_rmdir_free(args);
  21530. +out_unlock:
  21531. + aufs_read_unlock(dentry, AuLock_DW);
  21532. +out_free:
  21533. + au_kfree_rcu(a);
  21534. +out:
  21535. + AuTraceErr(err);
  21536. + return err;
  21537. +}
  21538. diff -Naur null/fs/aufs/i_op_ren.c linux-5.1/fs/aufs/i_op_ren.c
  21539. --- /dev/null
  21540. +++ linux-5.1/fs/aufs/i_op_ren.c 2019-05-22 11:23:07.441140996 +0200
  21541. @@ -0,0 +1,1250 @@
  21542. +// SPDX-License-Identifier: GPL-2.0
  21543. +/*
  21544. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  21545. + *
  21546. + * This program, aufs is free software; you can redistribute it and/or modify
  21547. + * it under the terms of the GNU General Public License as published by
  21548. + * the Free Software Foundation; either version 2 of the License, or
  21549. + * (at your option) any later version.
  21550. + *
  21551. + * This program is distributed in the hope that it will be useful,
  21552. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21553. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21554. + * GNU General Public License for more details.
  21555. + *
  21556. + * You should have received a copy of the GNU General Public License
  21557. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21558. + */
  21559. +
  21560. +/*
  21561. + * inode operation (rename entry)
  21562. + * todo: this is crazy monster
  21563. + */
  21564. +
  21565. +#include <linux/iversion.h>
  21566. +#include "aufs.h"
  21567. +
  21568. +enum { AuSRC, AuDST, AuSrcDst };
  21569. +enum { AuPARENT, AuCHILD, AuParentChild };
  21570. +
  21571. +#define AuRen_ISDIR_SRC 1
  21572. +#define AuRen_ISDIR_DST (1 << 1)
  21573. +#define AuRen_ISSAMEDIR (1 << 2)
  21574. +#define AuRen_WHSRC (1 << 3)
  21575. +#define AuRen_WHDST (1 << 4)
  21576. +#define AuRen_MNT_WRITE (1 << 5)
  21577. +#define AuRen_DT_DSTDIR (1 << 6)
  21578. +#define AuRen_DIROPQ_SRC (1 << 7)
  21579. +#define AuRen_DIROPQ_DST (1 << 8)
  21580. +#define AuRen_DIRREN (1 << 9)
  21581. +#define AuRen_DROPPED_SRC (1 << 10)
  21582. +#define AuRen_DROPPED_DST (1 << 11)
  21583. +#define au_ftest_ren(flags, name) ((flags) & AuRen_##name)
  21584. +#define au_fset_ren(flags, name) \
  21585. + do { (flags) |= AuRen_##name; } while (0)
  21586. +#define au_fclr_ren(flags, name) \
  21587. + do { (flags) &= ~AuRen_##name; } while (0)
  21588. +
  21589. +#ifndef CONFIG_AUFS_DIRREN
  21590. +#undef AuRen_DIRREN
  21591. +#define AuRen_DIRREN 0
  21592. +#endif
  21593. +
  21594. +struct au_ren_args {
  21595. + struct {
  21596. + struct dentry *dentry, *h_dentry, *parent, *h_parent,
  21597. + *wh_dentry;
  21598. + struct inode *dir, *inode;
  21599. + struct au_hinode *hdir, *hinode;
  21600. + struct au_dtime dt[AuParentChild];
  21601. + aufs_bindex_t btop, bdiropq;
  21602. + } sd[AuSrcDst];
  21603. +
  21604. +#define src_dentry sd[AuSRC].dentry
  21605. +#define src_dir sd[AuSRC].dir
  21606. +#define src_inode sd[AuSRC].inode
  21607. +#define src_h_dentry sd[AuSRC].h_dentry
  21608. +#define src_parent sd[AuSRC].parent
  21609. +#define src_h_parent sd[AuSRC].h_parent
  21610. +#define src_wh_dentry sd[AuSRC].wh_dentry
  21611. +#define src_hdir sd[AuSRC].hdir
  21612. +#define src_hinode sd[AuSRC].hinode
  21613. +#define src_h_dir sd[AuSRC].hdir->hi_inode
  21614. +#define src_dt sd[AuSRC].dt
  21615. +#define src_btop sd[AuSRC].btop
  21616. +#define src_bdiropq sd[AuSRC].bdiropq
  21617. +
  21618. +#define dst_dentry sd[AuDST].dentry
  21619. +#define dst_dir sd[AuDST].dir
  21620. +#define dst_inode sd[AuDST].inode
  21621. +#define dst_h_dentry sd[AuDST].h_dentry
  21622. +#define dst_parent sd[AuDST].parent
  21623. +#define dst_h_parent sd[AuDST].h_parent
  21624. +#define dst_wh_dentry sd[AuDST].wh_dentry
  21625. +#define dst_hdir sd[AuDST].hdir
  21626. +#define dst_hinode sd[AuDST].hinode
  21627. +#define dst_h_dir sd[AuDST].hdir->hi_inode
  21628. +#define dst_dt sd[AuDST].dt
  21629. +#define dst_btop sd[AuDST].btop
  21630. +#define dst_bdiropq sd[AuDST].bdiropq
  21631. +
  21632. + struct dentry *h_trap;
  21633. + struct au_branch *br;
  21634. + struct path h_path;
  21635. + struct au_nhash whlist;
  21636. + aufs_bindex_t btgt, src_bwh;
  21637. +
  21638. + struct {
  21639. + unsigned short auren_flags;
  21640. + unsigned char flags; /* syscall parameter */
  21641. + unsigned char exchange;
  21642. + } __packed;
  21643. +
  21644. + struct au_whtmp_rmdir *thargs;
  21645. + struct dentry *h_dst;
  21646. + struct au_hinode *h_root;
  21647. +};
  21648. +
  21649. +/* ---------------------------------------------------------------------- */
  21650. +
  21651. +/*
  21652. + * functions for reverting.
  21653. + * when an error happened in a single rename systemcall, we should revert
  21654. + * everything as if nothing happened.
  21655. + * we don't need to revert the copied-up/down the parent dir since they are
  21656. + * harmless.
  21657. + */
  21658. +
  21659. +#define RevertFailure(fmt, ...) do { \
  21660. + AuIOErr("revert failure: " fmt " (%d, %d)\n", \
  21661. + ##__VA_ARGS__, err, rerr); \
  21662. + err = -EIO; \
  21663. +} while (0)
  21664. +
  21665. +static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
  21666. +{
  21667. + int rerr;
  21668. + struct dentry *d;
  21669. +#define src_or_dst(member) a->sd[idx].member
  21670. +
  21671. + d = src_or_dst(dentry); /* {src,dst}_dentry */
  21672. + au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
  21673. + rerr = au_diropq_remove(d, a->btgt);
  21674. + au_hn_inode_unlock(src_or_dst(hinode));
  21675. + au_set_dbdiropq(d, src_or_dst(bdiropq));
  21676. + if (rerr)
  21677. + RevertFailure("remove diropq %pd", d);
  21678. +
  21679. +#undef src_or_dst_
  21680. +}
  21681. +
  21682. +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
  21683. +{
  21684. + if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
  21685. + au_ren_do_rev_diropq(err, a, AuSRC);
  21686. + if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
  21687. + au_ren_do_rev_diropq(err, a, AuDST);
  21688. +}
  21689. +
  21690. +static void au_ren_rev_rename(int err, struct au_ren_args *a)
  21691. +{
  21692. + int rerr;
  21693. + struct inode *delegated;
  21694. +
  21695. + a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name,
  21696. + a->src_h_parent);
  21697. + rerr = PTR_ERR(a->h_path.dentry);
  21698. + if (IS_ERR(a->h_path.dentry)) {
  21699. + RevertFailure("lkup one %pd", a->src_dentry);
  21700. + return;
  21701. + }
  21702. +
  21703. + delegated = NULL;
  21704. + rerr = vfsub_rename(a->dst_h_dir,
  21705. + au_h_dptr(a->src_dentry, a->btgt),
  21706. + a->src_h_dir, &a->h_path, &delegated, a->flags);
  21707. + if (unlikely(rerr == -EWOULDBLOCK)) {
  21708. + pr_warn("cannot retry for NFSv4 delegation"
  21709. + " for an internal rename\n");
  21710. + iput(delegated);
  21711. + }
  21712. + d_drop(a->h_path.dentry);
  21713. + dput(a->h_path.dentry);
  21714. + /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
  21715. + if (rerr)
  21716. + RevertFailure("rename %pd", a->src_dentry);
  21717. +}
  21718. +
  21719. +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
  21720. +{
  21721. + int rerr;
  21722. + struct inode *delegated;
  21723. +
  21724. + a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name,
  21725. + a->dst_h_parent);
  21726. + rerr = PTR_ERR(a->h_path.dentry);
  21727. + if (IS_ERR(a->h_path.dentry)) {
  21728. + RevertFailure("lkup one %pd", a->dst_dentry);
  21729. + return;
  21730. + }
  21731. + if (d_is_positive(a->h_path.dentry)) {
  21732. + d_drop(a->h_path.dentry);
  21733. + dput(a->h_path.dentry);
  21734. + return;
  21735. + }
  21736. +
  21737. + delegated = NULL;
  21738. + rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
  21739. + &delegated, a->flags);
  21740. + if (unlikely(rerr == -EWOULDBLOCK)) {
  21741. + pr_warn("cannot retry for NFSv4 delegation"
  21742. + " for an internal rename\n");
  21743. + iput(delegated);
  21744. + }
  21745. + d_drop(a->h_path.dentry);
  21746. + dput(a->h_path.dentry);
  21747. + if (!rerr)
  21748. + au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
  21749. + else
  21750. + RevertFailure("rename %pd", a->h_dst);
  21751. +}
  21752. +
  21753. +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
  21754. +{
  21755. + int rerr;
  21756. +
  21757. + a->h_path.dentry = a->src_wh_dentry;
  21758. + rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
  21759. + au_set_dbwh(a->src_dentry, a->src_bwh);
  21760. + if (rerr)
  21761. + RevertFailure("unlink %pd", a->src_wh_dentry);
  21762. +}
  21763. +#undef RevertFailure
  21764. +
  21765. +/* ---------------------------------------------------------------------- */
  21766. +
  21767. +/*
  21768. + * when we have to copyup the renaming entry, do it with the rename-target name
  21769. + * in order to minimize the cost (the later actual rename is unnecessary).
  21770. + * otherwise rename it on the target branch.
  21771. + */
  21772. +static int au_ren_or_cpup(struct au_ren_args *a)
  21773. +{
  21774. + int err;
  21775. + struct dentry *d;
  21776. + struct inode *delegated;
  21777. +
  21778. + d = a->src_dentry;
  21779. + if (au_dbtop(d) == a->btgt) {
  21780. + a->h_path.dentry = a->dst_h_dentry;
  21781. + AuDebugOn(au_dbtop(d) != a->btgt);
  21782. + delegated = NULL;
  21783. + err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
  21784. + a->dst_h_dir, &a->h_path, &delegated,
  21785. + a->flags);
  21786. + if (unlikely(err == -EWOULDBLOCK)) {
  21787. + pr_warn("cannot retry for NFSv4 delegation"
  21788. + " for an internal rename\n");
  21789. + iput(delegated);
  21790. + }
  21791. + } else
  21792. + BUG();
  21793. +
  21794. + if (!err && a->h_dst)
  21795. + /* it will be set to dinfo later */
  21796. + dget(a->h_dst);
  21797. +
  21798. + return err;
  21799. +}
  21800. +
  21801. +/* cf. aufs_rmdir() */
  21802. +static int au_ren_del_whtmp(struct au_ren_args *a)
  21803. +{
  21804. + int err;
  21805. + struct inode *dir;
  21806. +
  21807. + dir = a->dst_dir;
  21808. + SiMustAnyLock(dir->i_sb);
  21809. + if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
  21810. + au_sbi(dir->i_sb)->si_dirwh)
  21811. + || au_test_fs_remote(a->h_dst->d_sb)) {
  21812. + err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
  21813. + if (unlikely(err))
  21814. + pr_warn("failed removing whtmp dir %pd (%d), "
  21815. + "ignored.\n", a->h_dst, err);
  21816. + } else {
  21817. + au_nhash_wh_free(&a->thargs->whlist);
  21818. + a->thargs->whlist = a->whlist;
  21819. + a->whlist.nh_num = 0;
  21820. + au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
  21821. + dput(a->h_dst);
  21822. + a->thargs = NULL;
  21823. + }
  21824. +
  21825. + return 0;
  21826. +}
  21827. +
  21828. +/* make it 'opaque' dir. */
  21829. +static int au_ren_do_diropq(struct au_ren_args *a, int idx)
  21830. +{
  21831. + int err;
  21832. + struct dentry *d, *diropq;
  21833. +#define src_or_dst(member) a->sd[idx].member
  21834. +
  21835. + err = 0;
  21836. + d = src_or_dst(dentry); /* {src,dst}_dentry */
  21837. + src_or_dst(bdiropq) = au_dbdiropq(d);
  21838. + src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
  21839. + au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
  21840. + diropq = au_diropq_create(d, a->btgt);
  21841. + au_hn_inode_unlock(src_or_dst(hinode));
  21842. + if (IS_ERR(diropq))
  21843. + err = PTR_ERR(diropq);
  21844. + else
  21845. + dput(diropq);
  21846. +
  21847. +#undef src_or_dst_
  21848. + return err;
  21849. +}
  21850. +
  21851. +static int au_ren_diropq(struct au_ren_args *a)
  21852. +{
  21853. + int err;
  21854. + unsigned char always;
  21855. + struct dentry *d;
  21856. +
  21857. + err = 0;
  21858. + d = a->dst_dentry; /* already renamed on the branch */
  21859. + always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
  21860. + if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
  21861. + && !au_ftest_ren(a->auren_flags, DIRREN)
  21862. + && a->btgt != au_dbdiropq(a->src_dentry)
  21863. + && (a->dst_wh_dentry
  21864. + || a->btgt <= au_dbdiropq(d)
  21865. + /* hide the lower to keep xino */
  21866. + /* the lowers may not be a dir, but we hide them anyway */
  21867. + || a->btgt < au_dbbot(d)
  21868. + || always)) {
  21869. + AuDbg("here\n");
  21870. + err = au_ren_do_diropq(a, AuSRC);
  21871. + if (unlikely(err))
  21872. + goto out;
  21873. + au_fset_ren(a->auren_flags, DIROPQ_SRC);
  21874. + }
  21875. + if (!a->exchange)
  21876. + goto out; /* success */
  21877. +
  21878. + d = a->src_dentry; /* already renamed on the branch */
  21879. + if (au_ftest_ren(a->auren_flags, ISDIR_DST)
  21880. + && a->btgt != au_dbdiropq(a->dst_dentry)
  21881. + && (a->btgt < au_dbdiropq(d)
  21882. + || a->btgt < au_dbbot(d)
  21883. + || always)) {
  21884. + AuDbgDentry(a->src_dentry);
  21885. + AuDbgDentry(a->dst_dentry);
  21886. + err = au_ren_do_diropq(a, AuDST);
  21887. + if (unlikely(err))
  21888. + goto out_rev_src;
  21889. + au_fset_ren(a->auren_flags, DIROPQ_DST);
  21890. + }
  21891. + goto out; /* success */
  21892. +
  21893. +out_rev_src:
  21894. + AuDbg("err %d, reverting src\n", err);
  21895. + au_ren_rev_diropq(err, a);
  21896. +out:
  21897. + return err;
  21898. +}
  21899. +
  21900. +static int do_rename(struct au_ren_args *a)
  21901. +{
  21902. + int err;
  21903. + struct dentry *d, *h_d;
  21904. +
  21905. + if (!a->exchange) {
  21906. + /* prepare workqueue args for asynchronous rmdir */
  21907. + h_d = a->dst_h_dentry;
  21908. + if (au_ftest_ren(a->auren_flags, ISDIR_DST)
  21909. + /* && !au_ftest_ren(a->auren_flags, DIRREN) */
  21910. + && d_is_positive(h_d)) {
  21911. + err = -ENOMEM;
  21912. + a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
  21913. + GFP_NOFS);
  21914. + if (unlikely(!a->thargs))
  21915. + goto out;
  21916. + a->h_dst = dget(h_d);
  21917. + }
  21918. +
  21919. + /* create whiteout for src_dentry */
  21920. + if (au_ftest_ren(a->auren_flags, WHSRC)) {
  21921. + a->src_bwh = au_dbwh(a->src_dentry);
  21922. + AuDebugOn(a->src_bwh >= 0);
  21923. + a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
  21924. + a->src_h_parent);
  21925. + err = PTR_ERR(a->src_wh_dentry);
  21926. + if (IS_ERR(a->src_wh_dentry))
  21927. + goto out_thargs;
  21928. + }
  21929. +
  21930. + /* lookup whiteout for dentry */
  21931. + if (au_ftest_ren(a->auren_flags, WHDST)) {
  21932. + h_d = au_wh_lkup(a->dst_h_parent,
  21933. + &a->dst_dentry->d_name, a->br);
  21934. + err = PTR_ERR(h_d);
  21935. + if (IS_ERR(h_d))
  21936. + goto out_whsrc;
  21937. + if (d_is_negative(h_d))
  21938. + dput(h_d);
  21939. + else
  21940. + a->dst_wh_dentry = h_d;
  21941. + }
  21942. +
  21943. + /* rename dentry to tmpwh */
  21944. + if (a->thargs) {
  21945. + err = au_whtmp_ren(a->dst_h_dentry, a->br);
  21946. + if (unlikely(err))
  21947. + goto out_whdst;
  21948. +
  21949. + d = a->dst_dentry;
  21950. + au_set_h_dptr(d, a->btgt, NULL);
  21951. + err = au_lkup_neg(d, a->btgt, /*wh*/0);
  21952. + if (unlikely(err))
  21953. + goto out_whtmp;
  21954. + a->dst_h_dentry = au_h_dptr(d, a->btgt);
  21955. + }
  21956. + }
  21957. +
  21958. + BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
  21959. +#if 0
  21960. + BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
  21961. + && d_is_positive(a->dst_h_dentry)
  21962. + && a->src_btop != a->btgt);
  21963. +#endif
  21964. +
  21965. + /* rename by vfs_rename or cpup */
  21966. + err = au_ren_or_cpup(a);
  21967. + if (unlikely(err))
  21968. + /* leave the copied-up one */
  21969. + goto out_whtmp;
  21970. +
  21971. + /* make dir opaque */
  21972. + err = au_ren_diropq(a);
  21973. + if (unlikely(err))
  21974. + goto out_rename;
  21975. +
  21976. + /* update target timestamps */
  21977. + if (a->exchange) {
  21978. + AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
  21979. + a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
  21980. + vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
  21981. + a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
  21982. + }
  21983. + AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
  21984. + a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
  21985. + vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
  21986. + a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
  21987. +
  21988. + if (!a->exchange) {
  21989. + /* remove whiteout for dentry */
  21990. + if (a->dst_wh_dentry) {
  21991. + a->h_path.dentry = a->dst_wh_dentry;
  21992. + err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
  21993. + a->dst_dentry);
  21994. + if (unlikely(err))
  21995. + goto out_diropq;
  21996. + }
  21997. +
  21998. + /* remove whtmp */
  21999. + if (a->thargs)
  22000. + au_ren_del_whtmp(a); /* ignore this error */
  22001. +
  22002. + au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
  22003. + }
  22004. + err = 0;
  22005. + goto out_success;
  22006. +
  22007. +out_diropq:
  22008. + au_ren_rev_diropq(err, a);
  22009. +out_rename:
  22010. + au_ren_rev_rename(err, a);
  22011. + dput(a->h_dst);
  22012. +out_whtmp:
  22013. + if (a->thargs)
  22014. + au_ren_rev_whtmp(err, a);
  22015. +out_whdst:
  22016. + dput(a->dst_wh_dentry);
  22017. + a->dst_wh_dentry = NULL;
  22018. +out_whsrc:
  22019. + if (a->src_wh_dentry)
  22020. + au_ren_rev_whsrc(err, a);
  22021. +out_success:
  22022. + dput(a->src_wh_dentry);
  22023. + dput(a->dst_wh_dentry);
  22024. +out_thargs:
  22025. + if (a->thargs) {
  22026. + dput(a->h_dst);
  22027. + au_whtmp_rmdir_free(a->thargs);
  22028. + a->thargs = NULL;
  22029. + }
  22030. +out:
  22031. + return err;
  22032. +}
  22033. +
  22034. +/* ---------------------------------------------------------------------- */
  22035. +
  22036. +/*
  22037. + * test if @dentry dir can be rename destination or not.
  22038. + * success means, it is a logically empty dir.
  22039. + */
  22040. +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
  22041. +{
  22042. + return au_test_empty(dentry, whlist);
  22043. +}
  22044. +
  22045. +/*
  22046. + * test if @a->src_dentry dir can be rename source or not.
  22047. + * if it can, return 0.
  22048. + * success means,
  22049. + * - it is a logically empty dir.
  22050. + * - or, it exists on writable branch and has no children including whiteouts
  22051. + * on the lower branch unless DIRREN is on.
  22052. + */
  22053. +static int may_rename_srcdir(struct au_ren_args *a)
  22054. +{
  22055. + int err;
  22056. + unsigned int rdhash;
  22057. + aufs_bindex_t btop, btgt;
  22058. + struct dentry *dentry;
  22059. + struct super_block *sb;
  22060. + struct au_sbinfo *sbinfo;
  22061. +
  22062. + dentry = a->src_dentry;
  22063. + sb = dentry->d_sb;
  22064. + sbinfo = au_sbi(sb);
  22065. + if (au_opt_test(sbinfo->si_mntflags, DIRREN))
  22066. + au_fset_ren(a->auren_flags, DIRREN);
  22067. +
  22068. + btgt = a->btgt;
  22069. + btop = au_dbtop(dentry);
  22070. + if (btop != btgt) {
  22071. + struct au_nhash whlist;
  22072. +
  22073. + SiMustAnyLock(sb);
  22074. + rdhash = sbinfo->si_rdhash;
  22075. + if (!rdhash)
  22076. + rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
  22077. + dentry));
  22078. + err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
  22079. + if (unlikely(err))
  22080. + goto out;
  22081. + err = au_test_empty(dentry, &whlist);
  22082. + au_nhash_wh_free(&whlist);
  22083. + goto out;
  22084. + }
  22085. +
  22086. + if (btop == au_dbtaildir(dentry))
  22087. + return 0; /* success */
  22088. +
  22089. + err = au_test_empty_lower(dentry);
  22090. +
  22091. +out:
  22092. + if (err == -ENOTEMPTY) {
  22093. + if (au_ftest_ren(a->auren_flags, DIRREN)) {
  22094. + err = 0;
  22095. + } else {
  22096. + AuWarn1("renaming dir who has child(ren) on multiple "
  22097. + "branches, is not supported\n");
  22098. + err = -EXDEV;
  22099. + }
  22100. + }
  22101. + return err;
  22102. +}
  22103. +
  22104. +/* side effect: sets whlist and h_dentry */
  22105. +static int au_ren_may_dir(struct au_ren_args *a)
  22106. +{
  22107. + int err;
  22108. + unsigned int rdhash;
  22109. + struct dentry *d;
  22110. +
  22111. + d = a->dst_dentry;
  22112. + SiMustAnyLock(d->d_sb);
  22113. +
  22114. + err = 0;
  22115. + if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
  22116. + rdhash = au_sbi(d->d_sb)->si_rdhash;
  22117. + if (!rdhash)
  22118. + rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
  22119. + err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
  22120. + if (unlikely(err))
  22121. + goto out;
  22122. +
  22123. + if (!a->exchange) {
  22124. + au_set_dbtop(d, a->dst_btop);
  22125. + err = may_rename_dstdir(d, &a->whlist);
  22126. + au_set_dbtop(d, a->btgt);
  22127. + } else
  22128. + err = may_rename_srcdir(a);
  22129. + }
  22130. + a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
  22131. + if (unlikely(err))
  22132. + goto out;
  22133. +
  22134. + d = a->src_dentry;
  22135. + a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
  22136. + if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
  22137. + err = may_rename_srcdir(a);
  22138. + if (unlikely(err)) {
  22139. + au_nhash_wh_free(&a->whlist);
  22140. + a->whlist.nh_num = 0;
  22141. + }
  22142. + }
  22143. +out:
  22144. + return err;
  22145. +}
  22146. +
  22147. +/* ---------------------------------------------------------------------- */
  22148. +
  22149. +/*
  22150. + * simple tests for rename.
  22151. + * following the checks in vfs, plus the parent-child relationship.
  22152. + */
  22153. +static int au_may_ren(struct au_ren_args *a)
  22154. +{
  22155. + int err, isdir;
  22156. + struct inode *h_inode;
  22157. +
  22158. + if (a->src_btop == a->btgt) {
  22159. + err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
  22160. + au_ftest_ren(a->auren_flags, ISDIR_SRC));
  22161. + if (unlikely(err))
  22162. + goto out;
  22163. + err = -EINVAL;
  22164. + if (unlikely(a->src_h_dentry == a->h_trap))
  22165. + goto out;
  22166. + }
  22167. +
  22168. + err = 0;
  22169. + if (a->dst_btop != a->btgt)
  22170. + goto out;
  22171. +
  22172. + err = -ENOTEMPTY;
  22173. + if (unlikely(a->dst_h_dentry == a->h_trap))
  22174. + goto out;
  22175. +
  22176. + err = -EIO;
  22177. + isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
  22178. + if (d_really_is_negative(a->dst_dentry)) {
  22179. + if (d_is_negative(a->dst_h_dentry))
  22180. + err = au_may_add(a->dst_dentry, a->btgt,
  22181. + a->dst_h_parent, isdir);
  22182. + } else {
  22183. + if (unlikely(d_is_negative(a->dst_h_dentry)))
  22184. + goto out;
  22185. + h_inode = d_inode(a->dst_h_dentry);
  22186. + if (h_inode->i_nlink)
  22187. + err = au_may_del(a->dst_dentry, a->btgt,
  22188. + a->dst_h_parent, isdir);
  22189. + }
  22190. +
  22191. +out:
  22192. + if (unlikely(err == -ENOENT || err == -EEXIST))
  22193. + err = -EIO;
  22194. + AuTraceErr(err);
  22195. + return err;
  22196. +}
  22197. +
  22198. +/* ---------------------------------------------------------------------- */
  22199. +
  22200. +/*
  22201. + * locking order
  22202. + * (VFS)
  22203. + * - src_dir and dir by lock_rename()
  22204. + * - inode if exists
  22205. + * (aufs)
  22206. + * - lock all
  22207. + * + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
  22208. + * + si_read_lock
  22209. + * + di_write_lock2_child()
  22210. + * + di_write_lock_child()
  22211. + * + ii_write_lock_child()
  22212. + * + di_write_lock_child2()
  22213. + * + ii_write_lock_child2()
  22214. + * + src_parent and parent
  22215. + * + di_write_lock_parent()
  22216. + * + ii_write_lock_parent()
  22217. + * + di_write_lock_parent2()
  22218. + * + ii_write_lock_parent2()
  22219. + * + lower src_dir and dir by vfsub_lock_rename()
  22220. + * + verify the every relationships between child and parent. if any
  22221. + * of them failed, unlock all and return -EBUSY.
  22222. + */
  22223. +static void au_ren_unlock(struct au_ren_args *a)
  22224. +{
  22225. + vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
  22226. + a->dst_h_parent, a->dst_hdir);
  22227. + if (au_ftest_ren(a->auren_flags, DIRREN)
  22228. + && a->h_root)
  22229. + au_hn_inode_unlock(a->h_root);
  22230. + if (au_ftest_ren(a->auren_flags, MNT_WRITE))
  22231. + vfsub_mnt_drop_write(au_br_mnt(a->br));
  22232. +}
  22233. +
  22234. +static int au_ren_lock(struct au_ren_args *a)
  22235. +{
  22236. + int err;
  22237. + unsigned int udba;
  22238. +
  22239. + err = 0;
  22240. + a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
  22241. + a->src_hdir = au_hi(a->src_dir, a->btgt);
  22242. + a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
  22243. + a->dst_hdir = au_hi(a->dst_dir, a->btgt);
  22244. +
  22245. + err = vfsub_mnt_want_write(au_br_mnt(a->br));
  22246. + if (unlikely(err))
  22247. + goto out;
  22248. + au_fset_ren(a->auren_flags, MNT_WRITE);
  22249. + if (au_ftest_ren(a->auren_flags, DIRREN)) {
  22250. + struct dentry *root;
  22251. + struct inode *dir;
  22252. +
  22253. + /*
  22254. + * sbinfo is already locked, so this ii_read_lock is
  22255. + * unnecessary. but our debugging feature checks it.
  22256. + */
  22257. + root = a->src_inode->i_sb->s_root;
  22258. + if (root != a->src_parent && root != a->dst_parent) {
  22259. + dir = d_inode(root);
  22260. + ii_read_lock_parent3(dir);
  22261. + a->h_root = au_hi(dir, a->btgt);
  22262. + ii_read_unlock(dir);
  22263. + au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
  22264. + }
  22265. + }
  22266. + a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
  22267. + a->dst_h_parent, a->dst_hdir);
  22268. + udba = au_opt_udba(a->src_dentry->d_sb);
  22269. + if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
  22270. + || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
  22271. + err = au_busy_or_stale();
  22272. + if (!err && au_dbtop(a->src_dentry) == a->btgt)
  22273. + err = au_h_verify(a->src_h_dentry, udba,
  22274. + d_inode(a->src_h_parent), a->src_h_parent,
  22275. + a->br);
  22276. + if (!err && au_dbtop(a->dst_dentry) == a->btgt)
  22277. + err = au_h_verify(a->dst_h_dentry, udba,
  22278. + d_inode(a->dst_h_parent), a->dst_h_parent,
  22279. + a->br);
  22280. + if (!err)
  22281. + goto out; /* success */
  22282. +
  22283. + err = au_busy_or_stale();
  22284. + au_ren_unlock(a);
  22285. +
  22286. +out:
  22287. + return err;
  22288. +}
  22289. +
  22290. +/* ---------------------------------------------------------------------- */
  22291. +
  22292. +static void au_ren_refresh_dir(struct au_ren_args *a)
  22293. +{
  22294. + struct inode *dir;
  22295. +
  22296. + dir = a->dst_dir;
  22297. + inode_inc_iversion(dir);
  22298. + if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
  22299. + /* is this updating defined in POSIX? */
  22300. + au_cpup_attr_timesizes(a->src_inode);
  22301. + au_cpup_attr_nlink(dir, /*force*/1);
  22302. + }
  22303. + au_dir_ts(dir, a->btgt);
  22304. +
  22305. + if (a->exchange) {
  22306. + dir = a->src_dir;
  22307. + inode_inc_iversion(dir);
  22308. + if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
  22309. + /* is this updating defined in POSIX? */
  22310. + au_cpup_attr_timesizes(a->dst_inode);
  22311. + au_cpup_attr_nlink(dir, /*force*/1);
  22312. + }
  22313. + au_dir_ts(dir, a->btgt);
  22314. + }
  22315. +
  22316. + if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
  22317. + return;
  22318. +
  22319. + dir = a->src_dir;
  22320. + inode_inc_iversion(dir);
  22321. + if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
  22322. + au_cpup_attr_nlink(dir, /*force*/1);
  22323. + au_dir_ts(dir, a->btgt);
  22324. +}
  22325. +
  22326. +static void au_ren_refresh(struct au_ren_args *a)
  22327. +{
  22328. + aufs_bindex_t bbot, bindex;
  22329. + struct dentry *d, *h_d;
  22330. + struct inode *i, *h_i;
  22331. + struct super_block *sb;
  22332. +
  22333. + d = a->dst_dentry;
  22334. + d_drop(d);
  22335. + if (a->h_dst)
  22336. + /* already dget-ed by au_ren_or_cpup() */
  22337. + au_set_h_dptr(d, a->btgt, a->h_dst);
  22338. +
  22339. + i = a->dst_inode;
  22340. + if (i) {
  22341. + if (!a->exchange) {
  22342. + if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
  22343. + vfsub_drop_nlink(i);
  22344. + else {
  22345. + vfsub_dead_dir(i);
  22346. + au_cpup_attr_timesizes(i);
  22347. + }
  22348. + au_update_dbrange(d, /*do_put_zero*/1);
  22349. + } else
  22350. + au_cpup_attr_nlink(i, /*force*/1);
  22351. + } else {
  22352. + bbot = a->btgt;
  22353. + for (bindex = au_dbtop(d); bindex < bbot; bindex++)
  22354. + au_set_h_dptr(d, bindex, NULL);
  22355. + bbot = au_dbbot(d);
  22356. + for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
  22357. + au_set_h_dptr(d, bindex, NULL);
  22358. + au_update_dbrange(d, /*do_put_zero*/0);
  22359. + }
  22360. +
  22361. + if (a->exchange
  22362. + || au_ftest_ren(a->auren_flags, DIRREN)) {
  22363. + d_drop(a->src_dentry);
  22364. + if (au_ftest_ren(a->auren_flags, DIRREN))
  22365. + au_set_dbwh(a->src_dentry, -1);
  22366. + return;
  22367. + }
  22368. +
  22369. + d = a->src_dentry;
  22370. + au_set_dbwh(d, -1);
  22371. + bbot = au_dbbot(d);
  22372. + for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
  22373. + h_d = au_h_dptr(d, bindex);
  22374. + if (h_d)
  22375. + au_set_h_dptr(d, bindex, NULL);
  22376. + }
  22377. + au_set_dbbot(d, a->btgt);
  22378. +
  22379. + sb = d->d_sb;
  22380. + i = a->src_inode;
  22381. + if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
  22382. + return; /* success */
  22383. +
  22384. + bbot = au_ibbot(i);
  22385. + for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
  22386. + h_i = au_h_iptr(i, bindex);
  22387. + if (h_i) {
  22388. + au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
  22389. + /* ignore this error */
  22390. + au_set_h_iptr(i, bindex, NULL, 0);
  22391. + }
  22392. + }
  22393. + au_set_ibbot(i, a->btgt);
  22394. +}
  22395. +
  22396. +/* ---------------------------------------------------------------------- */
  22397. +
  22398. +/* mainly for link(2) and rename(2) */
  22399. +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
  22400. +{
  22401. + aufs_bindex_t bdiropq, bwh;
  22402. + struct dentry *parent;
  22403. + struct au_branch *br;
  22404. +
  22405. + parent = dentry->d_parent;
  22406. + IMustLock(d_inode(parent)); /* dir is locked */
  22407. +
  22408. + bdiropq = au_dbdiropq(parent);
  22409. + bwh = au_dbwh(dentry);
  22410. + br = au_sbr(dentry->d_sb, btgt);
  22411. + if (au_br_rdonly(br)
  22412. + || (0 <= bdiropq && bdiropq < btgt)
  22413. + || (0 <= bwh && bwh < btgt))
  22414. + btgt = -1;
  22415. +
  22416. + AuDbg("btgt %d\n", btgt);
  22417. + return btgt;
  22418. +}
  22419. +
  22420. +/* sets src_btop, dst_btop and btgt */
  22421. +static int au_ren_wbr(struct au_ren_args *a)
  22422. +{
  22423. + int err;
  22424. + struct au_wr_dir_args wr_dir_args = {
  22425. + /* .force_btgt = -1, */
  22426. + .flags = AuWrDir_ADD_ENTRY
  22427. + };
  22428. +
  22429. + a->src_btop = au_dbtop(a->src_dentry);
  22430. + a->dst_btop = au_dbtop(a->dst_dentry);
  22431. + if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
  22432. + || au_ftest_ren(a->auren_flags, ISDIR_DST))
  22433. + au_fset_wrdir(wr_dir_args.flags, ISDIR);
  22434. + wr_dir_args.force_btgt = a->src_btop;
  22435. + if (a->dst_inode && a->dst_btop < a->src_btop)
  22436. + wr_dir_args.force_btgt = a->dst_btop;
  22437. + wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
  22438. + err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
  22439. + a->btgt = err;
  22440. + if (a->exchange)
  22441. + au_update_dbtop(a->dst_dentry);
  22442. +
  22443. + return err;
  22444. +}
  22445. +
  22446. +static void au_ren_dt(struct au_ren_args *a)
  22447. +{
  22448. + a->h_path.dentry = a->src_h_parent;
  22449. + au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
  22450. + if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
  22451. + a->h_path.dentry = a->dst_h_parent;
  22452. + au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
  22453. + }
  22454. +
  22455. + au_fclr_ren(a->auren_flags, DT_DSTDIR);
  22456. + if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
  22457. + && !a->exchange)
  22458. + return;
  22459. +
  22460. + a->h_path.dentry = a->src_h_dentry;
  22461. + au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
  22462. + if (d_is_positive(a->dst_h_dentry)) {
  22463. + au_fset_ren(a->auren_flags, DT_DSTDIR);
  22464. + a->h_path.dentry = a->dst_h_dentry;
  22465. + au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
  22466. + }
  22467. +}
  22468. +
  22469. +static void au_ren_rev_dt(int err, struct au_ren_args *a)
  22470. +{
  22471. + struct dentry *h_d;
  22472. + struct inode *h_inode;
  22473. +
  22474. + au_dtime_revert(a->src_dt + AuPARENT);
  22475. + if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
  22476. + au_dtime_revert(a->dst_dt + AuPARENT);
  22477. +
  22478. + if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
  22479. + h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
  22480. + h_inode = d_inode(h_d);
  22481. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  22482. + au_dtime_revert(a->src_dt + AuCHILD);
  22483. + inode_unlock(h_inode);
  22484. +
  22485. + if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
  22486. + h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
  22487. + h_inode = d_inode(h_d);
  22488. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  22489. + au_dtime_revert(a->dst_dt + AuCHILD);
  22490. + inode_unlock(h_inode);
  22491. + }
  22492. + }
  22493. +}
  22494. +
  22495. +/* ---------------------------------------------------------------------- */
  22496. +
  22497. +int aufs_rename(struct inode *_src_dir, struct dentry *_src_dentry,
  22498. + struct inode *_dst_dir, struct dentry *_dst_dentry,
  22499. + unsigned int _flags)
  22500. +{
  22501. + int err, lock_flags;
  22502. + void *rev;
  22503. + /* reduce stack space */
  22504. + struct au_ren_args *a;
  22505. + struct au_pin pin;
  22506. +
  22507. + AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
  22508. + IMustLock(_src_dir);
  22509. + IMustLock(_dst_dir);
  22510. +
  22511. + err = -EINVAL;
  22512. + if (unlikely(_flags & RENAME_WHITEOUT))
  22513. + goto out;
  22514. +
  22515. + err = -ENOMEM;
  22516. + BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
  22517. + a = kzalloc(sizeof(*a), GFP_NOFS);
  22518. + if (unlikely(!a))
  22519. + goto out;
  22520. +
  22521. + a->flags = _flags;
  22522. + BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
  22523. + && RENAME_EXCHANGE > U8_MAX);
  22524. + a->exchange = _flags & RENAME_EXCHANGE;
  22525. + a->src_dir = _src_dir;
  22526. + a->src_dentry = _src_dentry;
  22527. + a->src_inode = NULL;
  22528. + if (d_really_is_positive(a->src_dentry))
  22529. + a->src_inode = d_inode(a->src_dentry);
  22530. + a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
  22531. + a->dst_dir = _dst_dir;
  22532. + a->dst_dentry = _dst_dentry;
  22533. + a->dst_inode = NULL;
  22534. + if (d_really_is_positive(a->dst_dentry))
  22535. + a->dst_inode = d_inode(a->dst_dentry);
  22536. + a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
  22537. + if (a->dst_inode) {
  22538. + /*
  22539. + * if EXCHANGE && src is non-dir && dst is dir,
  22540. + * dst is not locked.
  22541. + */
  22542. + /* IMustLock(a->dst_inode); */
  22543. + au_igrab(a->dst_inode);
  22544. + }
  22545. +
  22546. + err = -ENOTDIR;
  22547. + lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
  22548. + if (d_is_dir(a->src_dentry)) {
  22549. + au_fset_ren(a->auren_flags, ISDIR_SRC);
  22550. + if (unlikely(!a->exchange
  22551. + && d_really_is_positive(a->dst_dentry)
  22552. + && !d_is_dir(a->dst_dentry)))
  22553. + goto out_free;
  22554. + lock_flags |= AuLock_DIRS;
  22555. + }
  22556. + if (a->dst_inode && d_is_dir(a->dst_dentry)) {
  22557. + au_fset_ren(a->auren_flags, ISDIR_DST);
  22558. + if (unlikely(!a->exchange
  22559. + && d_really_is_positive(a->src_dentry)
  22560. + && !d_is_dir(a->src_dentry)))
  22561. + goto out_free;
  22562. + lock_flags |= AuLock_DIRS;
  22563. + }
  22564. + err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
  22565. + lock_flags);
  22566. + if (unlikely(err))
  22567. + goto out_free;
  22568. +
  22569. + err = au_d_hashed_positive(a->src_dentry);
  22570. + if (unlikely(err))
  22571. + goto out_unlock;
  22572. + err = -ENOENT;
  22573. + if (a->dst_inode) {
  22574. + /*
  22575. + * If it is a dir, VFS unhash it before this
  22576. + * function. It means we cannot rely upon d_unhashed().
  22577. + */
  22578. + if (unlikely(!a->dst_inode->i_nlink))
  22579. + goto out_unlock;
  22580. + if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
  22581. + err = au_d_hashed_positive(a->dst_dentry);
  22582. + if (unlikely(err && !a->exchange))
  22583. + goto out_unlock;
  22584. + } else if (unlikely(IS_DEADDIR(a->dst_inode)))
  22585. + goto out_unlock;
  22586. + } else if (unlikely(d_unhashed(a->dst_dentry)))
  22587. + goto out_unlock;
  22588. +
  22589. + /*
  22590. + * is it possible?
  22591. + * yes, it happened (in linux-3.3-rcN) but I don't know why.
  22592. + * there may exist a problem somewhere else.
  22593. + */
  22594. + err = -EINVAL;
  22595. + if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
  22596. + goto out_unlock;
  22597. +
  22598. + au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
  22599. + di_write_lock_parent(a->dst_parent);
  22600. +
  22601. + /* which branch we process */
  22602. + err = au_ren_wbr(a);
  22603. + if (unlikely(err < 0))
  22604. + goto out_parent;
  22605. + a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
  22606. + a->h_path.mnt = au_br_mnt(a->br);
  22607. +
  22608. + /* are they available to be renamed */
  22609. + err = au_ren_may_dir(a);
  22610. + if (unlikely(err))
  22611. + goto out_children;
  22612. +
  22613. + /* prepare the writable parent dir on the same branch */
  22614. + if (a->dst_btop == a->btgt) {
  22615. + au_fset_ren(a->auren_flags, WHDST);
  22616. + } else {
  22617. + err = au_cpup_dirs(a->dst_dentry, a->btgt);
  22618. + if (unlikely(err))
  22619. + goto out_children;
  22620. + }
  22621. +
  22622. + err = 0;
  22623. + if (!a->exchange) {
  22624. + if (a->src_dir != a->dst_dir) {
  22625. + /*
  22626. + * this temporary unlock is safe,
  22627. + * because both dir->i_mutex are locked.
  22628. + */
  22629. + di_write_unlock(a->dst_parent);
  22630. + di_write_lock_parent(a->src_parent);
  22631. + err = au_wr_dir_need_wh(a->src_dentry,
  22632. + au_ftest_ren(a->auren_flags,
  22633. + ISDIR_SRC),
  22634. + &a->btgt);
  22635. + di_write_unlock(a->src_parent);
  22636. + di_write_lock2_parent(a->src_parent, a->dst_parent,
  22637. + /*isdir*/1);
  22638. + au_fclr_ren(a->auren_flags, ISSAMEDIR);
  22639. + } else
  22640. + err = au_wr_dir_need_wh(a->src_dentry,
  22641. + au_ftest_ren(a->auren_flags,
  22642. + ISDIR_SRC),
  22643. + &a->btgt);
  22644. + }
  22645. + if (unlikely(err < 0))
  22646. + goto out_children;
  22647. + if (err)
  22648. + au_fset_ren(a->auren_flags, WHSRC);
  22649. +
  22650. + /* cpup src */
  22651. + if (a->src_btop != a->btgt) {
  22652. + err = au_pin(&pin, a->src_dentry, a->btgt,
  22653. + au_opt_udba(a->src_dentry->d_sb),
  22654. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  22655. + if (!err) {
  22656. + struct au_cp_generic cpg = {
  22657. + .dentry = a->src_dentry,
  22658. + .bdst = a->btgt,
  22659. + .bsrc = a->src_btop,
  22660. + .len = -1,
  22661. + .pin = &pin,
  22662. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  22663. + };
  22664. + AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
  22665. + err = au_sio_cpup_simple(&cpg);
  22666. + au_unpin(&pin);
  22667. + }
  22668. + if (unlikely(err))
  22669. + goto out_children;
  22670. + a->src_btop = a->btgt;
  22671. + a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
  22672. + if (!a->exchange)
  22673. + au_fset_ren(a->auren_flags, WHSRC);
  22674. + }
  22675. +
  22676. + /* cpup dst */
  22677. + if (a->exchange && a->dst_inode
  22678. + && a->dst_btop != a->btgt) {
  22679. + err = au_pin(&pin, a->dst_dentry, a->btgt,
  22680. + au_opt_udba(a->dst_dentry->d_sb),
  22681. + AuPin_DI_LOCKED | AuPin_MNT_WRITE);
  22682. + if (!err) {
  22683. + struct au_cp_generic cpg = {
  22684. + .dentry = a->dst_dentry,
  22685. + .bdst = a->btgt,
  22686. + .bsrc = a->dst_btop,
  22687. + .len = -1,
  22688. + .pin = &pin,
  22689. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  22690. + };
  22691. + err = au_sio_cpup_simple(&cpg);
  22692. + au_unpin(&pin);
  22693. + }
  22694. + if (unlikely(err))
  22695. + goto out_children;
  22696. + a->dst_btop = a->btgt;
  22697. + a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
  22698. + }
  22699. +
  22700. + /* lock them all */
  22701. + err = au_ren_lock(a);
  22702. + if (unlikely(err))
  22703. + /* leave the copied-up one */
  22704. + goto out_children;
  22705. +
  22706. + if (!a->exchange) {
  22707. + if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
  22708. + err = au_may_ren(a);
  22709. + else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
  22710. + err = -ENAMETOOLONG;
  22711. + if (unlikely(err))
  22712. + goto out_hdir;
  22713. + }
  22714. +
  22715. + /* store timestamps to be revertible */
  22716. + au_ren_dt(a);
  22717. +
  22718. + /* store dirren info */
  22719. + if (au_ftest_ren(a->auren_flags, DIRREN)) {
  22720. + err = au_dr_rename(a->src_dentry, a->btgt,
  22721. + &a->dst_dentry->d_name, &rev);
  22722. + AuTraceErr(err);
  22723. + if (unlikely(err))
  22724. + goto out_dt;
  22725. + }
  22726. +
  22727. + /* here we go */
  22728. + err = do_rename(a);
  22729. + if (unlikely(err))
  22730. + goto out_dirren;
  22731. +
  22732. + if (au_ftest_ren(a->auren_flags, DIRREN))
  22733. + au_dr_rename_fin(a->src_dentry, a->btgt, rev);
  22734. +
  22735. + /* update dir attributes */
  22736. + au_ren_refresh_dir(a);
  22737. +
  22738. + /* dput/iput all lower dentries */
  22739. + au_ren_refresh(a);
  22740. +
  22741. + goto out_hdir; /* success */
  22742. +
  22743. +out_dirren:
  22744. + if (au_ftest_ren(a->auren_flags, DIRREN))
  22745. + au_dr_rename_rev(a->src_dentry, a->btgt, rev);
  22746. +out_dt:
  22747. + au_ren_rev_dt(err, a);
  22748. +out_hdir:
  22749. + au_ren_unlock(a);
  22750. +out_children:
  22751. + au_nhash_wh_free(&a->whlist);
  22752. + if (err && a->dst_inode && a->dst_btop != a->btgt) {
  22753. + AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
  22754. + au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
  22755. + au_set_dbtop(a->dst_dentry, a->dst_btop);
  22756. + }
  22757. +out_parent:
  22758. + if (!err) {
  22759. + if (d_unhashed(a->src_dentry))
  22760. + au_fset_ren(a->auren_flags, DROPPED_SRC);
  22761. + if (d_unhashed(a->dst_dentry))
  22762. + au_fset_ren(a->auren_flags, DROPPED_DST);
  22763. + if (!a->exchange)
  22764. + d_move(a->src_dentry, a->dst_dentry);
  22765. + else {
  22766. + d_exchange(a->src_dentry, a->dst_dentry);
  22767. + if (au_ftest_ren(a->auren_flags, DROPPED_DST))
  22768. + d_drop(a->dst_dentry);
  22769. + }
  22770. + if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
  22771. + d_drop(a->src_dentry);
  22772. + } else {
  22773. + au_update_dbtop(a->dst_dentry);
  22774. + if (!a->dst_inode)
  22775. + d_drop(a->dst_dentry);
  22776. + }
  22777. + if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
  22778. + di_write_unlock(a->dst_parent);
  22779. + else
  22780. + di_write_unlock2(a->src_parent, a->dst_parent);
  22781. +out_unlock:
  22782. + aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
  22783. +out_free:
  22784. + iput(a->dst_inode);
  22785. + if (a->thargs)
  22786. + au_whtmp_rmdir_free(a->thargs);
  22787. + au_kfree_rcu(a);
  22788. +out:
  22789. + AuTraceErr(err);
  22790. + return err;
  22791. +}
  22792. diff -Naur null/fs/aufs/Kconfig linux-5.1/fs/aufs/Kconfig
  22793. --- /dev/null
  22794. +++ linux-5.1/fs/aufs/Kconfig 2019-05-22 11:23:07.437141180 +0200
  22795. @@ -0,0 +1,199 @@
  22796. +# SPDX-License-Identifier: GPL-2.0
  22797. +config AUFS_FS
  22798. + tristate "Aufs (Advanced multi layered unification filesystem) support"
  22799. + help
  22800. + Aufs is a stackable unification filesystem such as Unionfs,
  22801. + which unifies several directories and provides a merged single
  22802. + directory.
  22803. + In the early days, aufs was entirely re-designed and
  22804. + re-implemented Unionfs Version 1.x series. Introducing many
  22805. + original ideas, approaches and improvements, it becomes totally
  22806. + different from Unionfs while keeping the basic features.
  22807. +
  22808. +if AUFS_FS
  22809. +choice
  22810. + prompt "Maximum number of branches"
  22811. + default AUFS_BRANCH_MAX_127
  22812. + help
  22813. + Specifies the maximum number of branches (or member directories)
  22814. + in a single aufs. The larger value consumes more system
  22815. + resources and has a minor impact to performance.
  22816. +config AUFS_BRANCH_MAX_127
  22817. + bool "127"
  22818. + help
  22819. + Specifies the maximum number of branches (or member directories)
  22820. + in a single aufs. The larger value consumes more system
  22821. + resources and has a minor impact to performance.
  22822. +config AUFS_BRANCH_MAX_511
  22823. + bool "511"
  22824. + help
  22825. + Specifies the maximum number of branches (or member directories)
  22826. + in a single aufs. The larger value consumes more system
  22827. + resources and has a minor impact to performance.
  22828. +config AUFS_BRANCH_MAX_1023
  22829. + bool "1023"
  22830. + help
  22831. + Specifies the maximum number of branches (or member directories)
  22832. + in a single aufs. The larger value consumes more system
  22833. + resources and has a minor impact to performance.
  22834. +config AUFS_BRANCH_MAX_32767
  22835. + bool "32767"
  22836. + help
  22837. + Specifies the maximum number of branches (or member directories)
  22838. + in a single aufs. The larger value consumes more system
  22839. + resources and has a minor impact to performance.
  22840. +endchoice
  22841. +
  22842. +config AUFS_SBILIST
  22843. + bool
  22844. + depends on AUFS_MAGIC_SYSRQ || PROC_FS
  22845. + default y
  22846. + help
  22847. + Automatic configuration for internal use.
  22848. + When aufs supports Magic SysRq or /proc, enabled automatically.
  22849. +
  22850. +config AUFS_HNOTIFY
  22851. + bool "Detect direct branch access (bypassing aufs)"
  22852. + help
  22853. + If you want to modify files on branches directly, eg. bypassing aufs,
  22854. + and want aufs to detect the changes of them fully, then enable this
  22855. + option and use 'udba=notify' mount option.
  22856. + Currently there is only one available configuration, "fsnotify".
  22857. + It will have a negative impact to the performance.
  22858. + See detail in aufs.5.
  22859. +
  22860. +choice
  22861. + prompt "method" if AUFS_HNOTIFY
  22862. + default AUFS_HFSNOTIFY
  22863. +config AUFS_HFSNOTIFY
  22864. + bool "fsnotify"
  22865. + select FSNOTIFY
  22866. +endchoice
  22867. +
  22868. +config AUFS_EXPORT
  22869. + bool "NFS-exportable aufs"
  22870. + depends on EXPORTFS
  22871. + help
  22872. + If you want to export your mounted aufs via NFS, then enable this
  22873. + option. There are several requirements for this configuration.
  22874. + See detail in aufs.5.
  22875. +
  22876. +config AUFS_INO_T_64
  22877. + bool
  22878. + depends on AUFS_EXPORT
  22879. + depends on 64BIT && !(ALPHA || S390)
  22880. + default y
  22881. + help
  22882. + Automatic configuration for internal use.
  22883. + /* typedef unsigned long/int __kernel_ino_t */
  22884. + /* alpha and s390x are int */
  22885. +
  22886. +config AUFS_XATTR
  22887. + bool "support for XATTR/EA (including Security Labels)"
  22888. + help
  22889. + If your branch fs supports XATTR/EA and you want to make them
  22890. + available in aufs too, then enable this opsion and specify the
  22891. + branch attributes for EA.
  22892. + See detail in aufs.5.
  22893. +
  22894. +config AUFS_FHSM
  22895. + bool "File-based Hierarchical Storage Management"
  22896. + help
  22897. + Hierarchical Storage Management (or HSM) is a well-known feature
  22898. + in the storage world. Aufs provides this feature as file-based.
  22899. + with multiple branches.
  22900. + These multiple branches are prioritized, ie. the topmost one
  22901. + should be the fastest drive and be used heavily.
  22902. +
  22903. +config AUFS_RDU
  22904. + bool "Readdir in userspace"
  22905. + help
  22906. + Aufs has two methods to provide a merged view for a directory,
  22907. + by a user-space library and by kernel-space natively. The latter
  22908. + is always enabled but sometimes large and slow.
  22909. + If you enable this option, install the library in aufs2-util
  22910. + package, and set some environment variables for your readdir(3),
  22911. + then the work will be handled in user-space which generally
  22912. + shows better performance in most cases.
  22913. + See detail in aufs.5.
  22914. +
  22915. +config AUFS_DIRREN
  22916. + bool "Workaround for rename(2)-ing a directory"
  22917. + help
  22918. + By default, aufs returns EXDEV error in renameing a dir who has
  22919. + his child on the lower branch, since it is a bad idea to issue
  22920. + rename(2) internally for every lower branch. But user may not
  22921. + accept this behaviour. So here is a workaround to allow such
  22922. + rename(2) and store some extra infromation on the writable
  22923. + branch. Obviously this costs high (and I don't like it).
  22924. + To use this feature, you need to enable this configuration AND
  22925. + to specify the mount option `dirren.'
  22926. + See details in aufs.5 and the design documents.
  22927. +
  22928. +config AUFS_SHWH
  22929. + bool "Show whiteouts"
  22930. + help
  22931. + If you want to make the whiteouts in aufs visible, then enable
  22932. + this option and specify 'shwh' mount option. Although it may
  22933. + sounds like philosophy or something, but in technically it
  22934. + simply shows the name of whiteout with keeping its behaviour.
  22935. +
  22936. +config AUFS_BR_RAMFS
  22937. + bool "Ramfs (initramfs/rootfs) as an aufs branch"
  22938. + help
  22939. + If you want to use ramfs as an aufs branch fs, then enable this
  22940. + option. Generally tmpfs is recommended.
  22941. + Aufs prohibited them to be a branch fs by default, because
  22942. + initramfs becomes unusable after switch_root or something
  22943. + generally. If you sets initramfs as an aufs branch and boot your
  22944. + system by switch_root, you will meet a problem easily since the
  22945. + files in initramfs may be inaccessible.
  22946. + Unless you are going to use ramfs as an aufs branch fs without
  22947. + switch_root or something, leave it N.
  22948. +
  22949. +config AUFS_BR_FUSE
  22950. + bool "Fuse fs as an aufs branch"
  22951. + depends on FUSE_FS
  22952. + select AUFS_POLL
  22953. + help
  22954. + If you want to use fuse-based userspace filesystem as an aufs
  22955. + branch fs, then enable this option.
  22956. + It implements the internal poll(2) operation which is
  22957. + implemented by fuse only (curretnly).
  22958. +
  22959. +config AUFS_POLL
  22960. + bool
  22961. + help
  22962. + Automatic configuration for internal use.
  22963. +
  22964. +config AUFS_BR_HFSPLUS
  22965. + bool "Hfsplus as an aufs branch"
  22966. + depends on HFSPLUS_FS
  22967. + default y
  22968. + help
  22969. + If you want to use hfsplus fs as an aufs branch fs, then enable
  22970. + this option. This option introduces a small overhead at
  22971. + copying-up a file on hfsplus.
  22972. +
  22973. +config AUFS_BDEV_LOOP
  22974. + bool
  22975. + depends on BLK_DEV_LOOP
  22976. + default y
  22977. + help
  22978. + Automatic configuration for internal use.
  22979. + Convert =[ym] into =y.
  22980. +
  22981. +config AUFS_DEBUG
  22982. + bool "Debug aufs"
  22983. + help
  22984. + Enable this to compile aufs internal debug code.
  22985. + It will have a negative impact to the performance.
  22986. +
  22987. +config AUFS_MAGIC_SYSRQ
  22988. + bool
  22989. + depends on AUFS_DEBUG && MAGIC_SYSRQ
  22990. + default y
  22991. + help
  22992. + Automatic configuration for internal use.
  22993. + When aufs supports Magic SysRq, enabled automatically.
  22994. +endif
  22995. diff -Naur null/fs/aufs/lcnt.h linux-5.1/fs/aufs/lcnt.h
  22996. --- /dev/null
  22997. +++ linux-5.1/fs/aufs/lcnt.h 2019-05-22 11:23:07.441140996 +0200
  22998. @@ -0,0 +1,186 @@
  22999. +/* SPDX-License-Identifier: GPL-2.0 */
  23000. +/*
  23001. + * Copyright (C) 2018-2019 Junjiro R. Okajima
  23002. + *
  23003. + * This program, aufs is free software; you can redistribute it and/or modify
  23004. + * it under the terms of the GNU General Public License as published by
  23005. + * the Free Software Foundation; either version 2 of the License, or
  23006. + * (at your option) any later version.
  23007. + *
  23008. + * This program is distributed in the hope that it will be useful,
  23009. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23010. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23011. + * GNU General Public License for more details.
  23012. + *
  23013. + * You should have received a copy of the GNU General Public License
  23014. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23015. + */
  23016. +
  23017. +/*
  23018. + * simple long counter wrapper
  23019. + */
  23020. +
  23021. +#ifndef __AUFS_LCNT_H__
  23022. +#define __AUFS_LCNT_H__
  23023. +
  23024. +#ifdef __KERNEL__
  23025. +
  23026. +#include "debug.h"
  23027. +
  23028. +#define AuLCntATOMIC 1
  23029. +#define AuLCntPCPUCNT 2
  23030. +/*
  23031. + * why does percpu_refcount require extra synchronize_rcu()s in
  23032. + * au_br_do_free()
  23033. + */
  23034. +#define AuLCntPCPUREF 3
  23035. +
  23036. +/* #define AuLCntChosen AuLCntATOMIC */
  23037. +#define AuLCntChosen AuLCntPCPUCNT
  23038. +/* #define AuLCntChosen AuLCntPCPUREF */
  23039. +
  23040. +#if AuLCntChosen == AuLCntATOMIC
  23041. +#include <linux/atomic.h>
  23042. +
  23043. +typedef atomic_long_t au_lcnt_t;
  23044. +
  23045. +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
  23046. +{
  23047. + atomic_long_set(cnt, 0);
  23048. + return 0;
  23049. +}
  23050. +
  23051. +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
  23052. +{
  23053. + /* empty */
  23054. +}
  23055. +
  23056. +static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
  23057. + int do_sync __maybe_unused)
  23058. +{
  23059. + /* empty */
  23060. +}
  23061. +
  23062. +static inline void au_lcnt_inc(au_lcnt_t *cnt)
  23063. +{
  23064. + atomic_long_inc(cnt);
  23065. +}
  23066. +
  23067. +static inline void au_lcnt_dec(au_lcnt_t *cnt)
  23068. +{
  23069. + atomic_long_dec(cnt);
  23070. +}
  23071. +
  23072. +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
  23073. +{
  23074. + return atomic_long_read(cnt);
  23075. +}
  23076. +#endif
  23077. +
  23078. +#if AuLCntChosen == AuLCntPCPUCNT
  23079. +#include <linux/percpu_counter.h>
  23080. +
  23081. +typedef struct percpu_counter au_lcnt_t;
  23082. +
  23083. +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
  23084. +{
  23085. + return percpu_counter_init(cnt, 0, GFP_NOFS);
  23086. +}
  23087. +
  23088. +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
  23089. +{
  23090. + /* empty */
  23091. +}
  23092. +
  23093. +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
  23094. +{
  23095. + percpu_counter_destroy(cnt);
  23096. +}
  23097. +
  23098. +static inline void au_lcnt_inc(au_lcnt_t *cnt)
  23099. +{
  23100. + percpu_counter_inc(cnt);
  23101. +}
  23102. +
  23103. +static inline void au_lcnt_dec(au_lcnt_t *cnt)
  23104. +{
  23105. + percpu_counter_dec(cnt);
  23106. +}
  23107. +
  23108. +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
  23109. +{
  23110. + s64 n;
  23111. +
  23112. + n = percpu_counter_sum(cnt);
  23113. + BUG_ON(n < 0);
  23114. + if (LONG_MAX != LLONG_MAX
  23115. + && n > LONG_MAX)
  23116. + AuWarn1("%s\n", "wrap-around");
  23117. +
  23118. + return n;
  23119. +}
  23120. +#endif
  23121. +
  23122. +#if AuLCntChosen == AuLCntPCPUREF
  23123. +#include <linux/percpu-refcount.h>
  23124. +
  23125. +typedef struct percpu_ref au_lcnt_t;
  23126. +
  23127. +static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
  23128. +{
  23129. + if (!release)
  23130. + release = percpu_ref_exit;
  23131. + return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
  23132. +}
  23133. +
  23134. +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
  23135. +{
  23136. + synchronize_rcu();
  23137. +}
  23138. +
  23139. +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
  23140. +{
  23141. + percpu_ref_kill(cnt);
  23142. + if (do_sync)
  23143. + au_lcnt_wait_for_fin(cnt);
  23144. +}
  23145. +
  23146. +static inline void au_lcnt_inc(au_lcnt_t *cnt)
  23147. +{
  23148. + percpu_ref_get(cnt);
  23149. +}
  23150. +
  23151. +static inline void au_lcnt_dec(au_lcnt_t *cnt)
  23152. +{
  23153. + percpu_ref_put(cnt);
  23154. +}
  23155. +
  23156. +/*
  23157. + * avoid calling this func as possible.
  23158. + */
  23159. +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
  23160. +{
  23161. + long l;
  23162. +
  23163. + percpu_ref_switch_to_atomic_sync(cnt);
  23164. + l = atomic_long_read(&cnt->count);
  23165. + if (do_rev)
  23166. + percpu_ref_switch_to_percpu(cnt);
  23167. +
  23168. + /* percpu_ref is initialized by 1 instead of 0 */
  23169. + return l - 1;
  23170. +}
  23171. +#endif
  23172. +
  23173. +#ifdef CONFIG_AUFS_DEBUG
  23174. +#define AuLCntZero(val) do { \
  23175. + long l = val; \
  23176. + if (l) \
  23177. + AuDbg("%s = %ld\n", #val, l); \
  23178. +} while (0)
  23179. +#else
  23180. +#define AuLCntZero(val) do {} while (0)
  23181. +#endif
  23182. +
  23183. +#endif /* __KERNEL__ */
  23184. +#endif /* __AUFS_LCNT_H__ */
  23185. diff -Naur null/fs/aufs/loop.c linux-5.1/fs/aufs/loop.c
  23186. --- /dev/null
  23187. +++ linux-5.1/fs/aufs/loop.c 2019-05-22 11:23:07.441140996 +0200
  23188. @@ -0,0 +1,148 @@
  23189. +// SPDX-License-Identifier: GPL-2.0
  23190. +/*
  23191. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  23192. + *
  23193. + * This program, aufs is free software; you can redistribute it and/or modify
  23194. + * it under the terms of the GNU General Public License as published by
  23195. + * the Free Software Foundation; either version 2 of the License, or
  23196. + * (at your option) any later version.
  23197. + *
  23198. + * This program is distributed in the hope that it will be useful,
  23199. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23200. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23201. + * GNU General Public License for more details.
  23202. + *
  23203. + * You should have received a copy of the GNU General Public License
  23204. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23205. + */
  23206. +
  23207. +/*
  23208. + * support for loopback block device as a branch
  23209. + */
  23210. +
  23211. +#include "aufs.h"
  23212. +
  23213. +/* added into drivers/block/loop.c */
  23214. +static struct file *(*backing_file_func)(struct super_block *sb);
  23215. +
  23216. +/*
  23217. + * test if two lower dentries have overlapping branches.
  23218. + */
  23219. +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
  23220. +{
  23221. + struct super_block *h_sb;
  23222. + struct file *backing_file;
  23223. +
  23224. + if (unlikely(!backing_file_func)) {
  23225. + /* don't load "loop" module here */
  23226. + backing_file_func = symbol_get(loop_backing_file);
  23227. + if (unlikely(!backing_file_func))
  23228. + /* "loop" module is not loaded */
  23229. + return 0;
  23230. + }
  23231. +
  23232. + h_sb = h_adding->d_sb;
  23233. + backing_file = backing_file_func(h_sb);
  23234. + if (!backing_file)
  23235. + return 0;
  23236. +
  23237. + h_adding = backing_file->f_path.dentry;
  23238. + /*
  23239. + * h_adding can be local NFS.
  23240. + * in this case aufs cannot detect the loop.
  23241. + */
  23242. + if (unlikely(h_adding->d_sb == sb))
  23243. + return 1;
  23244. + return !!au_test_subdir(h_adding, sb->s_root);
  23245. +}
  23246. +
  23247. +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
  23248. +int au_test_loopback_kthread(void)
  23249. +{
  23250. + int ret;
  23251. + struct task_struct *tsk = current;
  23252. + char c, comm[sizeof(tsk->comm)];
  23253. +
  23254. + ret = 0;
  23255. + if (tsk->flags & PF_KTHREAD) {
  23256. + get_task_comm(comm, tsk);
  23257. + c = comm[4];
  23258. + ret = ('0' <= c && c <= '9'
  23259. + && !strncmp(comm, "loop", 4));
  23260. + }
  23261. +
  23262. + return ret;
  23263. +}
  23264. +
  23265. +/* ---------------------------------------------------------------------- */
  23266. +
  23267. +#define au_warn_loopback_step 16
  23268. +static int au_warn_loopback_nelem = au_warn_loopback_step;
  23269. +static unsigned long *au_warn_loopback_array;
  23270. +
  23271. +void au_warn_loopback(struct super_block *h_sb)
  23272. +{
  23273. + int i, new_nelem;
  23274. + unsigned long *a, magic;
  23275. + static DEFINE_SPINLOCK(spin);
  23276. +
  23277. + magic = h_sb->s_magic;
  23278. + spin_lock(&spin);
  23279. + a = au_warn_loopback_array;
  23280. + for (i = 0; i < au_warn_loopback_nelem && *a; i++)
  23281. + if (a[i] == magic) {
  23282. + spin_unlock(&spin);
  23283. + return;
  23284. + }
  23285. +
  23286. + /* h_sb is new to us, print it */
  23287. + if (i < au_warn_loopback_nelem) {
  23288. + a[i] = magic;
  23289. + goto pr;
  23290. + }
  23291. +
  23292. + /* expand the array */
  23293. + new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
  23294. + a = au_kzrealloc(au_warn_loopback_array,
  23295. + au_warn_loopback_nelem * sizeof(unsigned long),
  23296. + new_nelem * sizeof(unsigned long), GFP_ATOMIC,
  23297. + /*may_shrink*/0);
  23298. + if (a) {
  23299. + au_warn_loopback_nelem = new_nelem;
  23300. + au_warn_loopback_array = a;
  23301. + a[i] = magic;
  23302. + goto pr;
  23303. + }
  23304. +
  23305. + spin_unlock(&spin);
  23306. + AuWarn1("realloc failed, ignored\n");
  23307. + return;
  23308. +
  23309. +pr:
  23310. + spin_unlock(&spin);
  23311. + pr_warn("you may want to try another patch for loopback file "
  23312. + "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
  23313. +}
  23314. +
  23315. +int au_loopback_init(void)
  23316. +{
  23317. + int err;
  23318. + struct super_block *sb __maybe_unused;
  23319. +
  23320. + BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
  23321. +
  23322. + err = 0;
  23323. + au_warn_loopback_array = kcalloc(au_warn_loopback_step,
  23324. + sizeof(unsigned long), GFP_NOFS);
  23325. + if (unlikely(!au_warn_loopback_array))
  23326. + err = -ENOMEM;
  23327. +
  23328. + return err;
  23329. +}
  23330. +
  23331. +void au_loopback_fin(void)
  23332. +{
  23333. + if (backing_file_func)
  23334. + symbol_put(loop_backing_file);
  23335. + au_kfree_try_rcu(au_warn_loopback_array);
  23336. +}
  23337. diff -Naur null/fs/aufs/loop.h linux-5.1/fs/aufs/loop.h
  23338. --- /dev/null
  23339. +++ linux-5.1/fs/aufs/loop.h 2019-05-22 11:23:07.441140996 +0200
  23340. @@ -0,0 +1,55 @@
  23341. +/* SPDX-License-Identifier: GPL-2.0 */
  23342. +/*
  23343. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  23344. + *
  23345. + * This program, aufs is free software; you can redistribute it and/or modify
  23346. + * it under the terms of the GNU General Public License as published by
  23347. + * the Free Software Foundation; either version 2 of the License, or
  23348. + * (at your option) any later version.
  23349. + *
  23350. + * This program is distributed in the hope that it will be useful,
  23351. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23352. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23353. + * GNU General Public License for more details.
  23354. + *
  23355. + * You should have received a copy of the GNU General Public License
  23356. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23357. + */
  23358. +
  23359. +/*
  23360. + * support for loopback mount as a branch
  23361. + */
  23362. +
  23363. +#ifndef __AUFS_LOOP_H__
  23364. +#define __AUFS_LOOP_H__
  23365. +
  23366. +#ifdef __KERNEL__
  23367. +
  23368. +struct dentry;
  23369. +struct super_block;
  23370. +
  23371. +#ifdef CONFIG_AUFS_BDEV_LOOP
  23372. +/* drivers/block/loop.c */
  23373. +struct file *loop_backing_file(struct super_block *sb);
  23374. +
  23375. +/* loop.c */
  23376. +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
  23377. +int au_test_loopback_kthread(void);
  23378. +void au_warn_loopback(struct super_block *h_sb);
  23379. +
  23380. +int au_loopback_init(void);
  23381. +void au_loopback_fin(void);
  23382. +#else
  23383. +AuStub(struct file *, loop_backing_file, return NULL)
  23384. +
  23385. +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
  23386. + struct dentry *h_adding)
  23387. +AuStubInt0(au_test_loopback_kthread, void)
  23388. +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
  23389. +
  23390. +AuStubInt0(au_loopback_init, void)
  23391. +AuStubVoid(au_loopback_fin, void)
  23392. +#endif /* BLK_DEV_LOOP */
  23393. +
  23394. +#endif /* __KERNEL__ */
  23395. +#endif /* __AUFS_LOOP_H__ */
  23396. diff -Naur null/fs/aufs/magic.mk linux-5.1/fs/aufs/magic.mk
  23397. --- /dev/null
  23398. +++ linux-5.1/fs/aufs/magic.mk 2019-05-22 11:23:07.441140996 +0200
  23399. @@ -0,0 +1,31 @@
  23400. +# SPDX-License-Identifier: GPL-2.0
  23401. +
  23402. +# defined in ${srctree}/fs/fuse/inode.c
  23403. +# tristate
  23404. +ifdef CONFIG_FUSE_FS
  23405. +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
  23406. +endif
  23407. +
  23408. +# defined in ${srctree}/fs/xfs/xfs_sb.h
  23409. +# tristate
  23410. +ifdef CONFIG_XFS_FS
  23411. +ccflags-y += -DXFS_SB_MAGIC=0x58465342
  23412. +endif
  23413. +
  23414. +# defined in ${srctree}/fs/configfs/mount.c
  23415. +# tristate
  23416. +ifdef CONFIG_CONFIGFS_FS
  23417. +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
  23418. +endif
  23419. +
  23420. +# defined in ${srctree}/fs/ubifs/ubifs.h
  23421. +# tristate
  23422. +ifdef CONFIG_UBIFS_FS
  23423. +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
  23424. +endif
  23425. +
  23426. +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
  23427. +# tristate
  23428. +ifdef CONFIG_HFSPLUS_FS
  23429. +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
  23430. +endif
  23431. diff -Naur null/fs/aufs/Makefile linux-5.1/fs/aufs/Makefile
  23432. --- /dev/null
  23433. +++ linux-5.1/fs/aufs/Makefile 2019-05-22 11:23:07.438141134 +0200
  23434. @@ -0,0 +1,46 @@
  23435. +# SPDX-License-Identifier: GPL-2.0
  23436. +
  23437. +include ${src}/magic.mk
  23438. +ifeq (${CONFIG_AUFS_FS},m)
  23439. +include ${src}/conf.mk
  23440. +endif
  23441. +-include ${src}/priv_def.mk
  23442. +
  23443. +# cf. include/linux/kernel.h
  23444. +# enable pr_debug
  23445. +ccflags-y += -DDEBUG
  23446. +# sparse requires the full pathname
  23447. +ifdef M
  23448. +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
  23449. +else
  23450. +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
  23451. +endif
  23452. +
  23453. +obj-$(CONFIG_AUFS_FS) += aufs.o
  23454. +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
  23455. + wkq.o vfsub.o dcsub.o \
  23456. + cpup.o whout.o wbr_policy.o \
  23457. + dinfo.o dentry.o \
  23458. + dynop.o \
  23459. + finfo.o file.o f_op.o \
  23460. + dir.o vdir.o \
  23461. + iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
  23462. + mvdown.o ioctl.o
  23463. +
  23464. +# all are boolean
  23465. +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
  23466. +aufs-$(CONFIG_SYSFS) += sysfs.o
  23467. +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
  23468. +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
  23469. +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
  23470. +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
  23471. +aufs-$(CONFIG_AUFS_EXPORT) += export.o
  23472. +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
  23473. +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
  23474. +aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
  23475. +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
  23476. +aufs-$(CONFIG_AUFS_POLL) += poll.o
  23477. +aufs-$(CONFIG_AUFS_RDU) += rdu.o
  23478. +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
  23479. +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
  23480. +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
  23481. diff -Naur null/fs/aufs/module.c linux-5.1/fs/aufs/module.c
  23482. --- /dev/null
  23483. +++ linux-5.1/fs/aufs/module.c 2019-05-22 11:23:07.441140996 +0200
  23484. @@ -0,0 +1,273 @@
  23485. +// SPDX-License-Identifier: GPL-2.0
  23486. +/*
  23487. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  23488. + *
  23489. + * This program, aufs is free software; you can redistribute it and/or modify
  23490. + * it under the terms of the GNU General Public License as published by
  23491. + * the Free Software Foundation; either version 2 of the License, or
  23492. + * (at your option) any later version.
  23493. + *
  23494. + * This program is distributed in the hope that it will be useful,
  23495. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23496. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23497. + * GNU General Public License for more details.
  23498. + *
  23499. + * You should have received a copy of the GNU General Public License
  23500. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23501. + */
  23502. +
  23503. +/*
  23504. + * module global variables and operations
  23505. + */
  23506. +
  23507. +#include <linux/module.h>
  23508. +#include <linux/seq_file.h>
  23509. +#include "aufs.h"
  23510. +
  23511. +/* shrinkable realloc */
  23512. +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
  23513. +{
  23514. + size_t sz;
  23515. + int diff;
  23516. +
  23517. + sz = 0;
  23518. + diff = -1;
  23519. + if (p) {
  23520. +#if 0 /* unused */
  23521. + if (!new_sz) {
  23522. + au_kfree_rcu(p);
  23523. + p = NULL;
  23524. + goto out;
  23525. + }
  23526. +#else
  23527. + AuDebugOn(!new_sz);
  23528. +#endif
  23529. + sz = ksize(p);
  23530. + diff = au_kmidx_sub(sz, new_sz);
  23531. + }
  23532. + if (sz && !diff)
  23533. + goto out;
  23534. +
  23535. + if (sz < new_sz)
  23536. + /* expand or SLOB */
  23537. + p = krealloc(p, new_sz, gfp);
  23538. + else if (new_sz < sz && may_shrink) {
  23539. + /* shrink */
  23540. + void *q;
  23541. +
  23542. + q = kmalloc(new_sz, gfp);
  23543. + if (q) {
  23544. + if (p) {
  23545. + memcpy(q, p, new_sz);
  23546. + au_kfree_try_rcu(p);
  23547. + }
  23548. + p = q;
  23549. + } else
  23550. + p = NULL;
  23551. + }
  23552. +
  23553. +out:
  23554. + return p;
  23555. +}
  23556. +
  23557. +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
  23558. + int may_shrink)
  23559. +{
  23560. + p = au_krealloc(p, new_sz, gfp, may_shrink);
  23561. + if (p && new_sz > nused)
  23562. + memset(p + nused, 0, new_sz - nused);
  23563. + return p;
  23564. +}
  23565. +
  23566. +/* ---------------------------------------------------------------------- */
  23567. +/*
  23568. + * aufs caches
  23569. + */
  23570. +struct kmem_cache *au_cache[AuCache_Last];
  23571. +
  23572. +static void au_cache_fin(void)
  23573. +{
  23574. + int i;
  23575. +
  23576. + /*
  23577. + * Make sure all delayed rcu free inodes are flushed before we
  23578. + * destroy cache.
  23579. + */
  23580. + rcu_barrier();
  23581. +
  23582. + /* excluding AuCache_HNOTIFY */
  23583. + BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
  23584. + for (i = 0; i < AuCache_HNOTIFY; i++) {
  23585. + kmem_cache_destroy(au_cache[i]);
  23586. + au_cache[i] = NULL;
  23587. + }
  23588. +}
  23589. +
  23590. +static int __init au_cache_init(void)
  23591. +{
  23592. + au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
  23593. + if (au_cache[AuCache_DINFO])
  23594. + /* SLAB_DESTROY_BY_RCU */
  23595. + au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
  23596. + au_icntnr_init_once);
  23597. + if (au_cache[AuCache_ICNTNR])
  23598. + au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
  23599. + au_fi_init_once);
  23600. + if (au_cache[AuCache_FINFO])
  23601. + au_cache[AuCache_VDIR] = AuCache(au_vdir);
  23602. + if (au_cache[AuCache_VDIR])
  23603. + au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
  23604. + if (au_cache[AuCache_DEHSTR])
  23605. + return 0;
  23606. +
  23607. + au_cache_fin();
  23608. + return -ENOMEM;
  23609. +}
  23610. +
  23611. +/* ---------------------------------------------------------------------- */
  23612. +
  23613. +int au_dir_roflags;
  23614. +
  23615. +#ifdef CONFIG_AUFS_SBILIST
  23616. +/*
  23617. + * iterate_supers_type() doesn't protect us from
  23618. + * remounting (branch management)
  23619. + */
  23620. +struct hlist_bl_head au_sbilist;
  23621. +#endif
  23622. +
  23623. +/*
  23624. + * functions for module interface.
  23625. + */
  23626. +MODULE_LICENSE("GPL");
  23627. +/* MODULE_LICENSE("GPL v2"); */
  23628. +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
  23629. +MODULE_DESCRIPTION(AUFS_NAME
  23630. + " -- Advanced multi layered unification filesystem");
  23631. +MODULE_VERSION(AUFS_VERSION);
  23632. +MODULE_ALIAS_FS(AUFS_NAME);
  23633. +
  23634. +/* this module parameter has no meaning when SYSFS is disabled */
  23635. +int sysaufs_brs = 1;
  23636. +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
  23637. +module_param_named(brs, sysaufs_brs, int, 0444);
  23638. +
  23639. +/* this module parameter has no meaning when USER_NS is disabled */
  23640. +bool au_userns;
  23641. +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
  23642. +module_param_named(allow_userns, au_userns, bool, 0444);
  23643. +
  23644. +/* ---------------------------------------------------------------------- */
  23645. +
  23646. +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
  23647. +
  23648. +int au_seq_path(struct seq_file *seq, struct path *path)
  23649. +{
  23650. + int err;
  23651. +
  23652. + err = seq_path(seq, path, au_esc_chars);
  23653. + if (err >= 0)
  23654. + err = 0;
  23655. + else
  23656. + err = -ENOMEM;
  23657. +
  23658. + return err;
  23659. +}
  23660. +
  23661. +/* ---------------------------------------------------------------------- */
  23662. +
  23663. +static int __init aufs_init(void)
  23664. +{
  23665. + int err, i;
  23666. + char *p;
  23667. +
  23668. + p = au_esc_chars;
  23669. + for (i = 1; i <= ' '; i++)
  23670. + *p++ = i;
  23671. + *p++ = '\\';
  23672. + *p++ = '\x7f';
  23673. + *p = 0;
  23674. +
  23675. + au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
  23676. +
  23677. + memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
  23678. + for (i = 0; i < AuIop_Last; i++)
  23679. + aufs_iop_nogetattr[i].getattr = NULL;
  23680. +
  23681. + memset(au_cache, 0, sizeof(au_cache)); /* including hnotify */
  23682. +
  23683. + au_sbilist_init();
  23684. + sysaufs_brs_init();
  23685. + au_debug_init();
  23686. + au_dy_init();
  23687. + err = sysaufs_init();
  23688. + if (unlikely(err))
  23689. + goto out;
  23690. + err = dbgaufs_init();
  23691. + if (unlikely(err))
  23692. + goto out_sysaufs;
  23693. + err = au_procfs_init();
  23694. + if (unlikely(err))
  23695. + goto out_dbgaufs;
  23696. + err = au_wkq_init();
  23697. + if (unlikely(err))
  23698. + goto out_procfs;
  23699. + err = au_loopback_init();
  23700. + if (unlikely(err))
  23701. + goto out_wkq;
  23702. + err = au_hnotify_init();
  23703. + if (unlikely(err))
  23704. + goto out_loopback;
  23705. + err = au_sysrq_init();
  23706. + if (unlikely(err))
  23707. + goto out_hin;
  23708. + err = au_cache_init();
  23709. + if (unlikely(err))
  23710. + goto out_sysrq;
  23711. +
  23712. + aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
  23713. + err = register_filesystem(&aufs_fs_type);
  23714. + if (unlikely(err))
  23715. + goto out_cache;
  23716. +
  23717. + /* since we define pr_fmt, call printk directly */
  23718. + printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
  23719. + goto out; /* success */
  23720. +
  23721. +out_cache:
  23722. + au_cache_fin();
  23723. +out_sysrq:
  23724. + au_sysrq_fin();
  23725. +out_hin:
  23726. + au_hnotify_fin();
  23727. +out_loopback:
  23728. + au_loopback_fin();
  23729. +out_wkq:
  23730. + au_wkq_fin();
  23731. +out_procfs:
  23732. + au_procfs_fin();
  23733. +out_dbgaufs:
  23734. + dbgaufs_fin();
  23735. +out_sysaufs:
  23736. + sysaufs_fin();
  23737. + au_dy_fin();
  23738. +out:
  23739. + return err;
  23740. +}
  23741. +
  23742. +static void __exit aufs_exit(void)
  23743. +{
  23744. + unregister_filesystem(&aufs_fs_type);
  23745. + au_cache_fin();
  23746. + au_sysrq_fin();
  23747. + au_hnotify_fin();
  23748. + au_loopback_fin();
  23749. + au_wkq_fin();
  23750. + au_procfs_fin();
  23751. + dbgaufs_fin();
  23752. + sysaufs_fin();
  23753. + au_dy_fin();
  23754. +}
  23755. +
  23756. +module_init(aufs_init);
  23757. +module_exit(aufs_exit);
  23758. diff -Naur null/fs/aufs/module.h linux-5.1/fs/aufs/module.h
  23759. --- /dev/null
  23760. +++ linux-5.1/fs/aufs/module.h 2019-05-22 11:23:07.442140950 +0200
  23761. @@ -0,0 +1,166 @@
  23762. +/* SPDX-License-Identifier: GPL-2.0 */
  23763. +/*
  23764. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  23765. + *
  23766. + * This program, aufs is free software; you can redistribute it and/or modify
  23767. + * it under the terms of the GNU General Public License as published by
  23768. + * the Free Software Foundation; either version 2 of the License, or
  23769. + * (at your option) any later version.
  23770. + *
  23771. + * This program is distributed in the hope that it will be useful,
  23772. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23773. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23774. + * GNU General Public License for more details.
  23775. + *
  23776. + * You should have received a copy of the GNU General Public License
  23777. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23778. + */
  23779. +
  23780. +/*
  23781. + * module initialization and module-global
  23782. + */
  23783. +
  23784. +#ifndef __AUFS_MODULE_H__
  23785. +#define __AUFS_MODULE_H__
  23786. +
  23787. +#ifdef __KERNEL__
  23788. +
  23789. +#include <linux/slab.h>
  23790. +#include "debug.h"
  23791. +#include "dentry.h"
  23792. +#include "dir.h"
  23793. +#include "file.h"
  23794. +#include "inode.h"
  23795. +
  23796. +struct path;
  23797. +struct seq_file;
  23798. +
  23799. +/* module parameters */
  23800. +extern int sysaufs_brs;
  23801. +extern bool au_userns;
  23802. +
  23803. +/* ---------------------------------------------------------------------- */
  23804. +
  23805. +extern int au_dir_roflags;
  23806. +
  23807. +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
  23808. +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
  23809. + int may_shrink);
  23810. +
  23811. +/*
  23812. + * Comparing the size of the object with sizeof(struct rcu_head)
  23813. + * case 1: object is always larger
  23814. + * --> au_kfree_rcu() or au_kfree_do_rcu()
  23815. + * case 2: object is always smaller
  23816. + * --> au_kfree_small()
  23817. + * case 3: object can be any size
  23818. + * --> au_kfree_try_rcu()
  23819. + */
  23820. +
  23821. +static inline void au_kfree_do_rcu(const void *p)
  23822. +{
  23823. + struct {
  23824. + struct rcu_head rcu;
  23825. + } *a = (void *)p;
  23826. +
  23827. + kfree_rcu(a, rcu);
  23828. +}
  23829. +
  23830. +#define au_kfree_rcu(_p) do { \
  23831. + typeof(_p) p = (_p); \
  23832. + BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head)); \
  23833. + if (p) \
  23834. + au_kfree_do_rcu(p); \
  23835. + } while (0)
  23836. +
  23837. +#define au_kfree_do_sz_test(sz) (sz >= sizeof(struct rcu_head))
  23838. +#define au_kfree_sz_test(p) (p && au_kfree_do_sz_test(ksize(p)))
  23839. +
  23840. +static inline void au_kfree_try_rcu(const void *p)
  23841. +{
  23842. + if (!p)
  23843. + return;
  23844. + if (au_kfree_sz_test(p))
  23845. + au_kfree_do_rcu(p);
  23846. + else
  23847. + kfree(p);
  23848. +}
  23849. +
  23850. +static inline void au_kfree_small(const void *p)
  23851. +{
  23852. + if (!p)
  23853. + return;
  23854. + AuDebugOn(au_kfree_sz_test(p));
  23855. + kfree(p);
  23856. +}
  23857. +
  23858. +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
  23859. +{
  23860. +#ifndef CONFIG_SLOB
  23861. + return kmalloc_index(sz) - kmalloc_index(new_sz);
  23862. +#else
  23863. + return -1; /* SLOB is untested */
  23864. +#endif
  23865. +}
  23866. +
  23867. +int au_seq_path(struct seq_file *seq, struct path *path);
  23868. +
  23869. +#ifdef CONFIG_PROC_FS
  23870. +/* procfs.c */
  23871. +int __init au_procfs_init(void);
  23872. +void au_procfs_fin(void);
  23873. +#else
  23874. +AuStubInt0(au_procfs_init, void);
  23875. +AuStubVoid(au_procfs_fin, void);
  23876. +#endif
  23877. +
  23878. +/* ---------------------------------------------------------------------- */
  23879. +
  23880. +/* kmem cache */
  23881. +enum {
  23882. + AuCache_DINFO,
  23883. + AuCache_ICNTNR,
  23884. + AuCache_FINFO,
  23885. + AuCache_VDIR,
  23886. + AuCache_DEHSTR,
  23887. + AuCache_HNOTIFY, /* must be last */
  23888. + AuCache_Last
  23889. +};
  23890. +
  23891. +extern struct kmem_cache *au_cache[AuCache_Last];
  23892. +
  23893. +#define AuCacheFlags (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
  23894. +#define AuCache(type) KMEM_CACHE(type, AuCacheFlags)
  23895. +#define AuCacheCtor(type, ctor) \
  23896. + kmem_cache_create(#type, sizeof(struct type), \
  23897. + __alignof__(struct type), AuCacheFlags, ctor)
  23898. +
  23899. +#define AuCacheFuncs(name, index) \
  23900. + static inline struct au_##name *au_cache_alloc_##name(void) \
  23901. + { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
  23902. + static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
  23903. + { kmem_cache_free(au_cache[AuCache_##index], p); } \
  23904. + \
  23905. + static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
  23906. + { void *p = rcu; \
  23907. + p -= offsetof(struct au_##name, rcu); \
  23908. + kmem_cache_free(au_cache[AuCache_##index], p); } \
  23909. + static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
  23910. + { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
  23911. + call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); } \
  23912. + \
  23913. + static inline void au_cache_free_##name(struct au_##name *p) \
  23914. + { /* au_cache_free_##name##_norcu(p); */ \
  23915. + au_cache_free_##name##_rcu(p); }
  23916. +
  23917. +AuCacheFuncs(dinfo, DINFO);
  23918. +AuCacheFuncs(icntnr, ICNTNR);
  23919. +AuCacheFuncs(finfo, FINFO);
  23920. +AuCacheFuncs(vdir, VDIR);
  23921. +AuCacheFuncs(vdir_dehstr, DEHSTR);
  23922. +#ifdef CONFIG_AUFS_HNOTIFY
  23923. +AuCacheFuncs(hnotify, HNOTIFY);
  23924. +#endif
  23925. +
  23926. +#endif /* __KERNEL__ */
  23927. +#endif /* __AUFS_MODULE_H__ */
  23928. diff -Naur null/fs/aufs/mvdown.c linux-5.1/fs/aufs/mvdown.c
  23929. --- /dev/null
  23930. +++ linux-5.1/fs/aufs/mvdown.c 2019-05-22 11:23:07.442140950 +0200
  23931. @@ -0,0 +1,706 @@
  23932. +// SPDX-License-Identifier: GPL-2.0
  23933. +/*
  23934. + * Copyright (C) 2011-2019 Junjiro R. Okajima
  23935. + *
  23936. + * This program, aufs is free software; you can redistribute it and/or modify
  23937. + * it under the terms of the GNU General Public License as published by
  23938. + * the Free Software Foundation; either version 2 of the License, or
  23939. + * (at your option) any later version.
  23940. + *
  23941. + * This program is distributed in the hope that it will be useful,
  23942. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23943. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23944. + * GNU General Public License for more details.
  23945. + *
  23946. + * You should have received a copy of the GNU General Public License
  23947. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23948. + */
  23949. +
  23950. +/*
  23951. + * move-down, opposite of copy-up
  23952. + */
  23953. +
  23954. +#include "aufs.h"
  23955. +
  23956. +struct au_mvd_args {
  23957. + struct {
  23958. + struct super_block *h_sb;
  23959. + struct dentry *h_parent;
  23960. + struct au_hinode *hdir;
  23961. + struct inode *h_dir, *h_inode;
  23962. + struct au_pin pin;
  23963. + } info[AUFS_MVDOWN_NARRAY];
  23964. +
  23965. + struct aufs_mvdown mvdown;
  23966. + struct dentry *dentry, *parent;
  23967. + struct inode *inode, *dir;
  23968. + struct super_block *sb;
  23969. + aufs_bindex_t bopq, bwh, bfound;
  23970. + unsigned char rename_lock;
  23971. +};
  23972. +
  23973. +#define mvd_errno mvdown.au_errno
  23974. +#define mvd_bsrc mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
  23975. +#define mvd_src_brid mvdown.stbr[AUFS_MVDOWN_UPPER].brid
  23976. +#define mvd_bdst mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
  23977. +#define mvd_dst_brid mvdown.stbr[AUFS_MVDOWN_LOWER].brid
  23978. +
  23979. +#define mvd_h_src_sb info[AUFS_MVDOWN_UPPER].h_sb
  23980. +#define mvd_h_src_parent info[AUFS_MVDOWN_UPPER].h_parent
  23981. +#define mvd_hdir_src info[AUFS_MVDOWN_UPPER].hdir
  23982. +#define mvd_h_src_dir info[AUFS_MVDOWN_UPPER].h_dir
  23983. +#define mvd_h_src_inode info[AUFS_MVDOWN_UPPER].h_inode
  23984. +#define mvd_pin_src info[AUFS_MVDOWN_UPPER].pin
  23985. +
  23986. +#define mvd_h_dst_sb info[AUFS_MVDOWN_LOWER].h_sb
  23987. +#define mvd_h_dst_parent info[AUFS_MVDOWN_LOWER].h_parent
  23988. +#define mvd_hdir_dst info[AUFS_MVDOWN_LOWER].hdir
  23989. +#define mvd_h_dst_dir info[AUFS_MVDOWN_LOWER].h_dir
  23990. +#define mvd_h_dst_inode info[AUFS_MVDOWN_LOWER].h_inode
  23991. +#define mvd_pin_dst info[AUFS_MVDOWN_LOWER].pin
  23992. +
  23993. +#define AU_MVD_PR(flag, ...) do { \
  23994. + if (flag) \
  23995. + pr_err(__VA_ARGS__); \
  23996. + } while (0)
  23997. +
  23998. +static int find_lower_writable(struct au_mvd_args *a)
  23999. +{
  24000. + struct super_block *sb;
  24001. + aufs_bindex_t bindex, bbot;
  24002. + struct au_branch *br;
  24003. +
  24004. + sb = a->sb;
  24005. + bindex = a->mvd_bsrc;
  24006. + bbot = au_sbbot(sb);
  24007. + if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
  24008. + for (bindex++; bindex <= bbot; bindex++) {
  24009. + br = au_sbr(sb, bindex);
  24010. + if (au_br_fhsm(br->br_perm)
  24011. + && !sb_rdonly(au_br_sb(br)))
  24012. + return bindex;
  24013. + }
  24014. + else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
  24015. + for (bindex++; bindex <= bbot; bindex++) {
  24016. + br = au_sbr(sb, bindex);
  24017. + if (!au_br_rdonly(br))
  24018. + return bindex;
  24019. + }
  24020. + else
  24021. + for (bindex++; bindex <= bbot; bindex++) {
  24022. + br = au_sbr(sb, bindex);
  24023. + if (!sb_rdonly(au_br_sb(br))) {
  24024. + if (au_br_rdonly(br))
  24025. + a->mvdown.flags
  24026. + |= AUFS_MVDOWN_ROLOWER_R;
  24027. + return bindex;
  24028. + }
  24029. + }
  24030. +
  24031. + return -1;
  24032. +}
  24033. +
  24034. +/* make the parent dir on bdst */
  24035. +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
  24036. +{
  24037. + int err;
  24038. +
  24039. + err = 0;
  24040. + a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
  24041. + a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
  24042. + a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
  24043. + a->mvd_h_dst_parent = NULL;
  24044. + if (au_dbbot(a->parent) >= a->mvd_bdst)
  24045. + a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
  24046. + if (!a->mvd_h_dst_parent) {
  24047. + err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
  24048. + if (unlikely(err)) {
  24049. + AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
  24050. + goto out;
  24051. + }
  24052. + a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
  24053. + }
  24054. +
  24055. +out:
  24056. + AuTraceErr(err);
  24057. + return err;
  24058. +}
  24059. +
  24060. +/* lock them all */
  24061. +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
  24062. +{
  24063. + int err;
  24064. + struct dentry *h_trap;
  24065. +
  24066. + a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
  24067. + a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
  24068. + err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
  24069. + au_opt_udba(a->sb),
  24070. + AuPin_MNT_WRITE | AuPin_DI_LOCKED);
  24071. + AuTraceErr(err);
  24072. + if (unlikely(err)) {
  24073. + AU_MVD_PR(dmsg, "pin_dst failed\n");
  24074. + goto out;
  24075. + }
  24076. +
  24077. + if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
  24078. + a->rename_lock = 0;
  24079. + au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
  24080. + AuLsc_DI_PARENT, AuLsc_I_PARENT3,
  24081. + au_opt_udba(a->sb),
  24082. + AuPin_MNT_WRITE | AuPin_DI_LOCKED);
  24083. + err = au_do_pin(&a->mvd_pin_src);
  24084. + AuTraceErr(err);
  24085. + a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
  24086. + if (unlikely(err)) {
  24087. + AU_MVD_PR(dmsg, "pin_src failed\n");
  24088. + goto out_dst;
  24089. + }
  24090. + goto out; /* success */
  24091. + }
  24092. +
  24093. + a->rename_lock = 1;
  24094. + au_pin_hdir_unlock(&a->mvd_pin_dst);
  24095. + err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
  24096. + au_opt_udba(a->sb),
  24097. + AuPin_MNT_WRITE | AuPin_DI_LOCKED);
  24098. + AuTraceErr(err);
  24099. + a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
  24100. + if (unlikely(err)) {
  24101. + AU_MVD_PR(dmsg, "pin_src failed\n");
  24102. + au_pin_hdir_lock(&a->mvd_pin_dst);
  24103. + goto out_dst;
  24104. + }
  24105. + au_pin_hdir_unlock(&a->mvd_pin_src);
  24106. + h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
  24107. + a->mvd_h_dst_parent, a->mvd_hdir_dst);
  24108. + if (h_trap) {
  24109. + err = (h_trap != a->mvd_h_src_parent);
  24110. + if (err)
  24111. + err = (h_trap != a->mvd_h_dst_parent);
  24112. + }
  24113. + BUG_ON(err); /* it should never happen */
  24114. + if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
  24115. + err = -EBUSY;
  24116. + AuTraceErr(err);
  24117. + vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
  24118. + a->mvd_h_dst_parent, a->mvd_hdir_dst);
  24119. + au_pin_hdir_lock(&a->mvd_pin_src);
  24120. + au_unpin(&a->mvd_pin_src);
  24121. + au_pin_hdir_lock(&a->mvd_pin_dst);
  24122. + goto out_dst;
  24123. + }
  24124. + goto out; /* success */
  24125. +
  24126. +out_dst:
  24127. + au_unpin(&a->mvd_pin_dst);
  24128. +out:
  24129. + AuTraceErr(err);
  24130. + return err;
  24131. +}
  24132. +
  24133. +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
  24134. +{
  24135. + if (!a->rename_lock)
  24136. + au_unpin(&a->mvd_pin_src);
  24137. + else {
  24138. + vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
  24139. + a->mvd_h_dst_parent, a->mvd_hdir_dst);
  24140. + au_pin_hdir_lock(&a->mvd_pin_src);
  24141. + au_unpin(&a->mvd_pin_src);
  24142. + au_pin_hdir_lock(&a->mvd_pin_dst);
  24143. + }
  24144. + au_unpin(&a->mvd_pin_dst);
  24145. +}
  24146. +
  24147. +/* copy-down the file */
  24148. +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
  24149. +{
  24150. + int err;
  24151. + struct au_cp_generic cpg = {
  24152. + .dentry = a->dentry,
  24153. + .bdst = a->mvd_bdst,
  24154. + .bsrc = a->mvd_bsrc,
  24155. + .len = -1,
  24156. + .pin = &a->mvd_pin_dst,
  24157. + .flags = AuCpup_DTIME | AuCpup_HOPEN
  24158. + };
  24159. +
  24160. + AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
  24161. + if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
  24162. + au_fset_cpup(cpg.flags, OVERWRITE);
  24163. + if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
  24164. + au_fset_cpup(cpg.flags, RWDST);
  24165. + err = au_sio_cpdown_simple(&cpg);
  24166. + if (unlikely(err))
  24167. + AU_MVD_PR(dmsg, "cpdown failed\n");
  24168. +
  24169. + AuTraceErr(err);
  24170. + return err;
  24171. +}
  24172. +
  24173. +/*
  24174. + * unlink the whiteout on bdst if exist which may be created by UDBA while we
  24175. + * were sleeping
  24176. + */
  24177. +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
  24178. +{
  24179. + int err;
  24180. + struct path h_path;
  24181. + struct au_branch *br;
  24182. + struct inode *delegated;
  24183. +
  24184. + br = au_sbr(a->sb, a->mvd_bdst);
  24185. + h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
  24186. + err = PTR_ERR(h_path.dentry);
  24187. + if (IS_ERR(h_path.dentry)) {
  24188. + AU_MVD_PR(dmsg, "wh_lkup failed\n");
  24189. + goto out;
  24190. + }
  24191. +
  24192. + err = 0;
  24193. + if (d_is_positive(h_path.dentry)) {
  24194. + h_path.mnt = au_br_mnt(br);
  24195. + delegated = NULL;
  24196. + err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
  24197. + &delegated, /*force*/0);
  24198. + if (unlikely(err == -EWOULDBLOCK)) {
  24199. + pr_warn("cannot retry for NFSv4 delegation"
  24200. + " for an internal unlink\n");
  24201. + iput(delegated);
  24202. + }
  24203. + if (unlikely(err))
  24204. + AU_MVD_PR(dmsg, "wh_unlink failed\n");
  24205. + }
  24206. + dput(h_path.dentry);
  24207. +
  24208. +out:
  24209. + AuTraceErr(err);
  24210. + return err;
  24211. +}
  24212. +
  24213. +/*
  24214. + * unlink the topmost h_dentry
  24215. + */
  24216. +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
  24217. +{
  24218. + int err;
  24219. + struct path h_path;
  24220. + struct inode *delegated;
  24221. +
  24222. + h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
  24223. + h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
  24224. + delegated = NULL;
  24225. + err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
  24226. + if (unlikely(err == -EWOULDBLOCK)) {
  24227. + pr_warn("cannot retry for NFSv4 delegation"
  24228. + " for an internal unlink\n");
  24229. + iput(delegated);
  24230. + }
  24231. + if (unlikely(err))
  24232. + AU_MVD_PR(dmsg, "unlink failed\n");
  24233. +
  24234. + AuTraceErr(err);
  24235. + return err;
  24236. +}
  24237. +
  24238. +/* Since mvdown succeeded, we ignore an error of this function */
  24239. +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
  24240. +{
  24241. + int err;
  24242. + struct au_branch *br;
  24243. +
  24244. + a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
  24245. + br = au_sbr(a->sb, a->mvd_bsrc);
  24246. + err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
  24247. + if (!err) {
  24248. + br = au_sbr(a->sb, a->mvd_bdst);
  24249. + a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
  24250. + err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
  24251. + }
  24252. + if (!err)
  24253. + a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
  24254. + else
  24255. + AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
  24256. +}
  24257. +
  24258. +/*
  24259. + * copy-down the file and unlink the bsrc file.
  24260. + * - unlink the bdst whout if exist
  24261. + * - copy-down the file (with whtmp name and rename)
  24262. + * - unlink the bsrc file
  24263. + */
  24264. +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
  24265. +{
  24266. + int err;
  24267. +
  24268. + err = au_do_mkdir(dmsg, a);
  24269. + if (!err)
  24270. + err = au_do_lock(dmsg, a);
  24271. + if (unlikely(err))
  24272. + goto out;
  24273. +
  24274. + /*
  24275. + * do not revert the activities we made on bdst since they should be
  24276. + * harmless in aufs.
  24277. + */
  24278. +
  24279. + err = au_do_cpdown(dmsg, a);
  24280. + if (!err)
  24281. + err = au_do_unlink_wh(dmsg, a);
  24282. + if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
  24283. + err = au_do_unlink(dmsg, a);
  24284. + if (unlikely(err))
  24285. + goto out_unlock;
  24286. +
  24287. + AuDbg("%pd2, 0x%x, %d --> %d\n",
  24288. + a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
  24289. + if (find_lower_writable(a) < 0)
  24290. + a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
  24291. +
  24292. + if (a->mvdown.flags & AUFS_MVDOWN_STFS)
  24293. + au_do_stfs(dmsg, a);
  24294. +
  24295. + /* maintain internal array */
  24296. + if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
  24297. + au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
  24298. + au_set_dbtop(a->dentry, a->mvd_bdst);
  24299. + au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
  24300. + au_set_ibtop(a->inode, a->mvd_bdst);
  24301. + } else {
  24302. + /* hide the lower */
  24303. + au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
  24304. + au_set_dbbot(a->dentry, a->mvd_bsrc);
  24305. + au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
  24306. + au_set_ibbot(a->inode, a->mvd_bsrc);
  24307. + }
  24308. + if (au_dbbot(a->dentry) < a->mvd_bdst)
  24309. + au_set_dbbot(a->dentry, a->mvd_bdst);
  24310. + if (au_ibbot(a->inode) < a->mvd_bdst)
  24311. + au_set_ibbot(a->inode, a->mvd_bdst);
  24312. +
  24313. +out_unlock:
  24314. + au_do_unlock(dmsg, a);
  24315. +out:
  24316. + AuTraceErr(err);
  24317. + return err;
  24318. +}
  24319. +
  24320. +/* ---------------------------------------------------------------------- */
  24321. +
  24322. +/* make sure the file is idle */
  24323. +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
  24324. +{
  24325. + int err, plinked;
  24326. +
  24327. + err = 0;
  24328. + plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
  24329. + if (au_dbtop(a->dentry) == a->mvd_bsrc
  24330. + && au_dcount(a->dentry) == 1
  24331. + && atomic_read(&a->inode->i_count) == 1
  24332. + /* && a->mvd_h_src_inode->i_nlink == 1 */
  24333. + && (!plinked || !au_plink_test(a->inode))
  24334. + && a->inode->i_nlink == 1)
  24335. + goto out;
  24336. +
  24337. + err = -EBUSY;
  24338. + AU_MVD_PR(dmsg,
  24339. + "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
  24340. + a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
  24341. + atomic_read(&a->inode->i_count), a->inode->i_nlink,
  24342. + a->mvd_h_src_inode->i_nlink,
  24343. + plinked, plinked ? au_plink_test(a->inode) : 0);
  24344. +
  24345. +out:
  24346. + AuTraceErr(err);
  24347. + return err;
  24348. +}
  24349. +
  24350. +/* make sure the parent dir is fine */
  24351. +static int au_mvd_args_parent(const unsigned char dmsg,
  24352. + struct au_mvd_args *a)
  24353. +{
  24354. + int err;
  24355. + aufs_bindex_t bindex;
  24356. +
  24357. + err = 0;
  24358. + if (unlikely(au_alive_dir(a->parent))) {
  24359. + err = -ENOENT;
  24360. + AU_MVD_PR(dmsg, "parent dir is dead\n");
  24361. + goto out;
  24362. + }
  24363. +
  24364. + a->bopq = au_dbdiropq(a->parent);
  24365. + bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
  24366. + AuDbg("b%d\n", bindex);
  24367. + if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
  24368. + || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
  24369. + err = -EINVAL;
  24370. + a->mvd_errno = EAU_MVDOWN_OPAQUE;
  24371. + AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
  24372. + a->bopq, a->mvd_bdst);
  24373. + }
  24374. +
  24375. +out:
  24376. + AuTraceErr(err);
  24377. + return err;
  24378. +}
  24379. +
  24380. +static int au_mvd_args_intermediate(const unsigned char dmsg,
  24381. + struct au_mvd_args *a)
  24382. +{
  24383. + int err;
  24384. + struct au_dinfo *dinfo, *tmp;
  24385. +
  24386. + /* lookup the next lower positive entry */
  24387. + err = -ENOMEM;
  24388. + tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
  24389. + if (unlikely(!tmp))
  24390. + goto out;
  24391. +
  24392. + a->bfound = -1;
  24393. + a->bwh = -1;
  24394. + dinfo = au_di(a->dentry);
  24395. + au_di_cp(tmp, dinfo);
  24396. + au_di_swap(tmp, dinfo);
  24397. +
  24398. + /* returns the number of positive dentries */
  24399. + err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
  24400. + /* AuLkup_IGNORE_PERM */ 0);
  24401. + if (!err)
  24402. + a->bwh = au_dbwh(a->dentry);
  24403. + else if (err > 0)
  24404. + a->bfound = au_dbtop(a->dentry);
  24405. +
  24406. + au_di_swap(tmp, dinfo);
  24407. + au_rw_write_unlock(&tmp->di_rwsem);
  24408. + au_di_free(tmp);
  24409. + if (unlikely(err < 0))
  24410. + AU_MVD_PR(dmsg, "failed look-up lower\n");
  24411. +
  24412. + /*
  24413. + * here, we have these cases.
  24414. + * bfound == -1
  24415. + * no positive dentry under bsrc. there are more sub-cases.
  24416. + * bwh < 0
  24417. + * there no whiteout, we can safely move-down.
  24418. + * bwh <= bsrc
  24419. + * impossible
  24420. + * bsrc < bwh && bwh < bdst
  24421. + * there is a whiteout on RO branch. cannot proceed.
  24422. + * bwh == bdst
  24423. + * there is a whiteout on the RW target branch. it should
  24424. + * be removed.
  24425. + * bdst < bwh
  24426. + * there is a whiteout somewhere unrelated branch.
  24427. + * -1 < bfound && bfound <= bsrc
  24428. + * impossible.
  24429. + * bfound < bdst
  24430. + * found, but it is on RO branch between bsrc and bdst. cannot
  24431. + * proceed.
  24432. + * bfound == bdst
  24433. + * found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
  24434. + * error.
  24435. + * bdst < bfound
  24436. + * found, after we create the file on bdst, it will be hidden.
  24437. + */
  24438. +
  24439. + AuDebugOn(a->bfound == -1
  24440. + && a->bwh != -1
  24441. + && a->bwh <= a->mvd_bsrc);
  24442. + AuDebugOn(-1 < a->bfound
  24443. + && a->bfound <= a->mvd_bsrc);
  24444. +
  24445. + err = -EINVAL;
  24446. + if (a->bfound == -1
  24447. + && a->mvd_bsrc < a->bwh
  24448. + && a->bwh != -1
  24449. + && a->bwh < a->mvd_bdst) {
  24450. + a->mvd_errno = EAU_MVDOWN_WHITEOUT;
  24451. + AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
  24452. + a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
  24453. + goto out;
  24454. + } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
  24455. + a->mvd_errno = EAU_MVDOWN_UPPER;
  24456. + AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
  24457. + a->mvd_bdst, a->bfound);
  24458. + goto out;
  24459. + }
  24460. +
  24461. + err = 0; /* success */
  24462. +
  24463. +out:
  24464. + AuTraceErr(err);
  24465. + return err;
  24466. +}
  24467. +
  24468. +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
  24469. +{
  24470. + int err;
  24471. +
  24472. + err = 0;
  24473. + if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
  24474. + && a->bfound == a->mvd_bdst)
  24475. + err = -EEXIST;
  24476. + AuTraceErr(err);
  24477. + return err;
  24478. +}
  24479. +
  24480. +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
  24481. +{
  24482. + int err;
  24483. + struct au_branch *br;
  24484. +
  24485. + err = -EISDIR;
  24486. + if (unlikely(S_ISDIR(a->inode->i_mode)))
  24487. + goto out;
  24488. +
  24489. + err = -EINVAL;
  24490. + if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
  24491. + a->mvd_bsrc = au_ibtop(a->inode);
  24492. + else {
  24493. + a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
  24494. + if (unlikely(a->mvd_bsrc < 0
  24495. + || (a->mvd_bsrc < au_dbtop(a->dentry)
  24496. + || au_dbbot(a->dentry) < a->mvd_bsrc
  24497. + || !au_h_dptr(a->dentry, a->mvd_bsrc))
  24498. + || (a->mvd_bsrc < au_ibtop(a->inode)
  24499. + || au_ibbot(a->inode) < a->mvd_bsrc
  24500. + || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
  24501. + a->mvd_errno = EAU_MVDOWN_NOUPPER;
  24502. + AU_MVD_PR(dmsg, "no upper\n");
  24503. + goto out;
  24504. + }
  24505. + }
  24506. + if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
  24507. + a->mvd_errno = EAU_MVDOWN_BOTTOM;
  24508. + AU_MVD_PR(dmsg, "on the bottom\n");
  24509. + goto out;
  24510. + }
  24511. + a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
  24512. + br = au_sbr(a->sb, a->mvd_bsrc);
  24513. + err = au_br_rdonly(br);
  24514. + if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
  24515. + if (unlikely(err))
  24516. + goto out;
  24517. + } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
  24518. + || IS_APPEND(a->mvd_h_src_inode))) {
  24519. + if (err)
  24520. + a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
  24521. + /* go on */
  24522. + } else
  24523. + goto out;
  24524. +
  24525. + err = -EINVAL;
  24526. + if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
  24527. + a->mvd_bdst = find_lower_writable(a);
  24528. + if (unlikely(a->mvd_bdst < 0)) {
  24529. + a->mvd_errno = EAU_MVDOWN_BOTTOM;
  24530. + AU_MVD_PR(dmsg, "no writable lower branch\n");
  24531. + goto out;
  24532. + }
  24533. + } else {
  24534. + a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
  24535. + if (unlikely(a->mvd_bdst < 0
  24536. + || au_sbbot(a->sb) < a->mvd_bdst)) {
  24537. + a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
  24538. + AU_MVD_PR(dmsg, "no lower brid\n");
  24539. + goto out;
  24540. + }
  24541. + }
  24542. +
  24543. + err = au_mvd_args_busy(dmsg, a);
  24544. + if (!err)
  24545. + err = au_mvd_args_parent(dmsg, a);
  24546. + if (!err)
  24547. + err = au_mvd_args_intermediate(dmsg, a);
  24548. + if (!err)
  24549. + err = au_mvd_args_exist(dmsg, a);
  24550. + if (!err)
  24551. + AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
  24552. +
  24553. +out:
  24554. + AuTraceErr(err);
  24555. + return err;
  24556. +}
  24557. +
  24558. +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
  24559. +{
  24560. + int err, e;
  24561. + unsigned char dmsg;
  24562. + struct au_mvd_args *args;
  24563. + struct inode *inode;
  24564. +
  24565. + inode = d_inode(dentry);
  24566. + err = -EPERM;
  24567. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  24568. + goto out;
  24569. +
  24570. + err = -ENOMEM;
  24571. + args = kmalloc(sizeof(*args), GFP_NOFS);
  24572. + if (unlikely(!args))
  24573. + goto out;
  24574. +
  24575. + err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
  24576. + if (!err)
  24577. + /* VERIFY_WRITE */
  24578. + err = !access_ok(uarg, sizeof(*uarg));
  24579. + if (unlikely(err)) {
  24580. + err = -EFAULT;
  24581. + AuTraceErr(err);
  24582. + goto out_free;
  24583. + }
  24584. + AuDbg("flags 0x%x\n", args->mvdown.flags);
  24585. + args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
  24586. + args->mvdown.au_errno = 0;
  24587. + args->dentry = dentry;
  24588. + args->inode = inode;
  24589. + args->sb = dentry->d_sb;
  24590. +
  24591. + err = -ENOENT;
  24592. + dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
  24593. + args->parent = dget_parent(dentry);
  24594. + args->dir = d_inode(args->parent);
  24595. + inode_lock_nested(args->dir, I_MUTEX_PARENT);
  24596. + dput(args->parent);
  24597. + if (unlikely(args->parent != dentry->d_parent)) {
  24598. + AU_MVD_PR(dmsg, "parent dir is moved\n");
  24599. + goto out_dir;
  24600. + }
  24601. +
  24602. + inode_lock_nested(inode, I_MUTEX_CHILD);
  24603. + err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
  24604. + if (unlikely(err))
  24605. + goto out_inode;
  24606. +
  24607. + di_write_lock_parent(args->parent);
  24608. + err = au_mvd_args(dmsg, args);
  24609. + if (unlikely(err))
  24610. + goto out_parent;
  24611. +
  24612. + err = au_do_mvdown(dmsg, args);
  24613. + if (unlikely(err))
  24614. + goto out_parent;
  24615. +
  24616. + au_cpup_attr_timesizes(args->dir);
  24617. + au_cpup_attr_timesizes(inode);
  24618. + if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
  24619. + au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
  24620. + /* au_digen_dec(dentry); */
  24621. +
  24622. +out_parent:
  24623. + di_write_unlock(args->parent);
  24624. + aufs_read_unlock(dentry, AuLock_DW);
  24625. +out_inode:
  24626. + inode_unlock(inode);
  24627. +out_dir:
  24628. + inode_unlock(args->dir);
  24629. +out_free:
  24630. + e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
  24631. + if (unlikely(e))
  24632. + err = -EFAULT;
  24633. + au_kfree_rcu(args);
  24634. +out:
  24635. + AuTraceErr(err);
  24636. + return err;
  24637. +}
  24638. diff -Naur null/fs/aufs/opts.c linux-5.1/fs/aufs/opts.c
  24639. --- /dev/null
  24640. +++ linux-5.1/fs/aufs/opts.c 2019-05-22 11:23:07.442140950 +0200
  24641. @@ -0,0 +1,1877 @@
  24642. +// SPDX-License-Identifier: GPL-2.0
  24643. +/*
  24644. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  24645. + *
  24646. + * This program, aufs is free software; you can redistribute it and/or modify
  24647. + * it under the terms of the GNU General Public License as published by
  24648. + * the Free Software Foundation; either version 2 of the License, or
  24649. + * (at your option) any later version.
  24650. + *
  24651. + * This program is distributed in the hope that it will be useful,
  24652. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24653. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24654. + * GNU General Public License for more details.
  24655. + *
  24656. + * You should have received a copy of the GNU General Public License
  24657. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24658. + */
  24659. +
  24660. +/*
  24661. + * mount options/flags
  24662. + */
  24663. +
  24664. +#include <linux/namei.h>
  24665. +#include <linux/types.h> /* a distribution requires */
  24666. +#include <linux/parser.h>
  24667. +#include "aufs.h"
  24668. +
  24669. +/* ---------------------------------------------------------------------- */
  24670. +
  24671. +enum {
  24672. + Opt_br,
  24673. + Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
  24674. + Opt_idel, Opt_imod,
  24675. + Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
  24676. + Opt_rdblk_def, Opt_rdhash_def,
  24677. + Opt_xino, Opt_noxino,
  24678. + Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
  24679. + Opt_trunc_xino_path, Opt_itrunc_xino,
  24680. + Opt_trunc_xib, Opt_notrunc_xib,
  24681. + Opt_shwh, Opt_noshwh,
  24682. + Opt_plink, Opt_noplink, Opt_list_plink,
  24683. + Opt_udba,
  24684. + Opt_dio, Opt_nodio,
  24685. + Opt_diropq_a, Opt_diropq_w,
  24686. + Opt_warn_perm, Opt_nowarn_perm,
  24687. + Opt_wbr_copyup, Opt_wbr_create,
  24688. + Opt_fhsm_sec,
  24689. + Opt_verbose, Opt_noverbose,
  24690. + Opt_sum, Opt_nosum, Opt_wsum,
  24691. + Opt_dirperm1, Opt_nodirperm1,
  24692. + Opt_dirren, Opt_nodirren,
  24693. + Opt_acl, Opt_noacl,
  24694. + Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
  24695. +};
  24696. +
  24697. +static match_table_t options = {
  24698. + {Opt_br, "br=%s"},
  24699. + {Opt_br, "br:%s"},
  24700. +
  24701. + {Opt_add, "add=%d:%s"},
  24702. + {Opt_add, "add:%d:%s"},
  24703. + {Opt_add, "ins=%d:%s"},
  24704. + {Opt_add, "ins:%d:%s"},
  24705. + {Opt_append, "append=%s"},
  24706. + {Opt_append, "append:%s"},
  24707. + {Opt_prepend, "prepend=%s"},
  24708. + {Opt_prepend, "prepend:%s"},
  24709. +
  24710. + {Opt_del, "del=%s"},
  24711. + {Opt_del, "del:%s"},
  24712. + /* {Opt_idel, "idel:%d"}, */
  24713. + {Opt_mod, "mod=%s"},
  24714. + {Opt_mod, "mod:%s"},
  24715. + /* {Opt_imod, "imod:%d:%s"}, */
  24716. +
  24717. + {Opt_dirwh, "dirwh=%d"},
  24718. +
  24719. + {Opt_xino, "xino=%s"},
  24720. + {Opt_noxino, "noxino"},
  24721. + {Opt_trunc_xino, "trunc_xino"},
  24722. + {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
  24723. + {Opt_notrunc_xino, "notrunc_xino"},
  24724. + {Opt_trunc_xino_path, "trunc_xino=%s"},
  24725. + {Opt_itrunc_xino, "itrunc_xino=%d"},
  24726. + /* {Opt_zxino, "zxino=%s"}, */
  24727. + {Opt_trunc_xib, "trunc_xib"},
  24728. + {Opt_notrunc_xib, "notrunc_xib"},
  24729. +
  24730. +#ifdef CONFIG_PROC_FS
  24731. + {Opt_plink, "plink"},
  24732. +#else
  24733. + {Opt_ignore_silent, "plink"},
  24734. +#endif
  24735. +
  24736. + {Opt_noplink, "noplink"},
  24737. +
  24738. +#ifdef CONFIG_AUFS_DEBUG
  24739. + {Opt_list_plink, "list_plink"},
  24740. +#endif
  24741. +
  24742. + {Opt_udba, "udba=%s"},
  24743. +
  24744. + {Opt_dio, "dio"},
  24745. + {Opt_nodio, "nodio"},
  24746. +
  24747. +#ifdef CONFIG_AUFS_DIRREN
  24748. + {Opt_dirren, "dirren"},
  24749. + {Opt_nodirren, "nodirren"},
  24750. +#else
  24751. + {Opt_ignore, "dirren"},
  24752. + {Opt_ignore_silent, "nodirren"},
  24753. +#endif
  24754. +
  24755. +#ifdef CONFIG_AUFS_FHSM
  24756. + {Opt_fhsm_sec, "fhsm_sec=%d"},
  24757. +#else
  24758. + {Opt_ignore, "fhsm_sec=%d"},
  24759. +#endif
  24760. +
  24761. + {Opt_diropq_a, "diropq=always"},
  24762. + {Opt_diropq_a, "diropq=a"},
  24763. + {Opt_diropq_w, "diropq=whiteouted"},
  24764. + {Opt_diropq_w, "diropq=w"},
  24765. +
  24766. + {Opt_warn_perm, "warn_perm"},
  24767. + {Opt_nowarn_perm, "nowarn_perm"},
  24768. +
  24769. + /* keep them temporary */
  24770. + {Opt_ignore_silent, "nodlgt"},
  24771. + {Opt_ignore, "clean_plink"},
  24772. +
  24773. +#ifdef CONFIG_AUFS_SHWH
  24774. + {Opt_shwh, "shwh"},
  24775. +#endif
  24776. + {Opt_noshwh, "noshwh"},
  24777. +
  24778. + {Opt_dirperm1, "dirperm1"},
  24779. + {Opt_nodirperm1, "nodirperm1"},
  24780. +
  24781. + {Opt_verbose, "verbose"},
  24782. + {Opt_verbose, "v"},
  24783. + {Opt_noverbose, "noverbose"},
  24784. + {Opt_noverbose, "quiet"},
  24785. + {Opt_noverbose, "q"},
  24786. + {Opt_noverbose, "silent"},
  24787. +
  24788. + {Opt_sum, "sum"},
  24789. + {Opt_nosum, "nosum"},
  24790. + {Opt_wsum, "wsum"},
  24791. +
  24792. + {Opt_rdcache, "rdcache=%d"},
  24793. + {Opt_rdblk, "rdblk=%d"},
  24794. + {Opt_rdblk_def, "rdblk=def"},
  24795. + {Opt_rdhash, "rdhash=%d"},
  24796. + {Opt_rdhash_def, "rdhash=def"},
  24797. +
  24798. + {Opt_wbr_create, "create=%s"},
  24799. + {Opt_wbr_create, "create_policy=%s"},
  24800. + {Opt_wbr_copyup, "cpup=%s"},
  24801. + {Opt_wbr_copyup, "copyup=%s"},
  24802. + {Opt_wbr_copyup, "copyup_policy=%s"},
  24803. +
  24804. + /* generic VFS flag */
  24805. +#ifdef CONFIG_FS_POSIX_ACL
  24806. + {Opt_acl, "acl"},
  24807. + {Opt_noacl, "noacl"},
  24808. +#else
  24809. + {Opt_ignore, "acl"},
  24810. + {Opt_ignore_silent, "noacl"},
  24811. +#endif
  24812. +
  24813. + /* internal use for the scripts */
  24814. + {Opt_ignore_silent, "si=%s"},
  24815. +
  24816. + {Opt_br, "dirs=%s"},
  24817. + {Opt_ignore, "debug=%d"},
  24818. + {Opt_ignore, "delete=whiteout"},
  24819. + {Opt_ignore, "delete=all"},
  24820. + {Opt_ignore, "imap=%s"},
  24821. +
  24822. + /* temporary workaround, due to old mount(8)? */
  24823. + {Opt_ignore_silent, "relatime"},
  24824. +
  24825. + {Opt_err, NULL}
  24826. +};
  24827. +
  24828. +/* ---------------------------------------------------------------------- */
  24829. +
  24830. +static const char *au_parser_pattern(int val, match_table_t tbl)
  24831. +{
  24832. + struct match_token *p;
  24833. +
  24834. + p = tbl;
  24835. + while (p->pattern) {
  24836. + if (p->token == val)
  24837. + return p->pattern;
  24838. + p++;
  24839. + }
  24840. + BUG();
  24841. + return "??";
  24842. +}
  24843. +
  24844. +static const char *au_optstr(int *val, match_table_t tbl)
  24845. +{
  24846. + struct match_token *p;
  24847. + int v;
  24848. +
  24849. + v = *val;
  24850. + if (!v)
  24851. + goto out;
  24852. + p = tbl;
  24853. + while (p->pattern) {
  24854. + if (p->token
  24855. + && (v & p->token) == p->token) {
  24856. + *val &= ~p->token;
  24857. + return p->pattern;
  24858. + }
  24859. + p++;
  24860. + }
  24861. +
  24862. +out:
  24863. + return NULL;
  24864. +}
  24865. +
  24866. +/* ---------------------------------------------------------------------- */
  24867. +
  24868. +static match_table_t brperm = {
  24869. + {AuBrPerm_RO, AUFS_BRPERM_RO},
  24870. + {AuBrPerm_RR, AUFS_BRPERM_RR},
  24871. + {AuBrPerm_RW, AUFS_BRPERM_RW},
  24872. + {0, NULL}
  24873. +};
  24874. +
  24875. +static match_table_t brattr = {
  24876. + /* general */
  24877. + {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
  24878. + {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
  24879. + /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
  24880. + {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
  24881. +#ifdef CONFIG_AUFS_FHSM
  24882. + {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
  24883. +#endif
  24884. +#ifdef CONFIG_AUFS_XATTR
  24885. + {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
  24886. + {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
  24887. + {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
  24888. + {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
  24889. + {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
  24890. + {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
  24891. +#endif
  24892. +
  24893. + /* ro/rr branch */
  24894. + {AuBrRAttr_WH, AUFS_BRRATTR_WH},
  24895. +
  24896. + /* rw branch */
  24897. + {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
  24898. + {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
  24899. +
  24900. + {0, NULL}
  24901. +};
  24902. +
  24903. +static int br_attr_val(char *str, match_table_t table, substring_t args[])
  24904. +{
  24905. + int attr, v;
  24906. + char *p;
  24907. +
  24908. + attr = 0;
  24909. + do {
  24910. + p = strchr(str, '+');
  24911. + if (p)
  24912. + *p = 0;
  24913. + v = match_token(str, table, args);
  24914. + if (v) {
  24915. + if (v & AuBrAttr_CMOO_Mask)
  24916. + attr &= ~AuBrAttr_CMOO_Mask;
  24917. + attr |= v;
  24918. + } else {
  24919. + if (p)
  24920. + *p = '+';
  24921. + pr_warn("ignored branch attribute %s\n", str);
  24922. + break;
  24923. + }
  24924. + if (p)
  24925. + str = p + 1;
  24926. + } while (p);
  24927. +
  24928. + return attr;
  24929. +}
  24930. +
  24931. +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
  24932. +{
  24933. + int sz;
  24934. + const char *p;
  24935. + char *q;
  24936. +
  24937. + q = str->a;
  24938. + *q = 0;
  24939. + p = au_optstr(&perm, brattr);
  24940. + if (p) {
  24941. + sz = strlen(p);
  24942. + memcpy(q, p, sz + 1);
  24943. + q += sz;
  24944. + } else
  24945. + goto out;
  24946. +
  24947. + do {
  24948. + p = au_optstr(&perm, brattr);
  24949. + if (p) {
  24950. + *q++ = '+';
  24951. + sz = strlen(p);
  24952. + memcpy(q, p, sz + 1);
  24953. + q += sz;
  24954. + }
  24955. + } while (p);
  24956. +
  24957. +out:
  24958. + return q - str->a;
  24959. +}
  24960. +
  24961. +static int noinline_for_stack br_perm_val(char *perm)
  24962. +{
  24963. + int val, bad, sz;
  24964. + char *p;
  24965. + substring_t args[MAX_OPT_ARGS];
  24966. + au_br_perm_str_t attr;
  24967. +
  24968. + p = strchr(perm, '+');
  24969. + if (p)
  24970. + *p = 0;
  24971. + val = match_token(perm, brperm, args);
  24972. + if (!val) {
  24973. + if (p)
  24974. + *p = '+';
  24975. + pr_warn("ignored branch permission %s\n", perm);
  24976. + val = AuBrPerm_RO;
  24977. + goto out;
  24978. + }
  24979. + if (!p)
  24980. + goto out;
  24981. +
  24982. + val |= br_attr_val(p + 1, brattr, args);
  24983. +
  24984. + bad = 0;
  24985. + switch (val & AuBrPerm_Mask) {
  24986. + case AuBrPerm_RO:
  24987. + case AuBrPerm_RR:
  24988. + bad = val & AuBrWAttr_Mask;
  24989. + val &= ~AuBrWAttr_Mask;
  24990. + break;
  24991. + case AuBrPerm_RW:
  24992. + bad = val & AuBrRAttr_Mask;
  24993. + val &= ~AuBrRAttr_Mask;
  24994. + break;
  24995. + }
  24996. +
  24997. + /*
  24998. + * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
  24999. + * does not treat it as an error, just warning.
  25000. + * this is a tiny guard for the user operation.
  25001. + */
  25002. + if (val & AuBrAttr_UNPIN) {
  25003. + bad |= AuBrAttr_UNPIN;
  25004. + val &= ~AuBrAttr_UNPIN;
  25005. + }
  25006. +
  25007. + if (unlikely(bad)) {
  25008. + sz = au_do_optstr_br_attr(&attr, bad);
  25009. + AuDebugOn(!sz);
  25010. + pr_warn("ignored branch attribute %s\n", attr.a);
  25011. + }
  25012. +
  25013. +out:
  25014. + return val;
  25015. +}
  25016. +
  25017. +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
  25018. +{
  25019. + au_br_perm_str_t attr;
  25020. + const char *p;
  25021. + char *q;
  25022. + int sz;
  25023. +
  25024. + q = str->a;
  25025. + p = au_optstr(&perm, brperm);
  25026. + AuDebugOn(!p || !*p);
  25027. + sz = strlen(p);
  25028. + memcpy(q, p, sz + 1);
  25029. + q += sz;
  25030. +
  25031. + sz = au_do_optstr_br_attr(&attr, perm);
  25032. + if (sz) {
  25033. + *q++ = '+';
  25034. + memcpy(q, attr.a, sz + 1);
  25035. + }
  25036. +
  25037. + AuDebugOn(strlen(str->a) >= sizeof(str->a));
  25038. +}
  25039. +
  25040. +/* ---------------------------------------------------------------------- */
  25041. +
  25042. +static match_table_t udbalevel = {
  25043. + {AuOpt_UDBA_REVAL, "reval"},
  25044. + {AuOpt_UDBA_NONE, "none"},
  25045. +#ifdef CONFIG_AUFS_HNOTIFY
  25046. + {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
  25047. +#ifdef CONFIG_AUFS_HFSNOTIFY
  25048. + {AuOpt_UDBA_HNOTIFY, "fsnotify"},
  25049. +#endif
  25050. +#endif
  25051. + {-1, NULL}
  25052. +};
  25053. +
  25054. +static int noinline_for_stack udba_val(char *str)
  25055. +{
  25056. + substring_t args[MAX_OPT_ARGS];
  25057. +
  25058. + return match_token(str, udbalevel, args);
  25059. +}
  25060. +
  25061. +const char *au_optstr_udba(int udba)
  25062. +{
  25063. + return au_parser_pattern(udba, udbalevel);
  25064. +}
  25065. +
  25066. +/* ---------------------------------------------------------------------- */
  25067. +
  25068. +static match_table_t au_wbr_create_policy = {
  25069. + {AuWbrCreate_TDP, "tdp"},
  25070. + {AuWbrCreate_TDP, "top-down-parent"},
  25071. + {AuWbrCreate_RR, "rr"},
  25072. + {AuWbrCreate_RR, "round-robin"},
  25073. + {AuWbrCreate_MFS, "mfs"},
  25074. + {AuWbrCreate_MFS, "most-free-space"},
  25075. + {AuWbrCreate_MFSV, "mfs:%d"},
  25076. + {AuWbrCreate_MFSV, "most-free-space:%d"},
  25077. +
  25078. + /* top-down regardless the parent, and then mfs */
  25079. + {AuWbrCreate_TDMFS, "tdmfs:%d"},
  25080. + {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
  25081. +
  25082. + {AuWbrCreate_MFSRR, "mfsrr:%d"},
  25083. + {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
  25084. + {AuWbrCreate_PMFS, "pmfs"},
  25085. + {AuWbrCreate_PMFSV, "pmfs:%d"},
  25086. + {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
  25087. + {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
  25088. +
  25089. + {-1, NULL}
  25090. +};
  25091. +
  25092. +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
  25093. + struct au_opt_wbr_create *create)
  25094. +{
  25095. + int err;
  25096. + unsigned long long ull;
  25097. +
  25098. + err = 0;
  25099. + if (!match_u64(arg, &ull))
  25100. + create->mfsrr_watermark = ull;
  25101. + else {
  25102. + pr_err("bad integer in %s\n", str);
  25103. + err = -EINVAL;
  25104. + }
  25105. +
  25106. + return err;
  25107. +}
  25108. +
  25109. +static int au_wbr_mfs_sec(substring_t *arg, char *str,
  25110. + struct au_opt_wbr_create *create)
  25111. +{
  25112. + int n, err;
  25113. +
  25114. + err = 0;
  25115. + if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
  25116. + create->mfs_second = n;
  25117. + else {
  25118. + pr_err("bad integer in %s\n", str);
  25119. + err = -EINVAL;
  25120. + }
  25121. +
  25122. + return err;
  25123. +}
  25124. +
  25125. +static int noinline_for_stack
  25126. +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
  25127. +{
  25128. + int err, e;
  25129. + substring_t args[MAX_OPT_ARGS];
  25130. +
  25131. + err = match_token(str, au_wbr_create_policy, args);
  25132. + create->wbr_create = err;
  25133. + switch (err) {
  25134. + case AuWbrCreate_MFSRRV:
  25135. + case AuWbrCreate_TDMFSV:
  25136. + case AuWbrCreate_PMFSRRV:
  25137. + e = au_wbr_mfs_wmark(&args[0], str, create);
  25138. + if (!e)
  25139. + e = au_wbr_mfs_sec(&args[1], str, create);
  25140. + if (unlikely(e))
  25141. + err = e;
  25142. + break;
  25143. + case AuWbrCreate_MFSRR:
  25144. + case AuWbrCreate_TDMFS:
  25145. + case AuWbrCreate_PMFSRR:
  25146. + e = au_wbr_mfs_wmark(&args[0], str, create);
  25147. + if (unlikely(e)) {
  25148. + err = e;
  25149. + break;
  25150. + }
  25151. + /*FALLTHROUGH*/
  25152. + case AuWbrCreate_MFS:
  25153. + case AuWbrCreate_PMFS:
  25154. + create->mfs_second = AUFS_MFS_DEF_SEC;
  25155. + break;
  25156. + case AuWbrCreate_MFSV:
  25157. + case AuWbrCreate_PMFSV:
  25158. + e = au_wbr_mfs_sec(&args[0], str, create);
  25159. + if (unlikely(e))
  25160. + err = e;
  25161. + break;
  25162. + }
  25163. +
  25164. + return err;
  25165. +}
  25166. +
  25167. +const char *au_optstr_wbr_create(int wbr_create)
  25168. +{
  25169. + return au_parser_pattern(wbr_create, au_wbr_create_policy);
  25170. +}
  25171. +
  25172. +static match_table_t au_wbr_copyup_policy = {
  25173. + {AuWbrCopyup_TDP, "tdp"},
  25174. + {AuWbrCopyup_TDP, "top-down-parent"},
  25175. + {AuWbrCopyup_BUP, "bup"},
  25176. + {AuWbrCopyup_BUP, "bottom-up-parent"},
  25177. + {AuWbrCopyup_BU, "bu"},
  25178. + {AuWbrCopyup_BU, "bottom-up"},
  25179. + {-1, NULL}
  25180. +};
  25181. +
  25182. +static int noinline_for_stack au_wbr_copyup_val(char *str)
  25183. +{
  25184. + substring_t args[MAX_OPT_ARGS];
  25185. +
  25186. + return match_token(str, au_wbr_copyup_policy, args);
  25187. +}
  25188. +
  25189. +const char *au_optstr_wbr_copyup(int wbr_copyup)
  25190. +{
  25191. + return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
  25192. +}
  25193. +
  25194. +/* ---------------------------------------------------------------------- */
  25195. +
  25196. +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
  25197. +
  25198. +static void dump_opts(struct au_opts *opts)
  25199. +{
  25200. +#ifdef CONFIG_AUFS_DEBUG
  25201. + /* reduce stack space */
  25202. + union {
  25203. + struct au_opt_add *add;
  25204. + struct au_opt_del *del;
  25205. + struct au_opt_mod *mod;
  25206. + struct au_opt_xino *xino;
  25207. + struct au_opt_xino_itrunc *xino_itrunc;
  25208. + struct au_opt_wbr_create *create;
  25209. + } u;
  25210. + struct au_opt *opt;
  25211. +
  25212. + opt = opts->opt;
  25213. + while (opt->type != Opt_tail) {
  25214. + switch (opt->type) {
  25215. + case Opt_add:
  25216. + u.add = &opt->add;
  25217. + AuDbg("add {b%d, %s, 0x%x, %p}\n",
  25218. + u.add->bindex, u.add->pathname, u.add->perm,
  25219. + u.add->path.dentry);
  25220. + break;
  25221. + case Opt_del:
  25222. + case Opt_idel:
  25223. + u.del = &opt->del;
  25224. + AuDbg("del {%s, %p}\n",
  25225. + u.del->pathname, u.del->h_path.dentry);
  25226. + break;
  25227. + case Opt_mod:
  25228. + case Opt_imod:
  25229. + u.mod = &opt->mod;
  25230. + AuDbg("mod {%s, 0x%x, %p}\n",
  25231. + u.mod->path, u.mod->perm, u.mod->h_root);
  25232. + break;
  25233. + case Opt_append:
  25234. + u.add = &opt->add;
  25235. + AuDbg("append {b%d, %s, 0x%x, %p}\n",
  25236. + u.add->bindex, u.add->pathname, u.add->perm,
  25237. + u.add->path.dentry);
  25238. + break;
  25239. + case Opt_prepend:
  25240. + u.add = &opt->add;
  25241. + AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
  25242. + u.add->bindex, u.add->pathname, u.add->perm,
  25243. + u.add->path.dentry);
  25244. + break;
  25245. + case Opt_dirwh:
  25246. + AuDbg("dirwh %d\n", opt->dirwh);
  25247. + break;
  25248. + case Opt_rdcache:
  25249. + AuDbg("rdcache %d\n", opt->rdcache);
  25250. + break;
  25251. + case Opt_rdblk:
  25252. + AuDbg("rdblk %u\n", opt->rdblk);
  25253. + break;
  25254. + case Opt_rdblk_def:
  25255. + AuDbg("rdblk_def\n");
  25256. + break;
  25257. + case Opt_rdhash:
  25258. + AuDbg("rdhash %u\n", opt->rdhash);
  25259. + break;
  25260. + case Opt_rdhash_def:
  25261. + AuDbg("rdhash_def\n");
  25262. + break;
  25263. + case Opt_xino:
  25264. + u.xino = &opt->xino;
  25265. + AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
  25266. + break;
  25267. + case Opt_trunc_xino:
  25268. + AuLabel(trunc_xino);
  25269. + break;
  25270. + case Opt_notrunc_xino:
  25271. + AuLabel(notrunc_xino);
  25272. + break;
  25273. + case Opt_trunc_xino_path:
  25274. + case Opt_itrunc_xino:
  25275. + u.xino_itrunc = &opt->xino_itrunc;
  25276. + AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
  25277. + break;
  25278. + case Opt_noxino:
  25279. + AuLabel(noxino);
  25280. + break;
  25281. + case Opt_trunc_xib:
  25282. + AuLabel(trunc_xib);
  25283. + break;
  25284. + case Opt_notrunc_xib:
  25285. + AuLabel(notrunc_xib);
  25286. + break;
  25287. + case Opt_shwh:
  25288. + AuLabel(shwh);
  25289. + break;
  25290. + case Opt_noshwh:
  25291. + AuLabel(noshwh);
  25292. + break;
  25293. + case Opt_dirperm1:
  25294. + AuLabel(dirperm1);
  25295. + break;
  25296. + case Opt_nodirperm1:
  25297. + AuLabel(nodirperm1);
  25298. + break;
  25299. + case Opt_plink:
  25300. + AuLabel(plink);
  25301. + break;
  25302. + case Opt_noplink:
  25303. + AuLabel(noplink);
  25304. + break;
  25305. + case Opt_list_plink:
  25306. + AuLabel(list_plink);
  25307. + break;
  25308. + case Opt_udba:
  25309. + AuDbg("udba %d, %s\n",
  25310. + opt->udba, au_optstr_udba(opt->udba));
  25311. + break;
  25312. + case Opt_dio:
  25313. + AuLabel(dio);
  25314. + break;
  25315. + case Opt_nodio:
  25316. + AuLabel(nodio);
  25317. + break;
  25318. + case Opt_diropq_a:
  25319. + AuLabel(diropq_a);
  25320. + break;
  25321. + case Opt_diropq_w:
  25322. + AuLabel(diropq_w);
  25323. + break;
  25324. + case Opt_warn_perm:
  25325. + AuLabel(warn_perm);
  25326. + break;
  25327. + case Opt_nowarn_perm:
  25328. + AuLabel(nowarn_perm);
  25329. + break;
  25330. + case Opt_verbose:
  25331. + AuLabel(verbose);
  25332. + break;
  25333. + case Opt_noverbose:
  25334. + AuLabel(noverbose);
  25335. + break;
  25336. + case Opt_sum:
  25337. + AuLabel(sum);
  25338. + break;
  25339. + case Opt_nosum:
  25340. + AuLabel(nosum);
  25341. + break;
  25342. + case Opt_wsum:
  25343. + AuLabel(wsum);
  25344. + break;
  25345. + case Opt_wbr_create:
  25346. + u.create = &opt->wbr_create;
  25347. + AuDbg("create %d, %s\n", u.create->wbr_create,
  25348. + au_optstr_wbr_create(u.create->wbr_create));
  25349. + switch (u.create->wbr_create) {
  25350. + case AuWbrCreate_MFSV:
  25351. + case AuWbrCreate_PMFSV:
  25352. + AuDbg("%d sec\n", u.create->mfs_second);
  25353. + break;
  25354. + case AuWbrCreate_MFSRR:
  25355. + case AuWbrCreate_TDMFS:
  25356. + AuDbg("%llu watermark\n",
  25357. + u.create->mfsrr_watermark);
  25358. + break;
  25359. + case AuWbrCreate_MFSRRV:
  25360. + case AuWbrCreate_TDMFSV:
  25361. + case AuWbrCreate_PMFSRRV:
  25362. + AuDbg("%llu watermark, %d sec\n",
  25363. + u.create->mfsrr_watermark,
  25364. + u.create->mfs_second);
  25365. + break;
  25366. + }
  25367. + break;
  25368. + case Opt_wbr_copyup:
  25369. + AuDbg("copyup %d, %s\n", opt->wbr_copyup,
  25370. + au_optstr_wbr_copyup(opt->wbr_copyup));
  25371. + break;
  25372. + case Opt_fhsm_sec:
  25373. + AuDbg("fhsm_sec %u\n", opt->fhsm_second);
  25374. + break;
  25375. + case Opt_dirren:
  25376. + AuLabel(dirren);
  25377. + break;
  25378. + case Opt_nodirren:
  25379. + AuLabel(nodirren);
  25380. + break;
  25381. + case Opt_acl:
  25382. + AuLabel(acl);
  25383. + break;
  25384. + case Opt_noacl:
  25385. + AuLabel(noacl);
  25386. + break;
  25387. + default:
  25388. + BUG();
  25389. + }
  25390. + opt++;
  25391. + }
  25392. +#endif
  25393. +}
  25394. +
  25395. +void au_opts_free(struct au_opts *opts)
  25396. +{
  25397. + struct au_opt *opt;
  25398. +
  25399. + opt = opts->opt;
  25400. + while (opt->type != Opt_tail) {
  25401. + switch (opt->type) {
  25402. + case Opt_add:
  25403. + case Opt_append:
  25404. + case Opt_prepend:
  25405. + path_put(&opt->add.path);
  25406. + break;
  25407. + case Opt_del:
  25408. + case Opt_idel:
  25409. + path_put(&opt->del.h_path);
  25410. + break;
  25411. + case Opt_mod:
  25412. + case Opt_imod:
  25413. + dput(opt->mod.h_root);
  25414. + break;
  25415. + case Opt_xino:
  25416. + fput(opt->xino.file);
  25417. + break;
  25418. + }
  25419. + opt++;
  25420. + }
  25421. +}
  25422. +
  25423. +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
  25424. + aufs_bindex_t bindex)
  25425. +{
  25426. + int err;
  25427. + struct au_opt_add *add = &opt->add;
  25428. + char *p;
  25429. +
  25430. + add->bindex = bindex;
  25431. + add->perm = AuBrPerm_RO;
  25432. + add->pathname = opt_str;
  25433. + p = strchr(opt_str, '=');
  25434. + if (p) {
  25435. + *p++ = 0;
  25436. + if (*p)
  25437. + add->perm = br_perm_val(p);
  25438. + }
  25439. +
  25440. + err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
  25441. + if (!err) {
  25442. + if (!p) {
  25443. + add->perm = AuBrPerm_RO;
  25444. + if (au_test_fs_rr(add->path.dentry->d_sb))
  25445. + add->perm = AuBrPerm_RR;
  25446. + else if (!bindex && !(sb_flags & SB_RDONLY))
  25447. + add->perm = AuBrPerm_RW;
  25448. + }
  25449. + opt->type = Opt_add;
  25450. + goto out;
  25451. + }
  25452. + pr_err("lookup failed %s (%d)\n", add->pathname, err);
  25453. + err = -EINVAL;
  25454. +
  25455. +out:
  25456. + return err;
  25457. +}
  25458. +
  25459. +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
  25460. +{
  25461. + int err;
  25462. +
  25463. + del->pathname = args[0].from;
  25464. + AuDbg("del path %s\n", del->pathname);
  25465. +
  25466. + err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
  25467. + if (unlikely(err))
  25468. + pr_err("lookup failed %s (%d)\n", del->pathname, err);
  25469. +
  25470. + return err;
  25471. +}
  25472. +
  25473. +#if 0 /* reserved for future use */
  25474. +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
  25475. + struct au_opt_del *del, substring_t args[])
  25476. +{
  25477. + int err;
  25478. + struct dentry *root;
  25479. +
  25480. + err = -EINVAL;
  25481. + root = sb->s_root;
  25482. + aufs_read_lock(root, AuLock_FLUSH);
  25483. + if (bindex < 0 || au_sbbot(sb) < bindex) {
  25484. + pr_err("out of bounds, %d\n", bindex);
  25485. + goto out;
  25486. + }
  25487. +
  25488. + err = 0;
  25489. + del->h_path.dentry = dget(au_h_dptr(root, bindex));
  25490. + del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
  25491. +
  25492. +out:
  25493. + aufs_read_unlock(root, !AuLock_IR);
  25494. + return err;
  25495. +}
  25496. +#endif
  25497. +
  25498. +static int noinline_for_stack
  25499. +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
  25500. +{
  25501. + int err;
  25502. + struct path path;
  25503. + char *p;
  25504. +
  25505. + err = -EINVAL;
  25506. + mod->path = args[0].from;
  25507. + p = strchr(mod->path, '=');
  25508. + if (unlikely(!p)) {
  25509. + pr_err("no permission %s\n", args[0].from);
  25510. + goto out;
  25511. + }
  25512. +
  25513. + *p++ = 0;
  25514. + err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
  25515. + if (unlikely(err)) {
  25516. + pr_err("lookup failed %s (%d)\n", mod->path, err);
  25517. + goto out;
  25518. + }
  25519. +
  25520. + mod->perm = br_perm_val(p);
  25521. + AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
  25522. + mod->h_root = dget(path.dentry);
  25523. + path_put(&path);
  25524. +
  25525. +out:
  25526. + return err;
  25527. +}
  25528. +
  25529. +#if 0 /* reserved for future use */
  25530. +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
  25531. + struct au_opt_mod *mod, substring_t args[])
  25532. +{
  25533. + int err;
  25534. + struct dentry *root;
  25535. +
  25536. + err = -EINVAL;
  25537. + root = sb->s_root;
  25538. + aufs_read_lock(root, AuLock_FLUSH);
  25539. + if (bindex < 0 || au_sbbot(sb) < bindex) {
  25540. + pr_err("out of bounds, %d\n", bindex);
  25541. + goto out;
  25542. + }
  25543. +
  25544. + err = 0;
  25545. + mod->perm = br_perm_val(args[1].from);
  25546. + AuDbg("mod path %s, perm 0x%x, %s\n",
  25547. + mod->path, mod->perm, args[1].from);
  25548. + mod->h_root = dget(au_h_dptr(root, bindex));
  25549. +
  25550. +out:
  25551. + aufs_read_unlock(root, !AuLock_IR);
  25552. + return err;
  25553. +}
  25554. +#endif
  25555. +
  25556. +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
  25557. + substring_t args[])
  25558. +{
  25559. + int err;
  25560. + struct file *file;
  25561. +
  25562. + file = au_xino_create(sb, args[0].from, /*silent*/0);
  25563. + err = PTR_ERR(file);
  25564. + if (IS_ERR(file))
  25565. + goto out;
  25566. +
  25567. + err = -EINVAL;
  25568. + if (unlikely(file->f_path.dentry->d_sb == sb)) {
  25569. + fput(file);
  25570. + pr_err("%s must be outside\n", args[0].from);
  25571. + goto out;
  25572. + }
  25573. +
  25574. + err = 0;
  25575. + xino->file = file;
  25576. + xino->path = args[0].from;
  25577. +
  25578. +out:
  25579. + return err;
  25580. +}
  25581. +
  25582. +static int noinline_for_stack
  25583. +au_opts_parse_xino_itrunc_path(struct super_block *sb,
  25584. + struct au_opt_xino_itrunc *xino_itrunc,
  25585. + substring_t args[])
  25586. +{
  25587. + int err;
  25588. + aufs_bindex_t bbot, bindex;
  25589. + struct path path;
  25590. + struct dentry *root;
  25591. +
  25592. + err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
  25593. + if (unlikely(err)) {
  25594. + pr_err("lookup failed %s (%d)\n", args[0].from, err);
  25595. + goto out;
  25596. + }
  25597. +
  25598. + xino_itrunc->bindex = -1;
  25599. + root = sb->s_root;
  25600. + aufs_read_lock(root, AuLock_FLUSH);
  25601. + bbot = au_sbbot(sb);
  25602. + for (bindex = 0; bindex <= bbot; bindex++) {
  25603. + if (au_h_dptr(root, bindex) == path.dentry) {
  25604. + xino_itrunc->bindex = bindex;
  25605. + break;
  25606. + }
  25607. + }
  25608. + aufs_read_unlock(root, !AuLock_IR);
  25609. + path_put(&path);
  25610. +
  25611. + if (unlikely(xino_itrunc->bindex < 0)) {
  25612. + pr_err("no such branch %s\n", args[0].from);
  25613. + err = -EINVAL;
  25614. + }
  25615. +
  25616. +out:
  25617. + return err;
  25618. +}
  25619. +
  25620. +/* called without aufs lock */
  25621. +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
  25622. +{
  25623. + int err, n, token;
  25624. + aufs_bindex_t bindex;
  25625. + unsigned char skipped;
  25626. + struct dentry *root;
  25627. + struct au_opt *opt, *opt_tail;
  25628. + char *opt_str;
  25629. + /* reduce the stack space */
  25630. + union {
  25631. + struct au_opt_xino_itrunc *xino_itrunc;
  25632. + struct au_opt_wbr_create *create;
  25633. + } u;
  25634. + struct {
  25635. + substring_t args[MAX_OPT_ARGS];
  25636. + } *a;
  25637. +
  25638. + err = -ENOMEM;
  25639. + a = kmalloc(sizeof(*a), GFP_NOFS);
  25640. + if (unlikely(!a))
  25641. + goto out;
  25642. +
  25643. + root = sb->s_root;
  25644. + err = 0;
  25645. + bindex = 0;
  25646. + opt = opts->opt;
  25647. + opt_tail = opt + opts->max_opt - 1;
  25648. + opt->type = Opt_tail;
  25649. + while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
  25650. + err = -EINVAL;
  25651. + skipped = 0;
  25652. + token = match_token(opt_str, options, a->args);
  25653. + switch (token) {
  25654. + case Opt_br:
  25655. + err = 0;
  25656. + while (!err && (opt_str = strsep(&a->args[0].from, ":"))
  25657. + && *opt_str) {
  25658. + err = opt_add(opt, opt_str, opts->sb_flags,
  25659. + bindex++);
  25660. + if (unlikely(!err && ++opt > opt_tail)) {
  25661. + err = -E2BIG;
  25662. + break;
  25663. + }
  25664. + opt->type = Opt_tail;
  25665. + skipped = 1;
  25666. + }
  25667. + break;
  25668. + case Opt_add:
  25669. + if (unlikely(match_int(&a->args[0], &n))) {
  25670. + pr_err("bad integer in %s\n", opt_str);
  25671. + break;
  25672. + }
  25673. + bindex = n;
  25674. + err = opt_add(opt, a->args[1].from, opts->sb_flags,
  25675. + bindex);
  25676. + if (!err)
  25677. + opt->type = token;
  25678. + break;
  25679. + case Opt_append:
  25680. + err = opt_add(opt, a->args[0].from, opts->sb_flags,
  25681. + /*dummy bindex*/1);
  25682. + if (!err)
  25683. + opt->type = token;
  25684. + break;
  25685. + case Opt_prepend:
  25686. + err = opt_add(opt, a->args[0].from, opts->sb_flags,
  25687. + /*bindex*/0);
  25688. + if (!err)
  25689. + opt->type = token;
  25690. + break;
  25691. + case Opt_del:
  25692. + err = au_opts_parse_del(&opt->del, a->args);
  25693. + if (!err)
  25694. + opt->type = token;
  25695. + break;
  25696. +#if 0 /* reserved for future use */
  25697. + case Opt_idel:
  25698. + del->pathname = "(indexed)";
  25699. + if (unlikely(match_int(&args[0], &n))) {
  25700. + pr_err("bad integer in %s\n", opt_str);
  25701. + break;
  25702. + }
  25703. + err = au_opts_parse_idel(sb, n, &opt->del, a->args);
  25704. + if (!err)
  25705. + opt->type = token;
  25706. + break;
  25707. +#endif
  25708. + case Opt_mod:
  25709. + err = au_opts_parse_mod(&opt->mod, a->args);
  25710. + if (!err)
  25711. + opt->type = token;
  25712. + break;
  25713. +#ifdef IMOD /* reserved for future use */
  25714. + case Opt_imod:
  25715. + u.mod->path = "(indexed)";
  25716. + if (unlikely(match_int(&a->args[0], &n))) {
  25717. + pr_err("bad integer in %s\n", opt_str);
  25718. + break;
  25719. + }
  25720. + err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
  25721. + if (!err)
  25722. + opt->type = token;
  25723. + break;
  25724. +#endif
  25725. + case Opt_xino:
  25726. + err = au_opts_parse_xino(sb, &opt->xino, a->args);
  25727. + if (!err)
  25728. + opt->type = token;
  25729. + break;
  25730. +
  25731. + case Opt_trunc_xino_path:
  25732. + err = au_opts_parse_xino_itrunc_path
  25733. + (sb, &opt->xino_itrunc, a->args);
  25734. + if (!err)
  25735. + opt->type = token;
  25736. + break;
  25737. +
  25738. + case Opt_itrunc_xino:
  25739. + u.xino_itrunc = &opt->xino_itrunc;
  25740. + if (unlikely(match_int(&a->args[0], &n))) {
  25741. + pr_err("bad integer in %s\n", opt_str);
  25742. + break;
  25743. + }
  25744. + u.xino_itrunc->bindex = n;
  25745. + aufs_read_lock(root, AuLock_FLUSH);
  25746. + if (n < 0 || au_sbbot(sb) < n) {
  25747. + pr_err("out of bounds, %d\n", n);
  25748. + aufs_read_unlock(root, !AuLock_IR);
  25749. + break;
  25750. + }
  25751. + aufs_read_unlock(root, !AuLock_IR);
  25752. + err = 0;
  25753. + opt->type = token;
  25754. + break;
  25755. +
  25756. + case Opt_dirwh:
  25757. + if (unlikely(match_int(&a->args[0], &opt->dirwh)))
  25758. + break;
  25759. + err = 0;
  25760. + opt->type = token;
  25761. + break;
  25762. +
  25763. + case Opt_rdcache:
  25764. + if (unlikely(match_int(&a->args[0], &n))) {
  25765. + pr_err("bad integer in %s\n", opt_str);
  25766. + break;
  25767. + }
  25768. + if (unlikely(n > AUFS_RDCACHE_MAX)) {
  25769. + pr_err("rdcache must be smaller than %d\n",
  25770. + AUFS_RDCACHE_MAX);
  25771. + break;
  25772. + }
  25773. + opt->rdcache = n;
  25774. + err = 0;
  25775. + opt->type = token;
  25776. + break;
  25777. + case Opt_rdblk:
  25778. + if (unlikely(match_int(&a->args[0], &n)
  25779. + || n < 0
  25780. + || n > KMALLOC_MAX_SIZE)) {
  25781. + pr_err("bad integer in %s\n", opt_str);
  25782. + break;
  25783. + }
  25784. + if (unlikely(n && n < NAME_MAX)) {
  25785. + pr_err("rdblk must be larger than %d\n",
  25786. + NAME_MAX);
  25787. + break;
  25788. + }
  25789. + opt->rdblk = n;
  25790. + err = 0;
  25791. + opt->type = token;
  25792. + break;
  25793. + case Opt_rdhash:
  25794. + if (unlikely(match_int(&a->args[0], &n)
  25795. + || n < 0
  25796. + || n * sizeof(struct hlist_head)
  25797. + > KMALLOC_MAX_SIZE)) {
  25798. + pr_err("bad integer in %s\n", opt_str);
  25799. + break;
  25800. + }
  25801. + opt->rdhash = n;
  25802. + err = 0;
  25803. + opt->type = token;
  25804. + break;
  25805. +
  25806. + case Opt_trunc_xino:
  25807. + case Opt_notrunc_xino:
  25808. + case Opt_noxino:
  25809. + case Opt_trunc_xib:
  25810. + case Opt_notrunc_xib:
  25811. + case Opt_shwh:
  25812. + case Opt_noshwh:
  25813. + case Opt_dirperm1:
  25814. + case Opt_nodirperm1:
  25815. + case Opt_plink:
  25816. + case Opt_noplink:
  25817. + case Opt_list_plink:
  25818. + case Opt_dio:
  25819. + case Opt_nodio:
  25820. + case Opt_diropq_a:
  25821. + case Opt_diropq_w:
  25822. + case Opt_warn_perm:
  25823. + case Opt_nowarn_perm:
  25824. + case Opt_verbose:
  25825. + case Opt_noverbose:
  25826. + case Opt_sum:
  25827. + case Opt_nosum:
  25828. + case Opt_wsum:
  25829. + case Opt_rdblk_def:
  25830. + case Opt_rdhash_def:
  25831. + case Opt_dirren:
  25832. + case Opt_nodirren:
  25833. + case Opt_acl:
  25834. + case Opt_noacl:
  25835. + err = 0;
  25836. + opt->type = token;
  25837. + break;
  25838. +
  25839. + case Opt_udba:
  25840. + opt->udba = udba_val(a->args[0].from);
  25841. + if (opt->udba >= 0) {
  25842. + err = 0;
  25843. + opt->type = token;
  25844. + } else
  25845. + pr_err("wrong value, %s\n", opt_str);
  25846. + break;
  25847. +
  25848. + case Opt_wbr_create:
  25849. + u.create = &opt->wbr_create;
  25850. + u.create->wbr_create
  25851. + = au_wbr_create_val(a->args[0].from, u.create);
  25852. + if (u.create->wbr_create >= 0) {
  25853. + err = 0;
  25854. + opt->type = token;
  25855. + } else
  25856. + pr_err("wrong value, %s\n", opt_str);
  25857. + break;
  25858. + case Opt_wbr_copyup:
  25859. + opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
  25860. + if (opt->wbr_copyup >= 0) {
  25861. + err = 0;
  25862. + opt->type = token;
  25863. + } else
  25864. + pr_err("wrong value, %s\n", opt_str);
  25865. + break;
  25866. +
  25867. + case Opt_fhsm_sec:
  25868. + if (unlikely(match_int(&a->args[0], &n)
  25869. + || n < 0)) {
  25870. + pr_err("bad integer in %s\n", opt_str);
  25871. + break;
  25872. + }
  25873. + if (sysaufs_brs) {
  25874. + opt->fhsm_second = n;
  25875. + opt->type = token;
  25876. + } else
  25877. + pr_warn("ignored %s\n", opt_str);
  25878. + err = 0;
  25879. + break;
  25880. +
  25881. + case Opt_ignore:
  25882. + pr_warn("ignored %s\n", opt_str);
  25883. + /*FALLTHROUGH*/
  25884. + case Opt_ignore_silent:
  25885. + skipped = 1;
  25886. + err = 0;
  25887. + break;
  25888. + case Opt_err:
  25889. + pr_err("unknown option %s\n", opt_str);
  25890. + break;
  25891. + }
  25892. +
  25893. + if (!err && !skipped) {
  25894. + if (unlikely(++opt > opt_tail)) {
  25895. + err = -E2BIG;
  25896. + opt--;
  25897. + opt->type = Opt_tail;
  25898. + break;
  25899. + }
  25900. + opt->type = Opt_tail;
  25901. + }
  25902. + }
  25903. +
  25904. + au_kfree_rcu(a);
  25905. + dump_opts(opts);
  25906. + if (unlikely(err))
  25907. + au_opts_free(opts);
  25908. +
  25909. +out:
  25910. + return err;
  25911. +}
  25912. +
  25913. +static int au_opt_wbr_create(struct super_block *sb,
  25914. + struct au_opt_wbr_create *create)
  25915. +{
  25916. + int err;
  25917. + struct au_sbinfo *sbinfo;
  25918. +
  25919. + SiMustWriteLock(sb);
  25920. +
  25921. + err = 1; /* handled */
  25922. + sbinfo = au_sbi(sb);
  25923. + if (sbinfo->si_wbr_create_ops->fin) {
  25924. + err = sbinfo->si_wbr_create_ops->fin(sb);
  25925. + if (!err)
  25926. + err = 1;
  25927. + }
  25928. +
  25929. + sbinfo->si_wbr_create = create->wbr_create;
  25930. + sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
  25931. + switch (create->wbr_create) {
  25932. + case AuWbrCreate_MFSRRV:
  25933. + case AuWbrCreate_MFSRR:
  25934. + case AuWbrCreate_TDMFS:
  25935. + case AuWbrCreate_TDMFSV:
  25936. + case AuWbrCreate_PMFSRR:
  25937. + case AuWbrCreate_PMFSRRV:
  25938. + sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
  25939. + /*FALLTHROUGH*/
  25940. + case AuWbrCreate_MFS:
  25941. + case AuWbrCreate_MFSV:
  25942. + case AuWbrCreate_PMFS:
  25943. + case AuWbrCreate_PMFSV:
  25944. + sbinfo->si_wbr_mfs.mfs_expire
  25945. + = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
  25946. + break;
  25947. + }
  25948. +
  25949. + if (sbinfo->si_wbr_create_ops->init)
  25950. + sbinfo->si_wbr_create_ops->init(sb); /* ignore */
  25951. +
  25952. + return err;
  25953. +}
  25954. +
  25955. +/*
  25956. + * returns,
  25957. + * plus: processed without an error
  25958. + * zero: unprocessed
  25959. + */
  25960. +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
  25961. + struct au_opts *opts)
  25962. +{
  25963. + int err;
  25964. + struct au_sbinfo *sbinfo;
  25965. +
  25966. + SiMustWriteLock(sb);
  25967. +
  25968. + err = 1; /* handled */
  25969. + sbinfo = au_sbi(sb);
  25970. + switch (opt->type) {
  25971. + case Opt_udba:
  25972. + sbinfo->si_mntflags &= ~AuOptMask_UDBA;
  25973. + sbinfo->si_mntflags |= opt->udba;
  25974. + opts->given_udba |= opt->udba;
  25975. + break;
  25976. +
  25977. + case Opt_plink:
  25978. + au_opt_set(sbinfo->si_mntflags, PLINK);
  25979. + break;
  25980. + case Opt_noplink:
  25981. + if (au_opt_test(sbinfo->si_mntflags, PLINK))
  25982. + au_plink_put(sb, /*verbose*/1);
  25983. + au_opt_clr(sbinfo->si_mntflags, PLINK);
  25984. + break;
  25985. + case Opt_list_plink:
  25986. + if (au_opt_test(sbinfo->si_mntflags, PLINK))
  25987. + au_plink_list(sb);
  25988. + break;
  25989. +
  25990. + case Opt_dio:
  25991. + au_opt_set(sbinfo->si_mntflags, DIO);
  25992. + au_fset_opts(opts->flags, REFRESH_DYAOP);
  25993. + break;
  25994. + case Opt_nodio:
  25995. + au_opt_clr(sbinfo->si_mntflags, DIO);
  25996. + au_fset_opts(opts->flags, REFRESH_DYAOP);
  25997. + break;
  25998. +
  25999. + case Opt_fhsm_sec:
  26000. + au_fhsm_set(sbinfo, opt->fhsm_second);
  26001. + break;
  26002. +
  26003. + case Opt_diropq_a:
  26004. + au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
  26005. + break;
  26006. + case Opt_diropq_w:
  26007. + au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
  26008. + break;
  26009. +
  26010. + case Opt_warn_perm:
  26011. + au_opt_set(sbinfo->si_mntflags, WARN_PERM);
  26012. + break;
  26013. + case Opt_nowarn_perm:
  26014. + au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
  26015. + break;
  26016. +
  26017. + case Opt_verbose:
  26018. + au_opt_set(sbinfo->si_mntflags, VERBOSE);
  26019. + break;
  26020. + case Opt_noverbose:
  26021. + au_opt_clr(sbinfo->si_mntflags, VERBOSE);
  26022. + break;
  26023. +
  26024. + case Opt_sum:
  26025. + au_opt_set(sbinfo->si_mntflags, SUM);
  26026. + break;
  26027. + case Opt_wsum:
  26028. + au_opt_clr(sbinfo->si_mntflags, SUM);
  26029. + au_opt_set(sbinfo->si_mntflags, SUM_W);
  26030. + case Opt_nosum:
  26031. + au_opt_clr(sbinfo->si_mntflags, SUM);
  26032. + au_opt_clr(sbinfo->si_mntflags, SUM_W);
  26033. + break;
  26034. +
  26035. + case Opt_wbr_create:
  26036. + err = au_opt_wbr_create(sb, &opt->wbr_create);
  26037. + break;
  26038. + case Opt_wbr_copyup:
  26039. + sbinfo->si_wbr_copyup = opt->wbr_copyup;
  26040. + sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
  26041. + break;
  26042. +
  26043. + case Opt_dirwh:
  26044. + sbinfo->si_dirwh = opt->dirwh;
  26045. + break;
  26046. +
  26047. + case Opt_rdcache:
  26048. + sbinfo->si_rdcache
  26049. + = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
  26050. + break;
  26051. + case Opt_rdblk:
  26052. + sbinfo->si_rdblk = opt->rdblk;
  26053. + break;
  26054. + case Opt_rdblk_def:
  26055. + sbinfo->si_rdblk = AUFS_RDBLK_DEF;
  26056. + break;
  26057. + case Opt_rdhash:
  26058. + sbinfo->si_rdhash = opt->rdhash;
  26059. + break;
  26060. + case Opt_rdhash_def:
  26061. + sbinfo->si_rdhash = AUFS_RDHASH_DEF;
  26062. + break;
  26063. +
  26064. + case Opt_shwh:
  26065. + au_opt_set(sbinfo->si_mntflags, SHWH);
  26066. + break;
  26067. + case Opt_noshwh:
  26068. + au_opt_clr(sbinfo->si_mntflags, SHWH);
  26069. + break;
  26070. +
  26071. + case Opt_dirperm1:
  26072. + au_opt_set(sbinfo->si_mntflags, DIRPERM1);
  26073. + break;
  26074. + case Opt_nodirperm1:
  26075. + au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
  26076. + break;
  26077. +
  26078. + case Opt_trunc_xino:
  26079. + au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
  26080. + break;
  26081. + case Opt_notrunc_xino:
  26082. + au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
  26083. + break;
  26084. +
  26085. + case Opt_trunc_xino_path:
  26086. + case Opt_itrunc_xino:
  26087. + err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
  26088. + /*idx_begin*/0);
  26089. + if (!err)
  26090. + err = 1;
  26091. + break;
  26092. +
  26093. + case Opt_trunc_xib:
  26094. + au_fset_opts(opts->flags, TRUNC_XIB);
  26095. + break;
  26096. + case Opt_notrunc_xib:
  26097. + au_fclr_opts(opts->flags, TRUNC_XIB);
  26098. + break;
  26099. +
  26100. + case Opt_dirren:
  26101. + err = 1;
  26102. + if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
  26103. + err = au_dr_opt_set(sb);
  26104. + if (!err)
  26105. + err = 1;
  26106. + }
  26107. + if (err == 1)
  26108. + au_opt_set(sbinfo->si_mntflags, DIRREN);
  26109. + break;
  26110. + case Opt_nodirren:
  26111. + err = 1;
  26112. + if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
  26113. + err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
  26114. + DR_FLUSHED));
  26115. + if (!err)
  26116. + err = 1;
  26117. + }
  26118. + if (err == 1)
  26119. + au_opt_clr(sbinfo->si_mntflags, DIRREN);
  26120. + break;
  26121. +
  26122. + case Opt_acl:
  26123. + sb->s_flags |= SB_POSIXACL;
  26124. + break;
  26125. + case Opt_noacl:
  26126. + sb->s_flags &= ~SB_POSIXACL;
  26127. + break;
  26128. +
  26129. + default:
  26130. + err = 0;
  26131. + break;
  26132. + }
  26133. +
  26134. + return err;
  26135. +}
  26136. +
  26137. +/*
  26138. + * returns tri-state.
  26139. + * plus: processed without an error
  26140. + * zero: unprocessed
  26141. + * minus: error
  26142. + */
  26143. +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
  26144. + struct au_opts *opts)
  26145. +{
  26146. + int err, do_refresh;
  26147. +
  26148. + err = 0;
  26149. + switch (opt->type) {
  26150. + case Opt_append:
  26151. + opt->add.bindex = au_sbbot(sb) + 1;
  26152. + if (opt->add.bindex < 0)
  26153. + opt->add.bindex = 0;
  26154. + goto add;
  26155. + case Opt_prepend:
  26156. + opt->add.bindex = 0;
  26157. + add: /* indented label */
  26158. + case Opt_add:
  26159. + err = au_br_add(sb, &opt->add,
  26160. + au_ftest_opts(opts->flags, REMOUNT));
  26161. + if (!err) {
  26162. + err = 1;
  26163. + au_fset_opts(opts->flags, REFRESH);
  26164. + }
  26165. + break;
  26166. +
  26167. + case Opt_del:
  26168. + case Opt_idel:
  26169. + err = au_br_del(sb, &opt->del,
  26170. + au_ftest_opts(opts->flags, REMOUNT));
  26171. + if (!err) {
  26172. + err = 1;
  26173. + au_fset_opts(opts->flags, TRUNC_XIB);
  26174. + au_fset_opts(opts->flags, REFRESH);
  26175. + }
  26176. + break;
  26177. +
  26178. + case Opt_mod:
  26179. + case Opt_imod:
  26180. + err = au_br_mod(sb, &opt->mod,
  26181. + au_ftest_opts(opts->flags, REMOUNT),
  26182. + &do_refresh);
  26183. + if (!err) {
  26184. + err = 1;
  26185. + if (do_refresh)
  26186. + au_fset_opts(opts->flags, REFRESH);
  26187. + }
  26188. + break;
  26189. + }
  26190. + return err;
  26191. +}
  26192. +
  26193. +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
  26194. + struct au_opt_xino **opt_xino,
  26195. + struct au_opts *opts)
  26196. +{
  26197. + int err;
  26198. +
  26199. + err = 0;
  26200. + switch (opt->type) {
  26201. + case Opt_xino:
  26202. + err = au_xino_set(sb, &opt->xino,
  26203. + !!au_ftest_opts(opts->flags, REMOUNT));
  26204. + if (unlikely(err))
  26205. + break;
  26206. +
  26207. + *opt_xino = &opt->xino;
  26208. + break;
  26209. +
  26210. + case Opt_noxino:
  26211. + au_xino_clr(sb);
  26212. + *opt_xino = (void *)-1;
  26213. + break;
  26214. + }
  26215. +
  26216. + return err;
  26217. +}
  26218. +
  26219. +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
  26220. + unsigned int pending)
  26221. +{
  26222. + int err, fhsm;
  26223. + aufs_bindex_t bindex, bbot;
  26224. + unsigned char do_plink, skip, do_free, can_no_dreval;
  26225. + struct au_branch *br;
  26226. + struct au_wbr *wbr;
  26227. + struct dentry *root, *dentry;
  26228. + struct inode *dir, *h_dir;
  26229. + struct au_sbinfo *sbinfo;
  26230. + struct au_hinode *hdir;
  26231. +
  26232. + SiMustAnyLock(sb);
  26233. +
  26234. + sbinfo = au_sbi(sb);
  26235. + AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
  26236. +
  26237. + if (!(sb_flags & SB_RDONLY)) {
  26238. + if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
  26239. + pr_warn("first branch should be rw\n");
  26240. + if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
  26241. + pr_warn_once("shwh should be used with ro\n");
  26242. + }
  26243. +
  26244. + if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
  26245. + && !au_opt_test(sbinfo->si_mntflags, XINO))
  26246. + pr_warn_once("udba=*notify requires xino\n");
  26247. +
  26248. + if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
  26249. + pr_warn_once("dirperm1 breaks the protection"
  26250. + " by the permission bits on the lower branch\n");
  26251. +
  26252. + err = 0;
  26253. + fhsm = 0;
  26254. + root = sb->s_root;
  26255. + dir = d_inode(root);
  26256. + do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
  26257. + can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
  26258. + UDBA_NONE);
  26259. + bbot = au_sbbot(sb);
  26260. + for (bindex = 0; !err && bindex <= bbot; bindex++) {
  26261. + skip = 0;
  26262. + h_dir = au_h_iptr(dir, bindex);
  26263. + br = au_sbr(sb, bindex);
  26264. +
  26265. + if ((br->br_perm & AuBrAttr_ICEX)
  26266. + && !h_dir->i_op->listxattr)
  26267. + br->br_perm &= ~AuBrAttr_ICEX;
  26268. +#if 0
  26269. + if ((br->br_perm & AuBrAttr_ICEX_SEC)
  26270. + && (au_br_sb(br)->s_flags & SB_NOSEC))
  26271. + br->br_perm &= ~AuBrAttr_ICEX_SEC;
  26272. +#endif
  26273. +
  26274. + do_free = 0;
  26275. + wbr = br->br_wbr;
  26276. + if (wbr)
  26277. + wbr_wh_read_lock(wbr);
  26278. +
  26279. + if (!au_br_writable(br->br_perm)) {
  26280. + do_free = !!wbr;
  26281. + skip = (!wbr
  26282. + || (!wbr->wbr_whbase
  26283. + && !wbr->wbr_plink
  26284. + && !wbr->wbr_orph));
  26285. + } else if (!au_br_wh_linkable(br->br_perm)) {
  26286. + /* skip = (!br->br_whbase && !br->br_orph); */
  26287. + skip = (!wbr || !wbr->wbr_whbase);
  26288. + if (skip && wbr) {
  26289. + if (do_plink)
  26290. + skip = !!wbr->wbr_plink;
  26291. + else
  26292. + skip = !wbr->wbr_plink;
  26293. + }
  26294. + } else {
  26295. + /* skip = (br->br_whbase && br->br_ohph); */
  26296. + skip = (wbr && wbr->wbr_whbase);
  26297. + if (skip) {
  26298. + if (do_plink)
  26299. + skip = !!wbr->wbr_plink;
  26300. + else
  26301. + skip = !wbr->wbr_plink;
  26302. + }
  26303. + }
  26304. + if (wbr)
  26305. + wbr_wh_read_unlock(wbr);
  26306. +
  26307. + if (can_no_dreval) {
  26308. + dentry = br->br_path.dentry;
  26309. + spin_lock(&dentry->d_lock);
  26310. + if (dentry->d_flags &
  26311. + (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
  26312. + can_no_dreval = 0;
  26313. + spin_unlock(&dentry->d_lock);
  26314. + }
  26315. +
  26316. + if (au_br_fhsm(br->br_perm)) {
  26317. + fhsm++;
  26318. + AuDebugOn(!br->br_fhsm);
  26319. + }
  26320. +
  26321. + if (skip)
  26322. + continue;
  26323. +
  26324. + hdir = au_hi(dir, bindex);
  26325. + au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
  26326. + if (wbr)
  26327. + wbr_wh_write_lock(wbr);
  26328. + err = au_wh_init(br, sb);
  26329. + if (wbr)
  26330. + wbr_wh_write_unlock(wbr);
  26331. + au_hn_inode_unlock(hdir);
  26332. +
  26333. + if (!err && do_free) {
  26334. + au_kfree_rcu(wbr);
  26335. + br->br_wbr = NULL;
  26336. + }
  26337. + }
  26338. +
  26339. + if (can_no_dreval)
  26340. + au_fset_si(sbinfo, NO_DREVAL);
  26341. + else
  26342. + au_fclr_si(sbinfo, NO_DREVAL);
  26343. +
  26344. + if (fhsm >= 2) {
  26345. + au_fset_si(sbinfo, FHSM);
  26346. + for (bindex = bbot; bindex >= 0; bindex--) {
  26347. + br = au_sbr(sb, bindex);
  26348. + if (au_br_fhsm(br->br_perm)) {
  26349. + au_fhsm_set_bottom(sb, bindex);
  26350. + break;
  26351. + }
  26352. + }
  26353. + } else {
  26354. + au_fclr_si(sbinfo, FHSM);
  26355. + au_fhsm_set_bottom(sb, -1);
  26356. + }
  26357. +
  26358. + return err;
  26359. +}
  26360. +
  26361. +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
  26362. +{
  26363. + int err;
  26364. + unsigned int tmp;
  26365. + aufs_bindex_t bindex, bbot;
  26366. + struct au_opt *opt;
  26367. + struct au_opt_xino *opt_xino, xino;
  26368. + struct au_sbinfo *sbinfo;
  26369. + struct au_branch *br;
  26370. + struct inode *dir;
  26371. +
  26372. + SiMustWriteLock(sb);
  26373. +
  26374. + err = 0;
  26375. + opt_xino = NULL;
  26376. + opt = opts->opt;
  26377. + while (err >= 0 && opt->type != Opt_tail)
  26378. + err = au_opt_simple(sb, opt++, opts);
  26379. + if (err > 0)
  26380. + err = 0;
  26381. + else if (unlikely(err < 0))
  26382. + goto out;
  26383. +
  26384. + /* disable xino and udba temporary */
  26385. + sbinfo = au_sbi(sb);
  26386. + tmp = sbinfo->si_mntflags;
  26387. + au_opt_clr(sbinfo->si_mntflags, XINO);
  26388. + au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
  26389. +
  26390. + opt = opts->opt;
  26391. + while (err >= 0 && opt->type != Opt_tail)
  26392. + err = au_opt_br(sb, opt++, opts);
  26393. + if (err > 0)
  26394. + err = 0;
  26395. + else if (unlikely(err < 0))
  26396. + goto out;
  26397. +
  26398. + bbot = au_sbbot(sb);
  26399. + if (unlikely(bbot < 0)) {
  26400. + err = -EINVAL;
  26401. + pr_err("no branches\n");
  26402. + goto out;
  26403. + }
  26404. +
  26405. + if (au_opt_test(tmp, XINO))
  26406. + au_opt_set(sbinfo->si_mntflags, XINO);
  26407. + opt = opts->opt;
  26408. + while (!err && opt->type != Opt_tail)
  26409. + err = au_opt_xino(sb, opt++, &opt_xino, opts);
  26410. + if (unlikely(err))
  26411. + goto out;
  26412. +
  26413. + err = au_opts_verify(sb, sb->s_flags, tmp);
  26414. + if (unlikely(err))
  26415. + goto out;
  26416. +
  26417. + /* restore xino */
  26418. + if (au_opt_test(tmp, XINO) && !opt_xino) {
  26419. + xino.file = au_xino_def(sb);
  26420. + err = PTR_ERR(xino.file);
  26421. + if (IS_ERR(xino.file))
  26422. + goto out;
  26423. +
  26424. + err = au_xino_set(sb, &xino, /*remount*/0);
  26425. + fput(xino.file);
  26426. + if (unlikely(err))
  26427. + goto out;
  26428. + }
  26429. +
  26430. + /* restore udba */
  26431. + tmp &= AuOptMask_UDBA;
  26432. + sbinfo->si_mntflags &= ~AuOptMask_UDBA;
  26433. + sbinfo->si_mntflags |= tmp;
  26434. + bbot = au_sbbot(sb);
  26435. + for (bindex = 0; bindex <= bbot; bindex++) {
  26436. + br = au_sbr(sb, bindex);
  26437. + err = au_hnotify_reset_br(tmp, br, br->br_perm);
  26438. + if (unlikely(err))
  26439. + AuIOErr("hnotify failed on br %d, %d, ignored\n",
  26440. + bindex, err);
  26441. + /* go on even if err */
  26442. + }
  26443. + if (au_opt_test(tmp, UDBA_HNOTIFY)) {
  26444. + dir = d_inode(sb->s_root);
  26445. + au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
  26446. + }
  26447. +
  26448. +out:
  26449. + return err;
  26450. +}
  26451. +
  26452. +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
  26453. +{
  26454. + int err, rerr;
  26455. + unsigned char no_dreval;
  26456. + struct inode *dir;
  26457. + struct au_opt_xino *opt_xino;
  26458. + struct au_opt *opt;
  26459. + struct au_sbinfo *sbinfo;
  26460. +
  26461. + SiMustWriteLock(sb);
  26462. +
  26463. + err = au_dr_opt_flush(sb);
  26464. + if (unlikely(err))
  26465. + goto out;
  26466. + au_fset_opts(opts->flags, DR_FLUSHED);
  26467. +
  26468. + dir = d_inode(sb->s_root);
  26469. + sbinfo = au_sbi(sb);
  26470. + opt_xino = NULL;
  26471. + opt = opts->opt;
  26472. + while (err >= 0 && opt->type != Opt_tail) {
  26473. + err = au_opt_simple(sb, opt, opts);
  26474. + if (!err)
  26475. + err = au_opt_br(sb, opt, opts);
  26476. + if (!err)
  26477. + err = au_opt_xino(sb, opt, &opt_xino, opts);
  26478. + opt++;
  26479. + }
  26480. + if (err > 0)
  26481. + err = 0;
  26482. + AuTraceErr(err);
  26483. + /* go on even err */
  26484. +
  26485. + no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
  26486. + rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
  26487. + if (unlikely(rerr && !err))
  26488. + err = rerr;
  26489. +
  26490. + if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
  26491. + au_fset_opts(opts->flags, REFRESH_IDOP);
  26492. +
  26493. + if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
  26494. + rerr = au_xib_trunc(sb);
  26495. + if (unlikely(rerr && !err))
  26496. + err = rerr;
  26497. + }
  26498. +
  26499. + /* will be handled by the caller */
  26500. + if (!au_ftest_opts(opts->flags, REFRESH)
  26501. + && (opts->given_udba
  26502. + || au_opt_test(sbinfo->si_mntflags, XINO)
  26503. + || au_ftest_opts(opts->flags, REFRESH_IDOP)
  26504. + ))
  26505. + au_fset_opts(opts->flags, REFRESH);
  26506. +
  26507. + AuDbg("status 0x%x\n", opts->flags);
  26508. +
  26509. +out:
  26510. + return err;
  26511. +}
  26512. +
  26513. +/* ---------------------------------------------------------------------- */
  26514. +
  26515. +unsigned int au_opt_udba(struct super_block *sb)
  26516. +{
  26517. + return au_mntflags(sb) & AuOptMask_UDBA;
  26518. +}
  26519. diff -Naur null/fs/aufs/opts.h linux-5.1/fs/aufs/opts.h
  26520. --- /dev/null
  26521. +++ linux-5.1/fs/aufs/opts.h 2019-05-22 11:23:07.442140950 +0200
  26522. @@ -0,0 +1,225 @@
  26523. +/* SPDX-License-Identifier: GPL-2.0 */
  26524. +/*
  26525. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  26526. + *
  26527. + * This program, aufs is free software; you can redistribute it and/or modify
  26528. + * it under the terms of the GNU General Public License as published by
  26529. + * the Free Software Foundation; either version 2 of the License, or
  26530. + * (at your option) any later version.
  26531. + *
  26532. + * This program is distributed in the hope that it will be useful,
  26533. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26534. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26535. + * GNU General Public License for more details.
  26536. + *
  26537. + * You should have received a copy of the GNU General Public License
  26538. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26539. + */
  26540. +
  26541. +/*
  26542. + * mount options/flags
  26543. + */
  26544. +
  26545. +#ifndef __AUFS_OPTS_H__
  26546. +#define __AUFS_OPTS_H__
  26547. +
  26548. +#ifdef __KERNEL__
  26549. +
  26550. +#include <linux/path.h>
  26551. +
  26552. +struct file;
  26553. +
  26554. +/* ---------------------------------------------------------------------- */
  26555. +
  26556. +/* mount flags */
  26557. +#define AuOpt_XINO 1 /* external inode number bitmap
  26558. + and translation table */
  26559. +#define AuOpt_TRUNC_XINO (1 << 1) /* truncate xino files */
  26560. +#define AuOpt_UDBA_NONE (1 << 2) /* users direct branch access */
  26561. +#define AuOpt_UDBA_REVAL (1 << 3)
  26562. +#define AuOpt_UDBA_HNOTIFY (1 << 4)
  26563. +#define AuOpt_SHWH (1 << 5) /* show whiteout */
  26564. +#define AuOpt_PLINK (1 << 6) /* pseudo-link */
  26565. +#define AuOpt_DIRPERM1 (1 << 7) /* ignore the lower dir's perm
  26566. + bits */
  26567. +#define AuOpt_ALWAYS_DIROPQ (1 << 9) /* policy to creating diropq */
  26568. +#define AuOpt_SUM (1 << 10) /* summation for statfs(2) */
  26569. +#define AuOpt_SUM_W (1 << 11) /* unimplemented */
  26570. +#define AuOpt_WARN_PERM (1 << 12) /* warn when add-branch */
  26571. +#define AuOpt_VERBOSE (1 << 13) /* print the cause of error */
  26572. +#define AuOpt_DIO (1 << 14) /* direct io */
  26573. +#define AuOpt_DIRREN (1 << 15) /* directory rename */
  26574. +
  26575. +#ifndef CONFIG_AUFS_HNOTIFY
  26576. +#undef AuOpt_UDBA_HNOTIFY
  26577. +#define AuOpt_UDBA_HNOTIFY 0
  26578. +#endif
  26579. +#ifndef CONFIG_AUFS_DIRREN
  26580. +#undef AuOpt_DIRREN
  26581. +#define AuOpt_DIRREN 0
  26582. +#endif
  26583. +#ifndef CONFIG_AUFS_SHWH
  26584. +#undef AuOpt_SHWH
  26585. +#define AuOpt_SHWH 0
  26586. +#endif
  26587. +
  26588. +#define AuOpt_Def (AuOpt_XINO \
  26589. + | AuOpt_UDBA_REVAL \
  26590. + | AuOpt_PLINK \
  26591. + /* | AuOpt_DIRPERM1 */ \
  26592. + | AuOpt_WARN_PERM)
  26593. +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
  26594. + | AuOpt_UDBA_REVAL \
  26595. + | AuOpt_UDBA_HNOTIFY)
  26596. +
  26597. +#define au_opt_test(flags, name) (flags & AuOpt_##name)
  26598. +#define au_opt_set(flags, name) do { \
  26599. + BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
  26600. + ((flags) |= AuOpt_##name); \
  26601. +} while (0)
  26602. +#define au_opt_set_udba(flags, name) do { \
  26603. + (flags) &= ~AuOptMask_UDBA; \
  26604. + ((flags) |= AuOpt_##name); \
  26605. +} while (0)
  26606. +#define au_opt_clr(flags, name) do { \
  26607. + ((flags) &= ~AuOpt_##name); \
  26608. +} while (0)
  26609. +
  26610. +static inline unsigned int au_opts_plink(unsigned int mntflags)
  26611. +{
  26612. +#ifdef CONFIG_PROC_FS
  26613. + return mntflags;
  26614. +#else
  26615. + return mntflags & ~AuOpt_PLINK;
  26616. +#endif
  26617. +}
  26618. +
  26619. +/* ---------------------------------------------------------------------- */
  26620. +
  26621. +/* policies to select one among multiple writable branches */
  26622. +enum {
  26623. + AuWbrCreate_TDP, /* top down parent */
  26624. + AuWbrCreate_RR, /* round robin */
  26625. + AuWbrCreate_MFS, /* most free space */
  26626. + AuWbrCreate_MFSV, /* mfs with seconds */
  26627. + AuWbrCreate_MFSRR, /* mfs then rr */
  26628. + AuWbrCreate_MFSRRV, /* mfs then rr with seconds */
  26629. + AuWbrCreate_TDMFS, /* top down regardless parent and mfs */
  26630. + AuWbrCreate_TDMFSV, /* top down regardless parent and mfs */
  26631. + AuWbrCreate_PMFS, /* parent and mfs */
  26632. + AuWbrCreate_PMFSV, /* parent and mfs with seconds */
  26633. + AuWbrCreate_PMFSRR, /* parent, mfs and round-robin */
  26634. + AuWbrCreate_PMFSRRV, /* plus seconds */
  26635. +
  26636. + AuWbrCreate_Def = AuWbrCreate_TDP
  26637. +};
  26638. +
  26639. +enum {
  26640. + AuWbrCopyup_TDP, /* top down parent */
  26641. + AuWbrCopyup_BUP, /* bottom up parent */
  26642. + AuWbrCopyup_BU, /* bottom up */
  26643. +
  26644. + AuWbrCopyup_Def = AuWbrCopyup_TDP
  26645. +};
  26646. +
  26647. +/* ---------------------------------------------------------------------- */
  26648. +
  26649. +struct au_opt_add {
  26650. + aufs_bindex_t bindex;
  26651. + char *pathname;
  26652. + int perm;
  26653. + struct path path;
  26654. +};
  26655. +
  26656. +struct au_opt_del {
  26657. + char *pathname;
  26658. + struct path h_path;
  26659. +};
  26660. +
  26661. +struct au_opt_mod {
  26662. + char *path;
  26663. + int perm;
  26664. + struct dentry *h_root;
  26665. +};
  26666. +
  26667. +struct au_opt_xino {
  26668. + char *path;
  26669. + struct file *file;
  26670. +};
  26671. +
  26672. +struct au_opt_xino_itrunc {
  26673. + aufs_bindex_t bindex;
  26674. +};
  26675. +
  26676. +struct au_opt_wbr_create {
  26677. + int wbr_create;
  26678. + int mfs_second;
  26679. + unsigned long long mfsrr_watermark;
  26680. +};
  26681. +
  26682. +struct au_opt {
  26683. + int type;
  26684. + union {
  26685. + struct au_opt_xino xino;
  26686. + struct au_opt_xino_itrunc xino_itrunc;
  26687. + struct au_opt_add add;
  26688. + struct au_opt_del del;
  26689. + struct au_opt_mod mod;
  26690. + int dirwh;
  26691. + int rdcache;
  26692. + unsigned int rdblk;
  26693. + unsigned int rdhash;
  26694. + int udba;
  26695. + struct au_opt_wbr_create wbr_create;
  26696. + int wbr_copyup;
  26697. + unsigned int fhsm_second;
  26698. + };
  26699. +};
  26700. +
  26701. +/* opts flags */
  26702. +#define AuOpts_REMOUNT 1
  26703. +#define AuOpts_REFRESH (1 << 1)
  26704. +#define AuOpts_TRUNC_XIB (1 << 2)
  26705. +#define AuOpts_REFRESH_DYAOP (1 << 3)
  26706. +#define AuOpts_REFRESH_IDOP (1 << 4)
  26707. +#define AuOpts_DR_FLUSHED (1 << 5)
  26708. +#define au_ftest_opts(flags, name) ((flags) & AuOpts_##name)
  26709. +#define au_fset_opts(flags, name) \
  26710. + do { (flags) |= AuOpts_##name; } while (0)
  26711. +#define au_fclr_opts(flags, name) \
  26712. + do { (flags) &= ~AuOpts_##name; } while (0)
  26713. +
  26714. +#ifndef CONFIG_AUFS_DIRREN
  26715. +#undef AuOpts_DR_FLUSHED
  26716. +#define AuOpts_DR_FLUSHED 0
  26717. +#endif
  26718. +
  26719. +struct au_opts {
  26720. + struct au_opt *opt;
  26721. + int max_opt;
  26722. +
  26723. + unsigned int given_udba;
  26724. + unsigned int flags;
  26725. + unsigned long sb_flags;
  26726. +};
  26727. +
  26728. +/* ---------------------------------------------------------------------- */
  26729. +
  26730. +/* opts.c */
  26731. +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
  26732. +const char *au_optstr_udba(int udba);
  26733. +const char *au_optstr_wbr_copyup(int wbr_copyup);
  26734. +const char *au_optstr_wbr_create(int wbr_create);
  26735. +
  26736. +void au_opts_free(struct au_opts *opts);
  26737. +struct super_block;
  26738. +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
  26739. +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
  26740. + unsigned int pending);
  26741. +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
  26742. +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
  26743. +
  26744. +unsigned int au_opt_udba(struct super_block *sb);
  26745. +
  26746. +#endif /* __KERNEL__ */
  26747. +#endif /* __AUFS_OPTS_H__ */
  26748. diff -Naur null/fs/aufs/plink.c linux-5.1/fs/aufs/plink.c
  26749. --- /dev/null
  26750. +++ linux-5.1/fs/aufs/plink.c 2019-05-22 11:23:07.442140950 +0200
  26751. @@ -0,0 +1,516 @@
  26752. +// SPDX-License-Identifier: GPL-2.0
  26753. +/*
  26754. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  26755. + *
  26756. + * This program, aufs is free software; you can redistribute it and/or modify
  26757. + * it under the terms of the GNU General Public License as published by
  26758. + * the Free Software Foundation; either version 2 of the License, or
  26759. + * (at your option) any later version.
  26760. + *
  26761. + * This program is distributed in the hope that it will be useful,
  26762. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26763. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26764. + * GNU General Public License for more details.
  26765. + *
  26766. + * You should have received a copy of the GNU General Public License
  26767. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  26768. + */
  26769. +
  26770. +/*
  26771. + * pseudo-link
  26772. + */
  26773. +
  26774. +#include "aufs.h"
  26775. +
  26776. +/*
  26777. + * the pseudo-link maintenance mode.
  26778. + * during a user process maintains the pseudo-links,
  26779. + * prohibit adding a new plink and branch manipulation.
  26780. + *
  26781. + * Flags
  26782. + * NOPLM:
  26783. + * For entry functions which will handle plink, and i_mutex is already held
  26784. + * in VFS.
  26785. + * They cannot wait and should return an error at once.
  26786. + * Callers has to check the error.
  26787. + * NOPLMW:
  26788. + * For entry functions which will handle plink, but i_mutex is not held
  26789. + * in VFS.
  26790. + * They can wait the plink maintenance mode to finish.
  26791. + *
  26792. + * They behave like F_SETLK and F_SETLKW.
  26793. + * If the caller never handle plink, then both flags are unnecessary.
  26794. + */
  26795. +
  26796. +int au_plink_maint(struct super_block *sb, int flags)
  26797. +{
  26798. + int err;
  26799. + pid_t pid, ppid;
  26800. + struct task_struct *parent, *prev;
  26801. + struct au_sbinfo *sbi;
  26802. +
  26803. + SiMustAnyLock(sb);
  26804. +
  26805. + err = 0;
  26806. + if (!au_opt_test(au_mntflags(sb), PLINK))
  26807. + goto out;
  26808. +
  26809. + sbi = au_sbi(sb);
  26810. + pid = sbi->si_plink_maint_pid;
  26811. + if (!pid || pid == current->pid)
  26812. + goto out;
  26813. +
  26814. + /* todo: it highly depends upon /sbin/mount.aufs */
  26815. + prev = NULL;
  26816. + parent = current;
  26817. + ppid = 0;
  26818. + rcu_read_lock();
  26819. + while (1) {
  26820. + parent = rcu_dereference(parent->real_parent);
  26821. + if (parent == prev)
  26822. + break;
  26823. + ppid = task_pid_vnr(parent);
  26824. + if (pid == ppid) {
  26825. + rcu_read_unlock();
  26826. + goto out;
  26827. + }
  26828. + prev = parent;
  26829. + }
  26830. + rcu_read_unlock();
  26831. +
  26832. + if (au_ftest_lock(flags, NOPLMW)) {
  26833. + /* if there is no i_mutex lock in VFS, we don't need to wait */
  26834. + /* AuDebugOn(!lockdep_depth(current)); */
  26835. + while (sbi->si_plink_maint_pid) {
  26836. + si_read_unlock(sb);
  26837. + /* gave up wake_up_bit() */
  26838. + wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
  26839. +
  26840. + if (au_ftest_lock(flags, FLUSH))
  26841. + au_nwt_flush(&sbi->si_nowait);
  26842. + si_noflush_read_lock(sb);
  26843. + }
  26844. + } else if (au_ftest_lock(flags, NOPLM)) {
  26845. + AuDbg("ppid %d, pid %d\n", ppid, pid);
  26846. + err = -EAGAIN;
  26847. + }
  26848. +
  26849. +out:
  26850. + return err;
  26851. +}
  26852. +
  26853. +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
  26854. +{
  26855. + spin_lock(&sbinfo->si_plink_maint_lock);
  26856. + sbinfo->si_plink_maint_pid = 0;
  26857. + spin_unlock(&sbinfo->si_plink_maint_lock);
  26858. + wake_up_all(&sbinfo->si_plink_wq);
  26859. +}
  26860. +
  26861. +int au_plink_maint_enter(struct super_block *sb)
  26862. +{
  26863. + int err;
  26864. + struct au_sbinfo *sbinfo;
  26865. +
  26866. + err = 0;
  26867. + sbinfo = au_sbi(sb);
  26868. + /* make sure i am the only one in this fs */
  26869. + si_write_lock(sb, AuLock_FLUSH);
  26870. + if (au_opt_test(au_mntflags(sb), PLINK)) {
  26871. + spin_lock(&sbinfo->si_plink_maint_lock);
  26872. + if (!sbinfo->si_plink_maint_pid)
  26873. + sbinfo->si_plink_maint_pid = current->pid;
  26874. + else
  26875. + err = -EBUSY;
  26876. + spin_unlock(&sbinfo->si_plink_maint_lock);
  26877. + }
  26878. + si_write_unlock(sb);
  26879. +
  26880. + return err;
  26881. +}
  26882. +
  26883. +/* ---------------------------------------------------------------------- */
  26884. +
  26885. +#ifdef CONFIG_AUFS_DEBUG
  26886. +void au_plink_list(struct super_block *sb)
  26887. +{
  26888. + int i;
  26889. + struct au_sbinfo *sbinfo;
  26890. + struct hlist_bl_head *hbl;
  26891. + struct hlist_bl_node *pos;
  26892. + struct au_icntnr *icntnr;
  26893. +
  26894. + SiMustAnyLock(sb);
  26895. +
  26896. + sbinfo = au_sbi(sb);
  26897. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  26898. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  26899. +
  26900. + for (i = 0; i < AuPlink_NHASH; i++) {
  26901. + hbl = sbinfo->si_plink + i;
  26902. + hlist_bl_lock(hbl);
  26903. + hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
  26904. + AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
  26905. + hlist_bl_unlock(hbl);
  26906. + }
  26907. +}
  26908. +#endif
  26909. +
  26910. +/* is the inode pseudo-linked? */
  26911. +int au_plink_test(struct inode *inode)
  26912. +{
  26913. + int found, i;
  26914. + struct au_sbinfo *sbinfo;
  26915. + struct hlist_bl_head *hbl;
  26916. + struct hlist_bl_node *pos;
  26917. + struct au_icntnr *icntnr;
  26918. +
  26919. + sbinfo = au_sbi(inode->i_sb);
  26920. + AuRwMustAnyLock(&sbinfo->si_rwsem);
  26921. + AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
  26922. + AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
  26923. +
  26924. + found = 0;
  26925. + i = au_plink_hash(inode->i_ino);
  26926. + hbl = sbinfo->si_plink + i;
  26927. + hlist_bl_lock(hbl);
  26928. + hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
  26929. + if (&icntnr->vfs_inode == inode) {
  26930. + found = 1;
  26931. + break;
  26932. + }
  26933. + hlist_bl_unlock(hbl);
  26934. + return found;
  26935. +}
  26936. +
  26937. +/* ---------------------------------------------------------------------- */
  26938. +
  26939. +/*
  26940. + * generate a name for plink.
  26941. + * the file will be stored under AUFS_WH_PLINKDIR.
  26942. + */
  26943. +/* 20 is max digits length of ulong 64 */
  26944. +#define PLINK_NAME_LEN ((20 + 1) * 2)
  26945. +
  26946. +static int plink_name(char *name, int len, struct inode *inode,
  26947. + aufs_bindex_t bindex)
  26948. +{
  26949. + int rlen;
  26950. + struct inode *h_inode;
  26951. +
  26952. + h_inode = au_h_iptr(inode, bindex);
  26953. + rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
  26954. + return rlen;
  26955. +}
  26956. +
  26957. +struct au_do_plink_lkup_args {
  26958. + struct dentry **errp;
  26959. + struct qstr *tgtname;
  26960. + struct dentry *h_parent;
  26961. + struct au_branch *br;
  26962. +};
  26963. +
  26964. +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
  26965. + struct dentry *h_parent,
  26966. + struct au_branch *br)
  26967. +{
  26968. + struct dentry *h_dentry;
  26969. + struct inode *h_inode;
  26970. +
  26971. + h_inode = d_inode(h_parent);
  26972. + inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
  26973. + h_dentry = vfsub_lkup_one(tgtname, h_parent);
  26974. + inode_unlock_shared(h_inode);
  26975. + return h_dentry;
  26976. +}
  26977. +
  26978. +static void au_call_do_plink_lkup(void *args)
  26979. +{
  26980. + struct au_do_plink_lkup_args *a = args;
  26981. + *a->errp = au_do_plink_lkup(a->tgtname, a->h_parent, a->br);
  26982. +}
  26983. +
  26984. +/* lookup the plink-ed @inode under the branch at @bindex */
  26985. +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
  26986. +{
  26987. + struct dentry *h_dentry, *h_parent;
  26988. + struct au_branch *br;
  26989. + int wkq_err;
  26990. + char a[PLINK_NAME_LEN];
  26991. + struct qstr tgtname = QSTR_INIT(a, 0);
  26992. +
  26993. + AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
  26994. +
  26995. + br = au_sbr(inode->i_sb, bindex);
  26996. + h_parent = br->br_wbr->wbr_plink;
  26997. + tgtname.len = plink_name(a, sizeof(a), inode, bindex);
  26998. +
  26999. + if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
  27000. + struct au_do_plink_lkup_args args = {
  27001. + .errp = &h_dentry,
  27002. + .tgtname = &tgtname,
  27003. + .h_parent = h_parent,
  27004. + .br = br
  27005. + };
  27006. +
  27007. + wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
  27008. + if (unlikely(wkq_err))
  27009. + h_dentry = ERR_PTR(wkq_err);
  27010. + } else
  27011. + h_dentry = au_do_plink_lkup(&tgtname, h_parent, br);
  27012. +
  27013. + return h_dentry;
  27014. +}
  27015. +
  27016. +/* create a pseudo-link */
  27017. +static int do_whplink(struct qstr *tgt, struct dentry *h_parent,
  27018. + struct dentry *h_dentry, struct au_branch *br)
  27019. +{
  27020. + int err;
  27021. + struct path h_path = {
  27022. + .mnt = au_br_mnt(br)
  27023. + };
  27024. + struct inode *h_dir, *delegated;
  27025. +
  27026. + h_dir = d_inode(h_parent);
  27027. + inode_lock_nested(h_dir, AuLsc_I_CHILD2);
  27028. +again:
  27029. + h_path.dentry = vfsub_lkup_one(tgt, h_parent);
  27030. + err = PTR_ERR(h_path.dentry);
  27031. + if (IS_ERR(h_path.dentry))
  27032. + goto out;
  27033. +
  27034. + err = 0;
  27035. + /* wh.plink dir is not monitored */
  27036. + /* todo: is it really safe? */
  27037. + if (d_is_positive(h_path.dentry)
  27038. + && d_inode(h_path.dentry) != d_inode(h_dentry)) {
  27039. + delegated = NULL;
  27040. + err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
  27041. + if (unlikely(err == -EWOULDBLOCK)) {
  27042. + pr_warn("cannot retry for NFSv4 delegation"
  27043. + " for an internal unlink\n");
  27044. + iput(delegated);
  27045. + }
  27046. + dput(h_path.dentry);
  27047. + h_path.dentry = NULL;
  27048. + if (!err)
  27049. + goto again;
  27050. + }
  27051. + if (!err && d_is_negative(h_path.dentry)) {
  27052. + delegated = NULL;
  27053. + err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
  27054. + if (unlikely(err == -EWOULDBLOCK)) {
  27055. + pr_warn("cannot retry for NFSv4 delegation"
  27056. + " for an internal link\n");
  27057. + iput(delegated);
  27058. + }
  27059. + }
  27060. + dput(h_path.dentry);
  27061. +
  27062. +out:
  27063. + inode_unlock(h_dir);
  27064. + return err;
  27065. +}
  27066. +
  27067. +struct do_whplink_args {
  27068. + int *errp;
  27069. + struct qstr *tgt;
  27070. + struct dentry *h_parent;
  27071. + struct dentry *h_dentry;
  27072. + struct au_branch *br;
  27073. +};
  27074. +
  27075. +static void call_do_whplink(void *args)
  27076. +{
  27077. + struct do_whplink_args *a = args;
  27078. + *a->errp = do_whplink(a->tgt, a->h_parent, a->h_dentry, a->br);
  27079. +}
  27080. +
  27081. +static int whplink(struct dentry *h_dentry, struct inode *inode,
  27082. + aufs_bindex_t bindex, struct au_branch *br)
  27083. +{
  27084. + int err, wkq_err;
  27085. + struct au_wbr *wbr;
  27086. + struct dentry *h_parent;
  27087. + char a[PLINK_NAME_LEN];
  27088. + struct qstr tgtname = QSTR_INIT(a, 0);
  27089. +
  27090. + wbr = au_sbr(inode->i_sb, bindex)->br_wbr;
  27091. + h_parent = wbr->wbr_plink;
  27092. + tgtname.len = plink_name(a, sizeof(a), inode, bindex);
  27093. +
  27094. + /* always superio. */
  27095. + if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
  27096. + struct do_whplink_args args = {
  27097. + .errp = &err,
  27098. + .tgt = &tgtname,
  27099. + .h_parent = h_parent,
  27100. + .h_dentry = h_dentry,
  27101. + .br = br
  27102. + };
  27103. + wkq_err = au_wkq_wait(call_do_whplink, &args);
  27104. + if (unlikely(wkq_err))
  27105. + err = wkq_err;
  27106. + } else
  27107. + err = do_whplink(&tgtname, h_parent, h_dentry, br);
  27108. +
  27109. + return err;
  27110. +}
  27111. +
  27112. +/*
  27113. + * create a new pseudo-link for @h_dentry on @bindex.
  27114. + * the linked inode is held in aufs @inode.
  27115. + */
  27116. +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
  27117. + struct dentry *h_dentry)
  27118. +{
  27119. + struct super_block *sb;
  27120. + struct au_sbinfo *sbinfo;
  27121. + struct hlist_bl_head *hbl;
  27122. + struct hlist_bl_node *pos;
  27123. + struct au_icntnr *icntnr;
  27124. + int found, err, cnt, i;
  27125. +
  27126. + sb = inode->i_sb;
  27127. + sbinfo = au_sbi(sb);
  27128. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  27129. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  27130. +
  27131. + found = au_plink_test(inode);
  27132. + if (found)
  27133. + return;
  27134. +
  27135. + i = au_plink_hash(inode->i_ino);
  27136. + hbl = sbinfo->si_plink + i;
  27137. + au_igrab(inode);
  27138. +
  27139. + hlist_bl_lock(hbl);
  27140. + hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
  27141. + if (&icntnr->vfs_inode == inode) {
  27142. + found = 1;
  27143. + break;
  27144. + }
  27145. + }
  27146. + if (!found) {
  27147. + icntnr = container_of(inode, struct au_icntnr, vfs_inode);
  27148. + hlist_bl_add_head(&icntnr->plink, hbl);
  27149. + }
  27150. + hlist_bl_unlock(hbl);
  27151. + if (!found) {
  27152. + cnt = au_hbl_count(hbl);
  27153. +#define msg "unexpectedly unbalanced or too many pseudo-links"
  27154. + if (cnt > AUFS_PLINK_WARN)
  27155. + AuWarn1(msg ", %d\n", cnt);
  27156. +#undef msg
  27157. + err = whplink(h_dentry, inode, bindex, au_sbr(sb, bindex));
  27158. + if (unlikely(err)) {
  27159. + pr_warn("err %d, damaged pseudo link.\n", err);
  27160. + au_hbl_del(&icntnr->plink, hbl);
  27161. + iput(&icntnr->vfs_inode);
  27162. + }
  27163. + } else
  27164. + iput(&icntnr->vfs_inode);
  27165. +}
  27166. +
  27167. +/* free all plinks */
  27168. +void au_plink_put(struct super_block *sb, int verbose)
  27169. +{
  27170. + int i, warned;
  27171. + struct au_sbinfo *sbinfo;
  27172. + struct hlist_bl_head *hbl;
  27173. + struct hlist_bl_node *pos, *tmp;
  27174. + struct au_icntnr *icntnr;
  27175. +
  27176. + SiMustWriteLock(sb);
  27177. +
  27178. + sbinfo = au_sbi(sb);
  27179. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  27180. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  27181. +
  27182. + /* no spin_lock since sbinfo is write-locked */
  27183. + warned = 0;
  27184. + for (i = 0; i < AuPlink_NHASH; i++) {
  27185. + hbl = sbinfo->si_plink + i;
  27186. + if (!warned && verbose && !hlist_bl_empty(hbl)) {
  27187. + pr_warn("pseudo-link is not flushed");
  27188. + warned = 1;
  27189. + }
  27190. + hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
  27191. + iput(&icntnr->vfs_inode);
  27192. + INIT_HLIST_BL_HEAD(hbl);
  27193. + }
  27194. +}
  27195. +
  27196. +void au_plink_clean(struct super_block *sb, int verbose)
  27197. +{
  27198. + struct dentry *root;
  27199. +
  27200. + root = sb->s_root;
  27201. + aufs_write_lock(root);
  27202. + if (au_opt_test(au_mntflags(sb), PLINK))
  27203. + au_plink_put(sb, verbose);
  27204. + aufs_write_unlock(root);
  27205. +}
  27206. +
  27207. +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
  27208. +{
  27209. + int do_put;
  27210. + aufs_bindex_t btop, bbot, bindex;
  27211. +
  27212. + do_put = 0;
  27213. + btop = au_ibtop(inode);
  27214. + bbot = au_ibbot(inode);
  27215. + if (btop >= 0) {
  27216. + for (bindex = btop; bindex <= bbot; bindex++) {
  27217. + if (!au_h_iptr(inode, bindex)
  27218. + || au_ii_br_id(inode, bindex) != br_id)
  27219. + continue;
  27220. + au_set_h_iptr(inode, bindex, NULL, 0);
  27221. + do_put = 1;
  27222. + break;
  27223. + }
  27224. + if (do_put)
  27225. + for (bindex = btop; bindex <= bbot; bindex++)
  27226. + if (au_h_iptr(inode, bindex)) {
  27227. + do_put = 0;
  27228. + break;
  27229. + }
  27230. + } else
  27231. + do_put = 1;
  27232. +
  27233. + return do_put;
  27234. +}
  27235. +
  27236. +/* free the plinks on a branch specified by @br_id */
  27237. +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
  27238. +{
  27239. + struct au_sbinfo *sbinfo;
  27240. + struct hlist_bl_head *hbl;
  27241. + struct hlist_bl_node *pos, *tmp;
  27242. + struct au_icntnr *icntnr;
  27243. + struct inode *inode;
  27244. + int i, do_put;
  27245. +
  27246. + SiMustWriteLock(sb);
  27247. +
  27248. + sbinfo = au_sbi(sb);
  27249. + AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
  27250. + AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
  27251. +
  27252. + /* no bit_lock since sbinfo is write-locked */
  27253. + for (i = 0; i < AuPlink_NHASH; i++) {
  27254. + hbl = sbinfo->si_plink + i;
  27255. + hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
  27256. + inode = au_igrab(&icntnr->vfs_inode);
  27257. + ii_write_lock_child(inode);
  27258. + do_put = au_plink_do_half_refresh(inode, br_id);
  27259. + if (do_put) {
  27260. + hlist_bl_del(&icntnr->plink);
  27261. + iput(inode);
  27262. + }
  27263. + ii_write_unlock(inode);
  27264. + iput(inode);
  27265. + }
  27266. + }
  27267. +}
  27268. diff -Naur null/fs/aufs/poll.c linux-5.1/fs/aufs/poll.c
  27269. --- /dev/null
  27270. +++ linux-5.1/fs/aufs/poll.c 2019-05-22 11:23:07.442140950 +0200
  27271. @@ -0,0 +1,51 @@
  27272. +// SPDX-License-Identifier: GPL-2.0
  27273. +/*
  27274. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  27275. + *
  27276. + * This program, aufs is free software; you can redistribute it and/or modify
  27277. + * it under the terms of the GNU General Public License as published by
  27278. + * the Free Software Foundation; either version 2 of the License, or
  27279. + * (at your option) any later version.
  27280. + *
  27281. + * This program is distributed in the hope that it will be useful,
  27282. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27283. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27284. + * GNU General Public License for more details.
  27285. + *
  27286. + * You should have received a copy of the GNU General Public License
  27287. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27288. + */
  27289. +
  27290. +/*
  27291. + * poll operation
  27292. + * There is only one filesystem which implements ->poll operation, currently.
  27293. + */
  27294. +
  27295. +#include "aufs.h"
  27296. +
  27297. +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
  27298. +{
  27299. + __poll_t mask;
  27300. + struct file *h_file;
  27301. + struct super_block *sb;
  27302. +
  27303. + /* We should pretend an error happened. */
  27304. + mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
  27305. + sb = file->f_path.dentry->d_sb;
  27306. + si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
  27307. +
  27308. + h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
  27309. + if (IS_ERR(h_file)) {
  27310. + AuDbg("h_file %ld\n", PTR_ERR(h_file));
  27311. + goto out;
  27312. + }
  27313. +
  27314. + mask = vfs_poll(h_file, pt);
  27315. + fput(h_file); /* instead of au_read_post() */
  27316. +
  27317. +out:
  27318. + si_read_unlock(sb);
  27319. + if (mask & EPOLLERR)
  27320. + AuDbg("mask 0x%x\n", mask);
  27321. + return mask;
  27322. +}
  27323. diff -Naur null/fs/aufs/posix_acl.c linux-5.1/fs/aufs/posix_acl.c
  27324. --- /dev/null
  27325. +++ linux-5.1/fs/aufs/posix_acl.c 2019-05-22 11:23:07.442140950 +0200
  27326. @@ -0,0 +1,103 @@
  27327. +// SPDX-License-Identifier: GPL-2.0
  27328. +/*
  27329. + * Copyright (C) 2014-2019 Junjiro R. Okajima
  27330. + *
  27331. + * This program, aufs is free software; you can redistribute it and/or modify
  27332. + * it under the terms of the GNU General Public License as published by
  27333. + * the Free Software Foundation; either version 2 of the License, or
  27334. + * (at your option) any later version.
  27335. + *
  27336. + * This program is distributed in the hope that it will be useful,
  27337. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27338. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27339. + * GNU General Public License for more details.
  27340. + *
  27341. + * You should have received a copy of the GNU General Public License
  27342. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27343. + */
  27344. +
  27345. +/*
  27346. + * posix acl operations
  27347. + */
  27348. +
  27349. +#include <linux/fs.h>
  27350. +#include "aufs.h"
  27351. +
  27352. +struct posix_acl *aufs_get_acl(struct inode *inode, int type)
  27353. +{
  27354. + struct posix_acl *acl;
  27355. + int err;
  27356. + aufs_bindex_t bindex;
  27357. + struct inode *h_inode;
  27358. + struct super_block *sb;
  27359. +
  27360. + acl = NULL;
  27361. + sb = inode->i_sb;
  27362. + si_read_lock(sb, AuLock_FLUSH);
  27363. + ii_read_lock_child(inode);
  27364. + if (!(sb->s_flags & SB_POSIXACL))
  27365. + goto out;
  27366. +
  27367. + bindex = au_ibtop(inode);
  27368. + h_inode = au_h_iptr(inode, bindex);
  27369. + if (unlikely(!h_inode
  27370. + || ((h_inode->i_mode & S_IFMT)
  27371. + != (inode->i_mode & S_IFMT)))) {
  27372. + err = au_busy_or_stale();
  27373. + acl = ERR_PTR(err);
  27374. + goto out;
  27375. + }
  27376. +
  27377. + /* always topmost only */
  27378. + acl = get_acl(h_inode, type);
  27379. + if (!IS_ERR_OR_NULL(acl))
  27380. + set_cached_acl(inode, type, acl);
  27381. +
  27382. +out:
  27383. + ii_read_unlock(inode);
  27384. + si_read_unlock(sb);
  27385. +
  27386. + AuTraceErrPtr(acl);
  27387. + return acl;
  27388. +}
  27389. +
  27390. +int aufs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  27391. +{
  27392. + int err;
  27393. + ssize_t ssz;
  27394. + struct dentry *dentry;
  27395. + struct au_sxattr arg = {
  27396. + .type = AU_ACL_SET,
  27397. + .u.acl_set = {
  27398. + .acl = acl,
  27399. + .type = type
  27400. + },
  27401. + };
  27402. +
  27403. + IMustLock(inode);
  27404. +
  27405. + if (inode->i_ino == AUFS_ROOT_INO)
  27406. + dentry = dget(inode->i_sb->s_root);
  27407. + else {
  27408. + dentry = d_find_alias(inode);
  27409. + if (!dentry)
  27410. + dentry = d_find_any_alias(inode);
  27411. + if (!dentry) {
  27412. + pr_warn("cannot handle this inode, "
  27413. + "please report to aufs-users ML\n");
  27414. + err = -ENOENT;
  27415. + goto out;
  27416. + }
  27417. + }
  27418. +
  27419. + ssz = au_sxattr(dentry, inode, &arg);
  27420. + dput(dentry);
  27421. + err = ssz;
  27422. + if (ssz >= 0) {
  27423. + err = 0;
  27424. + set_cached_acl(inode, type, acl);
  27425. + }
  27426. +
  27427. +out:
  27428. + return err;
  27429. +}
  27430. diff -Naur null/fs/aufs/procfs.c linux-5.1/fs/aufs/procfs.c
  27431. --- /dev/null
  27432. +++ linux-5.1/fs/aufs/procfs.c 2019-05-22 11:23:07.442140950 +0200
  27433. @@ -0,0 +1,171 @@
  27434. +// SPDX-License-Identifier: GPL-2.0
  27435. +/*
  27436. + * Copyright (C) 2010-2019 Junjiro R. Okajima
  27437. + *
  27438. + * This program, aufs is free software; you can redistribute it and/or modify
  27439. + * it under the terms of the GNU General Public License as published by
  27440. + * the Free Software Foundation; either version 2 of the License, or
  27441. + * (at your option) any later version.
  27442. + *
  27443. + * This program is distributed in the hope that it will be useful,
  27444. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27445. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27446. + * GNU General Public License for more details.
  27447. + *
  27448. + * You should have received a copy of the GNU General Public License
  27449. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27450. + */
  27451. +
  27452. +/*
  27453. + * procfs interfaces
  27454. + */
  27455. +
  27456. +#include <linux/proc_fs.h>
  27457. +#include "aufs.h"
  27458. +
  27459. +static int au_procfs_plm_release(struct inode *inode, struct file *file)
  27460. +{
  27461. + struct au_sbinfo *sbinfo;
  27462. +
  27463. + sbinfo = file->private_data;
  27464. + if (sbinfo) {
  27465. + au_plink_maint_leave(sbinfo);
  27466. + kobject_put(&sbinfo->si_kobj);
  27467. + }
  27468. +
  27469. + return 0;
  27470. +}
  27471. +
  27472. +static void au_procfs_plm_write_clean(struct file *file)
  27473. +{
  27474. + struct au_sbinfo *sbinfo;
  27475. +
  27476. + sbinfo = file->private_data;
  27477. + if (sbinfo)
  27478. + au_plink_clean(sbinfo->si_sb, /*verbose*/0);
  27479. +}
  27480. +
  27481. +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
  27482. +{
  27483. + int err;
  27484. + struct super_block *sb;
  27485. + struct au_sbinfo *sbinfo;
  27486. + struct hlist_bl_node *pos;
  27487. +
  27488. + err = -EBUSY;
  27489. + if (unlikely(file->private_data))
  27490. + goto out;
  27491. +
  27492. + sb = NULL;
  27493. + /* don't use au_sbilist_lock() here */
  27494. + hlist_bl_lock(&au_sbilist);
  27495. + hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
  27496. + if (id == sysaufs_si_id(sbinfo)) {
  27497. + kobject_get(&sbinfo->si_kobj);
  27498. + sb = sbinfo->si_sb;
  27499. + break;
  27500. + }
  27501. + hlist_bl_unlock(&au_sbilist);
  27502. +
  27503. + err = -EINVAL;
  27504. + if (unlikely(!sb))
  27505. + goto out;
  27506. +
  27507. + err = au_plink_maint_enter(sb);
  27508. + if (!err)
  27509. + /* keep kobject_get() */
  27510. + file->private_data = sbinfo;
  27511. + else
  27512. + kobject_put(&sbinfo->si_kobj);
  27513. +out:
  27514. + return err;
  27515. +}
  27516. +
  27517. +/*
  27518. + * Accept a valid "si=xxxx" only.
  27519. + * Once it is accepted successfully, accept "clean" too.
  27520. + */
  27521. +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
  27522. + size_t count, loff_t *ppos)
  27523. +{
  27524. + ssize_t err;
  27525. + unsigned long id;
  27526. + /* last newline is allowed */
  27527. + char buf[3 + sizeof(unsigned long) * 2 + 1];
  27528. +
  27529. + err = -EACCES;
  27530. + if (unlikely(!capable(CAP_SYS_ADMIN)))
  27531. + goto out;
  27532. +
  27533. + err = -EINVAL;
  27534. + if (unlikely(count > sizeof(buf)))
  27535. + goto out;
  27536. +
  27537. + err = copy_from_user(buf, ubuf, count);
  27538. + if (unlikely(err)) {
  27539. + err = -EFAULT;
  27540. + goto out;
  27541. + }
  27542. + buf[count] = 0;
  27543. +
  27544. + err = -EINVAL;
  27545. + if (!strcmp("clean", buf)) {
  27546. + au_procfs_plm_write_clean(file);
  27547. + goto out_success;
  27548. + } else if (unlikely(strncmp("si=", buf, 3)))
  27549. + goto out;
  27550. +
  27551. + err = kstrtoul(buf + 3, 16, &id);
  27552. + if (unlikely(err))
  27553. + goto out;
  27554. +
  27555. + err = au_procfs_plm_write_si(file, id);
  27556. + if (unlikely(err))
  27557. + goto out;
  27558. +
  27559. +out_success:
  27560. + err = count; /* success */
  27561. +out:
  27562. + return err;
  27563. +}
  27564. +
  27565. +static const struct file_operations au_procfs_plm_fop = {
  27566. + .write = au_procfs_plm_write,
  27567. + .release = au_procfs_plm_release,
  27568. + .owner = THIS_MODULE
  27569. +};
  27570. +
  27571. +/* ---------------------------------------------------------------------- */
  27572. +
  27573. +static struct proc_dir_entry *au_procfs_dir;
  27574. +
  27575. +void au_procfs_fin(void)
  27576. +{
  27577. + remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
  27578. + remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
  27579. +}
  27580. +
  27581. +int __init au_procfs_init(void)
  27582. +{
  27583. + int err;
  27584. + struct proc_dir_entry *entry;
  27585. +
  27586. + err = -ENOMEM;
  27587. + au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
  27588. + if (unlikely(!au_procfs_dir))
  27589. + goto out;
  27590. +
  27591. + entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
  27592. + au_procfs_dir, &au_procfs_plm_fop);
  27593. + if (unlikely(!entry))
  27594. + goto out_dir;
  27595. +
  27596. + err = 0;
  27597. + goto out; /* success */
  27598. +
  27599. +
  27600. +out_dir:
  27601. + remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
  27602. +out:
  27603. + return err;
  27604. +}
  27605. diff -Naur null/fs/aufs/rdu.c linux-5.1/fs/aufs/rdu.c
  27606. --- /dev/null
  27607. +++ linux-5.1/fs/aufs/rdu.c 2019-05-22 11:23:07.442140950 +0200
  27608. @@ -0,0 +1,384 @@
  27609. +// SPDX-License-Identifier: GPL-2.0
  27610. +/*
  27611. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  27612. + *
  27613. + * This program, aufs is free software; you can redistribute it and/or modify
  27614. + * it under the terms of the GNU General Public License as published by
  27615. + * the Free Software Foundation; either version 2 of the License, or
  27616. + * (at your option) any later version.
  27617. + *
  27618. + * This program is distributed in the hope that it will be useful,
  27619. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27620. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  27621. + * GNU General Public License for more details.
  27622. + *
  27623. + * You should have received a copy of the GNU General Public License
  27624. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  27625. + */
  27626. +
  27627. +/*
  27628. + * readdir in userspace.
  27629. + */
  27630. +
  27631. +#include <linux/compat.h>
  27632. +#include <linux/fs_stack.h>
  27633. +#include <linux/security.h>
  27634. +#include "aufs.h"
  27635. +
  27636. +/* bits for struct aufs_rdu.flags */
  27637. +#define AuRdu_CALLED 1
  27638. +#define AuRdu_CONT (1 << 1)
  27639. +#define AuRdu_FULL (1 << 2)
  27640. +#define au_ftest_rdu(flags, name) ((flags) & AuRdu_##name)
  27641. +#define au_fset_rdu(flags, name) \
  27642. + do { (flags) |= AuRdu_##name; } while (0)
  27643. +#define au_fclr_rdu(flags, name) \
  27644. + do { (flags) &= ~AuRdu_##name; } while (0)
  27645. +
  27646. +struct au_rdu_arg {
  27647. + struct dir_context ctx;
  27648. + struct aufs_rdu *rdu;
  27649. + union au_rdu_ent_ul ent;
  27650. + unsigned long end;
  27651. +
  27652. + struct super_block *sb;
  27653. + int err;
  27654. +};
  27655. +
  27656. +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
  27657. + loff_t offset, u64 h_ino, unsigned int d_type)
  27658. +{
  27659. + int err, len;
  27660. + struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
  27661. + struct aufs_rdu *rdu = arg->rdu;
  27662. + struct au_rdu_ent ent;
  27663. +
  27664. + err = 0;
  27665. + arg->err = 0;
  27666. + au_fset_rdu(rdu->cookie.flags, CALLED);
  27667. + len = au_rdu_len(nlen);
  27668. + if (arg->ent.ul + len < arg->end) {
  27669. + ent.ino = h_ino;
  27670. + ent.bindex = rdu->cookie.bindex;
  27671. + ent.type = d_type;
  27672. + ent.nlen = nlen;
  27673. + if (unlikely(nlen > AUFS_MAX_NAMELEN))
  27674. + ent.type = DT_UNKNOWN;
  27675. +
  27676. + /* unnecessary to support mmap_sem since this is a dir */
  27677. + err = -EFAULT;
  27678. + if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
  27679. + goto out;
  27680. + if (copy_to_user(arg->ent.e->name, name, nlen))
  27681. + goto out;
  27682. + /* the terminating NULL */
  27683. + if (__put_user(0, arg->ent.e->name + nlen))
  27684. + goto out;
  27685. + err = 0;
  27686. + /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
  27687. + arg->ent.ul += len;
  27688. + rdu->rent++;
  27689. + } else {
  27690. + err = -EFAULT;
  27691. + au_fset_rdu(rdu->cookie.flags, FULL);
  27692. + rdu->full = 1;
  27693. + rdu->tail = arg->ent;
  27694. + }
  27695. +
  27696. +out:
  27697. + /* AuTraceErr(err); */
  27698. + return err;
  27699. +}
  27700. +
  27701. +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
  27702. +{
  27703. + int err;
  27704. + loff_t offset;
  27705. + struct au_rdu_cookie *cookie = &arg->rdu->cookie;
  27706. +
  27707. + /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
  27708. + offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
  27709. + err = offset;
  27710. + if (unlikely(offset != cookie->h_pos))
  27711. + goto out;
  27712. +
  27713. + err = 0;
  27714. + do {
  27715. + arg->err = 0;
  27716. + au_fclr_rdu(cookie->flags, CALLED);
  27717. + /* smp_mb(); */
  27718. + err = vfsub_iterate_dir(h_file, &arg->ctx);
  27719. + if (err >= 0)
  27720. + err = arg->err;
  27721. + } while (!err
  27722. + && au_ftest_rdu(cookie->flags, CALLED)
  27723. + && !au_ftest_rdu(cookie->flags, FULL));
  27724. + cookie->h_pos = h_file->f_pos;
  27725. +
  27726. +out:
  27727. + AuTraceErr(err);
  27728. + return err;
  27729. +}
  27730. +
  27731. +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
  27732. +{
  27733. + int err;
  27734. + aufs_bindex_t bbot;
  27735. + struct au_rdu_arg arg = {
  27736. + .ctx = {
  27737. + .actor = au_rdu_fill
  27738. + }
  27739. + };
  27740. + struct dentry *dentry;
  27741. + struct inode *inode;
  27742. + struct file *h_file;
  27743. + struct au_rdu_cookie *cookie = &rdu->cookie;
  27744. +
  27745. + /* VERIFY_WRITE */
  27746. + err = !access_ok(rdu->ent.e, rdu->sz);
  27747. + if (unlikely(err)) {
  27748. + err = -EFAULT;
  27749. + AuTraceErr(err);
  27750. + goto out;
  27751. + }
  27752. + rdu->rent = 0;
  27753. + rdu->tail = rdu->ent;
  27754. + rdu->full = 0;
  27755. + arg.rdu = rdu;
  27756. + arg.ent = rdu->ent;
  27757. + arg.end = arg.ent.ul;
  27758. + arg.end += rdu->sz;
  27759. +
  27760. + err = -ENOTDIR;
  27761. + if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
  27762. + goto out;
  27763. +
  27764. + err = security_file_permission(file, MAY_READ);
  27765. + AuTraceErr(err);
  27766. + if (unlikely(err))
  27767. + goto out;
  27768. +
  27769. + dentry = file->f_path.dentry;
  27770. + inode = d_inode(dentry);
  27771. + inode_lock_shared(inode);
  27772. +
  27773. + arg.sb = inode->i_sb;
  27774. + err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
  27775. + if (unlikely(err))
  27776. + goto out_mtx;
  27777. + err = au_alive_dir(dentry);
  27778. + if (unlikely(err))
  27779. + goto out_si;
  27780. + /* todo: reval? */
  27781. + fi_read_lock(file);
  27782. +
  27783. + err = -EAGAIN;
  27784. + if (unlikely(au_ftest_rdu(cookie->flags, CONT)
  27785. + && cookie->generation != au_figen(file)))
  27786. + goto out_unlock;
  27787. +
  27788. + err = 0;
  27789. + if (!rdu->blk) {
  27790. + rdu->blk = au_sbi(arg.sb)->si_rdblk;
  27791. + if (!rdu->blk)
  27792. + rdu->blk = au_dir_size(file, /*dentry*/NULL);
  27793. + }
  27794. + bbot = au_fbtop(file);
  27795. + if (cookie->bindex < bbot)
  27796. + cookie->bindex = bbot;
  27797. + bbot = au_fbbot_dir(file);
  27798. + /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
  27799. + for (; !err && cookie->bindex <= bbot;
  27800. + cookie->bindex++, cookie->h_pos = 0) {
  27801. + h_file = au_hf_dir(file, cookie->bindex);
  27802. + if (!h_file)
  27803. + continue;
  27804. +
  27805. + au_fclr_rdu(cookie->flags, FULL);
  27806. + err = au_rdu_do(h_file, &arg);
  27807. + AuTraceErr(err);
  27808. + if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
  27809. + break;
  27810. + }
  27811. + AuDbg("rent %llu\n", rdu->rent);
  27812. +
  27813. + if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
  27814. + rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
  27815. + au_fset_rdu(cookie->flags, CONT);
  27816. + cookie->generation = au_figen(file);
  27817. + }
  27818. +
  27819. + ii_read_lock_child(inode);
  27820. + fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
  27821. + ii_read_unlock(inode);
  27822. +
  27823. +out_unlock:
  27824. + fi_read_unlock(file);
  27825. +out_si:
  27826. + si_read_unlock(arg.sb);
  27827. +out_mtx:
  27828. + inode_unlock_shared(inode);
  27829. +out:
  27830. + AuTraceErr(err);
  27831. + return err;
  27832. +}
  27833. +
  27834. +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
  27835. +{
  27836. + int err;
  27837. + ino_t ino;
  27838. + unsigned long long nent;
  27839. + union au_rdu_ent_ul *u;
  27840. + struct au_rdu_ent ent;
  27841. + struct super_block *sb;
  27842. +
  27843. + err = 0;
  27844. + nent = rdu->nent;
  27845. + u = &rdu->ent;
  27846. + sb = file->f_path.dentry->d_sb;
  27847. + si_read_lock(sb, AuLock_FLUSH);
  27848. + while (nent-- > 0) {
  27849. + /* unnecessary to support mmap_sem since this is a dir */
  27850. + err = copy_from_user(&ent, u->e, sizeof(ent));
  27851. + if (!err)
  27852. + /* VERIFY_WRITE */
  27853. + err = !access_ok(&u->e->ino, sizeof(ino));
  27854. + if (unlikely(err)) {
  27855. + err = -EFAULT;
  27856. + AuTraceErr(err);
  27857. + break;
  27858. + }
  27859. +
  27860. + /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
  27861. + if (!ent.wh)
  27862. + err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
  27863. + else
  27864. + err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
  27865. + &ino);
  27866. + if (unlikely(err)) {
  27867. + AuTraceErr(err);
  27868. + break;
  27869. + }
  27870. +
  27871. + err = __put_user(ino, &u->e->ino);
  27872. + if (unlikely(err)) {
  27873. + err = -EFAULT;
  27874. + AuTraceErr(err);
  27875. + break;
  27876. + }
  27877. + u->ul += au_rdu_len(ent.nlen);
  27878. + }
  27879. + si_read_unlock(sb);
  27880. +
  27881. + return err;
  27882. +}
  27883. +
  27884. +/* ---------------------------------------------------------------------- */
  27885. +
  27886. +static int au_rdu_verify(struct aufs_rdu *rdu)
  27887. +{
  27888. + AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
  27889. + "%llu, b%d, 0x%x, g%u}\n",
  27890. + rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
  27891. + rdu->blk,
  27892. + rdu->rent, rdu->shwh, rdu->full,
  27893. + rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
  27894. + rdu->cookie.generation);
  27895. +
  27896. + if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
  27897. + return 0;
  27898. +
  27899. + AuDbg("%u:%u\n",
  27900. + rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
  27901. + return -EINVAL;
  27902. +}
  27903. +
  27904. +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  27905. +{
  27906. + long err, e;
  27907. + struct aufs_rdu rdu;
  27908. + void __user *p = (void __user *)arg;
  27909. +
  27910. + err = copy_from_user(&rdu, p, sizeof(rdu));
  27911. + if (unlikely(err)) {
  27912. + err = -EFAULT;
  27913. + AuTraceErr(err);
  27914. + goto out;
  27915. + }
  27916. + err = au_rdu_verify(&rdu);
  27917. + if (unlikely(err))
  27918. + goto out;
  27919. +
  27920. + switch (cmd) {
  27921. + case AUFS_CTL_RDU:
  27922. + err = au_rdu(file, &rdu);
  27923. + if (unlikely(err))
  27924. + break;
  27925. +
  27926. + e = copy_to_user(p, &rdu, sizeof(rdu));
  27927. + if (unlikely(e)) {
  27928. + err = -EFAULT;
  27929. + AuTraceErr(err);
  27930. + }
  27931. + break;
  27932. + case AUFS_CTL_RDU_INO:
  27933. + err = au_rdu_ino(file, &rdu);
  27934. + break;
  27935. +
  27936. + default:
  27937. + /* err = -ENOTTY; */
  27938. + err = -EINVAL;
  27939. + }
  27940. +
  27941. +out:
  27942. + AuTraceErr(err);
  27943. + return err;
  27944. +}
  27945. +
  27946. +#ifdef CONFIG_COMPAT
  27947. +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  27948. +{
  27949. + long err, e;
  27950. + struct aufs_rdu rdu;
  27951. + void __user *p = compat_ptr(arg);
  27952. +
  27953. + /* todo: get_user()? */
  27954. + err = copy_from_user(&rdu, p, sizeof(rdu));
  27955. + if (unlikely(err)) {
  27956. + err = -EFAULT;
  27957. + AuTraceErr(err);
  27958. + goto out;
  27959. + }
  27960. + rdu.ent.e = compat_ptr(rdu.ent.ul);
  27961. + err = au_rdu_verify(&rdu);
  27962. + if (unlikely(err))
  27963. + goto out;
  27964. +
  27965. + switch (cmd) {
  27966. + case AUFS_CTL_RDU:
  27967. + err = au_rdu(file, &rdu);
  27968. + if (unlikely(err))
  27969. + break;
  27970. +
  27971. + rdu.ent.ul = ptr_to_compat(rdu.ent.e);
  27972. + rdu.tail.ul = ptr_to_compat(rdu.tail.e);
  27973. + e = copy_to_user(p, &rdu, sizeof(rdu));
  27974. + if (unlikely(e)) {
  27975. + err = -EFAULT;
  27976. + AuTraceErr(err);
  27977. + }
  27978. + break;
  27979. + case AUFS_CTL_RDU_INO:
  27980. + err = au_rdu_ino(file, &rdu);
  27981. + break;
  27982. +
  27983. + default:
  27984. + /* err = -ENOTTY; */
  27985. + err = -EINVAL;
  27986. + }
  27987. +
  27988. +out:
  27989. + AuTraceErr(err);
  27990. + return err;
  27991. +}
  27992. +#endif
  27993. diff -Naur null/fs/aufs/rwsem.h linux-5.1/fs/aufs/rwsem.h
  27994. --- /dev/null
  27995. +++ linux-5.1/fs/aufs/rwsem.h 2019-05-22 11:23:07.442140950 +0200
  27996. @@ -0,0 +1,73 @@
  27997. +/* SPDX-License-Identifier: GPL-2.0 */
  27998. +/*
  27999. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  28000. + *
  28001. + * This program, aufs is free software; you can redistribute it and/or modify
  28002. + * it under the terms of the GNU General Public License as published by
  28003. + * the Free Software Foundation; either version 2 of the License, or
  28004. + * (at your option) any later version.
  28005. + *
  28006. + * This program is distributed in the hope that it will be useful,
  28007. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28008. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28009. + * GNU General Public License for more details.
  28010. + *
  28011. + * You should have received a copy of the GNU General Public License
  28012. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28013. + */
  28014. +
  28015. +/*
  28016. + * simple read-write semaphore wrappers
  28017. + */
  28018. +
  28019. +#ifndef __AUFS_RWSEM_H__
  28020. +#define __AUFS_RWSEM_H__
  28021. +
  28022. +#ifdef __KERNEL__
  28023. +
  28024. +#include "debug.h"
  28025. +
  28026. +/* in the future, the name 'au_rwsem' will be totally gone */
  28027. +#define au_rwsem rw_semaphore
  28028. +
  28029. +/* to debug easier, do not make them inlined functions */
  28030. +#define AuRwMustNoWaiters(rw) AuDebugOn(rwsem_is_contended(rw))
  28031. +/* rwsem_is_locked() is unusable */
  28032. +#define AuRwMustReadLock(rw) AuDebugOn(!lockdep_recursing(current) \
  28033. + && debug_locks \
  28034. + && !lockdep_is_held_type(rw, 1))
  28035. +#define AuRwMustWriteLock(rw) AuDebugOn(!lockdep_recursing(current) \
  28036. + && debug_locks \
  28037. + && !lockdep_is_held_type(rw, 0))
  28038. +#define AuRwMustAnyLock(rw) AuDebugOn(!lockdep_recursing(current) \
  28039. + && debug_locks \
  28040. + && !lockdep_is_held(rw))
  28041. +#define AuRwDestroy(rw) AuDebugOn(!lockdep_recursing(current) \
  28042. + && debug_locks \
  28043. + && lockdep_is_held(rw))
  28044. +
  28045. +#define au_rw_init(rw) init_rwsem(rw)
  28046. +
  28047. +#define au_rw_init_wlock(rw) do { \
  28048. + au_rw_init(rw); \
  28049. + down_write(rw); \
  28050. + } while (0)
  28051. +
  28052. +#define au_rw_init_wlock_nested(rw, lsc) do { \
  28053. + au_rw_init(rw); \
  28054. + down_write_nested(rw, lsc); \
  28055. + } while (0)
  28056. +
  28057. +#define au_rw_read_lock(rw) down_read(rw)
  28058. +#define au_rw_read_lock_nested(rw, lsc) down_read_nested(rw, lsc)
  28059. +#define au_rw_read_unlock(rw) up_read(rw)
  28060. +#define au_rw_dgrade_lock(rw) downgrade_write(rw)
  28061. +#define au_rw_write_lock(rw) down_write(rw)
  28062. +#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
  28063. +#define au_rw_write_unlock(rw) up_write(rw)
  28064. +/* why is not _nested version defined? */
  28065. +#define au_rw_read_trylock(rw) down_read_trylock(rw)
  28066. +#define au_rw_write_trylock(rw) down_write_trylock(rw)
  28067. +
  28068. +#endif /* __KERNEL__ */
  28069. +#endif /* __AUFS_RWSEM_H__ */
  28070. diff -Naur null/fs/aufs/sbinfo.c linux-5.1/fs/aufs/sbinfo.c
  28071. --- /dev/null
  28072. +++ linux-5.1/fs/aufs/sbinfo.c 2019-05-22 11:23:07.442140950 +0200
  28073. @@ -0,0 +1,314 @@
  28074. +// SPDX-License-Identifier: GPL-2.0
  28075. +/*
  28076. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  28077. + *
  28078. + * This program, aufs is free software; you can redistribute it and/or modify
  28079. + * it under the terms of the GNU General Public License as published by
  28080. + * the Free Software Foundation; either version 2 of the License, or
  28081. + * (at your option) any later version.
  28082. + *
  28083. + * This program is distributed in the hope that it will be useful,
  28084. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28085. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28086. + * GNU General Public License for more details.
  28087. + *
  28088. + * You should have received a copy of the GNU General Public License
  28089. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28090. + */
  28091. +
  28092. +/*
  28093. + * superblock private data
  28094. + */
  28095. +
  28096. +#include <linux/iversion.h>
  28097. +#include "aufs.h"
  28098. +
  28099. +/*
  28100. + * they are necessary regardless sysfs is disabled.
  28101. + */
  28102. +void au_si_free(struct kobject *kobj)
  28103. +{
  28104. + int i;
  28105. + struct au_sbinfo *sbinfo;
  28106. + char *locked __maybe_unused; /* debug only */
  28107. +
  28108. + sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
  28109. + for (i = 0; i < AuPlink_NHASH; i++)
  28110. + AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
  28111. + AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
  28112. +
  28113. + AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
  28114. + au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
  28115. + AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
  28116. + au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
  28117. +
  28118. + dbgaufs_si_fin(sbinfo);
  28119. + au_rw_write_lock(&sbinfo->si_rwsem);
  28120. + au_br_free(sbinfo);
  28121. + au_rw_write_unlock(&sbinfo->si_rwsem);
  28122. +
  28123. + au_kfree_try_rcu(sbinfo->si_branch);
  28124. + mutex_destroy(&sbinfo->si_xib_mtx);
  28125. + AuRwDestroy(&sbinfo->si_rwsem);
  28126. +
  28127. + au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
  28128. + /* si_nfiles is waited too */
  28129. + au_kfree_rcu(sbinfo);
  28130. +}
  28131. +
  28132. +int au_si_alloc(struct super_block *sb)
  28133. +{
  28134. + int err, i;
  28135. + struct au_sbinfo *sbinfo;
  28136. +
  28137. + err = -ENOMEM;
  28138. + sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
  28139. + if (unlikely(!sbinfo))
  28140. + goto out;
  28141. +
  28142. + /* will be reallocated separately */
  28143. + sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
  28144. + if (unlikely(!sbinfo->si_branch))
  28145. + goto out_sbinfo;
  28146. +
  28147. + err = sysaufs_si_init(sbinfo);
  28148. + if (!err) {
  28149. + dbgaufs_si_null(sbinfo);
  28150. + err = dbgaufs_si_init(sbinfo);
  28151. + if (unlikely(err))
  28152. + kobject_put(&sbinfo->si_kobj);
  28153. + }
  28154. + if (unlikely(err))
  28155. + goto out_br;
  28156. +
  28157. + au_nwt_init(&sbinfo->si_nowait);
  28158. + au_rw_init_wlock(&sbinfo->si_rwsem);
  28159. +
  28160. + au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
  28161. + au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
  28162. +
  28163. + sbinfo->si_bbot = -1;
  28164. + sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
  28165. +
  28166. + sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
  28167. + sbinfo->si_wbr_create = AuWbrCreate_Def;
  28168. + sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
  28169. + sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
  28170. +
  28171. + au_fhsm_init(sbinfo);
  28172. +
  28173. + sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
  28174. +
  28175. + sbinfo->si_xino_jiffy = jiffies;
  28176. + sbinfo->si_xino_expire
  28177. + = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
  28178. + mutex_init(&sbinfo->si_xib_mtx);
  28179. + /* leave si_xib_last_pindex and si_xib_next_bit */
  28180. +
  28181. + INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
  28182. +
  28183. + sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
  28184. + sbinfo->si_rdblk = AUFS_RDBLK_DEF;
  28185. + sbinfo->si_rdhash = AUFS_RDHASH_DEF;
  28186. + sbinfo->si_dirwh = AUFS_DIRWH_DEF;
  28187. +
  28188. + for (i = 0; i < AuPlink_NHASH; i++)
  28189. + INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
  28190. + init_waitqueue_head(&sbinfo->si_plink_wq);
  28191. + spin_lock_init(&sbinfo->si_plink_maint_lock);
  28192. +
  28193. + INIT_HLIST_BL_HEAD(&sbinfo->si_files);
  28194. +
  28195. + /* with getattr by default */
  28196. + sbinfo->si_iop_array = aufs_iop;
  28197. +
  28198. + /* leave other members for sysaufs and si_mnt. */
  28199. + sbinfo->si_sb = sb;
  28200. + sb->s_fs_info = sbinfo;
  28201. + si_pid_set(sb);
  28202. + return 0; /* success */
  28203. +
  28204. +out_br:
  28205. + au_kfree_try_rcu(sbinfo->si_branch);
  28206. +out_sbinfo:
  28207. + au_kfree_rcu(sbinfo);
  28208. +out:
  28209. + return err;
  28210. +}
  28211. +
  28212. +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
  28213. +{
  28214. + int err, sz;
  28215. + struct au_branch **brp;
  28216. +
  28217. + AuRwMustWriteLock(&sbinfo->si_rwsem);
  28218. +
  28219. + err = -ENOMEM;
  28220. + sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
  28221. + if (unlikely(!sz))
  28222. + sz = sizeof(*brp);
  28223. + brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
  28224. + may_shrink);
  28225. + if (brp) {
  28226. + sbinfo->si_branch = brp;
  28227. + err = 0;
  28228. + }
  28229. +
  28230. + return err;
  28231. +}
  28232. +
  28233. +/* ---------------------------------------------------------------------- */
  28234. +
  28235. +unsigned int au_sigen_inc(struct super_block *sb)
  28236. +{
  28237. + unsigned int gen;
  28238. + struct inode *inode;
  28239. +
  28240. + SiMustWriteLock(sb);
  28241. +
  28242. + gen = ++au_sbi(sb)->si_generation;
  28243. + au_update_digen(sb->s_root);
  28244. + inode = d_inode(sb->s_root);
  28245. + au_update_iigen(inode, /*half*/0);
  28246. + inode_inc_iversion(inode);
  28247. + return gen;
  28248. +}
  28249. +
  28250. +aufs_bindex_t au_new_br_id(struct super_block *sb)
  28251. +{
  28252. + aufs_bindex_t br_id;
  28253. + int i;
  28254. + struct au_sbinfo *sbinfo;
  28255. +
  28256. + SiMustWriteLock(sb);
  28257. +
  28258. + sbinfo = au_sbi(sb);
  28259. + for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
  28260. + br_id = ++sbinfo->si_last_br_id;
  28261. + AuDebugOn(br_id < 0);
  28262. + if (br_id && au_br_index(sb, br_id) < 0)
  28263. + return br_id;
  28264. + }
  28265. +
  28266. + return -1;
  28267. +}
  28268. +
  28269. +/* ---------------------------------------------------------------------- */
  28270. +
  28271. +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
  28272. +int si_read_lock(struct super_block *sb, int flags)
  28273. +{
  28274. + int err;
  28275. +
  28276. + err = 0;
  28277. + if (au_ftest_lock(flags, FLUSH))
  28278. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  28279. +
  28280. + si_noflush_read_lock(sb);
  28281. + err = au_plink_maint(sb, flags);
  28282. + if (unlikely(err))
  28283. + si_read_unlock(sb);
  28284. +
  28285. + return err;
  28286. +}
  28287. +
  28288. +int si_write_lock(struct super_block *sb, int flags)
  28289. +{
  28290. + int err;
  28291. +
  28292. + if (au_ftest_lock(flags, FLUSH))
  28293. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  28294. +
  28295. + si_noflush_write_lock(sb);
  28296. + err = au_plink_maint(sb, flags);
  28297. + if (unlikely(err))
  28298. + si_write_unlock(sb);
  28299. +
  28300. + return err;
  28301. +}
  28302. +
  28303. +/* dentry and super_block lock. call at entry point */
  28304. +int aufs_read_lock(struct dentry *dentry, int flags)
  28305. +{
  28306. + int err;
  28307. + struct super_block *sb;
  28308. +
  28309. + sb = dentry->d_sb;
  28310. + err = si_read_lock(sb, flags);
  28311. + if (unlikely(err))
  28312. + goto out;
  28313. +
  28314. + if (au_ftest_lock(flags, DW))
  28315. + di_write_lock_child(dentry);
  28316. + else
  28317. + di_read_lock_child(dentry, flags);
  28318. +
  28319. + if (au_ftest_lock(flags, GEN)) {
  28320. + err = au_digen_test(dentry, au_sigen(sb));
  28321. + if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
  28322. + AuDebugOn(!err && au_dbrange_test(dentry));
  28323. + else if (!err)
  28324. + err = au_dbrange_test(dentry);
  28325. + if (unlikely(err))
  28326. + aufs_read_unlock(dentry, flags);
  28327. + }
  28328. +
  28329. +out:
  28330. + return err;
  28331. +}
  28332. +
  28333. +void aufs_read_unlock(struct dentry *dentry, int flags)
  28334. +{
  28335. + if (au_ftest_lock(flags, DW))
  28336. + di_write_unlock(dentry);
  28337. + else
  28338. + di_read_unlock(dentry, flags);
  28339. + si_read_unlock(dentry->d_sb);
  28340. +}
  28341. +
  28342. +void aufs_write_lock(struct dentry *dentry)
  28343. +{
  28344. + si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
  28345. + di_write_lock_child(dentry);
  28346. +}
  28347. +
  28348. +void aufs_write_unlock(struct dentry *dentry)
  28349. +{
  28350. + di_write_unlock(dentry);
  28351. + si_write_unlock(dentry->d_sb);
  28352. +}
  28353. +
  28354. +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
  28355. +{
  28356. + int err;
  28357. + unsigned int sigen;
  28358. + struct super_block *sb;
  28359. +
  28360. + sb = d1->d_sb;
  28361. + err = si_read_lock(sb, flags);
  28362. + if (unlikely(err))
  28363. + goto out;
  28364. +
  28365. + di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
  28366. +
  28367. + if (au_ftest_lock(flags, GEN)) {
  28368. + sigen = au_sigen(sb);
  28369. + err = au_digen_test(d1, sigen);
  28370. + AuDebugOn(!err && au_dbrange_test(d1));
  28371. + if (!err) {
  28372. + err = au_digen_test(d2, sigen);
  28373. + AuDebugOn(!err && au_dbrange_test(d2));
  28374. + }
  28375. + if (unlikely(err))
  28376. + aufs_read_and_write_unlock2(d1, d2);
  28377. + }
  28378. +
  28379. +out:
  28380. + return err;
  28381. +}
  28382. +
  28383. +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
  28384. +{
  28385. + di_write_unlock2(d1, d2);
  28386. + si_read_unlock(d1->d_sb);
  28387. +}
  28388. diff -Naur null/fs/aufs/super.c linux-5.1/fs/aufs/super.c
  28389. --- /dev/null
  28390. +++ linux-5.1/fs/aufs/super.c 2019-05-22 11:23:07.443140904 +0200
  28391. @@ -0,0 +1,1049 @@
  28392. +// SPDX-License-Identifier: GPL-2.0
  28393. +/*
  28394. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  28395. + *
  28396. + * This program, aufs is free software; you can redistribute it and/or modify
  28397. + * it under the terms of the GNU General Public License as published by
  28398. + * the Free Software Foundation; either version 2 of the License, or
  28399. + * (at your option) any later version.
  28400. + *
  28401. + * This program is distributed in the hope that it will be useful,
  28402. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28403. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28404. + * GNU General Public License for more details.
  28405. + *
  28406. + * You should have received a copy of the GNU General Public License
  28407. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28408. + */
  28409. +
  28410. +/*
  28411. + * mount and super_block operations
  28412. + */
  28413. +
  28414. +#include <linux/iversion.h>
  28415. +#include <linux/mm.h>
  28416. +#include <linux/seq_file.h>
  28417. +#include <linux/statfs.h>
  28418. +#include <linux/vmalloc.h>
  28419. +#include "aufs.h"
  28420. +
  28421. +/*
  28422. + * super_operations
  28423. + */
  28424. +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
  28425. +{
  28426. + struct au_icntnr *c;
  28427. +
  28428. + c = au_cache_alloc_icntnr();
  28429. + if (c) {
  28430. + au_icntnr_init(c);
  28431. + inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
  28432. + c->iinfo.ii_hinode = NULL;
  28433. + return &c->vfs_inode;
  28434. + }
  28435. + return NULL;
  28436. +}
  28437. +
  28438. +static void aufs_destroy_inode_cb(struct rcu_head *head)
  28439. +{
  28440. + struct inode *inode = container_of(head, struct inode, i_rcu);
  28441. +
  28442. + au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
  28443. +}
  28444. +
  28445. +static void aufs_destroy_inode(struct inode *inode)
  28446. +{
  28447. + if (!au_is_bad_inode(inode))
  28448. + au_iinfo_fin(inode);
  28449. + call_rcu(&inode->i_rcu, aufs_destroy_inode_cb);
  28450. +}
  28451. +
  28452. +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
  28453. +{
  28454. + struct inode *inode;
  28455. + int err;
  28456. +
  28457. + inode = iget_locked(sb, ino);
  28458. + if (unlikely(!inode)) {
  28459. + inode = ERR_PTR(-ENOMEM);
  28460. + goto out;
  28461. + }
  28462. + if (!(inode->i_state & I_NEW))
  28463. + goto out;
  28464. +
  28465. + err = au_xigen_new(inode);
  28466. + if (!err)
  28467. + err = au_iinfo_init(inode);
  28468. + if (!err)
  28469. + inode_inc_iversion(inode);
  28470. + else {
  28471. + iget_failed(inode);
  28472. + inode = ERR_PTR(err);
  28473. + }
  28474. +
  28475. +out:
  28476. + /* never return NULL */
  28477. + AuDebugOn(!inode);
  28478. + AuTraceErrPtr(inode);
  28479. + return inode;
  28480. +}
  28481. +
  28482. +/* lock free root dinfo */
  28483. +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
  28484. +{
  28485. + int err;
  28486. + aufs_bindex_t bindex, bbot;
  28487. + struct path path;
  28488. + struct au_hdentry *hdp;
  28489. + struct au_branch *br;
  28490. + au_br_perm_str_t perm;
  28491. +
  28492. + err = 0;
  28493. + bbot = au_sbbot(sb);
  28494. + bindex = 0;
  28495. + hdp = au_hdentry(au_di(sb->s_root), bindex);
  28496. + for (; !err && bindex <= bbot; bindex++, hdp++) {
  28497. + br = au_sbr(sb, bindex);
  28498. + path.mnt = au_br_mnt(br);
  28499. + path.dentry = hdp->hd_dentry;
  28500. + err = au_seq_path(seq, &path);
  28501. + if (!err) {
  28502. + au_optstr_br_perm(&perm, br->br_perm);
  28503. + seq_printf(seq, "=%s", perm.a);
  28504. + if (bindex != bbot)
  28505. + seq_putc(seq, ':');
  28506. + }
  28507. + }
  28508. + if (unlikely(err || seq_has_overflowed(seq)))
  28509. + err = -E2BIG;
  28510. +
  28511. + return err;
  28512. +}
  28513. +
  28514. +static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
  28515. + const char *append)
  28516. +{
  28517. + char *p;
  28518. +
  28519. + p = fmt;
  28520. + while (*pat != ':')
  28521. + *p++ = *pat++;
  28522. + *p++ = *pat++;
  28523. + strcpy(p, append);
  28524. + AuDebugOn(strlen(fmt) >= len);
  28525. +}
  28526. +
  28527. +static void au_show_wbr_create(struct seq_file *m, int v,
  28528. + struct au_sbinfo *sbinfo)
  28529. +{
  28530. + const char *pat;
  28531. + char fmt[32];
  28532. + struct au_wbr_mfs *mfs;
  28533. +
  28534. + AuRwMustAnyLock(&sbinfo->si_rwsem);
  28535. +
  28536. + seq_puts(m, ",create=");
  28537. + pat = au_optstr_wbr_create(v);
  28538. + mfs = &sbinfo->si_wbr_mfs;
  28539. + switch (v) {
  28540. + case AuWbrCreate_TDP:
  28541. + case AuWbrCreate_RR:
  28542. + case AuWbrCreate_MFS:
  28543. + case AuWbrCreate_PMFS:
  28544. + seq_puts(m, pat);
  28545. + break;
  28546. + case AuWbrCreate_MFSRR:
  28547. + case AuWbrCreate_TDMFS:
  28548. + case AuWbrCreate_PMFSRR:
  28549. + au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
  28550. + seq_printf(m, fmt, mfs->mfsrr_watermark);
  28551. + break;
  28552. + case AuWbrCreate_MFSV:
  28553. + case AuWbrCreate_PMFSV:
  28554. + au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
  28555. + seq_printf(m, fmt,
  28556. + jiffies_to_msecs(mfs->mfs_expire)
  28557. + / MSEC_PER_SEC);
  28558. + break;
  28559. + case AuWbrCreate_MFSRRV:
  28560. + case AuWbrCreate_TDMFSV:
  28561. + case AuWbrCreate_PMFSRRV:
  28562. + au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
  28563. + seq_printf(m, fmt, mfs->mfsrr_watermark,
  28564. + jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
  28565. + break;
  28566. + default:
  28567. + BUG();
  28568. + }
  28569. +}
  28570. +
  28571. +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
  28572. +{
  28573. +#ifdef CONFIG_SYSFS
  28574. + return 0;
  28575. +#else
  28576. + int err;
  28577. + const int len = sizeof(AUFS_XINO_FNAME) - 1;
  28578. + aufs_bindex_t bindex, brid;
  28579. + struct qstr *name;
  28580. + struct file *f;
  28581. + struct dentry *d, *h_root;
  28582. + struct au_branch *br;
  28583. +
  28584. + AuRwMustAnyLock(&sbinfo->si_rwsem);
  28585. +
  28586. + err = 0;
  28587. + f = au_sbi(sb)->si_xib;
  28588. + if (!f)
  28589. + goto out;
  28590. +
  28591. + /* stop printing the default xino path on the first writable branch */
  28592. + h_root = NULL;
  28593. + bindex = au_xi_root(sb, f->f_path.dentry);
  28594. + if (bindex >= 0) {
  28595. + br = au_sbr_sb(sb, bindex);
  28596. + h_root = au_br_dentry(br);
  28597. + }
  28598. +
  28599. + d = f->f_path.dentry;
  28600. + name = &d->d_name;
  28601. + /* safe ->d_parent because the file is unlinked */
  28602. + if (d->d_parent == h_root
  28603. + && name->len == len
  28604. + && !memcmp(name->name, AUFS_XINO_FNAME, len))
  28605. + goto out;
  28606. +
  28607. + seq_puts(seq, ",xino=");
  28608. + err = au_xino_path(seq, f);
  28609. +
  28610. +out:
  28611. + return err;
  28612. +#endif
  28613. +}
  28614. +
  28615. +/* seq_file will re-call me in case of too long string */
  28616. +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
  28617. +{
  28618. + int err;
  28619. + unsigned int mnt_flags, v;
  28620. + struct super_block *sb;
  28621. + struct au_sbinfo *sbinfo;
  28622. +
  28623. +#define AuBool(name, str) do { \
  28624. + v = au_opt_test(mnt_flags, name); \
  28625. + if (v != au_opt_test(AuOpt_Def, name)) \
  28626. + seq_printf(m, ",%s" #str, v ? "" : "no"); \
  28627. +} while (0)
  28628. +
  28629. +#define AuStr(name, str) do { \
  28630. + v = mnt_flags & AuOptMask_##name; \
  28631. + if (v != (AuOpt_Def & AuOptMask_##name)) \
  28632. + seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
  28633. +} while (0)
  28634. +
  28635. +#define AuUInt(name, str, val) do { \
  28636. + if (val != AUFS_##name##_DEF) \
  28637. + seq_printf(m, "," #str "=%u", val); \
  28638. +} while (0)
  28639. +
  28640. + sb = dentry->d_sb;
  28641. + if (sb->s_flags & SB_POSIXACL)
  28642. + seq_puts(m, ",acl");
  28643. +#if 0
  28644. + if (sb->s_flags & SB_I_VERSION)
  28645. + seq_puts(m, ",i_version");
  28646. +#endif
  28647. +
  28648. + /* lock free root dinfo */
  28649. + si_noflush_read_lock(sb);
  28650. + sbinfo = au_sbi(sb);
  28651. + seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
  28652. +
  28653. + mnt_flags = au_mntflags(sb);
  28654. + if (au_opt_test(mnt_flags, XINO)) {
  28655. + err = au_show_xino(m, sb);
  28656. + if (unlikely(err))
  28657. + goto out;
  28658. + } else
  28659. + seq_puts(m, ",noxino");
  28660. +
  28661. + AuBool(TRUNC_XINO, trunc_xino);
  28662. + AuStr(UDBA, udba);
  28663. + AuBool(SHWH, shwh);
  28664. + AuBool(PLINK, plink);
  28665. + AuBool(DIO, dio);
  28666. + AuBool(DIRPERM1, dirperm1);
  28667. +
  28668. + v = sbinfo->si_wbr_create;
  28669. + if (v != AuWbrCreate_Def)
  28670. + au_show_wbr_create(m, v, sbinfo);
  28671. +
  28672. + v = sbinfo->si_wbr_copyup;
  28673. + if (v != AuWbrCopyup_Def)
  28674. + seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
  28675. +
  28676. + v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
  28677. + if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
  28678. + seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
  28679. +
  28680. + AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
  28681. +
  28682. + v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
  28683. + AuUInt(RDCACHE, rdcache, v);
  28684. +
  28685. + AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
  28686. + AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
  28687. +
  28688. + au_fhsm_show(m, sbinfo);
  28689. +
  28690. + AuBool(DIRREN, dirren);
  28691. + AuBool(SUM, sum);
  28692. + /* AuBool(SUM_W, wsum); */
  28693. + AuBool(WARN_PERM, warn_perm);
  28694. + AuBool(VERBOSE, verbose);
  28695. +
  28696. +out:
  28697. + /* be sure to print "br:" last */
  28698. + if (!sysaufs_brs) {
  28699. + seq_puts(m, ",br:");
  28700. + au_show_brs(m, sb);
  28701. + }
  28702. + si_read_unlock(sb);
  28703. + return 0;
  28704. +
  28705. +#undef AuBool
  28706. +#undef AuStr
  28707. +#undef AuUInt
  28708. +}
  28709. +
  28710. +/* ---------------------------------------------------------------------- */
  28711. +
  28712. +/* sum mode which returns the summation for statfs(2) */
  28713. +
  28714. +static u64 au_add_till_max(u64 a, u64 b)
  28715. +{
  28716. + u64 old;
  28717. +
  28718. + old = a;
  28719. + a += b;
  28720. + if (old <= a)
  28721. + return a;
  28722. + return ULLONG_MAX;
  28723. +}
  28724. +
  28725. +static u64 au_mul_till_max(u64 a, long mul)
  28726. +{
  28727. + u64 old;
  28728. +
  28729. + old = a;
  28730. + a *= mul;
  28731. + if (old <= a)
  28732. + return a;
  28733. + return ULLONG_MAX;
  28734. +}
  28735. +
  28736. +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
  28737. +{
  28738. + int err;
  28739. + long bsize, factor;
  28740. + u64 blocks, bfree, bavail, files, ffree;
  28741. + aufs_bindex_t bbot, bindex, i;
  28742. + unsigned char shared;
  28743. + struct path h_path;
  28744. + struct super_block *h_sb;
  28745. +
  28746. + err = 0;
  28747. + bsize = LONG_MAX;
  28748. + files = 0;
  28749. + ffree = 0;
  28750. + blocks = 0;
  28751. + bfree = 0;
  28752. + bavail = 0;
  28753. + bbot = au_sbbot(sb);
  28754. + for (bindex = 0; bindex <= bbot; bindex++) {
  28755. + h_path.mnt = au_sbr_mnt(sb, bindex);
  28756. + h_sb = h_path.mnt->mnt_sb;
  28757. + shared = 0;
  28758. + for (i = 0; !shared && i < bindex; i++)
  28759. + shared = (au_sbr_sb(sb, i) == h_sb);
  28760. + if (shared)
  28761. + continue;
  28762. +
  28763. + /* sb->s_root for NFS is unreliable */
  28764. + h_path.dentry = h_path.mnt->mnt_root;
  28765. + err = vfs_statfs(&h_path, buf);
  28766. + if (unlikely(err))
  28767. + goto out;
  28768. +
  28769. + if (bsize > buf->f_bsize) {
  28770. + /*
  28771. + * we will reduce bsize, so we have to expand blocks
  28772. + * etc. to match them again
  28773. + */
  28774. + factor = (bsize / buf->f_bsize);
  28775. + blocks = au_mul_till_max(blocks, factor);
  28776. + bfree = au_mul_till_max(bfree, factor);
  28777. + bavail = au_mul_till_max(bavail, factor);
  28778. + bsize = buf->f_bsize;
  28779. + }
  28780. +
  28781. + factor = (buf->f_bsize / bsize);
  28782. + blocks = au_add_till_max(blocks,
  28783. + au_mul_till_max(buf->f_blocks, factor));
  28784. + bfree = au_add_till_max(bfree,
  28785. + au_mul_till_max(buf->f_bfree, factor));
  28786. + bavail = au_add_till_max(bavail,
  28787. + au_mul_till_max(buf->f_bavail, factor));
  28788. + files = au_add_till_max(files, buf->f_files);
  28789. + ffree = au_add_till_max(ffree, buf->f_ffree);
  28790. + }
  28791. +
  28792. + buf->f_bsize = bsize;
  28793. + buf->f_blocks = blocks;
  28794. + buf->f_bfree = bfree;
  28795. + buf->f_bavail = bavail;
  28796. + buf->f_files = files;
  28797. + buf->f_ffree = ffree;
  28798. + buf->f_frsize = 0;
  28799. +
  28800. +out:
  28801. + return err;
  28802. +}
  28803. +
  28804. +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
  28805. +{
  28806. + int err;
  28807. + struct path h_path;
  28808. + struct super_block *sb;
  28809. +
  28810. + /* lock free root dinfo */
  28811. + sb = dentry->d_sb;
  28812. + si_noflush_read_lock(sb);
  28813. + if (!au_opt_test(au_mntflags(sb), SUM)) {
  28814. + /* sb->s_root for NFS is unreliable */
  28815. + h_path.mnt = au_sbr_mnt(sb, 0);
  28816. + h_path.dentry = h_path.mnt->mnt_root;
  28817. + err = vfs_statfs(&h_path, buf);
  28818. + } else
  28819. + err = au_statfs_sum(sb, buf);
  28820. + si_read_unlock(sb);
  28821. +
  28822. + if (!err) {
  28823. + buf->f_type = AUFS_SUPER_MAGIC;
  28824. + buf->f_namelen = AUFS_MAX_NAMELEN;
  28825. + memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
  28826. + }
  28827. + /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
  28828. +
  28829. + return err;
  28830. +}
  28831. +
  28832. +/* ---------------------------------------------------------------------- */
  28833. +
  28834. +static int aufs_sync_fs(struct super_block *sb, int wait)
  28835. +{
  28836. + int err, e;
  28837. + aufs_bindex_t bbot, bindex;
  28838. + struct au_branch *br;
  28839. + struct super_block *h_sb;
  28840. +
  28841. + err = 0;
  28842. + si_noflush_read_lock(sb);
  28843. + bbot = au_sbbot(sb);
  28844. + for (bindex = 0; bindex <= bbot; bindex++) {
  28845. + br = au_sbr(sb, bindex);
  28846. + if (!au_br_writable(br->br_perm))
  28847. + continue;
  28848. +
  28849. + h_sb = au_sbr_sb(sb, bindex);
  28850. + e = vfsub_sync_filesystem(h_sb, wait);
  28851. + if (unlikely(e && !err))
  28852. + err = e;
  28853. + /* go on even if an error happens */
  28854. + }
  28855. + si_read_unlock(sb);
  28856. +
  28857. + return err;
  28858. +}
  28859. +
  28860. +/* ---------------------------------------------------------------------- */
  28861. +
  28862. +/* final actions when unmounting a file system */
  28863. +static void aufs_put_super(struct super_block *sb)
  28864. +{
  28865. + struct au_sbinfo *sbinfo;
  28866. +
  28867. + sbinfo = au_sbi(sb);
  28868. + if (sbinfo)
  28869. + kobject_put(&sbinfo->si_kobj);
  28870. +}
  28871. +
  28872. +/* ---------------------------------------------------------------------- */
  28873. +
  28874. +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
  28875. + struct super_block *sb, void *arg)
  28876. +{
  28877. + void *array;
  28878. + unsigned long long n, sz;
  28879. +
  28880. + array = NULL;
  28881. + n = 0;
  28882. + if (!*hint)
  28883. + goto out;
  28884. +
  28885. + if (*hint > ULLONG_MAX / sizeof(array)) {
  28886. + array = ERR_PTR(-EMFILE);
  28887. + pr_err("hint %llu\n", *hint);
  28888. + goto out;
  28889. + }
  28890. +
  28891. + sz = sizeof(array) * *hint;
  28892. + array = kzalloc(sz, GFP_NOFS);
  28893. + if (unlikely(!array))
  28894. + array = vzalloc(sz);
  28895. + if (unlikely(!array)) {
  28896. + array = ERR_PTR(-ENOMEM);
  28897. + goto out;
  28898. + }
  28899. +
  28900. + n = cb(sb, array, *hint, arg);
  28901. + AuDebugOn(n > *hint);
  28902. +
  28903. +out:
  28904. + *hint = n;
  28905. + return array;
  28906. +}
  28907. +
  28908. +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
  28909. + unsigned long long max __maybe_unused,
  28910. + void *arg)
  28911. +{
  28912. + unsigned long long n;
  28913. + struct inode **p, *inode;
  28914. + struct list_head *head;
  28915. +
  28916. + n = 0;
  28917. + p = a;
  28918. + head = arg;
  28919. + spin_lock(&sb->s_inode_list_lock);
  28920. + list_for_each_entry(inode, head, i_sb_list) {
  28921. + if (!au_is_bad_inode(inode)
  28922. + && au_ii(inode)->ii_btop >= 0) {
  28923. + spin_lock(&inode->i_lock);
  28924. + if (atomic_read(&inode->i_count)) {
  28925. + au_igrab(inode);
  28926. + *p++ = inode;
  28927. + n++;
  28928. + AuDebugOn(n > max);
  28929. + }
  28930. + spin_unlock(&inode->i_lock);
  28931. + }
  28932. + }
  28933. + spin_unlock(&sb->s_inode_list_lock);
  28934. +
  28935. + return n;
  28936. +}
  28937. +
  28938. +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
  28939. +{
  28940. + struct au_sbinfo *sbi;
  28941. +
  28942. + sbi = au_sbi(sb);
  28943. + *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
  28944. + return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
  28945. +}
  28946. +
  28947. +void au_iarray_free(struct inode **a, unsigned long long max)
  28948. +{
  28949. + unsigned long long ull;
  28950. +
  28951. + for (ull = 0; ull < max; ull++)
  28952. + iput(a[ull]);
  28953. + kvfree(a);
  28954. +}
  28955. +
  28956. +/* ---------------------------------------------------------------------- */
  28957. +
  28958. +/*
  28959. + * refresh dentry and inode at remount time.
  28960. + */
  28961. +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
  28962. +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
  28963. + struct dentry *parent)
  28964. +{
  28965. + int err;
  28966. +
  28967. + di_write_lock_child(dentry);
  28968. + di_read_lock_parent(parent, AuLock_IR);
  28969. + err = au_refresh_dentry(dentry, parent);
  28970. + if (!err && dir_flags)
  28971. + au_hn_reset(d_inode(dentry), dir_flags);
  28972. + di_read_unlock(parent, AuLock_IR);
  28973. + di_write_unlock(dentry);
  28974. +
  28975. + return err;
  28976. +}
  28977. +
  28978. +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
  28979. + struct au_sbinfo *sbinfo,
  28980. + const unsigned int dir_flags, unsigned int do_idop)
  28981. +{
  28982. + int err;
  28983. + struct dentry *parent;
  28984. +
  28985. + err = 0;
  28986. + parent = dget_parent(dentry);
  28987. + if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
  28988. + if (d_really_is_positive(dentry)) {
  28989. + if (!d_is_dir(dentry))
  28990. + err = au_do_refresh(dentry, /*dir_flags*/0,
  28991. + parent);
  28992. + else {
  28993. + err = au_do_refresh(dentry, dir_flags, parent);
  28994. + if (unlikely(err))
  28995. + au_fset_si(sbinfo, FAILED_REFRESH_DIR);
  28996. + }
  28997. + } else
  28998. + err = au_do_refresh(dentry, /*dir_flags*/0, parent);
  28999. + AuDbgDentry(dentry);
  29000. + }
  29001. + dput(parent);
  29002. +
  29003. + if (!err) {
  29004. + if (do_idop)
  29005. + au_refresh_dop(dentry, /*force_reval*/0);
  29006. + } else
  29007. + au_refresh_dop(dentry, /*force_reval*/1);
  29008. +
  29009. + AuTraceErr(err);
  29010. + return err;
  29011. +}
  29012. +
  29013. +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
  29014. +{
  29015. + int err, i, j, ndentry, e;
  29016. + unsigned int sigen;
  29017. + struct au_dcsub_pages dpages;
  29018. + struct au_dpage *dpage;
  29019. + struct dentry **dentries, *d;
  29020. + struct au_sbinfo *sbinfo;
  29021. + struct dentry *root = sb->s_root;
  29022. + const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
  29023. +
  29024. + if (do_idop)
  29025. + au_refresh_dop(root, /*force_reval*/0);
  29026. +
  29027. + err = au_dpages_init(&dpages, GFP_NOFS);
  29028. + if (unlikely(err))
  29029. + goto out;
  29030. + err = au_dcsub_pages(&dpages, root, NULL, NULL);
  29031. + if (unlikely(err))
  29032. + goto out_dpages;
  29033. +
  29034. + sigen = au_sigen(sb);
  29035. + sbinfo = au_sbi(sb);
  29036. + for (i = 0; i < dpages.ndpage; i++) {
  29037. + dpage = dpages.dpages + i;
  29038. + dentries = dpage->dentries;
  29039. + ndentry = dpage->ndentry;
  29040. + for (j = 0; j < ndentry; j++) {
  29041. + d = dentries[j];
  29042. + e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
  29043. + do_idop);
  29044. + if (unlikely(e && !err))
  29045. + err = e;
  29046. + /* go on even err */
  29047. + }
  29048. + }
  29049. +
  29050. +out_dpages:
  29051. + au_dpages_free(&dpages);
  29052. +out:
  29053. + return err;
  29054. +}
  29055. +
  29056. +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
  29057. +{
  29058. + int err, e;
  29059. + unsigned int sigen;
  29060. + unsigned long long max, ull;
  29061. + struct inode *inode, **array;
  29062. +
  29063. + array = au_iarray_alloc(sb, &max);
  29064. + err = PTR_ERR(array);
  29065. + if (IS_ERR(array))
  29066. + goto out;
  29067. +
  29068. + err = 0;
  29069. + sigen = au_sigen(sb);
  29070. + for (ull = 0; ull < max; ull++) {
  29071. + inode = array[ull];
  29072. + if (unlikely(!inode))
  29073. + break;
  29074. +
  29075. + e = 0;
  29076. + ii_write_lock_child(inode);
  29077. + if (au_iigen(inode, NULL) != sigen) {
  29078. + e = au_refresh_hinode_self(inode);
  29079. + if (unlikely(e)) {
  29080. + au_refresh_iop(inode, /*force_getattr*/1);
  29081. + pr_err("error %d, i%lu\n", e, inode->i_ino);
  29082. + if (!err)
  29083. + err = e;
  29084. + /* go on even if err */
  29085. + }
  29086. + }
  29087. + if (!e && do_idop)
  29088. + au_refresh_iop(inode, /*force_getattr*/0);
  29089. + ii_write_unlock(inode);
  29090. + }
  29091. +
  29092. + au_iarray_free(array, max);
  29093. +
  29094. +out:
  29095. + return err;
  29096. +}
  29097. +
  29098. +static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
  29099. +{
  29100. + int err, e;
  29101. + unsigned int udba;
  29102. + aufs_bindex_t bindex, bbot;
  29103. + struct dentry *root;
  29104. + struct inode *inode;
  29105. + struct au_branch *br;
  29106. + struct au_sbinfo *sbi;
  29107. +
  29108. + au_sigen_inc(sb);
  29109. + sbi = au_sbi(sb);
  29110. + au_fclr_si(sbi, FAILED_REFRESH_DIR);
  29111. +
  29112. + root = sb->s_root;
  29113. + DiMustNoWaiters(root);
  29114. + inode = d_inode(root);
  29115. + IiMustNoWaiters(inode);
  29116. +
  29117. + udba = au_opt_udba(sb);
  29118. + bbot = au_sbbot(sb);
  29119. + for (bindex = 0; bindex <= bbot; bindex++) {
  29120. + br = au_sbr(sb, bindex);
  29121. + err = au_hnotify_reset_br(udba, br, br->br_perm);
  29122. + if (unlikely(err))
  29123. + AuIOErr("hnotify failed on br %d, %d, ignored\n",
  29124. + bindex, err);
  29125. + /* go on even if err */
  29126. + }
  29127. + au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
  29128. +
  29129. + if (do_idop) {
  29130. + if (au_ftest_si(sbi, NO_DREVAL)) {
  29131. + AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
  29132. + sb->s_d_op = &aufs_dop_noreval;
  29133. + AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
  29134. + sbi->si_iop_array = aufs_iop_nogetattr;
  29135. + } else {
  29136. + AuDebugOn(sb->s_d_op == &aufs_dop);
  29137. + sb->s_d_op = &aufs_dop;
  29138. + AuDebugOn(sbi->si_iop_array == aufs_iop);
  29139. + sbi->si_iop_array = aufs_iop;
  29140. + }
  29141. + pr_info("reset to %ps and %ps\n",
  29142. + sb->s_d_op, sbi->si_iop_array);
  29143. + }
  29144. +
  29145. + di_write_unlock(root);
  29146. + err = au_refresh_d(sb, do_idop);
  29147. + e = au_refresh_i(sb, do_idop);
  29148. + if (unlikely(e && !err))
  29149. + err = e;
  29150. + /* aufs_write_lock() calls ..._child() */
  29151. + di_write_lock_child(root);
  29152. +
  29153. + au_cpup_attr_all(inode, /*force*/1);
  29154. +
  29155. + if (unlikely(err))
  29156. + AuIOErr("refresh failed, ignored, %d\n", err);
  29157. +}
  29158. +
  29159. +/* stop extra interpretation of errno in mount(8), and strange error messages */
  29160. +static int cvt_err(int err)
  29161. +{
  29162. + AuTraceErr(err);
  29163. +
  29164. + switch (err) {
  29165. + case -ENOENT:
  29166. + case -ENOTDIR:
  29167. + case -EEXIST:
  29168. + case -EIO:
  29169. + err = -EINVAL;
  29170. + }
  29171. + return err;
  29172. +}
  29173. +
  29174. +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
  29175. +{
  29176. + int err, do_dx;
  29177. + unsigned int mntflags;
  29178. + struct au_opts opts = {
  29179. + .opt = NULL
  29180. + };
  29181. + struct dentry *root;
  29182. + struct inode *inode;
  29183. + struct au_sbinfo *sbinfo;
  29184. +
  29185. + err = 0;
  29186. + root = sb->s_root;
  29187. + if (!data || !*data) {
  29188. + err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  29189. + if (!err) {
  29190. + di_write_lock_child(root);
  29191. + err = au_opts_verify(sb, *flags, /*pending*/0);
  29192. + aufs_write_unlock(root);
  29193. + }
  29194. + goto out;
  29195. + }
  29196. +
  29197. + err = -ENOMEM;
  29198. + opts.opt = (void *)__get_free_page(GFP_NOFS);
  29199. + if (unlikely(!opts.opt))
  29200. + goto out;
  29201. + opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
  29202. + opts.flags = AuOpts_REMOUNT;
  29203. + opts.sb_flags = *flags;
  29204. +
  29205. + /* parse it before aufs lock */
  29206. + err = au_opts_parse(sb, data, &opts);
  29207. + if (unlikely(err))
  29208. + goto out_opts;
  29209. +
  29210. + sbinfo = au_sbi(sb);
  29211. + inode = d_inode(root);
  29212. + inode_lock(inode);
  29213. + err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  29214. + if (unlikely(err))
  29215. + goto out_mtx;
  29216. + di_write_lock_child(root);
  29217. +
  29218. + /* au_opts_remount() may return an error */
  29219. + err = au_opts_remount(sb, &opts);
  29220. + au_opts_free(&opts);
  29221. +
  29222. + if (au_ftest_opts(opts.flags, REFRESH))
  29223. + au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
  29224. +
  29225. + if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
  29226. + mntflags = au_mntflags(sb);
  29227. + do_dx = !!au_opt_test(mntflags, DIO);
  29228. + au_dy_arefresh(do_dx);
  29229. + }
  29230. +
  29231. + au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
  29232. + aufs_write_unlock(root);
  29233. +
  29234. +out_mtx:
  29235. + inode_unlock(inode);
  29236. +out_opts:
  29237. + free_page((unsigned long)opts.opt);
  29238. +out:
  29239. + err = cvt_err(err);
  29240. + AuTraceErr(err);
  29241. + return err;
  29242. +}
  29243. +
  29244. +static const struct super_operations aufs_sop = {
  29245. + .alloc_inode = aufs_alloc_inode,
  29246. + .destroy_inode = aufs_destroy_inode,
  29247. + /* always deleting, no clearing */
  29248. + .drop_inode = generic_delete_inode,
  29249. + .show_options = aufs_show_options,
  29250. + .statfs = aufs_statfs,
  29251. + .put_super = aufs_put_super,
  29252. + .sync_fs = aufs_sync_fs,
  29253. + .remount_fs = aufs_remount_fs
  29254. +};
  29255. +
  29256. +/* ---------------------------------------------------------------------- */
  29257. +
  29258. +static int alloc_root(struct super_block *sb)
  29259. +{
  29260. + int err;
  29261. + struct inode *inode;
  29262. + struct dentry *root;
  29263. +
  29264. + err = -ENOMEM;
  29265. + inode = au_iget_locked(sb, AUFS_ROOT_INO);
  29266. + err = PTR_ERR(inode);
  29267. + if (IS_ERR(inode))
  29268. + goto out;
  29269. +
  29270. + inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
  29271. + inode->i_fop = &aufs_dir_fop;
  29272. + inode->i_mode = S_IFDIR;
  29273. + set_nlink(inode, 2);
  29274. + unlock_new_inode(inode);
  29275. +
  29276. + root = d_make_root(inode);
  29277. + if (unlikely(!root))
  29278. + goto out;
  29279. + err = PTR_ERR(root);
  29280. + if (IS_ERR(root))
  29281. + goto out;
  29282. +
  29283. + err = au_di_init(root);
  29284. + if (!err) {
  29285. + sb->s_root = root;
  29286. + return 0; /* success */
  29287. + }
  29288. + dput(root);
  29289. +
  29290. +out:
  29291. + return err;
  29292. +}
  29293. +
  29294. +static int aufs_fill_super(struct super_block *sb, void *raw_data,
  29295. + int silent __maybe_unused)
  29296. +{
  29297. + int err;
  29298. + struct au_opts opts = {
  29299. + .opt = NULL
  29300. + };
  29301. + struct au_sbinfo *sbinfo;
  29302. + struct dentry *root;
  29303. + struct inode *inode;
  29304. + char *arg = raw_data;
  29305. +
  29306. + if (unlikely(!arg || !*arg)) {
  29307. + err = -EINVAL;
  29308. + pr_err("no arg\n");
  29309. + goto out;
  29310. + }
  29311. +
  29312. + err = -ENOMEM;
  29313. + opts.opt = (void *)__get_free_page(GFP_NOFS);
  29314. + if (unlikely(!opts.opt))
  29315. + goto out;
  29316. + opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
  29317. + opts.sb_flags = sb->s_flags;
  29318. +
  29319. + err = au_si_alloc(sb);
  29320. + if (unlikely(err))
  29321. + goto out_opts;
  29322. + sbinfo = au_sbi(sb);
  29323. +
  29324. + /* all timestamps always follow the ones on the branch */
  29325. + sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
  29326. + sb->s_flags |= SB_I_VERSION; /* do we really need this? */
  29327. + sb->s_op = &aufs_sop;
  29328. + sb->s_d_op = &aufs_dop;
  29329. + sb->s_magic = AUFS_SUPER_MAGIC;
  29330. + sb->s_maxbytes = 0;
  29331. + sb->s_stack_depth = 1;
  29332. + au_export_init(sb);
  29333. + au_xattr_init(sb);
  29334. +
  29335. + err = alloc_root(sb);
  29336. + if (unlikely(err)) {
  29337. + si_write_unlock(sb);
  29338. + goto out_info;
  29339. + }
  29340. + root = sb->s_root;
  29341. + inode = d_inode(root);
  29342. +
  29343. + /*
  29344. + * actually we can parse options regardless aufs lock here.
  29345. + * but at remount time, parsing must be done before aufs lock.
  29346. + * so we follow the same rule.
  29347. + */
  29348. + ii_write_lock_parent(inode);
  29349. + aufs_write_unlock(root);
  29350. + err = au_opts_parse(sb, arg, &opts);
  29351. + if (unlikely(err))
  29352. + goto out_root;
  29353. +
  29354. + /* lock vfs_inode first, then aufs. */
  29355. + inode_lock(inode);
  29356. + aufs_write_lock(root);
  29357. + err = au_opts_mount(sb, &opts);
  29358. + au_opts_free(&opts);
  29359. + if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
  29360. + sb->s_d_op = &aufs_dop_noreval;
  29361. + pr_info("%ps\n", sb->s_d_op);
  29362. + au_refresh_dop(root, /*force_reval*/0);
  29363. + sbinfo->si_iop_array = aufs_iop_nogetattr;
  29364. + au_refresh_iop(inode, /*force_getattr*/0);
  29365. + }
  29366. + aufs_write_unlock(root);
  29367. + inode_unlock(inode);
  29368. + if (!err)
  29369. + goto out_opts; /* success */
  29370. +
  29371. +out_root:
  29372. + dput(root);
  29373. + sb->s_root = NULL;
  29374. +out_info:
  29375. + kobject_put(&sbinfo->si_kobj);
  29376. + sb->s_fs_info = NULL;
  29377. +out_opts:
  29378. + free_page((unsigned long)opts.opt);
  29379. +out:
  29380. + AuTraceErr(err);
  29381. + err = cvt_err(err);
  29382. + AuTraceErr(err);
  29383. + return err;
  29384. +}
  29385. +
  29386. +/* ---------------------------------------------------------------------- */
  29387. +
  29388. +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
  29389. + const char *dev_name __maybe_unused,
  29390. + void *raw_data)
  29391. +{
  29392. + struct dentry *root;
  29393. +
  29394. + /* all timestamps always follow the ones on the branch */
  29395. + /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
  29396. + root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
  29397. + if (IS_ERR(root))
  29398. + goto out;
  29399. +
  29400. + au_sbilist_add(root->d_sb);
  29401. +
  29402. +out:
  29403. + return root;
  29404. +}
  29405. +
  29406. +static void aufs_kill_sb(struct super_block *sb)
  29407. +{
  29408. + struct au_sbinfo *sbinfo;
  29409. +
  29410. + sbinfo = au_sbi(sb);
  29411. + if (sbinfo) {
  29412. + au_sbilist_del(sb);
  29413. + aufs_write_lock(sb->s_root);
  29414. + au_fhsm_fin(sb);
  29415. + if (sbinfo->si_wbr_create_ops->fin)
  29416. + sbinfo->si_wbr_create_ops->fin(sb);
  29417. + if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
  29418. + au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
  29419. + au_remount_refresh(sb, /*do_idop*/0);
  29420. + }
  29421. + if (au_opt_test(sbinfo->si_mntflags, PLINK))
  29422. + au_plink_put(sb, /*verbose*/1);
  29423. + au_xino_clr(sb);
  29424. + au_dr_opt_flush(sb);
  29425. + sbinfo->si_sb = NULL;
  29426. + aufs_write_unlock(sb->s_root);
  29427. + au_nwt_flush(&sbinfo->si_nowait);
  29428. + }
  29429. + kill_anon_super(sb);
  29430. +}
  29431. +
  29432. +struct file_system_type aufs_fs_type = {
  29433. + .name = AUFS_FSTYPE,
  29434. + /* a race between rename and others */
  29435. + .fs_flags = FS_RENAME_DOES_D_MOVE,
  29436. + .mount = aufs_mount,
  29437. + .kill_sb = aufs_kill_sb,
  29438. + /* no need to __module_get() and module_put(). */
  29439. + .owner = THIS_MODULE,
  29440. +};
  29441. diff -Naur null/fs/aufs/super.h linux-5.1/fs/aufs/super.h
  29442. --- /dev/null
  29443. +++ linux-5.1/fs/aufs/super.h 2019-05-22 11:23:07.443140904 +0200
  29444. @@ -0,0 +1,589 @@
  29445. +/* SPDX-License-Identifier: GPL-2.0 */
  29446. +/*
  29447. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  29448. + *
  29449. + * This program, aufs is free software; you can redistribute it and/or modify
  29450. + * it under the terms of the GNU General Public License as published by
  29451. + * the Free Software Foundation; either version 2 of the License, or
  29452. + * (at your option) any later version.
  29453. + *
  29454. + * This program is distributed in the hope that it will be useful,
  29455. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29456. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29457. + * GNU General Public License for more details.
  29458. + *
  29459. + * You should have received a copy of the GNU General Public License
  29460. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  29461. + */
  29462. +
  29463. +/*
  29464. + * super_block operations
  29465. + */
  29466. +
  29467. +#ifndef __AUFS_SUPER_H__
  29468. +#define __AUFS_SUPER_H__
  29469. +
  29470. +#ifdef __KERNEL__
  29471. +
  29472. +#include <linux/fs.h>
  29473. +#include <linux/kobject.h>
  29474. +#include "hbl.h"
  29475. +#include "lcnt.h"
  29476. +#include "rwsem.h"
  29477. +#include "wkq.h"
  29478. +
  29479. +/* policies to select one among multiple writable branches */
  29480. +struct au_wbr_copyup_operations {
  29481. + int (*copyup)(struct dentry *dentry);
  29482. +};
  29483. +
  29484. +#define AuWbr_DIR 1 /* target is a dir */
  29485. +#define AuWbr_PARENT (1 << 1) /* always require a parent */
  29486. +
  29487. +#define au_ftest_wbr(flags, name) ((flags) & AuWbr_##name)
  29488. +#define au_fset_wbr(flags, name) { (flags) |= AuWbr_##name; }
  29489. +#define au_fclr_wbr(flags, name) { (flags) &= ~AuWbr_##name; }
  29490. +
  29491. +struct au_wbr_create_operations {
  29492. + int (*create)(struct dentry *dentry, unsigned int flags);
  29493. + int (*init)(struct super_block *sb);
  29494. + int (*fin)(struct super_block *sb);
  29495. +};
  29496. +
  29497. +struct au_wbr_mfs {
  29498. + struct mutex mfs_lock; /* protect this structure */
  29499. + unsigned long mfs_jiffy;
  29500. + unsigned long mfs_expire;
  29501. + aufs_bindex_t mfs_bindex;
  29502. +
  29503. + unsigned long long mfsrr_bytes;
  29504. + unsigned long long mfsrr_watermark;
  29505. +};
  29506. +
  29507. +#define AuPlink_NHASH 100
  29508. +static inline int au_plink_hash(ino_t ino)
  29509. +{
  29510. + return ino % AuPlink_NHASH;
  29511. +}
  29512. +
  29513. +/* File-based Hierarchical Storage Management */
  29514. +struct au_fhsm {
  29515. +#ifdef CONFIG_AUFS_FHSM
  29516. + /* allow only one process who can receive the notification */
  29517. + spinlock_t fhsm_spin;
  29518. + pid_t fhsm_pid;
  29519. + wait_queue_head_t fhsm_wqh;
  29520. + atomic_t fhsm_readable;
  29521. +
  29522. + /* these are protected by si_rwsem */
  29523. + unsigned long fhsm_expire;
  29524. + aufs_bindex_t fhsm_bottom;
  29525. +#endif
  29526. +};
  29527. +
  29528. +struct au_branch;
  29529. +struct au_sbinfo {
  29530. + /* nowait tasks in the system-wide workqueue */
  29531. + struct au_nowait_tasks si_nowait;
  29532. +
  29533. + /*
  29534. + * tried sb->s_umount, but failed due to the dependency between i_mutex.
  29535. + * rwsem for au_sbinfo is necessary.
  29536. + */
  29537. + struct au_rwsem si_rwsem;
  29538. +
  29539. + /*
  29540. + * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
  29541. + * remount.
  29542. + */
  29543. + au_lcnt_t si_ninodes, si_nfiles;
  29544. +
  29545. + /* branch management */
  29546. + unsigned int si_generation;
  29547. +
  29548. + /* see AuSi_ flags */
  29549. + unsigned char au_si_status;
  29550. +
  29551. + aufs_bindex_t si_bbot;
  29552. +
  29553. + /* dirty trick to keep br_id plus */
  29554. + unsigned int si_last_br_id :
  29555. + sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
  29556. + struct au_branch **si_branch;
  29557. +
  29558. + /* policy to select a writable branch */
  29559. + unsigned char si_wbr_copyup;
  29560. + unsigned char si_wbr_create;
  29561. + struct au_wbr_copyup_operations *si_wbr_copyup_ops;
  29562. + struct au_wbr_create_operations *si_wbr_create_ops;
  29563. +
  29564. + /* round robin */
  29565. + atomic_t si_wbr_rr_next;
  29566. +
  29567. + /* most free space */
  29568. + struct au_wbr_mfs si_wbr_mfs;
  29569. +
  29570. + /* File-based Hierarchical Storage Management */
  29571. + struct au_fhsm si_fhsm;
  29572. +
  29573. + /* mount flags */
  29574. + /* include/asm-ia64/siginfo.h defines a macro named si_flags */
  29575. + unsigned int si_mntflags;
  29576. +
  29577. + /* external inode number (bitmap and translation table) */
  29578. + vfs_readf_t si_xread;
  29579. + vfs_writef_t si_xwrite;
  29580. + loff_t si_ximaxent; /* max entries in a xino */
  29581. +
  29582. + struct file *si_xib;
  29583. + struct mutex si_xib_mtx; /* protect xib members */
  29584. + unsigned long *si_xib_buf;
  29585. + unsigned long si_xib_last_pindex;
  29586. + int si_xib_next_bit;
  29587. +
  29588. + unsigned long si_xino_jiffy;
  29589. + unsigned long si_xino_expire;
  29590. + /* reserved for future use */
  29591. + /* unsigned long long si_xib_limit; */ /* Max xib file size */
  29592. +
  29593. +#ifdef CONFIG_AUFS_EXPORT
  29594. + /* i_generation */
  29595. + /* todo: make xigen file an array to support many inode numbers */
  29596. + struct file *si_xigen;
  29597. + atomic_t si_xigen_next;
  29598. +#endif
  29599. +
  29600. + /* dirty trick to support atomic_open */
  29601. + struct hlist_bl_head si_aopen;
  29602. +
  29603. + /* vdir parameters */
  29604. + unsigned long si_rdcache; /* max cache time in jiffies */
  29605. + unsigned int si_rdblk; /* deblk size */
  29606. + unsigned int si_rdhash; /* hash size */
  29607. +
  29608. + /*
  29609. + * If the number of whiteouts are larger than si_dirwh, leave all of
  29610. + * them after au_whtmp_ren to reduce the cost of rmdir(2).
  29611. + * future fsck.aufs or kernel thread will remove them later.
  29612. + * Otherwise, remove all whiteouts and the dir in rmdir(2).
  29613. + */
  29614. + unsigned int si_dirwh;
  29615. +
  29616. + /* pseudo_link list */
  29617. + struct hlist_bl_head si_plink[AuPlink_NHASH];
  29618. + wait_queue_head_t si_plink_wq;
  29619. + spinlock_t si_plink_maint_lock;
  29620. + pid_t si_plink_maint_pid;
  29621. +
  29622. + /* file list */
  29623. + struct hlist_bl_head si_files;
  29624. +
  29625. + /* with/without getattr, brother of sb->s_d_op */
  29626. + struct inode_operations *si_iop_array;
  29627. +
  29628. + /*
  29629. + * sysfs and lifetime management.
  29630. + * this is not a small structure and it may be a waste of memory in case
  29631. + * of sysfs is disabled, particularly when many aufs-es are mounted.
  29632. + * but using sysfs is majority.
  29633. + */
  29634. + struct kobject si_kobj;
  29635. +#ifdef CONFIG_DEBUG_FS
  29636. + struct dentry *si_dbgaufs;
  29637. + struct dentry *si_dbgaufs_plink;
  29638. + struct dentry *si_dbgaufs_xib;
  29639. +#ifdef CONFIG_AUFS_EXPORT
  29640. + struct dentry *si_dbgaufs_xigen;
  29641. +#endif
  29642. +#endif
  29643. +
  29644. +#ifdef CONFIG_AUFS_SBILIST
  29645. + struct hlist_bl_node si_list;
  29646. +#endif
  29647. +
  29648. + /* dirty, necessary for unmounting, sysfs and sysrq */
  29649. + struct super_block *si_sb;
  29650. +};
  29651. +
  29652. +/* sbinfo status flags */
  29653. +/*
  29654. + * set true when refresh_dirs() failed at remount time.
  29655. + * then try refreshing dirs at access time again.
  29656. + * if it is false, refreshing dirs at access time is unnecessary
  29657. + */
  29658. +#define AuSi_FAILED_REFRESH_DIR 1
  29659. +#define AuSi_FHSM (1 << 1) /* fhsm is active now */
  29660. +#define AuSi_NO_DREVAL (1 << 2) /* disable all d_revalidate */
  29661. +
  29662. +#ifndef CONFIG_AUFS_FHSM
  29663. +#undef AuSi_FHSM
  29664. +#define AuSi_FHSM 0
  29665. +#endif
  29666. +
  29667. +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
  29668. + unsigned int flag)
  29669. +{
  29670. + AuRwMustAnyLock(&sbi->si_rwsem);
  29671. + return sbi->au_si_status & flag;
  29672. +}
  29673. +#define au_ftest_si(sbinfo, name) au_do_ftest_si(sbinfo, AuSi_##name)
  29674. +#define au_fset_si(sbinfo, name) do { \
  29675. + AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
  29676. + (sbinfo)->au_si_status |= AuSi_##name; \
  29677. +} while (0)
  29678. +#define au_fclr_si(sbinfo, name) do { \
  29679. + AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
  29680. + (sbinfo)->au_si_status &= ~AuSi_##name; \
  29681. +} while (0)
  29682. +
  29683. +/* ---------------------------------------------------------------------- */
  29684. +
  29685. +/* policy to select one among writable branches */
  29686. +#define AuWbrCopyup(sbinfo, ...) \
  29687. + ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
  29688. +#define AuWbrCreate(sbinfo, ...) \
  29689. + ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
  29690. +
  29691. +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
  29692. +#define AuLock_DW 1 /* write-lock dentry */
  29693. +#define AuLock_IR (1 << 1) /* read-lock inode */
  29694. +#define AuLock_IW (1 << 2) /* write-lock inode */
  29695. +#define AuLock_FLUSH (1 << 3) /* wait for 'nowait' tasks */
  29696. +#define AuLock_DIRS (1 << 4) /* target is a pair of dirs */
  29697. + /* except RENAME_EXCHANGE */
  29698. +#define AuLock_NOPLM (1 << 5) /* return err in plm mode */
  29699. +#define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
  29700. +#define AuLock_GEN (1 << 7) /* test digen/iigen */
  29701. +#define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
  29702. +#define au_fset_lock(flags, name) \
  29703. + do { (flags) |= AuLock_##name; } while (0)
  29704. +#define au_fclr_lock(flags, name) \
  29705. + do { (flags) &= ~AuLock_##name; } while (0)
  29706. +
  29707. +/* ---------------------------------------------------------------------- */
  29708. +
  29709. +/* super.c */
  29710. +extern struct file_system_type aufs_fs_type;
  29711. +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
  29712. +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
  29713. + unsigned long long max, void *arg);
  29714. +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
  29715. + struct super_block *sb, void *arg);
  29716. +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
  29717. +void au_iarray_free(struct inode **a, unsigned long long max);
  29718. +
  29719. +/* sbinfo.c */
  29720. +void au_si_free(struct kobject *kobj);
  29721. +int au_si_alloc(struct super_block *sb);
  29722. +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
  29723. +
  29724. +unsigned int au_sigen_inc(struct super_block *sb);
  29725. +aufs_bindex_t au_new_br_id(struct super_block *sb);
  29726. +
  29727. +int si_read_lock(struct super_block *sb, int flags);
  29728. +int si_write_lock(struct super_block *sb, int flags);
  29729. +int aufs_read_lock(struct dentry *dentry, int flags);
  29730. +void aufs_read_unlock(struct dentry *dentry, int flags);
  29731. +void aufs_write_lock(struct dentry *dentry);
  29732. +void aufs_write_unlock(struct dentry *dentry);
  29733. +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
  29734. +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
  29735. +
  29736. +/* wbr_policy.c */
  29737. +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
  29738. +extern struct au_wbr_create_operations au_wbr_create_ops[];
  29739. +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
  29740. +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
  29741. +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
  29742. +
  29743. +/* mvdown.c */
  29744. +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
  29745. +
  29746. +#ifdef CONFIG_AUFS_FHSM
  29747. +/* fhsm.c */
  29748. +
  29749. +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
  29750. +{
  29751. + pid_t pid;
  29752. +
  29753. + spin_lock(&fhsm->fhsm_spin);
  29754. + pid = fhsm->fhsm_pid;
  29755. + spin_unlock(&fhsm->fhsm_spin);
  29756. +
  29757. + return pid;
  29758. +}
  29759. +
  29760. +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
  29761. +void au_fhsm_wrote_all(struct super_block *sb, int force);
  29762. +int au_fhsm_fd(struct super_block *sb, int oflags);
  29763. +int au_fhsm_br_alloc(struct au_branch *br);
  29764. +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
  29765. +void au_fhsm_fin(struct super_block *sb);
  29766. +void au_fhsm_init(struct au_sbinfo *sbinfo);
  29767. +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
  29768. +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
  29769. +#else
  29770. +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
  29771. + int force)
  29772. +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
  29773. +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
  29774. +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
  29775. +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
  29776. +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
  29777. +AuStubVoid(au_fhsm_fin, struct super_block *sb)
  29778. +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
  29779. +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
  29780. +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
  29781. +#endif
  29782. +
  29783. +/* ---------------------------------------------------------------------- */
  29784. +
  29785. +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
  29786. +{
  29787. + return sb->s_fs_info;
  29788. +}
  29789. +
  29790. +/* ---------------------------------------------------------------------- */
  29791. +
  29792. +#ifdef CONFIG_AUFS_EXPORT
  29793. +int au_test_nfsd(void);
  29794. +void au_export_init(struct super_block *sb);
  29795. +void au_xigen_inc(struct inode *inode);
  29796. +int au_xigen_new(struct inode *inode);
  29797. +int au_xigen_set(struct super_block *sb, struct path *path);
  29798. +void au_xigen_clr(struct super_block *sb);
  29799. +
  29800. +static inline int au_busy_or_stale(void)
  29801. +{
  29802. + if (!au_test_nfsd())
  29803. + return -EBUSY;
  29804. + return -ESTALE;
  29805. +}
  29806. +#else
  29807. +AuStubInt0(au_test_nfsd, void)
  29808. +AuStubVoid(au_export_init, struct super_block *sb)
  29809. +AuStubVoid(au_xigen_inc, struct inode *inode)
  29810. +AuStubInt0(au_xigen_new, struct inode *inode)
  29811. +AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
  29812. +AuStubVoid(au_xigen_clr, struct super_block *sb)
  29813. +AuStub(int, au_busy_or_stale, return -EBUSY, void)
  29814. +#endif /* CONFIG_AUFS_EXPORT */
  29815. +
  29816. +/* ---------------------------------------------------------------------- */
  29817. +
  29818. +#ifdef CONFIG_AUFS_SBILIST
  29819. +/* module.c */
  29820. +extern struct hlist_bl_head au_sbilist;
  29821. +
  29822. +static inline void au_sbilist_init(void)
  29823. +{
  29824. + INIT_HLIST_BL_HEAD(&au_sbilist);
  29825. +}
  29826. +
  29827. +static inline void au_sbilist_add(struct super_block *sb)
  29828. +{
  29829. + au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
  29830. +}
  29831. +
  29832. +static inline void au_sbilist_del(struct super_block *sb)
  29833. +{
  29834. + au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
  29835. +}
  29836. +
  29837. +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
  29838. +static inline void au_sbilist_lock(void)
  29839. +{
  29840. + hlist_bl_lock(&au_sbilist);
  29841. +}
  29842. +
  29843. +static inline void au_sbilist_unlock(void)
  29844. +{
  29845. + hlist_bl_unlock(&au_sbilist);
  29846. +}
  29847. +#define AuGFP_SBILIST GFP_ATOMIC
  29848. +#else
  29849. +AuStubVoid(au_sbilist_lock, void)
  29850. +AuStubVoid(au_sbilist_unlock, void)
  29851. +#define AuGFP_SBILIST GFP_NOFS
  29852. +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
  29853. +#else
  29854. +AuStubVoid(au_sbilist_init, void)
  29855. +AuStubVoid(au_sbilist_add, struct super_block *sb)
  29856. +AuStubVoid(au_sbilist_del, struct super_block *sb)
  29857. +AuStubVoid(au_sbilist_lock, void)
  29858. +AuStubVoid(au_sbilist_unlock, void)
  29859. +#define AuGFP_SBILIST GFP_NOFS
  29860. +#endif
  29861. +
  29862. +/* ---------------------------------------------------------------------- */
  29863. +
  29864. +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
  29865. +{
  29866. + /*
  29867. + * This function is a dynamic '__init' function actually,
  29868. + * so the tiny check for si_rwsem is unnecessary.
  29869. + */
  29870. + /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
  29871. +#ifdef CONFIG_DEBUG_FS
  29872. + sbinfo->si_dbgaufs = NULL;
  29873. + sbinfo->si_dbgaufs_plink = NULL;
  29874. + sbinfo->si_dbgaufs_xib = NULL;
  29875. +#ifdef CONFIG_AUFS_EXPORT
  29876. + sbinfo->si_dbgaufs_xigen = NULL;
  29877. +#endif
  29878. +#endif
  29879. +}
  29880. +
  29881. +/* ---------------------------------------------------------------------- */
  29882. +
  29883. +/* current->atomic_flags */
  29884. +/* this value should never corrupt the ones defined in linux/sched.h */
  29885. +#define PFA_AUFS 0x10
  29886. +
  29887. +TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
  29888. +TASK_PFA_SET(AUFS, aufs) /* task_set_aufs */
  29889. +TASK_PFA_CLEAR(AUFS, aufs) /* task_clear_aufs */
  29890. +
  29891. +static inline int si_pid_test(struct super_block *sb)
  29892. +{
  29893. + return !!task_test_aufs(current);
  29894. +}
  29895. +
  29896. +static inline void si_pid_clr(struct super_block *sb)
  29897. +{
  29898. + AuDebugOn(!task_test_aufs(current));
  29899. + task_clear_aufs(current);
  29900. +}
  29901. +
  29902. +static inline void si_pid_set(struct super_block *sb)
  29903. +{
  29904. + AuDebugOn(task_test_aufs(current));
  29905. + task_set_aufs(current);
  29906. +}
  29907. +
  29908. +/* ---------------------------------------------------------------------- */
  29909. +
  29910. +/* lock superblock. mainly for entry point functions */
  29911. +#define __si_read_lock(sb) au_rw_read_lock(&au_sbi(sb)->si_rwsem)
  29912. +#define __si_write_lock(sb) au_rw_write_lock(&au_sbi(sb)->si_rwsem)
  29913. +#define __si_read_trylock(sb) au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
  29914. +#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
  29915. +/*
  29916. +#define __si_read_trylock_nested(sb) \
  29917. + au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
  29918. +#define __si_write_trylock_nested(sb) \
  29919. + au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
  29920. +*/
  29921. +
  29922. +#define __si_read_unlock(sb) au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
  29923. +#define __si_write_unlock(sb) au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
  29924. +#define __si_downgrade_lock(sb) au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
  29925. +
  29926. +#define SiMustNoWaiters(sb) AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
  29927. +#define SiMustAnyLock(sb) AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
  29928. +#define SiMustWriteLock(sb) AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
  29929. +
  29930. +static inline void si_noflush_read_lock(struct super_block *sb)
  29931. +{
  29932. + __si_read_lock(sb);
  29933. + si_pid_set(sb);
  29934. +}
  29935. +
  29936. +static inline int si_noflush_read_trylock(struct super_block *sb)
  29937. +{
  29938. + int locked;
  29939. +
  29940. + locked = __si_read_trylock(sb);
  29941. + if (locked)
  29942. + si_pid_set(sb);
  29943. + return locked;
  29944. +}
  29945. +
  29946. +static inline void si_noflush_write_lock(struct super_block *sb)
  29947. +{
  29948. + __si_write_lock(sb);
  29949. + si_pid_set(sb);
  29950. +}
  29951. +
  29952. +static inline int si_noflush_write_trylock(struct super_block *sb)
  29953. +{
  29954. + int locked;
  29955. +
  29956. + locked = __si_write_trylock(sb);
  29957. + if (locked)
  29958. + si_pid_set(sb);
  29959. + return locked;
  29960. +}
  29961. +
  29962. +#if 0 /* reserved */
  29963. +static inline int si_read_trylock(struct super_block *sb, int flags)
  29964. +{
  29965. + if (au_ftest_lock(flags, FLUSH))
  29966. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  29967. + return si_noflush_read_trylock(sb);
  29968. +}
  29969. +#endif
  29970. +
  29971. +static inline void si_read_unlock(struct super_block *sb)
  29972. +{
  29973. + si_pid_clr(sb);
  29974. + __si_read_unlock(sb);
  29975. +}
  29976. +
  29977. +#if 0 /* reserved */
  29978. +static inline int si_write_trylock(struct super_block *sb, int flags)
  29979. +{
  29980. + if (au_ftest_lock(flags, FLUSH))
  29981. + au_nwt_flush(&au_sbi(sb)->si_nowait);
  29982. + return si_noflush_write_trylock(sb);
  29983. +}
  29984. +#endif
  29985. +
  29986. +static inline void si_write_unlock(struct super_block *sb)
  29987. +{
  29988. + si_pid_clr(sb);
  29989. + __si_write_unlock(sb);
  29990. +}
  29991. +
  29992. +#if 0 /* reserved */
  29993. +static inline void si_downgrade_lock(struct super_block *sb)
  29994. +{
  29995. + __si_downgrade_lock(sb);
  29996. +}
  29997. +#endif
  29998. +
  29999. +/* ---------------------------------------------------------------------- */
  30000. +
  30001. +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
  30002. +{
  30003. + SiMustAnyLock(sb);
  30004. + return au_sbi(sb)->si_bbot;
  30005. +}
  30006. +
  30007. +static inline unsigned int au_mntflags(struct super_block *sb)
  30008. +{
  30009. + SiMustAnyLock(sb);
  30010. + return au_sbi(sb)->si_mntflags;
  30011. +}
  30012. +
  30013. +static inline unsigned int au_sigen(struct super_block *sb)
  30014. +{
  30015. + SiMustAnyLock(sb);
  30016. + return au_sbi(sb)->si_generation;
  30017. +}
  30018. +
  30019. +static inline struct au_branch *au_sbr(struct super_block *sb,
  30020. + aufs_bindex_t bindex)
  30021. +{
  30022. + SiMustAnyLock(sb);
  30023. + return au_sbi(sb)->si_branch[0 + bindex];
  30024. +}
  30025. +
  30026. +static inline loff_t au_xi_maxent(struct super_block *sb)
  30027. +{
  30028. + SiMustAnyLock(sb);
  30029. + return au_sbi(sb)->si_ximaxent;
  30030. +}
  30031. +
  30032. +#endif /* __KERNEL__ */
  30033. +#endif /* __AUFS_SUPER_H__ */
  30034. diff -Naur null/fs/aufs/sysaufs.c linux-5.1/fs/aufs/sysaufs.c
  30035. --- /dev/null
  30036. +++ linux-5.1/fs/aufs/sysaufs.c 2019-05-22 11:23:07.443140904 +0200
  30037. @@ -0,0 +1,93 @@
  30038. +// SPDX-License-Identifier: GPL-2.0
  30039. +/*
  30040. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  30041. + *
  30042. + * This program, aufs is free software; you can redistribute it and/or modify
  30043. + * it under the terms of the GNU General Public License as published by
  30044. + * the Free Software Foundation; either version 2 of the License, or
  30045. + * (at your option) any later version.
  30046. + *
  30047. + * This program is distributed in the hope that it will be useful,
  30048. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30049. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30050. + * GNU General Public License for more details.
  30051. + *
  30052. + * You should have received a copy of the GNU General Public License
  30053. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30054. + */
  30055. +
  30056. +/*
  30057. + * sysfs interface and lifetime management
  30058. + * they are necessary regardless sysfs is disabled.
  30059. + */
  30060. +
  30061. +#include <linux/random.h>
  30062. +#include "aufs.h"
  30063. +
  30064. +unsigned long sysaufs_si_mask;
  30065. +struct kset *sysaufs_kset;
  30066. +
  30067. +#define AuSiAttr(_name) { \
  30068. + .attr = { .name = __stringify(_name), .mode = 0444 }, \
  30069. + .show = sysaufs_si_##_name, \
  30070. +}
  30071. +
  30072. +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
  30073. +struct attribute *sysaufs_si_attrs[] = {
  30074. + &sysaufs_si_attr_xi_path.attr,
  30075. + NULL,
  30076. +};
  30077. +
  30078. +static const struct sysfs_ops au_sbi_ops = {
  30079. + .show = sysaufs_si_show
  30080. +};
  30081. +
  30082. +static struct kobj_type au_sbi_ktype = {
  30083. + .release = au_si_free,
  30084. + .sysfs_ops = &au_sbi_ops,
  30085. + .default_attrs = sysaufs_si_attrs
  30086. +};
  30087. +
  30088. +/* ---------------------------------------------------------------------- */
  30089. +
  30090. +int sysaufs_si_init(struct au_sbinfo *sbinfo)
  30091. +{
  30092. + int err;
  30093. +
  30094. + sbinfo->si_kobj.kset = sysaufs_kset;
  30095. + /* cf. sysaufs_name() */
  30096. + err = kobject_init_and_add
  30097. + (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
  30098. + SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
  30099. +
  30100. + return err;
  30101. +}
  30102. +
  30103. +void sysaufs_fin(void)
  30104. +{
  30105. + sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
  30106. + kset_unregister(sysaufs_kset);
  30107. +}
  30108. +
  30109. +int __init sysaufs_init(void)
  30110. +{
  30111. + int err;
  30112. +
  30113. + do {
  30114. + get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
  30115. + } while (!sysaufs_si_mask);
  30116. +
  30117. + err = -EINVAL;
  30118. + sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
  30119. + if (unlikely(!sysaufs_kset))
  30120. + goto out;
  30121. + err = PTR_ERR(sysaufs_kset);
  30122. + if (IS_ERR(sysaufs_kset))
  30123. + goto out;
  30124. + err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
  30125. + if (unlikely(err))
  30126. + kset_unregister(sysaufs_kset);
  30127. +
  30128. +out:
  30129. + return err;
  30130. +}
  30131. diff -Naur null/fs/aufs/sysaufs.h linux-5.1/fs/aufs/sysaufs.h
  30132. --- /dev/null
  30133. +++ linux-5.1/fs/aufs/sysaufs.h 2019-05-22 11:23:07.443140904 +0200
  30134. @@ -0,0 +1,102 @@
  30135. +/* SPDX-License-Identifier: GPL-2.0 */
  30136. +/*
  30137. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  30138. + *
  30139. + * This program, aufs is free software; you can redistribute it and/or modify
  30140. + * it under the terms of the GNU General Public License as published by
  30141. + * the Free Software Foundation; either version 2 of the License, or
  30142. + * (at your option) any later version.
  30143. + *
  30144. + * This program is distributed in the hope that it will be useful,
  30145. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30146. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30147. + * GNU General Public License for more details.
  30148. + *
  30149. + * You should have received a copy of the GNU General Public License
  30150. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30151. + */
  30152. +
  30153. +/*
  30154. + * sysfs interface and mount lifetime management
  30155. + */
  30156. +
  30157. +#ifndef __SYSAUFS_H__
  30158. +#define __SYSAUFS_H__
  30159. +
  30160. +#ifdef __KERNEL__
  30161. +
  30162. +#include <linux/sysfs.h>
  30163. +#include "module.h"
  30164. +
  30165. +struct super_block;
  30166. +struct au_sbinfo;
  30167. +
  30168. +struct sysaufs_si_attr {
  30169. + struct attribute attr;
  30170. + int (*show)(struct seq_file *seq, struct super_block *sb);
  30171. +};
  30172. +
  30173. +/* ---------------------------------------------------------------------- */
  30174. +
  30175. +/* sysaufs.c */
  30176. +extern unsigned long sysaufs_si_mask;
  30177. +extern struct kset *sysaufs_kset;
  30178. +extern struct attribute *sysaufs_si_attrs[];
  30179. +int sysaufs_si_init(struct au_sbinfo *sbinfo);
  30180. +int __init sysaufs_init(void);
  30181. +void sysaufs_fin(void);
  30182. +
  30183. +/* ---------------------------------------------------------------------- */
  30184. +
  30185. +/* some people doesn't like to show a pointer in kernel */
  30186. +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
  30187. +{
  30188. + return sysaufs_si_mask ^ (unsigned long)sbinfo;
  30189. +}
  30190. +
  30191. +#define SysaufsSiNamePrefix "si_"
  30192. +#define SysaufsSiNameLen (sizeof(SysaufsSiNamePrefix) + 16)
  30193. +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
  30194. +{
  30195. + snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
  30196. + sysaufs_si_id(sbinfo));
  30197. +}
  30198. +
  30199. +struct au_branch;
  30200. +#ifdef CONFIG_SYSFS
  30201. +/* sysfs.c */
  30202. +extern struct attribute_group *sysaufs_attr_group;
  30203. +
  30204. +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
  30205. +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
  30206. + char *buf);
  30207. +long au_brinfo_ioctl(struct file *file, unsigned long arg);
  30208. +#ifdef CONFIG_COMPAT
  30209. +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
  30210. +#endif
  30211. +
  30212. +void sysaufs_br_init(struct au_branch *br);
  30213. +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
  30214. +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
  30215. +
  30216. +#define sysaufs_brs_init() do {} while (0)
  30217. +
  30218. +#else
  30219. +#define sysaufs_attr_group NULL
  30220. +
  30221. +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
  30222. +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
  30223. + struct attribute *attr, char *buf)
  30224. +AuStubVoid(sysaufs_br_init, struct au_branch *br)
  30225. +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
  30226. +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
  30227. +
  30228. +static inline void sysaufs_brs_init(void)
  30229. +{
  30230. + sysaufs_brs = 0;
  30231. +}
  30232. +
  30233. +#endif /* CONFIG_SYSFS */
  30234. +
  30235. +#endif /* __KERNEL__ */
  30236. +#endif /* __SYSAUFS_H__ */
  30237. diff -Naur null/fs/aufs/sysfs.c linux-5.1/fs/aufs/sysfs.c
  30238. --- /dev/null
  30239. +++ linux-5.1/fs/aufs/sysfs.c 2019-05-22 11:23:07.443140904 +0200
  30240. @@ -0,0 +1,374 @@
  30241. +// SPDX-License-Identifier: GPL-2.0
  30242. +/*
  30243. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  30244. + *
  30245. + * This program, aufs is free software; you can redistribute it and/or modify
  30246. + * it under the terms of the GNU General Public License as published by
  30247. + * the Free Software Foundation; either version 2 of the License, or
  30248. + * (at your option) any later version.
  30249. + *
  30250. + * This program is distributed in the hope that it will be useful,
  30251. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30252. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30253. + * GNU General Public License for more details.
  30254. + *
  30255. + * You should have received a copy of the GNU General Public License
  30256. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30257. + */
  30258. +
  30259. +/*
  30260. + * sysfs interface
  30261. + */
  30262. +
  30263. +#include <linux/compat.h>
  30264. +#include <linux/seq_file.h>
  30265. +#include "aufs.h"
  30266. +
  30267. +#ifdef CONFIG_AUFS_FS_MODULE
  30268. +/* this entry violates the "one line per file" policy of sysfs */
  30269. +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
  30270. + char *buf)
  30271. +{
  30272. + ssize_t err;
  30273. + static char *conf =
  30274. +/* this file is generated at compiling */
  30275. +#include "conf.str"
  30276. + ;
  30277. +
  30278. + err = snprintf(buf, PAGE_SIZE, conf);
  30279. + if (unlikely(err >= PAGE_SIZE))
  30280. + err = -EFBIG;
  30281. + return err;
  30282. +}
  30283. +
  30284. +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
  30285. +#endif
  30286. +
  30287. +static struct attribute *au_attr[] = {
  30288. +#ifdef CONFIG_AUFS_FS_MODULE
  30289. + &au_config_attr.attr,
  30290. +#endif
  30291. + NULL, /* need to NULL terminate the list of attributes */
  30292. +};
  30293. +
  30294. +static struct attribute_group sysaufs_attr_group_body = {
  30295. + .attrs = au_attr
  30296. +};
  30297. +
  30298. +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
  30299. +
  30300. +/* ---------------------------------------------------------------------- */
  30301. +
  30302. +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
  30303. +{
  30304. + int err;
  30305. +
  30306. + SiMustAnyLock(sb);
  30307. +
  30308. + err = 0;
  30309. + if (au_opt_test(au_mntflags(sb), XINO)) {
  30310. + err = au_xino_path(seq, au_sbi(sb)->si_xib);
  30311. + seq_putc(seq, '\n');
  30312. + }
  30313. + return err;
  30314. +}
  30315. +
  30316. +/*
  30317. + * the lifetime of branch is independent from the entry under sysfs.
  30318. + * sysfs handles the lifetime of the entry, and never call ->show() after it is
  30319. + * unlinked.
  30320. + */
  30321. +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
  30322. + aufs_bindex_t bindex, int idx)
  30323. +{
  30324. + int err;
  30325. + struct path path;
  30326. + struct dentry *root;
  30327. + struct au_branch *br;
  30328. + au_br_perm_str_t perm;
  30329. +
  30330. + AuDbg("b%d\n", bindex);
  30331. +
  30332. + err = 0;
  30333. + root = sb->s_root;
  30334. + di_read_lock_parent(root, !AuLock_IR);
  30335. + br = au_sbr(sb, bindex);
  30336. +
  30337. + switch (idx) {
  30338. + case AuBrSysfs_BR:
  30339. + path.mnt = au_br_mnt(br);
  30340. + path.dentry = au_h_dptr(root, bindex);
  30341. + err = au_seq_path(seq, &path);
  30342. + if (!err) {
  30343. + au_optstr_br_perm(&perm, br->br_perm);
  30344. + seq_printf(seq, "=%s\n", perm.a);
  30345. + }
  30346. + break;
  30347. + case AuBrSysfs_BRID:
  30348. + seq_printf(seq, "%d\n", br->br_id);
  30349. + break;
  30350. + }
  30351. + di_read_unlock(root, !AuLock_IR);
  30352. + if (unlikely(err || seq_has_overflowed(seq)))
  30353. + err = -E2BIG;
  30354. +
  30355. + return err;
  30356. +}
  30357. +
  30358. +/* ---------------------------------------------------------------------- */
  30359. +
  30360. +static struct seq_file *au_seq(char *p, ssize_t len)
  30361. +{
  30362. + struct seq_file *seq;
  30363. +
  30364. + seq = kzalloc(sizeof(*seq), GFP_NOFS);
  30365. + if (seq) {
  30366. + /* mutex_init(&seq.lock); */
  30367. + seq->buf = p;
  30368. + seq->size = len;
  30369. + return seq; /* success */
  30370. + }
  30371. +
  30372. + seq = ERR_PTR(-ENOMEM);
  30373. + return seq;
  30374. +}
  30375. +
  30376. +#define SysaufsBr_PREFIX "br"
  30377. +#define SysaufsBrid_PREFIX "brid"
  30378. +
  30379. +/* todo: file size may exceed PAGE_SIZE */
  30380. +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
  30381. + char *buf)
  30382. +{
  30383. + ssize_t err;
  30384. + int idx;
  30385. + long l;
  30386. + aufs_bindex_t bbot;
  30387. + struct au_sbinfo *sbinfo;
  30388. + struct super_block *sb;
  30389. + struct seq_file *seq;
  30390. + char *name;
  30391. + struct attribute **cattr;
  30392. +
  30393. + sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
  30394. + sb = sbinfo->si_sb;
  30395. +
  30396. + /*
  30397. + * prevent a race condition between sysfs and aufs.
  30398. + * for instance, sysfs_file_read() calls sysfs_get_active_two() which
  30399. + * prohibits maintaining the sysfs entries.
  30400. + * hew we acquire read lock after sysfs_get_active_two().
  30401. + * on the other hand, the remount process may maintain the sysfs/aufs
  30402. + * entries after acquiring write lock.
  30403. + * it can cause a deadlock.
  30404. + * simply we gave up processing read here.
  30405. + */
  30406. + err = -EBUSY;
  30407. + if (unlikely(!si_noflush_read_trylock(sb)))
  30408. + goto out;
  30409. +
  30410. + seq = au_seq(buf, PAGE_SIZE);
  30411. + err = PTR_ERR(seq);
  30412. + if (IS_ERR(seq))
  30413. + goto out_unlock;
  30414. +
  30415. + name = (void *)attr->name;
  30416. + cattr = sysaufs_si_attrs;
  30417. + while (*cattr) {
  30418. + if (!strcmp(name, (*cattr)->name)) {
  30419. + err = container_of(*cattr, struct sysaufs_si_attr, attr)
  30420. + ->show(seq, sb);
  30421. + goto out_seq;
  30422. + }
  30423. + cattr++;
  30424. + }
  30425. +
  30426. + if (!strncmp(name, SysaufsBrid_PREFIX,
  30427. + sizeof(SysaufsBrid_PREFIX) - 1)) {
  30428. + idx = AuBrSysfs_BRID;
  30429. + name += sizeof(SysaufsBrid_PREFIX) - 1;
  30430. + } else if (!strncmp(name, SysaufsBr_PREFIX,
  30431. + sizeof(SysaufsBr_PREFIX) - 1)) {
  30432. + idx = AuBrSysfs_BR;
  30433. + name += sizeof(SysaufsBr_PREFIX) - 1;
  30434. + } else
  30435. + BUG();
  30436. +
  30437. + err = kstrtol(name, 10, &l);
  30438. + if (!err) {
  30439. + bbot = au_sbbot(sb);
  30440. + if (l <= bbot)
  30441. + err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
  30442. + else
  30443. + err = -ENOENT;
  30444. + }
  30445. +
  30446. +out_seq:
  30447. + if (!err) {
  30448. + err = seq->count;
  30449. + /* sysfs limit */
  30450. + if (unlikely(err == PAGE_SIZE))
  30451. + err = -EFBIG;
  30452. + }
  30453. + au_kfree_rcu(seq);
  30454. +out_unlock:
  30455. + si_read_unlock(sb);
  30456. +out:
  30457. + return err;
  30458. +}
  30459. +
  30460. +/* ---------------------------------------------------------------------- */
  30461. +
  30462. +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
  30463. +{
  30464. + int err;
  30465. + int16_t brid;
  30466. + aufs_bindex_t bindex, bbot;
  30467. + size_t sz;
  30468. + char *buf;
  30469. + struct seq_file *seq;
  30470. + struct au_branch *br;
  30471. +
  30472. + si_read_lock(sb, AuLock_FLUSH);
  30473. + bbot = au_sbbot(sb);
  30474. + err = bbot + 1;
  30475. + if (!arg)
  30476. + goto out;
  30477. +
  30478. + err = -ENOMEM;
  30479. + buf = (void *)__get_free_page(GFP_NOFS);
  30480. + if (unlikely(!buf))
  30481. + goto out;
  30482. +
  30483. + seq = au_seq(buf, PAGE_SIZE);
  30484. + err = PTR_ERR(seq);
  30485. + if (IS_ERR(seq))
  30486. + goto out_buf;
  30487. +
  30488. + sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
  30489. + for (bindex = 0; bindex <= bbot; bindex++, arg++) {
  30490. + /* VERIFY_WRITE */
  30491. + err = !access_ok(arg, sizeof(*arg));
  30492. + if (unlikely(err))
  30493. + break;
  30494. +
  30495. + br = au_sbr(sb, bindex);
  30496. + brid = br->br_id;
  30497. + BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
  30498. + err = __put_user(brid, &arg->id);
  30499. + if (unlikely(err))
  30500. + break;
  30501. +
  30502. + BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
  30503. + err = __put_user(br->br_perm, &arg->perm);
  30504. + if (unlikely(err))
  30505. + break;
  30506. +
  30507. + err = au_seq_path(seq, &br->br_path);
  30508. + if (unlikely(err))
  30509. + break;
  30510. + seq_putc(seq, '\0');
  30511. + if (!seq_has_overflowed(seq)) {
  30512. + err = copy_to_user(arg->path, seq->buf, seq->count);
  30513. + seq->count = 0;
  30514. + if (unlikely(err))
  30515. + break;
  30516. + } else {
  30517. + err = -E2BIG;
  30518. + goto out_seq;
  30519. + }
  30520. + }
  30521. + if (unlikely(err))
  30522. + err = -EFAULT;
  30523. +
  30524. +out_seq:
  30525. + au_kfree_rcu(seq);
  30526. +out_buf:
  30527. + free_page((unsigned long)buf);
  30528. +out:
  30529. + si_read_unlock(sb);
  30530. + return err;
  30531. +}
  30532. +
  30533. +long au_brinfo_ioctl(struct file *file, unsigned long arg)
  30534. +{
  30535. + return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
  30536. +}
  30537. +
  30538. +#ifdef CONFIG_COMPAT
  30539. +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
  30540. +{
  30541. + return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
  30542. +}
  30543. +#endif
  30544. +
  30545. +/* ---------------------------------------------------------------------- */
  30546. +
  30547. +void sysaufs_br_init(struct au_branch *br)
  30548. +{
  30549. + int i;
  30550. + struct au_brsysfs *br_sysfs;
  30551. + struct attribute *attr;
  30552. +
  30553. + br_sysfs = br->br_sysfs;
  30554. + for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
  30555. + attr = &br_sysfs->attr;
  30556. + sysfs_attr_init(attr);
  30557. + attr->name = br_sysfs->name;
  30558. + attr->mode = 0444;
  30559. + br_sysfs++;
  30560. + }
  30561. +}
  30562. +
  30563. +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
  30564. +{
  30565. + struct au_branch *br;
  30566. + struct kobject *kobj;
  30567. + struct au_brsysfs *br_sysfs;
  30568. + int i;
  30569. + aufs_bindex_t bbot;
  30570. +
  30571. + if (!sysaufs_brs)
  30572. + return;
  30573. +
  30574. + kobj = &au_sbi(sb)->si_kobj;
  30575. + bbot = au_sbbot(sb);
  30576. + for (; bindex <= bbot; bindex++) {
  30577. + br = au_sbr(sb, bindex);
  30578. + br_sysfs = br->br_sysfs;
  30579. + for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
  30580. + sysfs_remove_file(kobj, &br_sysfs->attr);
  30581. + br_sysfs++;
  30582. + }
  30583. + }
  30584. +}
  30585. +
  30586. +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
  30587. +{
  30588. + int err, i;
  30589. + aufs_bindex_t bbot;
  30590. + struct kobject *kobj;
  30591. + struct au_branch *br;
  30592. + struct au_brsysfs *br_sysfs;
  30593. +
  30594. + if (!sysaufs_brs)
  30595. + return;
  30596. +
  30597. + kobj = &au_sbi(sb)->si_kobj;
  30598. + bbot = au_sbbot(sb);
  30599. + for (; bindex <= bbot; bindex++) {
  30600. + br = au_sbr(sb, bindex);
  30601. + br_sysfs = br->br_sysfs;
  30602. + snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
  30603. + SysaufsBr_PREFIX "%d", bindex);
  30604. + snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
  30605. + SysaufsBrid_PREFIX "%d", bindex);
  30606. + for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
  30607. + err = sysfs_create_file(kobj, &br_sysfs->attr);
  30608. + if (unlikely(err))
  30609. + pr_warn("failed %s under sysfs(%d)\n",
  30610. + br_sysfs->name, err);
  30611. + br_sysfs++;
  30612. + }
  30613. + }
  30614. +}
  30615. diff -Naur null/fs/aufs/sysrq.c linux-5.1/fs/aufs/sysrq.c
  30616. --- /dev/null
  30617. +++ linux-5.1/fs/aufs/sysrq.c 2019-05-22 11:23:07.443140904 +0200
  30618. @@ -0,0 +1,160 @@
  30619. +// SPDX-License-Identifier: GPL-2.0
  30620. +/*
  30621. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  30622. + *
  30623. + * This program, aufs is free software; you can redistribute it and/or modify
  30624. + * it under the terms of the GNU General Public License as published by
  30625. + * the Free Software Foundation; either version 2 of the License, or
  30626. + * (at your option) any later version.
  30627. + *
  30628. + * This program is distributed in the hope that it will be useful,
  30629. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30630. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30631. + * GNU General Public License for more details.
  30632. + *
  30633. + * You should have received a copy of the GNU General Public License
  30634. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30635. + */
  30636. +
  30637. +/*
  30638. + * magic sysrq handler
  30639. + */
  30640. +
  30641. +/* #include <linux/sysrq.h> */
  30642. +#include <linux/writeback.h>
  30643. +#include "aufs.h"
  30644. +
  30645. +/* ---------------------------------------------------------------------- */
  30646. +
  30647. +static void sysrq_sb(struct super_block *sb)
  30648. +{
  30649. + char *plevel;
  30650. + struct au_sbinfo *sbinfo;
  30651. + struct file *file;
  30652. + struct hlist_bl_head *files;
  30653. + struct hlist_bl_node *pos;
  30654. + struct au_finfo *finfo;
  30655. +
  30656. + plevel = au_plevel;
  30657. + au_plevel = KERN_WARNING;
  30658. +
  30659. + /* since we define pr_fmt, call printk directly */
  30660. +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
  30661. +
  30662. + sbinfo = au_sbi(sb);
  30663. + printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
  30664. + pr("superblock\n");
  30665. + au_dpri_sb(sb);
  30666. +
  30667. +#if 0
  30668. + pr("root dentry\n");
  30669. + au_dpri_dentry(sb->s_root);
  30670. + pr("root inode\n");
  30671. + au_dpri_inode(d_inode(sb->s_root));
  30672. +#endif
  30673. +
  30674. +#if 0
  30675. + do {
  30676. + int err, i, j, ndentry;
  30677. + struct au_dcsub_pages dpages;
  30678. + struct au_dpage *dpage;
  30679. +
  30680. + err = au_dpages_init(&dpages, GFP_ATOMIC);
  30681. + if (unlikely(err))
  30682. + break;
  30683. + err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
  30684. + if (!err)
  30685. + for (i = 0; i < dpages.ndpage; i++) {
  30686. + dpage = dpages.dpages + i;
  30687. + ndentry = dpage->ndentry;
  30688. + for (j = 0; j < ndentry; j++)
  30689. + au_dpri_dentry(dpage->dentries[j]);
  30690. + }
  30691. + au_dpages_free(&dpages);
  30692. + } while (0);
  30693. +#endif
  30694. +
  30695. +#if 1
  30696. + {
  30697. + struct inode *i;
  30698. +
  30699. + pr("isolated inode\n");
  30700. + spin_lock(&sb->s_inode_list_lock);
  30701. + list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
  30702. + spin_lock(&i->i_lock);
  30703. + if (1 || hlist_empty(&i->i_dentry))
  30704. + au_dpri_inode(i);
  30705. + spin_unlock(&i->i_lock);
  30706. + }
  30707. + spin_unlock(&sb->s_inode_list_lock);
  30708. + }
  30709. +#endif
  30710. + pr("files\n");
  30711. + files = &au_sbi(sb)->si_files;
  30712. + hlist_bl_lock(files);
  30713. + hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
  30714. + umode_t mode;
  30715. +
  30716. + file = finfo->fi_file;
  30717. + mode = file_inode(file)->i_mode;
  30718. + if (!special_file(mode))
  30719. + au_dpri_file(file);
  30720. + }
  30721. + hlist_bl_unlock(files);
  30722. + pr("done\n");
  30723. +
  30724. +#undef pr
  30725. + au_plevel = plevel;
  30726. +}
  30727. +
  30728. +/* ---------------------------------------------------------------------- */
  30729. +
  30730. +/* module parameter */
  30731. +static char *aufs_sysrq_key = "a";
  30732. +module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
  30733. +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
  30734. +
  30735. +static void au_sysrq(int key __maybe_unused)
  30736. +{
  30737. + struct au_sbinfo *sbinfo;
  30738. + struct hlist_bl_node *pos;
  30739. +
  30740. + lockdep_off();
  30741. + au_sbilist_lock();
  30742. + hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
  30743. + sysrq_sb(sbinfo->si_sb);
  30744. + au_sbilist_unlock();
  30745. + lockdep_on();
  30746. +}
  30747. +
  30748. +static struct sysrq_key_op au_sysrq_op = {
  30749. + .handler = au_sysrq,
  30750. + .help_msg = "Aufs",
  30751. + .action_msg = "Aufs",
  30752. + .enable_mask = SYSRQ_ENABLE_DUMP
  30753. +};
  30754. +
  30755. +/* ---------------------------------------------------------------------- */
  30756. +
  30757. +int __init au_sysrq_init(void)
  30758. +{
  30759. + int err;
  30760. + char key;
  30761. +
  30762. + err = -1;
  30763. + key = *aufs_sysrq_key;
  30764. + if ('a' <= key && key <= 'z')
  30765. + err = register_sysrq_key(key, &au_sysrq_op);
  30766. + if (unlikely(err))
  30767. + pr_err("err %d, sysrq=%c\n", err, key);
  30768. + return err;
  30769. +}
  30770. +
  30771. +void au_sysrq_fin(void)
  30772. +{
  30773. + int err;
  30774. +
  30775. + err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
  30776. + if (unlikely(err))
  30777. + pr_err("err %d (ignored)\n", err);
  30778. +}
  30779. diff -Naur null/fs/aufs/vdir.c linux-5.1/fs/aufs/vdir.c
  30780. --- /dev/null
  30781. +++ linux-5.1/fs/aufs/vdir.c 2019-05-22 11:23:07.443140904 +0200
  30782. @@ -0,0 +1,896 @@
  30783. +// SPDX-License-Identifier: GPL-2.0
  30784. +/*
  30785. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  30786. + *
  30787. + * This program, aufs is free software; you can redistribute it and/or modify
  30788. + * it under the terms of the GNU General Public License as published by
  30789. + * the Free Software Foundation; either version 2 of the License, or
  30790. + * (at your option) any later version.
  30791. + *
  30792. + * This program is distributed in the hope that it will be useful,
  30793. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  30794. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  30795. + * GNU General Public License for more details.
  30796. + *
  30797. + * You should have received a copy of the GNU General Public License
  30798. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  30799. + */
  30800. +
  30801. +/*
  30802. + * virtual or vertical directory
  30803. + */
  30804. +
  30805. +#include <linux/iversion.h>
  30806. +#include "aufs.h"
  30807. +
  30808. +static unsigned int calc_size(int nlen)
  30809. +{
  30810. + return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
  30811. +}
  30812. +
  30813. +static int set_deblk_end(union au_vdir_deblk_p *p,
  30814. + union au_vdir_deblk_p *deblk_end)
  30815. +{
  30816. + if (calc_size(0) <= deblk_end->deblk - p->deblk) {
  30817. + p->de->de_str.len = 0;
  30818. + /* smp_mb(); */
  30819. + return 0;
  30820. + }
  30821. + return -1; /* error */
  30822. +}
  30823. +
  30824. +/* returns true or false */
  30825. +static int is_deblk_end(union au_vdir_deblk_p *p,
  30826. + union au_vdir_deblk_p *deblk_end)
  30827. +{
  30828. + if (calc_size(0) <= deblk_end->deblk - p->deblk)
  30829. + return !p->de->de_str.len;
  30830. + return 1;
  30831. +}
  30832. +
  30833. +static unsigned char *last_deblk(struct au_vdir *vdir)
  30834. +{
  30835. + return vdir->vd_deblk[vdir->vd_nblk - 1];
  30836. +}
  30837. +
  30838. +/* ---------------------------------------------------------------------- */
  30839. +
  30840. +/* estimate the appropriate size for name hash table */
  30841. +unsigned int au_rdhash_est(loff_t sz)
  30842. +{
  30843. + unsigned int n;
  30844. +
  30845. + n = UINT_MAX;
  30846. + sz >>= 10;
  30847. + if (sz < n)
  30848. + n = sz;
  30849. + if (sz < AUFS_RDHASH_DEF)
  30850. + n = AUFS_RDHASH_DEF;
  30851. + /* pr_info("n %u\n", n); */
  30852. + return n;
  30853. +}
  30854. +
  30855. +/*
  30856. + * the allocated memory has to be freed by
  30857. + * au_nhash_wh_free() or au_nhash_de_free().
  30858. + */
  30859. +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
  30860. +{
  30861. + struct hlist_head *head;
  30862. + unsigned int u;
  30863. + size_t sz;
  30864. +
  30865. + sz = sizeof(*nhash->nh_head) * num_hash;
  30866. + head = kmalloc(sz, gfp);
  30867. + if (head) {
  30868. + nhash->nh_num = num_hash;
  30869. + nhash->nh_head = head;
  30870. + for (u = 0; u < num_hash; u++)
  30871. + INIT_HLIST_HEAD(head++);
  30872. + return 0; /* success */
  30873. + }
  30874. +
  30875. + return -ENOMEM;
  30876. +}
  30877. +
  30878. +static void nhash_count(struct hlist_head *head)
  30879. +{
  30880. +#if 0
  30881. + unsigned long n;
  30882. + struct hlist_node *pos;
  30883. +
  30884. + n = 0;
  30885. + hlist_for_each(pos, head)
  30886. + n++;
  30887. + pr_info("%lu\n", n);
  30888. +#endif
  30889. +}
  30890. +
  30891. +static void au_nhash_wh_do_free(struct hlist_head *head)
  30892. +{
  30893. + struct au_vdir_wh *pos;
  30894. + struct hlist_node *node;
  30895. +
  30896. + hlist_for_each_entry_safe(pos, node, head, wh_hash)
  30897. + au_kfree_rcu(pos);
  30898. +}
  30899. +
  30900. +static void au_nhash_de_do_free(struct hlist_head *head)
  30901. +{
  30902. + struct au_vdir_dehstr *pos;
  30903. + struct hlist_node *node;
  30904. +
  30905. + hlist_for_each_entry_safe(pos, node, head, hash)
  30906. + au_cache_free_vdir_dehstr(pos);
  30907. +}
  30908. +
  30909. +static void au_nhash_do_free(struct au_nhash *nhash,
  30910. + void (*free)(struct hlist_head *head))
  30911. +{
  30912. + unsigned int n;
  30913. + struct hlist_head *head;
  30914. +
  30915. + n = nhash->nh_num;
  30916. + if (!n)
  30917. + return;
  30918. +
  30919. + head = nhash->nh_head;
  30920. + while (n-- > 0) {
  30921. + nhash_count(head);
  30922. + free(head++);
  30923. + }
  30924. + au_kfree_try_rcu(nhash->nh_head);
  30925. +}
  30926. +
  30927. +void au_nhash_wh_free(struct au_nhash *whlist)
  30928. +{
  30929. + au_nhash_do_free(whlist, au_nhash_wh_do_free);
  30930. +}
  30931. +
  30932. +static void au_nhash_de_free(struct au_nhash *delist)
  30933. +{
  30934. + au_nhash_do_free(delist, au_nhash_de_do_free);
  30935. +}
  30936. +
  30937. +/* ---------------------------------------------------------------------- */
  30938. +
  30939. +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
  30940. + int limit)
  30941. +{
  30942. + int num;
  30943. + unsigned int u, n;
  30944. + struct hlist_head *head;
  30945. + struct au_vdir_wh *pos;
  30946. +
  30947. + num = 0;
  30948. + n = whlist->nh_num;
  30949. + head = whlist->nh_head;
  30950. + for (u = 0; u < n; u++, head++)
  30951. + hlist_for_each_entry(pos, head, wh_hash)
  30952. + if (pos->wh_bindex == btgt && ++num > limit)
  30953. + return 1;
  30954. + return 0;
  30955. +}
  30956. +
  30957. +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
  30958. + unsigned char *name,
  30959. + unsigned int len)
  30960. +{
  30961. + unsigned int v;
  30962. + /* const unsigned int magic_bit = 12; */
  30963. +
  30964. + AuDebugOn(!nhash->nh_num || !nhash->nh_head);
  30965. +
  30966. + v = 0;
  30967. + if (len > 8)
  30968. + len = 8;
  30969. + while (len--)
  30970. + v += *name++;
  30971. + /* v = hash_long(v, magic_bit); */
  30972. + v %= nhash->nh_num;
  30973. + return nhash->nh_head + v;
  30974. +}
  30975. +
  30976. +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
  30977. + int nlen)
  30978. +{
  30979. + return str->len == nlen && !memcmp(str->name, name, nlen);
  30980. +}
  30981. +
  30982. +/* returns found or not */
  30983. +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
  30984. +{
  30985. + struct hlist_head *head;
  30986. + struct au_vdir_wh *pos;
  30987. + struct au_vdir_destr *str;
  30988. +
  30989. + head = au_name_hash(whlist, name, nlen);
  30990. + hlist_for_each_entry(pos, head, wh_hash) {
  30991. + str = &pos->wh_str;
  30992. + AuDbg("%.*s\n", str->len, str->name);
  30993. + if (au_nhash_test_name(str, name, nlen))
  30994. + return 1;
  30995. + }
  30996. + return 0;
  30997. +}
  30998. +
  30999. +/* returns found(true) or not */
  31000. +static int test_known(struct au_nhash *delist, char *name, int nlen)
  31001. +{
  31002. + struct hlist_head *head;
  31003. + struct au_vdir_dehstr *pos;
  31004. + struct au_vdir_destr *str;
  31005. +
  31006. + head = au_name_hash(delist, name, nlen);
  31007. + hlist_for_each_entry(pos, head, hash) {
  31008. + str = pos->str;
  31009. + AuDbg("%.*s\n", str->len, str->name);
  31010. + if (au_nhash_test_name(str, name, nlen))
  31011. + return 1;
  31012. + }
  31013. + return 0;
  31014. +}
  31015. +
  31016. +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
  31017. + unsigned char d_type)
  31018. +{
  31019. +#ifdef CONFIG_AUFS_SHWH
  31020. + wh->wh_ino = ino;
  31021. + wh->wh_type = d_type;
  31022. +#endif
  31023. +}
  31024. +
  31025. +/* ---------------------------------------------------------------------- */
  31026. +
  31027. +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
  31028. + unsigned int d_type, aufs_bindex_t bindex,
  31029. + unsigned char shwh)
  31030. +{
  31031. + int err;
  31032. + struct au_vdir_destr *str;
  31033. + struct au_vdir_wh *wh;
  31034. +
  31035. + AuDbg("%.*s\n", nlen, name);
  31036. + AuDebugOn(!whlist->nh_num || !whlist->nh_head);
  31037. +
  31038. + err = -ENOMEM;
  31039. + wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
  31040. + if (unlikely(!wh))
  31041. + goto out;
  31042. +
  31043. + err = 0;
  31044. + wh->wh_bindex = bindex;
  31045. + if (shwh)
  31046. + au_shwh_init_wh(wh, ino, d_type);
  31047. + str = &wh->wh_str;
  31048. + str->len = nlen;
  31049. + memcpy(str->name, name, nlen);
  31050. + hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
  31051. + /* smp_mb(); */
  31052. +
  31053. +out:
  31054. + return err;
  31055. +}
  31056. +
  31057. +static int append_deblk(struct au_vdir *vdir)
  31058. +{
  31059. + int err;
  31060. + unsigned long ul;
  31061. + const unsigned int deblk_sz = vdir->vd_deblk_sz;
  31062. + union au_vdir_deblk_p p, deblk_end;
  31063. + unsigned char **o;
  31064. +
  31065. + err = -ENOMEM;
  31066. + o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
  31067. + GFP_NOFS, /*may_shrink*/0);
  31068. + if (unlikely(!o))
  31069. + goto out;
  31070. +
  31071. + vdir->vd_deblk = o;
  31072. + p.deblk = kmalloc(deblk_sz, GFP_NOFS);
  31073. + if (p.deblk) {
  31074. + ul = vdir->vd_nblk++;
  31075. + vdir->vd_deblk[ul] = p.deblk;
  31076. + vdir->vd_last.ul = ul;
  31077. + vdir->vd_last.p.deblk = p.deblk;
  31078. + deblk_end.deblk = p.deblk + deblk_sz;
  31079. + err = set_deblk_end(&p, &deblk_end);
  31080. + }
  31081. +
  31082. +out:
  31083. + return err;
  31084. +}
  31085. +
  31086. +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
  31087. + unsigned int d_type, struct au_nhash *delist)
  31088. +{
  31089. + int err;
  31090. + unsigned int sz;
  31091. + const unsigned int deblk_sz = vdir->vd_deblk_sz;
  31092. + union au_vdir_deblk_p p, *room, deblk_end;
  31093. + struct au_vdir_dehstr *dehstr;
  31094. +
  31095. + p.deblk = last_deblk(vdir);
  31096. + deblk_end.deblk = p.deblk + deblk_sz;
  31097. + room = &vdir->vd_last.p;
  31098. + AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
  31099. + || !is_deblk_end(room, &deblk_end));
  31100. +
  31101. + sz = calc_size(nlen);
  31102. + if (unlikely(sz > deblk_end.deblk - room->deblk)) {
  31103. + err = append_deblk(vdir);
  31104. + if (unlikely(err))
  31105. + goto out;
  31106. +
  31107. + p.deblk = last_deblk(vdir);
  31108. + deblk_end.deblk = p.deblk + deblk_sz;
  31109. + /* smp_mb(); */
  31110. + AuDebugOn(room->deblk != p.deblk);
  31111. + }
  31112. +
  31113. + err = -ENOMEM;
  31114. + dehstr = au_cache_alloc_vdir_dehstr();
  31115. + if (unlikely(!dehstr))
  31116. + goto out;
  31117. +
  31118. + dehstr->str = &room->de->de_str;
  31119. + hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
  31120. + room->de->de_ino = ino;
  31121. + room->de->de_type = d_type;
  31122. + room->de->de_str.len = nlen;
  31123. + memcpy(room->de->de_str.name, name, nlen);
  31124. +
  31125. + err = 0;
  31126. + room->deblk += sz;
  31127. + if (unlikely(set_deblk_end(room, &deblk_end)))
  31128. + err = append_deblk(vdir);
  31129. + /* smp_mb(); */
  31130. +
  31131. +out:
  31132. + return err;
  31133. +}
  31134. +
  31135. +/* ---------------------------------------------------------------------- */
  31136. +
  31137. +void au_vdir_free(struct au_vdir *vdir)
  31138. +{
  31139. + unsigned char **deblk;
  31140. +
  31141. + deblk = vdir->vd_deblk;
  31142. + while (vdir->vd_nblk--)
  31143. + au_kfree_try_rcu(*deblk++);
  31144. + au_kfree_try_rcu(vdir->vd_deblk);
  31145. + au_cache_free_vdir(vdir);
  31146. +}
  31147. +
  31148. +static struct au_vdir *alloc_vdir(struct file *file)
  31149. +{
  31150. + struct au_vdir *vdir;
  31151. + struct super_block *sb;
  31152. + int err;
  31153. +
  31154. + sb = file->f_path.dentry->d_sb;
  31155. + SiMustAnyLock(sb);
  31156. +
  31157. + err = -ENOMEM;
  31158. + vdir = au_cache_alloc_vdir();
  31159. + if (unlikely(!vdir))
  31160. + goto out;
  31161. +
  31162. + vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
  31163. + if (unlikely(!vdir->vd_deblk))
  31164. + goto out_free;
  31165. +
  31166. + vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
  31167. + if (!vdir->vd_deblk_sz) {
  31168. + /* estimate the appropriate size for deblk */
  31169. + vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
  31170. + /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
  31171. + }
  31172. + vdir->vd_nblk = 0;
  31173. + vdir->vd_version = 0;
  31174. + vdir->vd_jiffy = 0;
  31175. + err = append_deblk(vdir);
  31176. + if (!err)
  31177. + return vdir; /* success */
  31178. +
  31179. + au_kfree_try_rcu(vdir->vd_deblk);
  31180. +
  31181. +out_free:
  31182. + au_cache_free_vdir(vdir);
  31183. +out:
  31184. + vdir = ERR_PTR(err);
  31185. + return vdir;
  31186. +}
  31187. +
  31188. +static int reinit_vdir(struct au_vdir *vdir)
  31189. +{
  31190. + int err;
  31191. + union au_vdir_deblk_p p, deblk_end;
  31192. +
  31193. + while (vdir->vd_nblk > 1) {
  31194. + au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
  31195. + /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
  31196. + vdir->vd_nblk--;
  31197. + }
  31198. + p.deblk = vdir->vd_deblk[0];
  31199. + deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
  31200. + err = set_deblk_end(&p, &deblk_end);
  31201. + /* keep vd_dblk_sz */
  31202. + vdir->vd_last.ul = 0;
  31203. + vdir->vd_last.p.deblk = vdir->vd_deblk[0];
  31204. + vdir->vd_version = 0;
  31205. + vdir->vd_jiffy = 0;
  31206. + /* smp_mb(); */
  31207. + return err;
  31208. +}
  31209. +
  31210. +/* ---------------------------------------------------------------------- */
  31211. +
  31212. +#define AuFillVdir_CALLED 1
  31213. +#define AuFillVdir_WHABLE (1 << 1)
  31214. +#define AuFillVdir_SHWH (1 << 2)
  31215. +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
  31216. +#define au_fset_fillvdir(flags, name) \
  31217. + do { (flags) |= AuFillVdir_##name; } while (0)
  31218. +#define au_fclr_fillvdir(flags, name) \
  31219. + do { (flags) &= ~AuFillVdir_##name; } while (0)
  31220. +
  31221. +#ifndef CONFIG_AUFS_SHWH
  31222. +#undef AuFillVdir_SHWH
  31223. +#define AuFillVdir_SHWH 0
  31224. +#endif
  31225. +
  31226. +struct fillvdir_arg {
  31227. + struct dir_context ctx;
  31228. + struct file *file;
  31229. + struct au_vdir *vdir;
  31230. + struct au_nhash delist;
  31231. + struct au_nhash whlist;
  31232. + aufs_bindex_t bindex;
  31233. + unsigned int flags;
  31234. + int err;
  31235. +};
  31236. +
  31237. +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
  31238. + loff_t offset __maybe_unused, u64 h_ino,
  31239. + unsigned int d_type)
  31240. +{
  31241. + struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
  31242. + char *name = (void *)__name;
  31243. + struct super_block *sb;
  31244. + ino_t ino;
  31245. + const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
  31246. +
  31247. + arg->err = 0;
  31248. + sb = arg->file->f_path.dentry->d_sb;
  31249. + au_fset_fillvdir(arg->flags, CALLED);
  31250. + /* smp_mb(); */
  31251. + if (nlen <= AUFS_WH_PFX_LEN
  31252. + || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
  31253. + if (test_known(&arg->delist, name, nlen)
  31254. + || au_nhash_test_known_wh(&arg->whlist, name, nlen))
  31255. + goto out; /* already exists or whiteouted */
  31256. +
  31257. + arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
  31258. + if (!arg->err) {
  31259. + if (unlikely(nlen > AUFS_MAX_NAMELEN))
  31260. + d_type = DT_UNKNOWN;
  31261. + arg->err = append_de(arg->vdir, name, nlen, ino,
  31262. + d_type, &arg->delist);
  31263. + }
  31264. + } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
  31265. + name += AUFS_WH_PFX_LEN;
  31266. + nlen -= AUFS_WH_PFX_LEN;
  31267. + if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
  31268. + goto out; /* already whiteouted */
  31269. +
  31270. + ino = 0; /* just to suppress a warning */
  31271. + if (shwh)
  31272. + arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
  31273. + &ino);
  31274. + if (!arg->err) {
  31275. + if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
  31276. + d_type = DT_UNKNOWN;
  31277. + arg->err = au_nhash_append_wh
  31278. + (&arg->whlist, name, nlen, ino, d_type,
  31279. + arg->bindex, shwh);
  31280. + }
  31281. + }
  31282. +
  31283. +out:
  31284. + if (!arg->err)
  31285. + arg->vdir->vd_jiffy = jiffies;
  31286. + /* smp_mb(); */
  31287. + AuTraceErr(arg->err);
  31288. + return arg->err;
  31289. +}
  31290. +
  31291. +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
  31292. + struct au_nhash *whlist, struct au_nhash *delist)
  31293. +{
  31294. +#ifdef CONFIG_AUFS_SHWH
  31295. + int err;
  31296. + unsigned int nh, u;
  31297. + struct hlist_head *head;
  31298. + struct au_vdir_wh *pos;
  31299. + struct hlist_node *n;
  31300. + char *p, *o;
  31301. + struct au_vdir_destr *destr;
  31302. +
  31303. + AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
  31304. +
  31305. + err = -ENOMEM;
  31306. + o = p = (void *)__get_free_page(GFP_NOFS);
  31307. + if (unlikely(!p))
  31308. + goto out;
  31309. +
  31310. + err = 0;
  31311. + nh = whlist->nh_num;
  31312. + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
  31313. + p += AUFS_WH_PFX_LEN;
  31314. + for (u = 0; u < nh; u++) {
  31315. + head = whlist->nh_head + u;
  31316. + hlist_for_each_entry_safe(pos, n, head, wh_hash) {
  31317. + destr = &pos->wh_str;
  31318. + memcpy(p, destr->name, destr->len);
  31319. + err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
  31320. + pos->wh_ino, pos->wh_type, delist);
  31321. + if (unlikely(err))
  31322. + break;
  31323. + }
  31324. + }
  31325. +
  31326. + free_page((unsigned long)o);
  31327. +
  31328. +out:
  31329. + AuTraceErr(err);
  31330. + return err;
  31331. +#else
  31332. + return 0;
  31333. +#endif
  31334. +}
  31335. +
  31336. +static int au_do_read_vdir(struct fillvdir_arg *arg)
  31337. +{
  31338. + int err;
  31339. + unsigned int rdhash;
  31340. + loff_t offset;
  31341. + aufs_bindex_t bbot, bindex, btop;
  31342. + unsigned char shwh;
  31343. + struct file *hf, *file;
  31344. + struct super_block *sb;
  31345. +
  31346. + file = arg->file;
  31347. + sb = file->f_path.dentry->d_sb;
  31348. + SiMustAnyLock(sb);
  31349. +
  31350. + rdhash = au_sbi(sb)->si_rdhash;
  31351. + if (!rdhash)
  31352. + rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
  31353. + err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
  31354. + if (unlikely(err))
  31355. + goto out;
  31356. + err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
  31357. + if (unlikely(err))
  31358. + goto out_delist;
  31359. +
  31360. + err = 0;
  31361. + arg->flags = 0;
  31362. + shwh = 0;
  31363. + if (au_opt_test(au_mntflags(sb), SHWH)) {
  31364. + shwh = 1;
  31365. + au_fset_fillvdir(arg->flags, SHWH);
  31366. + }
  31367. + btop = au_fbtop(file);
  31368. + bbot = au_fbbot_dir(file);
  31369. + for (bindex = btop; !err && bindex <= bbot; bindex++) {
  31370. + hf = au_hf_dir(file, bindex);
  31371. + if (!hf)
  31372. + continue;
  31373. +
  31374. + offset = vfsub_llseek(hf, 0, SEEK_SET);
  31375. + err = offset;
  31376. + if (unlikely(offset))
  31377. + break;
  31378. +
  31379. + arg->bindex = bindex;
  31380. + au_fclr_fillvdir(arg->flags, WHABLE);
  31381. + if (shwh
  31382. + || (bindex != bbot
  31383. + && au_br_whable(au_sbr_perm(sb, bindex))))
  31384. + au_fset_fillvdir(arg->flags, WHABLE);
  31385. + do {
  31386. + arg->err = 0;
  31387. + au_fclr_fillvdir(arg->flags, CALLED);
  31388. + /* smp_mb(); */
  31389. + err = vfsub_iterate_dir(hf, &arg->ctx);
  31390. + if (err >= 0)
  31391. + err = arg->err;
  31392. + } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
  31393. +
  31394. + /*
  31395. + * dir_relax() may be good for concurrency, but aufs should not
  31396. + * use it since it will cause a lockdep problem.
  31397. + */
  31398. + }
  31399. +
  31400. + if (!err && shwh)
  31401. + err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
  31402. +
  31403. + au_nhash_wh_free(&arg->whlist);
  31404. +
  31405. +out_delist:
  31406. + au_nhash_de_free(&arg->delist);
  31407. +out:
  31408. + return err;
  31409. +}
  31410. +
  31411. +static int read_vdir(struct file *file, int may_read)
  31412. +{
  31413. + int err;
  31414. + unsigned long expire;
  31415. + unsigned char do_read;
  31416. + struct fillvdir_arg arg = {
  31417. + .ctx = {
  31418. + .actor = fillvdir
  31419. + }
  31420. + };
  31421. + struct inode *inode;
  31422. + struct au_vdir *vdir, *allocated;
  31423. +
  31424. + err = 0;
  31425. + inode = file_inode(file);
  31426. + IMustLock(inode);
  31427. + IiMustWriteLock(inode);
  31428. + SiMustAnyLock(inode->i_sb);
  31429. +
  31430. + allocated = NULL;
  31431. + do_read = 0;
  31432. + expire = au_sbi(inode->i_sb)->si_rdcache;
  31433. + vdir = au_ivdir(inode);
  31434. + if (!vdir) {
  31435. + do_read = 1;
  31436. + vdir = alloc_vdir(file);
  31437. + err = PTR_ERR(vdir);
  31438. + if (IS_ERR(vdir))
  31439. + goto out;
  31440. + err = 0;
  31441. + allocated = vdir;
  31442. + } else if (may_read
  31443. + && (!inode_eq_iversion(inode, vdir->vd_version)
  31444. + || time_after(jiffies, vdir->vd_jiffy + expire))) {
  31445. + do_read = 1;
  31446. + err = reinit_vdir(vdir);
  31447. + if (unlikely(err))
  31448. + goto out;
  31449. + }
  31450. +
  31451. + if (!do_read)
  31452. + return 0; /* success */
  31453. +
  31454. + arg.file = file;
  31455. + arg.vdir = vdir;
  31456. + err = au_do_read_vdir(&arg);
  31457. + if (!err) {
  31458. + /* file->f_pos = 0; */ /* todo: ctx->pos? */
  31459. + vdir->vd_version = inode_query_iversion(inode);
  31460. + vdir->vd_last.ul = 0;
  31461. + vdir->vd_last.p.deblk = vdir->vd_deblk[0];
  31462. + if (allocated)
  31463. + au_set_ivdir(inode, allocated);
  31464. + } else if (allocated)
  31465. + au_vdir_free(allocated);
  31466. +
  31467. +out:
  31468. + return err;
  31469. +}
  31470. +
  31471. +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
  31472. +{
  31473. + int err, rerr;
  31474. + unsigned long ul, n;
  31475. + const unsigned int deblk_sz = src->vd_deblk_sz;
  31476. +
  31477. + AuDebugOn(tgt->vd_nblk != 1);
  31478. +
  31479. + err = -ENOMEM;
  31480. + if (tgt->vd_nblk < src->vd_nblk) {
  31481. + unsigned char **p;
  31482. +
  31483. + p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
  31484. + GFP_NOFS, /*may_shrink*/0);
  31485. + if (unlikely(!p))
  31486. + goto out;
  31487. + tgt->vd_deblk = p;
  31488. + }
  31489. +
  31490. + if (tgt->vd_deblk_sz != deblk_sz) {
  31491. + unsigned char *p;
  31492. +
  31493. + tgt->vd_deblk_sz = deblk_sz;
  31494. + p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
  31495. + /*may_shrink*/1);
  31496. + if (unlikely(!p))
  31497. + goto out;
  31498. + tgt->vd_deblk[0] = p;
  31499. + }
  31500. + memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
  31501. + tgt->vd_version = src->vd_version;
  31502. + tgt->vd_jiffy = src->vd_jiffy;
  31503. +
  31504. + n = src->vd_nblk;
  31505. + for (ul = 1; ul < n; ul++) {
  31506. + tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
  31507. + GFP_NOFS);
  31508. + if (unlikely(!tgt->vd_deblk[ul]))
  31509. + goto out;
  31510. + tgt->vd_nblk++;
  31511. + }
  31512. + tgt->vd_nblk = n;
  31513. + tgt->vd_last.ul = tgt->vd_last.ul;
  31514. + tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
  31515. + tgt->vd_last.p.deblk += src->vd_last.p.deblk
  31516. + - src->vd_deblk[src->vd_last.ul];
  31517. + /* smp_mb(); */
  31518. + return 0; /* success */
  31519. +
  31520. +out:
  31521. + rerr = reinit_vdir(tgt);
  31522. + BUG_ON(rerr);
  31523. + return err;
  31524. +}
  31525. +
  31526. +int au_vdir_init(struct file *file)
  31527. +{
  31528. + int err;
  31529. + struct inode *inode;
  31530. + struct au_vdir *vdir_cache, *allocated;
  31531. +
  31532. + /* test file->f_pos here instead of ctx->pos */
  31533. + err = read_vdir(file, !file->f_pos);
  31534. + if (unlikely(err))
  31535. + goto out;
  31536. +
  31537. + allocated = NULL;
  31538. + vdir_cache = au_fvdir_cache(file);
  31539. + if (!vdir_cache) {
  31540. + vdir_cache = alloc_vdir(file);
  31541. + err = PTR_ERR(vdir_cache);
  31542. + if (IS_ERR(vdir_cache))
  31543. + goto out;
  31544. + allocated = vdir_cache;
  31545. + } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
  31546. + /* test file->f_pos here instead of ctx->pos */
  31547. + err = reinit_vdir(vdir_cache);
  31548. + if (unlikely(err))
  31549. + goto out;
  31550. + } else
  31551. + return 0; /* success */
  31552. +
  31553. + inode = file_inode(file);
  31554. + err = copy_vdir(vdir_cache, au_ivdir(inode));
  31555. + if (!err) {
  31556. + file->f_version = inode_query_iversion(inode);
  31557. + if (allocated)
  31558. + au_set_fvdir_cache(file, allocated);
  31559. + } else if (allocated)
  31560. + au_vdir_free(allocated);
  31561. +
  31562. +out:
  31563. + return err;
  31564. +}
  31565. +
  31566. +static loff_t calc_offset(struct au_vdir *vdir)
  31567. +{
  31568. + loff_t offset;
  31569. + union au_vdir_deblk_p p;
  31570. +
  31571. + p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
  31572. + offset = vdir->vd_last.p.deblk - p.deblk;
  31573. + offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
  31574. + return offset;
  31575. +}
  31576. +
  31577. +/* returns true or false */
  31578. +static int seek_vdir(struct file *file, struct dir_context *ctx)
  31579. +{
  31580. + int valid;
  31581. + unsigned int deblk_sz;
  31582. + unsigned long ul, n;
  31583. + loff_t offset;
  31584. + union au_vdir_deblk_p p, deblk_end;
  31585. + struct au_vdir *vdir_cache;
  31586. +
  31587. + valid = 1;
  31588. + vdir_cache = au_fvdir_cache(file);
  31589. + offset = calc_offset(vdir_cache);
  31590. + AuDbg("offset %lld\n", offset);
  31591. + if (ctx->pos == offset)
  31592. + goto out;
  31593. +
  31594. + vdir_cache->vd_last.ul = 0;
  31595. + vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
  31596. + if (!ctx->pos)
  31597. + goto out;
  31598. +
  31599. + valid = 0;
  31600. + deblk_sz = vdir_cache->vd_deblk_sz;
  31601. + ul = div64_u64(ctx->pos, deblk_sz);
  31602. + AuDbg("ul %lu\n", ul);
  31603. + if (ul >= vdir_cache->vd_nblk)
  31604. + goto out;
  31605. +
  31606. + n = vdir_cache->vd_nblk;
  31607. + for (; ul < n; ul++) {
  31608. + p.deblk = vdir_cache->vd_deblk[ul];
  31609. + deblk_end.deblk = p.deblk + deblk_sz;
  31610. + offset = ul;
  31611. + offset *= deblk_sz;
  31612. + while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
  31613. + unsigned int l;
  31614. +
  31615. + l = calc_size(p.de->de_str.len);
  31616. + offset += l;
  31617. + p.deblk += l;
  31618. + }
  31619. + if (!is_deblk_end(&p, &deblk_end)) {
  31620. + valid = 1;
  31621. + vdir_cache->vd_last.ul = ul;
  31622. + vdir_cache->vd_last.p = p;
  31623. + break;
  31624. + }
  31625. + }
  31626. +
  31627. +out:
  31628. + /* smp_mb(); */
  31629. + if (!valid)
  31630. + AuDbg("valid %d\n", !valid);
  31631. + return valid;
  31632. +}
  31633. +
  31634. +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
  31635. +{
  31636. + unsigned int l, deblk_sz;
  31637. + union au_vdir_deblk_p deblk_end;
  31638. + struct au_vdir *vdir_cache;
  31639. + struct au_vdir_de *de;
  31640. +
  31641. + if (!seek_vdir(file, ctx))
  31642. + return 0;
  31643. +
  31644. + vdir_cache = au_fvdir_cache(file);
  31645. + deblk_sz = vdir_cache->vd_deblk_sz;
  31646. + while (1) {
  31647. + deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
  31648. + deblk_end.deblk += deblk_sz;
  31649. + while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
  31650. + de = vdir_cache->vd_last.p.de;
  31651. + AuDbg("%.*s, off%lld, i%lu, dt%d\n",
  31652. + de->de_str.len, de->de_str.name, ctx->pos,
  31653. + (unsigned long)de->de_ino, de->de_type);
  31654. + if (unlikely(!dir_emit(ctx, de->de_str.name,
  31655. + de->de_str.len, de->de_ino,
  31656. + de->de_type))) {
  31657. + /* todo: ignore the error caused by udba? */
  31658. + /* return err; */
  31659. + return 0;
  31660. + }
  31661. +
  31662. + l = calc_size(de->de_str.len);
  31663. + vdir_cache->vd_last.p.deblk += l;
  31664. + ctx->pos += l;
  31665. + }
  31666. + if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
  31667. + vdir_cache->vd_last.ul++;
  31668. + vdir_cache->vd_last.p.deblk
  31669. + = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
  31670. + ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
  31671. + continue;
  31672. + }
  31673. + break;
  31674. + }
  31675. +
  31676. + /* smp_mb(); */
  31677. + return 0;
  31678. +}
  31679. diff -Naur null/fs/aufs/vfsub.c linux-5.1/fs/aufs/vfsub.c
  31680. --- /dev/null
  31681. +++ linux-5.1/fs/aufs/vfsub.c 2019-05-22 11:23:07.443140904 +0200
  31682. @@ -0,0 +1,902 @@
  31683. +// SPDX-License-Identifier: GPL-2.0
  31684. +/*
  31685. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  31686. + *
  31687. + * This program, aufs is free software; you can redistribute it and/or modify
  31688. + * it under the terms of the GNU General Public License as published by
  31689. + * the Free Software Foundation; either version 2 of the License, or
  31690. + * (at your option) any later version.
  31691. + *
  31692. + * This program is distributed in the hope that it will be useful,
  31693. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31694. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  31695. + * GNU General Public License for more details.
  31696. + *
  31697. + * You should have received a copy of the GNU General Public License
  31698. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  31699. + */
  31700. +
  31701. +/*
  31702. + * sub-routines for VFS
  31703. + */
  31704. +
  31705. +#include <linux/mnt_namespace.h>
  31706. +#include <linux/namei.h>
  31707. +#include <linux/nsproxy.h>
  31708. +#include <linux/security.h>
  31709. +#include <linux/splice.h>
  31710. +#include "aufs.h"
  31711. +
  31712. +#ifdef CONFIG_AUFS_BR_FUSE
  31713. +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
  31714. +{
  31715. + if (!au_test_fuse(h_sb) || !au_userns)
  31716. + return 0;
  31717. +
  31718. + return is_current_mnt_ns(mnt) ? 0 : -EACCES;
  31719. +}
  31720. +#endif
  31721. +
  31722. +int vfsub_sync_filesystem(struct super_block *h_sb, int wait)
  31723. +{
  31724. + int err;
  31725. +
  31726. + lockdep_off();
  31727. + down_read(&h_sb->s_umount);
  31728. + err = __sync_filesystem(h_sb, wait);
  31729. + up_read(&h_sb->s_umount);
  31730. + lockdep_on();
  31731. +
  31732. + return err;
  31733. +}
  31734. +
  31735. +/* ---------------------------------------------------------------------- */
  31736. +
  31737. +int vfsub_update_h_iattr(struct path *h_path, int *did)
  31738. +{
  31739. + int err;
  31740. + struct kstat st;
  31741. + struct super_block *h_sb;
  31742. +
  31743. + /* for remote fs, leave work for its getattr or d_revalidate */
  31744. + /* for bad i_attr fs, handle them in aufs_getattr() */
  31745. + /* still some fs may acquire i_mutex. we need to skip them */
  31746. + err = 0;
  31747. + if (!did)
  31748. + did = &err;
  31749. + h_sb = h_path->dentry->d_sb;
  31750. + *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
  31751. + if (*did)
  31752. + err = vfsub_getattr(h_path, &st);
  31753. +
  31754. + return err;
  31755. +}
  31756. +
  31757. +/* ---------------------------------------------------------------------- */
  31758. +
  31759. +struct file *vfsub_dentry_open(struct path *path, int flags)
  31760. +{
  31761. + struct file *file;
  31762. +
  31763. + file = dentry_open(path, flags /* | __FMODE_NONOTIFY */,
  31764. + current_cred());
  31765. + if (!IS_ERR_OR_NULL(file)
  31766. + && (file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)
  31767. + i_readcount_inc(d_inode(path->dentry));
  31768. +
  31769. + return file;
  31770. +}
  31771. +
  31772. +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
  31773. +{
  31774. + struct file *file;
  31775. +
  31776. + lockdep_off();
  31777. + file = filp_open(path,
  31778. + oflags /* | __FMODE_NONOTIFY */,
  31779. + mode);
  31780. + lockdep_on();
  31781. + if (IS_ERR(file))
  31782. + goto out;
  31783. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  31784. +
  31785. +out:
  31786. + return file;
  31787. +}
  31788. +
  31789. +/*
  31790. + * Ideally this function should call VFS:do_last() in order to keep all its
  31791. + * checkings. But it is very hard for aufs to regenerate several VFS internal
  31792. + * structure such as nameidata. This is a second (or third) best approach.
  31793. + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
  31794. + */
  31795. +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
  31796. + struct vfsub_aopen_args *args)
  31797. +{
  31798. + int err;
  31799. + struct au_branch *br = args->br;
  31800. + struct file *file = args->file;
  31801. + /* copied from linux/fs/namei.c:atomic_open() */
  31802. + struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
  31803. +
  31804. + IMustLock(dir);
  31805. + AuDebugOn(!dir->i_op->atomic_open);
  31806. +
  31807. + err = au_br_test_oflag(args->open_flag, br);
  31808. + if (unlikely(err))
  31809. + goto out;
  31810. +
  31811. + au_lcnt_inc(&br->br_nfiles);
  31812. + file->f_path.dentry = DENTRY_NOT_SET;
  31813. + file->f_path.mnt = au_br_mnt(br);
  31814. + AuDbg("%ps\n", dir->i_op->atomic_open);
  31815. + err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
  31816. + args->create_mode);
  31817. + if (unlikely(err < 0)) {
  31818. + au_lcnt_dec(&br->br_nfiles);
  31819. + goto out;
  31820. + }
  31821. +
  31822. + /* temporary workaround for nfsv4 branch */
  31823. + if (au_test_nfs(dir->i_sb))
  31824. + nfs_mark_for_revalidate(dir);
  31825. +
  31826. + if (file->f_mode & FMODE_CREATED)
  31827. + fsnotify_create(dir, dentry);
  31828. + if (!(file->f_mode & FMODE_OPENED)) {
  31829. + au_lcnt_dec(&br->br_nfiles);
  31830. + goto out;
  31831. + }
  31832. +
  31833. + /* todo: call VFS:may_open() here */
  31834. + /* todo: ima_file_check() too? */
  31835. + if (!err && (args->open_flag & __FMODE_EXEC))
  31836. + err = deny_write_access(file);
  31837. + if (!err)
  31838. + fsnotify_open(file);
  31839. + else
  31840. + au_lcnt_dec(&br->br_nfiles);
  31841. + /* note that the file is created and still opened */
  31842. +
  31843. +out:
  31844. + return err;
  31845. +}
  31846. +
  31847. +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
  31848. +{
  31849. + int err;
  31850. +
  31851. + err = kern_path(name, flags, path);
  31852. + if (!err && d_is_positive(path->dentry))
  31853. + vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
  31854. + return err;
  31855. +}
  31856. +
  31857. +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
  31858. + struct dentry *parent, int len)
  31859. +{
  31860. + struct path path = {
  31861. + .mnt = NULL
  31862. + };
  31863. +
  31864. + path.dentry = lookup_one_len_unlocked(name, parent, len);
  31865. + if (IS_ERR(path.dentry))
  31866. + goto out;
  31867. + if (d_is_positive(path.dentry))
  31868. + vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
  31869. +
  31870. +out:
  31871. + AuTraceErrPtr(path.dentry);
  31872. + return path.dentry;
  31873. +}
  31874. +
  31875. +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
  31876. + int len)
  31877. +{
  31878. + struct path path = {
  31879. + .mnt = NULL
  31880. + };
  31881. +
  31882. + /* VFS checks it too, but by WARN_ON_ONCE() */
  31883. + IMustLock(d_inode(parent));
  31884. +
  31885. + path.dentry = lookup_one_len(name, parent, len);
  31886. + if (IS_ERR(path.dentry))
  31887. + goto out;
  31888. + if (d_is_positive(path.dentry))
  31889. + vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
  31890. +
  31891. +out:
  31892. + AuTraceErrPtr(path.dentry);
  31893. + return path.dentry;
  31894. +}
  31895. +
  31896. +void vfsub_call_lkup_one(void *args)
  31897. +{
  31898. + struct vfsub_lkup_one_args *a = args;
  31899. + *a->errp = vfsub_lkup_one(a->name, a->parent);
  31900. +}
  31901. +
  31902. +/* ---------------------------------------------------------------------- */
  31903. +
  31904. +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
  31905. + struct dentry *d2, struct au_hinode *hdir2)
  31906. +{
  31907. + struct dentry *d;
  31908. +
  31909. + lockdep_off();
  31910. + d = lock_rename(d1, d2);
  31911. + lockdep_on();
  31912. + au_hn_suspend(hdir1);
  31913. + if (hdir1 != hdir2)
  31914. + au_hn_suspend(hdir2);
  31915. +
  31916. + return d;
  31917. +}
  31918. +
  31919. +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
  31920. + struct dentry *d2, struct au_hinode *hdir2)
  31921. +{
  31922. + au_hn_resume(hdir1);
  31923. + if (hdir1 != hdir2)
  31924. + au_hn_resume(hdir2);
  31925. + lockdep_off();
  31926. + unlock_rename(d1, d2);
  31927. + lockdep_on();
  31928. +}
  31929. +
  31930. +/* ---------------------------------------------------------------------- */
  31931. +
  31932. +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
  31933. +{
  31934. + int err;
  31935. + struct dentry *d;
  31936. +
  31937. + IMustLock(dir);
  31938. +
  31939. + d = path->dentry;
  31940. + path->dentry = d->d_parent;
  31941. + err = security_path_mknod(path, d, mode, 0);
  31942. + path->dentry = d;
  31943. + if (unlikely(err))
  31944. + goto out;
  31945. +
  31946. + lockdep_off();
  31947. + err = vfs_create(dir, path->dentry, mode, want_excl);
  31948. + lockdep_on();
  31949. + if (!err) {
  31950. + struct path tmp = *path;
  31951. + int did;
  31952. +
  31953. + vfsub_update_h_iattr(&tmp, &did);
  31954. + if (did) {
  31955. + tmp.dentry = path->dentry->d_parent;
  31956. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  31957. + }
  31958. + /*ignore*/
  31959. + }
  31960. +
  31961. +out:
  31962. + return err;
  31963. +}
  31964. +
  31965. +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
  31966. +{
  31967. + int err;
  31968. + struct dentry *d;
  31969. +
  31970. + IMustLock(dir);
  31971. +
  31972. + d = path->dentry;
  31973. + path->dentry = d->d_parent;
  31974. + err = security_path_symlink(path, d, symname);
  31975. + path->dentry = d;
  31976. + if (unlikely(err))
  31977. + goto out;
  31978. +
  31979. + lockdep_off();
  31980. + err = vfs_symlink(dir, path->dentry, symname);
  31981. + lockdep_on();
  31982. + if (!err) {
  31983. + struct path tmp = *path;
  31984. + int did;
  31985. +
  31986. + vfsub_update_h_iattr(&tmp, &did);
  31987. + if (did) {
  31988. + tmp.dentry = path->dentry->d_parent;
  31989. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  31990. + }
  31991. + /*ignore*/
  31992. + }
  31993. +
  31994. +out:
  31995. + return err;
  31996. +}
  31997. +
  31998. +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
  31999. +{
  32000. + int err;
  32001. + struct dentry *d;
  32002. +
  32003. + IMustLock(dir);
  32004. +
  32005. + d = path->dentry;
  32006. + path->dentry = d->d_parent;
  32007. + err = security_path_mknod(path, d, mode, new_encode_dev(dev));
  32008. + path->dentry = d;
  32009. + if (unlikely(err))
  32010. + goto out;
  32011. +
  32012. + lockdep_off();
  32013. + err = vfs_mknod(dir, path->dentry, mode, dev);
  32014. + lockdep_on();
  32015. + if (!err) {
  32016. + struct path tmp = *path;
  32017. + int did;
  32018. +
  32019. + vfsub_update_h_iattr(&tmp, &did);
  32020. + if (did) {
  32021. + tmp.dentry = path->dentry->d_parent;
  32022. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  32023. + }
  32024. + /*ignore*/
  32025. + }
  32026. +
  32027. +out:
  32028. + return err;
  32029. +}
  32030. +
  32031. +static int au_test_nlink(struct inode *inode)
  32032. +{
  32033. + const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
  32034. +
  32035. + if (!au_test_fs_no_limit_nlink(inode->i_sb)
  32036. + || inode->i_nlink < link_max)
  32037. + return 0;
  32038. + return -EMLINK;
  32039. +}
  32040. +
  32041. +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
  32042. + struct inode **delegated_inode)
  32043. +{
  32044. + int err;
  32045. + struct dentry *d;
  32046. +
  32047. + IMustLock(dir);
  32048. +
  32049. + err = au_test_nlink(d_inode(src_dentry));
  32050. + if (unlikely(err))
  32051. + return err;
  32052. +
  32053. + /* we don't call may_linkat() */
  32054. + d = path->dentry;
  32055. + path->dentry = d->d_parent;
  32056. + err = security_path_link(src_dentry, path, d);
  32057. + path->dentry = d;
  32058. + if (unlikely(err))
  32059. + goto out;
  32060. +
  32061. + lockdep_off();
  32062. + err = vfs_link(src_dentry, dir, path->dentry, delegated_inode);
  32063. + lockdep_on();
  32064. + if (!err) {
  32065. + struct path tmp = *path;
  32066. + int did;
  32067. +
  32068. + /* fuse has different memory inode for the same inumber */
  32069. + vfsub_update_h_iattr(&tmp, &did);
  32070. + if (did) {
  32071. + tmp.dentry = path->dentry->d_parent;
  32072. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  32073. + tmp.dentry = src_dentry;
  32074. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  32075. + }
  32076. + /*ignore*/
  32077. + }
  32078. +
  32079. +out:
  32080. + return err;
  32081. +}
  32082. +
  32083. +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
  32084. + struct inode *dir, struct path *path,
  32085. + struct inode **delegated_inode, unsigned int flags)
  32086. +{
  32087. + int err;
  32088. + struct path tmp = {
  32089. + .mnt = path->mnt
  32090. + };
  32091. + struct dentry *d;
  32092. +
  32093. + IMustLock(dir);
  32094. + IMustLock(src_dir);
  32095. +
  32096. + d = path->dentry;
  32097. + path->dentry = d->d_parent;
  32098. + tmp.dentry = src_dentry->d_parent;
  32099. + err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
  32100. + path->dentry = d;
  32101. + if (unlikely(err))
  32102. + goto out;
  32103. +
  32104. + lockdep_off();
  32105. + err = vfs_rename(src_dir, src_dentry, dir, path->dentry,
  32106. + delegated_inode, flags);
  32107. + lockdep_on();
  32108. + if (!err) {
  32109. + int did;
  32110. +
  32111. + tmp.dentry = d->d_parent;
  32112. + vfsub_update_h_iattr(&tmp, &did);
  32113. + if (did) {
  32114. + tmp.dentry = src_dentry;
  32115. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  32116. + tmp.dentry = src_dentry->d_parent;
  32117. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  32118. + }
  32119. + /*ignore*/
  32120. + }
  32121. +
  32122. +out:
  32123. + return err;
  32124. +}
  32125. +
  32126. +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
  32127. +{
  32128. + int err;
  32129. + struct dentry *d;
  32130. +
  32131. + IMustLock(dir);
  32132. +
  32133. + d = path->dentry;
  32134. + path->dentry = d->d_parent;
  32135. + err = security_path_mkdir(path, d, mode);
  32136. + path->dentry = d;
  32137. + if (unlikely(err))
  32138. + goto out;
  32139. +
  32140. + lockdep_off();
  32141. + err = vfs_mkdir(dir, path->dentry, mode);
  32142. + lockdep_on();
  32143. + if (!err) {
  32144. + struct path tmp = *path;
  32145. + int did;
  32146. +
  32147. + vfsub_update_h_iattr(&tmp, &did);
  32148. + if (did) {
  32149. + tmp.dentry = path->dentry->d_parent;
  32150. + vfsub_update_h_iattr(&tmp, /*did*/NULL);
  32151. + }
  32152. + /*ignore*/
  32153. + }
  32154. +
  32155. +out:
  32156. + return err;
  32157. +}
  32158. +
  32159. +int vfsub_rmdir(struct inode *dir, struct path *path)
  32160. +{
  32161. + int err;
  32162. + struct dentry *d;
  32163. +
  32164. + IMustLock(dir);
  32165. +
  32166. + d = path->dentry;
  32167. + path->dentry = d->d_parent;
  32168. + err = security_path_rmdir(path, d);
  32169. + path->dentry = d;
  32170. + if (unlikely(err))
  32171. + goto out;
  32172. +
  32173. + lockdep_off();
  32174. + err = vfs_rmdir(dir, path->dentry);
  32175. + lockdep_on();
  32176. + if (!err) {
  32177. + struct path tmp = {
  32178. + .dentry = path->dentry->d_parent,
  32179. + .mnt = path->mnt
  32180. + };
  32181. +
  32182. + vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
  32183. + }
  32184. +
  32185. +out:
  32186. + return err;
  32187. +}
  32188. +
  32189. +/* ---------------------------------------------------------------------- */
  32190. +
  32191. +/* todo: support mmap_sem? */
  32192. +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
  32193. + loff_t *ppos)
  32194. +{
  32195. + ssize_t err;
  32196. +
  32197. + lockdep_off();
  32198. + err = vfs_read(file, ubuf, count, ppos);
  32199. + lockdep_on();
  32200. + if (err >= 0)
  32201. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  32202. + return err;
  32203. +}
  32204. +
  32205. +/* todo: kernel_read()? */
  32206. +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
  32207. + loff_t *ppos)
  32208. +{
  32209. + ssize_t err;
  32210. + mm_segment_t oldfs;
  32211. + union {
  32212. + void *k;
  32213. + char __user *u;
  32214. + } buf;
  32215. +
  32216. + buf.k = kbuf;
  32217. + oldfs = get_fs();
  32218. + set_fs(KERNEL_DS);
  32219. + err = vfsub_read_u(file, buf.u, count, ppos);
  32220. + set_fs(oldfs);
  32221. + return err;
  32222. +}
  32223. +
  32224. +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
  32225. + loff_t *ppos)
  32226. +{
  32227. + ssize_t err;
  32228. +
  32229. + lockdep_off();
  32230. + err = vfs_write(file, ubuf, count, ppos);
  32231. + lockdep_on();
  32232. + if (err >= 0)
  32233. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  32234. + return err;
  32235. +}
  32236. +
  32237. +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
  32238. +{
  32239. + ssize_t err;
  32240. + mm_segment_t oldfs;
  32241. + union {
  32242. + void *k;
  32243. + const char __user *u;
  32244. + } buf;
  32245. +
  32246. + buf.k = kbuf;
  32247. + oldfs = get_fs();
  32248. + set_fs(KERNEL_DS);
  32249. + err = vfsub_write_u(file, buf.u, count, ppos);
  32250. + set_fs(oldfs);
  32251. + return err;
  32252. +}
  32253. +
  32254. +int vfsub_flush(struct file *file, fl_owner_t id)
  32255. +{
  32256. + int err;
  32257. +
  32258. + err = 0;
  32259. + if (file->f_op->flush) {
  32260. + if (!au_test_nfs(file->f_path.dentry->d_sb))
  32261. + err = file->f_op->flush(file, id);
  32262. + else {
  32263. + lockdep_off();
  32264. + err = file->f_op->flush(file, id);
  32265. + lockdep_on();
  32266. + }
  32267. + if (!err)
  32268. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
  32269. + /*ignore*/
  32270. + }
  32271. + return err;
  32272. +}
  32273. +
  32274. +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
  32275. +{
  32276. + int err;
  32277. +
  32278. + AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
  32279. +
  32280. + lockdep_off();
  32281. + err = iterate_dir(file, ctx);
  32282. + lockdep_on();
  32283. + if (err >= 0)
  32284. + vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
  32285. +
  32286. + return err;
  32287. +}
  32288. +
  32289. +long vfsub_splice_to(struct file *in, loff_t *ppos,
  32290. + struct pipe_inode_info *pipe, size_t len,
  32291. + unsigned int flags)
  32292. +{
  32293. + long err;
  32294. +
  32295. + lockdep_off();
  32296. + err = do_splice_to(in, ppos, pipe, len, flags);
  32297. + lockdep_on();
  32298. + file_accessed(in);
  32299. + if (err >= 0)
  32300. + vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
  32301. + return err;
  32302. +}
  32303. +
  32304. +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
  32305. + loff_t *ppos, size_t len, unsigned int flags)
  32306. +{
  32307. + long err;
  32308. +
  32309. + lockdep_off();
  32310. + err = do_splice_from(pipe, out, ppos, len, flags);
  32311. + lockdep_on();
  32312. + if (err >= 0)
  32313. + vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
  32314. + return err;
  32315. +}
  32316. +
  32317. +int vfsub_fsync(struct file *file, struct path *path, int datasync)
  32318. +{
  32319. + int err;
  32320. +
  32321. + /* file can be NULL */
  32322. + lockdep_off();
  32323. + err = vfs_fsync(file, datasync);
  32324. + lockdep_on();
  32325. + if (!err) {
  32326. + if (!path) {
  32327. + AuDebugOn(!file);
  32328. + path = &file->f_path;
  32329. + }
  32330. + vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
  32331. + }
  32332. + return err;
  32333. +}
  32334. +
  32335. +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
  32336. +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
  32337. + struct file *h_file)
  32338. +{
  32339. + int err;
  32340. + struct inode *h_inode;
  32341. + struct super_block *h_sb;
  32342. +
  32343. + if (!h_file) {
  32344. + err = vfsub_truncate(h_path, length);
  32345. + goto out;
  32346. + }
  32347. +
  32348. + h_inode = d_inode(h_path->dentry);
  32349. + h_sb = h_inode->i_sb;
  32350. + lockdep_off();
  32351. + sb_start_write(h_sb);
  32352. + lockdep_on();
  32353. + err = locks_verify_truncate(h_inode, h_file, length);
  32354. + if (!err)
  32355. + err = security_path_truncate(h_path);
  32356. + if (!err) {
  32357. + lockdep_off();
  32358. + err = do_truncate(h_path->dentry, length, attr, h_file);
  32359. + lockdep_on();
  32360. + }
  32361. + lockdep_off();
  32362. + sb_end_write(h_sb);
  32363. + lockdep_on();
  32364. +
  32365. +out:
  32366. + return err;
  32367. +}
  32368. +
  32369. +/* ---------------------------------------------------------------------- */
  32370. +
  32371. +struct au_vfsub_mkdir_args {
  32372. + int *errp;
  32373. + struct inode *dir;
  32374. + struct path *path;
  32375. + int mode;
  32376. +};
  32377. +
  32378. +static void au_call_vfsub_mkdir(void *args)
  32379. +{
  32380. + struct au_vfsub_mkdir_args *a = args;
  32381. + *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
  32382. +}
  32383. +
  32384. +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
  32385. +{
  32386. + int err, do_sio, wkq_err;
  32387. +
  32388. + do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
  32389. + if (!do_sio) {
  32390. + lockdep_off();
  32391. + err = vfsub_mkdir(dir, path, mode);
  32392. + lockdep_on();
  32393. + } else {
  32394. + struct au_vfsub_mkdir_args args = {
  32395. + .errp = &err,
  32396. + .dir = dir,
  32397. + .path = path,
  32398. + .mode = mode
  32399. + };
  32400. + wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
  32401. + if (unlikely(wkq_err))
  32402. + err = wkq_err;
  32403. + }
  32404. +
  32405. + return err;
  32406. +}
  32407. +
  32408. +struct au_vfsub_rmdir_args {
  32409. + int *errp;
  32410. + struct inode *dir;
  32411. + struct path *path;
  32412. +};
  32413. +
  32414. +static void au_call_vfsub_rmdir(void *args)
  32415. +{
  32416. + struct au_vfsub_rmdir_args *a = args;
  32417. + *a->errp = vfsub_rmdir(a->dir, a->path);
  32418. +}
  32419. +
  32420. +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
  32421. +{
  32422. + int err, do_sio, wkq_err;
  32423. +
  32424. + do_sio = au_test_h_perm_sio(dir, MAY_EXEC | MAY_WRITE);
  32425. + if (!do_sio) {
  32426. + lockdep_off();
  32427. + err = vfsub_rmdir(dir, path);
  32428. + lockdep_on();
  32429. + } else {
  32430. + struct au_vfsub_rmdir_args args = {
  32431. + .errp = &err,
  32432. + .dir = dir,
  32433. + .path = path
  32434. + };
  32435. + wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
  32436. + if (unlikely(wkq_err))
  32437. + err = wkq_err;
  32438. + }
  32439. +
  32440. + return err;
  32441. +}
  32442. +
  32443. +/* ---------------------------------------------------------------------- */
  32444. +
  32445. +struct notify_change_args {
  32446. + int *errp;
  32447. + struct path *path;
  32448. + struct iattr *ia;
  32449. + struct inode **delegated_inode;
  32450. +};
  32451. +
  32452. +static void call_notify_change(void *args)
  32453. +{
  32454. + struct notify_change_args *a = args;
  32455. + struct inode *h_inode;
  32456. +
  32457. + h_inode = d_inode(a->path->dentry);
  32458. + IMustLock(h_inode);
  32459. +
  32460. + *a->errp = -EPERM;
  32461. + if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
  32462. + lockdep_off();
  32463. + *a->errp = notify_change(a->path->dentry, a->ia,
  32464. + a->delegated_inode);
  32465. + lockdep_on();
  32466. + if (!*a->errp)
  32467. + vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
  32468. + }
  32469. + AuTraceErr(*a->errp);
  32470. +}
  32471. +
  32472. +int vfsub_notify_change(struct path *path, struct iattr *ia,
  32473. + struct inode **delegated_inode)
  32474. +{
  32475. + int err;
  32476. + struct notify_change_args args = {
  32477. + .errp = &err,
  32478. + .path = path,
  32479. + .ia = ia,
  32480. + .delegated_inode = delegated_inode
  32481. + };
  32482. +
  32483. + call_notify_change(&args);
  32484. +
  32485. + return err;
  32486. +}
  32487. +
  32488. +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
  32489. + struct inode **delegated_inode)
  32490. +{
  32491. + int err, wkq_err;
  32492. + struct notify_change_args args = {
  32493. + .errp = &err,
  32494. + .path = path,
  32495. + .ia = ia,
  32496. + .delegated_inode = delegated_inode
  32497. + };
  32498. +
  32499. + wkq_err = au_wkq_wait(call_notify_change, &args);
  32500. + if (unlikely(wkq_err))
  32501. + err = wkq_err;
  32502. +
  32503. + return err;
  32504. +}
  32505. +
  32506. +/* ---------------------------------------------------------------------- */
  32507. +
  32508. +struct unlink_args {
  32509. + int *errp;
  32510. + struct inode *dir;
  32511. + struct path *path;
  32512. + struct inode **delegated_inode;
  32513. +};
  32514. +
  32515. +static void call_unlink(void *args)
  32516. +{
  32517. + struct unlink_args *a = args;
  32518. + struct dentry *d = a->path->dentry;
  32519. + struct inode *h_inode;
  32520. + const int stop_sillyrename = (au_test_nfs(d->d_sb)
  32521. + && au_dcount(d) == 1);
  32522. +
  32523. + IMustLock(a->dir);
  32524. +
  32525. + a->path->dentry = d->d_parent;
  32526. + *a->errp = security_path_unlink(a->path, d);
  32527. + a->path->dentry = d;
  32528. + if (unlikely(*a->errp))
  32529. + return;
  32530. +
  32531. + if (!stop_sillyrename)
  32532. + dget(d);
  32533. + h_inode = NULL;
  32534. + if (d_is_positive(d)) {
  32535. + h_inode = d_inode(d);
  32536. + ihold(h_inode);
  32537. + }
  32538. +
  32539. + lockdep_off();
  32540. + *a->errp = vfs_unlink(a->dir, d, a->delegated_inode);
  32541. + lockdep_on();
  32542. + if (!*a->errp) {
  32543. + struct path tmp = {
  32544. + .dentry = d->d_parent,
  32545. + .mnt = a->path->mnt
  32546. + };
  32547. + vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
  32548. + }
  32549. +
  32550. + if (!stop_sillyrename)
  32551. + dput(d);
  32552. + if (h_inode)
  32553. + iput(h_inode);
  32554. +
  32555. + AuTraceErr(*a->errp);
  32556. +}
  32557. +
  32558. +/*
  32559. + * @dir: must be locked.
  32560. + * @dentry: target dentry.
  32561. + */
  32562. +int vfsub_unlink(struct inode *dir, struct path *path,
  32563. + struct inode **delegated_inode, int force)
  32564. +{
  32565. + int err;
  32566. + struct unlink_args args = {
  32567. + .errp = &err,
  32568. + .dir = dir,
  32569. + .path = path,
  32570. + .delegated_inode = delegated_inode
  32571. + };
  32572. +
  32573. + if (!force)
  32574. + call_unlink(&args);
  32575. + else {
  32576. + int wkq_err;
  32577. +
  32578. + wkq_err = au_wkq_wait(call_unlink, &args);
  32579. + if (unlikely(wkq_err))
  32580. + err = wkq_err;
  32581. + }
  32582. +
  32583. + return err;
  32584. +}
  32585. diff -Naur null/fs/aufs/vfsub.h linux-5.1/fs/aufs/vfsub.h
  32586. --- /dev/null
  32587. +++ linux-5.1/fs/aufs/vfsub.h 2019-05-22 11:23:07.443140904 +0200
  32588. @@ -0,0 +1,354 @@
  32589. +/* SPDX-License-Identifier: GPL-2.0 */
  32590. +/*
  32591. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  32592. + *
  32593. + * This program, aufs is free software; you can redistribute it and/or modify
  32594. + * it under the terms of the GNU General Public License as published by
  32595. + * the Free Software Foundation; either version 2 of the License, or
  32596. + * (at your option) any later version.
  32597. + *
  32598. + * This program is distributed in the hope that it will be useful,
  32599. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32600. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32601. + * GNU General Public License for more details.
  32602. + *
  32603. + * You should have received a copy of the GNU General Public License
  32604. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  32605. + */
  32606. +
  32607. +/*
  32608. + * sub-routines for VFS
  32609. + */
  32610. +
  32611. +#ifndef __AUFS_VFSUB_H__
  32612. +#define __AUFS_VFSUB_H__
  32613. +
  32614. +#ifdef __KERNEL__
  32615. +
  32616. +#include <linux/fs.h>
  32617. +#include <linux/mount.h>
  32618. +#include <linux/posix_acl.h>
  32619. +#include <linux/xattr.h>
  32620. +#include "debug.h"
  32621. +
  32622. +/* copied from linux/fs/internal.h */
  32623. +/* todo: BAD approach!! */
  32624. +extern void __mnt_drop_write(struct vfsmount *);
  32625. +extern struct file *alloc_empty_file(int, const struct cred *);
  32626. +
  32627. +/* ---------------------------------------------------------------------- */
  32628. +
  32629. +/* lock subclass for lower inode */
  32630. +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
  32631. +/* reduce? gave up. */
  32632. +enum {
  32633. + AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
  32634. + AuLsc_I_PARENT, /* lower inode, parent first */
  32635. + AuLsc_I_PARENT2, /* copyup dirs */
  32636. + AuLsc_I_PARENT3, /* copyup wh */
  32637. + AuLsc_I_CHILD,
  32638. + AuLsc_I_CHILD2,
  32639. + AuLsc_I_End
  32640. +};
  32641. +
  32642. +/* to debug easier, do not make them inlined functions */
  32643. +#define MtxMustLock(mtx) AuDebugOn(!mutex_is_locked(mtx))
  32644. +#define IMustLock(i) AuDebugOn(!inode_is_locked(i))
  32645. +
  32646. +/* ---------------------------------------------------------------------- */
  32647. +
  32648. +static inline void vfsub_drop_nlink(struct inode *inode)
  32649. +{
  32650. + AuDebugOn(!inode->i_nlink);
  32651. + drop_nlink(inode);
  32652. +}
  32653. +
  32654. +static inline void vfsub_dead_dir(struct inode *inode)
  32655. +{
  32656. + AuDebugOn(!S_ISDIR(inode->i_mode));
  32657. + inode->i_flags |= S_DEAD;
  32658. + clear_nlink(inode);
  32659. +}
  32660. +
  32661. +static inline int vfsub_native_ro(struct inode *inode)
  32662. +{
  32663. + return sb_rdonly(inode->i_sb)
  32664. + || IS_RDONLY(inode)
  32665. + /* || IS_APPEND(inode) */
  32666. + || IS_IMMUTABLE(inode);
  32667. +}
  32668. +
  32669. +#ifdef CONFIG_AUFS_BR_FUSE
  32670. +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
  32671. +#else
  32672. +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
  32673. +#endif
  32674. +
  32675. +int vfsub_sync_filesystem(struct super_block *h_sb, int wait);
  32676. +
  32677. +/* ---------------------------------------------------------------------- */
  32678. +
  32679. +int vfsub_update_h_iattr(struct path *h_path, int *did);
  32680. +struct file *vfsub_dentry_open(struct path *path, int flags);
  32681. +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
  32682. +struct au_branch;
  32683. +struct vfsub_aopen_args {
  32684. + struct file *file;
  32685. + unsigned int open_flag;
  32686. + umode_t create_mode;
  32687. + struct au_branch *br;
  32688. +};
  32689. +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
  32690. + struct vfsub_aopen_args *args);
  32691. +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
  32692. +
  32693. +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
  32694. + struct dentry *parent, int len);
  32695. +struct dentry *vfsub_lookup_one_len(const char *name, struct dentry *parent,
  32696. + int len);
  32697. +
  32698. +struct vfsub_lkup_one_args {
  32699. + struct dentry **errp;
  32700. + struct qstr *name;
  32701. + struct dentry *parent;
  32702. +};
  32703. +
  32704. +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
  32705. + struct dentry *parent)
  32706. +{
  32707. + return vfsub_lookup_one_len(name->name, parent, name->len);
  32708. +}
  32709. +
  32710. +void vfsub_call_lkup_one(void *args);
  32711. +
  32712. +/* ---------------------------------------------------------------------- */
  32713. +
  32714. +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
  32715. +{
  32716. + int err;
  32717. +
  32718. + lockdep_off();
  32719. + err = mnt_want_write(mnt);
  32720. + lockdep_on();
  32721. + return err;
  32722. +}
  32723. +
  32724. +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
  32725. +{
  32726. + lockdep_off();
  32727. + mnt_drop_write(mnt);
  32728. + lockdep_on();
  32729. +}
  32730. +
  32731. +#if 0 /* reserved */
  32732. +static inline void vfsub_mnt_drop_write_file(struct file *file)
  32733. +{
  32734. + lockdep_off();
  32735. + mnt_drop_write_file(file);
  32736. + lockdep_on();
  32737. +}
  32738. +#endif
  32739. +
  32740. +/* ---------------------------------------------------------------------- */
  32741. +
  32742. +struct au_hinode;
  32743. +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
  32744. + struct dentry *d2, struct au_hinode *hdir2);
  32745. +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
  32746. + struct dentry *d2, struct au_hinode *hdir2);
  32747. +
  32748. +int vfsub_create(struct inode *dir, struct path *path, int mode,
  32749. + bool want_excl);
  32750. +int vfsub_symlink(struct inode *dir, struct path *path,
  32751. + const char *symname);
  32752. +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
  32753. +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
  32754. + struct path *path, struct inode **delegated_inode);
  32755. +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
  32756. + struct inode *hdir, struct path *path,
  32757. + struct inode **delegated_inode, unsigned int flags);
  32758. +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
  32759. +int vfsub_rmdir(struct inode *dir, struct path *path);
  32760. +
  32761. +/* ---------------------------------------------------------------------- */
  32762. +
  32763. +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
  32764. + loff_t *ppos);
  32765. +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
  32766. + loff_t *ppos);
  32767. +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
  32768. + loff_t *ppos);
  32769. +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
  32770. + loff_t *ppos);
  32771. +int vfsub_flush(struct file *file, fl_owner_t id);
  32772. +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
  32773. +
  32774. +static inline loff_t vfsub_f_size_read(struct file *file)
  32775. +{
  32776. + return i_size_read(file_inode(file));
  32777. +}
  32778. +
  32779. +static inline unsigned int vfsub_file_flags(struct file *file)
  32780. +{
  32781. + unsigned int flags;
  32782. +
  32783. + spin_lock(&file->f_lock);
  32784. + flags = file->f_flags;
  32785. + spin_unlock(&file->f_lock);
  32786. +
  32787. + return flags;
  32788. +}
  32789. +
  32790. +static inline int vfsub_file_execed(struct file *file)
  32791. +{
  32792. + /* todo: direct access f_flags */
  32793. + return !!(vfsub_file_flags(file) & __FMODE_EXEC);
  32794. +}
  32795. +
  32796. +#if 0 /* reserved */
  32797. +static inline void vfsub_file_accessed(struct file *h_file)
  32798. +{
  32799. + file_accessed(h_file);
  32800. + vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
  32801. +}
  32802. +#endif
  32803. +
  32804. +#if 0 /* reserved */
  32805. +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
  32806. + struct dentry *h_dentry)
  32807. +{
  32808. + struct path h_path = {
  32809. + .dentry = h_dentry,
  32810. + .mnt = h_mnt
  32811. + };
  32812. + touch_atime(&h_path);
  32813. + vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
  32814. +}
  32815. +#endif
  32816. +
  32817. +static inline int vfsub_update_time(struct inode *h_inode,
  32818. + struct timespec64 *ts, int flags)
  32819. +{
  32820. + return update_time(h_inode, ts, flags);
  32821. + /* no vfsub_update_h_iattr() since we don't have struct path */
  32822. +}
  32823. +
  32824. +#ifdef CONFIG_FS_POSIX_ACL
  32825. +static inline int vfsub_acl_chmod(struct inode *h_inode, umode_t h_mode)
  32826. +{
  32827. + int err;
  32828. +
  32829. + err = posix_acl_chmod(h_inode, h_mode);
  32830. + if (err == -EOPNOTSUPP)
  32831. + err = 0;
  32832. + return err;
  32833. +}
  32834. +#else
  32835. +AuStubInt0(vfsub_acl_chmod, struct inode *h_inode, umode_t h_mode);
  32836. +#endif
  32837. +
  32838. +long vfsub_splice_to(struct file *in, loff_t *ppos,
  32839. + struct pipe_inode_info *pipe, size_t len,
  32840. + unsigned int flags);
  32841. +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
  32842. + loff_t *ppos, size_t len, unsigned int flags);
  32843. +
  32844. +static inline long vfsub_truncate(struct path *path, loff_t length)
  32845. +{
  32846. + long err;
  32847. +
  32848. + lockdep_off();
  32849. + err = vfs_truncate(path, length);
  32850. + lockdep_on();
  32851. + return err;
  32852. +}
  32853. +
  32854. +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
  32855. + struct file *h_file);
  32856. +int vfsub_fsync(struct file *file, struct path *path, int datasync);
  32857. +
  32858. +/*
  32859. + * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
  32860. + * ioctl.
  32861. + */
  32862. +static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
  32863. + loff_t len)
  32864. +{
  32865. + loff_t err;
  32866. +
  32867. + lockdep_off();
  32868. + err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
  32869. + lockdep_on();
  32870. +
  32871. + return err;
  32872. +}
  32873. +
  32874. +/* copy_file_range(2) is a systemcall */
  32875. +static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
  32876. + struct file *dst, loff_t dst_pos,
  32877. + size_t len, unsigned int flags)
  32878. +{
  32879. + ssize_t ssz;
  32880. +
  32881. + lockdep_off();
  32882. + ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
  32883. + lockdep_on();
  32884. +
  32885. + return ssz;
  32886. +}
  32887. +
  32888. +/* ---------------------------------------------------------------------- */
  32889. +
  32890. +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
  32891. +{
  32892. + loff_t err;
  32893. +
  32894. + lockdep_off();
  32895. + err = vfs_llseek(file, offset, origin);
  32896. + lockdep_on();
  32897. + return err;
  32898. +}
  32899. +
  32900. +/* ---------------------------------------------------------------------- */
  32901. +
  32902. +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
  32903. +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
  32904. +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
  32905. + struct inode **delegated_inode);
  32906. +int vfsub_notify_change(struct path *path, struct iattr *ia,
  32907. + struct inode **delegated_inode);
  32908. +int vfsub_unlink(struct inode *dir, struct path *path,
  32909. + struct inode **delegated_inode, int force);
  32910. +
  32911. +static inline int vfsub_getattr(const struct path *path, struct kstat *st)
  32912. +{
  32913. + return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
  32914. +}
  32915. +
  32916. +/* ---------------------------------------------------------------------- */
  32917. +
  32918. +static inline int vfsub_setxattr(struct dentry *dentry, const char *name,
  32919. + const void *value, size_t size, int flags)
  32920. +{
  32921. + int err;
  32922. +
  32923. + lockdep_off();
  32924. + err = vfs_setxattr(dentry, name, value, size, flags);
  32925. + lockdep_on();
  32926. +
  32927. + return err;
  32928. +}
  32929. +
  32930. +static inline int vfsub_removexattr(struct dentry *dentry, const char *name)
  32931. +{
  32932. + int err;
  32933. +
  32934. + lockdep_off();
  32935. + err = vfs_removexattr(dentry, name);
  32936. + lockdep_on();
  32937. +
  32938. + return err;
  32939. +}
  32940. +
  32941. +#endif /* __KERNEL__ */
  32942. +#endif /* __AUFS_VFSUB_H__ */
  32943. diff -Naur null/fs/aufs/wbr_policy.c linux-5.1/fs/aufs/wbr_policy.c
  32944. --- /dev/null
  32945. +++ linux-5.1/fs/aufs/wbr_policy.c 2019-05-22 11:23:07.443140904 +0200
  32946. @@ -0,0 +1,830 @@
  32947. +// SPDX-License-Identifier: GPL-2.0
  32948. +/*
  32949. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  32950. + *
  32951. + * This program, aufs is free software; you can redistribute it and/or modify
  32952. + * it under the terms of the GNU General Public License as published by
  32953. + * the Free Software Foundation; either version 2 of the License, or
  32954. + * (at your option) any later version.
  32955. + *
  32956. + * This program is distributed in the hope that it will be useful,
  32957. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32958. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32959. + * GNU General Public License for more details.
  32960. + *
  32961. + * You should have received a copy of the GNU General Public License
  32962. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  32963. + */
  32964. +
  32965. +/*
  32966. + * policies for selecting one among multiple writable branches
  32967. + */
  32968. +
  32969. +#include <linux/statfs.h>
  32970. +#include "aufs.h"
  32971. +
  32972. +/* subset of cpup_attr() */
  32973. +static noinline_for_stack
  32974. +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
  32975. +{
  32976. + int err, sbits;
  32977. + struct iattr ia;
  32978. + struct inode *h_isrc;
  32979. +
  32980. + h_isrc = d_inode(h_src);
  32981. + ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
  32982. + ia.ia_mode = h_isrc->i_mode;
  32983. + ia.ia_uid = h_isrc->i_uid;
  32984. + ia.ia_gid = h_isrc->i_gid;
  32985. + sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
  32986. + au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
  32987. + /* no delegation since it is just created */
  32988. + err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
  32989. +
  32990. + /* is this nfs only? */
  32991. + if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
  32992. + ia.ia_valid = ATTR_FORCE | ATTR_MODE;
  32993. + ia.ia_mode = h_isrc->i_mode;
  32994. + err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
  32995. + }
  32996. +
  32997. + return err;
  32998. +}
  32999. +
  33000. +#define AuCpdown_PARENT_OPQ 1
  33001. +#define AuCpdown_WHED (1 << 1)
  33002. +#define AuCpdown_MADE_DIR (1 << 2)
  33003. +#define AuCpdown_DIROPQ (1 << 3)
  33004. +#define au_ftest_cpdown(flags, name) ((flags) & AuCpdown_##name)
  33005. +#define au_fset_cpdown(flags, name) \
  33006. + do { (flags) |= AuCpdown_##name; } while (0)
  33007. +#define au_fclr_cpdown(flags, name) \
  33008. + do { (flags) &= ~AuCpdown_##name; } while (0)
  33009. +
  33010. +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
  33011. + unsigned int *flags)
  33012. +{
  33013. + int err;
  33014. + struct dentry *opq_dentry;
  33015. +
  33016. + opq_dentry = au_diropq_create(dentry, bdst);
  33017. + err = PTR_ERR(opq_dentry);
  33018. + if (IS_ERR(opq_dentry))
  33019. + goto out;
  33020. + dput(opq_dentry);
  33021. + au_fset_cpdown(*flags, DIROPQ);
  33022. +
  33023. +out:
  33024. + return err;
  33025. +}
  33026. +
  33027. +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
  33028. + struct inode *dir, aufs_bindex_t bdst)
  33029. +{
  33030. + int err;
  33031. + struct path h_path;
  33032. + struct au_branch *br;
  33033. +
  33034. + br = au_sbr(dentry->d_sb, bdst);
  33035. + h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
  33036. + err = PTR_ERR(h_path.dentry);
  33037. + if (IS_ERR(h_path.dentry))
  33038. + goto out;
  33039. +
  33040. + err = 0;
  33041. + if (d_is_positive(h_path.dentry)) {
  33042. + h_path.mnt = au_br_mnt(br);
  33043. + err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
  33044. + dentry);
  33045. + }
  33046. + dput(h_path.dentry);
  33047. +
  33048. +out:
  33049. + return err;
  33050. +}
  33051. +
  33052. +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
  33053. + struct au_pin *pin,
  33054. + struct dentry *h_parent, void *arg)
  33055. +{
  33056. + int err, rerr;
  33057. + aufs_bindex_t bopq, btop;
  33058. + struct path h_path;
  33059. + struct dentry *parent;
  33060. + struct inode *h_dir, *h_inode, *inode, *dir;
  33061. + unsigned int *flags = arg;
  33062. +
  33063. + btop = au_dbtop(dentry);
  33064. + /* dentry is di-locked */
  33065. + parent = dget_parent(dentry);
  33066. + dir = d_inode(parent);
  33067. + h_dir = d_inode(h_parent);
  33068. + AuDebugOn(h_dir != au_h_iptr(dir, bdst));
  33069. + IMustLock(h_dir);
  33070. +
  33071. + err = au_lkup_neg(dentry, bdst, /*wh*/0);
  33072. + if (unlikely(err < 0))
  33073. + goto out;
  33074. + h_path.dentry = au_h_dptr(dentry, bdst);
  33075. + h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
  33076. + err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
  33077. + if (unlikely(err))
  33078. + goto out_put;
  33079. + au_fset_cpdown(*flags, MADE_DIR);
  33080. +
  33081. + bopq = au_dbdiropq(dentry);
  33082. + au_fclr_cpdown(*flags, WHED);
  33083. + au_fclr_cpdown(*flags, DIROPQ);
  33084. + if (au_dbwh(dentry) == bdst)
  33085. + au_fset_cpdown(*flags, WHED);
  33086. + if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
  33087. + au_fset_cpdown(*flags, PARENT_OPQ);
  33088. + h_inode = d_inode(h_path.dentry);
  33089. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  33090. + if (au_ftest_cpdown(*flags, WHED)) {
  33091. + err = au_cpdown_dir_opq(dentry, bdst, flags);
  33092. + if (unlikely(err)) {
  33093. + inode_unlock(h_inode);
  33094. + goto out_dir;
  33095. + }
  33096. + }
  33097. +
  33098. + err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
  33099. + inode_unlock(h_inode);
  33100. + if (unlikely(err))
  33101. + goto out_opq;
  33102. +
  33103. + if (au_ftest_cpdown(*flags, WHED)) {
  33104. + err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
  33105. + if (unlikely(err))
  33106. + goto out_opq;
  33107. + }
  33108. +
  33109. + inode = d_inode(dentry);
  33110. + if (au_ibbot(inode) < bdst)
  33111. + au_set_ibbot(inode, bdst);
  33112. + au_set_h_iptr(inode, bdst, au_igrab(h_inode),
  33113. + au_hi_flags(inode, /*isdir*/1));
  33114. + au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
  33115. + goto out; /* success */
  33116. +
  33117. + /* revert */
  33118. +out_opq:
  33119. + if (au_ftest_cpdown(*flags, DIROPQ)) {
  33120. + inode_lock_nested(h_inode, AuLsc_I_CHILD);
  33121. + rerr = au_diropq_remove(dentry, bdst);
  33122. + inode_unlock(h_inode);
  33123. + if (unlikely(rerr)) {
  33124. + AuIOErr("failed removing diropq for %pd b%d (%d)\n",
  33125. + dentry, bdst, rerr);
  33126. + err = -EIO;
  33127. + goto out;
  33128. + }
  33129. + }
  33130. +out_dir:
  33131. + if (au_ftest_cpdown(*flags, MADE_DIR)) {
  33132. + rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
  33133. + if (unlikely(rerr)) {
  33134. + AuIOErr("failed removing %pd b%d (%d)\n",
  33135. + dentry, bdst, rerr);
  33136. + err = -EIO;
  33137. + }
  33138. + }
  33139. +out_put:
  33140. + au_set_h_dptr(dentry, bdst, NULL);
  33141. + if (au_dbbot(dentry) == bdst)
  33142. + au_update_dbbot(dentry);
  33143. +out:
  33144. + dput(parent);
  33145. + return err;
  33146. +}
  33147. +
  33148. +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
  33149. +{
  33150. + int err;
  33151. + unsigned int flags;
  33152. +
  33153. + flags = 0;
  33154. + err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
  33155. +
  33156. + return err;
  33157. +}
  33158. +
  33159. +/* ---------------------------------------------------------------------- */
  33160. +
  33161. +/* policies for create */
  33162. +
  33163. +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
  33164. +{
  33165. + int err, i, j, ndentry;
  33166. + aufs_bindex_t bopq;
  33167. + struct au_dcsub_pages dpages;
  33168. + struct au_dpage *dpage;
  33169. + struct dentry **dentries, *parent, *d;
  33170. +
  33171. + err = au_dpages_init(&dpages, GFP_NOFS);
  33172. + if (unlikely(err))
  33173. + goto out;
  33174. + parent = dget_parent(dentry);
  33175. + err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
  33176. + if (unlikely(err))
  33177. + goto out_free;
  33178. +
  33179. + err = bindex;
  33180. + for (i = 0; i < dpages.ndpage; i++) {
  33181. + dpage = dpages.dpages + i;
  33182. + dentries = dpage->dentries;
  33183. + ndentry = dpage->ndentry;
  33184. + for (j = 0; j < ndentry; j++) {
  33185. + d = dentries[j];
  33186. + di_read_lock_parent2(d, !AuLock_IR);
  33187. + bopq = au_dbdiropq(d);
  33188. + di_read_unlock(d, !AuLock_IR);
  33189. + if (bopq >= 0 && bopq < err)
  33190. + err = bopq;
  33191. + }
  33192. + }
  33193. +
  33194. +out_free:
  33195. + dput(parent);
  33196. + au_dpages_free(&dpages);
  33197. +out:
  33198. + return err;
  33199. +}
  33200. +
  33201. +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
  33202. +{
  33203. + for (; bindex >= 0; bindex--)
  33204. + if (!au_br_rdonly(au_sbr(sb, bindex)))
  33205. + return bindex;
  33206. + return -EROFS;
  33207. +}
  33208. +
  33209. +/* top down parent */
  33210. +static int au_wbr_create_tdp(struct dentry *dentry,
  33211. + unsigned int flags __maybe_unused)
  33212. +{
  33213. + int err;
  33214. + aufs_bindex_t btop, bindex;
  33215. + struct super_block *sb;
  33216. + struct dentry *parent, *h_parent;
  33217. +
  33218. + sb = dentry->d_sb;
  33219. + btop = au_dbtop(dentry);
  33220. + err = btop;
  33221. + if (!au_br_rdonly(au_sbr(sb, btop)))
  33222. + goto out;
  33223. +
  33224. + err = -EROFS;
  33225. + parent = dget_parent(dentry);
  33226. + for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
  33227. + h_parent = au_h_dptr(parent, bindex);
  33228. + if (!h_parent || d_is_negative(h_parent))
  33229. + continue;
  33230. +
  33231. + if (!au_br_rdonly(au_sbr(sb, bindex))) {
  33232. + err = bindex;
  33233. + break;
  33234. + }
  33235. + }
  33236. + dput(parent);
  33237. +
  33238. + /* bottom up here */
  33239. + if (unlikely(err < 0)) {
  33240. + err = au_wbr_bu(sb, btop - 1);
  33241. + if (err >= 0)
  33242. + err = au_wbr_nonopq(dentry, err);
  33243. + }
  33244. +
  33245. +out:
  33246. + AuDbg("b%d\n", err);
  33247. + return err;
  33248. +}
  33249. +
  33250. +/* ---------------------------------------------------------------------- */
  33251. +
  33252. +/* an exception for the policy other than tdp */
  33253. +static int au_wbr_create_exp(struct dentry *dentry)
  33254. +{
  33255. + int err;
  33256. + aufs_bindex_t bwh, bdiropq;
  33257. + struct dentry *parent;
  33258. +
  33259. + err = -1;
  33260. + bwh = au_dbwh(dentry);
  33261. + parent = dget_parent(dentry);
  33262. + bdiropq = au_dbdiropq(parent);
  33263. + if (bwh >= 0) {
  33264. + if (bdiropq >= 0)
  33265. + err = min(bdiropq, bwh);
  33266. + else
  33267. + err = bwh;
  33268. + AuDbg("%d\n", err);
  33269. + } else if (bdiropq >= 0) {
  33270. + err = bdiropq;
  33271. + AuDbg("%d\n", err);
  33272. + }
  33273. + dput(parent);
  33274. +
  33275. + if (err >= 0)
  33276. + err = au_wbr_nonopq(dentry, err);
  33277. +
  33278. + if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
  33279. + err = -1;
  33280. +
  33281. + AuDbg("%d\n", err);
  33282. + return err;
  33283. +}
  33284. +
  33285. +/* ---------------------------------------------------------------------- */
  33286. +
  33287. +/* round robin */
  33288. +static int au_wbr_create_init_rr(struct super_block *sb)
  33289. +{
  33290. + int err;
  33291. +
  33292. + err = au_wbr_bu(sb, au_sbbot(sb));
  33293. + atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
  33294. + /* smp_mb(); */
  33295. +
  33296. + AuDbg("b%d\n", err);
  33297. + return err;
  33298. +}
  33299. +
  33300. +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
  33301. +{
  33302. + int err, nbr;
  33303. + unsigned int u;
  33304. + aufs_bindex_t bindex, bbot;
  33305. + struct super_block *sb;
  33306. + atomic_t *next;
  33307. +
  33308. + err = au_wbr_create_exp(dentry);
  33309. + if (err >= 0)
  33310. + goto out;
  33311. +
  33312. + sb = dentry->d_sb;
  33313. + next = &au_sbi(sb)->si_wbr_rr_next;
  33314. + bbot = au_sbbot(sb);
  33315. + nbr = bbot + 1;
  33316. + for (bindex = 0; bindex <= bbot; bindex++) {
  33317. + if (!au_ftest_wbr(flags, DIR)) {
  33318. + err = atomic_dec_return(next) + 1;
  33319. + /* modulo for 0 is meaningless */
  33320. + if (unlikely(!err))
  33321. + err = atomic_dec_return(next) + 1;
  33322. + } else
  33323. + err = atomic_read(next);
  33324. + AuDbg("%d\n", err);
  33325. + u = err;
  33326. + err = u % nbr;
  33327. + AuDbg("%d\n", err);
  33328. + if (!au_br_rdonly(au_sbr(sb, err)))
  33329. + break;
  33330. + err = -EROFS;
  33331. + }
  33332. +
  33333. + if (err >= 0)
  33334. + err = au_wbr_nonopq(dentry, err);
  33335. +
  33336. +out:
  33337. + AuDbg("%d\n", err);
  33338. + return err;
  33339. +}
  33340. +
  33341. +/* ---------------------------------------------------------------------- */
  33342. +
  33343. +/* most free space */
  33344. +static void au_mfs(struct dentry *dentry, struct dentry *parent)
  33345. +{
  33346. + struct super_block *sb;
  33347. + struct au_branch *br;
  33348. + struct au_wbr_mfs *mfs;
  33349. + struct dentry *h_parent;
  33350. + aufs_bindex_t bindex, bbot;
  33351. + int err;
  33352. + unsigned long long b, bavail;
  33353. + struct path h_path;
  33354. + /* reduce the stack usage */
  33355. + struct kstatfs *st;
  33356. +
  33357. + st = kmalloc(sizeof(*st), GFP_NOFS);
  33358. + if (unlikely(!st)) {
  33359. + AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
  33360. + return;
  33361. + }
  33362. +
  33363. + bavail = 0;
  33364. + sb = dentry->d_sb;
  33365. + mfs = &au_sbi(sb)->si_wbr_mfs;
  33366. + MtxMustLock(&mfs->mfs_lock);
  33367. + mfs->mfs_bindex = -EROFS;
  33368. + mfs->mfsrr_bytes = 0;
  33369. + if (!parent) {
  33370. + bindex = 0;
  33371. + bbot = au_sbbot(sb);
  33372. + } else {
  33373. + bindex = au_dbtop(parent);
  33374. + bbot = au_dbtaildir(parent);
  33375. + }
  33376. +
  33377. + for (; bindex <= bbot; bindex++) {
  33378. + if (parent) {
  33379. + h_parent = au_h_dptr(parent, bindex);
  33380. + if (!h_parent || d_is_negative(h_parent))
  33381. + continue;
  33382. + }
  33383. + br = au_sbr(sb, bindex);
  33384. + if (au_br_rdonly(br))
  33385. + continue;
  33386. +
  33387. + /* sb->s_root for NFS is unreliable */
  33388. + h_path.mnt = au_br_mnt(br);
  33389. + h_path.dentry = h_path.mnt->mnt_root;
  33390. + err = vfs_statfs(&h_path, st);
  33391. + if (unlikely(err)) {
  33392. + AuWarn1("failed statfs, b%d, %d\n", bindex, err);
  33393. + continue;
  33394. + }
  33395. +
  33396. + /* when the available size is equal, select the lower one */
  33397. + BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
  33398. + || sizeof(b) < sizeof(st->f_bsize));
  33399. + b = st->f_bavail * st->f_bsize;
  33400. + br->br_wbr->wbr_bytes = b;
  33401. + if (b >= bavail) {
  33402. + bavail = b;
  33403. + mfs->mfs_bindex = bindex;
  33404. + mfs->mfs_jiffy = jiffies;
  33405. + }
  33406. + }
  33407. +
  33408. + mfs->mfsrr_bytes = bavail;
  33409. + AuDbg("b%d\n", mfs->mfs_bindex);
  33410. + au_kfree_rcu(st);
  33411. +}
  33412. +
  33413. +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
  33414. +{
  33415. + int err;
  33416. + struct dentry *parent;
  33417. + struct super_block *sb;
  33418. + struct au_wbr_mfs *mfs;
  33419. +
  33420. + err = au_wbr_create_exp(dentry);
  33421. + if (err >= 0)
  33422. + goto out;
  33423. +
  33424. + sb = dentry->d_sb;
  33425. + parent = NULL;
  33426. + if (au_ftest_wbr(flags, PARENT))
  33427. + parent = dget_parent(dentry);
  33428. + mfs = &au_sbi(sb)->si_wbr_mfs;
  33429. + mutex_lock(&mfs->mfs_lock);
  33430. + if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
  33431. + || mfs->mfs_bindex < 0
  33432. + || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
  33433. + au_mfs(dentry, parent);
  33434. + mutex_unlock(&mfs->mfs_lock);
  33435. + err = mfs->mfs_bindex;
  33436. + dput(parent);
  33437. +
  33438. + if (err >= 0)
  33439. + err = au_wbr_nonopq(dentry, err);
  33440. +
  33441. +out:
  33442. + AuDbg("b%d\n", err);
  33443. + return err;
  33444. +}
  33445. +
  33446. +static int au_wbr_create_init_mfs(struct super_block *sb)
  33447. +{
  33448. + struct au_wbr_mfs *mfs;
  33449. +
  33450. + mfs = &au_sbi(sb)->si_wbr_mfs;
  33451. + mutex_init(&mfs->mfs_lock);
  33452. + mfs->mfs_jiffy = 0;
  33453. + mfs->mfs_bindex = -EROFS;
  33454. +
  33455. + return 0;
  33456. +}
  33457. +
  33458. +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
  33459. +{
  33460. + mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
  33461. + return 0;
  33462. +}
  33463. +
  33464. +/* ---------------------------------------------------------------------- */
  33465. +
  33466. +/* top down regardless parent, and then mfs */
  33467. +static int au_wbr_create_tdmfs(struct dentry *dentry,
  33468. + unsigned int flags __maybe_unused)
  33469. +{
  33470. + int err;
  33471. + aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
  33472. + unsigned long long watermark;
  33473. + struct super_block *sb;
  33474. + struct au_wbr_mfs *mfs;
  33475. + struct au_branch *br;
  33476. + struct dentry *parent;
  33477. +
  33478. + sb = dentry->d_sb;
  33479. + mfs = &au_sbi(sb)->si_wbr_mfs;
  33480. + mutex_lock(&mfs->mfs_lock);
  33481. + if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
  33482. + || mfs->mfs_bindex < 0)
  33483. + au_mfs(dentry, /*parent*/NULL);
  33484. + watermark = mfs->mfsrr_watermark;
  33485. + bmfs = mfs->mfs_bindex;
  33486. + mutex_unlock(&mfs->mfs_lock);
  33487. +
  33488. + /* another style of au_wbr_create_exp() */
  33489. + bwh = au_dbwh(dentry);
  33490. + parent = dget_parent(dentry);
  33491. + btail = au_dbtaildir(parent);
  33492. + if (bwh >= 0 && bwh < btail)
  33493. + btail = bwh;
  33494. +
  33495. + err = au_wbr_nonopq(dentry, btail);
  33496. + if (unlikely(err < 0))
  33497. + goto out;
  33498. + btail = err;
  33499. + bfound = -1;
  33500. + for (bindex = 0; bindex <= btail; bindex++) {
  33501. + br = au_sbr(sb, bindex);
  33502. + if (au_br_rdonly(br))
  33503. + continue;
  33504. + if (br->br_wbr->wbr_bytes > watermark) {
  33505. + bfound = bindex;
  33506. + break;
  33507. + }
  33508. + }
  33509. + err = bfound;
  33510. + if (err < 0)
  33511. + err = bmfs;
  33512. +
  33513. +out:
  33514. + dput(parent);
  33515. + AuDbg("b%d\n", err);
  33516. + return err;
  33517. +}
  33518. +
  33519. +/* ---------------------------------------------------------------------- */
  33520. +
  33521. +/* most free space and then round robin */
  33522. +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
  33523. +{
  33524. + int err;
  33525. + struct au_wbr_mfs *mfs;
  33526. +
  33527. + err = au_wbr_create_mfs(dentry, flags);
  33528. + if (err >= 0) {
  33529. + mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
  33530. + mutex_lock(&mfs->mfs_lock);
  33531. + if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
  33532. + err = au_wbr_create_rr(dentry, flags);
  33533. + mutex_unlock(&mfs->mfs_lock);
  33534. + }
  33535. +
  33536. + AuDbg("b%d\n", err);
  33537. + return err;
  33538. +}
  33539. +
  33540. +static int au_wbr_create_init_mfsrr(struct super_block *sb)
  33541. +{
  33542. + int err;
  33543. +
  33544. + au_wbr_create_init_mfs(sb); /* ignore */
  33545. + err = au_wbr_create_init_rr(sb);
  33546. +
  33547. + return err;
  33548. +}
  33549. +
  33550. +/* ---------------------------------------------------------------------- */
  33551. +
  33552. +/* top down parent and most free space */
  33553. +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
  33554. +{
  33555. + int err, e2;
  33556. + unsigned long long b;
  33557. + aufs_bindex_t bindex, btop, bbot;
  33558. + struct super_block *sb;
  33559. + struct dentry *parent, *h_parent;
  33560. + struct au_branch *br;
  33561. +
  33562. + err = au_wbr_create_tdp(dentry, flags);
  33563. + if (unlikely(err < 0))
  33564. + goto out;
  33565. + parent = dget_parent(dentry);
  33566. + btop = au_dbtop(parent);
  33567. + bbot = au_dbtaildir(parent);
  33568. + if (btop == bbot)
  33569. + goto out_parent; /* success */
  33570. +
  33571. + e2 = au_wbr_create_mfs(dentry, flags);
  33572. + if (e2 < 0)
  33573. + goto out_parent; /* success */
  33574. +
  33575. + /* when the available size is equal, select upper one */
  33576. + sb = dentry->d_sb;
  33577. + br = au_sbr(sb, err);
  33578. + b = br->br_wbr->wbr_bytes;
  33579. + AuDbg("b%d, %llu\n", err, b);
  33580. +
  33581. + for (bindex = btop; bindex <= bbot; bindex++) {
  33582. + h_parent = au_h_dptr(parent, bindex);
  33583. + if (!h_parent || d_is_negative(h_parent))
  33584. + continue;
  33585. +
  33586. + br = au_sbr(sb, bindex);
  33587. + if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
  33588. + b = br->br_wbr->wbr_bytes;
  33589. + err = bindex;
  33590. + AuDbg("b%d, %llu\n", err, b);
  33591. + }
  33592. + }
  33593. +
  33594. + if (err >= 0)
  33595. + err = au_wbr_nonopq(dentry, err);
  33596. +
  33597. +out_parent:
  33598. + dput(parent);
  33599. +out:
  33600. + AuDbg("b%d\n", err);
  33601. + return err;
  33602. +}
  33603. +
  33604. +/* ---------------------------------------------------------------------- */
  33605. +
  33606. +/*
  33607. + * - top down parent
  33608. + * - most free space with parent
  33609. + * - most free space round-robin regardless parent
  33610. + */
  33611. +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
  33612. +{
  33613. + int err;
  33614. + unsigned long long watermark;
  33615. + struct super_block *sb;
  33616. + struct au_branch *br;
  33617. + struct au_wbr_mfs *mfs;
  33618. +
  33619. + err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
  33620. + if (unlikely(err < 0))
  33621. + goto out;
  33622. +
  33623. + sb = dentry->d_sb;
  33624. + br = au_sbr(sb, err);
  33625. + mfs = &au_sbi(sb)->si_wbr_mfs;
  33626. + mutex_lock(&mfs->mfs_lock);
  33627. + watermark = mfs->mfsrr_watermark;
  33628. + mutex_unlock(&mfs->mfs_lock);
  33629. + if (br->br_wbr->wbr_bytes < watermark)
  33630. + /* regardless the parent dir */
  33631. + err = au_wbr_create_mfsrr(dentry, flags);
  33632. +
  33633. +out:
  33634. + AuDbg("b%d\n", err);
  33635. + return err;
  33636. +}
  33637. +
  33638. +/* ---------------------------------------------------------------------- */
  33639. +
  33640. +/* policies for copyup */
  33641. +
  33642. +/* top down parent */
  33643. +static int au_wbr_copyup_tdp(struct dentry *dentry)
  33644. +{
  33645. + return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
  33646. +}
  33647. +
  33648. +/* bottom up parent */
  33649. +static int au_wbr_copyup_bup(struct dentry *dentry)
  33650. +{
  33651. + int err;
  33652. + aufs_bindex_t bindex, btop;
  33653. + struct dentry *parent, *h_parent;
  33654. + struct super_block *sb;
  33655. +
  33656. + err = -EROFS;
  33657. + sb = dentry->d_sb;
  33658. + parent = dget_parent(dentry);
  33659. + btop = au_dbtop(parent);
  33660. + for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
  33661. + h_parent = au_h_dptr(parent, bindex);
  33662. + if (!h_parent || d_is_negative(h_parent))
  33663. + continue;
  33664. +
  33665. + if (!au_br_rdonly(au_sbr(sb, bindex))) {
  33666. + err = bindex;
  33667. + break;
  33668. + }
  33669. + }
  33670. + dput(parent);
  33671. +
  33672. + /* bottom up here */
  33673. + if (unlikely(err < 0))
  33674. + err = au_wbr_bu(sb, btop - 1);
  33675. +
  33676. + AuDbg("b%d\n", err);
  33677. + return err;
  33678. +}
  33679. +
  33680. +/* bottom up */
  33681. +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
  33682. +{
  33683. + int err;
  33684. +
  33685. + err = au_wbr_bu(dentry->d_sb, btop);
  33686. + AuDbg("b%d\n", err);
  33687. + if (err > btop)
  33688. + err = au_wbr_nonopq(dentry, err);
  33689. +
  33690. + AuDbg("b%d\n", err);
  33691. + return err;
  33692. +}
  33693. +
  33694. +static int au_wbr_copyup_bu(struct dentry *dentry)
  33695. +{
  33696. + int err;
  33697. + aufs_bindex_t btop;
  33698. +
  33699. + btop = au_dbtop(dentry);
  33700. + err = au_wbr_do_copyup_bu(dentry, btop);
  33701. + return err;
  33702. +}
  33703. +
  33704. +/* ---------------------------------------------------------------------- */
  33705. +
  33706. +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
  33707. + [AuWbrCopyup_TDP] = {
  33708. + .copyup = au_wbr_copyup_tdp
  33709. + },
  33710. + [AuWbrCopyup_BUP] = {
  33711. + .copyup = au_wbr_copyup_bup
  33712. + },
  33713. + [AuWbrCopyup_BU] = {
  33714. + .copyup = au_wbr_copyup_bu
  33715. + }
  33716. +};
  33717. +
  33718. +struct au_wbr_create_operations au_wbr_create_ops[] = {
  33719. + [AuWbrCreate_TDP] = {
  33720. + .create = au_wbr_create_tdp
  33721. + },
  33722. + [AuWbrCreate_RR] = {
  33723. + .create = au_wbr_create_rr,
  33724. + .init = au_wbr_create_init_rr
  33725. + },
  33726. + [AuWbrCreate_MFS] = {
  33727. + .create = au_wbr_create_mfs,
  33728. + .init = au_wbr_create_init_mfs,
  33729. + .fin = au_wbr_create_fin_mfs
  33730. + },
  33731. + [AuWbrCreate_MFSV] = {
  33732. + .create = au_wbr_create_mfs,
  33733. + .init = au_wbr_create_init_mfs,
  33734. + .fin = au_wbr_create_fin_mfs
  33735. + },
  33736. + [AuWbrCreate_MFSRR] = {
  33737. + .create = au_wbr_create_mfsrr,
  33738. + .init = au_wbr_create_init_mfsrr,
  33739. + .fin = au_wbr_create_fin_mfs
  33740. + },
  33741. + [AuWbrCreate_MFSRRV] = {
  33742. + .create = au_wbr_create_mfsrr,
  33743. + .init = au_wbr_create_init_mfsrr,
  33744. + .fin = au_wbr_create_fin_mfs
  33745. + },
  33746. + [AuWbrCreate_TDMFS] = {
  33747. + .create = au_wbr_create_tdmfs,
  33748. + .init = au_wbr_create_init_mfs,
  33749. + .fin = au_wbr_create_fin_mfs
  33750. + },
  33751. + [AuWbrCreate_TDMFSV] = {
  33752. + .create = au_wbr_create_tdmfs,
  33753. + .init = au_wbr_create_init_mfs,
  33754. + .fin = au_wbr_create_fin_mfs
  33755. + },
  33756. + [AuWbrCreate_PMFS] = {
  33757. + .create = au_wbr_create_pmfs,
  33758. + .init = au_wbr_create_init_mfs,
  33759. + .fin = au_wbr_create_fin_mfs
  33760. + },
  33761. + [AuWbrCreate_PMFSV] = {
  33762. + .create = au_wbr_create_pmfs,
  33763. + .init = au_wbr_create_init_mfs,
  33764. + .fin = au_wbr_create_fin_mfs
  33765. + },
  33766. + [AuWbrCreate_PMFSRR] = {
  33767. + .create = au_wbr_create_pmfsrr,
  33768. + .init = au_wbr_create_init_mfsrr,
  33769. + .fin = au_wbr_create_fin_mfs
  33770. + },
  33771. + [AuWbrCreate_PMFSRRV] = {
  33772. + .create = au_wbr_create_pmfsrr,
  33773. + .init = au_wbr_create_init_mfsrr,
  33774. + .fin = au_wbr_create_fin_mfs
  33775. + }
  33776. +};
  33777. diff -Naur null/fs/aufs/whout.c linux-5.1/fs/aufs/whout.c
  33778. --- /dev/null
  33779. +++ linux-5.1/fs/aufs/whout.c 2019-05-22 11:23:07.443140904 +0200
  33780. @@ -0,0 +1,1062 @@
  33781. +// SPDX-License-Identifier: GPL-2.0
  33782. +/*
  33783. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  33784. + *
  33785. + * This program, aufs is free software; you can redistribute it and/or modify
  33786. + * it under the terms of the GNU General Public License as published by
  33787. + * the Free Software Foundation; either version 2 of the License, or
  33788. + * (at your option) any later version.
  33789. + *
  33790. + * This program is distributed in the hope that it will be useful,
  33791. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33792. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  33793. + * GNU General Public License for more details.
  33794. + *
  33795. + * You should have received a copy of the GNU General Public License
  33796. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  33797. + */
  33798. +
  33799. +/*
  33800. + * whiteout for logical deletion and opaque directory
  33801. + */
  33802. +
  33803. +#include "aufs.h"
  33804. +
  33805. +#define WH_MASK 0444
  33806. +
  33807. +/*
  33808. + * If a directory contains this file, then it is opaque. We start with the
  33809. + * .wh. flag so that it is blocked by lookup.
  33810. + */
  33811. +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
  33812. + sizeof(AUFS_WH_DIROPQ) - 1);
  33813. +
  33814. +/*
  33815. + * generate whiteout name, which is NOT terminated by NULL.
  33816. + * @name: original d_name.name
  33817. + * @len: original d_name.len
  33818. + * @wh: whiteout qstr
  33819. + * returns zero when succeeds, otherwise error.
  33820. + * succeeded value as wh->name should be freed by kfree().
  33821. + */
  33822. +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
  33823. +{
  33824. + char *p;
  33825. +
  33826. + if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
  33827. + return -ENAMETOOLONG;
  33828. +
  33829. + wh->len = name->len + AUFS_WH_PFX_LEN;
  33830. + p = kmalloc(wh->len, GFP_NOFS);
  33831. + wh->name = p;
  33832. + if (p) {
  33833. + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
  33834. + memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
  33835. + /* smp_mb(); */
  33836. + return 0;
  33837. + }
  33838. + return -ENOMEM;
  33839. +}
  33840. +
  33841. +/* ---------------------------------------------------------------------- */
  33842. +
  33843. +/*
  33844. + * test if the @wh_name exists under @h_parent.
  33845. + * @try_sio specifies the necessary of super-io.
  33846. + */
  33847. +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio)
  33848. +{
  33849. + int err;
  33850. + struct dentry *wh_dentry;
  33851. +
  33852. + if (!try_sio)
  33853. + wh_dentry = vfsub_lkup_one(wh_name, h_parent);
  33854. + else
  33855. + wh_dentry = au_sio_lkup_one(wh_name, h_parent);
  33856. + err = PTR_ERR(wh_dentry);
  33857. + if (IS_ERR(wh_dentry)) {
  33858. + if (err == -ENAMETOOLONG)
  33859. + err = 0;
  33860. + goto out;
  33861. + }
  33862. +
  33863. + err = 0;
  33864. + if (d_is_negative(wh_dentry))
  33865. + goto out_wh; /* success */
  33866. +
  33867. + err = 1;
  33868. + if (d_is_reg(wh_dentry))
  33869. + goto out_wh; /* success */
  33870. +
  33871. + err = -EIO;
  33872. + AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
  33873. + wh_dentry, d_inode(wh_dentry)->i_mode);
  33874. +
  33875. +out_wh:
  33876. + dput(wh_dentry);
  33877. +out:
  33878. + return err;
  33879. +}
  33880. +
  33881. +/*
  33882. + * test if the @h_dentry sets opaque or not.
  33883. + */
  33884. +int au_diropq_test(struct dentry *h_dentry)
  33885. +{
  33886. + int err;
  33887. + struct inode *h_dir;
  33888. +
  33889. + h_dir = d_inode(h_dentry);
  33890. + err = au_wh_test(h_dentry, &diropq_name,
  33891. + au_test_h_perm_sio(h_dir, MAY_EXEC));
  33892. + return err;
  33893. +}
  33894. +
  33895. +/*
  33896. + * returns a negative dentry whose name is unique and temporary.
  33897. + */
  33898. +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
  33899. + struct qstr *prefix)
  33900. +{
  33901. + struct dentry *dentry;
  33902. + int i;
  33903. + char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
  33904. + *name, *p;
  33905. + /* strict atomic_t is unnecessary here */
  33906. + static unsigned short cnt;
  33907. + struct qstr qs;
  33908. +
  33909. + BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
  33910. +
  33911. + name = defname;
  33912. + qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
  33913. + if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
  33914. + dentry = ERR_PTR(-ENAMETOOLONG);
  33915. + if (unlikely(qs.len > NAME_MAX))
  33916. + goto out;
  33917. + dentry = ERR_PTR(-ENOMEM);
  33918. + name = kmalloc(qs.len + 1, GFP_NOFS);
  33919. + if (unlikely(!name))
  33920. + goto out;
  33921. + }
  33922. +
  33923. + /* doubly whiteout-ed */
  33924. + memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
  33925. + p = name + AUFS_WH_PFX_LEN * 2;
  33926. + memcpy(p, prefix->name, prefix->len);
  33927. + p += prefix->len;
  33928. + *p++ = '.';
  33929. + AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
  33930. +
  33931. + qs.name = name;
  33932. + for (i = 0; i < 3; i++) {
  33933. + sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
  33934. + dentry = au_sio_lkup_one(&qs, h_parent);
  33935. + if (IS_ERR(dentry) || d_is_negative(dentry))
  33936. + goto out_name;
  33937. + dput(dentry);
  33938. + }
  33939. + /* pr_warn("could not get random name\n"); */
  33940. + dentry = ERR_PTR(-EEXIST);
  33941. + AuDbg("%.*s\n", AuLNPair(&qs));
  33942. + BUG();
  33943. +
  33944. +out_name:
  33945. + if (name != defname)
  33946. + au_kfree_try_rcu(name);
  33947. +out:
  33948. + AuTraceErrPtr(dentry);
  33949. + return dentry;
  33950. +}
  33951. +
  33952. +/*
  33953. + * rename the @h_dentry on @br to the whiteouted temporary name.
  33954. + */
  33955. +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
  33956. +{
  33957. + int err;
  33958. + struct path h_path = {
  33959. + .mnt = au_br_mnt(br)
  33960. + };
  33961. + struct inode *h_dir, *delegated;
  33962. + struct dentry *h_parent;
  33963. +
  33964. + h_parent = h_dentry->d_parent; /* dir inode is locked */
  33965. + h_dir = d_inode(h_parent);
  33966. + IMustLock(h_dir);
  33967. +
  33968. + h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
  33969. + err = PTR_ERR(h_path.dentry);
  33970. + if (IS_ERR(h_path.dentry))
  33971. + goto out;
  33972. +
  33973. + /* under the same dir, no need to lock_rename() */
  33974. + delegated = NULL;
  33975. + err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
  33976. + /*flags*/0);
  33977. + AuTraceErr(err);
  33978. + if (unlikely(err == -EWOULDBLOCK)) {
  33979. + pr_warn("cannot retry for NFSv4 delegation"
  33980. + " for an internal rename\n");
  33981. + iput(delegated);
  33982. + }
  33983. + dput(h_path.dentry);
  33984. +
  33985. +out:
  33986. + AuTraceErr(err);
  33987. + return err;
  33988. +}
  33989. +
  33990. +/* ---------------------------------------------------------------------- */
  33991. +/*
  33992. + * functions for removing a whiteout
  33993. + */
  33994. +
  33995. +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
  33996. +{
  33997. + int err, force;
  33998. + struct inode *delegated;
  33999. +
  34000. + /*
  34001. + * forces superio when the dir has a sticky bit.
  34002. + * this may be a violation of unix fs semantics.
  34003. + */
  34004. + force = (h_dir->i_mode & S_ISVTX)
  34005. + && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
  34006. + delegated = NULL;
  34007. + err = vfsub_unlink(h_dir, h_path, &delegated, force);
  34008. + if (unlikely(err == -EWOULDBLOCK)) {
  34009. + pr_warn("cannot retry for NFSv4 delegation"
  34010. + " for an internal unlink\n");
  34011. + iput(delegated);
  34012. + }
  34013. + return err;
  34014. +}
  34015. +
  34016. +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
  34017. + struct dentry *dentry)
  34018. +{
  34019. + int err;
  34020. +
  34021. + err = do_unlink_wh(h_dir, h_path);
  34022. + if (!err && dentry)
  34023. + au_set_dbwh(dentry, -1);
  34024. +
  34025. + return err;
  34026. +}
  34027. +
  34028. +static int unlink_wh_name(struct dentry *h_parent, struct qstr *wh,
  34029. + struct au_branch *br)
  34030. +{
  34031. + int err;
  34032. + struct path h_path = {
  34033. + .mnt = au_br_mnt(br)
  34034. + };
  34035. +
  34036. + err = 0;
  34037. + h_path.dentry = vfsub_lkup_one(wh, h_parent);
  34038. + if (IS_ERR(h_path.dentry))
  34039. + err = PTR_ERR(h_path.dentry);
  34040. + else {
  34041. + if (d_is_reg(h_path.dentry))
  34042. + err = do_unlink_wh(d_inode(h_parent), &h_path);
  34043. + dput(h_path.dentry);
  34044. + }
  34045. +
  34046. + return err;
  34047. +}
  34048. +
  34049. +/* ---------------------------------------------------------------------- */
  34050. +/*
  34051. + * initialize/clean whiteout for a branch
  34052. + */
  34053. +
  34054. +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
  34055. + const int isdir)
  34056. +{
  34057. + int err;
  34058. + struct inode *delegated;
  34059. +
  34060. + if (d_is_negative(whpath->dentry))
  34061. + return;
  34062. +
  34063. + if (isdir)
  34064. + err = vfsub_rmdir(h_dir, whpath);
  34065. + else {
  34066. + delegated = NULL;
  34067. + err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
  34068. + if (unlikely(err == -EWOULDBLOCK)) {
  34069. + pr_warn("cannot retry for NFSv4 delegation"
  34070. + " for an internal unlink\n");
  34071. + iput(delegated);
  34072. + }
  34073. + }
  34074. + if (unlikely(err))
  34075. + pr_warn("failed removing %pd (%d), ignored.\n",
  34076. + whpath->dentry, err);
  34077. +}
  34078. +
  34079. +static int test_linkable(struct dentry *h_root)
  34080. +{
  34081. + struct inode *h_dir = d_inode(h_root);
  34082. +
  34083. + if (h_dir->i_op->link)
  34084. + return 0;
  34085. +
  34086. + pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
  34087. + h_root, au_sbtype(h_root->d_sb));
  34088. + return -ENOSYS;
  34089. +}
  34090. +
  34091. +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
  34092. +static int au_whdir(struct inode *h_dir, struct path *path)
  34093. +{
  34094. + int err;
  34095. +
  34096. + err = -EEXIST;
  34097. + if (d_is_negative(path->dentry)) {
  34098. + int mode = 0700;
  34099. +
  34100. + if (au_test_nfs(path->dentry->d_sb))
  34101. + mode |= 0111;
  34102. + err = vfsub_mkdir(h_dir, path, mode);
  34103. + } else if (d_is_dir(path->dentry))
  34104. + err = 0;
  34105. + else
  34106. + pr_err("unknown %pd exists\n", path->dentry);
  34107. +
  34108. + return err;
  34109. +}
  34110. +
  34111. +struct au_wh_base {
  34112. + const struct qstr *name;
  34113. + struct dentry *dentry;
  34114. +};
  34115. +
  34116. +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
  34117. + struct path *h_path)
  34118. +{
  34119. + h_path->dentry = base[AuBrWh_BASE].dentry;
  34120. + au_wh_clean(h_dir, h_path, /*isdir*/0);
  34121. + h_path->dentry = base[AuBrWh_PLINK].dentry;
  34122. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  34123. + h_path->dentry = base[AuBrWh_ORPH].dentry;
  34124. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  34125. +}
  34126. +
  34127. +/*
  34128. + * returns tri-state,
  34129. + * minus: error, caller should print the message
  34130. + * zero: success
  34131. + * plus: error, caller should NOT print the message
  34132. + */
  34133. +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
  34134. + int do_plink, struct au_wh_base base[],
  34135. + struct path *h_path)
  34136. +{
  34137. + int err;
  34138. + struct inode *h_dir;
  34139. +
  34140. + h_dir = d_inode(h_root);
  34141. + h_path->dentry = base[AuBrWh_BASE].dentry;
  34142. + au_wh_clean(h_dir, h_path, /*isdir*/0);
  34143. + h_path->dentry = base[AuBrWh_PLINK].dentry;
  34144. + if (do_plink) {
  34145. + err = test_linkable(h_root);
  34146. + if (unlikely(err)) {
  34147. + err = 1;
  34148. + goto out;
  34149. + }
  34150. +
  34151. + err = au_whdir(h_dir, h_path);
  34152. + if (unlikely(err))
  34153. + goto out;
  34154. + wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
  34155. + } else
  34156. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  34157. + h_path->dentry = base[AuBrWh_ORPH].dentry;
  34158. + err = au_whdir(h_dir, h_path);
  34159. + if (unlikely(err))
  34160. + goto out;
  34161. + wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
  34162. +
  34163. +out:
  34164. + return err;
  34165. +}
  34166. +
  34167. +/*
  34168. + * for the moment, aufs supports the branch filesystem which does not support
  34169. + * link(2). testing on FAT which does not support i_op->setattr() fully either,
  34170. + * copyup failed. finally, such filesystem will not be used as the writable
  34171. + * branch.
  34172. + *
  34173. + * returns tri-state, see above.
  34174. + */
  34175. +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
  34176. + int do_plink, struct au_wh_base base[],
  34177. + struct path *h_path)
  34178. +{
  34179. + int err;
  34180. + struct inode *h_dir;
  34181. +
  34182. + WbrWhMustWriteLock(wbr);
  34183. +
  34184. + err = test_linkable(h_root);
  34185. + if (unlikely(err)) {
  34186. + err = 1;
  34187. + goto out;
  34188. + }
  34189. +
  34190. + /*
  34191. + * todo: should this create be done in /sbin/mount.aufs helper?
  34192. + */
  34193. + err = -EEXIST;
  34194. + h_dir = d_inode(h_root);
  34195. + if (d_is_negative(base[AuBrWh_BASE].dentry)) {
  34196. + h_path->dentry = base[AuBrWh_BASE].dentry;
  34197. + err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
  34198. + } else if (d_is_reg(base[AuBrWh_BASE].dentry))
  34199. + err = 0;
  34200. + else
  34201. + pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
  34202. + if (unlikely(err))
  34203. + goto out;
  34204. +
  34205. + h_path->dentry = base[AuBrWh_PLINK].dentry;
  34206. + if (do_plink) {
  34207. + err = au_whdir(h_dir, h_path);
  34208. + if (unlikely(err))
  34209. + goto out;
  34210. + wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
  34211. + } else
  34212. + au_wh_clean(h_dir, h_path, /*isdir*/1);
  34213. + wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
  34214. +
  34215. + h_path->dentry = base[AuBrWh_ORPH].dentry;
  34216. + err = au_whdir(h_dir, h_path);
  34217. + if (unlikely(err))
  34218. + goto out;
  34219. + wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
  34220. +
  34221. +out:
  34222. + return err;
  34223. +}
  34224. +
  34225. +/*
  34226. + * initialize the whiteout base file/dir for @br.
  34227. + */
  34228. +int au_wh_init(struct au_branch *br, struct super_block *sb)
  34229. +{
  34230. + int err, i;
  34231. + const unsigned char do_plink
  34232. + = !!au_opt_test(au_mntflags(sb), PLINK);
  34233. + struct inode *h_dir;
  34234. + struct path path = br->br_path;
  34235. + struct dentry *h_root = path.dentry;
  34236. + struct au_wbr *wbr = br->br_wbr;
  34237. + static const struct qstr base_name[] = {
  34238. + [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
  34239. + sizeof(AUFS_BASE_NAME) - 1),
  34240. + [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
  34241. + sizeof(AUFS_PLINKDIR_NAME) - 1),
  34242. + [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
  34243. + sizeof(AUFS_ORPHDIR_NAME) - 1)
  34244. + };
  34245. + struct au_wh_base base[] = {
  34246. + [AuBrWh_BASE] = {
  34247. + .name = base_name + AuBrWh_BASE,
  34248. + .dentry = NULL
  34249. + },
  34250. + [AuBrWh_PLINK] = {
  34251. + .name = base_name + AuBrWh_PLINK,
  34252. + .dentry = NULL
  34253. + },
  34254. + [AuBrWh_ORPH] = {
  34255. + .name = base_name + AuBrWh_ORPH,
  34256. + .dentry = NULL
  34257. + }
  34258. + };
  34259. +
  34260. + if (wbr)
  34261. + WbrWhMustWriteLock(wbr);
  34262. +
  34263. + for (i = 0; i < AuBrWh_Last; i++) {
  34264. + /* doubly whiteouted */
  34265. + struct dentry *d;
  34266. +
  34267. + d = au_wh_lkup(h_root, (void *)base[i].name, br);
  34268. + err = PTR_ERR(d);
  34269. + if (IS_ERR(d))
  34270. + goto out;
  34271. +
  34272. + base[i].dentry = d;
  34273. + AuDebugOn(wbr
  34274. + && wbr->wbr_wh[i]
  34275. + && wbr->wbr_wh[i] != base[i].dentry);
  34276. + }
  34277. +
  34278. + if (wbr)
  34279. + for (i = 0; i < AuBrWh_Last; i++) {
  34280. + dput(wbr->wbr_wh[i]);
  34281. + wbr->wbr_wh[i] = NULL;
  34282. + }
  34283. +
  34284. + err = 0;
  34285. + if (!au_br_writable(br->br_perm)) {
  34286. + h_dir = d_inode(h_root);
  34287. + au_wh_init_ro(h_dir, base, &path);
  34288. + } else if (!au_br_wh_linkable(br->br_perm)) {
  34289. + err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
  34290. + if (err > 0)
  34291. + goto out;
  34292. + else if (err)
  34293. + goto out_err;
  34294. + } else {
  34295. + err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
  34296. + if (err > 0)
  34297. + goto out;
  34298. + else if (err)
  34299. + goto out_err;
  34300. + }
  34301. + goto out; /* success */
  34302. +
  34303. +out_err:
  34304. + pr_err("an error(%d) on the writable branch %pd(%s)\n",
  34305. + err, h_root, au_sbtype(h_root->d_sb));
  34306. +out:
  34307. + for (i = 0; i < AuBrWh_Last; i++)
  34308. + dput(base[i].dentry);
  34309. + return err;
  34310. +}
  34311. +
  34312. +/* ---------------------------------------------------------------------- */
  34313. +/*
  34314. + * whiteouts are all hard-linked usually.
  34315. + * when its link count reaches a ceiling, we create a new whiteout base
  34316. + * asynchronously.
  34317. + */
  34318. +
  34319. +struct reinit_br_wh {
  34320. + struct super_block *sb;
  34321. + struct au_branch *br;
  34322. +};
  34323. +
  34324. +static void reinit_br_wh(void *arg)
  34325. +{
  34326. + int err;
  34327. + aufs_bindex_t bindex;
  34328. + struct path h_path;
  34329. + struct reinit_br_wh *a = arg;
  34330. + struct au_wbr *wbr;
  34331. + struct inode *dir, *delegated;
  34332. + struct dentry *h_root;
  34333. + struct au_hinode *hdir;
  34334. +
  34335. + err = 0;
  34336. + wbr = a->br->br_wbr;
  34337. + /* big aufs lock */
  34338. + si_noflush_write_lock(a->sb);
  34339. + if (!au_br_writable(a->br->br_perm))
  34340. + goto out;
  34341. + bindex = au_br_index(a->sb, a->br->br_id);
  34342. + if (unlikely(bindex < 0))
  34343. + goto out;
  34344. +
  34345. + di_read_lock_parent(a->sb->s_root, AuLock_IR);
  34346. + dir = d_inode(a->sb->s_root);
  34347. + hdir = au_hi(dir, bindex);
  34348. + h_root = au_h_dptr(a->sb->s_root, bindex);
  34349. + AuDebugOn(h_root != au_br_dentry(a->br));
  34350. +
  34351. + au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
  34352. + wbr_wh_write_lock(wbr);
  34353. + err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
  34354. + h_root, a->br);
  34355. + if (!err) {
  34356. + h_path.dentry = wbr->wbr_whbase;
  34357. + h_path.mnt = au_br_mnt(a->br);
  34358. + delegated = NULL;
  34359. + err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
  34360. + /*force*/0);
  34361. + if (unlikely(err == -EWOULDBLOCK)) {
  34362. + pr_warn("cannot retry for NFSv4 delegation"
  34363. + " for an internal unlink\n");
  34364. + iput(delegated);
  34365. + }
  34366. + } else {
  34367. + pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
  34368. + err = 0;
  34369. + }
  34370. + dput(wbr->wbr_whbase);
  34371. + wbr->wbr_whbase = NULL;
  34372. + if (!err)
  34373. + err = au_wh_init(a->br, a->sb);
  34374. + wbr_wh_write_unlock(wbr);
  34375. + au_hn_inode_unlock(hdir);
  34376. + di_read_unlock(a->sb->s_root, AuLock_IR);
  34377. + if (!err)
  34378. + au_fhsm_wrote(a->sb, bindex, /*force*/0);
  34379. +
  34380. +out:
  34381. + if (wbr)
  34382. + atomic_dec(&wbr->wbr_wh_running);
  34383. + au_lcnt_dec(&a->br->br_count);
  34384. + si_write_unlock(a->sb);
  34385. + au_nwt_done(&au_sbi(a->sb)->si_nowait);
  34386. + au_kfree_rcu(a);
  34387. + if (unlikely(err))
  34388. + AuIOErr("err %d\n", err);
  34389. +}
  34390. +
  34391. +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
  34392. +{
  34393. + int do_dec, wkq_err;
  34394. + struct reinit_br_wh *arg;
  34395. +
  34396. + do_dec = 1;
  34397. + if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
  34398. + goto out;
  34399. +
  34400. + /* ignore ENOMEM */
  34401. + arg = kmalloc(sizeof(*arg), GFP_NOFS);
  34402. + if (arg) {
  34403. + /*
  34404. + * dec(wh_running), kfree(arg) and dec(br_count)
  34405. + * in reinit function
  34406. + */
  34407. + arg->sb = sb;
  34408. + arg->br = br;
  34409. + au_lcnt_inc(&br->br_count);
  34410. + wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
  34411. + if (unlikely(wkq_err)) {
  34412. + atomic_dec(&br->br_wbr->wbr_wh_running);
  34413. + au_lcnt_dec(&br->br_count);
  34414. + au_kfree_rcu(arg);
  34415. + }
  34416. + do_dec = 0;
  34417. + }
  34418. +
  34419. +out:
  34420. + if (do_dec)
  34421. + atomic_dec(&br->br_wbr->wbr_wh_running);
  34422. +}
  34423. +
  34424. +/* ---------------------------------------------------------------------- */
  34425. +
  34426. +/*
  34427. + * create the whiteout @wh.
  34428. + */
  34429. +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
  34430. + struct dentry *wh)
  34431. +{
  34432. + int err;
  34433. + struct path h_path = {
  34434. + .dentry = wh
  34435. + };
  34436. + struct au_branch *br;
  34437. + struct au_wbr *wbr;
  34438. + struct dentry *h_parent;
  34439. + struct inode *h_dir, *delegated;
  34440. +
  34441. + h_parent = wh->d_parent; /* dir inode is locked */
  34442. + h_dir = d_inode(h_parent);
  34443. + IMustLock(h_dir);
  34444. +
  34445. + br = au_sbr(sb, bindex);
  34446. + h_path.mnt = au_br_mnt(br);
  34447. + wbr = br->br_wbr;
  34448. + wbr_wh_read_lock(wbr);
  34449. + if (wbr->wbr_whbase) {
  34450. + delegated = NULL;
  34451. + err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
  34452. + if (unlikely(err == -EWOULDBLOCK)) {
  34453. + pr_warn("cannot retry for NFSv4 delegation"
  34454. + " for an internal link\n");
  34455. + iput(delegated);
  34456. + }
  34457. + if (!err || err != -EMLINK)
  34458. + goto out;
  34459. +
  34460. + /* link count full. re-initialize br_whbase. */
  34461. + kick_reinit_br_wh(sb, br);
  34462. + }
  34463. +
  34464. + /* return this error in this context */
  34465. + err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
  34466. + if (!err)
  34467. + au_fhsm_wrote(sb, bindex, /*force*/0);
  34468. +
  34469. +out:
  34470. + wbr_wh_read_unlock(wbr);
  34471. + return err;
  34472. +}
  34473. +
  34474. +/* ---------------------------------------------------------------------- */
  34475. +
  34476. +/*
  34477. + * create or remove the diropq.
  34478. + */
  34479. +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
  34480. + unsigned int flags)
  34481. +{
  34482. + struct dentry *opq_dentry, *h_dentry;
  34483. + struct super_block *sb;
  34484. + struct au_branch *br;
  34485. + int err;
  34486. +
  34487. + sb = dentry->d_sb;
  34488. + br = au_sbr(sb, bindex);
  34489. + h_dentry = au_h_dptr(dentry, bindex);
  34490. + opq_dentry = vfsub_lkup_one(&diropq_name, h_dentry);
  34491. + if (IS_ERR(opq_dentry))
  34492. + goto out;
  34493. +
  34494. + if (au_ftest_diropq(flags, CREATE)) {
  34495. + err = link_or_create_wh(sb, bindex, opq_dentry);
  34496. + if (!err) {
  34497. + au_set_dbdiropq(dentry, bindex);
  34498. + goto out; /* success */
  34499. + }
  34500. + } else {
  34501. + struct path tmp = {
  34502. + .dentry = opq_dentry,
  34503. + .mnt = au_br_mnt(br)
  34504. + };
  34505. + err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &tmp);
  34506. + if (!err)
  34507. + au_set_dbdiropq(dentry, -1);
  34508. + }
  34509. + dput(opq_dentry);
  34510. + opq_dentry = ERR_PTR(err);
  34511. +
  34512. +out:
  34513. + return opq_dentry;
  34514. +}
  34515. +
  34516. +struct do_diropq_args {
  34517. + struct dentry **errp;
  34518. + struct dentry *dentry;
  34519. + aufs_bindex_t bindex;
  34520. + unsigned int flags;
  34521. +};
  34522. +
  34523. +static void call_do_diropq(void *args)
  34524. +{
  34525. + struct do_diropq_args *a = args;
  34526. + *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
  34527. +}
  34528. +
  34529. +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
  34530. + unsigned int flags)
  34531. +{
  34532. + struct dentry *diropq, *h_dentry;
  34533. +
  34534. + h_dentry = au_h_dptr(dentry, bindex);
  34535. + if (!au_test_h_perm_sio(d_inode(h_dentry), MAY_EXEC | MAY_WRITE))
  34536. + diropq = do_diropq(dentry, bindex, flags);
  34537. + else {
  34538. + int wkq_err;
  34539. + struct do_diropq_args args = {
  34540. + .errp = &diropq,
  34541. + .dentry = dentry,
  34542. + .bindex = bindex,
  34543. + .flags = flags
  34544. + };
  34545. +
  34546. + wkq_err = au_wkq_wait(call_do_diropq, &args);
  34547. + if (unlikely(wkq_err))
  34548. + diropq = ERR_PTR(wkq_err);
  34549. + }
  34550. +
  34551. + return diropq;
  34552. +}
  34553. +
  34554. +/* ---------------------------------------------------------------------- */
  34555. +
  34556. +/*
  34557. + * lookup whiteout dentry.
  34558. + * @h_parent: lower parent dentry which must exist and be locked
  34559. + * @base_name: name of dentry which will be whiteouted
  34560. + * returns dentry for whiteout.
  34561. + */
  34562. +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
  34563. + struct au_branch *br)
  34564. +{
  34565. + int err;
  34566. + struct qstr wh_name;
  34567. + struct dentry *wh_dentry;
  34568. +
  34569. + err = au_wh_name_alloc(&wh_name, base_name);
  34570. + wh_dentry = ERR_PTR(err);
  34571. + if (!err) {
  34572. + wh_dentry = vfsub_lkup_one(&wh_name, h_parent);
  34573. + au_kfree_try_rcu(wh_name.name);
  34574. + }
  34575. + return wh_dentry;
  34576. +}
  34577. +
  34578. +/*
  34579. + * link/create a whiteout for @dentry on @bindex.
  34580. + */
  34581. +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
  34582. + struct dentry *h_parent)
  34583. +{
  34584. + struct dentry *wh_dentry;
  34585. + struct super_block *sb;
  34586. + int err;
  34587. +
  34588. + sb = dentry->d_sb;
  34589. + wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
  34590. + if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
  34591. + err = link_or_create_wh(sb, bindex, wh_dentry);
  34592. + if (!err) {
  34593. + au_set_dbwh(dentry, bindex);
  34594. + au_fhsm_wrote(sb, bindex, /*force*/0);
  34595. + } else {
  34596. + dput(wh_dentry);
  34597. + wh_dentry = ERR_PTR(err);
  34598. + }
  34599. + }
  34600. +
  34601. + return wh_dentry;
  34602. +}
  34603. +
  34604. +/* ---------------------------------------------------------------------- */
  34605. +
  34606. +/* Delete all whiteouts in this directory on branch bindex. */
  34607. +static int del_wh_children(struct dentry *h_dentry, struct au_nhash *whlist,
  34608. + aufs_bindex_t bindex, struct au_branch *br)
  34609. +{
  34610. + int err;
  34611. + unsigned long ul, n;
  34612. + struct qstr wh_name;
  34613. + char *p;
  34614. + struct hlist_head *head;
  34615. + struct au_vdir_wh *pos;
  34616. + struct au_vdir_destr *str;
  34617. +
  34618. + err = -ENOMEM;
  34619. + p = (void *)__get_free_page(GFP_NOFS);
  34620. + wh_name.name = p;
  34621. + if (unlikely(!wh_name.name))
  34622. + goto out;
  34623. +
  34624. + err = 0;
  34625. + memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
  34626. + p += AUFS_WH_PFX_LEN;
  34627. + n = whlist->nh_num;
  34628. + head = whlist->nh_head;
  34629. + for (ul = 0; !err && ul < n; ul++, head++) {
  34630. + hlist_for_each_entry(pos, head, wh_hash) {
  34631. + if (pos->wh_bindex != bindex)
  34632. + continue;
  34633. +
  34634. + str = &pos->wh_str;
  34635. + if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
  34636. + memcpy(p, str->name, str->len);
  34637. + wh_name.len = AUFS_WH_PFX_LEN + str->len;
  34638. + err = unlink_wh_name(h_dentry, &wh_name, br);
  34639. + if (!err)
  34640. + continue;
  34641. + break;
  34642. + }
  34643. + AuIOErr("whiteout name too long %.*s\n",
  34644. + str->len, str->name);
  34645. + err = -EIO;
  34646. + break;
  34647. + }
  34648. + }
  34649. + free_page((unsigned long)wh_name.name);
  34650. +
  34651. +out:
  34652. + return err;
  34653. +}
  34654. +
  34655. +struct del_wh_children_args {
  34656. + int *errp;
  34657. + struct dentry *h_dentry;
  34658. + struct au_nhash *whlist;
  34659. + aufs_bindex_t bindex;
  34660. + struct au_branch *br;
  34661. +};
  34662. +
  34663. +static void call_del_wh_children(void *args)
  34664. +{
  34665. + struct del_wh_children_args *a = args;
  34666. + *a->errp = del_wh_children(a->h_dentry, a->whlist, a->bindex, a->br);
  34667. +}
  34668. +
  34669. +/* ---------------------------------------------------------------------- */
  34670. +
  34671. +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
  34672. +{
  34673. + struct au_whtmp_rmdir *whtmp;
  34674. + int err;
  34675. + unsigned int rdhash;
  34676. +
  34677. + SiMustAnyLock(sb);
  34678. +
  34679. + whtmp = kzalloc(sizeof(*whtmp), gfp);
  34680. + if (unlikely(!whtmp)) {
  34681. + whtmp = ERR_PTR(-ENOMEM);
  34682. + goto out;
  34683. + }
  34684. +
  34685. + /* no estimation for dir size */
  34686. + rdhash = au_sbi(sb)->si_rdhash;
  34687. + if (!rdhash)
  34688. + rdhash = AUFS_RDHASH_DEF;
  34689. + err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
  34690. + if (unlikely(err)) {
  34691. + au_kfree_rcu(whtmp);
  34692. + whtmp = ERR_PTR(err);
  34693. + }
  34694. +
  34695. +out:
  34696. + return whtmp;
  34697. +}
  34698. +
  34699. +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
  34700. +{
  34701. + if (whtmp->br)
  34702. + au_lcnt_dec(&whtmp->br->br_count);
  34703. + dput(whtmp->wh_dentry);
  34704. + iput(whtmp->dir);
  34705. + au_nhash_wh_free(&whtmp->whlist);
  34706. + au_kfree_rcu(whtmp);
  34707. +}
  34708. +
  34709. +/*
  34710. + * rmdir the whiteouted temporary named dir @h_dentry.
  34711. + * @whlist: whiteouted children.
  34712. + */
  34713. +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
  34714. + struct dentry *wh_dentry, struct au_nhash *whlist)
  34715. +{
  34716. + int err;
  34717. + unsigned int h_nlink;
  34718. + struct path h_tmp;
  34719. + struct inode *wh_inode, *h_dir;
  34720. + struct au_branch *br;
  34721. +
  34722. + h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
  34723. + IMustLock(h_dir);
  34724. +
  34725. + br = au_sbr(dir->i_sb, bindex);
  34726. + wh_inode = d_inode(wh_dentry);
  34727. + inode_lock_nested(wh_inode, AuLsc_I_CHILD);
  34728. +
  34729. + /*
  34730. + * someone else might change some whiteouts while we were sleeping.
  34731. + * it means this whlist may have an obsoleted entry.
  34732. + */
  34733. + if (!au_test_h_perm_sio(wh_inode, MAY_EXEC | MAY_WRITE))
  34734. + err = del_wh_children(wh_dentry, whlist, bindex, br);
  34735. + else {
  34736. + int wkq_err;
  34737. + struct del_wh_children_args args = {
  34738. + .errp = &err,
  34739. + .h_dentry = wh_dentry,
  34740. + .whlist = whlist,
  34741. + .bindex = bindex,
  34742. + .br = br
  34743. + };
  34744. +
  34745. + wkq_err = au_wkq_wait(call_del_wh_children, &args);
  34746. + if (unlikely(wkq_err))
  34747. + err = wkq_err;
  34748. + }
  34749. + inode_unlock(wh_inode);
  34750. +
  34751. + if (!err) {
  34752. + h_tmp.dentry = wh_dentry;
  34753. + h_tmp.mnt = au_br_mnt(br);
  34754. + h_nlink = h_dir->i_nlink;
  34755. + err = vfsub_rmdir(h_dir, &h_tmp);
  34756. + /* some fs doesn't change the parent nlink in some cases */
  34757. + h_nlink -= h_dir->i_nlink;
  34758. + }
  34759. +
  34760. + if (!err) {
  34761. + if (au_ibtop(dir) == bindex) {
  34762. + /* todo: dir->i_mutex is necessary */
  34763. + au_cpup_attr_timesizes(dir);
  34764. + if (h_nlink)
  34765. + vfsub_drop_nlink(dir);
  34766. + }
  34767. + return 0; /* success */
  34768. + }
  34769. +
  34770. + pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
  34771. + return err;
  34772. +}
  34773. +
  34774. +static void call_rmdir_whtmp(void *args)
  34775. +{
  34776. + int err;
  34777. + aufs_bindex_t bindex;
  34778. + struct au_whtmp_rmdir *a = args;
  34779. + struct super_block *sb;
  34780. + struct dentry *h_parent;
  34781. + struct inode *h_dir;
  34782. + struct au_hinode *hdir;
  34783. +
  34784. + /* rmdir by nfsd may cause deadlock with this i_mutex */
  34785. + /* inode_lock(a->dir); */
  34786. + err = -EROFS;
  34787. + sb = a->dir->i_sb;
  34788. + si_read_lock(sb, !AuLock_FLUSH);
  34789. + if (!au_br_writable(a->br->br_perm))
  34790. + goto out;
  34791. + bindex = au_br_index(sb, a->br->br_id);
  34792. + if (unlikely(bindex < 0))
  34793. + goto out;
  34794. +
  34795. + err = -EIO;
  34796. + ii_write_lock_parent(a->dir);
  34797. + h_parent = dget_parent(a->wh_dentry);
  34798. + h_dir = d_inode(h_parent);
  34799. + hdir = au_hi(a->dir, bindex);
  34800. + err = vfsub_mnt_want_write(au_br_mnt(a->br));
  34801. + if (unlikely(err))
  34802. + goto out_mnt;
  34803. + au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
  34804. + err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
  34805. + a->br);
  34806. + if (!err)
  34807. + err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
  34808. + au_hn_inode_unlock(hdir);
  34809. + vfsub_mnt_drop_write(au_br_mnt(a->br));
  34810. +
  34811. +out_mnt:
  34812. + dput(h_parent);
  34813. + ii_write_unlock(a->dir);
  34814. +out:
  34815. + /* inode_unlock(a->dir); */
  34816. + au_whtmp_rmdir_free(a);
  34817. + si_read_unlock(sb);
  34818. + au_nwt_done(&au_sbi(sb)->si_nowait);
  34819. + if (unlikely(err))
  34820. + AuIOErr("err %d\n", err);
  34821. +}
  34822. +
  34823. +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
  34824. + struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
  34825. +{
  34826. + int wkq_err;
  34827. + struct super_block *sb;
  34828. +
  34829. + IMustLock(dir);
  34830. +
  34831. + /* all post-process will be done in do_rmdir_whtmp(). */
  34832. + sb = dir->i_sb;
  34833. + args->dir = au_igrab(dir);
  34834. + args->br = au_sbr(sb, bindex);
  34835. + au_lcnt_inc(&args->br->br_count);
  34836. + args->wh_dentry = dget(wh_dentry);
  34837. + wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
  34838. + if (unlikely(wkq_err)) {
  34839. + pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
  34840. + au_whtmp_rmdir_free(args);
  34841. + }
  34842. +}
  34843. diff -Naur null/fs/aufs/whout.h linux-5.1/fs/aufs/whout.h
  34844. --- /dev/null
  34845. +++ linux-5.1/fs/aufs/whout.h 2019-05-22 11:23:07.444140858 +0200
  34846. @@ -0,0 +1,86 @@
  34847. +/* SPDX-License-Identifier: GPL-2.0 */
  34848. +/*
  34849. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  34850. + *
  34851. + * This program, aufs is free software; you can redistribute it and/or modify
  34852. + * it under the terms of the GNU General Public License as published by
  34853. + * the Free Software Foundation; either version 2 of the License, or
  34854. + * (at your option) any later version.
  34855. + *
  34856. + * This program is distributed in the hope that it will be useful,
  34857. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  34858. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34859. + * GNU General Public License for more details.
  34860. + *
  34861. + * You should have received a copy of the GNU General Public License
  34862. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  34863. + */
  34864. +
  34865. +/*
  34866. + * whiteout for logical deletion and opaque directory
  34867. + */
  34868. +
  34869. +#ifndef __AUFS_WHOUT_H__
  34870. +#define __AUFS_WHOUT_H__
  34871. +
  34872. +#ifdef __KERNEL__
  34873. +
  34874. +#include "dir.h"
  34875. +
  34876. +/* whout.c */
  34877. +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
  34878. +int au_wh_test(struct dentry *h_parent, struct qstr *wh_name, int try_sio);
  34879. +int au_diropq_test(struct dentry *h_dentry);
  34880. +struct au_branch;
  34881. +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
  34882. + struct qstr *prefix);
  34883. +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
  34884. +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
  34885. + struct dentry *dentry);
  34886. +int au_wh_init(struct au_branch *br, struct super_block *sb);
  34887. +
  34888. +/* diropq flags */
  34889. +#define AuDiropq_CREATE 1
  34890. +#define au_ftest_diropq(flags, name) ((flags) & AuDiropq_##name)
  34891. +#define au_fset_diropq(flags, name) \
  34892. + do { (flags) |= AuDiropq_##name; } while (0)
  34893. +#define au_fclr_diropq(flags, name) \
  34894. + do { (flags) &= ~AuDiropq_##name; } while (0)
  34895. +
  34896. +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
  34897. + unsigned int flags);
  34898. +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
  34899. + struct au_branch *br);
  34900. +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
  34901. + struct dentry *h_parent);
  34902. +
  34903. +/* real rmdir for the whiteout-ed dir */
  34904. +struct au_whtmp_rmdir {
  34905. + struct inode *dir;
  34906. + struct au_branch *br;
  34907. + struct dentry *wh_dentry;
  34908. + struct au_nhash whlist;
  34909. +};
  34910. +
  34911. +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
  34912. +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
  34913. +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
  34914. + struct dentry *wh_dentry, struct au_nhash *whlist);
  34915. +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
  34916. + struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
  34917. +
  34918. +/* ---------------------------------------------------------------------- */
  34919. +
  34920. +static inline struct dentry *au_diropq_create(struct dentry *dentry,
  34921. + aufs_bindex_t bindex)
  34922. +{
  34923. + return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
  34924. +}
  34925. +
  34926. +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
  34927. +{
  34928. + return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
  34929. +}
  34930. +
  34931. +#endif /* __KERNEL__ */
  34932. +#endif /* __AUFS_WHOUT_H__ */
  34933. diff -Naur null/fs/aufs/wkq.c linux-5.1/fs/aufs/wkq.c
  34934. --- /dev/null
  34935. +++ linux-5.1/fs/aufs/wkq.c 2019-05-22 11:23:07.445140813 +0200
  34936. @@ -0,0 +1,392 @@
  34937. +// SPDX-License-Identifier: GPL-2.0
  34938. +/*
  34939. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  34940. + *
  34941. + * This program, aufs is free software; you can redistribute it and/or modify
  34942. + * it under the terms of the GNU General Public License as published by
  34943. + * the Free Software Foundation; either version 2 of the License, or
  34944. + * (at your option) any later version.
  34945. + *
  34946. + * This program is distributed in the hope that it will be useful,
  34947. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  34948. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34949. + * GNU General Public License for more details.
  34950. + *
  34951. + * You should have received a copy of the GNU General Public License
  34952. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  34953. + */
  34954. +
  34955. +/*
  34956. + * workqueue for asynchronous/super-io operations
  34957. + * todo: try new credential scheme
  34958. + */
  34959. +
  34960. +#include <linux/module.h>
  34961. +#include "aufs.h"
  34962. +
  34963. +/* internal workqueue named AUFS_WKQ_NAME */
  34964. +
  34965. +static struct workqueue_struct *au_wkq;
  34966. +
  34967. +struct au_wkinfo {
  34968. + struct work_struct wk;
  34969. + struct kobject *kobj;
  34970. +
  34971. + unsigned int flags; /* see wkq.h */
  34972. +
  34973. + au_wkq_func_t func;
  34974. + void *args;
  34975. +
  34976. +#ifdef CONFIG_LOCKDEP
  34977. + int dont_check;
  34978. + struct held_lock **hlock;
  34979. +#endif
  34980. +
  34981. + struct completion *comp;
  34982. +};
  34983. +
  34984. +/* ---------------------------------------------------------------------- */
  34985. +/*
  34986. + * Aufs passes some operations to the workqueue such as the internal copyup.
  34987. + * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
  34988. + * job run by workqueue depends upon the locks acquired in the other task.
  34989. + * Delegating a small operation to the workqueue, aufs passes its lockdep
  34990. + * information too. And the job in the workqueue restores the info in order to
  34991. + * pretend as if it acquired those locks. This is just to make LOCKDEP work
  34992. + * correctly and expectedly.
  34993. + */
  34994. +
  34995. +#ifndef CONFIG_LOCKDEP
  34996. +AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
  34997. +AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
  34998. +AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
  34999. +AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
  35000. +AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
  35001. +#else
  35002. +static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
  35003. +{
  35004. + wkinfo->hlock = NULL;
  35005. + wkinfo->dont_check = 0;
  35006. +}
  35007. +
  35008. +/*
  35009. + * 1: matched
  35010. + * 0: unmatched
  35011. + */
  35012. +static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
  35013. +{
  35014. + static DEFINE_SPINLOCK(spin);
  35015. + static struct {
  35016. + char *name;
  35017. + struct lock_class_key *key;
  35018. + } a[] = {
  35019. + { .name = "&sbinfo->si_rwsem" },
  35020. + { .name = "&finfo->fi_rwsem" },
  35021. + { .name = "&dinfo->di_rwsem" },
  35022. + { .name = "&iinfo->ii_rwsem" }
  35023. + };
  35024. + static int set;
  35025. + int i;
  35026. +
  35027. + /* lockless read from 'set.' see below */
  35028. + if (set == ARRAY_SIZE(a)) {
  35029. + for (i = 0; i < ARRAY_SIZE(a); i++)
  35030. + if (a[i].key == key)
  35031. + goto match;
  35032. + goto unmatch;
  35033. + }
  35034. +
  35035. + spin_lock(&spin);
  35036. + if (set)
  35037. + for (i = 0; i < ARRAY_SIZE(a); i++)
  35038. + if (a[i].key == key) {
  35039. + spin_unlock(&spin);
  35040. + goto match;
  35041. + }
  35042. + for (i = 0; i < ARRAY_SIZE(a); i++) {
  35043. + if (a[i].key) {
  35044. + if (unlikely(a[i].key == key)) { /* rare but possible */
  35045. + spin_unlock(&spin);
  35046. + goto match;
  35047. + } else
  35048. + continue;
  35049. + }
  35050. + if (strstr(a[i].name, name)) {
  35051. + /*
  35052. + * the order of these three lines is important for the
  35053. + * lockless read above.
  35054. + */
  35055. + a[i].key = key;
  35056. + spin_unlock(&spin);
  35057. + set++;
  35058. + /* AuDbg("%d, %s\n", set, name); */
  35059. + goto match;
  35060. + }
  35061. + }
  35062. + spin_unlock(&spin);
  35063. + goto unmatch;
  35064. +
  35065. +match:
  35066. + return 1;
  35067. +unmatch:
  35068. + return 0;
  35069. +}
  35070. +
  35071. +static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
  35072. +{
  35073. + int err, n;
  35074. + struct task_struct *curr;
  35075. + struct held_lock **hl, *held_locks, *p;
  35076. +
  35077. + err = 0;
  35078. + curr = current;
  35079. + wkinfo->dont_check = lockdep_recursing(curr);
  35080. + if (wkinfo->dont_check)
  35081. + goto out;
  35082. + n = curr->lockdep_depth;
  35083. + if (!n)
  35084. + goto out;
  35085. +
  35086. + err = -ENOMEM;
  35087. + wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
  35088. + if (unlikely(!wkinfo->hlock))
  35089. + goto out;
  35090. +
  35091. + err = 0;
  35092. +#if 0
  35093. + if (0 && au_debug_test()) /* left for debugging */
  35094. + lockdep_print_held_locks(curr);
  35095. +#endif
  35096. + held_locks = curr->held_locks;
  35097. + hl = wkinfo->hlock;
  35098. + while (n--) {
  35099. + p = held_locks++;
  35100. + if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
  35101. + *hl++ = p;
  35102. + }
  35103. + *hl = NULL;
  35104. +
  35105. +out:
  35106. + return err;
  35107. +}
  35108. +
  35109. +static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
  35110. +{
  35111. + au_kfree_try_rcu(wkinfo->hlock);
  35112. +}
  35113. +
  35114. +static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
  35115. +{
  35116. + struct held_lock *p, **hl = wkinfo->hlock;
  35117. + int subclass;
  35118. +
  35119. + if (wkinfo->dont_check)
  35120. + lockdep_off();
  35121. + if (!hl)
  35122. + return;
  35123. + while ((p = *hl++)) { /* assignment */
  35124. + subclass = lockdep_hlock_class(p)->subclass;
  35125. + /* AuDbg("%s, %d\n", p->instance->name, subclass); */
  35126. + if (p->read)
  35127. + rwsem_acquire_read(p->instance, subclass, 0,
  35128. + /*p->acquire_ip*/_RET_IP_);
  35129. + else
  35130. + rwsem_acquire(p->instance, subclass, 0,
  35131. + /*p->acquire_ip*/_RET_IP_);
  35132. + }
  35133. +}
  35134. +
  35135. +static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
  35136. +{
  35137. + struct held_lock *p, **hl = wkinfo->hlock;
  35138. +
  35139. + if (wkinfo->dont_check)
  35140. + lockdep_on();
  35141. + if (!hl)
  35142. + return;
  35143. + while ((p = *hl++)) /* assignment */
  35144. + rwsem_release(p->instance, 0, /*p->acquire_ip*/_RET_IP_);
  35145. +}
  35146. +#endif
  35147. +
  35148. +static void wkq_func(struct work_struct *wk)
  35149. +{
  35150. + struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
  35151. +
  35152. + AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
  35153. + AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
  35154. +
  35155. + au_wkq_lockdep_pre(wkinfo);
  35156. + wkinfo->func(wkinfo->args);
  35157. + au_wkq_lockdep_post(wkinfo);
  35158. + if (au_ftest_wkq(wkinfo->flags, WAIT))
  35159. + complete(wkinfo->comp);
  35160. + else {
  35161. + kobject_put(wkinfo->kobj);
  35162. + module_put(THIS_MODULE); /* todo: ?? */
  35163. + au_kfree_rcu(wkinfo);
  35164. + }
  35165. +}
  35166. +
  35167. +/*
  35168. + * Since struct completion is large, try allocating it dynamically.
  35169. + */
  35170. +#if 1 /* defined(CONFIG_4KSTACKS) || defined(AuTest4KSTACKS) */
  35171. +#define AuWkqCompDeclare(name) struct completion *comp = NULL
  35172. +
  35173. +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
  35174. +{
  35175. + *comp = kmalloc(sizeof(**comp), GFP_NOFS);
  35176. + if (*comp) {
  35177. + init_completion(*comp);
  35178. + wkinfo->comp = *comp;
  35179. + return 0;
  35180. + }
  35181. + return -ENOMEM;
  35182. +}
  35183. +
  35184. +static void au_wkq_comp_free(struct completion *comp)
  35185. +{
  35186. + au_kfree_rcu(comp);
  35187. +}
  35188. +
  35189. +#else
  35190. +
  35191. +/* no braces */
  35192. +#define AuWkqCompDeclare(name) \
  35193. + DECLARE_COMPLETION_ONSTACK(_ ## name); \
  35194. + struct completion *comp = &_ ## name
  35195. +
  35196. +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
  35197. +{
  35198. + wkinfo->comp = *comp;
  35199. + return 0;
  35200. +}
  35201. +
  35202. +static void au_wkq_comp_free(struct completion *comp __maybe_unused)
  35203. +{
  35204. + /* empty */
  35205. +}
  35206. +#endif /* 4KSTACKS */
  35207. +
  35208. +static void au_wkq_run(struct au_wkinfo *wkinfo)
  35209. +{
  35210. + if (au_ftest_wkq(wkinfo->flags, NEST)) {
  35211. + if (au_wkq_test()) {
  35212. + AuWarn1("wkq from wkq, unless silly-rename on NFS,"
  35213. + " due to a dead dir by UDBA,"
  35214. + " or async xino write?\n");
  35215. + AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
  35216. + }
  35217. + } else
  35218. + au_dbg_verify_kthread();
  35219. +
  35220. + if (au_ftest_wkq(wkinfo->flags, WAIT)) {
  35221. + INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
  35222. + queue_work(au_wkq, &wkinfo->wk);
  35223. + } else {
  35224. + INIT_WORK(&wkinfo->wk, wkq_func);
  35225. + schedule_work(&wkinfo->wk);
  35226. + }
  35227. +}
  35228. +
  35229. +/*
  35230. + * Be careful. It is easy to make deadlock happen.
  35231. + * processA: lock, wkq and wait
  35232. + * processB: wkq and wait, lock in wkq
  35233. + * --> deadlock
  35234. + */
  35235. +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
  35236. +{
  35237. + int err;
  35238. + AuWkqCompDeclare(comp);
  35239. + struct au_wkinfo wkinfo = {
  35240. + .flags = flags,
  35241. + .func = func,
  35242. + .args = args
  35243. + };
  35244. +
  35245. + err = au_wkq_comp_alloc(&wkinfo, &comp);
  35246. + if (unlikely(err))
  35247. + goto out;
  35248. + err = au_wkq_lockdep_alloc(&wkinfo);
  35249. + if (unlikely(err))
  35250. + goto out_comp;
  35251. + if (!err) {
  35252. + au_wkq_run(&wkinfo);
  35253. + /* no timeout, no interrupt */
  35254. + wait_for_completion(wkinfo.comp);
  35255. + }
  35256. + au_wkq_lockdep_free(&wkinfo);
  35257. +
  35258. +out_comp:
  35259. + au_wkq_comp_free(comp);
  35260. +out:
  35261. + destroy_work_on_stack(&wkinfo.wk);
  35262. + return err;
  35263. +}
  35264. +
  35265. +/*
  35266. + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
  35267. + * problem in a concurrent umounting.
  35268. + */
  35269. +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
  35270. + unsigned int flags)
  35271. +{
  35272. + int err;
  35273. + struct au_wkinfo *wkinfo;
  35274. +
  35275. + atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
  35276. +
  35277. + /*
  35278. + * wkq_func() must free this wkinfo.
  35279. + * it highly depends upon the implementation of workqueue.
  35280. + */
  35281. + err = 0;
  35282. + wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
  35283. + if (wkinfo) {
  35284. + wkinfo->kobj = &au_sbi(sb)->si_kobj;
  35285. + wkinfo->flags = flags & ~AuWkq_WAIT;
  35286. + wkinfo->func = func;
  35287. + wkinfo->args = args;
  35288. + wkinfo->comp = NULL;
  35289. + au_wkq_lockdep_init(wkinfo);
  35290. + kobject_get(wkinfo->kobj);
  35291. + __module_get(THIS_MODULE); /* todo: ?? */
  35292. +
  35293. + au_wkq_run(wkinfo);
  35294. + } else {
  35295. + err = -ENOMEM;
  35296. + au_nwt_done(&au_sbi(sb)->si_nowait);
  35297. + }
  35298. +
  35299. + return err;
  35300. +}
  35301. +
  35302. +/* ---------------------------------------------------------------------- */
  35303. +
  35304. +void au_nwt_init(struct au_nowait_tasks *nwt)
  35305. +{
  35306. + atomic_set(&nwt->nw_len, 0);
  35307. + /* smp_mb(); */ /* atomic_set */
  35308. + init_waitqueue_head(&nwt->nw_wq);
  35309. +}
  35310. +
  35311. +void au_wkq_fin(void)
  35312. +{
  35313. + destroy_workqueue(au_wkq);
  35314. +}
  35315. +
  35316. +int __init au_wkq_init(void)
  35317. +{
  35318. + int err;
  35319. +
  35320. + err = 0;
  35321. + au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
  35322. + if (IS_ERR(au_wkq))
  35323. + err = PTR_ERR(au_wkq);
  35324. + else if (!au_wkq)
  35325. + err = -ENOMEM;
  35326. +
  35327. + return err;
  35328. +}
  35329. diff -Naur null/fs/aufs/wkq.h linux-5.1/fs/aufs/wkq.h
  35330. --- /dev/null
  35331. +++ linux-5.1/fs/aufs/wkq.h 2019-05-22 11:23:07.445140813 +0200
  35332. @@ -0,0 +1,89 @@
  35333. +/* SPDX-License-Identifier: GPL-2.0 */
  35334. +/*
  35335. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  35336. + *
  35337. + * This program, aufs is free software; you can redistribute it and/or modify
  35338. + * it under the terms of the GNU General Public License as published by
  35339. + * the Free Software Foundation; either version 2 of the License, or
  35340. + * (at your option) any later version.
  35341. + *
  35342. + * This program is distributed in the hope that it will be useful,
  35343. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35344. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  35345. + * GNU General Public License for more details.
  35346. + *
  35347. + * You should have received a copy of the GNU General Public License
  35348. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  35349. + */
  35350. +
  35351. +/*
  35352. + * workqueue for asynchronous/super-io operations
  35353. + * todo: try new credentials management scheme
  35354. + */
  35355. +
  35356. +#ifndef __AUFS_WKQ_H__
  35357. +#define __AUFS_WKQ_H__
  35358. +
  35359. +#ifdef __KERNEL__
  35360. +
  35361. +#include <linux/wait.h>
  35362. +
  35363. +struct super_block;
  35364. +
  35365. +/* ---------------------------------------------------------------------- */
  35366. +
  35367. +/*
  35368. + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
  35369. + */
  35370. +struct au_nowait_tasks {
  35371. + atomic_t nw_len;
  35372. + wait_queue_head_t nw_wq;
  35373. +};
  35374. +
  35375. +/* ---------------------------------------------------------------------- */
  35376. +
  35377. +typedef void (*au_wkq_func_t)(void *args);
  35378. +
  35379. +/* wkq flags */
  35380. +#define AuWkq_WAIT 1
  35381. +#define AuWkq_NEST (1 << 1)
  35382. +#define au_ftest_wkq(flags, name) ((flags) & AuWkq_##name)
  35383. +#define au_fset_wkq(flags, name) \
  35384. + do { (flags) |= AuWkq_##name; } while (0)
  35385. +#define au_fclr_wkq(flags, name) \
  35386. + do { (flags) &= ~AuWkq_##name; } while (0)
  35387. +
  35388. +/* wkq.c */
  35389. +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
  35390. +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
  35391. + unsigned int flags);
  35392. +void au_nwt_init(struct au_nowait_tasks *nwt);
  35393. +int __init au_wkq_init(void);
  35394. +void au_wkq_fin(void);
  35395. +
  35396. +/* ---------------------------------------------------------------------- */
  35397. +
  35398. +static inline int au_wkq_test(void)
  35399. +{
  35400. + return current->flags & PF_WQ_WORKER;
  35401. +}
  35402. +
  35403. +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
  35404. +{
  35405. + return au_wkq_do_wait(AuWkq_WAIT, func, args);
  35406. +}
  35407. +
  35408. +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
  35409. +{
  35410. + if (atomic_dec_and_test(&nwt->nw_len))
  35411. + wake_up_all(&nwt->nw_wq);
  35412. +}
  35413. +
  35414. +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
  35415. +{
  35416. + wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
  35417. + return 0;
  35418. +}
  35419. +
  35420. +#endif /* __KERNEL__ */
  35421. +#endif /* __AUFS_WKQ_H__ */
  35422. diff -Naur null/fs/aufs/xattr.c linux-5.1/fs/aufs/xattr.c
  35423. --- /dev/null
  35424. +++ linux-5.1/fs/aufs/xattr.c 2019-05-22 11:23:07.445140813 +0200
  35425. @@ -0,0 +1,356 @@
  35426. +// SPDX-License-Identifier: GPL-2.0
  35427. +/*
  35428. + * Copyright (C) 2014-2019 Junjiro R. Okajima
  35429. + *
  35430. + * This program, aufs is free software; you can redistribute it and/or modify
  35431. + * it under the terms of the GNU General Public License as published by
  35432. + * the Free Software Foundation; either version 2 of the License, or
  35433. + * (at your option) any later version.
  35434. + *
  35435. + * This program is distributed in the hope that it will be useful,
  35436. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35437. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  35438. + * GNU General Public License for more details.
  35439. + *
  35440. + * You should have received a copy of the GNU General Public License
  35441. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  35442. + */
  35443. +
  35444. +/*
  35445. + * handling xattr functions
  35446. + */
  35447. +
  35448. +#include <linux/fs.h>
  35449. +#include <linux/posix_acl_xattr.h>
  35450. +#include <linux/xattr.h>
  35451. +#include "aufs.h"
  35452. +
  35453. +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
  35454. +{
  35455. + if (!ignore_flags)
  35456. + goto out;
  35457. + switch (err) {
  35458. + case -ENOMEM:
  35459. + case -EDQUOT:
  35460. + goto out;
  35461. + }
  35462. +
  35463. + if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
  35464. + err = 0;
  35465. + goto out;
  35466. + }
  35467. +
  35468. +#define cmp(brattr, prefix) do { \
  35469. + if (!strncmp(name, XATTR_##prefix##_PREFIX, \
  35470. + XATTR_##prefix##_PREFIX_LEN)) { \
  35471. + if (ignore_flags & AuBrAttr_ICEX_##brattr) \
  35472. + err = 0; \
  35473. + goto out; \
  35474. + } \
  35475. + } while (0)
  35476. +
  35477. + cmp(SEC, SECURITY);
  35478. + cmp(SYS, SYSTEM);
  35479. + cmp(TR, TRUSTED);
  35480. + cmp(USR, USER);
  35481. +#undef cmp
  35482. +
  35483. + if (ignore_flags & AuBrAttr_ICEX_OTH)
  35484. + err = 0;
  35485. +
  35486. +out:
  35487. + return err;
  35488. +}
  35489. +
  35490. +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
  35491. +
  35492. +static int au_do_cpup_xattr(struct dentry *h_dst, struct dentry *h_src,
  35493. + char *name, char **buf, unsigned int ignore_flags,
  35494. + unsigned int verbose)
  35495. +{
  35496. + int err;
  35497. + ssize_t ssz;
  35498. + struct inode *h_idst;
  35499. +
  35500. + ssz = vfs_getxattr_alloc(h_src, name, buf, 0, GFP_NOFS);
  35501. + err = ssz;
  35502. + if (unlikely(err <= 0)) {
  35503. + if (err == -ENODATA
  35504. + || (err == -EOPNOTSUPP
  35505. + && ((ignore_flags & au_xattr_out_of_list)
  35506. + || (au_test_nfs_noacl(d_inode(h_src))
  35507. + && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
  35508. + || !strcmp(name,
  35509. + XATTR_NAME_POSIX_ACL_DEFAULT))))
  35510. + ))
  35511. + err = 0;
  35512. + if (err && (verbose || au_debug_test()))
  35513. + pr_err("%s, err %d\n", name, err);
  35514. + goto out;
  35515. + }
  35516. +
  35517. + /* unlock it temporary */
  35518. + h_idst = d_inode(h_dst);
  35519. + inode_unlock(h_idst);
  35520. + err = vfsub_setxattr(h_dst, name, *buf, ssz, /*flags*/0);
  35521. + inode_lock_nested(h_idst, AuLsc_I_CHILD2);
  35522. + if (unlikely(err)) {
  35523. + if (verbose || au_debug_test())
  35524. + pr_err("%s, err %d\n", name, err);
  35525. + err = au_xattr_ignore(err, name, ignore_flags);
  35526. + }
  35527. +
  35528. +out:
  35529. + return err;
  35530. +}
  35531. +
  35532. +int au_cpup_xattr(struct dentry *h_dst, struct dentry *h_src, int ignore_flags,
  35533. + unsigned int verbose)
  35534. +{
  35535. + int err, unlocked, acl_access, acl_default;
  35536. + ssize_t ssz;
  35537. + struct inode *h_isrc, *h_idst;
  35538. + char *value, *p, *o, *e;
  35539. +
  35540. + /* try stopping to update the source inode while we are referencing */
  35541. + /* there should not be the parent-child relationship between them */
  35542. + h_isrc = d_inode(h_src);
  35543. + h_idst = d_inode(h_dst);
  35544. + inode_unlock(h_idst);
  35545. + inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
  35546. + inode_lock_nested(h_idst, AuLsc_I_CHILD2);
  35547. + unlocked = 0;
  35548. +
  35549. + /* some filesystems don't list POSIX ACL, for example tmpfs */
  35550. + ssz = vfs_listxattr(h_src, NULL, 0);
  35551. + err = ssz;
  35552. + if (unlikely(err < 0)) {
  35553. + AuTraceErr(err);
  35554. + if (err == -ENODATA
  35555. + || err == -EOPNOTSUPP)
  35556. + err = 0; /* ignore */
  35557. + goto out;
  35558. + }
  35559. +
  35560. + err = 0;
  35561. + p = NULL;
  35562. + o = NULL;
  35563. + if (ssz) {
  35564. + err = -ENOMEM;
  35565. + p = kmalloc(ssz, GFP_NOFS);
  35566. + o = p;
  35567. + if (unlikely(!p))
  35568. + goto out;
  35569. + err = vfs_listxattr(h_src, p, ssz);
  35570. + }
  35571. + inode_unlock_shared(h_isrc);
  35572. + unlocked = 1;
  35573. + AuDbg("err %d, ssz %zd\n", err, ssz);
  35574. + if (unlikely(err < 0))
  35575. + goto out_free;
  35576. +
  35577. + err = 0;
  35578. + e = p + ssz;
  35579. + value = NULL;
  35580. + acl_access = 0;
  35581. + acl_default = 0;
  35582. + while (!err && p < e) {
  35583. + acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
  35584. + sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
  35585. + acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
  35586. + sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
  35587. + - 1);
  35588. + err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
  35589. + verbose);
  35590. + p += strlen(p) + 1;
  35591. + }
  35592. + AuTraceErr(err);
  35593. + ignore_flags |= au_xattr_out_of_list;
  35594. + if (!err && !acl_access) {
  35595. + err = au_do_cpup_xattr(h_dst, h_src,
  35596. + XATTR_NAME_POSIX_ACL_ACCESS, &value,
  35597. + ignore_flags, verbose);
  35598. + AuTraceErr(err);
  35599. + }
  35600. + if (!err && !acl_default) {
  35601. + err = au_do_cpup_xattr(h_dst, h_src,
  35602. + XATTR_NAME_POSIX_ACL_DEFAULT, &value,
  35603. + ignore_flags, verbose);
  35604. + AuTraceErr(err);
  35605. + }
  35606. +
  35607. + au_kfree_try_rcu(value);
  35608. +
  35609. +out_free:
  35610. + au_kfree_try_rcu(o);
  35611. +out:
  35612. + if (!unlocked)
  35613. + inode_unlock_shared(h_isrc);
  35614. + AuTraceErr(err);
  35615. + return err;
  35616. +}
  35617. +
  35618. +/* ---------------------------------------------------------------------- */
  35619. +
  35620. +static int au_smack_reentering(struct super_block *sb)
  35621. +{
  35622. +#if IS_ENABLED(CONFIG_SECURITY_SMACK)
  35623. + /*
  35624. + * as a part of lookup, smack_d_instantiate() is called, and it calls
  35625. + * i_op->getxattr(). ouch.
  35626. + */
  35627. + return si_pid_test(sb);
  35628. +#else
  35629. + return 0;
  35630. +#endif
  35631. +}
  35632. +
  35633. +enum {
  35634. + AU_XATTR_LIST,
  35635. + AU_XATTR_GET
  35636. +};
  35637. +
  35638. +struct au_lgxattr {
  35639. + int type;
  35640. + union {
  35641. + struct {
  35642. + char *list;
  35643. + size_t size;
  35644. + } list;
  35645. + struct {
  35646. + const char *name;
  35647. + void *value;
  35648. + size_t size;
  35649. + } get;
  35650. + } u;
  35651. +};
  35652. +
  35653. +static ssize_t au_lgxattr(struct dentry *dentry, struct au_lgxattr *arg)
  35654. +{
  35655. + ssize_t err;
  35656. + int reenter;
  35657. + struct path h_path;
  35658. + struct super_block *sb;
  35659. +
  35660. + sb = dentry->d_sb;
  35661. + reenter = au_smack_reentering(sb);
  35662. + if (!reenter) {
  35663. + err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
  35664. + if (unlikely(err))
  35665. + goto out;
  35666. + }
  35667. + err = au_h_path_getattr(dentry, /*force*/1, &h_path, reenter);
  35668. + if (unlikely(err))
  35669. + goto out_si;
  35670. + if (unlikely(!h_path.dentry))
  35671. + /* illegally overlapped or something */
  35672. + goto out_di; /* pretending success */
  35673. +
  35674. + /* always topmost entry only */
  35675. + switch (arg->type) {
  35676. + case AU_XATTR_LIST:
  35677. + err = vfs_listxattr(h_path.dentry,
  35678. + arg->u.list.list, arg->u.list.size);
  35679. + break;
  35680. + case AU_XATTR_GET:
  35681. + AuDebugOn(d_is_negative(h_path.dentry));
  35682. + err = vfs_getxattr(h_path.dentry,
  35683. + arg->u.get.name, arg->u.get.value,
  35684. + arg->u.get.size);
  35685. + break;
  35686. + }
  35687. +
  35688. +out_di:
  35689. + if (!reenter)
  35690. + di_read_unlock(dentry, AuLock_IR);
  35691. +out_si:
  35692. + if (!reenter)
  35693. + si_read_unlock(sb);
  35694. +out:
  35695. + AuTraceErr(err);
  35696. + return err;
  35697. +}
  35698. +
  35699. +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
  35700. +{
  35701. + struct au_lgxattr arg = {
  35702. + .type = AU_XATTR_LIST,
  35703. + .u.list = {
  35704. + .list = list,
  35705. + .size = size
  35706. + },
  35707. + };
  35708. +
  35709. + return au_lgxattr(dentry, &arg);
  35710. +}
  35711. +
  35712. +static ssize_t au_getxattr(struct dentry *dentry,
  35713. + struct inode *inode __maybe_unused,
  35714. + const char *name, void *value, size_t size)
  35715. +{
  35716. + struct au_lgxattr arg = {
  35717. + .type = AU_XATTR_GET,
  35718. + .u.get = {
  35719. + .name = name,
  35720. + .value = value,
  35721. + .size = size
  35722. + },
  35723. + };
  35724. +
  35725. + return au_lgxattr(dentry, &arg);
  35726. +}
  35727. +
  35728. +static int au_setxattr(struct dentry *dentry, struct inode *inode,
  35729. + const char *name, const void *value, size_t size,
  35730. + int flags)
  35731. +{
  35732. + struct au_sxattr arg = {
  35733. + .type = AU_XATTR_SET,
  35734. + .u.set = {
  35735. + .name = name,
  35736. + .value = value,
  35737. + .size = size,
  35738. + .flags = flags
  35739. + },
  35740. + };
  35741. +
  35742. + return au_sxattr(dentry, inode, &arg);
  35743. +}
  35744. +
  35745. +/* ---------------------------------------------------------------------- */
  35746. +
  35747. +static int au_xattr_get(const struct xattr_handler *handler,
  35748. + struct dentry *dentry, struct inode *inode,
  35749. + const char *name, void *buffer, size_t size)
  35750. +{
  35751. + return au_getxattr(dentry, inode, name, buffer, size);
  35752. +}
  35753. +
  35754. +static int au_xattr_set(const struct xattr_handler *handler,
  35755. + struct dentry *dentry, struct inode *inode,
  35756. + const char *name, const void *value, size_t size,
  35757. + int flags)
  35758. +{
  35759. + return au_setxattr(dentry, inode, name, value, size, flags);
  35760. +}
  35761. +
  35762. +static const struct xattr_handler au_xattr_handler = {
  35763. + .name = "",
  35764. + .prefix = "",
  35765. + .get = au_xattr_get,
  35766. + .set = au_xattr_set
  35767. +};
  35768. +
  35769. +static const struct xattr_handler *au_xattr_handlers[] = {
  35770. +#ifdef CONFIG_FS_POSIX_ACL
  35771. + &posix_acl_access_xattr_handler,
  35772. + &posix_acl_default_xattr_handler,
  35773. +#endif
  35774. + &au_xattr_handler, /* must be last */
  35775. + NULL
  35776. +};
  35777. +
  35778. +void au_xattr_init(struct super_block *sb)
  35779. +{
  35780. + sb->s_xattr = au_xattr_handlers;
  35781. +}
  35782. diff -Naur null/fs/aufs/xino.c linux-5.1/fs/aufs/xino.c
  35783. --- /dev/null
  35784. +++ linux-5.1/fs/aufs/xino.c 2019-05-22 11:23:07.446140766 +0200
  35785. @@ -0,0 +1,1956 @@
  35786. +// SPDX-License-Identifier: GPL-2.0
  35787. +/*
  35788. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  35789. + *
  35790. + * This program, aufs is free software; you can redistribute it and/or modify
  35791. + * it under the terms of the GNU General Public License as published by
  35792. + * the Free Software Foundation; either version 2 of the License, or
  35793. + * (at your option) any later version.
  35794. + *
  35795. + * This program is distributed in the hope that it will be useful,
  35796. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  35797. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  35798. + * GNU General Public License for more details.
  35799. + *
  35800. + * You should have received a copy of the GNU General Public License
  35801. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  35802. + */
  35803. +
  35804. +/*
  35805. + * external inode number translation table and bitmap
  35806. + *
  35807. + * things to consider
  35808. + * - the lifetime
  35809. + * + au_xino object
  35810. + * + XINO files (xino, xib, xigen)
  35811. + * + dynamic debugfs entries (xiN)
  35812. + * + static debugfs entries (xib, xigen)
  35813. + * + static sysfs entry (xi_path)
  35814. + * - several entry points to handle them.
  35815. + * + mount(2) without xino option (default)
  35816. + * + mount(2) with xino option
  35817. + * + mount(2) with noxino option
  35818. + * + umount(2)
  35819. + * + remount with add/del branches
  35820. + * + remount with xino/noxino options
  35821. + */
  35822. +
  35823. +#include <linux/seq_file.h>
  35824. +#include <linux/statfs.h>
  35825. +#include "aufs.h"
  35826. +
  35827. +static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
  35828. + aufs_bindex_t bbot,
  35829. + struct super_block *h_sb)
  35830. +{
  35831. + /* todo: try binary-search if the branches are many */
  35832. + for (; btop <= bbot; btop++)
  35833. + if (h_sb == au_sbr_sb(sb, btop))
  35834. + return btop;
  35835. + return -1;
  35836. +}
  35837. +
  35838. +/*
  35839. + * find another branch who is on the same filesystem of the specified
  35840. + * branch{@btgt}. search until @bbot.
  35841. + */
  35842. +static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
  35843. + aufs_bindex_t bbot)
  35844. +{
  35845. + aufs_bindex_t bindex;
  35846. + struct super_block *tgt_sb;
  35847. +
  35848. + tgt_sb = au_sbr_sb(sb, btgt);
  35849. + bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
  35850. + if (bindex < 0)
  35851. + bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
  35852. +
  35853. + return bindex;
  35854. +}
  35855. +
  35856. +/* ---------------------------------------------------------------------- */
  35857. +
  35858. +/*
  35859. + * stop unnecessary notify events at creating xino files
  35860. + */
  35861. +
  35862. +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
  35863. +{
  35864. + aufs_bindex_t bfound, bindex, bbot;
  35865. + struct dentry *parent;
  35866. + struct au_branch *br;
  35867. +
  35868. + bfound = -1;
  35869. + parent = dentry->d_parent; /* safe d_parent access */
  35870. + bbot = au_sbbot(sb);
  35871. + for (bindex = 0; bindex <= bbot; bindex++) {
  35872. + br = au_sbr(sb, bindex);
  35873. + if (au_br_dentry(br) == parent) {
  35874. + bfound = bindex;
  35875. + break;
  35876. + }
  35877. + }
  35878. +
  35879. + AuDbg("bfound b%d\n", bfound);
  35880. + return bfound;
  35881. +}
  35882. +
  35883. +struct au_xino_lock_dir {
  35884. + struct au_hinode *hdir;
  35885. + struct dentry *parent;
  35886. + struct inode *dir;
  35887. +};
  35888. +
  35889. +static struct dentry *au_dget_parent_lock(struct dentry *dentry,
  35890. + unsigned int lsc)
  35891. +{
  35892. + struct dentry *parent;
  35893. + struct inode *dir;
  35894. +
  35895. + parent = dget_parent(dentry);
  35896. + dir = d_inode(parent);
  35897. + inode_lock_nested(dir, lsc);
  35898. +#if 0 /* it should not happen */
  35899. + spin_lock(&dentry->d_lock);
  35900. + if (unlikely(dentry->d_parent != parent)) {
  35901. + spin_unlock(&dentry->d_lock);
  35902. + inode_unlock(dir);
  35903. + dput(parent);
  35904. + parent = NULL;
  35905. + goto out;
  35906. + }
  35907. + spin_unlock(&dentry->d_lock);
  35908. +
  35909. +out:
  35910. +#endif
  35911. + return parent;
  35912. +}
  35913. +
  35914. +static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
  35915. + struct au_xino_lock_dir *ldir)
  35916. +{
  35917. + aufs_bindex_t bindex;
  35918. +
  35919. + ldir->hdir = NULL;
  35920. + bindex = au_xi_root(sb, xipath->dentry);
  35921. + if (bindex >= 0) {
  35922. + /* rw branch root */
  35923. + ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
  35924. + au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
  35925. + } else {
  35926. + /* other */
  35927. + ldir->parent = au_dget_parent_lock(xipath->dentry,
  35928. + AuLsc_I_PARENT);
  35929. + ldir->dir = d_inode(ldir->parent);
  35930. + }
  35931. +}
  35932. +
  35933. +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
  35934. +{
  35935. + if (ldir->hdir)
  35936. + au_hn_inode_unlock(ldir->hdir);
  35937. + else {
  35938. + inode_unlock(ldir->dir);
  35939. + dput(ldir->parent);
  35940. + }
  35941. +}
  35942. +
  35943. +/* ---------------------------------------------------------------------- */
  35944. +
  35945. +/*
  35946. + * create and set a new xino file
  35947. + */
  35948. +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent)
  35949. +{
  35950. + struct file *file;
  35951. + struct dentry *h_parent, *d;
  35952. + struct inode *h_dir, *inode;
  35953. + int err;
  35954. +
  35955. + /*
  35956. + * at mount-time, and the xino file is the default path,
  35957. + * hnotify is disabled so we have no notify events to ignore.
  35958. + * when a user specified the xino, we cannot get au_hdir to be ignored.
  35959. + */
  35960. + file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
  35961. + /* | __FMODE_NONOTIFY */,
  35962. + 0666);
  35963. + if (IS_ERR(file)) {
  35964. + if (!silent)
  35965. + pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
  35966. + return file;
  35967. + }
  35968. +
  35969. + /* keep file count */
  35970. + err = 0;
  35971. + d = file->f_path.dentry;
  35972. + h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
  35973. + /* mnt_want_write() is unnecessary here */
  35974. + h_dir = d_inode(h_parent);
  35975. + inode = file_inode(file);
  35976. + /* no delegation since it is just created */
  35977. + if (inode->i_nlink)
  35978. + err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
  35979. + /*force*/0);
  35980. + inode_unlock(h_dir);
  35981. + dput(h_parent);
  35982. + if (unlikely(err)) {
  35983. + if (!silent)
  35984. + pr_err("unlink %s(%d)\n", fpath, err);
  35985. + goto out;
  35986. + }
  35987. +
  35988. + err = -EINVAL;
  35989. + if (unlikely(sb == d->d_sb)) {
  35990. + if (!silent)
  35991. + pr_err("%s must be outside\n", fpath);
  35992. + goto out;
  35993. + }
  35994. + if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
  35995. + if (!silent)
  35996. + pr_err("xino doesn't support %s(%s)\n",
  35997. + fpath, au_sbtype(d->d_sb));
  35998. + goto out;
  35999. + }
  36000. + return file; /* success */
  36001. +
  36002. +out:
  36003. + fput(file);
  36004. + file = ERR_PTR(err);
  36005. + return file;
  36006. +}
  36007. +
  36008. +/*
  36009. + * create a new xinofile at the same place/path as @base.
  36010. + */
  36011. +struct file *au_xino_create2(struct super_block *sb, struct path *base,
  36012. + struct file *copy_src)
  36013. +{
  36014. + struct file *file;
  36015. + struct dentry *dentry, *parent;
  36016. + struct inode *dir, *delegated;
  36017. + struct qstr *name;
  36018. + struct path path;
  36019. + int err, do_unlock;
  36020. + struct au_xino_lock_dir ldir;
  36021. +
  36022. + do_unlock = 1;
  36023. + au_xino_lock_dir(sb, base, &ldir);
  36024. + dentry = base->dentry;
  36025. + parent = dentry->d_parent; /* dir inode is locked */
  36026. + dir = d_inode(parent);
  36027. + IMustLock(dir);
  36028. +
  36029. + name = &dentry->d_name;
  36030. + path.dentry = vfsub_lookup_one_len(name->name, parent, name->len);
  36031. + if (IS_ERR(path.dentry)) {
  36032. + file = (void *)path.dentry;
  36033. + pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
  36034. + goto out;
  36035. + }
  36036. +
  36037. + /* no need to mnt_want_write() since we call dentry_open() later */
  36038. + err = vfs_create(dir, path.dentry, 0666, NULL);
  36039. + if (unlikely(err)) {
  36040. + file = ERR_PTR(err);
  36041. + pr_err("%pd create err %d\n", dentry, err);
  36042. + goto out_dput;
  36043. + }
  36044. +
  36045. + path.mnt = base->mnt;
  36046. + file = vfsub_dentry_open(&path,
  36047. + O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
  36048. + /* | __FMODE_NONOTIFY */);
  36049. + if (IS_ERR(file)) {
  36050. + pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
  36051. + goto out_dput;
  36052. + }
  36053. +
  36054. + delegated = NULL;
  36055. + err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
  36056. + au_xino_unlock_dir(&ldir);
  36057. + do_unlock = 0;
  36058. + if (unlikely(err == -EWOULDBLOCK)) {
  36059. + pr_warn("cannot retry for NFSv4 delegation"
  36060. + " for an internal unlink\n");
  36061. + iput(delegated);
  36062. + }
  36063. + if (unlikely(err)) {
  36064. + pr_err("%pd unlink err %d\n", dentry, err);
  36065. + goto out_fput;
  36066. + }
  36067. +
  36068. + if (copy_src) {
  36069. + /* no one can touch copy_src xino */
  36070. + err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
  36071. + if (unlikely(err)) {
  36072. + pr_err("%pd copy err %d\n", dentry, err);
  36073. + goto out_fput;
  36074. + }
  36075. + }
  36076. + goto out_dput; /* success */
  36077. +
  36078. +out_fput:
  36079. + fput(file);
  36080. + file = ERR_PTR(err);
  36081. +out_dput:
  36082. + dput(path.dentry);
  36083. +out:
  36084. + if (do_unlock)
  36085. + au_xino_unlock_dir(&ldir);
  36086. + return file;
  36087. +}
  36088. +
  36089. +struct file *au_xino_file1(struct au_xino *xi)
  36090. +{
  36091. + struct file *file;
  36092. + unsigned int u, nfile;
  36093. +
  36094. + file = NULL;
  36095. + nfile = xi->xi_nfile;
  36096. + for (u = 0; u < nfile; u++) {
  36097. + file = xi->xi_file[u];
  36098. + if (file)
  36099. + break;
  36100. + }
  36101. +
  36102. + return file;
  36103. +}
  36104. +
  36105. +static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
  36106. +{
  36107. + int err;
  36108. + struct file *f;
  36109. + void *p;
  36110. +
  36111. + if (file)
  36112. + get_file(file);
  36113. +
  36114. + err = 0;
  36115. + f = NULL;
  36116. + if (idx < xi->xi_nfile) {
  36117. + f = xi->xi_file[idx];
  36118. + if (f)
  36119. + fput(f);
  36120. + } else {
  36121. + p = au_kzrealloc(xi->xi_file,
  36122. + sizeof(*xi->xi_file) * xi->xi_nfile,
  36123. + sizeof(*xi->xi_file) * (idx + 1),
  36124. + GFP_NOFS, /*may_shrink*/0);
  36125. + if (p) {
  36126. + MtxMustLock(&xi->xi_mtx);
  36127. + xi->xi_file = p;
  36128. + xi->xi_nfile = idx + 1;
  36129. + } else {
  36130. + err = -ENOMEM;
  36131. + if (file)
  36132. + fput(file);
  36133. + goto out;
  36134. + }
  36135. + }
  36136. + xi->xi_file[idx] = file;
  36137. +
  36138. +out:
  36139. + return err;
  36140. +}
  36141. +
  36142. +/*
  36143. + * if @xinew->xi is not set, then create new xigen file.
  36144. + */
  36145. +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
  36146. +{
  36147. + struct file *file;
  36148. + int err;
  36149. +
  36150. + SiMustAnyLock(sb);
  36151. +
  36152. + file = au_xino_create2(sb, xinew->base, xinew->copy_src);
  36153. + if (IS_ERR(file)) {
  36154. + err = PTR_ERR(file);
  36155. + pr_err("%s[%d], err %d\n",
  36156. + xinew->xi ? "xino" : "xigen",
  36157. + xinew->idx, err);
  36158. + goto out;
  36159. + }
  36160. +
  36161. + if (xinew->xi)
  36162. + err = au_xino_file_set(xinew->xi, xinew->idx, file);
  36163. + else {
  36164. + BUG();
  36165. + /* todo: make xigen file an array */
  36166. + /* err = au_xigen_file_set(sb, xinew->idx, file); */
  36167. + }
  36168. + fput(file);
  36169. + if (unlikely(err))
  36170. + file = ERR_PTR(err);
  36171. +
  36172. +out:
  36173. + return file;
  36174. +}
  36175. +
  36176. +/* ---------------------------------------------------------------------- */
  36177. +
  36178. +/*
  36179. + * truncate xino files
  36180. + */
  36181. +static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
  36182. + int idx, struct kstatfs *st)
  36183. +{
  36184. + int err;
  36185. + blkcnt_t blocks;
  36186. + struct file *file, *new_xino;
  36187. + struct au_xi_new xinew = {
  36188. + .idx = idx
  36189. + };
  36190. +
  36191. + err = 0;
  36192. + xinew.xi = au_sbr(sb, bindex)->br_xino;
  36193. + file = au_xino_file(xinew.xi, idx);
  36194. + if (!file)
  36195. + goto out;
  36196. +
  36197. + xinew.base = &file->f_path;
  36198. + err = vfs_statfs(xinew.base, st);
  36199. + if (unlikely(err)) {
  36200. + AuErr1("statfs err %d, ignored\n", err);
  36201. + err = 0;
  36202. + goto out;
  36203. + }
  36204. +
  36205. + blocks = file_inode(file)->i_blocks;
  36206. + pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
  36207. + bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
  36208. +
  36209. + xinew.copy_src = file;
  36210. + new_xino = au_xi_new(sb, &xinew);
  36211. + if (IS_ERR(new_xino)) {
  36212. + err = PTR_ERR(new_xino);
  36213. + pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
  36214. + goto out;
  36215. + }
  36216. +
  36217. + err = vfs_statfs(&new_xino->f_path, st);
  36218. + if (!err)
  36219. + pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
  36220. + bindex, idx, (u64)file_inode(new_xino)->i_blocks,
  36221. + st->f_bfree, st->f_blocks);
  36222. + else {
  36223. + AuErr1("statfs err %d, ignored\n", err);
  36224. + err = 0;
  36225. + }
  36226. +
  36227. +out:
  36228. + return err;
  36229. +}
  36230. +
  36231. +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
  36232. +{
  36233. + int err, i;
  36234. + unsigned long jiffy;
  36235. + aufs_bindex_t bbot;
  36236. + struct kstatfs *st;
  36237. + struct au_branch *br;
  36238. + struct au_xino *xi;
  36239. +
  36240. + err = -ENOMEM;
  36241. + st = kmalloc(sizeof(*st), GFP_NOFS);
  36242. + if (unlikely(!st))
  36243. + goto out;
  36244. +
  36245. + err = -EINVAL;
  36246. + bbot = au_sbbot(sb);
  36247. + if (unlikely(bindex < 0 || bbot < bindex))
  36248. + goto out_st;
  36249. +
  36250. + err = 0;
  36251. + jiffy = jiffies;
  36252. + br = au_sbr(sb, bindex);
  36253. + xi = br->br_xino;
  36254. + for (i = idx_begin; !err && i < xi->xi_nfile; i++)
  36255. + err = au_xino_do_trunc(sb, bindex, i, st);
  36256. + if (!err)
  36257. + au_sbi(sb)->si_xino_jiffy = jiffy;
  36258. +
  36259. +out_st:
  36260. + au_kfree_rcu(st);
  36261. +out:
  36262. + return err;
  36263. +}
  36264. +
  36265. +struct xino_do_trunc_args {
  36266. + struct super_block *sb;
  36267. + struct au_branch *br;
  36268. + int idx;
  36269. +};
  36270. +
  36271. +static void xino_do_trunc(void *_args)
  36272. +{
  36273. + struct xino_do_trunc_args *args = _args;
  36274. + struct super_block *sb;
  36275. + struct au_branch *br;
  36276. + struct inode *dir;
  36277. + int err, idx;
  36278. + aufs_bindex_t bindex;
  36279. +
  36280. + err = 0;
  36281. + sb = args->sb;
  36282. + dir = d_inode(sb->s_root);
  36283. + br = args->br;
  36284. + idx = args->idx;
  36285. +
  36286. + si_noflush_write_lock(sb);
  36287. + ii_read_lock_parent(dir);
  36288. + bindex = au_br_index(sb, br->br_id);
  36289. + err = au_xino_trunc(sb, bindex, idx);
  36290. + ii_read_unlock(dir);
  36291. + if (unlikely(err))
  36292. + pr_warn("err b%d, (%d)\n", bindex, err);
  36293. + atomic_dec(&br->br_xino->xi_truncating);
  36294. + au_lcnt_dec(&br->br_count);
  36295. + si_write_unlock(sb);
  36296. + au_nwt_done(&au_sbi(sb)->si_nowait);
  36297. + au_kfree_rcu(args);
  36298. +}
  36299. +
  36300. +/*
  36301. + * returns the index in the xi_file array whose corresponding file is necessary
  36302. + * to truncate, or -1 which means no need to truncate.
  36303. + */
  36304. +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
  36305. +{
  36306. + int err;
  36307. + unsigned int u;
  36308. + struct kstatfs st;
  36309. + struct au_sbinfo *sbinfo;
  36310. + struct au_xino *xi;
  36311. + struct file *file;
  36312. +
  36313. + /* todo: si_xino_expire and the ratio should be customizable */
  36314. + sbinfo = au_sbi(sb);
  36315. + if (time_before(jiffies,
  36316. + sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
  36317. + return -1;
  36318. +
  36319. + /* truncation border */
  36320. + xi = br->br_xino;
  36321. + for (u = 0; u < xi->xi_nfile; u++) {
  36322. + file = au_xino_file(xi, u);
  36323. + if (!file)
  36324. + continue;
  36325. +
  36326. + err = vfs_statfs(&file->f_path, &st);
  36327. + if (unlikely(err)) {
  36328. + AuErr1("statfs err %d, ignored\n", err);
  36329. + return -1;
  36330. + }
  36331. + if (div64_u64(st.f_bfree * 100, st.f_blocks)
  36332. + >= AUFS_XINO_DEF_TRUNC)
  36333. + return u;
  36334. + }
  36335. +
  36336. + return -1;
  36337. +}
  36338. +
  36339. +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
  36340. +{
  36341. + int idx;
  36342. + struct xino_do_trunc_args *args;
  36343. + int wkq_err;
  36344. +
  36345. + idx = xino_trunc_test(sb, br);
  36346. + if (idx < 0)
  36347. + return;
  36348. +
  36349. + if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
  36350. + goto out;
  36351. +
  36352. + /* lock and kfree() will be called in trunc_xino() */
  36353. + args = kmalloc(sizeof(*args), GFP_NOFS);
  36354. + if (unlikely(!args)) {
  36355. + AuErr1("no memory\n");
  36356. + goto out;
  36357. + }
  36358. +
  36359. + au_lcnt_inc(&br->br_count);
  36360. + args->sb = sb;
  36361. + args->br = br;
  36362. + args->idx = idx;
  36363. + wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
  36364. + if (!wkq_err)
  36365. + return; /* success */
  36366. +
  36367. + pr_err("wkq %d\n", wkq_err);
  36368. + au_lcnt_dec(&br->br_count);
  36369. + au_kfree_rcu(args);
  36370. +
  36371. +out:
  36372. + atomic_dec(&br->br_xino->xi_truncating);
  36373. +}
  36374. +
  36375. +/* ---------------------------------------------------------------------- */
  36376. +
  36377. +struct au_xi_calc {
  36378. + int idx;
  36379. + loff_t pos;
  36380. +};
  36381. +
  36382. +static void au_xi_calc(struct super_block *sb, ino_t h_ino,
  36383. + struct au_xi_calc *calc)
  36384. +{
  36385. + loff_t maxent;
  36386. +
  36387. + maxent = au_xi_maxent(sb);
  36388. + calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
  36389. + calc->pos *= sizeof(ino_t);
  36390. +}
  36391. +
  36392. +static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
  36393. + struct au_xi_calc *calc)
  36394. +{
  36395. + int err;
  36396. + struct file *file;
  36397. + struct au_xino *xi = br->br_xino;
  36398. + struct au_xi_new xinew = {
  36399. + .xi = xi
  36400. + };
  36401. +
  36402. + SiMustAnyLock(sb);
  36403. +
  36404. + err = 0;
  36405. + if (!xi)
  36406. + goto out;
  36407. +
  36408. + mutex_lock(&xi->xi_mtx);
  36409. + file = au_xino_file(xi, calc->idx);
  36410. + if (file)
  36411. + goto out_mtx;
  36412. +
  36413. + file = au_xino_file(xi, /*idx*/-1);
  36414. + AuDebugOn(!file);
  36415. + xinew.idx = calc->idx;
  36416. + xinew.base = &file->f_path;
  36417. + /* xinew.copy_src = NULL; */
  36418. + file = au_xi_new(sb, &xinew);
  36419. + if (IS_ERR(file))
  36420. + err = PTR_ERR(file);
  36421. +
  36422. +out_mtx:
  36423. + mutex_unlock(&xi->xi_mtx);
  36424. +out:
  36425. + return err;
  36426. +}
  36427. +
  36428. +struct au_xino_do_new_async_args {
  36429. + struct super_block *sb;
  36430. + struct au_branch *br;
  36431. + struct au_xi_calc calc;
  36432. + ino_t ino;
  36433. +};
  36434. +
  36435. +struct au_xi_writing {
  36436. + struct hlist_bl_node node;
  36437. + ino_t h_ino, ino;
  36438. +};
  36439. +
  36440. +static int au_xino_do_write(vfs_writef_t write, struct file *file,
  36441. + struct au_xi_calc *calc, ino_t ino);
  36442. +
  36443. +static void au_xino_call_do_new_async(void *args)
  36444. +{
  36445. + struct au_xino_do_new_async_args *a = args;
  36446. + struct au_branch *br;
  36447. + struct super_block *sb;
  36448. + struct au_sbinfo *sbi;
  36449. + struct inode *root;
  36450. + struct file *file;
  36451. + struct au_xi_writing *del, *p;
  36452. + struct hlist_bl_head *hbl;
  36453. + struct hlist_bl_node *pos;
  36454. + int err;
  36455. +
  36456. + br = a->br;
  36457. + sb = a->sb;
  36458. + sbi = au_sbi(sb);
  36459. + si_noflush_read_lock(sb);
  36460. + root = d_inode(sb->s_root);
  36461. + ii_read_lock_child(root);
  36462. + err = au_xino_do_new_async(sb, br, &a->calc);
  36463. + if (unlikely(err)) {
  36464. + AuIOErr("err %d\n", err);
  36465. + goto out;
  36466. + }
  36467. +
  36468. + file = au_xino_file(br->br_xino, a->calc.idx);
  36469. + AuDebugOn(!file);
  36470. + err = au_xino_do_write(sbi->si_xwrite, file, &a->calc, a->ino);
  36471. + if (unlikely(err)) {
  36472. + AuIOErr("err %d\n", err);
  36473. + goto out;
  36474. + }
  36475. +
  36476. + del = NULL;
  36477. + hbl = &br->br_xino->xi_writing;
  36478. + hlist_bl_lock(hbl);
  36479. + au_hbl_for_each(pos, hbl) {
  36480. + p = container_of(pos, struct au_xi_writing, node);
  36481. + if (p->ino == a->ino) {
  36482. + del = p;
  36483. + hlist_bl_del(&p->node);
  36484. + break;
  36485. + }
  36486. + }
  36487. + hlist_bl_unlock(hbl);
  36488. + au_kfree_rcu(del);
  36489. +
  36490. +out:
  36491. + au_lcnt_dec(&br->br_count);
  36492. + ii_read_unlock(root);
  36493. + si_read_unlock(sb);
  36494. + au_nwt_done(&sbi->si_nowait);
  36495. + au_kfree_rcu(a);
  36496. +}
  36497. +
  36498. +/*
  36499. + * create a new xino file asynchronously
  36500. + */
  36501. +static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
  36502. + struct au_xi_calc *calc, ino_t ino)
  36503. +{
  36504. + int err;
  36505. + struct au_xino_do_new_async_args *arg;
  36506. +
  36507. + err = -ENOMEM;
  36508. + arg = kmalloc(sizeof(*arg), GFP_NOFS);
  36509. + if (unlikely(!arg))
  36510. + goto out;
  36511. +
  36512. + arg->sb = sb;
  36513. + arg->br = br;
  36514. + arg->calc = *calc;
  36515. + arg->ino = ino;
  36516. + au_lcnt_inc(&br->br_count);
  36517. + err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
  36518. + if (unlikely(err)) {
  36519. + pr_err("wkq %d\n", err);
  36520. + au_lcnt_dec(&br->br_count);
  36521. + au_kfree_rcu(arg);
  36522. + }
  36523. +
  36524. +out:
  36525. + return err;
  36526. +}
  36527. +
  36528. +/*
  36529. + * read @ino from xinofile for the specified branch{@sb, @bindex}
  36530. + * at the position of @h_ino.
  36531. + */
  36532. +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  36533. + ino_t *ino)
  36534. +{
  36535. + int err;
  36536. + ssize_t sz;
  36537. + struct au_xi_calc calc;
  36538. + struct au_sbinfo *sbinfo;
  36539. + struct file *file;
  36540. + struct au_xino *xi;
  36541. + struct hlist_bl_head *hbl;
  36542. + struct hlist_bl_node *pos;
  36543. + struct au_xi_writing *p;
  36544. +
  36545. + *ino = 0;
  36546. + if (!au_opt_test(au_mntflags(sb), XINO))
  36547. + return 0; /* no xino */
  36548. +
  36549. + err = 0;
  36550. + au_xi_calc(sb, h_ino, &calc);
  36551. + xi = au_sbr(sb, bindex)->br_xino;
  36552. + file = au_xino_file(xi, calc.idx);
  36553. + if (!file) {
  36554. + hbl = &xi->xi_writing;
  36555. + hlist_bl_lock(hbl);
  36556. + au_hbl_for_each(pos, hbl) {
  36557. + p = container_of(pos, struct au_xi_writing, node);
  36558. + if (p->h_ino == h_ino) {
  36559. + AuDbg("hi%llu, i%llu, found\n",
  36560. + (u64)p->h_ino, (u64)p->ino);
  36561. + *ino = p->ino;
  36562. + break;
  36563. + }
  36564. + }
  36565. + hlist_bl_unlock(hbl);
  36566. + return 0;
  36567. + } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
  36568. + return 0; /* no xino */
  36569. +
  36570. + sbinfo = au_sbi(sb);
  36571. + sz = xino_fread(sbinfo->si_xread, file, ino, sizeof(*ino), &calc.pos);
  36572. + if (sz == sizeof(*ino))
  36573. + return 0; /* success */
  36574. +
  36575. + err = sz;
  36576. + if (unlikely(sz >= 0)) {
  36577. + err = -EIO;
  36578. + AuIOErr("xino read error (%zd)\n", sz);
  36579. + }
  36580. + return err;
  36581. +}
  36582. +
  36583. +static int au_xino_do_write(vfs_writef_t write, struct file *file,
  36584. + struct au_xi_calc *calc, ino_t ino)
  36585. +{
  36586. + ssize_t sz;
  36587. +
  36588. + sz = xino_fwrite(write, file, &ino, sizeof(ino), &calc->pos);
  36589. + if (sz == sizeof(ino))
  36590. + return 0; /* success */
  36591. +
  36592. + AuIOErr("write failed (%zd)\n", sz);
  36593. + return -EIO;
  36594. +}
  36595. +
  36596. +/*
  36597. + * write @ino to the xinofile for the specified branch{@sb, @bindex}
  36598. + * at the position of @h_ino.
  36599. + * even if @ino is zero, it is written to the xinofile and means no entry.
  36600. + * if the size of the xino file on a specific filesystem exceeds the watermark,
  36601. + * try truncating it.
  36602. + */
  36603. +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  36604. + ino_t ino)
  36605. +{
  36606. + int err;
  36607. + unsigned int mnt_flags;
  36608. + struct au_xi_calc calc;
  36609. + struct file *file;
  36610. + struct au_branch *br;
  36611. + struct au_xino *xi;
  36612. + struct au_xi_writing *p;
  36613. +
  36614. + SiMustAnyLock(sb);
  36615. +
  36616. + mnt_flags = au_mntflags(sb);
  36617. + if (!au_opt_test(mnt_flags, XINO))
  36618. + return 0;
  36619. +
  36620. + au_xi_calc(sb, h_ino, &calc);
  36621. + br = au_sbr(sb, bindex);
  36622. + xi = br->br_xino;
  36623. + file = au_xino_file(xi, calc.idx);
  36624. + if (!file) {
  36625. + /* store the inum pair into the list */
  36626. + p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
  36627. + p->h_ino = h_ino;
  36628. + p->ino = ino;
  36629. + au_hbl_add(&p->node, &xi->xi_writing);
  36630. +
  36631. + /* create and write a new xino file asynchronously */
  36632. + err = au_xino_new_async(sb, br, &calc, ino);
  36633. + if (!err)
  36634. + return 0; /* success */
  36635. + goto out;
  36636. + }
  36637. +
  36638. + err = au_xino_do_write(au_sbi(sb)->si_xwrite, file, &calc, ino);
  36639. + if (!err) {
  36640. + br = au_sbr(sb, bindex);
  36641. + if (au_opt_test(mnt_flags, TRUNC_XINO)
  36642. + && au_test_fs_trunc_xino(au_br_sb(br)))
  36643. + xino_try_trunc(sb, br);
  36644. + return 0; /* success */
  36645. + }
  36646. +
  36647. +out:
  36648. + AuIOErr("write failed (%d)\n", err);
  36649. + return -EIO;
  36650. +}
  36651. +
  36652. +static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
  36653. + size_t size, loff_t *pos);
  36654. +
  36655. +/* todo: unnecessary to support mmap_sem since kernel-space? */
  36656. +ssize_t xino_fread(vfs_readf_t func, struct file *file, void *kbuf, size_t size,
  36657. + loff_t *pos)
  36658. +{
  36659. + ssize_t err;
  36660. + mm_segment_t oldfs;
  36661. + union {
  36662. + void *k;
  36663. + char __user *u;
  36664. + } buf;
  36665. + int i;
  36666. + const int prevent_endless = 10;
  36667. +
  36668. + i = 0;
  36669. + buf.k = kbuf;
  36670. + oldfs = get_fs();
  36671. + set_fs(KERNEL_DS);
  36672. + do {
  36673. + err = func(file, buf.u, size, pos);
  36674. + if (err == -EINTR
  36675. + && !au_wkq_test()
  36676. + && fatal_signal_pending(current)) {
  36677. + set_fs(oldfs);
  36678. + err = xino_fread_wkq(func, file, kbuf, size, pos);
  36679. + BUG_ON(err == -EINTR);
  36680. + oldfs = get_fs();
  36681. + set_fs(KERNEL_DS);
  36682. + }
  36683. + } while (i++ < prevent_endless
  36684. + && (err == -EAGAIN || err == -EINTR));
  36685. + set_fs(oldfs);
  36686. +
  36687. +#if 0 /* reserved for future use */
  36688. + if (err > 0)
  36689. + fsnotify_access(file->f_path.dentry);
  36690. +#endif
  36691. +
  36692. + return err;
  36693. +}
  36694. +
  36695. +struct xino_fread_args {
  36696. + ssize_t *errp;
  36697. + vfs_readf_t func;
  36698. + struct file *file;
  36699. + void *buf;
  36700. + size_t size;
  36701. + loff_t *pos;
  36702. +};
  36703. +
  36704. +static void call_xino_fread(void *args)
  36705. +{
  36706. + struct xino_fread_args *a = args;
  36707. + *a->errp = xino_fread(a->func, a->file, a->buf, a->size, a->pos);
  36708. +}
  36709. +
  36710. +static ssize_t xino_fread_wkq(vfs_readf_t func, struct file *file, void *buf,
  36711. + size_t size, loff_t *pos)
  36712. +{
  36713. + ssize_t err;
  36714. + int wkq_err;
  36715. + struct xino_fread_args args = {
  36716. + .errp = &err,
  36717. + .func = func,
  36718. + .file = file,
  36719. + .buf = buf,
  36720. + .size = size,
  36721. + .pos = pos
  36722. + };
  36723. +
  36724. + wkq_err = au_wkq_wait(call_xino_fread, &args);
  36725. + if (unlikely(wkq_err))
  36726. + err = wkq_err;
  36727. +
  36728. + return err;
  36729. +}
  36730. +
  36731. +static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
  36732. + size_t size, loff_t *pos);
  36733. +
  36734. +static ssize_t do_xino_fwrite(vfs_writef_t func, struct file *file, void *kbuf,
  36735. + size_t size, loff_t *pos)
  36736. +{
  36737. + ssize_t err;
  36738. + mm_segment_t oldfs;
  36739. + union {
  36740. + void *k;
  36741. + const char __user *u;
  36742. + } buf;
  36743. + int i;
  36744. + const int prevent_endless = 10;
  36745. +
  36746. + i = 0;
  36747. + buf.k = kbuf;
  36748. + oldfs = get_fs();
  36749. + set_fs(KERNEL_DS);
  36750. + do {
  36751. + err = func(file, buf.u, size, pos);
  36752. + if (err == -EINTR
  36753. + && !au_wkq_test()
  36754. + && fatal_signal_pending(current)) {
  36755. + set_fs(oldfs);
  36756. + err = xino_fwrite_wkq(func, file, kbuf, size, pos);
  36757. + BUG_ON(err == -EINTR);
  36758. + oldfs = get_fs();
  36759. + set_fs(KERNEL_DS);
  36760. + }
  36761. + } while (i++ < prevent_endless
  36762. + && (err == -EAGAIN || err == -EINTR));
  36763. + set_fs(oldfs);
  36764. +
  36765. +#if 0 /* reserved for future use */
  36766. + if (err > 0)
  36767. + fsnotify_modify(file->f_path.dentry);
  36768. +#endif
  36769. +
  36770. + return err;
  36771. +}
  36772. +
  36773. +struct do_xino_fwrite_args {
  36774. + ssize_t *errp;
  36775. + vfs_writef_t func;
  36776. + struct file *file;
  36777. + void *buf;
  36778. + size_t size;
  36779. + loff_t *pos;
  36780. +};
  36781. +
  36782. +static void call_do_xino_fwrite(void *args)
  36783. +{
  36784. + struct do_xino_fwrite_args *a = args;
  36785. + *a->errp = do_xino_fwrite(a->func, a->file, a->buf, a->size, a->pos);
  36786. +}
  36787. +
  36788. +static ssize_t xino_fwrite_wkq(vfs_writef_t func, struct file *file, void *buf,
  36789. + size_t size, loff_t *pos)
  36790. +{
  36791. + ssize_t err;
  36792. + int wkq_err;
  36793. + struct do_xino_fwrite_args args = {
  36794. + .errp = &err,
  36795. + .func = func,
  36796. + .file = file,
  36797. + .buf = buf,
  36798. + .size = size,
  36799. + .pos = pos
  36800. + };
  36801. +
  36802. + /*
  36803. + * it breaks RLIMIT_FSIZE and normal user's limit,
  36804. + * users should care about quota and real 'filesystem full.'
  36805. + */
  36806. + wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
  36807. + if (unlikely(wkq_err))
  36808. + err = wkq_err;
  36809. +
  36810. + return err;
  36811. +}
  36812. +
  36813. +ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
  36814. + size_t size, loff_t *pos)
  36815. +{
  36816. + ssize_t err;
  36817. +
  36818. + if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
  36819. + lockdep_off();
  36820. + err = do_xino_fwrite(func, file, buf, size, pos);
  36821. + lockdep_on();
  36822. + } else {
  36823. + lockdep_off();
  36824. + err = xino_fwrite_wkq(func, file, buf, size, pos);
  36825. + lockdep_on();
  36826. + }
  36827. +
  36828. + return err;
  36829. +}
  36830. +
  36831. +/* ---------------------------------------------------------------------- */
  36832. +
  36833. +/*
  36834. + * inode number bitmap
  36835. + */
  36836. +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
  36837. +static ino_t xib_calc_ino(unsigned long pindex, int bit)
  36838. +{
  36839. + ino_t ino;
  36840. +
  36841. + AuDebugOn(bit < 0 || page_bits <= bit);
  36842. + ino = AUFS_FIRST_INO + pindex * page_bits + bit;
  36843. + return ino;
  36844. +}
  36845. +
  36846. +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
  36847. +{
  36848. + AuDebugOn(ino < AUFS_FIRST_INO);
  36849. + ino -= AUFS_FIRST_INO;
  36850. + *pindex = ino / page_bits;
  36851. + *bit = ino % page_bits;
  36852. +}
  36853. +
  36854. +static int xib_pindex(struct super_block *sb, unsigned long pindex)
  36855. +{
  36856. + int err;
  36857. + loff_t pos;
  36858. + ssize_t sz;
  36859. + struct au_sbinfo *sbinfo;
  36860. + struct file *xib;
  36861. + unsigned long *p;
  36862. +
  36863. + sbinfo = au_sbi(sb);
  36864. + MtxMustLock(&sbinfo->si_xib_mtx);
  36865. + AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
  36866. + || !au_opt_test(sbinfo->si_mntflags, XINO));
  36867. +
  36868. + if (pindex == sbinfo->si_xib_last_pindex)
  36869. + return 0;
  36870. +
  36871. + xib = sbinfo->si_xib;
  36872. + p = sbinfo->si_xib_buf;
  36873. + pos = sbinfo->si_xib_last_pindex;
  36874. + pos *= PAGE_SIZE;
  36875. + sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
  36876. + if (unlikely(sz != PAGE_SIZE))
  36877. + goto out;
  36878. +
  36879. + pos = pindex;
  36880. + pos *= PAGE_SIZE;
  36881. + if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
  36882. + sz = xino_fread(sbinfo->si_xread, xib, p, PAGE_SIZE, &pos);
  36883. + else {
  36884. + memset(p, 0, PAGE_SIZE);
  36885. + sz = xino_fwrite(sbinfo->si_xwrite, xib, p, PAGE_SIZE, &pos);
  36886. + }
  36887. + if (sz == PAGE_SIZE) {
  36888. + sbinfo->si_xib_last_pindex = pindex;
  36889. + return 0; /* success */
  36890. + }
  36891. +
  36892. +out:
  36893. + AuIOErr1("write failed (%zd)\n", sz);
  36894. + err = sz;
  36895. + if (sz >= 0)
  36896. + err = -EIO;
  36897. + return err;
  36898. +}
  36899. +
  36900. +static void au_xib_clear_bit(struct inode *inode)
  36901. +{
  36902. + int err, bit;
  36903. + unsigned long pindex;
  36904. + struct super_block *sb;
  36905. + struct au_sbinfo *sbinfo;
  36906. +
  36907. + AuDebugOn(inode->i_nlink);
  36908. +
  36909. + sb = inode->i_sb;
  36910. + xib_calc_bit(inode->i_ino, &pindex, &bit);
  36911. + AuDebugOn(page_bits <= bit);
  36912. + sbinfo = au_sbi(sb);
  36913. + mutex_lock(&sbinfo->si_xib_mtx);
  36914. + err = xib_pindex(sb, pindex);
  36915. + if (!err) {
  36916. + clear_bit(bit, sbinfo->si_xib_buf);
  36917. + sbinfo->si_xib_next_bit = bit;
  36918. + }
  36919. + mutex_unlock(&sbinfo->si_xib_mtx);
  36920. +}
  36921. +
  36922. +/* ---------------------------------------------------------------------- */
  36923. +
  36924. +/*
  36925. + * truncate a xino bitmap file
  36926. + */
  36927. +
  36928. +/* todo: slow */
  36929. +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
  36930. +{
  36931. + int err, bit;
  36932. + ssize_t sz;
  36933. + unsigned long pindex;
  36934. + loff_t pos, pend;
  36935. + struct au_sbinfo *sbinfo;
  36936. + vfs_readf_t func;
  36937. + ino_t *ino;
  36938. + unsigned long *p;
  36939. +
  36940. + err = 0;
  36941. + sbinfo = au_sbi(sb);
  36942. + MtxMustLock(&sbinfo->si_xib_mtx);
  36943. + p = sbinfo->si_xib_buf;
  36944. + func = sbinfo->si_xread;
  36945. + pend = vfsub_f_size_read(file);
  36946. + pos = 0;
  36947. + while (pos < pend) {
  36948. + sz = xino_fread(func, file, page, PAGE_SIZE, &pos);
  36949. + err = sz;
  36950. + if (unlikely(sz <= 0))
  36951. + goto out;
  36952. +
  36953. + err = 0;
  36954. + for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
  36955. + if (unlikely(*ino < AUFS_FIRST_INO))
  36956. + continue;
  36957. +
  36958. + xib_calc_bit(*ino, &pindex, &bit);
  36959. + AuDebugOn(page_bits <= bit);
  36960. + err = xib_pindex(sb, pindex);
  36961. + if (!err)
  36962. + set_bit(bit, p);
  36963. + else
  36964. + goto out;
  36965. + }
  36966. + }
  36967. +
  36968. +out:
  36969. + return err;
  36970. +}
  36971. +
  36972. +static int xib_restore(struct super_block *sb)
  36973. +{
  36974. + int err, i;
  36975. + unsigned int nfile;
  36976. + aufs_bindex_t bindex, bbot;
  36977. + void *page;
  36978. + struct au_branch *br;
  36979. + struct au_xino *xi;
  36980. + struct file *file;
  36981. +
  36982. + err = -ENOMEM;
  36983. + page = (void *)__get_free_page(GFP_NOFS);
  36984. + if (unlikely(!page))
  36985. + goto out;
  36986. +
  36987. + err = 0;
  36988. + bbot = au_sbbot(sb);
  36989. + for (bindex = 0; !err && bindex <= bbot; bindex++)
  36990. + if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
  36991. + br = au_sbr(sb, bindex);
  36992. + xi = br->br_xino;
  36993. + nfile = xi->xi_nfile;
  36994. + for (i = 0; i < nfile; i++) {
  36995. + file = au_xino_file(xi, i);
  36996. + if (file)
  36997. + err = do_xib_restore(sb, file, page);
  36998. + }
  36999. + } else
  37000. + AuDbg("skip shared b%d\n", bindex);
  37001. + free_page((unsigned long)page);
  37002. +
  37003. +out:
  37004. + return err;
  37005. +}
  37006. +
  37007. +int au_xib_trunc(struct super_block *sb)
  37008. +{
  37009. + int err;
  37010. + ssize_t sz;
  37011. + loff_t pos;
  37012. + struct au_sbinfo *sbinfo;
  37013. + unsigned long *p;
  37014. + struct file *file;
  37015. +
  37016. + SiMustWriteLock(sb);
  37017. +
  37018. + err = 0;
  37019. + sbinfo = au_sbi(sb);
  37020. + if (!au_opt_test(sbinfo->si_mntflags, XINO))
  37021. + goto out;
  37022. +
  37023. + file = sbinfo->si_xib;
  37024. + if (vfsub_f_size_read(file) <= PAGE_SIZE)
  37025. + goto out;
  37026. +
  37027. + file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
  37028. + err = PTR_ERR(file);
  37029. + if (IS_ERR(file))
  37030. + goto out;
  37031. + fput(sbinfo->si_xib);
  37032. + sbinfo->si_xib = file;
  37033. +
  37034. + p = sbinfo->si_xib_buf;
  37035. + memset(p, 0, PAGE_SIZE);
  37036. + pos = 0;
  37037. + sz = xino_fwrite(sbinfo->si_xwrite, sbinfo->si_xib, p, PAGE_SIZE, &pos);
  37038. + if (unlikely(sz != PAGE_SIZE)) {
  37039. + err = sz;
  37040. + AuIOErr("err %d\n", err);
  37041. + if (sz >= 0)
  37042. + err = -EIO;
  37043. + goto out;
  37044. + }
  37045. +
  37046. + mutex_lock(&sbinfo->si_xib_mtx);
  37047. + /* mnt_want_write() is unnecessary here */
  37048. + err = xib_restore(sb);
  37049. + mutex_unlock(&sbinfo->si_xib_mtx);
  37050. +
  37051. +out:
  37052. + return err;
  37053. +}
  37054. +
  37055. +/* ---------------------------------------------------------------------- */
  37056. +
  37057. +struct au_xino *au_xino_alloc(unsigned int nfile)
  37058. +{
  37059. + struct au_xino *xi;
  37060. +
  37061. + xi = kzalloc(sizeof(*xi), GFP_NOFS);
  37062. + if (unlikely(!xi))
  37063. + goto out;
  37064. + xi->xi_nfile = nfile;
  37065. + xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
  37066. + if (unlikely(!xi->xi_file))
  37067. + goto out_free;
  37068. +
  37069. + xi->xi_nondir.total = 8; /* initial size */
  37070. + xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
  37071. + GFP_NOFS);
  37072. + if (unlikely(!xi->xi_nondir.array))
  37073. + goto out_file;
  37074. +
  37075. + spin_lock_init(&xi->xi_nondir.spin);
  37076. + init_waitqueue_head(&xi->xi_nondir.wqh);
  37077. + mutex_init(&xi->xi_mtx);
  37078. + INIT_HLIST_BL_HEAD(&xi->xi_writing);
  37079. + atomic_set(&xi->xi_truncating, 0);
  37080. + kref_init(&xi->xi_kref);
  37081. + goto out; /* success */
  37082. +
  37083. +out_file:
  37084. + au_kfree_try_rcu(xi->xi_file);
  37085. +out_free:
  37086. + au_kfree_rcu(xi);
  37087. + xi = NULL;
  37088. +out:
  37089. + return xi;
  37090. +}
  37091. +
  37092. +static int au_xino_init(struct au_branch *br, int idx, struct file *file)
  37093. +{
  37094. + int err;
  37095. + struct au_xino *xi;
  37096. +
  37097. + err = 0;
  37098. + xi = au_xino_alloc(idx + 1);
  37099. + if (unlikely(!xi)) {
  37100. + err = -ENOMEM;
  37101. + goto out;
  37102. + }
  37103. +
  37104. + if (file)
  37105. + get_file(file);
  37106. + xi->xi_file[idx] = file;
  37107. + AuDebugOn(br->br_xino);
  37108. + br->br_xino = xi;
  37109. +
  37110. +out:
  37111. + return err;
  37112. +}
  37113. +
  37114. +static void au_xino_release(struct kref *kref)
  37115. +{
  37116. + struct au_xino *xi;
  37117. + int i;
  37118. + unsigned long ul;
  37119. + struct hlist_bl_head *hbl;
  37120. + struct hlist_bl_node *pos, *n;
  37121. + struct au_xi_writing *p;
  37122. +
  37123. + xi = container_of(kref, struct au_xino, xi_kref);
  37124. + for (i = 0; i < xi->xi_nfile; i++)
  37125. + if (xi->xi_file[i])
  37126. + fput(xi->xi_file[i]);
  37127. + for (i = xi->xi_nondir.total - 1; i >= 0; i--)
  37128. + AuDebugOn(xi->xi_nondir.array[i]);
  37129. + mutex_destroy(&xi->xi_mtx);
  37130. + hbl = &xi->xi_writing;
  37131. + ul = au_hbl_count(hbl);
  37132. + if (unlikely(ul)) {
  37133. + pr_warn("xi_writing %lu\n", ul);
  37134. + hlist_bl_lock(hbl);
  37135. + hlist_bl_for_each_entry_safe (p, pos, n, hbl, node) {
  37136. + hlist_bl_del(&p->node);
  37137. + au_kfree_rcu(p);
  37138. + }
  37139. + hlist_bl_unlock(hbl);
  37140. + }
  37141. + au_kfree_try_rcu(xi->xi_file);
  37142. + au_kfree_try_rcu(xi->xi_nondir.array);
  37143. + au_kfree_rcu(xi);
  37144. +}
  37145. +
  37146. +int au_xino_put(struct au_branch *br)
  37147. +{
  37148. + int ret;
  37149. + struct au_xino *xi;
  37150. +
  37151. + ret = 0;
  37152. + xi = br->br_xino;
  37153. + if (xi) {
  37154. + br->br_xino = NULL;
  37155. + ret = kref_put(&xi->xi_kref, au_xino_release);
  37156. + }
  37157. +
  37158. + return ret;
  37159. +}
  37160. +
  37161. +/* ---------------------------------------------------------------------- */
  37162. +
  37163. +/*
  37164. + * xino mount option handlers
  37165. + */
  37166. +
  37167. +/* xino bitmap */
  37168. +static void xino_clear_xib(struct super_block *sb)
  37169. +{
  37170. + struct au_sbinfo *sbinfo;
  37171. +
  37172. + SiMustWriteLock(sb);
  37173. +
  37174. + sbinfo = au_sbi(sb);
  37175. + /* unnecessary to clear sbinfo->si_xread and ->si_xwrite */
  37176. + if (sbinfo->si_xib)
  37177. + fput(sbinfo->si_xib);
  37178. + sbinfo->si_xib = NULL;
  37179. + if (sbinfo->si_xib_buf)
  37180. + free_page((unsigned long)sbinfo->si_xib_buf);
  37181. + sbinfo->si_xib_buf = NULL;
  37182. +}
  37183. +
  37184. +static int au_xino_set_xib(struct super_block *sb, struct path *path)
  37185. +{
  37186. + int err;
  37187. + loff_t pos;
  37188. + struct au_sbinfo *sbinfo;
  37189. + struct file *file;
  37190. + struct super_block *xi_sb;
  37191. +
  37192. + SiMustWriteLock(sb);
  37193. +
  37194. + sbinfo = au_sbi(sb);
  37195. + file = au_xino_create2(sb, path, sbinfo->si_xib);
  37196. + err = PTR_ERR(file);
  37197. + if (IS_ERR(file))
  37198. + goto out;
  37199. + if (sbinfo->si_xib)
  37200. + fput(sbinfo->si_xib);
  37201. + sbinfo->si_xib = file;
  37202. + sbinfo->si_xread = vfs_readf(file);
  37203. + sbinfo->si_xwrite = vfs_writef(file);
  37204. + xi_sb = file_inode(file)->i_sb;
  37205. + sbinfo->si_ximaxent = xi_sb->s_maxbytes;
  37206. + if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
  37207. + err = -EIO;
  37208. + pr_err("s_maxbytes(%llu) on %s is too small\n",
  37209. + (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
  37210. + goto out_unset;
  37211. + }
  37212. + sbinfo->si_ximaxent /= sizeof(ino_t);
  37213. +
  37214. + err = -ENOMEM;
  37215. + if (!sbinfo->si_xib_buf)
  37216. + sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
  37217. + if (unlikely(!sbinfo->si_xib_buf))
  37218. + goto out_unset;
  37219. +
  37220. + sbinfo->si_xib_last_pindex = 0;
  37221. + sbinfo->si_xib_next_bit = 0;
  37222. + if (vfsub_f_size_read(file) < PAGE_SIZE) {
  37223. + pos = 0;
  37224. + err = xino_fwrite(sbinfo->si_xwrite, file, sbinfo->si_xib_buf,
  37225. + PAGE_SIZE, &pos);
  37226. + if (unlikely(err != PAGE_SIZE))
  37227. + goto out_free;
  37228. + }
  37229. + err = 0;
  37230. + goto out; /* success */
  37231. +
  37232. +out_free:
  37233. + if (sbinfo->si_xib_buf)
  37234. + free_page((unsigned long)sbinfo->si_xib_buf);
  37235. + sbinfo->si_xib_buf = NULL;
  37236. + if (err >= 0)
  37237. + err = -EIO;
  37238. +out_unset:
  37239. + fput(sbinfo->si_xib);
  37240. + sbinfo->si_xib = NULL;
  37241. +out:
  37242. + AuTraceErr(err);
  37243. + return err;
  37244. +}
  37245. +
  37246. +/* xino for each branch */
  37247. +static void xino_clear_br(struct super_block *sb)
  37248. +{
  37249. + aufs_bindex_t bindex, bbot;
  37250. + struct au_branch *br;
  37251. +
  37252. + bbot = au_sbbot(sb);
  37253. + for (bindex = 0; bindex <= bbot; bindex++) {
  37254. + br = au_sbr(sb, bindex);
  37255. + AuDebugOn(!br);
  37256. + au_xino_put(br);
  37257. + }
  37258. +}
  37259. +
  37260. +static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
  37261. + aufs_bindex_t bshared)
  37262. +{
  37263. + struct au_branch *brshared;
  37264. +
  37265. + brshared = au_sbr(sb, bshared);
  37266. + AuDebugOn(!brshared->br_xino);
  37267. + AuDebugOn(!brshared->br_xino->xi_file);
  37268. + if (br->br_xino != brshared->br_xino) {
  37269. + au_xino_get(brshared);
  37270. + au_xino_put(br);
  37271. + br->br_xino = brshared->br_xino;
  37272. + }
  37273. +}
  37274. +
  37275. +struct au_xino_do_set_br {
  37276. + vfs_writef_t writef;
  37277. + struct au_branch *br;
  37278. + ino_t h_ino;
  37279. + aufs_bindex_t bshared;
  37280. +};
  37281. +
  37282. +static int au_xino_do_set_br(struct super_block *sb, struct path *path,
  37283. + struct au_xino_do_set_br *args)
  37284. +{
  37285. + int err;
  37286. + struct au_xi_calc calc;
  37287. + struct file *file;
  37288. + struct au_branch *br;
  37289. + struct au_xi_new xinew = {
  37290. + .base = path
  37291. + };
  37292. +
  37293. + br = args->br;
  37294. + xinew.xi = br->br_xino;
  37295. + au_xi_calc(sb, args->h_ino, &calc);
  37296. + xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
  37297. + if (args->bshared >= 0)
  37298. + /* shared xino */
  37299. + au_xino_set_br_shared(sb, br, args->bshared);
  37300. + else if (!xinew.xi) {
  37301. + /* new xino */
  37302. + err = au_xino_init(br, calc.idx, xinew.copy_src);
  37303. + if (unlikely(err))
  37304. + goto out;
  37305. + }
  37306. +
  37307. + /* force re-creating */
  37308. + xinew.xi = br->br_xino;
  37309. + xinew.idx = calc.idx;
  37310. + mutex_lock(&xinew.xi->xi_mtx);
  37311. + file = au_xi_new(sb, &xinew);
  37312. + mutex_unlock(&xinew.xi->xi_mtx);
  37313. + err = PTR_ERR(file);
  37314. + if (IS_ERR(file))
  37315. + goto out;
  37316. + AuDebugOn(!file);
  37317. +
  37318. + err = au_xino_do_write(args->writef, file, &calc, AUFS_ROOT_INO);
  37319. + if (unlikely(err))
  37320. + au_xino_put(br);
  37321. +
  37322. +out:
  37323. + AuTraceErr(err);
  37324. + return err;
  37325. +}
  37326. +
  37327. +static int au_xino_set_br(struct super_block *sb, struct path *path)
  37328. +{
  37329. + int err;
  37330. + aufs_bindex_t bindex, bbot;
  37331. + struct au_xino_do_set_br args;
  37332. + struct inode *inode;
  37333. +
  37334. + SiMustWriteLock(sb);
  37335. +
  37336. + bbot = au_sbbot(sb);
  37337. + inode = d_inode(sb->s_root);
  37338. + args.writef = au_sbi(sb)->si_xwrite;
  37339. + for (bindex = 0; bindex <= bbot; bindex++) {
  37340. + args.h_ino = au_h_iptr(inode, bindex)->i_ino;
  37341. + args.br = au_sbr(sb, bindex);
  37342. + args.bshared = is_sb_shared(sb, bindex, bindex - 1);
  37343. + err = au_xino_do_set_br(sb, path, &args);
  37344. + if (unlikely(err))
  37345. + break;
  37346. + }
  37347. +
  37348. + AuTraceErr(err);
  37349. + return err;
  37350. +}
  37351. +
  37352. +void au_xino_clr(struct super_block *sb)
  37353. +{
  37354. + struct au_sbinfo *sbinfo;
  37355. +
  37356. + au_xigen_clr(sb);
  37357. + xino_clear_xib(sb);
  37358. + xino_clear_br(sb);
  37359. + dbgaufs_brs_del(sb, 0);
  37360. + sbinfo = au_sbi(sb);
  37361. + /* lvalue, do not call au_mntflags() */
  37362. + au_opt_clr(sbinfo->si_mntflags, XINO);
  37363. +}
  37364. +
  37365. +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
  37366. +{
  37367. + int err, skip;
  37368. + struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
  37369. + struct qstr *dname, *cur_name;
  37370. + struct file *cur_xino;
  37371. + struct au_sbinfo *sbinfo;
  37372. + struct path *path, *cur_path;
  37373. +
  37374. + SiMustWriteLock(sb);
  37375. +
  37376. + err = 0;
  37377. + sbinfo = au_sbi(sb);
  37378. + path = &xiopt->file->f_path;
  37379. + dentry = path->dentry;
  37380. + parent = dget_parent(dentry);
  37381. + if (remount) {
  37382. + skip = 0;
  37383. + cur_xino = sbinfo->si_xib;
  37384. + if (cur_xino) {
  37385. + cur_path = &cur_xino->f_path;
  37386. + cur_dentry = cur_path->dentry;
  37387. + cur_parent = dget_parent(cur_dentry);
  37388. + cur_name = &cur_dentry->d_name;
  37389. + dname = &dentry->d_name;
  37390. + skip = (cur_parent == parent
  37391. + && au_qstreq(dname, cur_name));
  37392. + dput(cur_parent);
  37393. + }
  37394. + if (skip)
  37395. + goto out;
  37396. + }
  37397. +
  37398. + au_opt_set(sbinfo->si_mntflags, XINO);
  37399. + err = au_xino_set_xib(sb, path);
  37400. + /* si_x{read,write} are set */
  37401. + if (!err)
  37402. + err = au_xigen_set(sb, path);
  37403. + if (!err)
  37404. + err = au_xino_set_br(sb, path);
  37405. + if (!err) {
  37406. + dbgaufs_brs_add(sb, 0, /*topdown*/1);
  37407. + goto out; /* success */
  37408. + }
  37409. +
  37410. + /* reset all */
  37411. + AuIOErr("failed setting xino(%d).\n", err);
  37412. + au_xino_clr(sb);
  37413. +
  37414. +out:
  37415. + dput(parent);
  37416. + return err;
  37417. +}
  37418. +
  37419. +/*
  37420. + * create a xinofile at the default place/path.
  37421. + */
  37422. +struct file *au_xino_def(struct super_block *sb)
  37423. +{
  37424. + struct file *file;
  37425. + char *page, *p;
  37426. + struct au_branch *br;
  37427. + struct super_block *h_sb;
  37428. + struct path path;
  37429. + aufs_bindex_t bbot, bindex, bwr;
  37430. +
  37431. + br = NULL;
  37432. + bbot = au_sbbot(sb);
  37433. + bwr = -1;
  37434. + for (bindex = 0; bindex <= bbot; bindex++) {
  37435. + br = au_sbr(sb, bindex);
  37436. + if (au_br_writable(br->br_perm)
  37437. + && !au_test_fs_bad_xino(au_br_sb(br))) {
  37438. + bwr = bindex;
  37439. + break;
  37440. + }
  37441. + }
  37442. +
  37443. + if (bwr >= 0) {
  37444. + file = ERR_PTR(-ENOMEM);
  37445. + page = (void *)__get_free_page(GFP_NOFS);
  37446. + if (unlikely(!page))
  37447. + goto out;
  37448. + path.mnt = au_br_mnt(br);
  37449. + path.dentry = au_h_dptr(sb->s_root, bwr);
  37450. + p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
  37451. + file = (void *)p;
  37452. + if (!IS_ERR(p)) {
  37453. + strcat(p, "/" AUFS_XINO_FNAME);
  37454. + AuDbg("%s\n", p);
  37455. + file = au_xino_create(sb, p, /*silent*/0);
  37456. + }
  37457. + free_page((unsigned long)page);
  37458. + } else {
  37459. + file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0);
  37460. + if (IS_ERR(file))
  37461. + goto out;
  37462. + h_sb = file->f_path.dentry->d_sb;
  37463. + if (unlikely(au_test_fs_bad_xino(h_sb))) {
  37464. + pr_err("xino doesn't support %s(%s)\n",
  37465. + AUFS_XINO_DEFPATH, au_sbtype(h_sb));
  37466. + fput(file);
  37467. + file = ERR_PTR(-EINVAL);
  37468. + }
  37469. + }
  37470. +
  37471. +out:
  37472. + return file;
  37473. +}
  37474. +
  37475. +/* ---------------------------------------------------------------------- */
  37476. +
  37477. +/*
  37478. + * initialize the xinofile for the specified branch @br
  37479. + * at the place/path where @base_file indicates.
  37480. + * test whether another branch is on the same filesystem or not,
  37481. + * if found then share the xinofile with another branch.
  37482. + */
  37483. +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
  37484. + struct path *base)
  37485. +{
  37486. + int err;
  37487. + struct au_xino_do_set_br args = {
  37488. + .h_ino = h_ino,
  37489. + .br = br
  37490. + };
  37491. +
  37492. + args.writef = au_sbi(sb)->si_xwrite;
  37493. + args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
  37494. + au_br_sb(br));
  37495. + err = au_xino_do_set_br(sb, base, &args);
  37496. + if (unlikely(err))
  37497. + au_xino_put(br);
  37498. +
  37499. + return err;
  37500. +}
  37501. +
  37502. +/* ---------------------------------------------------------------------- */
  37503. +
  37504. +/*
  37505. + * get an unused inode number from bitmap
  37506. + */
  37507. +ino_t au_xino_new_ino(struct super_block *sb)
  37508. +{
  37509. + ino_t ino;
  37510. + unsigned long *p, pindex, ul, pend;
  37511. + struct au_sbinfo *sbinfo;
  37512. + struct file *file;
  37513. + int free_bit, err;
  37514. +
  37515. + if (!au_opt_test(au_mntflags(sb), XINO))
  37516. + return iunique(sb, AUFS_FIRST_INO);
  37517. +
  37518. + sbinfo = au_sbi(sb);
  37519. + mutex_lock(&sbinfo->si_xib_mtx);
  37520. + p = sbinfo->si_xib_buf;
  37521. + free_bit = sbinfo->si_xib_next_bit;
  37522. + if (free_bit < page_bits && !test_bit(free_bit, p))
  37523. + goto out; /* success */
  37524. + free_bit = find_first_zero_bit(p, page_bits);
  37525. + if (free_bit < page_bits)
  37526. + goto out; /* success */
  37527. +
  37528. + pindex = sbinfo->si_xib_last_pindex;
  37529. + for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
  37530. + err = xib_pindex(sb, ul);
  37531. + if (unlikely(err))
  37532. + goto out_err;
  37533. + free_bit = find_first_zero_bit(p, page_bits);
  37534. + if (free_bit < page_bits)
  37535. + goto out; /* success */
  37536. + }
  37537. +
  37538. + file = sbinfo->si_xib;
  37539. + pend = vfsub_f_size_read(file) / PAGE_SIZE;
  37540. + for (ul = pindex + 1; ul <= pend; ul++) {
  37541. + err = xib_pindex(sb, ul);
  37542. + if (unlikely(err))
  37543. + goto out_err;
  37544. + free_bit = find_first_zero_bit(p, page_bits);
  37545. + if (free_bit < page_bits)
  37546. + goto out; /* success */
  37547. + }
  37548. + BUG();
  37549. +
  37550. +out:
  37551. + set_bit(free_bit, p);
  37552. + sbinfo->si_xib_next_bit = free_bit + 1;
  37553. + pindex = sbinfo->si_xib_last_pindex;
  37554. + mutex_unlock(&sbinfo->si_xib_mtx);
  37555. + ino = xib_calc_ino(pindex, free_bit);
  37556. + AuDbg("i%lu\n", (unsigned long)ino);
  37557. + return ino;
  37558. +out_err:
  37559. + mutex_unlock(&sbinfo->si_xib_mtx);
  37560. + AuDbg("i0\n");
  37561. + return 0;
  37562. +}
  37563. +
  37564. +/* for s_op->delete_inode() */
  37565. +void au_xino_delete_inode(struct inode *inode, const int unlinked)
  37566. +{
  37567. + int err;
  37568. + unsigned int mnt_flags;
  37569. + aufs_bindex_t bindex, bbot, bi;
  37570. + unsigned char try_trunc;
  37571. + struct au_iinfo *iinfo;
  37572. + struct super_block *sb;
  37573. + struct au_hinode *hi;
  37574. + struct inode *h_inode;
  37575. + struct au_branch *br;
  37576. + vfs_writef_t xwrite;
  37577. + struct au_xi_calc calc;
  37578. + struct file *file;
  37579. +
  37580. + AuDebugOn(au_is_bad_inode(inode));
  37581. +
  37582. + sb = inode->i_sb;
  37583. + mnt_flags = au_mntflags(sb);
  37584. + if (!au_opt_test(mnt_flags, XINO)
  37585. + || inode->i_ino == AUFS_ROOT_INO)
  37586. + return;
  37587. +
  37588. + if (unlinked) {
  37589. + au_xigen_inc(inode);
  37590. + au_xib_clear_bit(inode);
  37591. + }
  37592. +
  37593. + iinfo = au_ii(inode);
  37594. + bindex = iinfo->ii_btop;
  37595. + if (bindex < 0)
  37596. + return;
  37597. +
  37598. + xwrite = au_sbi(sb)->si_xwrite;
  37599. + try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
  37600. + hi = au_hinode(iinfo, bindex);
  37601. + bbot = iinfo->ii_bbot;
  37602. + for (; bindex <= bbot; bindex++, hi++) {
  37603. + h_inode = hi->hi_inode;
  37604. + if (!h_inode
  37605. + || (!unlinked && h_inode->i_nlink))
  37606. + continue;
  37607. +
  37608. + /* inode may not be revalidated */
  37609. + bi = au_br_index(sb, hi->hi_id);
  37610. + if (bi < 0)
  37611. + continue;
  37612. +
  37613. + br = au_sbr(sb, bi);
  37614. + au_xi_calc(sb, h_inode->i_ino, &calc);
  37615. + file = au_xino_file(br->br_xino, calc.idx);
  37616. + if (IS_ERR_OR_NULL(file))
  37617. + continue;
  37618. +
  37619. + err = au_xino_do_write(xwrite, file, &calc, /*ino*/0);
  37620. + if (!err && try_trunc
  37621. + && au_test_fs_trunc_xino(au_br_sb(br)))
  37622. + xino_try_trunc(sb, br);
  37623. + }
  37624. +}
  37625. +
  37626. +/* ---------------------------------------------------------------------- */
  37627. +
  37628. +static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
  37629. +{
  37630. + int found, total, i;
  37631. +
  37632. + found = -1;
  37633. + total = xi->xi_nondir.total;
  37634. + for (i = 0; i < total; i++) {
  37635. + if (xi->xi_nondir.array[i] != h_ino)
  37636. + continue;
  37637. + found = i;
  37638. + break;
  37639. + }
  37640. +
  37641. + return found;
  37642. +}
  37643. +
  37644. +static int au_xinondir_expand(struct au_xino *xi)
  37645. +{
  37646. + int err, sz;
  37647. + ino_t *p;
  37648. +
  37649. + BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
  37650. +
  37651. + err = -ENOMEM;
  37652. + sz = xi->xi_nondir.total * sizeof(ino_t);
  37653. + if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
  37654. + goto out;
  37655. + p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
  37656. + /*may_shrink*/0);
  37657. + if (p) {
  37658. + xi->xi_nondir.array = p;
  37659. + xi->xi_nondir.total <<= 1;
  37660. + AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
  37661. + err = 0;
  37662. + }
  37663. +
  37664. +out:
  37665. + return err;
  37666. +}
  37667. +
  37668. +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
  37669. + ino_t h_ino, int idx)
  37670. +{
  37671. + struct au_xino *xi;
  37672. +
  37673. + AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
  37674. + xi = au_sbr(sb, bindex)->br_xino;
  37675. + AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
  37676. +
  37677. + spin_lock(&xi->xi_nondir.spin);
  37678. + AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
  37679. + xi->xi_nondir.array[idx] = 0;
  37680. + spin_unlock(&xi->xi_nondir.spin);
  37681. + wake_up_all(&xi->xi_nondir.wqh);
  37682. +}
  37683. +
  37684. +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
  37685. + int *idx)
  37686. +{
  37687. + int err, found, empty;
  37688. + struct au_xino *xi;
  37689. +
  37690. + err = 0;
  37691. + *idx = -1;
  37692. + if (!au_opt_test(au_mntflags(sb), XINO))
  37693. + goto out; /* no xino */
  37694. +
  37695. + xi = au_sbr(sb, bindex)->br_xino;
  37696. +
  37697. +again:
  37698. + spin_lock(&xi->xi_nondir.spin);
  37699. + found = au_xinondir_find(xi, h_ino);
  37700. + if (found == -1) {
  37701. + empty = au_xinondir_find(xi, /*h_ino*/0);
  37702. + if (empty == -1) {
  37703. + empty = xi->xi_nondir.total;
  37704. + err = au_xinondir_expand(xi);
  37705. + if (unlikely(err))
  37706. + goto out_unlock;
  37707. + }
  37708. + xi->xi_nondir.array[empty] = h_ino;
  37709. + *idx = empty;
  37710. + } else {
  37711. + spin_unlock(&xi->xi_nondir.spin);
  37712. + wait_event(xi->xi_nondir.wqh,
  37713. + xi->xi_nondir.array[found] != h_ino);
  37714. + goto again;
  37715. + }
  37716. +
  37717. +out_unlock:
  37718. + spin_unlock(&xi->xi_nondir.spin);
  37719. +out:
  37720. + return err;
  37721. +}
  37722. +
  37723. +/* ---------------------------------------------------------------------- */
  37724. +
  37725. +int au_xino_path(struct seq_file *seq, struct file *file)
  37726. +{
  37727. + int err;
  37728. +
  37729. + err = au_seq_path(seq, &file->f_path);
  37730. + if (unlikely(err))
  37731. + goto out;
  37732. +
  37733. +#define Deleted "\\040(deleted)"
  37734. + seq->count -= sizeof(Deleted) - 1;
  37735. + AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
  37736. + sizeof(Deleted) - 1));
  37737. +#undef Deleted
  37738. +
  37739. +out:
  37740. + return err;
  37741. +}
  37742. diff -Naur null/include/uapi/linux/aufs_type.h linux-5.1/include/uapi/linux/aufs_type.h
  37743. --- /dev/null
  37744. +++ linux-5.1/include/uapi/linux/aufs_type.h 2019-05-22 11:23:07.446140766 +0200
  37745. @@ -0,0 +1,448 @@
  37746. +/* SPDX-License-Identifier: GPL-2.0 */
  37747. +/*
  37748. + * Copyright (C) 2005-2019 Junjiro R. Okajima
  37749. + *
  37750. + * This program, aufs is free software; you can redistribute it and/or modify
  37751. + * it under the terms of the GNU General Public License as published by
  37752. + * the Free Software Foundation; either version 2 of the License, or
  37753. + * (at your option) any later version.
  37754. + *
  37755. + * This program is distributed in the hope that it will be useful,
  37756. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  37757. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  37758. + * GNU General Public License for more details.
  37759. + *
  37760. + * You should have received a copy of the GNU General Public License
  37761. + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  37762. + */
  37763. +
  37764. +#ifndef __AUFS_TYPE_H__
  37765. +#define __AUFS_TYPE_H__
  37766. +
  37767. +#define AUFS_NAME "aufs"
  37768. +
  37769. +#ifdef __KERNEL__
  37770. +/*
  37771. + * define it before including all other headers.
  37772. + * sched.h may use pr_* macros before defining "current", so define the
  37773. + * no-current version first, and re-define later.
  37774. + */
  37775. +#define pr_fmt(fmt) AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
  37776. +#include <linux/sched.h>
  37777. +#undef pr_fmt
  37778. +#define pr_fmt(fmt) \
  37779. + AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
  37780. + (int)sizeof(current->comm), current->comm, current->pid
  37781. +#else
  37782. +#include <stdint.h>
  37783. +#include <sys/types.h>
  37784. +#endif /* __KERNEL__ */
  37785. +
  37786. +#include <linux/limits.h>
  37787. +
  37788. +#define AUFS_VERSION "5.1-20190520"
  37789. +
  37790. +/* todo? move this to linux-2.6.19/include/magic.h */
  37791. +#define AUFS_SUPER_MAGIC ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
  37792. +
  37793. +/* ---------------------------------------------------------------------- */
  37794. +
  37795. +#ifdef CONFIG_AUFS_BRANCH_MAX_127
  37796. +typedef int8_t aufs_bindex_t;
  37797. +#define AUFS_BRANCH_MAX 127
  37798. +#else
  37799. +typedef int16_t aufs_bindex_t;
  37800. +#ifdef CONFIG_AUFS_BRANCH_MAX_511
  37801. +#define AUFS_BRANCH_MAX 511
  37802. +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
  37803. +#define AUFS_BRANCH_MAX 1023
  37804. +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
  37805. +#define AUFS_BRANCH_MAX 32767
  37806. +#endif
  37807. +#endif
  37808. +
  37809. +#ifdef __KERNEL__
  37810. +#ifndef AUFS_BRANCH_MAX
  37811. +#error unknown CONFIG_AUFS_BRANCH_MAX value
  37812. +#endif
  37813. +#endif /* __KERNEL__ */
  37814. +
  37815. +/* ---------------------------------------------------------------------- */
  37816. +
  37817. +#define AUFS_FSTYPE AUFS_NAME
  37818. +
  37819. +#define AUFS_ROOT_INO 2
  37820. +#define AUFS_FIRST_INO 11
  37821. +
  37822. +#define AUFS_WH_PFX ".wh."
  37823. +#define AUFS_WH_PFX_LEN ((int)sizeof(AUFS_WH_PFX) - 1)
  37824. +#define AUFS_WH_TMP_LEN 4
  37825. +/* a limit for rmdir/rename a dir and copyup */
  37826. +#define AUFS_MAX_NAMELEN (NAME_MAX \
  37827. + - AUFS_WH_PFX_LEN * 2 /* doubly whiteouted */\
  37828. + - 1 /* dot */\
  37829. + - AUFS_WH_TMP_LEN) /* hex */
  37830. +#define AUFS_XINO_FNAME "." AUFS_NAME ".xino"
  37831. +#define AUFS_XINO_DEFPATH "/tmp/" AUFS_XINO_FNAME
  37832. +#define AUFS_XINO_DEF_SEC 30 /* seconds */
  37833. +#define AUFS_XINO_DEF_TRUNC 45 /* percentage */
  37834. +#define AUFS_DIRWH_DEF 3
  37835. +#define AUFS_RDCACHE_DEF 10 /* seconds */
  37836. +#define AUFS_RDCACHE_MAX 3600 /* seconds */
  37837. +#define AUFS_RDBLK_DEF 512 /* bytes */
  37838. +#define AUFS_RDHASH_DEF 32
  37839. +#define AUFS_WKQ_NAME AUFS_NAME "d"
  37840. +#define AUFS_MFS_DEF_SEC 30 /* seconds */
  37841. +#define AUFS_MFS_MAX_SEC 3600 /* seconds */
  37842. +#define AUFS_FHSM_CACHE_DEF_SEC 30 /* seconds */
  37843. +#define AUFS_PLINK_WARN 50 /* number of plinks in a single bucket */
  37844. +
  37845. +/* pseudo-link maintenace under /proc */
  37846. +#define AUFS_PLINK_MAINT_NAME "plink_maint"
  37847. +#define AUFS_PLINK_MAINT_DIR "fs/" AUFS_NAME
  37848. +#define AUFS_PLINK_MAINT_PATH AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
  37849. +
  37850. +/* dirren, renamed dir */
  37851. +#define AUFS_DR_INFO_PFX AUFS_WH_PFX ".dr."
  37852. +#define AUFS_DR_BRHINO_NAME AUFS_WH_PFX "hino"
  37853. +/* whiteouted doubly */
  37854. +#define AUFS_WH_DR_INFO_PFX AUFS_WH_PFX AUFS_DR_INFO_PFX
  37855. +#define AUFS_WH_DR_BRHINO AUFS_WH_PFX AUFS_DR_BRHINO_NAME
  37856. +
  37857. +#define AUFS_DIROPQ_NAME AUFS_WH_PFX ".opq" /* whiteouted doubly */
  37858. +#define AUFS_WH_DIROPQ AUFS_WH_PFX AUFS_DIROPQ_NAME
  37859. +
  37860. +#define AUFS_BASE_NAME AUFS_WH_PFX AUFS_NAME
  37861. +#define AUFS_PLINKDIR_NAME AUFS_WH_PFX "plnk"
  37862. +#define AUFS_ORPHDIR_NAME AUFS_WH_PFX "orph"
  37863. +
  37864. +/* doubly whiteouted */
  37865. +#define AUFS_WH_BASE AUFS_WH_PFX AUFS_BASE_NAME
  37866. +#define AUFS_WH_PLINKDIR AUFS_WH_PFX AUFS_PLINKDIR_NAME
  37867. +#define AUFS_WH_ORPHDIR AUFS_WH_PFX AUFS_ORPHDIR_NAME
  37868. +
  37869. +/* branch permissions and attributes */
  37870. +#define AUFS_BRPERM_RW "rw"
  37871. +#define AUFS_BRPERM_RO "ro"
  37872. +#define AUFS_BRPERM_RR "rr"
  37873. +#define AUFS_BRATTR_COO_REG "coo_reg"
  37874. +#define AUFS_BRATTR_COO_ALL "coo_all"
  37875. +#define AUFS_BRATTR_FHSM "fhsm"
  37876. +#define AUFS_BRATTR_UNPIN "unpin"
  37877. +#define AUFS_BRATTR_ICEX "icex"
  37878. +#define AUFS_BRATTR_ICEX_SEC "icexsec"
  37879. +#define AUFS_BRATTR_ICEX_SYS "icexsys"
  37880. +#define AUFS_BRATTR_ICEX_TR "icextr"
  37881. +#define AUFS_BRATTR_ICEX_USR "icexusr"
  37882. +#define AUFS_BRATTR_ICEX_OTH "icexoth"
  37883. +#define AUFS_BRRATTR_WH "wh"
  37884. +#define AUFS_BRWATTR_NLWH "nolwh"
  37885. +#define AUFS_BRWATTR_MOO "moo"
  37886. +
  37887. +#define AuBrPerm_RW 1 /* writable, hardlinkable wh */
  37888. +#define AuBrPerm_RO (1 << 1) /* readonly */
  37889. +#define AuBrPerm_RR (1 << 2) /* natively readonly */
  37890. +#define AuBrPerm_Mask (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
  37891. +
  37892. +#define AuBrAttr_COO_REG (1 << 3) /* copy-up on open */
  37893. +#define AuBrAttr_COO_ALL (1 << 4)
  37894. +#define AuBrAttr_COO_Mask (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
  37895. +
  37896. +#define AuBrAttr_FHSM (1 << 5) /* file-based hsm */
  37897. +#define AuBrAttr_UNPIN (1 << 6) /* rename-able top dir of
  37898. + branch. meaningless since
  37899. + linux-3.18-rc1 */
  37900. +
  37901. +/* ignore error in copying XATTR */
  37902. +#define AuBrAttr_ICEX_SEC (1 << 7)
  37903. +#define AuBrAttr_ICEX_SYS (1 << 8)
  37904. +#define AuBrAttr_ICEX_TR (1 << 9)
  37905. +#define AuBrAttr_ICEX_USR (1 << 10)
  37906. +#define AuBrAttr_ICEX_OTH (1 << 11)
  37907. +#define AuBrAttr_ICEX (AuBrAttr_ICEX_SEC \
  37908. + | AuBrAttr_ICEX_SYS \
  37909. + | AuBrAttr_ICEX_TR \
  37910. + | AuBrAttr_ICEX_USR \
  37911. + | AuBrAttr_ICEX_OTH)
  37912. +
  37913. +#define AuBrRAttr_WH (1 << 12) /* whiteout-able */
  37914. +#define AuBrRAttr_Mask AuBrRAttr_WH
  37915. +
  37916. +#define AuBrWAttr_NoLinkWH (1 << 13) /* un-hardlinkable whiteouts */
  37917. +#define AuBrWAttr_MOO (1 << 14) /* move-up on open */
  37918. +#define AuBrWAttr_Mask (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
  37919. +
  37920. +#define AuBrAttr_CMOO_Mask (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
  37921. +
  37922. +/* #warning test userspace */
  37923. +#ifdef __KERNEL__
  37924. +#ifndef CONFIG_AUFS_FHSM
  37925. +#undef AuBrAttr_FHSM
  37926. +#define AuBrAttr_FHSM 0
  37927. +#endif
  37928. +#ifndef CONFIG_AUFS_XATTR
  37929. +#undef AuBrAttr_ICEX
  37930. +#define AuBrAttr_ICEX 0
  37931. +#undef AuBrAttr_ICEX_SEC
  37932. +#define AuBrAttr_ICEX_SEC 0
  37933. +#undef AuBrAttr_ICEX_SYS
  37934. +#define AuBrAttr_ICEX_SYS 0
  37935. +#undef AuBrAttr_ICEX_TR
  37936. +#define AuBrAttr_ICEX_TR 0
  37937. +#undef AuBrAttr_ICEX_USR
  37938. +#define AuBrAttr_ICEX_USR 0
  37939. +#undef AuBrAttr_ICEX_OTH
  37940. +#define AuBrAttr_ICEX_OTH 0
  37941. +#endif
  37942. +#endif
  37943. +
  37944. +/* the longest combination */
  37945. +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
  37946. +#define AuBrPermStrSz sizeof(AUFS_BRPERM_RW \
  37947. + "+" AUFS_BRATTR_COO_REG \
  37948. + "+" AUFS_BRATTR_FHSM \
  37949. + "+" AUFS_BRATTR_UNPIN \
  37950. + "+" AUFS_BRATTR_ICEX_SEC \
  37951. + "+" AUFS_BRATTR_ICEX_SYS \
  37952. + "+" AUFS_BRATTR_ICEX_USR \
  37953. + "+" AUFS_BRATTR_ICEX_OTH \
  37954. + "+" AUFS_BRWATTR_NLWH)
  37955. +
  37956. +typedef struct {
  37957. + char a[AuBrPermStrSz];
  37958. +} au_br_perm_str_t;
  37959. +
  37960. +static inline int au_br_writable(int brperm)
  37961. +{
  37962. + return brperm & AuBrPerm_RW;
  37963. +}
  37964. +
  37965. +static inline int au_br_whable(int brperm)
  37966. +{
  37967. + return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
  37968. +}
  37969. +
  37970. +static inline int au_br_wh_linkable(int brperm)
  37971. +{
  37972. + return !(brperm & AuBrWAttr_NoLinkWH);
  37973. +}
  37974. +
  37975. +static inline int au_br_cmoo(int brperm)
  37976. +{
  37977. + return brperm & AuBrAttr_CMOO_Mask;
  37978. +}
  37979. +
  37980. +static inline int au_br_fhsm(int brperm)
  37981. +{
  37982. + return brperm & AuBrAttr_FHSM;
  37983. +}
  37984. +
  37985. +/* ---------------------------------------------------------------------- */
  37986. +
  37987. +/* ioctl */
  37988. +enum {
  37989. + /* readdir in userspace */
  37990. + AuCtl_RDU,
  37991. + AuCtl_RDU_INO,
  37992. +
  37993. + AuCtl_WBR_FD, /* pathconf wrapper */
  37994. + AuCtl_IBUSY, /* busy inode */
  37995. + AuCtl_MVDOWN, /* move-down */
  37996. + AuCtl_BR, /* info about branches */
  37997. + AuCtl_FHSM_FD /* connection for fhsm */
  37998. +};
  37999. +
  38000. +/* borrowed from linux/include/linux/kernel.h */
  38001. +#ifndef ALIGN
  38002. +#define ALIGN(x, a) __ALIGN_MASK(x, (typeof(x))(a)-1)
  38003. +#define __ALIGN_MASK(x, mask) (((x)+(mask))&~(mask))
  38004. +#endif
  38005. +
  38006. +/* borrowed from linux/include/linux/compiler-gcc3.h */
  38007. +#ifndef __aligned
  38008. +#define __aligned(x) __attribute__((aligned(x)))
  38009. +#endif
  38010. +
  38011. +#ifdef __KERNEL__
  38012. +#ifndef __packed
  38013. +#define __packed __attribute__((packed))
  38014. +#endif
  38015. +#endif
  38016. +
  38017. +struct au_rdu_cookie {
  38018. + uint64_t h_pos;
  38019. + int16_t bindex;
  38020. + uint8_t flags;
  38021. + uint8_t pad;
  38022. + uint32_t generation;
  38023. +} __aligned(8);
  38024. +
  38025. +struct au_rdu_ent {
  38026. + uint64_t ino;
  38027. + int16_t bindex;
  38028. + uint8_t type;
  38029. + uint8_t nlen;
  38030. + uint8_t wh;
  38031. + char name[0];
  38032. +} __aligned(8);
  38033. +
  38034. +static inline int au_rdu_len(int nlen)
  38035. +{
  38036. + /* include the terminating NULL */
  38037. + return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
  38038. + sizeof(uint64_t));
  38039. +}
  38040. +
  38041. +union au_rdu_ent_ul {
  38042. + struct au_rdu_ent __user *e;
  38043. + uint64_t ul;
  38044. +};
  38045. +
  38046. +enum {
  38047. + AufsCtlRduV_SZ,
  38048. + AufsCtlRduV_End
  38049. +};
  38050. +
  38051. +struct aufs_rdu {
  38052. + /* input */
  38053. + union {
  38054. + uint64_t sz; /* AuCtl_RDU */
  38055. + uint64_t nent; /* AuCtl_RDU_INO */
  38056. + };
  38057. + union au_rdu_ent_ul ent;
  38058. + uint16_t verify[AufsCtlRduV_End];
  38059. +
  38060. + /* input/output */
  38061. + uint32_t blk;
  38062. +
  38063. + /* output */
  38064. + union au_rdu_ent_ul tail;
  38065. + /* number of entries which were added in a single call */
  38066. + uint64_t rent;
  38067. + uint8_t full;
  38068. + uint8_t shwh;
  38069. +
  38070. + struct au_rdu_cookie cookie;
  38071. +} __aligned(8);
  38072. +
  38073. +/* ---------------------------------------------------------------------- */
  38074. +
  38075. +/* dirren. the branch is identified by the filename who contains this */
  38076. +struct au_drinfo {
  38077. + uint64_t ino;
  38078. + union {
  38079. + uint8_t oldnamelen;
  38080. + uint64_t _padding;
  38081. + };
  38082. + uint8_t oldname[0];
  38083. +} __aligned(8);
  38084. +
  38085. +struct au_drinfo_fdata {
  38086. + uint32_t magic;
  38087. + struct au_drinfo drinfo;
  38088. +} __aligned(8);
  38089. +
  38090. +#define AUFS_DRINFO_MAGIC_V1 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
  38091. +/* future */
  38092. +#define AUFS_DRINFO_MAGIC_V2 ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
  38093. +
  38094. +/* ---------------------------------------------------------------------- */
  38095. +
  38096. +struct aufs_wbr_fd {
  38097. + uint32_t oflags;
  38098. + int16_t brid;
  38099. +} __aligned(8);
  38100. +
  38101. +/* ---------------------------------------------------------------------- */
  38102. +
  38103. +struct aufs_ibusy {
  38104. + uint64_t ino, h_ino;
  38105. + int16_t bindex;
  38106. +} __aligned(8);
  38107. +
  38108. +/* ---------------------------------------------------------------------- */
  38109. +
  38110. +/* error code for move-down */
  38111. +/* the actual message strings are implemented in aufs-util.git */
  38112. +enum {
  38113. + EAU_MVDOWN_OPAQUE = 1,
  38114. + EAU_MVDOWN_WHITEOUT,
  38115. + EAU_MVDOWN_UPPER,
  38116. + EAU_MVDOWN_BOTTOM,
  38117. + EAU_MVDOWN_NOUPPER,
  38118. + EAU_MVDOWN_NOLOWERBR,
  38119. + EAU_Last
  38120. +};
  38121. +
  38122. +/* flags for move-down */
  38123. +#define AUFS_MVDOWN_DMSG 1
  38124. +#define AUFS_MVDOWN_OWLOWER (1 << 1) /* overwrite lower */
  38125. +#define AUFS_MVDOWN_KUPPER (1 << 2) /* keep upper */
  38126. +#define AUFS_MVDOWN_ROLOWER (1 << 3) /* do even if lower is RO */
  38127. +#define AUFS_MVDOWN_ROLOWER_R (1 << 4) /* did on lower RO */
  38128. +#define AUFS_MVDOWN_ROUPPER (1 << 5) /* do even if upper is RO */
  38129. +#define AUFS_MVDOWN_ROUPPER_R (1 << 6) /* did on upper RO */
  38130. +#define AUFS_MVDOWN_BRID_UPPER (1 << 7) /* upper brid */
  38131. +#define AUFS_MVDOWN_BRID_LOWER (1 << 8) /* lower brid */
  38132. +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9) /* find fhsm attr for lower */
  38133. +#define AUFS_MVDOWN_STFS (1 << 10) /* req. stfs */
  38134. +#define AUFS_MVDOWN_STFS_FAILED (1 << 11) /* output: stfs is unusable */
  38135. +#define AUFS_MVDOWN_BOTTOM (1 << 12) /* output: no more lowers */
  38136. +
  38137. +/* index for move-down */
  38138. +enum {
  38139. + AUFS_MVDOWN_UPPER,
  38140. + AUFS_MVDOWN_LOWER,
  38141. + AUFS_MVDOWN_NARRAY
  38142. +};
  38143. +
  38144. +/*
  38145. + * additional info of move-down
  38146. + * number of free blocks and inodes.
  38147. + * subset of struct kstatfs, but smaller and always 64bit.
  38148. + */
  38149. +struct aufs_stfs {
  38150. + uint64_t f_blocks;
  38151. + uint64_t f_bavail;
  38152. + uint64_t f_files;
  38153. + uint64_t f_ffree;
  38154. +};
  38155. +
  38156. +struct aufs_stbr {
  38157. + int16_t brid; /* optional input */
  38158. + int16_t bindex; /* output */
  38159. + struct aufs_stfs stfs; /* output when AUFS_MVDOWN_STFS set */
  38160. +} __aligned(8);
  38161. +
  38162. +struct aufs_mvdown {
  38163. + uint32_t flags; /* input/output */
  38164. + struct aufs_stbr stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
  38165. + int8_t au_errno; /* output */
  38166. +} __aligned(8);
  38167. +
  38168. +/* ---------------------------------------------------------------------- */
  38169. +
  38170. +union aufs_brinfo {
  38171. + /* PATH_MAX may differ between kernel-space and user-space */
  38172. + char _spacer[4096];
  38173. + struct {
  38174. + int16_t id;
  38175. + int perm;
  38176. + char path[0];
  38177. + };
  38178. +} __aligned(8);
  38179. +
  38180. +/* ---------------------------------------------------------------------- */
  38181. +
  38182. +#define AuCtlType 'A'
  38183. +#define AUFS_CTL_RDU _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
  38184. +#define AUFS_CTL_RDU_INO _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
  38185. +#define AUFS_CTL_WBR_FD _IOW(AuCtlType, AuCtl_WBR_FD, \
  38186. + struct aufs_wbr_fd)
  38187. +#define AUFS_CTL_IBUSY _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
  38188. +#define AUFS_CTL_MVDOWN _IOWR(AuCtlType, AuCtl_MVDOWN, \
  38189. + struct aufs_mvdown)
  38190. +#define AUFS_CTL_BRINFO _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
  38191. +#define AUFS_CTL_FHSM_FD _IOW(AuCtlType, AuCtl_FHSM_FD, int)
  38192. +
  38193. +#endif /* __AUFS_TYPE_H__ */